2019-12-13 11:41:32 +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 (
|
|
|
|
"context"
|
2020-01-29 11:44:45 +00:00
|
|
|
"encoding/base64"
|
2020-02-06 16:23:14 +00:00
|
|
|
"encoding/json"
|
2021-02-12 13:23:17 +00:00
|
|
|
"errors"
|
2019-12-13 11:41:32 +00:00
|
|
|
"fmt"
|
2020-02-06 16:23:14 +00:00
|
|
|
"io/ioutil"
|
2020-12-15 10:07:28 +00:00
|
|
|
"os"
|
2019-12-13 11:41:32 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2020-01-29 11:44:45 +00:00
|
|
|
"crypto/rand"
|
2019-12-13 11:41:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mapperFilePrefix = "luks-rbd-"
|
|
|
|
mapperFilePathPrefix = "/dev/mapper"
|
|
|
|
|
2021-03-04 14:55:41 +00:00
|
|
|
kmsTypeKey = "encryptionKMSType"
|
2020-01-29 11:44:45 +00:00
|
|
|
|
|
|
|
// kmsConfigPath is the location of the vault config file
|
|
|
|
kmsConfigPath = "/etc/ceph-csi-encryption-kms-config/config.json"
|
|
|
|
|
|
|
|
// Passphrase size - 20 bytes is 160 bits to satisfy:
|
|
|
|
// https://tools.ietf.org/html/rfc6749#section-10.10
|
|
|
|
encryptionPassphraseSize = 20
|
2020-12-15 10:07:28 +00:00
|
|
|
// podNamespace ENV should be set in the cephcsi container
|
|
|
|
podNamespace = "POD_NAMESPACE"
|
|
|
|
|
|
|
|
// kmsConfigMapName env to read a ConfigMap by name
|
|
|
|
kmsConfigMapName = "KMS_CONFIGMAP_NAME"
|
|
|
|
|
|
|
|
// defaultConfigMapToRead default ConfigMap name to fetch kms connection details
|
|
|
|
defaultConfigMapToRead = "csi-kms-connection-details"
|
2019-12-13 11:41:32 +00:00
|
|
|
)
|
|
|
|
|
2021-02-12 13:23:17 +00:00
|
|
|
var (
|
|
|
|
// ErrDEKStoreNotFound is an error that is returned when the DEKStore
|
|
|
|
// has not been configured for the volumeID in the KMS instance.
|
|
|
|
ErrDEKStoreNotFound = errors.New("DEKStore not found")
|
|
|
|
|
|
|
|
// ErrDEKStoreNeeded is an indication that gets returned with
|
|
|
|
// NewVolumeEncryption when the KMS does not include support for the
|
|
|
|
// DEKStore interface.
|
|
|
|
ErrDEKStoreNeeded = errors.New("DEKStore required, use " +
|
|
|
|
"VolumeEncryption.SetDEKStore()")
|
|
|
|
)
|
|
|
|
|
2021-02-22 15:25:35 +00:00
|
|
|
type VolumeEncryption struct {
|
|
|
|
KMS EncryptionKMS
|
2021-03-01 15:35:35 +00:00
|
|
|
|
|
|
|
// dekStore that will be used, this can be the EncryptionKMS or a
|
|
|
|
// different object implementing the DEKStore interface.
|
|
|
|
dekStore DEKStore
|
2021-02-22 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 15:35:35 +00:00
|
|
|
// NewVolumeEncryption creates a new instance of VolumeEncryption and
|
|
|
|
// configures the DEKStore. If the KMS does not provide a DEKStore interface,
|
|
|
|
// the VolumeEncryption will be created *and* a ErrDEKStoreNeeded is returned.
|
|
|
|
// Callers that receive a ErrDEKStoreNeeded error, should use
|
|
|
|
// VolumeEncryption.SetDEKStore() to configure an alternative storage for the
|
|
|
|
// DEKs.
|
2021-02-22 15:25:35 +00:00
|
|
|
func NewVolumeEncryption(kms EncryptionKMS) (*VolumeEncryption, error) {
|
|
|
|
ve := &VolumeEncryption{KMS: kms}
|
|
|
|
|
2021-03-01 15:35:35 +00:00
|
|
|
if kms.requiresDEKStore() == DEKStoreIntegrated {
|
|
|
|
dekStore, ok := kms.(DEKStore)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("KMS %T does not implement the "+
|
|
|
|
"DEKStore interface", kms)
|
|
|
|
}
|
|
|
|
|
|
|
|
ve.dekStore = dekStore
|
|
|
|
return ve, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ve, ErrDEKStoreNeeded
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDEKStore sets the DEKStore for this VolumeEncryption instance. It will be
|
|
|
|
// used when StoreNewCryptoPassphrase() or RemoveDEK() is called.
|
|
|
|
func (ve *VolumeEncryption) SetDEKStore(dekStore DEKStore) {
|
|
|
|
ve.dekStore = dekStore
|
2021-02-22 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy frees any resources that the VolumeEncryption instance allocated.
|
|
|
|
func (ve *VolumeEncryption) Destroy() {
|
|
|
|
ve.KMS.Destroy()
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:44:45 +00:00
|
|
|
// EncryptionKMS provides external Key Management System for encryption
|
2020-07-19 12:21:03 +00:00
|
|
|
// passphrases storage.
|
2020-01-29 11:44:45 +00:00
|
|
|
type EncryptionKMS interface {
|
2020-12-03 08:25:52 +00:00
|
|
|
Destroy()
|
2020-01-29 11:44:45 +00:00
|
|
|
GetPassphrase(key string) (string, error)
|
|
|
|
SavePassphrase(key, value string) error
|
|
|
|
DeletePassphrase(key string) error
|
2020-02-06 16:23:14 +00:00
|
|
|
GetID() string
|
2021-02-12 13:23:17 +00:00
|
|
|
|
|
|
|
// requiresDEKStore returns the DEKStoreType that is needed to be
|
|
|
|
// configure for the KMS. Nothing needs to be done when this function
|
|
|
|
// returns DEKStoreIntegrated, otherwise you will need to configure an
|
|
|
|
// alternative storage for the DEKs.
|
|
|
|
requiresDEKStore() DEKStoreType
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEKStoreType describes what DEKStore needs to be configured when using a
|
|
|
|
// particular KMS. A KMS might support different DEKStores depending on its
|
|
|
|
// configuration.
|
|
|
|
type DEKStoreType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DEKStoreIntegrated indicates that the KMS itself supports storing
|
|
|
|
// DEKs.
|
|
|
|
DEKStoreIntegrated = DEKStoreType("")
|
|
|
|
// DEKStoreMetadata indicates that the KMS should be configured to
|
|
|
|
// store the DEK in the metadata of the volume.
|
|
|
|
DEKStoreMetadata = DEKStoreType("metadata")
|
|
|
|
)
|
|
|
|
|
|
|
|
// DEKStore allows KMS instances to implement a modular backend for DEK
|
|
|
|
// storage. This can be used to store the DEK in a different location, in case
|
|
|
|
// the KMS can not store passphrases for volumes.
|
|
|
|
type DEKStore interface {
|
|
|
|
// StoreDEK saves the DEK in the configured store.
|
|
|
|
StoreDEK(volumeID string, dek string) error
|
|
|
|
// FetchDEK reads the DEK from the configured store and returns it.
|
|
|
|
FetchDEK(volumeID string) (string, error)
|
|
|
|
// RemoveDEK deletes the DEK from the configured store.
|
|
|
|
RemoveDEK(volumeID string) error
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// GetKMS returns an instance of Key Management System.
|
2020-12-02 14:44:16 +00:00
|
|
|
//
|
|
|
|
// - tenant is the owner of the Volume, used to fetch the Vault Token from the
|
|
|
|
// Kubernetes Namespace where the PVC lives
|
|
|
|
// - kmsID is the service name of the KMS configuration
|
|
|
|
// - secrets contain additional details, like TLS certificates to connect to
|
|
|
|
// the KMS
|
|
|
|
func GetKMS(tenant, kmsID string, secrets map[string]string) (EncryptionKMS, error) {
|
2020-02-06 16:23:14 +00:00
|
|
|
if kmsID == "" || kmsID == defaultKMSType {
|
2020-01-29 11:44:45 +00:00
|
|
|
return initSecretsKMS(secrets)
|
|
|
|
}
|
2020-12-15 10:07:28 +00:00
|
|
|
var config map[string]interface{}
|
2020-02-06 16:23:14 +00:00
|
|
|
// #nosec
|
|
|
|
content, err := ioutil.ReadFile(kmsConfigPath)
|
|
|
|
if err != nil {
|
2020-12-15 10:07:28 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("failed to read kms configuration from %s: %w",
|
|
|
|
kmsConfigPath, err)
|
|
|
|
}
|
|
|
|
// If the configmap is not mounted to the CSI pods read the configmap
|
|
|
|
// the kubernetes.
|
|
|
|
namespace := os.Getenv(podNamespace)
|
2021-01-28 06:14:24 +00:00
|
|
|
if namespace == "" {
|
2020-12-15 10:07:28 +00:00
|
|
|
return nil, fmt.Errorf("%q is not set", podNamespace)
|
|
|
|
}
|
|
|
|
name := os.Getenv(kmsConfigMapName)
|
2021-01-28 06:14:24 +00:00
|
|
|
if name == "" {
|
2020-12-15 10:07:28 +00:00
|
|
|
name = defaultConfigMapToRead
|
|
|
|
}
|
|
|
|
config, err = getVaultConfiguration(namespace, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read kms configuration from configmap %s in namespace %s: %w",
|
|
|
|
namespace, name, err)
|
|
|
|
}
|
2021-02-03 11:19:03 +00:00
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(content, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse kms configuration: %w", err)
|
|
|
|
}
|
2020-02-06 16:23:14 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 08:30:28 +00:00
|
|
|
kmsConfig, ok := config[kmsID].(map[string]interface{})
|
2020-02-06 16:23:14 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("missing encryption KMS configuration with %s", kmsID)
|
|
|
|
}
|
2020-01-29 11:44:45 +00:00
|
|
|
|
2020-02-06 16:23:14 +00:00
|
|
|
kmsType, ok := kmsConfig[kmsTypeKey]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("encryption KMS configuration for %s is missing KMS type", kmsID)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2020-02-06 16:23:14 +00:00
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
switch kmsType {
|
2020-12-14 10:26:31 +00:00
|
|
|
case kmsTypeVault:
|
2020-02-06 16:23:14 +00:00
|
|
|
return InitVaultKMS(kmsID, kmsConfig, secrets)
|
2020-12-08 10:44:04 +00:00
|
|
|
case kmsTypeVaultTokens:
|
2020-12-15 07:11:28 +00:00
|
|
|
return InitVaultTokensKMS(tenant, kmsID, kmsConfig)
|
2020-02-06 16:23:14 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown encryption KMS type %s", kmsType)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 09:53:25 +00:00
|
|
|
// StoreNewCryptoPassphrase generates a new passphrase and saves it in the KMS.
|
|
|
|
func StoreNewCryptoPassphrase(volumeID string, kms EncryptionKMS) error {
|
|
|
|
passphrase, err := generateNewEncryptionPassphrase()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to generate passphrase for %s: %w", volumeID, err)
|
|
|
|
}
|
|
|
|
err = kms.SavePassphrase(volumeID, passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to save the passphrase for %s: %w", volumeID, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// GetCryptoPassphrase Retrieves passphrase to encrypt volume.
|
2021-02-15 09:53:25 +00:00
|
|
|
func GetCryptoPassphrase(volumeID string, kms EncryptionKMS) (string, error) {
|
2020-01-29 11:44:45 +00:00
|
|
|
passphrase, err := kms.GetPassphrase(volumeID)
|
2021-02-15 09:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2021-02-15 09:53:25 +00:00
|
|
|
return passphrase, nil
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// generateNewEncryptionPassphrase generates a random passphrase for encryption.
|
2020-01-29 11:44:45 +00:00
|
|
|
func generateNewEncryptionPassphrase() (string, error) {
|
|
|
|
bytesPassphrase := make([]byte, encryptionPassphraseSize)
|
|
|
|
_, err := rand.Read(bytesPassphrase)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(bytesPassphrase), nil
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// VolumeMapper returns file name and it's path to where encrypted device should be open.
|
2019-12-13 11:41:32 +00:00
|
|
|
func VolumeMapper(volumeID string) (mapperFile, mapperFilePath string) {
|
|
|
|
mapperFile = mapperFilePrefix + volumeID
|
|
|
|
mapperFilePath = path.Join(mapperFilePathPrefix, mapperFile)
|
|
|
|
return mapperFile, mapperFilePath
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// EncryptVolume encrypts provided device with LUKS.
|
2019-12-13 11:41:32 +00:00
|
|
|
func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
|
2020-07-09 14:48:24 +00:00
|
|
|
DebugLog(ctx, "Encrypting device %s with LUKS", devicePath)
|
2019-12-13 11:41:32 +00:00
|
|
|
if _, _, err := LuksFormat(devicePath, passphrase); err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return fmt.Errorf("failed to encrypt device %s with LUKS: %w", devicePath, err)
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// OpenEncryptedVolume opens volume so that it can be used by the client.
|
2019-12-13 11:41:32 +00:00
|
|
|
func OpenEncryptedVolume(ctx context.Context, devicePath, mapperFile, passphrase string) error {
|
2020-07-09 14:48:24 +00:00
|
|
|
DebugLog(ctx, "Opening device %s with LUKS on %s", devicePath, mapperFile)
|
2019-12-13 11:41:32 +00:00
|
|
|
_, _, err := LuksOpen(devicePath, mapperFile, passphrase)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// CloseEncryptedVolume closes encrypted volume so it can be detached.
|
2019-12-13 11:41:32 +00:00
|
|
|
func CloseEncryptedVolume(ctx context.Context, mapperFile string) error {
|
2020-07-09 14:48:24 +00:00
|
|
|
DebugLog(ctx, "Closing LUKS device %s", mapperFile)
|
2019-12-13 11:41:32 +00:00
|
|
|
_, _, err := LuksClose(mapperFile)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// IsDeviceOpen determines if encrypted device is already open.
|
2019-12-13 11:41:32 +00:00
|
|
|
func IsDeviceOpen(ctx context.Context, device string) (bool, error) {
|
|
|
|
_, mappedFile, err := DeviceEncryptionStatus(ctx, device)
|
|
|
|
return (mappedFile != ""), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeviceEncryptionStatus looks to identify if the passed device is a LUKS mapping
|
|
|
|
// and if so what the device is and the mapper name as used by LUKS.
|
|
|
|
// If not, just returns the original device and an empty string.
|
|
|
|
func DeviceEncryptionStatus(ctx context.Context, devicePath string) (mappedDevice, mapper string, err error) {
|
|
|
|
if !strings.HasPrefix(devicePath, mapperFilePathPrefix) {
|
|
|
|
return devicePath, "", nil
|
|
|
|
}
|
|
|
|
mapPath := strings.TrimPrefix(devicePath, mapperFilePathPrefix+"/")
|
|
|
|
stdout, _, err := LuksStatus(mapPath)
|
|
|
|
if err != nil {
|
2020-07-09 14:48:24 +00:00
|
|
|
DebugLog(ctx, "device %s is not an active LUKS device: %v", devicePath, err)
|
2019-12-13 11:41:32 +00:00
|
|
|
return devicePath, "", nil
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(stdout), "\n")
|
|
|
|
if len(lines) < 1 {
|
|
|
|
return "", "", fmt.Errorf("device encryption status returned no stdout for %s", devicePath)
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(lines[0], " is active.") {
|
|
|
|
// Implies this is not a LUKS device
|
|
|
|
return devicePath, "", nil
|
|
|
|
}
|
|
|
|
for i := 1; i < len(lines); i++ {
|
|
|
|
kv := strings.SplitN(strings.TrimSpace(lines[i]), ":", 2)
|
|
|
|
if len(kv) < 1 {
|
|
|
|
return "", "", fmt.Errorf("device encryption status output for %s is badly formatted: %s",
|
|
|
|
devicePath, lines[i])
|
|
|
|
}
|
|
|
|
if strings.Compare(kv[0], "device") == 0 {
|
|
|
|
return strings.TrimSpace(kv[1]), mapPath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Identified as LUKS, but failed to identify a mapped device
|
|
|
|
return "", "", fmt.Errorf("mapped device not found in path %s", devicePath)
|
|
|
|
}
|