osd: add support for updating secret for default KMS

This commit adds support for updating secret for default KMS.
This is required for encryption key rotation.

Signed-off-by: Rakshith R <rar@redhat.com>
(cherry picked from commit b41fdfbb62)
This commit is contained in:
Rakshith R authored and Mergify committed 2023-03-08 16:37:32 +00:00
1 parent 2381bff45a
commit 801c81555b
2 files changed
+43 -1

No files matched your search

+18
View File
@@ -54,6 +54,24 @@ func (c *Config) storeSecretInKubernetes(pvcName, key string) error {
return nil
}
// updateSecretInKubernetes updates the dmcrypt key in a Kubernetes Secret
func (c *Config) updateSecretInKubernetes(pvcName, key string) error {
secretName := GenerateOSDEncryptionSecretName(pvcName)
secret, err := c.context.Clientset.CoreV1().Secrets(c.ClusterInfo.Namespace).Get(c.ClusterInfo.Context, secretName, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "failed to get secret %q", secretName)
}
secret.StringData = map[string]string{OsdEncryptionSecretNameKeyName: key}
// Update the Kubernetes Secret
_, err = c.context.Clientset.CoreV1().Secrets(c.ClusterInfo.Namespace).Update(c.ClusterInfo.Context, secret, metav1.UpdateOptions{})
if err != nil {
return errors.Wrapf(err, "failed to update ceph osd encryption key for pvc %q", pvcName)
}
return nil
}
// getKubernetesSecret returns key value from secret.
func (c *Config) getKubernetesSecret(pvcName string) (string, error) {
secretName := GenerateOSDEncryptionSecretName(pvcName)
+25 -1
View File
@@ -151,8 +151,16 @@ func (c *Config) PutSecret(secretName, secretValue string) error {
// GetSecret returns an encrypted key from a KMS
func (c *Config) GetSecret(secretName string) (string, error) {
var value string
if c.IsK8s() {
// Retrieve the secret from Kubernetes Secrets
value, err := c.getKubernetesSecret(secretName)
if err != nil {
return "", errors.Wrap(err, "failed to get secret from kubernetes secret")
}
return value, nil
}
if c.IsVault() {
// Store the secret in Vault
// Retrieve the secret from Vault
v, err := InitVault(c.ClusterInfo.Context, c.context, c.ClusterInfo.Namespace, c.clusterSpec.Security.KeyManagementService.ConnectionDetails)
if err != nil {
return "", errors.Wrap(err, "failed to init vault")
@@ -195,6 +203,22 @@ func (c *Config) GetSecret(secretName string) (string, error) {
return value, nil
}
// UpdateSecret updates the encrypted key in a KMS
func (c *Config) UpdateSecret(secretName, secretValue string) error {
// If Kubernetes Secret KMS is selected (default)
if c.IsK8s() {
// Update the secret in Kubernetes Secrets
err := c.updateSecretInKubernetes(secretName, secretValue)
if err != nil {
return errors.Wrap(err, "failed to update secret in kubernetes secret")
}
return nil
}
return errors.Errorf("update secret is not supported for the %q KMS", c.Provider)
}
// DeleteSecret deletes an encrypted key from a KMS
func (c *Config) DeleteSecret(secretName string) error {
if c.IsVault() {