2020-12-08 10:44:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 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
|
2020-12-08 10:44:04 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-15 14:50:12 +00:00
|
|
|
"encoding/json"
|
2020-12-08 10:44:04 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-03-18 12:50:38 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-12-08 10:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseConfig(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-06-11 10:17:51 +00:00
|
|
|
vtc := vaultTenantConnection{}
|
2020-12-08 10:44:04 +00:00
|
|
|
|
|
|
|
config := make(map[string]interface{})
|
|
|
|
|
|
|
|
// empty config map
|
2021-06-11 10:17:51 +00:00
|
|
|
err := vtc.parseConfig(config)
|
2020-12-08 10:44:04 +00:00
|
|
|
if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
t.Errorf("unexpected error (%T): %s", err, err)
|
|
|
|
}
|
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
// fill default options (normally done in initVaultTokensKMS)
|
2020-12-08 10:44:04 +00:00
|
|
|
config["vaultAddress"] = "https://vault.default.cluster.svc"
|
|
|
|
config["tenantConfigName"] = vaultTokensDefaultConfigName
|
|
|
|
|
|
|
|
// parsing with all required options
|
2021-06-11 10:17:51 +00:00
|
|
|
err = vtc.parseConfig(config)
|
2020-12-08 10:44:04 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
2021-06-11 10:17:51 +00:00
|
|
|
case vtc.ConfigName != vaultTokensDefaultConfigName:
|
|
|
|
t.Errorf("ConfigName contains unexpected value: %s", vtc.ConfigName)
|
2020-12-08 10:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tenant "bob" uses a different kms.ConfigName
|
|
|
|
bob := make(map[string]interface{})
|
|
|
|
bob["tenantConfigName"] = "the-config-from-bob"
|
2021-06-11 10:17:51 +00:00
|
|
|
err = vtc.parseConfig(bob)
|
2020-12-08 10:44:04 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
2021-06-11 10:17:51 +00:00
|
|
|
case vtc.ConfigName != "the-config-from-bob":
|
|
|
|
t.Errorf("ConfigName contains unexpected value: %s", vtc.ConfigName)
|
2020-12-08 10:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestInitVaultTokensKMS verifies that passing partial and complex
|
|
|
|
// configurations get applied correctly.
|
|
|
|
//
|
2021-03-18 12:50:38 +00:00
|
|
|
// When vault.New() is called at the end of initVaultTokensKMS(), errors will
|
2020-12-08 10:44:04 +00:00
|
|
|
// mention the missing VAULT_TOKEN, and that is expected.
|
|
|
|
func TestInitVaultTokensKMS(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2020-12-08 10:44:04 +00:00
|
|
|
if true {
|
|
|
|
// FIXME: testing only works when KUBE_CONFIG is set to a
|
|
|
|
// cluster that has a working Vault deployment
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
args := ProviderInitArgs{
|
2021-03-18 12:50:38 +00:00
|
|
|
Tenant: "bob",
|
|
|
|
Config: make(map[string]interface{}),
|
|
|
|
Secrets: nil,
|
|
|
|
}
|
2020-12-08 10:44:04 +00:00
|
|
|
|
|
|
|
// empty config map
|
2021-03-18 12:50:38 +00:00
|
|
|
_, err := initVaultTokensKMS(args)
|
2020-12-08 10:44:04 +00:00
|
|
|
if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
t.Errorf("unexpected error (%T): %s", err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill required options
|
2021-03-18 12:50:38 +00:00
|
|
|
args.Config["vaultAddress"] = "https://vault.default.cluster.svc"
|
2020-12-08 10:44:04 +00:00
|
|
|
|
|
|
|
// parsing with all required options
|
2021-03-18 12:50:38 +00:00
|
|
|
_, err = initVaultTokensKMS(args)
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "VAULT_TOKEN") {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill tenants
|
|
|
|
tenants := make(map[string]interface{})
|
2021-03-18 12:50:38 +00:00
|
|
|
args.Config["tenants"] = tenants
|
2020-12-08 10:44:04 +00:00
|
|
|
|
|
|
|
// empty tenants list
|
2021-03-18 12:50:38 +00:00
|
|
|
_, err = initVaultTokensKMS(args)
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "VAULT_TOKEN") {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add tenant "bob"
|
|
|
|
bob := make(map[string]interface{})
|
|
|
|
bob["vaultAddress"] = "https://vault.bob.example.org"
|
2021-03-18 12:50:38 +00:00
|
|
|
args.Config["tenants"].(map[string]interface{})["bob"] = bob
|
2020-12-08 10:44:04 +00:00
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
_, err = initVaultTokensKMS(args)
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "VAULT_TOKEN") {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 14:50:12 +00:00
|
|
|
|
|
|
|
// TestStdVaultToCSIConfig converts a JSON document with standard VAULT_*
|
|
|
|
// environment variables to a vaultTokenConf structure.
|
|
|
|
func TestStdVaultToCSIConfig(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-02-15 14:50:12 +00:00
|
|
|
vaultConfigMap := `{
|
|
|
|
"KMS_PROVIDER":"vaulttokens",
|
|
|
|
"VAULT_ADDR":"https://vault.example.com",
|
2021-07-20 09:18:58 +00:00
|
|
|
"VAULT_BACKEND":"kv-v2",
|
2021-02-15 14:50:12 +00:00
|
|
|
"VAULT_BACKEND_PATH":"/secret",
|
2021-08-02 09:35:26 +00:00
|
|
|
"VAULT_DESTROY_KEYS":"true",
|
2021-02-15 14:50:12 +00:00
|
|
|
"VAULT_CACERT":"",
|
|
|
|
"VAULT_TLS_SERVER_NAME":"vault.example.com",
|
|
|
|
"VAULT_CLIENT_CERT":"",
|
|
|
|
"VAULT_CLIENT_KEY":"",
|
2021-07-30 07:53:27 +00:00
|
|
|
"VAULT_AUTH_NAMESPACE":"devops",
|
|
|
|
"VAULT_NAMESPACE":"devops/homepage",
|
2021-02-15 14:50:12 +00:00
|
|
|
"VAULT_SKIP_VERIFY":"true"
|
|
|
|
}`
|
|
|
|
|
|
|
|
sv := &standardVault{}
|
|
|
|
err := json.Unmarshal([]byte(vaultConfigMap), sv)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-15 14:50:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
v := vaultTokenConf{}
|
|
|
|
v.convertStdVaultToCSIConfig(sv)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case v.EncryptionKMSType != kmsTypeVaultTokens:
|
|
|
|
t.Errorf("unexpected value for EncryptionKMSType: %s", v.EncryptionKMSType)
|
|
|
|
case v.VaultAddress != "https://vault.example.com":
|
|
|
|
t.Errorf("unexpected value for VaultAddress: %s", v.VaultAddress)
|
2021-07-20 09:18:58 +00:00
|
|
|
case v.VaultBackend != "kv-v2":
|
|
|
|
t.Errorf("unexpected value for VaultBackend: %s", v.VaultBackend)
|
2021-02-15 14:50:12 +00:00
|
|
|
case v.VaultBackendPath != "/secret":
|
|
|
|
t.Errorf("unexpected value for VaultBackendPath: %s", v.VaultBackendPath)
|
2021-08-02 09:35:26 +00:00
|
|
|
case v.VaultDestroyKeys != vaultDefaultDestroyKeys:
|
|
|
|
t.Errorf("unexpected value for VaultDestroyKeys: %s", v.VaultDestroyKeys)
|
2021-02-15 14:50:12 +00:00
|
|
|
case v.VaultCAFromSecret != "":
|
|
|
|
t.Errorf("unexpected value for VaultCAFromSecret: %s", v.VaultCAFromSecret)
|
|
|
|
case v.VaultClientCertFromSecret != "":
|
|
|
|
t.Errorf("unexpected value for VaultClientCertFromSecret: %s", v.VaultClientCertFromSecret)
|
|
|
|
case v.VaultClientCertKeyFromSecret != "":
|
|
|
|
t.Errorf("unexpected value for VaultClientCertKeyFromSecret: %s", v.VaultClientCertKeyFromSecret)
|
2021-07-30 07:53:27 +00:00
|
|
|
case v.VaultAuthNamespace != "devops":
|
|
|
|
t.Errorf("unexpected value for VaultAuthNamespace: %s", v.VaultAuthNamespace)
|
|
|
|
case v.VaultNamespace != "devops/homepage":
|
2021-02-15 14:50:12 +00:00
|
|
|
t.Errorf("unexpected value for VaultNamespace: %s", v.VaultNamespace)
|
|
|
|
case v.VaultTLSServerName != "vault.example.com":
|
|
|
|
t.Errorf("unexpected value for VaultTLSServerName: %s", v.VaultTLSServerName)
|
|
|
|
case v.VaultCAVerify != "false":
|
|
|
|
t.Errorf("unexpected value for VaultCAVerify: %s", v.VaultCAVerify)
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 12:50:38 +00:00
|
|
|
|
|
|
|
func TestTransformConfig(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-03-18 12:50:38 +00:00
|
|
|
cm := make(map[string]interface{})
|
|
|
|
cm["KMS_PROVIDER"] = "vaulttokens"
|
|
|
|
cm["VAULT_ADDR"] = "https://vault.example.com"
|
2021-07-20 09:18:58 +00:00
|
|
|
cm["VAULT_BACKEND"] = "kv-v2"
|
2021-03-18 12:50:38 +00:00
|
|
|
cm["VAULT_BACKEND_PATH"] = "/secret"
|
2021-08-02 09:35:26 +00:00
|
|
|
cm["VAULT_DESTROY_KEYS"] = "true"
|
2021-03-18 12:50:38 +00:00
|
|
|
cm["VAULT_CACERT"] = ""
|
|
|
|
cm["VAULT_TLS_SERVER_NAME"] = "vault.example.com"
|
|
|
|
cm["VAULT_CLIENT_CERT"] = ""
|
|
|
|
cm["VAULT_CLIENT_KEY"] = ""
|
2021-07-30 07:53:27 +00:00
|
|
|
cm["VAULT_AUTH_NAMESPACE"] = "devops"
|
|
|
|
cm["VAULT_NAMESPACE"] = "devops/homepage"
|
2021-03-18 12:50:38 +00:00
|
|
|
cm["VAULT_SKIP_VERIFY"] = "true" // inverse of "vaultCAVerify"
|
|
|
|
|
|
|
|
config, err := transformConfig(cm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, config["encryptionKMSType"], cm["KMS_PROVIDER"])
|
|
|
|
assert.Equal(t, config["vaultAddress"], cm["VAULT_ADDR"])
|
2021-07-20 09:18:58 +00:00
|
|
|
assert.Equal(t, config["vaultBackend"], cm["VAULT_BACKEND"])
|
2021-03-18 12:50:38 +00:00
|
|
|
assert.Equal(t, config["vaultBackendPath"], cm["VAULT_BACKEND_PATH"])
|
2021-08-02 09:35:26 +00:00
|
|
|
assert.Equal(t, config["vaultDestroyKeys"], cm["VAULT_DESTROY_KEYS"])
|
2021-03-18 12:50:38 +00:00
|
|
|
assert.Equal(t, config["vaultCAFromSecret"], cm["VAULT_CACERT"])
|
|
|
|
assert.Equal(t, config["vaultTLSServerName"], cm["VAULT_TLS_SERVER_NAME"])
|
|
|
|
assert.Equal(t, config["vaultClientCertFromSecret"], cm["VAULT_CLIENT_CERT"])
|
|
|
|
assert.Equal(t, config["vaultClientCertKeyFromSecret"], cm["VAULT_CLIENT_KEY"])
|
2021-07-30 07:53:27 +00:00
|
|
|
assert.Equal(t, config["vaultAuthNamespace"], cm["VAULT_AUTH_NAMESPACE"])
|
2021-03-18 12:50:38 +00:00
|
|
|
assert.Equal(t, config["vaultNamespace"], cm["VAULT_NAMESPACE"])
|
|
|
|
assert.Equal(t, config["vaultCAVerify"], "false")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVaultTokensKMSRegistered(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2021-03-18 12:50:38 +00:00
|
|
|
_, ok := kmsManager.providers[kmsTypeVaultTokens]
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|