2021-02-15 07:26:35 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-16 15:56:42 +00:00
|
|
|
"errors"
|
2021-02-15 07:26:35 +00:00
|
|
|
"fmt"
|
2023-05-24 10:52:52 +00:00
|
|
|
"strconv"
|
2023-05-25 12:31:30 +00:00
|
|
|
"strings"
|
2021-02-15 07:26:35 +00:00
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
kmsapi "github.com/ceph/ceph-csi/internal/kms"
|
2021-02-15 07:26:35 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2021-02-16 15:56:42 +00:00
|
|
|
|
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
2021-02-15 07:26:35 +00:00
|
|
|
)
|
|
|
|
|
2021-02-16 15:56:42 +00:00
|
|
|
// rbdEncryptionState describes the status of the process where the image is
|
|
|
|
// with respect to being encrypted.
|
2021-02-15 07:26:35 +00:00
|
|
|
type rbdEncryptionState string
|
|
|
|
|
|
|
|
const (
|
2021-02-16 15:56:42 +00:00
|
|
|
// rbdImageEncryptionUnknown means the image is not encrypted, or the
|
|
|
|
// metadata of the image can not be fetched.
|
|
|
|
rbdImageEncryptionUnknown = rbdEncryptionState("")
|
|
|
|
// rbdImageEncrypted is set in the image metadata after the image has
|
|
|
|
// been formatted with cryptsetup. Future usage of the image should
|
|
|
|
// unlock the image before mounting.
|
|
|
|
rbdImageEncrypted = rbdEncryptionState("encrypted")
|
|
|
|
// rbdImageEncryptionPrepared gets set in the image metadata once the
|
|
|
|
// passphrase for the image has been generated and stored in the KMS.
|
|
|
|
// When using the image for the first time, it needs to be encrypted
|
|
|
|
// with cryptsetup before updating the state to `rbdImageEncrypted`.
|
|
|
|
rbdImageEncryptionPrepared = rbdEncryptionState("encryptionPrepared")
|
|
|
|
|
2021-07-08 14:59:34 +00:00
|
|
|
// image metadata key for encryption.
|
2021-07-23 15:23:43 +00:00
|
|
|
encryptionMetaKey = "rbd.csi.ceph.com/encrypted"
|
|
|
|
oldEncryptionMetaKey = ".rbd.csi.ceph.com/encrypted"
|
2021-02-17 15:55:06 +00:00
|
|
|
|
|
|
|
// metadataDEK is the key in the image metadata where the (encrypted)
|
|
|
|
// DEK is stored.
|
2021-07-23 15:23:43 +00:00
|
|
|
metadataDEK = "rbd.csi.ceph.com/dek"
|
|
|
|
oldMetadataDEK = ".rbd.csi.ceph.com/dek"
|
2022-02-11 15:30:23 +00:00
|
|
|
|
|
|
|
encryptionPassphraseSize = 20
|
2022-05-27 19:55:58 +00:00
|
|
|
|
|
|
|
// rbdDefaultEncryptionType is the default to use when the
|
|
|
|
// user did not specify an "encryptionType", but set
|
|
|
|
// "encryption": true.
|
|
|
|
rbdDefaultEncryptionType = util.EncryptionTypeBlock
|
2024-06-21 10:19:06 +00:00
|
|
|
|
|
|
|
// Luks slots.
|
|
|
|
luksSlot0 = "0"
|
|
|
|
luksSlot1 = "1"
|
2021-02-15 07:26:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// checkRbdImageEncrypted verifies if rbd image was encrypted when created.
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) checkRbdImageEncrypted(ctx context.Context) (rbdEncryptionState, error) {
|
2021-07-23 15:23:43 +00:00
|
|
|
value, err := ri.MigrateMetadata(oldEncryptionMetaKey, encryptionMetaKey, string(rbdImageEncryptionUnknown))
|
2021-02-16 15:56:42 +00:00
|
|
|
if errors.Is(err, librbd.ErrNotFound) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx, "image %s encrypted state not set", ri)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-16 15:56:42 +00:00
|
|
|
return rbdImageEncryptionUnknown, nil
|
|
|
|
} else if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "checking image %s encrypted state metadata failed: %s", ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-15 07:26:35 +00:00
|
|
|
return rbdImageEncryptionUnknown, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypted := rbdEncryptionState(strings.TrimSpace(value))
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx, "image %s encrypted state metadata reports %q", ri, encrypted)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-15 07:26:35 +00:00
|
|
|
return encrypted, nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) ensureEncryptionMetadataSet(status rbdEncryptionState) error {
|
|
|
|
err := ri.SetMetadata(encryptionMetaKey, string(status))
|
2021-02-15 07:26:35 +00:00
|
|
|
if err != nil {
|
2021-03-12 12:37:15 +00:00
|
|
|
return fmt.Errorf("failed to save encryption status for %s: %w", ri, err)
|
2021-02-15 07:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:03:32 +00:00
|
|
|
// isBlockEncrypted returns `true` if the rbdImage is (or needs to be) encrypted.
|
|
|
|
func (ri *rbdImage) isBlockEncrypted() bool {
|
|
|
|
return ri.blockEncryption != nil
|
2021-02-22 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
// isFileEncrypted returns `true` if the filesystem on the rbdImage is (or needs to be) encrypted.
|
2022-05-27 18:03:32 +00:00
|
|
|
func (ri *rbdImage) isFileEncrypted() bool {
|
|
|
|
return ri.fileEncryption != nil
|
|
|
|
}
|
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
func IsFileEncrypted(ctx context.Context, volOptions map[string]string) (bool, error) {
|
2023-05-24 13:43:15 +00:00
|
|
|
_, encType, err := ParseEncryptionOpts(volOptions, util.EncryptionTypeInvalid)
|
2022-05-27 19:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encType == util.EncryptionTypeFile, nil
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:03:32 +00:00
|
|
|
// setupBlockEncryption configures the metadata of the RBD image for encryption:
|
2021-02-15 07:26:35 +00:00
|
|
|
// - the Data-Encryption-Key (DEK) will be generated stored for use by the KMS;
|
|
|
|
// - the RBD image will be marked to support encryption in its metadata.
|
2022-05-27 18:03:32 +00:00
|
|
|
func (ri *rbdImage) setupBlockEncryption(ctx context.Context) error {
|
2024-03-04 15:13:31 +00:00
|
|
|
err := ri.blockEncryption.StoreNewCryptoPassphrase(ctx, ri.VolID, encryptionPassphraseSize)
|
2021-02-15 07:26:35 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to save encryption passphrase for "+
|
2021-05-07 05:30:37 +00:00
|
|
|
"image %s: %s", ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-15 07:26:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
err = ri.ensureEncryptionMetadataSet(rbdImageEncryptionPrepared)
|
2021-02-15 07:26:35 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to save encryption status, deleting "+
|
2021-05-07 05:30:37 +00:00
|
|
|
"image %s: %s", ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-15 07:26:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-22 15:57:44 +00:00
|
|
|
|
2021-03-12 13:26:21 +00:00
|
|
|
// copyEncryptionConfig copies the VolumeEncryption object from the source
|
2022-04-25 10:15:08 +00:00
|
|
|
// rbdImage to the passed argument if the source rbdImage is encrypted.
|
|
|
|
// This function re-encrypts the passphrase from the original, so that
|
|
|
|
// both encrypted passphrases (potentially, depends on the DEKStore) have
|
|
|
|
// different contents.
|
2021-09-28 05:06:20 +00:00
|
|
|
// When copyOnlyPassphrase is set to true, only the passphrase is copied to the
|
|
|
|
// destination rbdImage's VolumeEncryption object which needs to be initialized
|
|
|
|
// beforehand and is possibly different from the source VolumeEncryption
|
|
|
|
// (Usecase: Restoring snapshot into a storageclass with different encryption config).
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ri *rbdImage) copyEncryptionConfig(ctx context.Context, cp *rbdImage, copyOnlyPassphrase bool) error {
|
2022-04-25 10:15:08 +00:00
|
|
|
// nothing to do if parent image is not encrypted.
|
2022-05-27 19:55:58 +00:00
|
|
|
if !ri.isBlockEncrypted() && !ri.isFileEncrypted() {
|
2022-04-25 10:15:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 12:04:38 +00:00
|
|
|
if ri.VolID == cp.VolID {
|
|
|
|
return fmt.Errorf("BUG: %q and %q have the same VolID (%s) "+
|
|
|
|
"set!? Call stack: %s", ri, cp, ri.VolID, util.CallStack())
|
|
|
|
}
|
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
if ri.isBlockEncrypted() {
|
|
|
|
// get the unencrypted passphrase
|
2024-03-04 15:13:31 +00:00
|
|
|
passphrase, err := ri.blockEncryption.GetCryptoPassphrase(ctx, ri.VolID)
|
2022-05-27 19:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch passphrase for %q: %w",
|
|
|
|
ri, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !copyOnlyPassphrase {
|
|
|
|
cp.blockEncryption, err = util.NewVolumeEncryption(ri.blockEncryption.GetID(), ri.blockEncryption.KMS)
|
|
|
|
if errors.Is(err, util.ErrDEKStoreNeeded) {
|
|
|
|
cp.blockEncryption.SetDEKStore(cp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-encrypt the plain passphrase for the cloned volume
|
2024-03-04 15:13:31 +00:00
|
|
|
err = cp.blockEncryption.StoreCryptoPassphrase(ctx, cp.VolID, passphrase)
|
2022-05-27 19:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to store passphrase for %q: %w",
|
|
|
|
cp, err)
|
|
|
|
}
|
2021-03-12 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
if ri.isFileEncrypted() && !copyOnlyPassphrase {
|
|
|
|
var err error
|
|
|
|
cp.fileEncryption, err = util.NewVolumeEncryption(ri.fileEncryption.GetID(), ri.fileEncryption.KMS)
|
2021-09-28 05:06:20 +00:00
|
|
|
if errors.Is(err, util.ErrDEKStoreNeeded) {
|
2024-03-04 15:13:31 +00:00
|
|
|
_, err := ri.fileEncryption.KMS.GetSecret(ctx, "")
|
2022-05-27 19:55:58 +00:00
|
|
|
if errors.Is(err, kmsapi.ErrGetSecretUnsupported) {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-28 05:06:20 +00:00
|
|
|
}
|
2021-03-12 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
if ri.isFileEncrypted() && ri.fileEncryption.KMS.RequiresDEKStore() == kmsapi.DEKStoreIntegrated {
|
|
|
|
// get the unencrypted passphrase
|
2024-03-04 15:13:31 +00:00
|
|
|
passphrase, err := ri.fileEncryption.GetCryptoPassphrase(ctx, ri.VolID)
|
2022-05-27 19:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch passphrase for %q: %w",
|
|
|
|
ri, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-encrypt the plain passphrase for the cloned volume
|
2024-03-04 15:13:31 +00:00
|
|
|
err = cp.fileEncryption.StoreCryptoPassphrase(ctx, cp.VolID, passphrase)
|
2022-05-27 19:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to store passphrase for %q: %w",
|
|
|
|
cp, err)
|
|
|
|
}
|
2021-03-12 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy encryption status for the original volume
|
|
|
|
status, err := ri.checkRbdImageEncrypted(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get encryption status for %q: %w",
|
2021-05-07 05:30:37 +00:00
|
|
|
ri, err)
|
2021-03-12 13:26:21 +00:00
|
|
|
}
|
2022-05-27 19:55:58 +00:00
|
|
|
|
2021-03-12 13:26:21 +00:00
|
|
|
err = cp.ensureEncryptionMetadataSet(status)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to store encryption status for %q: "+
|
2021-05-07 05:30:37 +00:00
|
|
|
"%w", cp, err)
|
2021-03-12 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 08:36:42 +00:00
|
|
|
// repairEncryptionConfig checks the encryption state of the current rbdImage,
|
|
|
|
// and makes sure that the destination rbdImage has the same configuration.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ri *rbdImage) repairEncryptionConfig(ctx context.Context, dest *rbdImage) error {
|
2022-05-27 19:55:58 +00:00
|
|
|
if !ri.isBlockEncrypted() && !ri.isFileEncrypted() {
|
2021-04-09 08:36:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if ri is encrypted, copy its configuration in case it is missing
|
2022-05-27 19:55:58 +00:00
|
|
|
if !dest.isBlockEncrypted() && !dest.isFileEncrypted() {
|
2021-04-09 08:36:42 +00:00
|
|
|
// dest needs to be connected to the cluster, otherwise it will
|
|
|
|
// not be possible to write any metadata
|
|
|
|
if dest.conn == nil {
|
|
|
|
dest.conn = ri.conn.Copy()
|
|
|
|
}
|
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
return ri.copyEncryptionConfig(ctx, dest, true)
|
2021-04-09 08:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
func (ri *rbdImage) encryptDevice(ctx context.Context, devicePath string) error {
|
2024-03-04 15:13:31 +00:00
|
|
|
passphrase, err := ri.blockEncryption.GetCryptoPassphrase(ctx, ri.VolID)
|
2021-02-22 16:20:11 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get crypto passphrase for %s: %v",
|
2021-05-07 05:30:37 +00:00
|
|
|
ri, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-22 16:20:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = util.EncryptVolume(ctx, devicePath, passphrase); err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
err = fmt.Errorf("failed to encrypt volume %s: %w", ri, err)
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-22 16:20:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
err = ri.ensureEncryptionMetadataSet(rbdImageEncrypted)
|
2021-02-22 16:20:11 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-22 16:20:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 15:57:44 +00:00
|
|
|
func (rv *rbdVolume) openEncryptedDevice(ctx context.Context, devicePath string) (string, error) {
|
2024-03-04 15:13:31 +00:00
|
|
|
passphrase, err := rv.blockEncryption.GetCryptoPassphrase(ctx, rv.VolID)
|
2021-02-22 15:57:44 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get passphrase for encrypted device %s: %v",
|
2021-05-07 05:30:37 +00:00
|
|
|
rv, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-22 15:57:44 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
mapperFile, mapperFilePath := util.VolumeMapper(rv.VolID)
|
|
|
|
|
|
|
|
isOpen, err := util.IsDeviceOpen(ctx, mapperFilePath)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to check device %s encryption status: %s", devicePath, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-22 15:57:44 +00:00
|
|
|
return devicePath, err
|
|
|
|
}
|
|
|
|
if isOpen {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx, "encrypted device is already open at %s", mapperFilePath)
|
2021-02-22 15:57:44 +00:00
|
|
|
} else {
|
|
|
|
err = util.OpenEncryptedVolume(ctx, devicePath, mapperFile, passphrase)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to open device %s: %v",
|
2021-05-07 05:30:37 +00:00
|
|
|
rv, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-22 15:57:44 +00:00
|
|
|
return devicePath, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapperFilePath, nil
|
|
|
|
}
|
2021-02-15 07:26:35 +00:00
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ri *rbdImage) initKMS(ctx context.Context, volOptions, credentials map[string]string) error {
|
2023-05-24 13:43:15 +00:00
|
|
|
kmsID, encType, err := ParseEncryptionOpts(volOptions, rbdDefaultEncryptionType)
|
2021-07-16 10:07:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
switch encType {
|
|
|
|
case util.EncryptionTypeBlock:
|
|
|
|
err = ri.configureBlockEncryption(kmsID, credentials)
|
|
|
|
case util.EncryptionTypeFile:
|
2024-03-04 15:13:31 +00:00
|
|
|
err = ri.configureFileEncryption(ctx, kmsID, credentials)
|
2022-05-27 19:55:58 +00:00
|
|
|
case util.EncryptionTypeInvalid:
|
2024-04-04 08:52:46 +00:00
|
|
|
return errors.New("invalid encryption type")
|
2022-09-06 16:46:56 +00:00
|
|
|
case util.EncryptionTypeNone:
|
|
|
|
return nil
|
2022-05-27 19:55:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:07:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid encryption kms configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseEncryptionOpts returns kmsID and sets Owner attribute.
|
2022-05-27 19:55:58 +00:00
|
|
|
func ParseEncryptionOpts(
|
2022-06-01 10:17:19 +00:00
|
|
|
volOptions map[string]string,
|
2022-08-04 15:15:52 +00:00
|
|
|
fallbackEncType util.EncryptionType,
|
2022-05-27 19:55:58 +00:00
|
|
|
) (string, util.EncryptionType, error) {
|
2021-02-15 07:26:35 +00:00
|
|
|
var (
|
2021-07-16 10:07:56 +00:00
|
|
|
err error
|
|
|
|
ok bool
|
|
|
|
encrypted, kmsID string
|
2021-02-15 07:26:35 +00:00
|
|
|
)
|
|
|
|
encrypted, ok = volOptions["encrypted"]
|
2023-05-25 12:31:30 +00:00
|
|
|
if !ok {
|
|
|
|
return "", util.EncryptionTypeNone, nil
|
|
|
|
}
|
|
|
|
ok, err = strconv.ParseBool(encrypted)
|
|
|
|
if err != nil {
|
|
|
|
return "", util.EncryptionTypeInvalid, err
|
|
|
|
}
|
|
|
|
if !ok {
|
2022-09-06 16:46:56 +00:00
|
|
|
return "", util.EncryptionTypeNone, nil
|
2021-02-15 07:26:35 +00:00
|
|
|
}
|
2021-07-16 10:07:56 +00:00
|
|
|
kmsID, err = util.FetchEncryptionKMSID(encrypted, volOptions["encryptionKMSID"])
|
2021-02-15 07:26:35 +00:00
|
|
|
if err != nil {
|
2022-05-27 19:55:58 +00:00
|
|
|
return "", util.EncryptionTypeInvalid, err
|
2021-02-15 07:26:35 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 15:15:52 +00:00
|
|
|
encType := util.FetchEncryptionType(volOptions, fallbackEncType)
|
2022-05-27 19:55:58 +00:00
|
|
|
|
|
|
|
return kmsID, encType, nil
|
2021-02-15 07:26:35 +00:00
|
|
|
}
|
2021-02-22 15:25:35 +00:00
|
|
|
|
2022-05-27 18:03:32 +00:00
|
|
|
// configureBlockDeviceEncryption sets up the VolumeEncryption for this rbdImage. Once
|
|
|
|
// configured, use isBlockEncrypted() to see if the volume supports block encryption.
|
|
|
|
func (ri *rbdImage) configureBlockEncryption(kmsID string, credentials map[string]string) error {
|
2021-08-26 11:43:20 +00:00
|
|
|
kms, err := kmsapi.GetKMS(ri.Owner, kmsID, credentials)
|
2021-02-22 15:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:03:32 +00:00
|
|
|
ri.blockEncryption, err = util.NewVolumeEncryption(kmsID, kms)
|
2021-02-25 16:26:05 +00:00
|
|
|
|
|
|
|
// if the KMS can not store the DEK itself, we'll store it in the
|
|
|
|
// metadata of the RBD image itself
|
|
|
|
if errors.Is(err, util.ErrDEKStoreNeeded) {
|
2022-05-27 18:03:32 +00:00
|
|
|
ri.blockEncryption.SetDEKStore(ri)
|
2021-02-25 16:26:05 +00:00
|
|
|
}
|
2021-02-22 15:25:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-17 15:55:06 +00:00
|
|
|
|
2022-05-27 19:55:58 +00:00
|
|
|
// configureBlockDeviceEncryption sets up the VolumeEncryption for this rbdImage. Once
|
|
|
|
// configured, use isEncrypted() to see if the volume supports encryption.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ri *rbdImage) configureFileEncryption(ctx context.Context, kmsID string, credentials map[string]string) error {
|
2022-05-27 19:55:58 +00:00
|
|
|
kms, err := kmsapi.GetKMS(ri.Owner, kmsID, credentials)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ri.fileEncryption, err = util.NewVolumeEncryption(kmsID, kms)
|
|
|
|
|
|
|
|
if errors.Is(err, util.ErrDEKStoreNeeded) {
|
|
|
|
// fscrypt uses secrets directly from the KMS.
|
|
|
|
// Therefore we do not support an additional DEK
|
|
|
|
// store. Since not all "metadata" KMS support
|
|
|
|
// GetSecret, test for support here. Postpone any
|
|
|
|
// other error handling
|
2024-03-04 15:13:31 +00:00
|
|
|
_, err := ri.fileEncryption.KMS.GetSecret(ctx, "")
|
2022-05-27 19:55:58 +00:00
|
|
|
if errors.Is(err, kmsapi.ErrGetSecretUnsupported) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:55:06 +00:00
|
|
|
// StoreDEK saves the DEK in the metadata, overwrites any existing contents.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ri *rbdImage) StoreDEK(ctx context.Context, volumeID, dek string) error {
|
2021-04-09 12:04:38 +00:00
|
|
|
if ri.VolID == "" {
|
|
|
|
return fmt.Errorf("BUG: %q does not have VolID set, call "+
|
|
|
|
"stack: %s", ri, util.CallStack())
|
|
|
|
} else if ri.VolID != volumeID {
|
|
|
|
return fmt.Errorf("volume %q can not store DEK for %q",
|
2021-05-07 05:30:37 +00:00
|
|
|
ri, volumeID)
|
2021-02-17 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:37:15 +00:00
|
|
|
return ri.SetMetadata(metadataDEK, dek)
|
2021-02-17 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchDEK reads the DEK from the image metadata.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ri *rbdImage) FetchDEK(ctx context.Context, volumeID string) (string, error) {
|
2021-04-09 12:04:38 +00:00
|
|
|
if ri.VolID == "" {
|
|
|
|
return "", fmt.Errorf("BUG: %q does not have VolID set, call "+
|
|
|
|
"stack: %s", ri, util.CallStack())
|
|
|
|
} else if ri.VolID != volumeID {
|
2021-05-07 05:30:37 +00:00
|
|
|
return "", fmt.Errorf("volume %q can not fetch DEK for %q", ri, volumeID)
|
2021-02-17 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 15:23:43 +00:00
|
|
|
return ri.MigrateMetadata(oldMetadataDEK, metadataDEK, "")
|
2021-02-17 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDEK does not need to remove the DEK from the metadata, the image is
|
|
|
|
// most likely getting removed.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ri *rbdImage) RemoveDEK(ctx context.Context, volumeID string) error {
|
2021-04-09 12:04:38 +00:00
|
|
|
if ri.VolID == "" {
|
|
|
|
return fmt.Errorf("BUG: %q does not have VolID set, call "+
|
|
|
|
"stack: %s", ri, util.CallStack())
|
|
|
|
} else if ri.VolID != volumeID {
|
|
|
|
return fmt.Errorf("volume %q can not remove DEK for %q",
|
2021-05-07 05:30:37 +00:00
|
|
|
ri, volumeID)
|
2021-02-17 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-21 10:19:06 +00:00
|
|
|
|
|
|
|
// GetEncryptionPassphraseSize returns the value of `encryptionPassphraseSize`.
|
|
|
|
func GetEncryptionPassphraseSize() int {
|
|
|
|
return encryptionPassphraseSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// RotateEncryptionKey processes the key rotation for the RBD Volume.
|
|
|
|
func (rv *rbdVolume) RotateEncryptionKey(ctx context.Context) error {
|
|
|
|
if !rv.isBlockEncrypted() {
|
|
|
|
return errors.New("key rotation unsupported for non block encrypted device")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the underlying device has been setup for encryption
|
|
|
|
currState, err := rv.checkRbdImageEncrypted(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check encryption state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if currState != rbdImageEncrypted {
|
|
|
|
return errors.New("key rotation not supported for unencrypted device")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the device path for the underlying image
|
|
|
|
useNbd := rv.Mounter == rbdNbdMounter && hasNBD
|
|
|
|
devicePath, found := waitForPath(ctx, rv.Pool, rv.RadosNamespace, rv.RbdImageName, 1, useNbd)
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("failed to get the device path for %q: %w", rv, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 1: Get the current passphrase
|
|
|
|
oldPassphrase, err := rv.blockEncryption.GetCryptoPassphrase(ctx, rv.VolID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch the current passphrase for %q: %w", rv, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Add current key to slot 1
|
|
|
|
err = util.LuksAddKey(devicePath, oldPassphrase, oldPassphrase, luksSlot1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to add curr key to luksSlot1: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: Generate new key and add it to slot 0
|
|
|
|
newPassphrase, err := rv.blockEncryption.GetNewCryptoPassphrase(
|
|
|
|
GetEncryptionPassphraseSize())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to generate a new passphrase: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = util.LuksAddKey(devicePath, oldPassphrase, newPassphrase, luksSlot0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to add the new key to luksSlot0: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 4: Add the new key to KMS
|
|
|
|
err = rv.blockEncryption.StoreCryptoPassphrase(ctx, rv.VolID, newPassphrase)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update the new key into the KMS: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 5: Remove the old key from slot 1
|
|
|
|
// We use the newPassphrase to authenticate LUKS here
|
|
|
|
err = util.LuksRemoveKey(devicePath, newPassphrase, luksSlot1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to remove the backup key from luksSlot1: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return error accordingly.
|
|
|
|
return nil
|
|
|
|
}
|