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/assert"
|
|
|
|
"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)
|
|
|
|
assert.NoError(t, err)
|
2022-02-11 15:30:23 +00:00
|
|
|
assert.Equal(t, defaultEncryptionPassphraseSize, len(passphrase))
|
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)
|
2021-03-04 09:55:09 +00:00
|
|
|
assert.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)
|
2021-02-15 09:24:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NotNil(t, ve)
|
2021-08-26 11:43:20 +00:00
|
|
|
assert.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)
|
2021-03-04 09:55:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
passphrase, err := ve.GetCryptoPassphrase(ctx, volumeID)
|
2021-03-04 09:55:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-08-26 11:43:20 +00:00
|
|
|
assert.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()
|
|
|
|
assert.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("wat?"))
|
|
|
|
assert.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("both"))
|
|
|
|
assert.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("file,block"))
|
|
|
|
assert.EqualValues(t, EncryptionTypeInvalid, ParseEncryptionType("block,file"))
|
|
|
|
assert.EqualValues(t, EncryptionTypeBlock, ParseEncryptionType("block"))
|
|
|
|
assert.EqualValues(t, EncryptionTypeFile, ParseEncryptionType("file"))
|
|
|
|
assert.EqualValues(t, EncryptionTypeNone, ParseEncryptionType(""))
|
|
|
|
|
|
|
|
for _, s := range []string{"file", "block", ""} {
|
|
|
|
assert.EqualValues(t, s, EncryptionTypeString(ParseEncryptionType(s)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchEncryptionType(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
volOpts := map[string]string{}
|
|
|
|
assert.EqualValues(t, EncryptionTypeBlock, FetchEncryptionType(volOpts, EncryptionTypeBlock))
|
|
|
|
assert.EqualValues(t, EncryptionTypeFile, FetchEncryptionType(volOpts, EncryptionTypeFile))
|
|
|
|
assert.EqualValues(t, EncryptionTypeNone, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
|
|
|
volOpts["encryptionType"] = ""
|
|
|
|
assert.EqualValues(t, EncryptionTypeInvalid, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
|
|
|
volOpts["encryptionType"] = "block"
|
|
|
|
assert.EqualValues(t, EncryptionTypeBlock, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
|
|
|
volOpts["encryptionType"] = "file"
|
|
|
|
assert.EqualValues(t, EncryptionTypeFile, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
|
|
|
volOpts["encryptionType"] = "INVALID"
|
|
|
|
assert.EqualValues(t, EncryptionTypeInvalid, FetchEncryptionType(volOpts, EncryptionTypeNone))
|
|
|
|
}
|