2020-01-29 11:44:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-11-16 09:05:23 +00:00
|
|
|
"errors"
|
2020-01-29 11:44:45 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-11-16 09:05:23 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-06 16:23:14 +00:00
|
|
|
"strconv"
|
2020-01-29 11:44:45 +00:00
|
|
|
"strings"
|
2020-11-16 09:05:23 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
loss "github.com/libopenstorage/secrets"
|
|
|
|
"github.com/libopenstorage/secrets/vault"
|
2020-01-29 11:44:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-14 10:26:31 +00:00
|
|
|
kmsTypeVault = "vault"
|
|
|
|
|
2020-01-29 11:44:45 +00:00
|
|
|
// path to service account token that will be used to authenticate with Vault
|
|
|
|
// #nosec
|
|
|
|
serviceAccountTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
|
|
|
|
|
2021-07-08 14:59:34 +00:00
|
|
|
// vault configuration defaults.
|
2020-01-29 11:44:45 +00:00
|
|
|
vaultDefaultAuthPath = "/v1/auth/kubernetes/login"
|
|
|
|
vaultDefaultRole = "csi-kubernetes"
|
|
|
|
vaultDefaultNamespace = ""
|
|
|
|
vaultDefaultPassphrasePath = ""
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultDefaultCAVerify = "true"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errConfigOptionMissing = errors.New("configuration option not set")
|
|
|
|
errConfigOptionInvalid = errors.New("configuration option not valid")
|
2020-01-29 11:44:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2020-03-26 12:45:45 +00:00
|
|
|
VaultKMS represents a Hashicorp Vault KMS configuration
|
2020-01-29 11:44:45 +00:00
|
|
|
|
|
|
|
Example JSON structure in the KMS config is,
|
2020-02-06 16:23:14 +00:00
|
|
|
{
|
|
|
|
"local_vault_unique_identifier": {
|
|
|
|
"encryptionKMSType": "vault",
|
2020-01-29 11:44:45 +00:00
|
|
|
"vaultAddress": "https://127.0.0.1:8500",
|
|
|
|
"vaultAuthPath": "/v1/auth/kubernetes/login",
|
|
|
|
"vaultRole": "csi-kubernetes",
|
|
|
|
"vaultNamespace": "",
|
|
|
|
"vaultPassphraseRoot": "/v1/secret",
|
|
|
|
"vaultPassphrasePath": "",
|
|
|
|
"vaultCAVerify": true,
|
|
|
|
"vaultCAFromSecret": "vault-ca"
|
|
|
|
},
|
|
|
|
...
|
2020-07-19 12:21:03 +00:00
|
|
|
}.
|
2020-01-29 11:44:45 +00:00
|
|
|
*/
|
2020-11-25 11:45:15 +00:00
|
|
|
|
|
|
|
type vaultConnection struct {
|
2021-03-19 15:21:48 +00:00
|
|
|
secrets loss.Secrets
|
|
|
|
vaultConfig map[string]interface{}
|
|
|
|
keyContext map[string]string
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type VaultKMS struct {
|
|
|
|
vaultConnection
|
2021-03-01 15:35:35 +00:00
|
|
|
integratedDEK
|
2020-11-16 09:05:23 +00:00
|
|
|
|
|
|
|
// vaultPassphrasePath (VPP) used to be added before the "key" of the
|
|
|
|
// secret (like /v1/secret/data/<VPP>/key)
|
|
|
|
vaultPassphrasePath string
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
// setConfigString fetches a value from a configuration map and converts it to
|
|
|
|
// a string.
|
|
|
|
//
|
|
|
|
// If the value is not available, *option is not adjusted and
|
|
|
|
// errConfigOptionMissing is returned.
|
|
|
|
// In case the value is available, but can not be converted to a string,
|
|
|
|
// errConfigOptionInvalid is returned.
|
|
|
|
func setConfigString(option *string, config map[string]interface{}, key string) error {
|
|
|
|
value, ok := config[key]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%w: %s", errConfigOptionMissing, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok := value.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%w: expected string for %q, but got %T",
|
|
|
|
errConfigOptionInvalid, key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
*option = s
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:21:32 +00:00
|
|
|
// initConnection sets VAULT_* environment variables in the vc.vaultConfig map,
|
|
|
|
// these settings will be used when connecting to the Vault service with
|
|
|
|
// vc.connectVault().
|
|
|
|
//
|
|
|
|
// nolint:gocyclo // iterating through many config options, not complex at all.
|
2021-03-19 15:21:48 +00:00
|
|
|
func (vc *vaultConnection) initConnection(config map[string]interface{}) error {
|
2020-11-25 11:45:15 +00:00
|
|
|
vaultConfig := make(map[string]interface{})
|
|
|
|
keyContext := make(map[string]string)
|
|
|
|
|
2020-12-02 20:26:08 +00:00
|
|
|
firstInit := (vc.vaultConfig == nil)
|
|
|
|
|
|
|
|
vaultAddress := "" // required
|
2020-12-01 07:39:15 +00:00
|
|
|
err := setConfigString(&vaultAddress, config, "vaultAddress")
|
2020-12-02 20:26:08 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, errConfigOptionInvalid):
|
|
|
|
return err
|
|
|
|
case firstInit && errors.Is(err, errConfigOptionMissing):
|
2020-12-01 07:39:15 +00:00
|
|
|
return err
|
2020-12-02 20:26:08 +00:00
|
|
|
case !errors.Is(err, errConfigOptionMissing):
|
|
|
|
vaultConfig[api.EnvVaultAddress] = vaultAddress
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
2020-12-02 20:26:08 +00:00
|
|
|
// default: !firstInit
|
2020-11-25 11:45:15 +00:00
|
|
|
|
2021-07-20 09:18:58 +00:00
|
|
|
vaultBackend := "" // optional
|
|
|
|
err = setConfigString(&vaultBackend, config, "vaultBackend")
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set the option if the value was not invalid
|
|
|
|
if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
vaultConfig[vault.VaultBackendKey] = vaultBackend
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:21:32 +00:00
|
|
|
vaultBackendPath := "" // optional
|
|
|
|
err = setConfigString(&vaultBackendPath, config, "vaultBackendPath")
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set the option if the value was not invalid
|
|
|
|
if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
vaultConfig[vault.VaultBackendPathKey] = vaultBackendPath
|
|
|
|
}
|
|
|
|
|
|
|
|
vaultTLSServerName := "" // optional
|
|
|
|
err = setConfigString(&vaultTLSServerName, config, "vaultTLSServerName")
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set the option if the value was not invalid
|
|
|
|
if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
vaultConfig[api.EnvVaultTLSServerName] = vaultTLSServerName
|
|
|
|
}
|
|
|
|
|
2020-12-02 20:26:08 +00:00
|
|
|
vaultNamespace := vaultDefaultNamespace // optional
|
2020-12-01 07:39:15 +00:00
|
|
|
err = setConfigString(&vaultNamespace, config, "vaultNamespace")
|
2020-12-02 20:26:08 +00:00
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
2020-12-01 07:39:15 +00:00
|
|
|
return err
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
2020-12-02 20:26:08 +00:00
|
|
|
// set the option if the value was not invalid
|
|
|
|
if firstInit || !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
vaultConfig[api.EnvVaultNamespace] = vaultNamespace
|
|
|
|
keyContext[loss.KeyVaultNamespace] = vaultNamespace
|
|
|
|
}
|
2020-11-25 11:45:15 +00:00
|
|
|
|
2020-12-02 20:26:08 +00:00
|
|
|
verifyCA := vaultDefaultCAVerify // optional
|
2020-12-01 07:39:15 +00:00
|
|
|
err = setConfigString(&verifyCA, config, "vaultCAVerify")
|
2020-12-02 20:26:08 +00:00
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
2020-12-01 07:39:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-12-02 20:26:08 +00:00
|
|
|
if firstInit || !errors.Is(err, errConfigOptionMissing) {
|
2021-05-11 12:43:39 +00:00
|
|
|
var vaultCAVerify bool
|
2020-12-02 20:26:08 +00:00
|
|
|
vaultCAVerify, err = strconv.ParseBool(verifyCA)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse 'vaultCAVerify': %w", err)
|
|
|
|
}
|
2021-02-02 09:49:30 +00:00
|
|
|
vaultConfig[api.EnvVaultInsecure] = strconv.FormatBool(!vaultCAVerify)
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 20:26:08 +00:00
|
|
|
vaultCAFromSecret := "" // optional
|
2020-12-01 07:39:15 +00:00
|
|
|
err = setConfigString(&vaultCAFromSecret, config, "vaultCAFromSecret")
|
2020-12-02 20:26:08 +00:00
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-25 11:45:15 +00:00
|
|
|
|
2020-12-02 20:26:08 +00:00
|
|
|
// update the existing config only if no config is available yet
|
|
|
|
if vc.keyContext != nil {
|
|
|
|
for key, value := range keyContext {
|
|
|
|
vc.keyContext[key] = value
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vc.keyContext = keyContext
|
|
|
|
}
|
|
|
|
if vc.vaultConfig != nil {
|
|
|
|
for key, value := range vaultConfig {
|
|
|
|
vc.vaultConfig[key] = value
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vc.vaultConfig = vaultConfig
|
|
|
|
}
|
2020-11-25 11:45:15 +00:00
|
|
|
|
|
|
|
return nil
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 07:11:28 +00:00
|
|
|
// initCertificates sets VAULT_* environment variables in the vc.vaultConfig map,
|
|
|
|
// these settings will be used when connecting to the Vault service with
|
|
|
|
// vc.connectVault().
|
|
|
|
//
|
|
|
|
func (vc *vaultConnection) initCertificates(config map[string]interface{}, secrets map[string]string) error {
|
|
|
|
vaultConfig := make(map[string]interface{})
|
|
|
|
|
|
|
|
vaultCAFromSecret := "" // optional
|
|
|
|
err := setConfigString(&vaultCAFromSecret, config, "vaultCAFromSecret")
|
|
|
|
if errors.Is(err, errConfigOptionInvalid) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ignore errConfigOptionMissing, no default was set
|
|
|
|
if vaultCAFromSecret != "" {
|
|
|
|
caPEM, ok := secrets[vaultCAFromSecret]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing vault CA in secret %s", vaultCAFromSecret)
|
|
|
|
}
|
|
|
|
|
|
|
|
vaultConfig[api.EnvVaultCACert], err = createTempFile("vault-ca-cert", []byte(caPEM))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create temporary file for Vault CA: %w", err)
|
|
|
|
}
|
|
|
|
// update the existing config
|
|
|
|
for key, value := range vaultConfig {
|
|
|
|
vc.vaultConfig[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:53:17 +00:00
|
|
|
// connectVault creates a new connection to Vault. This should be called after
|
|
|
|
// filling vc.vaultConfig.
|
|
|
|
func (vc *vaultConnection) connectVault() error {
|
|
|
|
v, err := vault.New(vc.vaultConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating new Vault Secrets: %w", err)
|
|
|
|
}
|
|
|
|
vc.secrets = v
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-03 08:25:52 +00:00
|
|
|
// Destroy frees allocated resources. For a vaultConnection that means removing
|
|
|
|
// the created temporary files.
|
|
|
|
func (vc *vaultConnection) Destroy() {
|
|
|
|
if vc.vaultConfig != nil {
|
|
|
|
tmpFile, ok := vc.vaultConfig[api.EnvVaultCACert]
|
|
|
|
if ok {
|
|
|
|
// ignore error on failure to remove tmpfile (gosec complains)
|
|
|
|
_ = os.Remove(tmpFile.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
var _ = RegisterKMSProvider(KMSProvider{
|
|
|
|
UniqueID: kmsTypeVault,
|
|
|
|
Initializer: initVaultKMS,
|
|
|
|
})
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// InitVaultKMS returns an interface to HashiCorp Vault KMS.
|
2021-03-18 12:50:38 +00:00
|
|
|
func initVaultKMS(args KMSInitializerArgs) (EncryptionKMS, error) {
|
2020-02-06 16:23:14 +00:00
|
|
|
kms := &VaultKMS{}
|
2021-03-19 15:21:48 +00:00
|
|
|
err := kms.initConnection(args.Config)
|
2020-11-25 11:45:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize Vault connection: %w", err)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2020-11-16 09:05:23 +00:00
|
|
|
|
2021-03-18 12:50:38 +00:00
|
|
|
err = kms.initCertificates(args.Config, args.Secrets)
|
2020-12-15 07:11:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize Vault certificates: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultAuthPath := vaultDefaultAuthPath
|
2021-03-18 12:50:38 +00:00
|
|
|
err = setConfigString(&vaultAuthPath, args.Config, "vaultAuthPath")
|
2020-12-01 07:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2020-12-01 07:39:15 +00:00
|
|
|
|
2020-11-25 11:45:15 +00:00
|
|
|
kms.vaultConfig[vault.AuthMountPath], err = detectAuthMountPath(vaultAuthPath)
|
2020-11-16 09:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to set %s in Vault config: %w", vault.AuthMountPath, err)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2020-11-16 09:05:23 +00:00
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultRole := vaultDefaultRole
|
2021-03-18 12:50:38 +00:00
|
|
|
err = setConfigString(&vaultRole, args.Config, "vaultRole")
|
2020-12-01 07:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-06 16:23:14 +00:00
|
|
|
}
|
2020-11-25 11:45:15 +00:00
|
|
|
kms.vaultConfig[vault.AuthKubernetesRole] = vaultRole
|
2020-11-16 09:05:23 +00:00
|
|
|
|
|
|
|
// vault.VaultBackendPathKey is "secret/" by default, use vaultPassphraseRoot if configured
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultPassphraseRoot := ""
|
2021-03-18 12:50:38 +00:00
|
|
|
err = setConfigString(&vaultPassphraseRoot, args.Config, "vaultPassphraseRoot")
|
2020-12-01 07:39:15 +00:00
|
|
|
if err == nil {
|
2020-11-16 09:05:23 +00:00
|
|
|
// the old example did have "/v1/secret/", convert that format
|
|
|
|
if strings.HasPrefix(vaultPassphraseRoot, "/v1/") {
|
2020-11-25 11:45:15 +00:00
|
|
|
kms.vaultConfig[vault.VaultBackendPathKey] = strings.TrimPrefix(vaultPassphraseRoot, "/v1/")
|
2020-11-16 09:05:23 +00:00
|
|
|
} else {
|
2020-11-25 11:45:15 +00:00
|
|
|
kms.vaultConfig[vault.VaultBackendPathKey] = vaultPassphraseRoot
|
2020-11-16 09:05:23 +00:00
|
|
|
}
|
2020-12-01 07:39:15 +00:00
|
|
|
} else if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
return nil, err
|
2020-02-06 16:23:14 +00:00
|
|
|
}
|
2020-11-16 09:05:23 +00:00
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
kms.vaultPassphrasePath = vaultDefaultPassphrasePath
|
2021-03-18 12:50:38 +00:00
|
|
|
err = setConfigString(&kms.vaultPassphrasePath, args.Config, "vaultPassphrasePath")
|
2020-12-01 07:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-16 09:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: vault.AuthKubernetesTokenPath is not enough? EnvVaultToken needs to be set?
|
2020-11-25 11:45:15 +00:00
|
|
|
kms.vaultConfig[vault.AuthMethod] = vault.AuthMethodKubernetes
|
|
|
|
kms.vaultConfig[vault.AuthKubernetesTokenPath] = serviceAccountTokenPath
|
2020-11-16 09:05:23 +00:00
|
|
|
|
2020-12-02 07:53:17 +00:00
|
|
|
err = kms.connectVault()
|
2020-11-16 09:05:23 +00:00
|
|
|
if err != nil {
|
2020-12-02 07:53:17 +00:00
|
|
|
return nil, err
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2020-11-16 09:05:23 +00:00
|
|
|
|
2020-02-06 16:23:14 +00:00
|
|
|
return kms, nil
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// FetchDEK returns passphrase from Vault. The passphrase is stored in a
|
2020-11-16 09:05:23 +00:00
|
|
|
// data.data.passphrase structure.
|
2021-02-15 09:24:47 +00:00
|
|
|
func (kms *VaultKMS) FetchDEK(key string) (string, error) {
|
2020-11-16 09:05:23 +00:00
|
|
|
s, err := kms.secrets.GetSecret(filepath.Join(kms.vaultPassphrasePath, key), kms.keyContext)
|
2021-02-15 09:56:22 +00:00
|
|
|
if err != nil {
|
2020-01-29 11:44:45 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
data, ok := s["data"].(map[string]interface{})
|
2020-01-29 11:44:45 +00:00
|
|
|
if !ok {
|
2020-12-08 14:26:42 +00:00
|
|
|
return "", fmt.Errorf("failed parsing data for get passphrase request for %q", key)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2020-11-16 09:05:23 +00:00
|
|
|
passphrase, ok := data["passphrase"].(string)
|
2020-01-29 11:44:45 +00:00
|
|
|
if !ok {
|
2020-12-08 14:26:42 +00:00
|
|
|
return "", fmt.Errorf("failed parsing passphrase for get passphrase request for %q", key)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return passphrase, nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// StoreDEK saves new passphrase in Vault.
|
|
|
|
func (kms *VaultKMS) StoreDEK(key, value string) error {
|
2020-11-16 09:05:23 +00:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"data": map[string]string{
|
2020-01-29 11:44:45 +00:00
|
|
|
"passphrase": value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
pathKey := filepath.Join(kms.vaultPassphrasePath, key)
|
|
|
|
err := kms.secrets.PutSecret(pathKey, data, kms.keyContext)
|
2020-01-29 11:44:45 +00:00
|
|
|
if err != nil {
|
2020-11-16 09:05:23 +00:00
|
|
|
return fmt.Errorf("saving passphrase at %s request to vault failed: %w", pathKey, err)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:24:47 +00:00
|
|
|
// RemoveDEK deletes passphrase from Vault.
|
|
|
|
func (kms *VaultKMS) RemoveDEK(key string) error {
|
2020-11-16 09:05:23 +00:00
|
|
|
pathKey := filepath.Join(kms.vaultPassphrasePath, key)
|
|
|
|
err := kms.secrets.DeleteSecret(pathKey, kms.keyContext)
|
2020-01-29 11:44:45 +00:00
|
|
|
if err != nil {
|
2020-11-16 09:05:23 +00:00
|
|
|
return fmt.Errorf("delete passphrase at %s request to vault failed: %w", pathKey, err)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
// detectAuthMountPath takes the vaultAuthPath configuration option that
|
|
|
|
// defaults to "/v1/auth/kubernetes/login" and makes it a vault.AuthMountPath
|
|
|
|
// like "kubernetes".
|
|
|
|
func detectAuthMountPath(path string) (string, error) {
|
|
|
|
var authMountPath string
|
2020-01-29 11:44:45 +00:00
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
if path == "" {
|
|
|
|
return "", errors.New("path is empty")
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 03:36:22 +00:00
|
|
|
// add all components between "login" and "auth" to authMountPath
|
2020-11-16 09:05:23 +00:00
|
|
|
match := false
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
for _, part := range parts {
|
|
|
|
if part == "auth" {
|
|
|
|
match = true
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if part == "login" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if match && authMountPath == "" {
|
|
|
|
authMountPath = part
|
|
|
|
} else if match {
|
|
|
|
authMountPath += "/" + part
|
|
|
|
}
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
// in case authMountPath is empty, return original path as it was
|
|
|
|
if authMountPath == "" {
|
|
|
|
authMountPath = path
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
return authMountPath, nil
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
// createTempFile writes data to a temporary file that contains the pattern in
|
|
|
|
// the filename (see ioutil.TempFile for details).
|
|
|
|
func createTempFile(pattern string, data []byte) (string, error) {
|
|
|
|
t, err := ioutil.TempFile("", pattern)
|
2020-01-29 11:44:45 +00:00
|
|
|
if err != nil {
|
2020-11-16 09:05:23 +00:00
|
|
|
return "", fmt.Errorf("failed to create temporary file: %w", err)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
// delete the tmpfile on error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// ignore error on failure to remove tmpfile (gosec complains)
|
|
|
|
_ = os.Remove(t.Name())
|
|
|
|
}
|
|
|
|
}()
|
2020-01-29 11:44:45 +00:00
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
s, err := t.Write(data)
|
|
|
|
if err != nil || s != len(data) {
|
|
|
|
return "", fmt.Errorf("failed to write temporary file: %w", err)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
2020-11-16 09:05:23 +00:00
|
|
|
err = t.Close()
|
2020-01-29 11:44:45 +00:00
|
|
|
if err != nil {
|
2020-11-16 09:05:23 +00:00
|
|
|
return "", fmt.Errorf("failed to close temporary file: %w", err)
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
return t.Name(), nil
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|