Skip to content

Commit

Permalink
Fix typos and misc refactors
Browse files Browse the repository at this point in the history
Signed-off-by: Niraj Yadav <niryadav@redhat.com>
  • Loading branch information
black-dragon74 committed Jun 25, 2024
1 parent 84ac4d2 commit 98506e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 67 deletions.
65 changes: 65 additions & 0 deletions internal/rbd/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,68 @@ func (ri *rbdImage) RemoveDEK(ctx context.Context, volumeID string) error {
func GetEncryptionPassphraseSize() int {
return encryptionPassphraseSize
}

// RotateKey processes the key rotation for the RBD Volume
func (rv *rbdVolume) RotateEncryptionKey(ctx context.Context) error {
if !rv.isBlockEncrypted() {
return fmt.Errorf("key rotation not supported for the encryption type")
}

// Verify that the underlying device has been setup for encryption
currState, err := rv.checkRbdImageEncrypted(ctx)
if err != nil {
return fmt.Errorf("error: %v while checking encrpytion state", err)
}

if currState != rbdImageEncrypted {
return fmt.Errorf("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("unable to get the device path for the image")
}

// Step 1: Get the current passphrase
oldPassphrase, err := rv.blockEncryption.GetCryptoPassphrase(ctx, rv.VolID)
if err != nil {
return fmt.Errorf("error in fetching the current passphrase: %v", err)
}

// Step 2: Add current key to slot 1
err = util.LuksAddKey(devicePath, oldPassphrase, oldPassphrase, "1")
if err != nil {
return fmt.Errorf("error in adding curr key to slot 1: %v", err)
}

// Step 3: Generate new key and add it to slot 0
newPassphrase, err := rv.blockEncryption.GetNewCryptoPassphrase(
GetEncryptionPassphraseSize())
if err != nil {
return fmt.Errorf("error in generating a new passphrase: %v", err)
}

err = util.LuksAddKey(devicePath, oldPassphrase, newPassphrase, "0")
if err != nil {
return fmt.Errorf("error in adding the new key to slot 0: %v", 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: %v", err)
}

// Step 5: Remove the old key from slot 1
// We use the newPassphrase to authenticate LUKS here
err = util.LuksRemoveKey(devicePath, newPassphrase, "1")
if err != nil {
// FIXME: Discuss if we should return an error here
return nil
}

// Return error accordingly.
return nil
}
65 changes: 0 additions & 65 deletions internal/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2158,68 +2158,3 @@ func (rv *rbdVolume) unsetAllMetadata(keys []string) error {

return nil
}

// RotateKey processes the key rotation for the RBD Volume
func (rv *rbdVolume) RotateEncryptionKey(ctx context.Context) error {
if !rv.isBlockEncrypted() {
return fmt.Errorf("key rotation not supported for the encryption type")
}

// Verify that the underlying device has been setup for encryption
currState, err := rv.checkRbdImageEncrypted(ctx)
if err != nil {
return fmt.Errorf("error: %v while checking encrpytion state", err)
}

if currState != rbdImageEncrypted {
return fmt.Errorf("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("unable to get the device path for the image")
}

// Step 1: Get the current passphrase
oldPassphrase, err := rv.blockEncryption.GetCryptoPassphrase(ctx, rv.VolID)
if err != nil {
return fmt.Errorf("error in fetching the current passphrase: %v", err)
}

// Step 2: Add current key to slot 1
err = util.LuksAddKey(devicePath, oldPassphrase, oldPassphrase, "1")
if err != nil {
return fmt.Errorf("error in adding curr key to slot 1: %v", err)
}

// Step 3: Generate new key and add it to slot 0
newPassphrase, err := rv.blockEncryption.GetNewCryptoPassphrase(
GetEncryptionPassphraseSize())
if err != nil {
return fmt.Errorf("error in generating a new passphrase: %v", err)
}

err = util.LuksAddKey(devicePath, oldPassphrase, newPassphrase, "0")
if err != nil {
return fmt.Errorf("error in adding the new key to slot 0: %v", 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: %v", err)
}

// Step 5: Remove the old key from slot 1
// We use the newPassphrase to autheticate LUKS here
err = util.LuksRemoveKey(devicePath, newPassphrase, "1")
if err != nil {
// FIXME: Discuss if we should return an error here
return nil
}

// Return error accordingly.
return nil
}
2 changes: 1 addition & 1 deletion internal/util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (ve *VolumeEncryption) GetCryptoPassphrase(ctx context.Context, volumeID st
return ve.KMS.DecryptDEK(ctx, volumeID, passphrase)
}

// GetNewCryptoPassphrase return a random passphrase of given length
// GetNewCryptoPassphrase returns a random passphrase of given length
func (ve *VolumeEncryption) GetNewCryptoPassphrase(length int) (string, error) {
return generateNewEncryptionPassphrase(length)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/util/cryptsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func LuksAddKey(devicePath, passphrase, newPassphrase, slot string) error {
return nil
}

// The exisitng passphrase is wrong and the slot is empty
// The existing passphrase is wrong and the slot is empty
return err
}

Expand Down

0 comments on commit 98506e0

Please sign in to comment.