2021-03-04 14:55:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-06-21 07:01:22 +00:00
|
|
|
"context"
|
2021-03-02 17:48:05 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/json"
|
2021-03-04 14:55:41 +00:00
|
|
|
"errors"
|
2021-03-02 17:48:05 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/scrypt"
|
2021-06-21 07:01:22 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-03-04 14:55:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-07-08 14:59:34 +00:00
|
|
|
// Encryption passphrase location in K8s secrets.
|
2021-03-04 14:55:41 +00:00
|
|
|
encryptionPassphraseKey = "encryptionPassphrase"
|
|
|
|
|
2021-07-08 14:59:34 +00:00
|
|
|
// Default KMS type.
|
2021-03-04 14:55:41 +00:00
|
|
|
defaultKMSType = "default"
|
2021-03-02 17:48:05 +00:00
|
|
|
|
|
|
|
// kmsTypeSecretsMetadata is the SecretsKMS with per-volume encryption,
|
|
|
|
// where the DEK is stored in the metadata of the volume itself.
|
|
|
|
kmsTypeSecretsMetadata = "metadata"
|
2021-06-21 07:01:22 +00:00
|
|
|
|
|
|
|
// metadataSecretNameKey contains the key which corresponds to the
|
2021-07-22 06:28:10 +00:00
|
|
|
// kubernetes secret name from where encryptionPassphrase is feteched.
|
2021-06-21 07:01:22 +00:00
|
|
|
metadataSecretNameKey = "secretName"
|
|
|
|
// metadataSecretNamespaceKey contains the key which corresponds to the
|
2021-07-22 06:28:10 +00:00
|
|
|
// kubernetes secret namespace from where encryptionPassphrase is feteched.
|
2021-06-21 07:01:22 +00:00
|
|
|
metadataSecretNamespaceKey = "secretNamespace"
|
2021-03-04 14:55:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecretsKMS is default KMS implementation that means no KMS is in use.
|
|
|
|
type SecretsKMS struct {
|
2021-03-01 15:35:35 +00:00
|
|
|
integratedDEK
|
|
|
|
|
2021-03-04 14:55:41 +00:00
|
|
|
passphrase string
|
|
|
|
}
|
|
|
|
|
|
|
|
// initSecretsKMS initializes a SecretsKMS that uses the passphrase from the
|
|
|
|
// secret that is configured for the StorageClass. This KMS provider uses a
|
|
|
|
// single (LUKS) passhprase for all volumes.
|
|
|
|
func initSecretsKMS(secrets map[string]string) (EncryptionKMS, error) {
|
|
|
|
passphraseValue, ok := secrets[encryptionPassphraseKey]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("missing encryption passphrase in secrets")
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-04 14:55:41 +00:00
|
|
|
return SecretsKMS{passphrase: passphraseValue}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy frees all used resources.
|
|
|
|
func (kms SecretsKMS) Destroy() {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// FetchDEK returns passphrase from Kubernetes secrets.
|
|
|
|
func (kms SecretsKMS) FetchDEK(key string) (string, error) {
|
2021-03-04 14:55:41 +00:00
|
|
|
return kms.passphrase, nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// StoreDEK does nothing, as there is no passphrase per key (volume), so
|
2021-03-04 14:55:41 +00:00
|
|
|
// no need to store is anywhere.
|
2021-02-15 09:24:47 +00:00
|
|
|
func (kms SecretsKMS) StoreDEK(key, value string) error {
|
2021-03-04 14:55:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// RemoveDEK is doing nothing as no new passphrases are saved with
|
2021-03-04 14:55:41 +00:00
|
|
|
// SecretsKMS.
|
2021-02-15 09:24:47 +00:00
|
|
|
func (kms SecretsKMS) RemoveDEK(key string) error {
|
2021-03-04 14:55:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-02 17:48:05 +00:00
|
|
|
|
|
|
|
// SecretsMetadataKMS is a KMS based on the SecretsKMS, but stores the
|
|
|
|
// Data-Encryption-Key (DEK) in the metadata of the volume.
|
|
|
|
type SecretsMetadataKMS struct {
|
|
|
|
SecretsKMS
|
|
|
|
}
|
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
var _ = RegisterKMSProvider(KMSProvider{
|
|
|
|
UniqueID: kmsTypeSecretsMetadata,
|
|
|
|
Initializer: initSecretsMetadataKMS,
|
|
|
|
})
|
|
|
|
|
2021-06-21 07:01:22 +00:00
|
|
|
// initSecretsMetadataKMS initializes a SecretsMetadataKMS that wraps a SecretsKMS,
|
|
|
|
// so that the passphrase from the user provided or StorageClass secrets can be used
|
2021-03-02 17:48:05 +00:00
|
|
|
// for encrypting/decrypting DEKs that are stored in a detached DEKStore.
|
2021-03-18 12:50:38 +00:00
|
|
|
func initSecretsMetadataKMS(args KMSInitializerArgs) (EncryptionKMS, error) {
|
2021-06-21 07:01:22 +00:00
|
|
|
var (
|
|
|
|
smKMS SecretsMetadataKMS
|
|
|
|
encryptionPassphrase string
|
|
|
|
ok bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
encryptionPassphrase, err = smKMS.fetchEncryptionPassphrase(
|
|
|
|
args.Config, args.Tenant)
|
2021-03-02 17:48:05 +00:00
|
|
|
if err != nil {
|
2021-06-21 07:01:22 +00:00
|
|
|
if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// if 'userSecret' option is not specified, fetch encryptionPassphrase
|
|
|
|
// from storageclass secrets.
|
|
|
|
encryptionPassphrase, ok = args.Secrets[encryptionPassphraseKey]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"missing %q in storageclass secret", encryptionPassphraseKey)
|
|
|
|
}
|
2021-03-02 17:48:05 +00:00
|
|
|
}
|
2021-06-21 07:01:22 +00:00
|
|
|
smKMS.SecretsKMS = SecretsKMS{passphrase: encryptionPassphrase}
|
2021-03-02 17:48:05 +00:00
|
|
|
|
2021-06-21 07:01:22 +00:00
|
|
|
return smKMS, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchEncryptionPassphrase fetches encryptionPassphrase from user provided secret.
|
|
|
|
func (kms SecretsMetadataKMS) fetchEncryptionPassphrase(
|
|
|
|
config map[string]interface{},
|
|
|
|
defaultNamespace string) (string, error) {
|
|
|
|
var (
|
|
|
|
secretName string
|
|
|
|
secretNamespace string
|
|
|
|
)
|
|
|
|
|
|
|
|
err := setConfigString(&secretName, config, metadataSecretNameKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setConfigString(&secretNamespace, config, metadataSecretNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// if 'secretNamespace' option is not specified, defaults to namespace in
|
|
|
|
// which PVC was created
|
|
|
|
secretNamespace = defaultNamespace
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:18:37 +00:00
|
|
|
c, err := NewK8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("can not get Secret %s/%s, failed to "+
|
|
|
|
"connect to Kubernetes: %w", secretNamespace, secretName, err)
|
|
|
|
}
|
|
|
|
|
2021-06-21 07:01:22 +00:00
|
|
|
secret, err := c.CoreV1().Secrets(secretNamespace).Get(context.TODO(),
|
|
|
|
secretName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get Secret %s/%s: %w",
|
|
|
|
secretNamespace, secretName, err)
|
2021-03-02 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 07:01:22 +00:00
|
|
|
passphraseValue, ok := secret.Data[encryptionPassphraseKey]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("missing %q in Secret %s/%s",
|
|
|
|
encryptionPassphraseKey, secretNamespace, secretName)
|
|
|
|
}
|
2021-03-02 17:48:05 +00:00
|
|
|
|
2021-06-21 07:01:22 +00:00
|
|
|
return string(passphraseValue), nil
|
2021-03-02 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy frees all used resources.
|
|
|
|
func (kms SecretsMetadataKMS) Destroy() {
|
|
|
|
kms.SecretsKMS.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kms SecretsMetadataKMS) requiresDEKStore() DEKStoreType {
|
|
|
|
return DEKStoreMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptedMetedataDEK contains the encrypted DEK and the Nonce that was used
|
|
|
|
// during encryption. This structure is stored (in JSON format) in the DEKStore
|
|
|
|
// that is linked to this KMS provider.
|
|
|
|
type encryptedMetedataDEK struct {
|
|
|
|
// DEK is the encrypted data-encryption-key for the volume.
|
|
|
|
DEK []byte `json:"dek"`
|
|
|
|
// Nonce is a random byte slice to guarantee the uniqueness of the
|
|
|
|
// encrypted DEK.
|
|
|
|
Nonce []byte `json:"nonce"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptDEK encrypts the plainDEK with a key derived from the passphrase from
|
|
|
|
// the SecretsKMS and the volumeID.
|
|
|
|
// The resulting encryptedDEK contains a JSON with the encrypted DEK and the
|
|
|
|
// nonce that was used for encrypting.
|
|
|
|
func (kms SecretsMetadataKMS) EncryptDEK(volumeID, plainDEK string) (string, error) {
|
|
|
|
// use the passphrase from the SecretsKMS
|
|
|
|
passphrase, err := kms.SecretsKMS.FetchDEK(volumeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get passphrase: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aead, err := generateCipher(passphrase, volumeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate cipher: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
emd := encryptedMetedataDEK{}
|
|
|
|
emd.Nonce, err = generateNonce(aead.NonceSize())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to generated nonce: %w", err)
|
|
|
|
}
|
|
|
|
emd.DEK = aead.Seal(nil, emd.Nonce, []byte(plainDEK), nil)
|
|
|
|
|
|
|
|
emdData, err := json.Marshal(&emd)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to convert "+
|
|
|
|
"encryptedMetedataDEK to JSON: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(emdData), nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:49:37 +00:00
|
|
|
// DecryptDEK takes the JSON formatted `encryptedMetadataDEK` contents, and it
|
|
|
|
// fetches SecretsKMS passphrase to decrypt the DEK.
|
2021-03-02 17:48:05 +00:00
|
|
|
func (kms SecretsMetadataKMS) DecryptDEK(volumeID, encryptedDEK string) (string, error) {
|
|
|
|
// use the passphrase from the SecretsKMS
|
|
|
|
passphrase, err := kms.SecretsKMS.FetchDEK(volumeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get passphrase: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aead, err := generateCipher(passphrase, volumeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate cipher: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
emd := encryptedMetedataDEK{}
|
|
|
|
err = json.Unmarshal([]byte(encryptedDEK), &emd)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to convert data to "+
|
|
|
|
"encryptedMetedataDEK: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dek, err := aead.Open(nil, emd.Nonce, emd.DEK, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to decrypt DEK: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(dek), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateCipher returns a AEAD cipher based on a passphrase and salt
|
|
|
|
// (volumeID). The cipher can then be used to encrypt/decrypt the DEK.
|
|
|
|
func generateCipher(passphrase, salt string) (cipher.AEAD, error) {
|
|
|
|
key, err := scrypt.Key([]byte(passphrase), []byte(salt), 32768, 8, 1, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blockCipher, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aead, err := cipher.NewGCM(blockCipher)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-02 17:48:05 +00:00
|
|
|
return aead, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateNonce returns a byte slice with random contents.
|
|
|
|
func generateNonce(size int) ([]byte, error) {
|
|
|
|
nonce := make([]byte, size)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonce, nil
|
|
|
|
}
|