2021-03-02 17:48:05 +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.
|
|
|
|
*/
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
package kms
|
2021-03-02 17:48:05 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-04 15:13:31 +00:00
|
|
|
"context"
|
2021-03-02 17:48:05 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
func TestNewSecretsKMS(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
secrets := map[string]string{}
|
|
|
|
|
|
|
|
// no passphrase in the secrets, should fail
|
|
|
|
kms, err := newSecretsKMS(ProviderInitArgs{
|
|
|
|
Secrets: secrets,
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, kms)
|
|
|
|
|
|
|
|
// set a passphrase and it should pass
|
|
|
|
secrets[encryptionPassphraseKey] = "plaintext encryption key"
|
|
|
|
kms, err = newSecretsKMS(ProviderInitArgs{
|
|
|
|
Secrets: secrets,
|
|
|
|
})
|
|
|
|
assert.NotNil(t, kms)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-03-02 17:48:05 +00:00
|
|
|
func TestGenerateNonce(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-03-02 17:48:05 +00:00
|
|
|
size := 64
|
|
|
|
nonce, err := generateNonce(size)
|
|
|
|
assert.Equal(t, size, len(nonce))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateCipher(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2023-06-02 08:59:52 +00:00
|
|
|
//nolint:gosec // this passphrase is intentionally hardcoded
|
2021-03-02 17:48:05 +00:00
|
|
|
passphrase := "my-cool-luks-passphrase"
|
|
|
|
salt := "unique-id-for-the-volume"
|
|
|
|
|
|
|
|
aead, err := generateCipher(passphrase, salt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, aead)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitSecretsMetadataKMS(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-08-26 11:43:20 +00:00
|
|
|
args := ProviderInitArgs{
|
2021-03-18 12:50:38 +00:00
|
|
|
Tenant: "tenant",
|
|
|
|
Config: nil,
|
|
|
|
Secrets: map[string]string{},
|
|
|
|
}
|
2021-03-02 17:48:05 +00:00
|
|
|
|
|
|
|
// passphrase it not set, init should fail
|
2021-03-18 12:50:38 +00:00
|
|
|
kms, err := initSecretsMetadataKMS(args)
|
2021-03-02 17:48:05 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, kms)
|
|
|
|
|
|
|
|
// set a passphrase to get a working KMS
|
2021-03-18 12:50:38 +00:00
|
|
|
args.Secrets[encryptionPassphraseKey] = "my-passphrase-from-kubernetes"
|
2021-03-02 17:48:05 +00:00
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
kms, err = initSecretsMetadataKMS(args)
|
2021-03-02 17:48:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NotNil(t, kms)
|
2021-08-26 11:43:20 +00:00
|
|
|
assert.Equal(t, DEKStoreMetadata, kms.RequiresDEKStore())
|
2021-03-02 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWorkflowSecretsMetadataKMS(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-03-02 17:48:05 +00:00
|
|
|
secrets := map[string]string{
|
|
|
|
encryptionPassphraseKey: "my-passphrase-from-kubernetes",
|
|
|
|
}
|
2021-08-26 11:43:20 +00:00
|
|
|
args := ProviderInitArgs{
|
2021-03-18 12:50:38 +00:00
|
|
|
Tenant: "tenant",
|
|
|
|
Config: nil,
|
|
|
|
Secrets: secrets,
|
|
|
|
}
|
2021-03-02 17:48:05 +00:00
|
|
|
volumeID := "csi-vol-1b00f5f8-b1c1-11e9-8421-9243c1f659f0"
|
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
kms, err := initSecretsMetadataKMS(args)
|
2021-03-02 17:48:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NotNil(t, kms)
|
|
|
|
|
|
|
|
// plainDEK is the (LUKS) passphrase for the volume
|
2021-08-26 11:43:20 +00:00
|
|
|
plainDEK := "usually created with generateNewEncryptionPassphrase()"
|
2021-03-02 17:48:05 +00:00
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
encryptedDEK, err := kms.EncryptDEK(ctx, volumeID, plainDEK)
|
2021-03-02 17:48:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, "", encryptedDEK)
|
|
|
|
assert.NotEqual(t, plainDEK, encryptedDEK)
|
|
|
|
|
|
|
|
// with an incorrect volumeID, decrypting should fail
|
2024-03-04 15:13:31 +00:00
|
|
|
decryptedDEK, err := kms.DecryptDEK(ctx, "incorrect-volumeID", encryptedDEK)
|
2021-03-02 17:48:05 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "", decryptedDEK)
|
|
|
|
assert.NotEqual(t, plainDEK, decryptedDEK)
|
|
|
|
|
|
|
|
// with the right volumeID, decrypting should return the plainDEK
|
2024-03-04 15:13:31 +00:00
|
|
|
decryptedDEK, err = kms.DecryptDEK(ctx, volumeID, encryptedDEK)
|
2021-03-02 17:48:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, "", decryptedDEK)
|
|
|
|
assert.Equal(t, plainDEK, decryptedDEK)
|
|
|
|
}
|
2021-03-18 12:50:38 +00:00
|
|
|
|
|
|
|
func TestSecretsMetadataKMSRegistered(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-03-18 12:50:38 +00:00
|
|
|
_, ok := kmsManager.providers[kmsTypeSecretsMetadata]
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|