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"
|
2021-07-13 12:21:05 +00:00
|
|
|
"crypto/rand"
|
2020-01-29 11:44:45 +00:00
|
|
|
"encoding/base64"
|
2021-02-12 13:23:17 +00:00
|
|
|
"errors"
|
2019-12-13 11:41:32 +00:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2021-07-16 10:07:56 +00:00
|
|
|
"strconv"
|
2019-12-13 11:41:32 +00:00
|
|
|
"strings"
|
2021-08-24 15:03:25 +00:00
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/kms"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2019-12-13 11:41:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mapperFilePrefix = "luks-rbd-"
|
|
|
|
mapperFilePathPrefix = "/dev/mapper"
|
|
|
|
|
2020-01-29 11:44:45 +00:00
|
|
|
// Passphrase size - 20 bytes is 160 bits to satisfy:
|
|
|
|
// https://tools.ietf.org/html/rfc6749#section-10.10
|
2022-02-11 15:30:23 +00:00
|
|
|
defaultEncryptionPassphraseSize = 20
|
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 {
|
2021-08-26 11:43:20 +00:00
|
|
|
KMS 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.
|
2021-08-26 11:43:20 +00:00
|
|
|
dekStore kms.DEKStore
|
2021-03-19 15:21:48 +00:00
|
|
|
|
|
|
|
id string
|
2021-02-22 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:07:56 +00:00
|
|
|
// FetchEncryptionKMSID returns non-empty kmsID if 'encrypted' parameter is evaluated as true.
|
|
|
|
func FetchEncryptionKMSID(encrypted, kmsID string) (string, error) {
|
|
|
|
isEncrypted, err := strconv.ParseBool(encrypted)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"invalid value set in 'encrypted': %s (should be \"true\" or \"false\"): %w",
|
|
|
|
encrypted, err)
|
|
|
|
}
|
|
|
|
if !isEncrypted {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if kmsID == "" {
|
2021-08-26 11:43:20 +00:00
|
|
|
kmsID = kms.DefaultKMSType
|
2021-07-16 10:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return kmsID, nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 15:36:59 +00:00
|
|
|
type EncryptionType int
|
|
|
|
|
|
|
|
const (
|
2022-09-06 16:44:00 +00:00
|
|
|
// EncryptionTypeInvalid signals invalid or unsupported configuration.
|
2022-07-13 15:36:59 +00:00
|
|
|
EncryptionTypeInvalid EncryptionType = iota
|
2022-09-06 16:44:00 +00:00
|
|
|
// EncryptionTypeNone disables encryption.
|
|
|
|
EncryptionTypeNone
|
|
|
|
// EncryptionTypeBlock enables block encryption.
|
|
|
|
EncryptionTypeBlock
|
|
|
|
// EncryptionTypeBlock enables file encryption (fscrypt).
|
|
|
|
EncryptionTypeFile
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
encryptionTypeBlockString = "block"
|
|
|
|
encryptionTypeFileString = "file"
|
2022-07-13 15:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ParseEncryptionType(typeStr string) EncryptionType {
|
|
|
|
switch typeStr {
|
2022-09-06 16:44:00 +00:00
|
|
|
case encryptionTypeBlockString:
|
2022-07-13 15:36:59 +00:00
|
|
|
return EncryptionTypeBlock
|
2022-09-06 16:44:00 +00:00
|
|
|
case encryptionTypeFileString:
|
2022-07-13 15:36:59 +00:00
|
|
|
return EncryptionTypeFile
|
2022-09-06 16:44:00 +00:00
|
|
|
case "":
|
|
|
|
return EncryptionTypeNone
|
2022-07-13 15:36:59 +00:00
|
|
|
default:
|
|
|
|
return EncryptionTypeInvalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 16:07:14 +00:00
|
|
|
func (encType EncryptionType) String() string {
|
2022-07-13 15:36:59 +00:00
|
|
|
switch encType {
|
|
|
|
case EncryptionTypeBlock:
|
2022-09-06 16:44:00 +00:00
|
|
|
return encryptionTypeBlockString
|
2022-07-13 15:36:59 +00:00
|
|
|
case EncryptionTypeFile:
|
2022-09-06 16:44:00 +00:00
|
|
|
return encryptionTypeFileString
|
|
|
|
case EncryptionTypeNone:
|
2022-07-13 15:36:59 +00:00
|
|
|
return ""
|
2022-09-06 16:44:00 +00:00
|
|
|
case EncryptionTypeInvalid:
|
|
|
|
return "INVALID"
|
2022-07-13 15:36:59 +00:00
|
|
|
default:
|
2022-09-06 16:44:00 +00:00
|
|
|
return "UNKNOWN"
|
2022-07-13 15:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchEncryptionType returns encryptionType specified in volOptions.
|
|
|
|
// If not specified, use fallback. If specified but invalid, return
|
|
|
|
// invalid.
|
|
|
|
func FetchEncryptionType(volOptions map[string]string, fallback EncryptionType) EncryptionType {
|
|
|
|
encType, ok := volOptions["encryptionType"]
|
|
|
|
if !ok {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
2022-09-06 16:44:00 +00:00
|
|
|
if encType == "" {
|
|
|
|
return EncryptionTypeInvalid
|
|
|
|
}
|
|
|
|
|
2022-07-13 15:36:59 +00:00
|
|
|
return ParseEncryptionType(encType)
|
|
|
|
}
|
|
|
|
|
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-08-26 11:43:20 +00:00
|
|
|
func NewVolumeEncryption(id string, ekms kms.EncryptionKMS) (*VolumeEncryption, error) {
|
2021-03-19 15:21:48 +00:00
|
|
|
kmsID := id
|
|
|
|
if kmsID == "" {
|
|
|
|
// if kmsID is not set, encryption is enabled, and the type is
|
|
|
|
// SecretsKMS
|
2021-08-26 11:43:20 +00:00
|
|
|
kmsID = kms.DefaultKMSType
|
2021-03-19 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ve := &VolumeEncryption{
|
|
|
|
id: kmsID,
|
2021-08-26 11:43:20 +00:00
|
|
|
KMS: ekms,
|
2021-03-19 15:21:48 +00:00
|
|
|
}
|
2021-02-22 15:25:35 +00:00
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
if ekms.RequiresDEKStore() == kms.DEKStoreIntegrated {
|
|
|
|
dekStore, ok := ekms.(kms.DEKStore)
|
2021-03-01 15:35:35 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("KMS %T does not implement the "+
|
2021-08-26 11:43:20 +00:00
|
|
|
"DEKStore interface", ekms)
|
2021-03-01 15:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ve.dekStore = dekStore
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-01 15:35:35 +00:00
|
|
|
return ve, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ve, ErrDEKStoreNeeded
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDEKStore sets the DEKStore for this VolumeEncryption instance. It will be
|
|
|
|
// used when StoreNewCryptoPassphrase() or RemoveDEK() is called.
|
2021-08-26 11:43:20 +00:00
|
|
|
func (ve *VolumeEncryption) SetDEKStore(dekStore kms.DEKStore) {
|
2021-03-01 15:35:35 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// RemoveDEK deletes the DEK for a particular volumeID from the DEKStore linked
|
|
|
|
// with this VolumeEncryption instance.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ve *VolumeEncryption) RemoveDEK(ctx context.Context, volumeID string) error {
|
2021-02-15 09:24:47 +00:00
|
|
|
if ve.dekStore == nil {
|
|
|
|
return ErrDEKStoreNotFound
|
|
|
|
}
|
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
return ve.dekStore.RemoveDEK(ctx, volumeID)
|
2021-02-15 09:24:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 15:21:48 +00:00
|
|
|
func (ve *VolumeEncryption) GetID() string {
|
|
|
|
return ve.id
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:06:27 +00:00
|
|
|
// StoreCryptoPassphrase takes an unencrypted passphrase, encrypts it and saves
|
|
|
|
// it in the DEKStore.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ve *VolumeEncryption) StoreCryptoPassphrase(ctx context.Context, volumeID, passphrase string) error {
|
|
|
|
encryptedPassphrase, err := ve.KMS.EncryptDEK(ctx, volumeID, passphrase)
|
2021-02-17 10:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed encrypt the passphrase for %s: %w", volumeID, err)
|
|
|
|
}
|
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
err = ve.dekStore.StoreDEK(ctx, volumeID, encryptedPassphrase)
|
2021-02-15 09:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to save the passphrase for %s: %w", volumeID, err)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-15 09:53:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:06:27 +00:00
|
|
|
// StoreNewCryptoPassphrase generates a new passphrase and saves it in the KMS.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ve *VolumeEncryption) StoreNewCryptoPassphrase(ctx context.Context, volumeID string, length int) error {
|
2022-02-11 15:30:23 +00:00
|
|
|
passphrase, err := generateNewEncryptionPassphrase(length)
|
2021-03-12 14:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to generate passphrase for %s: %w", volumeID, err)
|
|
|
|
}
|
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
return ve.StoreCryptoPassphrase(ctx, volumeID, passphrase)
|
2021-03-12 14:06:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// GetCryptoPassphrase Retrieves passphrase to encrypt volume.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (ve *VolumeEncryption) GetCryptoPassphrase(ctx context.Context, volumeID string) (string, error) {
|
|
|
|
passphrase, err := ve.dekStore.FetchDEK(ctx, volumeID)
|
2021-02-15 09:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2021-02-17 10:57:31 +00:00
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
return ve.KMS.DecryptDEK(ctx, volumeID, passphrase)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 10:19:06 +00:00
|
|
|
// GetNewCryptoPassphrase returns a random passphrase of given length.
|
|
|
|
func (ve *VolumeEncryption) GetNewCryptoPassphrase(length int) (string, error) {
|
|
|
|
return generateNewEncryptionPassphrase(length)
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// generateNewEncryptionPassphrase generates a random passphrase for encryption.
|
2022-02-11 15:30:23 +00:00
|
|
|
func generateNewEncryptionPassphrase(length int) (string, error) {
|
|
|
|
bytesPassphrase := make([]byte, length)
|
2020-01-29 11:44:45 +00:00
|
|
|
_, err := rand.Read(bytesPassphrase)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-01-29 11:44:45 +00:00
|
|
|
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.
|
2022-06-01 10:17:19 +00:00
|
|
|
func VolumeMapper(volumeID string) (string, string) {
|
|
|
|
mapperFile := mapperFilePrefix + volumeID
|
|
|
|
mapperFilePath := path.Join(mapperFilePathPrefix, mapperFile)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-13 11:41:32 +00:00
|
|
|
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 {
|
2021-11-08 07:56:23 +00:00
|
|
|
log.DebugLog(ctx, "Encrypting device %q with LUKS", devicePath)
|
|
|
|
_, stdErr, err := LuksFormat(devicePath, passphrase)
|
|
|
|
if err != nil || stdErr != "" {
|
|
|
|
log.ErrorLog(ctx, "failed to encrypt device %q with LUKS (%v): %s", devicePath, err, stdErr)
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-11-08 07:56:23 +00:00
|
|
|
return err
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-11-08 07:56:23 +00:00
|
|
|
log.DebugLog(ctx, "Opening device %q with LUKS on %q", devicePath, mapperFile)
|
|
|
|
_, stdErr, err := LuksOpen(devicePath, mapperFile, passphrase)
|
|
|
|
if err != nil || stdErr != "" {
|
|
|
|
log.ErrorLog(ctx, "failed to open device %q (%v): %s", devicePath, err, stdErr)
|
2021-04-01 14:28:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-13 11:41:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:31:08 +00:00
|
|
|
// ResizeEncryptedVolume resizes encrypted volume so that it can be used by the client.
|
|
|
|
func ResizeEncryptedVolume(ctx context.Context, mapperFile string) error {
|
2021-11-08 07:56:23 +00:00
|
|
|
log.DebugLog(ctx, "Resizing LUKS device %q", mapperFile)
|
|
|
|
_, stdErr, err := LuksResize(mapperFile)
|
|
|
|
if err != nil || stdErr != "" {
|
|
|
|
log.ErrorLog(ctx, "failed to resize LUKS device %q (%v): %s", mapperFile, err, stdErr)
|
2021-07-06 07:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-11-08 07:56:23 +00:00
|
|
|
log.DebugLog(ctx, "Closing LUKS device %q", mapperFile)
|
|
|
|
_, stdErr, err := LuksClose(mapperFile)
|
|
|
|
if err != nil || stdErr != "" {
|
|
|
|
log.ErrorLog(ctx, "failed to close LUKS device %q (%v): %s", mapperFile, err, stdErr)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-13 11:41:32 +00:00
|
|
|
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)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-13 11:41:32 +00:00
|
|
|
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.
|
2022-06-01 10:17:19 +00:00
|
|
|
func DeviceEncryptionStatus(ctx context.Context, devicePath string) (string, string, error) {
|
2019-12-13 11:41:32 +00:00
|
|
|
if !strings.HasPrefix(devicePath, mapperFilePathPrefix) {
|
|
|
|
return devicePath, "", nil
|
|
|
|
}
|
|
|
|
mapPath := strings.TrimPrefix(devicePath, mapperFilePathPrefix+"/")
|
2021-11-08 07:56:23 +00:00
|
|
|
stdout, stdErr, err := LuksStatus(mapPath)
|
|
|
|
if err != nil || stdErr != "" {
|
|
|
|
log.DebugLog(ctx, "%q is not an active LUKS device (%v): %s", devicePath, err, stdErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-13 11:41:32 +00:00
|
|
|
return devicePath, "", nil
|
|
|
|
}
|
2021-11-08 07:56:23 +00:00
|
|
|
lines := strings.Split(stdout, "\n")
|
2019-12-13 11:41:32 +00:00
|
|
|
if len(lines) < 1 {
|
|
|
|
return "", "", fmt.Errorf("device encryption status returned no stdout for %s", devicePath)
|
|
|
|
}
|
2021-07-06 07:22:32 +00:00
|
|
|
// The line will look like: "/dev/mapper/xxx is active and is in use."
|
|
|
|
if !strings.Contains(lines[0], " is active") {
|
2019-12-13 11:41:32 +00:00
|
|
|
// 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])
|
|
|
|
}
|
2022-06-01 10:17:19 +00:00
|
|
|
if kv[0] == "device" {
|
2019-12-13 11:41:32 +00:00
|
|
|
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)
|
|
|
|
}
|