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 (
|
|
|
|
"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"
|
|
|
|
|
2022-02-11 15:30:23 +00:00
|
|
|
err = ve.StoreNewCryptoPassphrase(volumeID, defaultEncryptionPassphraseSize)
|
2021-03-04 09:55:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
passphrase, err := ve.GetCryptoPassphrase(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
|
|
|
}
|