2018-08-31 13:39:02 -06:00
|
|
|
/*
|
|
|
|
|
Copyright 2018 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-07-01 22:46:39 -06:00
|
|
|
package client
|
2018-08-31 13:39:02 -06:00
|
|
|
|
|
|
|
|
import (
|
2020-07-10 16:40:16 -06:00
|
|
|
"encoding/base64"
|
2018-08-31 13:39:02 -06:00
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2019-10-08 12:51:36 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-08-31 13:39:02 -06:00
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// AdminKeyringTemplate is a string template of Ceph keyring settings which allow connection
|
|
|
|
|
// as admin. The key value must be filled in by the admin auth key for the cluster.
|
|
|
|
|
AdminKeyringTemplate = `
|
2019-07-03 15:42:13 +08:00
|
|
|
[client.admin]
|
|
|
|
|
key = %s
|
|
|
|
|
caps mds = "allow *"
|
|
|
|
|
caps mon = "allow *"
|
|
|
|
|
caps osd = "allow *"
|
|
|
|
|
caps mgr = "allow *"
|
|
|
|
|
`
|
2020-04-09 17:03:45 +02:00
|
|
|
|
2020-06-24 22:42:38 -06:00
|
|
|
// UserKeyringTemplate is a string template of Ceph keyring settings which allow connection.
|
|
|
|
|
UserKeyringTemplate = `
|
2020-04-09 17:03:45 +02:00
|
|
|
[%s]
|
|
|
|
|
key = %s
|
|
|
|
|
`
|
2018-08-31 13:39:02 -06:00
|
|
|
)
|
|
|
|
|
|
2020-06-24 22:42:38 -06:00
|
|
|
// CephKeyring returns the filled-out user keyring
|
|
|
|
|
func CephKeyring(cred CephCred) string {
|
2020-07-01 22:46:39 -06:00
|
|
|
if cred.Username == AdminUsername {
|
2020-06-24 22:42:38 -06:00
|
|
|
return fmt.Sprintf(AdminKeyringTemplate, cred.Secret)
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf(UserKeyringTemplate, cred.Username, cred.Secret)
|
2020-04-09 17:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-31 13:39:02 -06:00
|
|
|
// CreateKeyring creates a keyring for access to the cluster with the desired set of privileges
|
|
|
|
|
// and writes it to disk at the keyring path
|
2020-07-10 16:40:16 -06:00
|
|
|
func CreateKeyring(context *clusterd.Context, clusterInfo *ClusterInfo, username, keyringPath string, access []string, generateContents func(string) string) error {
|
2018-08-31 13:39:02 -06:00
|
|
|
// get-or-create-key for the user account
|
2020-07-10 16:40:16 -06:00
|
|
|
key, err := AuthGetOrCreateKey(context, clusterInfo, username, access)
|
2018-08-31 13:39:02 -06:00
|
|
|
if err != nil {
|
2019-10-08 12:51:36 +02:00
|
|
|
return errors.Wrapf(err, "failed to get or create auth key for %s", username)
|
2018-08-31 13:39:02 -06:00
|
|
|
}
|
|
|
|
|
|
2025-08-06 13:24:33 -06:00
|
|
|
contents := generateContents(key)
|
|
|
|
|
return WriteKeyring(keyringPath, contents)
|
2018-08-31 13:39:02 -06:00
|
|
|
}
|
|
|
|
|
|
2025-08-06 13:24:33 -06:00
|
|
|
// Writes the keyring to disk, creating parent dirs as necessary
|
|
|
|
|
func WriteKeyring(keyringPath, keyring string) error {
|
2018-08-31 13:39:02 -06:00
|
|
|
// save the keyring to the given path
|
2025-03-13 15:24:58 -07:00
|
|
|
if err := os.MkdirAll(filepath.Dir(keyringPath), 0o700); err != nil {
|
2019-10-08 12:51:36 +02:00
|
|
|
return errors.Wrapf(err, "failed to create keyring directory for %s", keyringPath)
|
2018-08-31 13:39:02 -06:00
|
|
|
}
|
2025-03-13 15:24:58 -07:00
|
|
|
if err := os.WriteFile(keyringPath, []byte(keyring), 0o600); err != nil {
|
2019-10-08 12:51:36 +02:00
|
|
|
return errors.Wrapf(err, "failed to write monitor keyring to %s", keyringPath)
|
2018-08-31 13:39:02 -06:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-07-10 16:40:16 -06:00
|
|
|
|
|
|
|
|
// IsKeyringBase64Encoded returns whether the keyring is valid
|
|
|
|
|
func IsKeyringBase64Encoded(keyring string) bool {
|
|
|
|
|
// If the keyring is not base64 we fail
|
|
|
|
|
_, err := base64.StdEncoding.DecodeString(keyring)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf("key is not base64 encoded. %v", err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|