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 (
|
|
|
|
"context"
|
2020-12-15 10:07:28 +00:00
|
|
|
"encoding/json"
|
2020-12-08 10:44:04 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-15 07:11:28 +00:00
|
|
|
"os"
|
2021-02-03 12:42:17 +00:00
|
|
|
"strconv"
|
2020-12-08 10:44:04 +00:00
|
|
|
|
2024-06-21 10:19:06 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/file"
|
2021-08-26 11:15:47 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/k8s"
|
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2022-03-01 10:45:37 +00:00
|
|
|
loss "github.com/libopenstorage/secrets"
|
2020-12-15 07:11:28 +00:00
|
|
|
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
2020-12-08 10:44:04 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-06-11 10:17:51 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2020-12-08 10:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
kmsTypeVaultTokens = "vaulttokens"
|
|
|
|
|
|
|
|
// vaultTokensDefaultConfigName is the name of the Kubernetes ConfigMap
|
|
|
|
// that contains the Vault connection configuration for the tenant.
|
|
|
|
// This ConfigMap is located in the Kubernetes Namespace where the
|
|
|
|
// tenant created the PVC.
|
|
|
|
//
|
|
|
|
// #nosec:G101, value not credential, just references token.
|
|
|
|
vaultTokensDefaultConfigName = "ceph-csi-kms-config"
|
|
|
|
|
|
|
|
// vaultTokensDefaultTokenName is the name of the Kubernetes Secret
|
|
|
|
// that contains the Vault Token for the tenant. This Secret is
|
|
|
|
// located in the Kubernetes Namespace where the tenant created the
|
|
|
|
// PVC.
|
|
|
|
//
|
|
|
|
// #nosec:G101, value not credential, just references token.
|
|
|
|
vaultTokensDefaultTokenName = "ceph-csi-kms-token"
|
|
|
|
|
|
|
|
// vaultTokenSecretKey refers to the key in the Kubernetes Secret that
|
|
|
|
// contains the VAULT_TOKEN.
|
|
|
|
vaultTokenSecretKey = "token"
|
|
|
|
)
|
|
|
|
|
2020-12-15 10:07:28 +00:00
|
|
|
type standardVault struct {
|
|
|
|
KmsPROVIDER string `json:"KMS_PROVIDER"`
|
|
|
|
VaultADDR string `json:"VAULT_ADDR"`
|
2021-07-20 09:18:58 +00:00
|
|
|
VaultBackend string `json:"VAULT_BACKEND"`
|
2020-12-15 10:07:28 +00:00
|
|
|
VaultBackendPath string `json:"VAULT_BACKEND_PATH"`
|
2021-08-02 09:35:26 +00:00
|
|
|
VaultDestroyKeys string `json:"VAULT_DESTROY_KEYS"`
|
2020-12-15 10:07:28 +00:00
|
|
|
VaultCACert string `json:"VAULT_CACERT"`
|
|
|
|
VaultTLSServerName string `json:"VAULT_TLS_SERVER_NAME"`
|
|
|
|
VaultClientCert string `json:"VAULT_CLIENT_CERT"`
|
|
|
|
VaultClientKey string `json:"VAULT_CLIENT_KEY"`
|
2021-07-30 07:53:27 +00:00
|
|
|
VaultAuthNamespace string `json:"VAULT_AUTH_NAMESPACE"`
|
2020-12-15 10:07:28 +00:00
|
|
|
VaultNamespace string `json:"VAULT_NAMESPACE"`
|
2021-02-15 14:50:12 +00:00
|
|
|
VaultSkipVerify string `json:"VAULT_SKIP_VERIFY"`
|
2020-12-15 10:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type vaultTokenConf struct {
|
|
|
|
EncryptionKMSType string `json:"encryptionKMSType"`
|
|
|
|
VaultAddress string `json:"vaultAddress"`
|
2021-07-20 09:18:58 +00:00
|
|
|
VaultBackend string `json:"vaultBackend"`
|
2020-12-15 10:07:28 +00:00
|
|
|
VaultBackendPath string `json:"vaultBackendPath"`
|
2021-08-02 09:35:26 +00:00
|
|
|
VaultDestroyKeys string `json:"vaultDestroyKeys"`
|
2020-12-15 10:07:28 +00:00
|
|
|
VaultCAFromSecret string `json:"vaultCAFromSecret"`
|
|
|
|
VaultTLSServerName string `json:"vaultTLSServerName"`
|
|
|
|
VaultClientCertFromSecret string `json:"vaultClientCertFromSecret"`
|
|
|
|
VaultClientCertKeyFromSecret string `json:"vaultClientCertKeyFromSecret"`
|
2021-07-30 07:53:27 +00:00
|
|
|
VaultAuthNamespace string `json:"vaultAuthNamespace"`
|
2020-12-15 10:07:28 +00:00
|
|
|
VaultNamespace string `json:"vaultNamespace"`
|
2021-02-03 12:42:17 +00:00
|
|
|
VaultCAVerify string `json:"vaultCAVerify"`
|
2020-12-15 10:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultTokenConf) convertStdVaultToCSIConfig(s *standardVault) {
|
|
|
|
v.EncryptionKMSType = s.KmsPROVIDER
|
|
|
|
v.VaultAddress = s.VaultADDR
|
2021-07-20 09:18:58 +00:00
|
|
|
v.VaultBackend = s.VaultBackend
|
2020-12-15 10:07:28 +00:00
|
|
|
v.VaultBackendPath = s.VaultBackendPath
|
2021-08-02 09:35:26 +00:00
|
|
|
v.VaultDestroyKeys = s.VaultDestroyKeys
|
2020-12-15 10:07:28 +00:00
|
|
|
v.VaultCAFromSecret = s.VaultCACert
|
|
|
|
v.VaultClientCertFromSecret = s.VaultClientCert
|
|
|
|
v.VaultClientCertKeyFromSecret = s.VaultClientKey
|
2021-07-30 07:53:27 +00:00
|
|
|
v.VaultAuthNamespace = s.VaultAuthNamespace
|
2020-12-15 10:07:28 +00:00
|
|
|
v.VaultNamespace = s.VaultNamespace
|
|
|
|
v.VaultTLSServerName = s.VaultTLSServerName
|
2021-01-29 08:30:04 +00:00
|
|
|
|
|
|
|
// by default the CA should get verified, only when VaultSkipVerify is
|
|
|
|
// set, verification should be disabled
|
2021-02-15 14:50:12 +00:00
|
|
|
verify, err := strconv.ParseBool(s.VaultSkipVerify)
|
|
|
|
if err == nil {
|
|
|
|
v.VaultCAVerify = strconv.FormatBool(!verify)
|
2021-01-29 08:30:04 +00:00
|
|
|
}
|
2020-12-15 10:07:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
// convertConfig takes the keys/values in standard Vault environment variable
|
|
|
|
// format, and converts them to the format that is used in the configuration
|
|
|
|
// file.
|
2021-04-15 10:49:37 +00:00
|
|
|
// This uses JSON marshaling and unmarshalling to map the Vault environment
|
2021-03-18 12:50:38 +00:00
|
|
|
// configuration into bytes, then in the standardVault struct, which is passed
|
|
|
|
// through convertStdVaultToCSIConfig before converting back to a
|
|
|
|
// map[string]interface{} configuration.
|
|
|
|
//
|
|
|
|
// FIXME: this can surely be simplified?!
|
|
|
|
func transformConfig(svMap map[string]interface{}) (map[string]interface{}, error) {
|
|
|
|
// convert the map to JSON
|
|
|
|
data, err := json.Marshal(svMap)
|
2020-12-15 10:07:28 +00:00
|
|
|
if err != nil {
|
2021-03-18 12:50:38 +00:00
|
|
|
return nil, fmt.Errorf("failed to convert config %T to JSON: %w", svMap, err)
|
2020-12-15 10:07:28 +00:00
|
|
|
}
|
2021-03-18 12:50:38 +00:00
|
|
|
|
2021-10-28 10:21:31 +00:00
|
|
|
// convert the JSON back to a standardVault struct, default values are
|
|
|
|
// set in case the configuration does not provide all options
|
|
|
|
sv := &standardVault{
|
|
|
|
VaultDestroyKeys: vaultDefaultDestroyKeys,
|
|
|
|
VaultNamespace: vaultDefaultNamespace,
|
|
|
|
VaultSkipVerify: strconv.FormatBool(!vaultDefaultCAVerify),
|
|
|
|
}
|
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
err = json.Unmarshal(data, sv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to Unmarshal the vault configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert the standardVault struct to a vaultTokenConf struct
|
|
|
|
vc := vaultTokenConf{}
|
|
|
|
vc.convertStdVaultToCSIConfig(sv)
|
|
|
|
data, err = json.Marshal(vc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to Marshal the CSI vault configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert the vaultTokenConf struct to a map[string]interface{}
|
|
|
|
jsonMap := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(data, &jsonMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to Unmarshal the CSI vault configuration: %w", err)
|
2020-12-15 10:07:28 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
return jsonMap, nil
|
2020-12-15 10:07:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
/*
|
|
|
|
VaultTokens represents a Hashicorp Vault KMS configuration that provides a
|
|
|
|
Token per tenant.
|
|
|
|
|
|
|
|
Example JSON structure in the KMS config is,
|
2023-06-02 09:49:22 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"vault-with-tokens": {
|
|
|
|
"encryptionKMSType": "vaulttokens",
|
|
|
|
"vaultAddress": "http://vault.default.svc.cluster.local:8200",
|
|
|
|
"vaultBackend": "kv-v2",
|
|
|
|
"vaultBackendPath": "secret/",
|
|
|
|
"vaultTLSServerName": "vault.default.svc.cluster.local",
|
|
|
|
"vaultCAFromSecret": "vault-ca",
|
|
|
|
"vaultClientCertFromSecret": "vault-client-cert",
|
|
|
|
"vaultClientCertKeyFromSecret": "vault-client-cert-key",
|
|
|
|
"vaultCAVerify": "false",
|
|
|
|
"tenantConfigName": "ceph-csi-kms-config",
|
|
|
|
"tenantTokenName": "ceph-csi-kms-token",
|
|
|
|
"tenants": {
|
|
|
|
"my-app": {
|
|
|
|
"vaultAddress": "https://vault.example.com",
|
|
|
|
"vaultCAVerify": "true"
|
|
|
|
},
|
|
|
|
"an-other-app": {
|
|
|
|
"tenantTokenName": "storage-encryption-token"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...
|
|
|
|
}.
|
2020-12-08 10:44:04 +00:00
|
|
|
*/
|
2021-06-11 10:17:51 +00:00
|
|
|
type vaultTenantConnection struct {
|
2020-12-08 10:44:04 +00:00
|
|
|
vaultConnection
|
2022-01-24 12:57:54 +00:00
|
|
|
integratedDEK
|
2020-12-08 10:44:04 +00:00
|
|
|
|
2021-06-11 10:17:51 +00:00
|
|
|
client *kubernetes.Clientset
|
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
// Tenant is the name of the owner of the volume
|
|
|
|
Tenant string
|
|
|
|
// ConfigName is the name of the ConfigMap in the Tenants Kubernetes Namespace
|
|
|
|
ConfigName string
|
2021-07-13 10:59:29 +00:00
|
|
|
|
|
|
|
// tenantConfigOptionFilter ise used to filter configuration options
|
|
|
|
// for the KMS that are provided by the ConfigMap in the Tenants
|
|
|
|
// Namespace. It defaults to isTenantConfigOption() as setup by the
|
|
|
|
// init() function.
|
|
|
|
tenantConfigOptionFilter func(string) bool
|
2021-06-11 10:17:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:02:51 +00:00
|
|
|
type vaultTokensKMS struct {
|
2021-06-11 10:17:51 +00:00
|
|
|
vaultTenantConnection
|
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
// TokenName is the name of the Secret in the Tenants Kubernetes Namespace
|
|
|
|
TokenName string
|
|
|
|
}
|
|
|
|
|
2021-08-26 11:43:20 +00:00
|
|
|
var _ = RegisterProvider(Provider{
|
2021-03-18 12:50:38 +00:00
|
|
|
UniqueID: kmsTypeVaultTokens,
|
|
|
|
Initializer: initVaultTokensKMS,
|
|
|
|
})
|
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
// InitVaultTokensKMS returns an interface to HashiCorp Vault KMS.
|
2021-08-26 11:43:20 +00:00
|
|
|
func initVaultTokensKMS(args ProviderInitArgs) (EncryptionKMS, error) {
|
2021-03-18 12:50:38 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
config := args.Config
|
2021-05-06 09:49:27 +00:00
|
|
|
if _, ok := config[kmsProviderKey]; ok {
|
2021-03-18 12:50:38 +00:00
|
|
|
// configuration comes from the ConfigMap, needs to be
|
|
|
|
// converted to vaultTokenConf type
|
|
|
|
config, err = transformConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to convert configuration: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 13:02:51 +00:00
|
|
|
kms := &vaultTokensKMS{}
|
2021-07-13 10:59:29 +00:00
|
|
|
kms.vaultTenantConnection.init()
|
2021-03-29 12:19:19 +00:00
|
|
|
err = kms.initConnection(config)
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize Vault connection: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set default values for optional config options
|
|
|
|
kms.ConfigName = vaultTokensDefaultConfigName
|
|
|
|
kms.TokenName = vaultTokensDefaultTokenName
|
|
|
|
|
2020-12-15 07:11:28 +00:00
|
|
|
err = kms.parseConfig(config)
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-11 10:17:51 +00:00
|
|
|
err = kms.setTokenName(config)
|
|
|
|
if err != nil && !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
return nil, fmt.Errorf("failed to set the TokenName from global config %q: %w",
|
|
|
|
kms.ConfigName, err)
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
// fetch the configuration for the tenant
|
2021-03-18 12:50:38 +00:00
|
|
|
if args.Tenant != "" {
|
2021-07-13 10:59:29 +00:00
|
|
|
err = kms.configureTenant(config, args.Tenant)
|
2020-12-15 08:11:53 +00:00
|
|
|
if err != nil {
|
2021-07-13 10:59:29 +00:00
|
|
|
return nil, err
|
2021-06-11 10:17:51 +00:00
|
|
|
}
|
2020-12-08 10:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the Vault Token from the Secret (TokenName) in the Kubernetes
|
|
|
|
// Namespace (tenant)
|
2021-06-11 10:17:51 +00:00
|
|
|
kms.vaultConfig[api.EnvVaultToken], err = kms.getToken()
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil {
|
2021-03-18 12:50:38 +00:00
|
|
|
return nil, fmt.Errorf("failed fetching token from %s/%s: %w", args.Tenant, kms.TokenName, err)
|
2020-12-08 10:44:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 07:11:28 +00:00
|
|
|
err = kms.initCertificates(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize Vault certificates: %w", err)
|
|
|
|
}
|
2020-12-08 10:44:04 +00:00
|
|
|
// connect to the Vault service
|
|
|
|
err = kms.connectVault()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return kms, nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 13:02:51 +00:00
|
|
|
func (kms *vaultTokensKMS) configureTenant(config map[string]interface{}, tenant string) error {
|
2021-07-13 10:59:29 +00:00
|
|
|
kms.Tenant = tenant
|
|
|
|
tenantConfig, found := fetchTenantConfig(config, tenant)
|
|
|
|
if found {
|
|
|
|
// override connection details from the tenant
|
|
|
|
err := kms.parseConfig(tenantConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kms.setTokenName(tenantConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set the TokenName for tenant (%s): %w",
|
|
|
|
kms.Tenant, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the ConfigMap from the Tenant and apply the options
|
|
|
|
tenantConfig, err := kms.parseTenantConfig()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse config for tenant: %w", err)
|
|
|
|
} else if tenantConfig != nil {
|
|
|
|
err = kms.parseConfig(tenantConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse config (%s) for tenant (%s): %w",
|
|
|
|
kms.ConfigName, kms.Tenant, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kms.setTokenName(tenantConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set the TokenName from %s for tenant (%s): %w",
|
|
|
|
kms.ConfigName, kms.Tenant, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vtc *vaultTenantConnection) init() {
|
|
|
|
vtc.tenantConfigOptionFilter = isTenantConfigOption
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:44:04 +00:00
|
|
|
// parseConfig updates the kms.vaultConfig with the options from config and
|
|
|
|
// secrets. This method can be called multiple times, i.e. to override
|
|
|
|
// configuration options from tenants.
|
2021-06-11 10:17:51 +00:00
|
|
|
func (vtc *vaultTenantConnection) parseConfig(config map[string]interface{}) error {
|
|
|
|
err := vtc.initConnection(config)
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-11 10:17:51 +00:00
|
|
|
err = setConfigString(&vtc.ConfigName, config, "tenantConfigName")
|
2020-12-08 10:44:04 +00:00
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-11 10:17:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setTokenName updates the kms.TokenName with the options from config. This
|
|
|
|
// method can be called multiple times, i.e. to override configuration options
|
|
|
|
// from tenants.
|
2022-01-24 13:02:51 +00:00
|
|
|
func (kms *vaultTokensKMS) setTokenName(config map[string]interface{}) error {
|
2021-06-11 10:17:51 +00:00
|
|
|
err := setConfigString(&kms.TokenName, config, "tenantTokenName")
|
2020-12-08 10:44:04 +00:00
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-15 07:11:28 +00:00
|
|
|
// initCertificates updates the kms.vaultConfig with the options from config
|
|
|
|
// it calls the kubernetes secrets and get the required data.
|
|
|
|
|
2023-06-02 08:59:52 +00:00
|
|
|
//nolint:gocyclo,cyclop // iterating through many config options, not complex at all.
|
2021-06-11 10:17:51 +00:00
|
|
|
func (vtc *vaultTenantConnection) initCertificates(config map[string]interface{}) error {
|
2020-12-15 07:11:28 +00:00
|
|
|
vaultConfig := make(map[string]interface{})
|
|
|
|
|
|
|
|
csiNamespace := os.Getenv("POD_NAMESPACE")
|
|
|
|
vaultCAFromSecret := "" // optional
|
|
|
|
err := setConfigString(&vaultCAFromSecret, config, "vaultCAFromSecret")
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ignore errConfigOptionMissing, no default was set
|
|
|
|
if vaultCAFromSecret != "" {
|
2021-06-11 10:17:51 +00:00
|
|
|
cert, cErr := vtc.getCertificate(vtc.Tenant, vaultCAFromSecret, "cert")
|
2021-02-03 12:45:39 +00:00
|
|
|
if cErr != nil && !apierrs.IsNotFound(cErr) {
|
2020-12-15 07:11:28 +00:00
|
|
|
return fmt.Errorf("failed to get CA certificate from secret %s: %w", vaultCAFromSecret, cErr)
|
|
|
|
}
|
|
|
|
// if the certificate is not present in tenant namespace get it from
|
|
|
|
// cephcsi pod namespace
|
|
|
|
if apierrs.IsNotFound(cErr) {
|
2021-06-11 10:17:51 +00:00
|
|
|
cert, cErr = vtc.getCertificate(csiNamespace, vaultCAFromSecret, "cert")
|
2020-12-15 07:11:28 +00:00
|
|
|
if cErr != nil {
|
|
|
|
return fmt.Errorf("failed to get CA certificate from secret %s: %w", vaultCAFromSecret, cErr)
|
|
|
|
}
|
|
|
|
}
|
2024-06-21 10:19:06 +00:00
|
|
|
cer, ferr := file.CreateTempFile("vault-ca-cert", cert)
|
|
|
|
if ferr != nil {
|
|
|
|
return fmt.Errorf("failed to create temporary file for Vault CA: %w", ferr)
|
2020-12-15 07:11:28 +00:00
|
|
|
}
|
2024-06-21 10:19:06 +00:00
|
|
|
vaultConfig[api.EnvVaultCACert] = cer.Name()
|
2020-12-15 07:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vaultClientCertFromSecret := "" // optional
|
|
|
|
err = setConfigString(&vaultClientCertFromSecret, config, "vaultClientCertFromSecret")
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ignore errConfigOptionMissing, no default was set
|
|
|
|
if vaultClientCertFromSecret != "" {
|
2021-06-11 10:17:51 +00:00
|
|
|
cert, cErr := vtc.getCertificate(vtc.Tenant, vaultClientCertFromSecret, "cert")
|
2020-12-15 07:11:28 +00:00
|
|
|
if cErr != nil && !apierrs.IsNotFound(cErr) {
|
|
|
|
return fmt.Errorf("failed to get client certificate from secret %s: %w", vaultClientCertFromSecret, cErr)
|
|
|
|
}
|
|
|
|
// if the certificate is not present in tenant namespace get it from
|
|
|
|
// cephcsi pod namespace
|
|
|
|
if apierrs.IsNotFound(cErr) {
|
2021-06-11 10:17:51 +00:00
|
|
|
cert, cErr = vtc.getCertificate(csiNamespace, vaultClientCertFromSecret, "cert")
|
2020-12-15 07:11:28 +00:00
|
|
|
if cErr != nil {
|
|
|
|
return fmt.Errorf("failed to get client certificate from secret %s: %w", vaultCAFromSecret, cErr)
|
|
|
|
}
|
|
|
|
}
|
2024-06-21 10:19:06 +00:00
|
|
|
cer, ferr := file.CreateTempFile("vault-ca-cert", cert)
|
|
|
|
if ferr != nil {
|
|
|
|
return fmt.Errorf("failed to create temporary file for Vault client certificate: %w", ferr)
|
2020-12-15 07:11:28 +00:00
|
|
|
}
|
2024-06-21 10:19:06 +00:00
|
|
|
vaultConfig[api.EnvVaultClientCert] = cer.Name()
|
2020-12-15 07:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vaultClientCertKeyFromSecret := "" // optional
|
|
|
|
err = setConfigString(&vaultClientCertKeyFromSecret, config, "vaultClientCertKeyFromSecret")
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore errConfigOptionMissing, no default was set
|
|
|
|
if vaultClientCertKeyFromSecret != "" {
|
2021-06-11 10:17:51 +00:00
|
|
|
certKey, err := vtc.getCertificate(vtc.Tenant, vaultClientCertKeyFromSecret, "key")
|
2020-12-15 07:11:28 +00:00
|
|
|
if err != nil && !apierrs.IsNotFound(err) {
|
2021-06-25 12:15:08 +00:00
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to get client certificate key from secret %s: %w",
|
|
|
|
vaultClientCertKeyFromSecret,
|
|
|
|
err)
|
2020-12-15 07:11:28 +00:00
|
|
|
}
|
|
|
|
// if the certificate is not present in tenant namespace get it from
|
|
|
|
// cephcsi pod namespace
|
|
|
|
if apierrs.IsNotFound(err) {
|
2021-06-11 10:17:51 +00:00
|
|
|
certKey, err = vtc.getCertificate(csiNamespace, vaultClientCertKeyFromSecret, "key")
|
2020-12-15 07:11:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get client certificate key from secret %s: %w", vaultCAFromSecret, err)
|
|
|
|
}
|
|
|
|
}
|
2024-06-21 10:19:06 +00:00
|
|
|
ckey, err := file.CreateTempFile("vault-client-cert-key", certKey)
|
2020-12-15 07:11:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create temporary file for Vault client cert key: %w", err)
|
|
|
|
}
|
2024-06-21 10:19:06 +00:00
|
|
|
vaultConfig[api.EnvVaultClientKey] = ckey.Name()
|
2020-12-15 07:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range vaultConfig {
|
2021-06-11 10:17:51 +00:00
|
|
|
vtc.vaultConfig[key] = value
|
2020-12-15 07:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:18:37 +00:00
|
|
|
func (vtc *vaultTenantConnection) getK8sClient() (*kubernetes.Clientset, error) {
|
2021-06-11 10:17:51 +00:00
|
|
|
if vtc.client == nil {
|
2021-08-31 12:18:37 +00:00
|
|
|
client, err := k8s.NewK8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vtc.client = client
|
2021-06-11 10:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 12:18:37 +00:00
|
|
|
return vtc.client, nil
|
2021-06-11 10:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// FetchDEK returns passphrase from Vault. The passphrase is stored in a
|
2020-12-08 10:44:04 +00:00
|
|
|
// data.data.passphrase structure.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (vtc *vaultTenantConnection) FetchDEK(ctx context.Context, key string) (string, error) {
|
2023-10-17 04:59:04 +00:00
|
|
|
// Since the second return variable loss.Version is not used, there it is ignored.
|
|
|
|
s, _, err := vtc.secrets.GetSecret(key, vtc.keyContext)
|
2021-02-15 09:56:22 +00:00
|
|
|
if err != nil {
|
2020-12-08 10:44:04 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, ok := s["data"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("failed parsing data for get passphrase request for %s", key)
|
|
|
|
}
|
|
|
|
passphrase, ok := data["passphrase"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("failed parsing passphrase for get passphrase request for %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return passphrase, nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// StoreDEK saves new passphrase in Vault.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (vtc *vaultTenantConnection) StoreDEK(ctx context.Context, key, value string) error {
|
2020-12-08 10:44:04 +00:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"data": map[string]string{
|
|
|
|
"passphrase": value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-17 04:59:04 +00:00
|
|
|
// Since the first return variable loss.Version is not used, there it is ignored.
|
|
|
|
_, err := vtc.secrets.PutSecret(key, data, vtc.keyContext)
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("saving passphrase at %s request to vault failed: %w", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// RemoveDEK deletes passphrase from Vault.
|
2024-03-04 15:13:31 +00:00
|
|
|
func (vtc *vaultTenantConnection) RemoveDEK(ctx context.Context, key string) error {
|
2021-08-02 09:35:26 +00:00
|
|
|
err := vtc.secrets.DeleteSecret(key, vtc.getDeleteKeyContext())
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete passphrase at %s request to vault failed: %w", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 13:02:51 +00:00
|
|
|
func (kms *vaultTokensKMS) getToken() (string, error) {
|
2021-08-31 12:18:37 +00:00
|
|
|
c, err := kms.getK8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-06-11 10:17:51 +00:00
|
|
|
secret, err := c.CoreV1().Secrets(kms.Tenant).Get(context.TODO(), kms.TokenName, metav1.GetOptions{})
|
2020-12-08 10:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
token, ok := secret.Data[vaultTokenSecretKey]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("failed to parse token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(token), nil
|
|
|
|
}
|
2020-12-15 07:11:28 +00:00
|
|
|
|
2021-06-11 10:17:51 +00:00
|
|
|
func (vtc *vaultTenantConnection) getCertificate(tenant, secretName, key string) (string, error) {
|
2021-08-31 12:18:37 +00:00
|
|
|
c, err := vtc.getK8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-12-15 07:11:28 +00:00
|
|
|
secret, err := c.CoreV1().Secrets(tenant).Get(context.TODO(), secretName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, ok := secret.Data[key]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("failed to parse certificates")
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(cert), nil
|
|
|
|
}
|
2020-12-15 08:11:53 +00:00
|
|
|
|
|
|
|
// isTenantConfigOption return true if a tenant may (re)configure the option in
|
|
|
|
// their own ConfigMap, false otherwise.
|
|
|
|
func isTenantConfigOption(opt string) bool {
|
|
|
|
switch opt {
|
|
|
|
case "vaultAddress":
|
2021-07-20 09:18:58 +00:00
|
|
|
case "vaultBackend":
|
2020-12-15 08:11:53 +00:00
|
|
|
case "vaultBackendPath":
|
2021-07-30 07:53:27 +00:00
|
|
|
case "vaultAuthNamespace":
|
|
|
|
case "vaultNamespace":
|
2021-08-02 09:35:26 +00:00
|
|
|
case "vaultDestroyKeys":
|
2020-12-15 08:11:53 +00:00
|
|
|
case "vaultTLSServerName":
|
|
|
|
case "vaultCAFromSecret":
|
|
|
|
case "vaultCAVerify":
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTenantConfig gets the optional ConfigMap from the Tenants namespace,
|
|
|
|
// and applies the allowable options (see isTenantConfigOption) to the KMS
|
|
|
|
// configuration.
|
2021-07-13 10:59:29 +00:00
|
|
|
func (vtc *vaultTenantConnection) parseTenantConfig() (map[string]interface{}, error) {
|
2021-06-11 10:17:51 +00:00
|
|
|
if vtc.Tenant == "" || vtc.ConfigName == "" {
|
2021-07-13 10:59:29 +00:00
|
|
|
return nil, nil
|
2020-12-15 08:11:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 08:56:54 +00:00
|
|
|
// fetch the ConfigMap from the tenants namespace
|
2021-08-31 12:18:37 +00:00
|
|
|
c, err := vtc.getK8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-11 10:17:51 +00:00
|
|
|
cm, err := c.CoreV1().ConfigMaps(vtc.Tenant).Get(context.TODO(),
|
|
|
|
vtc.ConfigName, metav1.GetOptions{})
|
2020-12-15 08:11:53 +00:00
|
|
|
if apierrs.IsNotFound(err) {
|
|
|
|
// the tenant did not (re)configure any options
|
2021-07-13 10:59:29 +00:00
|
|
|
return nil, nil
|
2020-12-15 08:11:53 +00:00
|
|
|
} else if err != nil {
|
2021-07-13 10:59:29 +00:00
|
|
|
return nil, fmt.Errorf("failed to get config (%s) for tenant (%s): %w",
|
2021-06-11 10:17:51 +00:00
|
|
|
vtc.ConfigName, vtc.Tenant, err)
|
2020-12-15 08:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a new map with config options, but only include the options
|
|
|
|
// that a tenant may (re)configure
|
|
|
|
config := make(map[string]interface{})
|
|
|
|
for k, v := range cm.Data {
|
2021-07-13 10:59:29 +00:00
|
|
|
if vtc.tenantConfigOptionFilter(k) {
|
2020-12-15 08:11:53 +00:00
|
|
|
config[k] = v
|
|
|
|
} // else: silently ignore the option
|
|
|
|
}
|
|
|
|
if len(config) == 0 {
|
|
|
|
// no options configured by the tenant
|
2021-07-13 10:59:29 +00:00
|
|
|
return nil, nil
|
2020-12-15 08:11:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 10:45:37 +00:00
|
|
|
vtc.setTenantAuthNamespace(config)
|
|
|
|
|
2021-07-13 10:59:29 +00:00
|
|
|
return config, nil
|
2020-12-15 08:11:53 +00:00
|
|
|
}
|
2021-04-05 04:31:47 +00:00
|
|
|
|
2022-03-01 10:45:37 +00:00
|
|
|
// setTenantAuthNamespace configures the vaultAuthNamespace for the tenant.
|
|
|
|
// vaultAuthNamespace defaults to vaultNamespace from the global configuration,
|
|
|
|
// even if the tenant has vaultNamespace configured. Users expect to have the
|
|
|
|
// vaultAuthNamespace updated when they configure vaultNamespace, if
|
|
|
|
// vaultAuthNamespace was not explicitly set in the global configuration.
|
|
|
|
func (vtc *vaultTenantConnection) setTenantAuthNamespace(tenantConfig map[string]interface{}) {
|
|
|
|
vaultAuthNamespace, ok := vtc.keyContext[loss.KeyVaultNamespace]
|
|
|
|
if !ok {
|
|
|
|
// nothing to do, global connection config does not have the
|
|
|
|
// vaultAuthNamespace set
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vaultNamespace, ok := vtc.vaultConfig[api.EnvVaultNamespace]
|
|
|
|
if !ok {
|
|
|
|
// nothing to do, global connection config does not have the
|
|
|
|
// vaultNamespace set, not overriding vaultAuthNamespace with
|
|
|
|
// vaultNamespace from the tenant
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if vaultAuthNamespace != vaultNamespace {
|
|
|
|
// vaultAuthNamespace and vaultNamespace have been configured
|
|
|
|
// differently in the global connection. Not going to override
|
|
|
|
// those pre-defined options if the tenantConfig does not have
|
|
|
|
// them set.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we reached here, we need to make sure that the vaultAuthNamespace
|
|
|
|
// gets configured for the tenant, in case the tenant config has
|
|
|
|
// vaultNamespace set
|
|
|
|
|
|
|
|
_, ok = tenantConfig["vaultAuthNamespace"]
|
|
|
|
if ok {
|
|
|
|
// the tenant already has vaultAuthNamespace configured, no
|
|
|
|
// action needed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tenantNamespace, ok := tenantConfig["vaultNamespace"]
|
|
|
|
if !ok {
|
|
|
|
// the tenant does not have vaultNamespace configured, no need
|
|
|
|
// to set vaultAuthNamespace either
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// the tenant has vaultNamespace configured, use that for
|
|
|
|
// vaultAuthNamespace as well
|
|
|
|
tenantConfig["vaultAuthNamespace"] = tenantNamespace
|
|
|
|
}
|
|
|
|
|
2021-04-05 04:31:47 +00:00
|
|
|
// fetchTenantConfig fetches the configuration for the tenant if it exists.
|
|
|
|
func fetchTenantConfig(config map[string]interface{}, tenant string) (map[string]interface{}, bool) {
|
|
|
|
tenantsMap, ok := config["tenants"]
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
// tenants is a map per tenant, containing key/values
|
|
|
|
tenants, ok := tenantsMap.(map[string]map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
// get the map for the tenant of the current operation
|
|
|
|
tenantConfig, ok := tenants[tenant]
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-04-05 04:31:47 +00:00
|
|
|
return tenantConfig, true
|
|
|
|
}
|