2021-03-04 09:55:09 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 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 (
|
2024-03-04 15:13:31 +00:00
|
|
|
"context"
|
2021-03-04 09:55:09 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"testing"
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/kms"
|
|
|
|
|
2021-03-04 09:55:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateNewEncryptionPassphrase(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2022-02-11 15:30:23 +00:00
|
|
|
b64Passphrase, err := generateNewEncryptionPassphrase(defaultEncryptionPassphraseSize)
|
2021-03-04 09:55:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// b64Passphrase is URL-encoded, decode to verify the length of the
|
|
|
|
// passphrase
|
|
|
|
passphrase, err := base64.URLEncoding.DecodeString(b64Passphrase)
|
2024-04-04 08:53:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, passphrase, defaultEncryptionPassphraseSize)
|
2021-03-04 09:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKMSWorkflow(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-03-04 09:55:09 +00:00
|
|
|
secrets := map[string]string{
|
2021-08-26 11:43:20 +00:00
|
|
|
// FIXME: use encryptionPassphraseKey from SecretsKMS
|
|
|
|
"encryptionPassphrase": "workflow test",
|
2021-03-04 09:55:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
kmsProvider, err := kms.GetDefaultKMS(secrets)
|
2024-04-04 08:53:45 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-26 11:43:20 +00:00
|
|
|
require.NotNil(t, kmsProvider)
|
2021-03-04 09:55:09 +00:00
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
ve, err := NewVolumeEncryption("", kmsProvider)
|
2024-04-04 08:53:45 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-15 09:24:47 +00:00
|
|
|
require.NotNil(t, ve)
|
2024-04-04 08:53:45 +00:00
|
|
|
require.Equal(t, kms.DefaultKMSType, ve.GetID())
|
2021-02-15 09:24:47 +00:00
|
|
|
|
2021-03-04 09:55:09 +00:00
|
|
|
volumeID := "volume-id"
|
2024-03-04 15:13:31 +00:00
|
|
|
ctx := context.TODO()
|
2021-03-04 09:55:09 +00:00
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
err = ve.StoreNewCryptoPassphrase(ctx, volumeID, defaultEncryptionPassphraseSize)
|
2024-04-04 08:53:45 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-04 09:55:09 +00:00
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
passphrase, err := ve.GetCryptoPassphrase(ctx, volumeID)
|
2024-04-04 08:53:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, secrets["encryptionPassphrase"], passphrase)
|
2021-03-04 09:55:09 +00:00
|
|
|
}
|
2022-09-06 16:44:00 +00:00
|
|
|
|
|
|
|
func TestEncryptionType(t *testing.T) {
|
|
|
|
t.Parallel()
|
2024-04-04 08:53:45 +00:00
|
|
|
require.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("wat?"))
|
|
|
|
require.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("both"))
|
|
|
|
require.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("file,block"))
|
|
|
|
require.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("block,file"))
|
|
|
|
require.EqualValues(t, EncryptionTypeBlock, ParseEncryptionType("block"))
|
|
|
|
require.EqualValues(t, EncryptionTypeFile, ParseEncryptionType("file"))
|
|
|
|
require.EqualValues(t, EncryptionTypeNone, ParseEncryptionType(""))
|
2022-09-06 16:44:00 +00:00
|
|
|
|
|
|
|
for _, s := range []string{"file", "block", ""} {
|
2024-04-04 08:53:45 +00:00
|
|
|
require.EqualValues(t, s, ParseEncryptionType(s).String())
|
2022-09-06 16:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchEncryptionType(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
volOpts := map[string]string{}
|
2024-04-04 08:53:45 +00:00
|
|
|
require.EqualValues(t, EncryptionTypeBlock, FetchEncryptionType(volOpts, EncryptionTypeBlock))
|
|
|
|
require.EqualValues(t, EncryptionTypeFile, FetchEncryptionType(volOpts, EncryptionTypeFile))
|
|
|
|
require.EqualValues(t, EncryptionTypeNone, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
2022-09-06 16:44:00 +00:00
|
|
|
volOpts["encryptionType"] = ""
|
2024-04-04 08:53:45 +00:00
|
|
|
require.EqualValues(t, EncryptionTypeInvalid, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
2022-09-06 16:44:00 +00:00
|
|
|
volOpts["encryptionType"] = "block"
|
2024-04-04 08:53:45 +00:00
|
|
|
require.EqualValues(t, EncryptionTypeBlock, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
2022-09-06 16:44:00 +00:00
|
|
|
volOpts["encryptionType"] = "file"
|
2024-04-04 08:53:45 +00:00
|
|
|
require.EqualValues(t, EncryptionTypeFile, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
2022-09-06 16:44:00 +00:00
|
|
|
volOpts["encryptionType"] = "INVALID"
|
2024-04-04 08:53:45 +00:00
|
|
|
require.EqualValues(t, EncryptionTypeInvalid, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
2022-09-06 16:44:00 +00:00
|
|
|
}
|