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"
|
2021-10-28 10:21:31 +00:00
|
|
|
"strconv"
|
2020-12-08 10:44:04 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-03-18 12:50:38 +00:00
|
|
|
|
2022-02-04 13:36:19 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
loss "github.com/libopenstorage/secrets"
|
2021-03-18 12:50:38 +00:00
|
|
|
"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"
|
2022-02-04 13:36:19 +00:00
|
|
|
config["vaultNamespace"] = "default"
|
|
|
|
config["vaultAuthNamespace"] = "company-sso"
|
2020-12-08 10:44:04 +00:00
|
|
|
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"
|
2022-02-04 13:36:19 +00:00
|
|
|
bob["vaultNamespace"] = "bobs-place"
|
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)
|
2022-02-04 13:36:19 +00:00
|
|
|
case vtc.vaultConfig[api.EnvVaultNamespace] != "company-sso":
|
|
|
|
t.Errorf("EnvVaultNamespace contains unexpected value: %s", vtc.vaultConfig[api.EnvVaultNamespace])
|
|
|
|
case vtc.keyContext[loss.KeyVaultNamespace] != "bobs-place":
|
|
|
|
t.Errorf("KeyVaultNamespace contains unexpected value: %s", vtc.keyContext[loss.KeyVaultNamespace])
|
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"
|
2024-12-20 05:45:55 +00:00
|
|
|
//nolint:forcetypeassert,errcheck // as its a test we dont need to check assertion here.
|
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)
|
2024-04-04 08:55:00 +00:00
|
|
|
require.Equal(t, cm["KMS_PROVIDER"], config["encryptionKMSType"])
|
|
|
|
require.Equal(t, cm["VAULT_ADDR"], config["vaultAddress"])
|
|
|
|
require.Equal(t, cm["VAULT_BACKEND"], config["vaultBackend"])
|
|
|
|
require.Equal(t, cm["VAULT_BACKEND_PATH"], config["vaultBackendPath"])
|
|
|
|
require.Equal(t, cm["VAULT_DESTROY_KEYS"], config["vaultDestroyKeys"])
|
|
|
|
require.Equal(t, cm["VAULT_CACERT"], config["vaultCAFromSecret"])
|
|
|
|
require.Equal(t, cm["VAULT_TLS_SERVER_NAME"], config["vaultTLSServerName"])
|
|
|
|
require.Equal(t, cm["VAULT_CLIENT_CERT"], config["vaultClientCertFromSecret"])
|
|
|
|
require.Equal(t, cm["VAULT_CLIENT_KEY"], config["vaultClientCertKeyFromSecret"])
|
|
|
|
require.Equal(t, cm["VAULT_AUTH_NAMESPACE"], config["vaultAuthNamespace"])
|
|
|
|
require.Equal(t, cm["VAULT_NAMESPACE"], config["vaultNamespace"])
|
|
|
|
require.Equal(t, "false", config["vaultCAVerify"])
|
2021-03-18 12:50:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 10:21:31 +00:00
|
|
|
func TestTransformConfigDefaults(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cm := make(map[string]interface{})
|
|
|
|
cm["KMS_PROVIDER"] = kmsTypeVaultTokens
|
|
|
|
|
|
|
|
config, err := transformConfig(cm)
|
|
|
|
require.NoError(t, err)
|
2024-04-04 08:55:00 +00:00
|
|
|
require.Equal(t, cm["KMS_PROVIDER"], config["encryptionKMSType"])
|
|
|
|
require.Equal(t, vaultDefaultDestroyKeys, config["vaultDestroyKeys"])
|
|
|
|
require.Equal(t, strconv.FormatBool(vaultDefaultCAVerify), config["vaultCAVerify"])
|
2021-10-28 10:21:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
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]
|
2024-04-04 08:55:00 +00:00
|
|
|
require.True(t, ok)
|
2021-03-18 12:50:38 +00:00
|
|
|
}
|
2022-03-01 10:45:37 +00:00
|
|
|
|
|
|
|
func TestSetTenantAuthNamespace(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
vaultNamespace := "tenant"
|
|
|
|
|
|
|
|
t.Run("override vaultAuthNamespace", func(tt *testing.T) {
|
|
|
|
tt.Parallel()
|
|
|
|
|
|
|
|
kms := &vaultTenantConnection{}
|
|
|
|
kms.keyContext = map[string]string{
|
|
|
|
loss.KeyVaultNamespace: "global",
|
|
|
|
}
|
|
|
|
kms.vaultConfig = map[string]interface{}{
|
|
|
|
api.EnvVaultNamespace: "global",
|
|
|
|
}
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"vaultNamespace": vaultNamespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
kms.setTenantAuthNamespace(config)
|
|
|
|
|
2024-04-04 08:55:00 +00:00
|
|
|
require.Equal(tt, vaultNamespace, config["vaultAuthNamespace"])
|
2022-03-01 10:45:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inherit vaultAuthNamespace", func(tt *testing.T) {
|
|
|
|
tt.Parallel()
|
|
|
|
|
|
|
|
vaultAuthNamespace := "configured"
|
|
|
|
|
|
|
|
kms := &vaultTenantConnection{}
|
|
|
|
kms.keyContext = map[string]string{
|
|
|
|
loss.KeyVaultNamespace: vaultAuthNamespace,
|
|
|
|
}
|
|
|
|
kms.vaultConfig = map[string]interface{}{
|
|
|
|
api.EnvVaultNamespace: "global",
|
|
|
|
}
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"vaultNamespace": vaultNamespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
kms.setTenantAuthNamespace(config)
|
|
|
|
|
|
|
|
// when inheriting from the global config, the config of the
|
|
|
|
// tenant should not have vaultAuthNamespace configured
|
2024-04-04 08:55:00 +00:00
|
|
|
require.Nil(tt, config["vaultAuthNamespace"])
|
2022-03-01 10:45:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unset vaultAuthNamespace", func(tt *testing.T) {
|
|
|
|
tt.Parallel()
|
|
|
|
|
|
|
|
kms := &vaultTenantConnection{}
|
|
|
|
kms.keyContext = map[string]string{
|
|
|
|
// no vaultAuthNamespace configured
|
|
|
|
}
|
|
|
|
kms.vaultConfig = map[string]interface{}{
|
|
|
|
api.EnvVaultNamespace: "global",
|
|
|
|
}
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"vaultNamespace": vaultNamespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
kms.setTenantAuthNamespace(config)
|
|
|
|
|
|
|
|
// global vaultAuthNamespace is not set, tenant
|
|
|
|
// vaultAuthNamespace will be configured as vaultNamespace by
|
|
|
|
// default
|
2024-04-04 08:55:00 +00:00
|
|
|
require.Nil(tt, config["vaultAuthNamespace"])
|
2022-03-01 10:45:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no vaultNamespace", func(tt *testing.T) {
|
|
|
|
tt.Parallel()
|
|
|
|
|
|
|
|
kms := &vaultTenantConnection{}
|
|
|
|
kms.keyContext = map[string]string{
|
|
|
|
// no vaultAuthNamespace configured
|
|
|
|
}
|
|
|
|
kms.vaultConfig = map[string]interface{}{
|
|
|
|
// no vaultNamespace configured
|
|
|
|
}
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
// no tenant namespaces configured
|
|
|
|
}
|
|
|
|
|
|
|
|
kms.setTenantAuthNamespace(config)
|
|
|
|
|
2024-04-04 08:55:00 +00:00
|
|
|
require.Nil(tt, config["vaultAuthNamespace"])
|
2022-03-01 10:45:37 +00:00
|
|
|
})
|
|
|
|
}
|