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 (
|
|
|
|
// path to service account token that will be used to authenticate with Vault
|
|
|
|
// #nosec
|
|
|
|
serviceAccountTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
|
|
|
|
|
|
|
|
// vault configuration defaults
|
|
|
|
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 {
|
2020-11-16 09:05:23 +00:00
|
|
|
EncryptionKMSID string
|
2020-12-02 07:53:17 +00:00
|
|
|
secrets loss.Secrets
|
2020-11-25 11:45:15 +00:00
|
|
|
vaultConfig map[string]interface{}
|
|
|
|
keyContext map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type VaultKMS struct {
|
|
|
|
vaultConnection
|
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
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:30:28 +00:00
|
|
|
func (vc *vaultConnection) initConnection(kmsID string, config map[string]interface{}, secrets map[string]string) error {
|
2020-11-25 11:45:15 +00:00
|
|
|
vaultConfig := make(map[string]interface{})
|
|
|
|
keyContext := make(map[string]string)
|
|
|
|
|
|
|
|
vc.EncryptionKMSID = kmsID
|
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultAddress := ""
|
|
|
|
err := setConfigString(&vaultAddress, config, "vaultAddress")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
|
|
|
vaultConfig[api.EnvVaultAddress] = vaultAddress
|
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultNamespace := vaultDefaultNamespace
|
|
|
|
err = setConfigString(&vaultNamespace, config, "vaultNamespace")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
|
|
|
vaultConfig[api.EnvVaultNamespace] = vaultNamespace
|
|
|
|
keyContext[loss.KeyVaultNamespace] = vaultNamespace
|
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
verifyCA := vaultDefaultCAVerify
|
|
|
|
err = setConfigString(&verifyCA, config, "vaultCAVerify")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vaultCAVerify, err := strconv.ParseBool(verifyCA)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse 'vaultCAVerify': %w", err)
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultConfig[api.EnvVaultInsecure] = !vaultCAVerify
|
2020-11-25 11:45:15 +00:00
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultCAFromSecret := ""
|
|
|
|
err = setConfigString(&vaultCAFromSecret, config, "vaultCAFromSecret")
|
|
|
|
if err == nil && vaultCAFromSecret != "" {
|
2020-11-25 11:45:15 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
// TODO: delete f.Name() when vaultConnection is destroyed
|
2020-12-01 07:39:15 +00:00
|
|
|
} else if !errors.Is(err, errConfigOptionMissing) {
|
|
|
|
return err
|
2020-11-25 11:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vc.keyContext = keyContext
|
|
|
|
vc.vaultConfig = vaultConfig
|
|
|
|
|
|
|
|
return nil
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
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-07-19 12:21:03 +00:00
|
|
|
// InitVaultKMS returns an interface to HashiCorp Vault KMS.
|
2020-12-01 08:30:28 +00:00
|
|
|
func InitVaultKMS(kmsID string, config map[string]interface{}, secrets map[string]string) (EncryptionKMS, error) {
|
2020-02-06 16:23:14 +00:00
|
|
|
kms := &VaultKMS{}
|
2020-12-01 07:39:15 +00:00
|
|
|
err := kms.initConnection(kmsID, config, secrets)
|
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
|
|
|
|
2020-12-01 07:39:15 +00:00
|
|
|
vaultAuthPath := vaultDefaultAuthPath
|
|
|
|
err = setConfigString(&vaultAuthPath, config, "vaultAuthPath")
|
|
|
|
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
|
|
|
|
err = setConfigString(&vaultRole, config, "vaultRole")
|
|
|
|
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 := ""
|
|
|
|
err = setConfigString(&vaultPassphraseRoot, config, "vaultPassphraseRoot")
|
|
|
|
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
|
|
|
|
err = setConfigString(&kms.vaultPassphrasePath, config, "vaultPassphrasePath")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// GetID is returning correlation ID to KMS configuration.
|
2020-11-25 11:45:15 +00:00
|
|
|
func (vc *vaultConnection) GetID() string {
|
|
|
|
return vc.EncryptionKMSID
|
2020-01-29 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:05:23 +00:00
|
|
|
// GetPassphrase returns passphrase from Vault. The passphrase is stored in a
|
|
|
|
// data.data.passphrase structure.
|
2020-01-29 11:44:45 +00:00
|
|
|
func (kms *VaultKMS) GetPassphrase(key string) (string, error) {
|
2020-11-16 09:05:23 +00:00
|
|
|
s, err := kms.secrets.GetSecret(filepath.Join(kms.vaultPassphrasePath, key), kms.keyContext)
|
|
|
|
if errors.Is(err, loss.ErrInvalidSecretId) {
|
|
|
|
return "", MissingPassphrase{err}
|
|
|
|
} else 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
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// SavePassphrase saves new passphrase in Vault.
|
2020-01-29 11:44:45 +00:00
|
|
|
func (kms *VaultKMS) SavePassphrase(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
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// DeletePassphrase deletes passphrase from Vault.
|
2020-01-29 11:44:45 +00:00
|
|
|
func (kms *VaultKMS) DeletePassphrase(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
|
|
|
|
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
|
|
|
}
|