2021-03-22 15:34:40 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
package kms
|
2021-03-22 15:34:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-08-26 11:15:47 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/k8s"
|
|
|
|
|
2021-03-22 15:34:40 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
awsCreds "github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
awsSession "github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
awsKMS "github.com/aws/aws-sdk-go/service/kms"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
kmsTypeAWSMetadata = "aws-metadata"
|
|
|
|
|
|
|
|
// awsMetadataDefaultSecretsName is the default name of the Kubernetes Secret
|
|
|
|
// that contains the credentials to access the Amazon KMS. The name of
|
|
|
|
// the Secret can be configured by setting the `KMS_SECRET_NAME`
|
|
|
|
// option.
|
|
|
|
//
|
|
|
|
// #nosec:G101, value not credential, just references token.
|
|
|
|
awsMetadataDefaultSecretsName = "ceph-csi-aws-credentials"
|
|
|
|
|
|
|
|
// awsSecretNameKey contains the name of the Kubernetes Secret that has
|
|
|
|
// the credentials to access the Amazon KMS.
|
|
|
|
//
|
|
|
|
// #nosec:G101, no hardcoded secret, this is a configuration key.
|
|
|
|
awsSecretNameKey = "KMS_SECRET_NAME"
|
|
|
|
awsRegionKey = "AWS_REGION"
|
|
|
|
|
|
|
|
// The following options are part of the Kubernetes Secrets.
|
|
|
|
//
|
|
|
|
// #nosec:G101, no hardcoded secrets, only configuration keys.
|
|
|
|
awsAccessKey = "AWS_ACCESS_KEY_ID"
|
2021-07-08 14:59:34 +00:00
|
|
|
// #nosec:G101.
|
2021-03-22 15:34:40 +00:00
|
|
|
awsSecretAccessKey = "AWS_SECRET_ACCESS_KEY"
|
2021-07-08 14:59:34 +00:00
|
|
|
// #nosec:G101.
|
2021-03-22 15:34:40 +00:00
|
|
|
awsSessionToken = "AWS_SESSION_TOKEN"
|
|
|
|
awsCMK = "AWS_CMK_ARN"
|
|
|
|
)
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
var _ = RegisterProvider(Provider{
|
2021-03-22 15:34:40 +00:00
|
|
|
UniqueID: kmsTypeAWSMetadata,
|
|
|
|
Initializer: initAWSMetadataKMS,
|
|
|
|
})
|
|
|
|
|
2022-01-24 12:54:33 +00:00
|
|
|
type awsMetadataKMS struct {
|
2021-03-22 15:34:40 +00:00
|
|
|
// basic options to get the secret
|
|
|
|
namespace string
|
|
|
|
secretName string
|
|
|
|
|
|
|
|
// standard AWS configuration options
|
|
|
|
region string
|
|
|
|
secretAccessKey string
|
|
|
|
accessKey string
|
|
|
|
sessionToken string
|
|
|
|
cmk string
|
|
|
|
}
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
func initAWSMetadataKMS(args ProviderInitArgs) (EncryptionKMS, error) {
|
2022-01-24 12:54:33 +00:00
|
|
|
kms := &awsMetadataKMS{
|
2021-03-22 15:34:40 +00:00
|
|
|
namespace: args.Namespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
// required options for further configuration (getting secrets)
|
|
|
|
err := setConfigString(&kms.secretName, args.Config, awsSecretNameKey)
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return nil, err
|
|
|
|
} else if errors.Is(err, errConfigOptionMissing) {
|
|
|
|
kms.secretName = awsMetadataDefaultSecretsName
|
|
|
|
}
|
|
|
|
err = setConfigString(&kms.region, args.Config, awsRegionKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-11 05:36:59 +00:00
|
|
|
// read the Kubernetes Secret with credentials
|
2021-03-22 15:34:40 +00:00
|
|
|
secrets, err := kms.getSecrets()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get secrets for %T: %w", kms,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setConfigString(&kms.accessKey, secrets, awsAccessKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = setConfigString(&kms.secretAccessKey, secrets,
|
|
|
|
awsSecretAccessKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// awsSessionToken is optional
|
|
|
|
err = setConfigString(&kms.sessionToken, secrets, awsSessionToken)
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = setConfigString(&kms.cmk, secrets, awsCMK)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return kms, nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:54:33 +00:00
|
|
|
func (kms *awsMetadataKMS) getSecrets() (map[string]interface{}, error) {
|
2021-08-31 12:18:37 +00:00
|
|
|
c, err := k8s.NewK8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect to Kubernetes to "+
|
|
|
|
"get Secret %s/%s: %w", kms.namespace, kms.secretName, err)
|
|
|
|
}
|
|
|
|
|
2021-03-22 15:34:40 +00:00
|
|
|
secret, err := c.CoreV1().Secrets(kms.namespace).Get(context.TODO(),
|
|
|
|
kms.secretName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get Secret %s/%s: %w",
|
|
|
|
kms.namespace, kms.secretName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := make(map[string]interface{})
|
|
|
|
|
|
|
|
for k, v := range secret.Data {
|
|
|
|
switch k {
|
|
|
|
case awsSecretAccessKey, awsAccessKey, awsSessionToken, awsCMK:
|
|
|
|
config[k] = string(v)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported option for KMS "+
|
|
|
|
"provider %q: %s", kmsTypeAWSMetadata, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:54:33 +00:00
|
|
|
func (kms *awsMetadataKMS) Destroy() {
|
2021-03-22 15:34:40 +00:00
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
// RequiresDEKStore indicates that the DEKs should get stored in the metadata
|
2021-03-22 15:34:40 +00:00
|
|
|
// of the volumes. This Amazon KMS provider does not support storing DEKs in
|
|
|
|
// AWS as that adds additional costs.
|
2022-01-24 12:54:33 +00:00
|
|
|
func (kms *awsMetadataKMS) RequiresDEKStore() DEKStoreType {
|
2021-03-22 15:34:40 +00:00
|
|
|
return DEKStoreMetadata
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:54:33 +00:00
|
|
|
func (kms *awsMetadataKMS) getService() (*awsKMS.KMS, error) {
|
2021-03-22 15:34:40 +00:00
|
|
|
creds := awsCreds.NewStaticCredentials(kms.accessKey,
|
|
|
|
kms.secretAccessKey, kms.sessionToken)
|
|
|
|
|
|
|
|
sess, err := awsSession.NewSessionWithOptions(awsSession.Options{
|
|
|
|
SharedConfigState: awsSession.SharedConfigDisable,
|
|
|
|
Config: aws.Config{
|
|
|
|
Credentials: creds,
|
|
|
|
Region: aws.String(kms.region),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create AWS session: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return awsKMS.New(sess), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptDEK uses the Amazon KMS and the configured CMK to encrypt the DEK.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (kms *awsMetadataKMS) EncryptDEK(ctx context.Context, volumeID, plainDEK string) (string, error) {
|
2021-03-22 15:34:40 +00:00
|
|
|
svc, err := kms.getService()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get KMS service: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := svc.Encrypt(&awsKMS.EncryptInput{
|
|
|
|
KeyId: aws.String(kms.cmk),
|
|
|
|
Plaintext: []byte(plainDEK),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to encrypt DEK: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// base64 encode the encrypted DEK, so that storing it should not have
|
|
|
|
// issues
|
2022-06-01 10:17:19 +00:00
|
|
|
encryptedDEK := base64.StdEncoding.EncodeToString(result.CiphertextBlob)
|
2021-03-22 15:34:40 +00:00
|
|
|
|
|
|
|
return encryptedDEK, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptDEK uses the Amazon KMS and the configured CMK to decrypt the DEK.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (kms *awsMetadataKMS) DecryptDEK(ctx context.Context, volumeID, encryptedDEK string) (string, error) {
|
2021-03-22 15:34:40 +00:00
|
|
|
svc, err := kms.getService()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get KMS service: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ciphertextBlob, err := base64.StdEncoding.DecodeString(encryptedDEK)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to decode base64 cipher: %w",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := svc.Decrypt(&awsKMS.DecryptInput{
|
|
|
|
CiphertextBlob: ciphertextBlob,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to decrypt DEK: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(result.Plaintext), nil
|
|
|
|
}
|
2022-03-11 18:51:18 +00:00
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
func (kms *awsMetadataKMS) GetSecret(ctx context.Context, volumeID string) (string, error) {
|
2022-03-11 18:51:18 +00:00
|
|
|
return "", ErrGetSecretUnsupported
|
|
|
|
}
|