2020-11-19 07:52:04 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/libopenstorage/secrets"
|
2021-08-02 08:40:42 +00:00
|
|
|
"github.com/libopenstorage/secrets/vault/utils"
|
2023-10-17 04:59:04 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-11-19 07:52:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Name = secrets.TypeVault
|
|
|
|
DefaultBackendPath = "secret/"
|
|
|
|
VaultBackendPathKey = "VAULT_BACKEND_PATH"
|
|
|
|
VaultBackendKey = "VAULT_BACKEND"
|
|
|
|
kvVersionKey = "version"
|
|
|
|
kvDataKey = "data"
|
2021-08-02 08:40:42 +00:00
|
|
|
kvMetadataKey = "metadata"
|
2020-11-19 07:52:04 +00:00
|
|
|
kvVersion1 = "kv"
|
|
|
|
kvVersion2 = "kv-v2"
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
AuthMethodKubernetes = utils.AuthMethodKubernetes
|
|
|
|
AuthMethod = utils.AuthMethod
|
|
|
|
AuthMountPath = utils.AuthMountPath
|
|
|
|
AuthKubernetesRole = utils.AuthKubernetesRole
|
|
|
|
AuthKubernetesTokenPath = utils.AuthKubernetesTokenPath
|
|
|
|
AuthKubernetesMountPath = utils.AuthKubernetesMountPath
|
2023-10-17 04:59:04 +00:00
|
|
|
AuthAppRoleRoleID = utils.AuthAppRoleRoleID
|
|
|
|
AuthAppRoleSecretID = utils.AuthAppRoleSecretID
|
2020-11-19 07:52:04 +00:00
|
|
|
)
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
func init() {
|
|
|
|
if err := secrets.Register(Name, New); err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 07:52:04 +00:00
|
|
|
|
|
|
|
type vaultSecrets struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
client *api.Client
|
|
|
|
|
|
|
|
currentNamespace string
|
|
|
|
lockClientToken sync.Mutex
|
|
|
|
|
|
|
|
endpoint string
|
|
|
|
backendPath string
|
|
|
|
namespace string
|
|
|
|
isKvBackendV2 bool
|
|
|
|
autoAuth bool
|
|
|
|
config map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These variables are helpful in testing to stub method call from packages
|
|
|
|
var (
|
|
|
|
newVaultClient = api.NewClient
|
|
|
|
isKvV2 = isKvBackendV2
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(
|
|
|
|
secretConfig map[string]interface{},
|
|
|
|
) (secrets.Secrets, error) {
|
|
|
|
// DefaultConfig uses the environment variables if present.
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
|
|
|
|
if len(secretConfig) == 0 && config.Error != nil {
|
|
|
|
return nil, config.Error
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
address := utils.GetVaultParam(secretConfig, api.EnvVaultAddress)
|
2020-11-19 07:52:04 +00:00
|
|
|
if address == "" {
|
2021-08-02 08:40:42 +00:00
|
|
|
return nil, utils.ErrVaultAddressNotSet
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
2021-08-02 08:40:42 +00:00
|
|
|
if err := utils.IsValidAddr(address); err != nil {
|
|
|
|
return nil, err
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
config.Address = address
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
if err := utils.ConfigureTLS(config, secretConfig); err != nil {
|
2020-11-19 07:52:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := newVaultClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
namespace := utils.GetVaultParam(secretConfig, api.EnvVaultNamespace)
|
2020-11-19 07:52:04 +00:00
|
|
|
if len(namespace) > 0 {
|
|
|
|
// use a namespace as a header for setup purposes
|
|
|
|
// later use it as a key prefix
|
|
|
|
client.SetNamespace(namespace)
|
|
|
|
defer client.SetNamespace("")
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
token, autoAuth, err := utils.Authenticate(client, secretConfig)
|
|
|
|
if err != nil {
|
|
|
|
utils.CloseIdleConnections(config)
|
|
|
|
return nil, fmt.Errorf("failed to get the authentication token: %w", err)
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
|
2023-10-17 04:59:04 +00:00
|
|
|
authMethod := "token"
|
|
|
|
method := utils.GetVaultParam(secretConfig, AuthMethod)
|
|
|
|
if method != "" && utils.GetVaultParam(secretConfig, api.EnvVaultToken) == "" {
|
|
|
|
authMethod = method
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Authenticated to Vault with %v\n", authMethod)
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
backendPath := utils.GetVaultParam(secretConfig, VaultBackendPathKey)
|
2020-11-19 07:52:04 +00:00
|
|
|
if backendPath == "" {
|
|
|
|
backendPath = DefaultBackendPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var isBackendV2 bool
|
2021-08-02 08:40:42 +00:00
|
|
|
backend := utils.GetVaultParam(secretConfig, VaultBackendKey)
|
2020-11-19 07:52:04 +00:00
|
|
|
if backend == kvVersion1 {
|
|
|
|
isBackendV2 = false
|
|
|
|
} else if backend == kvVersion2 {
|
|
|
|
isBackendV2 = true
|
|
|
|
} else {
|
|
|
|
// TODO: Handle backends other than kv
|
|
|
|
isBackendV2, err = isKvV2(client, backendPath)
|
|
|
|
if err != nil {
|
2021-08-02 08:40:42 +00:00
|
|
|
utils.CloseIdleConnections(config)
|
2020-11-19 07:52:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &vaultSecrets{
|
|
|
|
endpoint: config.Address,
|
|
|
|
namespace: namespace,
|
|
|
|
currentNamespace: namespace,
|
|
|
|
client: client,
|
|
|
|
backendPath: backendPath,
|
|
|
|
isKvBackendV2: isBackendV2,
|
|
|
|
autoAuth: autoAuth,
|
|
|
|
config: secretConfig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) String() string {
|
|
|
|
return Name
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
func (v *vaultSecrets) keyPath(secretID string, keyContext map[string]string) keyPath {
|
|
|
|
backendPath := v.backendPath
|
|
|
|
var namespace string
|
|
|
|
var ok bool
|
|
|
|
if namespace, ok = keyContext[secrets.KeyVaultNamespace]; !ok {
|
2020-11-19 07:52:04 +00:00
|
|
|
namespace = v.namespace
|
|
|
|
}
|
2021-08-02 08:40:42 +00:00
|
|
|
|
|
|
|
var isDestroyKey bool
|
|
|
|
if v.isKvBackendV2 {
|
|
|
|
if destroyAllSecrets, ok := keyContext[secrets.DestroySecret]; ok {
|
|
|
|
// checking for any value seems sufficient to assume 'destroy' is requested
|
|
|
|
if destroyAllSecrets != "" {
|
|
|
|
isDestroyKey = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 07:52:04 +00:00
|
|
|
return keyPath{
|
2021-08-02 08:40:42 +00:00
|
|
|
backendPath: backendPath,
|
|
|
|
isBackendV2: v.isKvBackendV2,
|
|
|
|
namespace: namespace,
|
|
|
|
secretID: secretID,
|
|
|
|
isDestroyKey: isDestroyKey,
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) GetSecret(
|
|
|
|
secretID string,
|
|
|
|
keyContext map[string]string,
|
2023-10-17 04:59:04 +00:00
|
|
|
) (map[string]interface{}, secrets.Version, error) {
|
2021-08-02 08:40:42 +00:00
|
|
|
key := v.keyPath(secretID, keyContext)
|
2020-11-19 07:52:04 +00:00
|
|
|
secretValue, err := v.read(key)
|
|
|
|
if err != nil {
|
2023-10-17 04:59:04 +00:00
|
|
|
return nil, secrets.NoVersion, fmt.Errorf("failed to get secret: %s: %s", key, err)
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
if secretValue == nil {
|
2023-10-17 04:59:04 +00:00
|
|
|
return nil, secrets.NoVersion, secrets.ErrInvalidSecretId
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.isKvBackendV2 {
|
|
|
|
if data, exists := secretValue.Data[kvDataKey]; exists && data != nil {
|
|
|
|
if data, ok := data.(map[string]interface{}); ok {
|
2023-10-17 04:59:04 +00:00
|
|
|
// TODO: Vault does support versioned secrets with KV Backend 2
|
|
|
|
// However it requires clients to invoke a different metadata API call
|
|
|
|
// to fetch the version. Once there is a need for versions from Vault
|
|
|
|
// we can add support for it.
|
|
|
|
return data, secrets.NoVersion, nil
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-17 04:59:04 +00:00
|
|
|
return nil, secrets.NoVersion, secrets.ErrInvalidSecretId
|
2020-11-19 07:52:04 +00:00
|
|
|
} else {
|
2023-10-17 04:59:04 +00:00
|
|
|
return secretValue.Data, secrets.NoVersion, nil
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) PutSecret(
|
|
|
|
secretID string,
|
|
|
|
secretData map[string]interface{},
|
|
|
|
keyContext map[string]string,
|
2023-10-17 04:59:04 +00:00
|
|
|
) (secrets.Version, error) {
|
2020-11-19 07:52:04 +00:00
|
|
|
if v.isKvBackendV2 {
|
|
|
|
secretData = map[string]interface{}{
|
|
|
|
kvDataKey: secretData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:40:42 +00:00
|
|
|
key := v.keyPath(secretID, keyContext)
|
2020-11-19 07:52:04 +00:00
|
|
|
if _, err := v.write(key, secretData); err != nil {
|
2023-10-17 04:59:04 +00:00
|
|
|
return secrets.NoVersion, fmt.Errorf("failed to put secret: %s: %s", key, err)
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
2023-10-17 04:59:04 +00:00
|
|
|
return secrets.NoVersion, nil
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) DeleteSecret(
|
|
|
|
secretID string,
|
|
|
|
keyContext map[string]string,
|
|
|
|
) error {
|
2021-08-02 08:40:42 +00:00
|
|
|
key := v.keyPath(secretID, keyContext)
|
2020-11-19 07:52:04 +00:00
|
|
|
if _, err := v.delete(key); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete secret: %s: %s", key, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) Encrypt(
|
|
|
|
secretID string,
|
|
|
|
plaintTextData string,
|
|
|
|
keyContext map[string]string,
|
|
|
|
) (string, error) {
|
|
|
|
return "", secrets.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) Decrypt(
|
|
|
|
secretID string,
|
|
|
|
encryptedData string,
|
|
|
|
keyContext map[string]string,
|
|
|
|
) (string, error) {
|
|
|
|
return "", secrets.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) Rencrypt(
|
|
|
|
originalSecretID string,
|
|
|
|
newSecretID string,
|
|
|
|
originalKeyContext map[string]string,
|
|
|
|
newKeyContext map[string]string,
|
|
|
|
encryptedData string,
|
|
|
|
) (string, error) {
|
|
|
|
return "", secrets.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) ListSecrets() ([]string, error) {
|
|
|
|
return nil, secrets.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) read(path keyPath) (*api.Secret, error) {
|
|
|
|
if v.autoAuth {
|
|
|
|
v.lockClientToken.Lock()
|
|
|
|
defer v.lockClientToken.Unlock()
|
|
|
|
|
|
|
|
if err := v.setNamespaceToken(path.Namespace()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
secretValue, err := v.lockedRead(path.Path())
|
|
|
|
if v.isTokenExpired(err) {
|
|
|
|
if err = v.renewToken(path.Namespace()); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to renew token: %s", err)
|
|
|
|
}
|
|
|
|
return v.lockedRead(path.Path())
|
|
|
|
}
|
|
|
|
return secretValue, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) write(path keyPath, data map[string]interface{}) (*api.Secret, error) {
|
|
|
|
if v.autoAuth {
|
|
|
|
v.lockClientToken.Lock()
|
|
|
|
defer v.lockClientToken.Unlock()
|
|
|
|
|
|
|
|
if err := v.setNamespaceToken(path.Namespace()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
secretValue, err := v.lockedWrite(path.Path(), data)
|
|
|
|
if v.isTokenExpired(err) {
|
|
|
|
if err = v.renewToken(path.Namespace()); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to renew token: %s", err)
|
|
|
|
}
|
|
|
|
return v.lockedWrite(path.Path(), data)
|
|
|
|
}
|
|
|
|
return secretValue, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) delete(path keyPath) (*api.Secret, error) {
|
|
|
|
if v.autoAuth {
|
|
|
|
v.lockClientToken.Lock()
|
|
|
|
defer v.lockClientToken.Unlock()
|
|
|
|
|
|
|
|
if err := v.setNamespaceToken(path.Namespace()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
secretValue, err := v.lockedDelete(path.Path())
|
|
|
|
if v.isTokenExpired(err) {
|
|
|
|
if err = v.renewToken(path.Namespace()); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to renew token: %s", err)
|
|
|
|
}
|
|
|
|
return v.lockedDelete(path.Path())
|
|
|
|
}
|
|
|
|
return secretValue, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) lockedRead(path string) (*api.Secret, error) {
|
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
|
|
|
|
|
|
|
return v.client.Logical().Read(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) lockedWrite(path string, data map[string]interface{}) (*api.Secret, error) {
|
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
|
|
|
|
|
|
|
return v.client.Logical().Write(path, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) lockedDelete(path string) (*api.Secret, error) {
|
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
|
|
|
|
|
|
|
return v.client.Logical().Delete(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) renewToken(namespace string) error {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
|
|
|
|
if len(namespace) > 0 {
|
|
|
|
v.client.SetNamespace(namespace)
|
|
|
|
defer v.client.SetNamespace("")
|
|
|
|
}
|
2021-08-02 08:40:42 +00:00
|
|
|
token, err := utils.GetAuthToken(v.client, v.config)
|
2020-11-19 07:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get auth token for %s namespace: %s", namespace, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v.currentNamespace = namespace
|
|
|
|
v.client.SetToken(token)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vaultSecrets) isTokenExpired(err error) bool {
|
|
|
|
return err != nil && v.autoAuth && strings.Contains(err.Error(), "permission denied")
|
|
|
|
}
|
|
|
|
|
|
|
|
// setNamespaceToken is used for a multi-token support with a kubernetes auto auth setup.
|
|
|
|
//
|
|
|
|
// This allows to talk with a multiple vault namespaces (which are not sub-namespace). Create
|
|
|
|
// the same “Kubernetes Auth Role” in each of the configured namespace. For every request it
|
|
|
|
// fetches the token for that specific namespace.
|
|
|
|
func (v *vaultSecrets) setNamespaceToken(namespace string) error {
|
|
|
|
if v.currentNamespace == namespace {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.renewToken(namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isKvBackendV2(client *api.Client, backendPath string) (bool, error) {
|
|
|
|
mounts, err := client.Sys().ListMounts()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for path, mount := range mounts {
|
|
|
|
// path is represented as 'path/'
|
|
|
|
if trimSlash(path) == trimSlash(backendPath) {
|
|
|
|
version := mount.Options[kvVersionKey]
|
|
|
|
if version == "2" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("Secrets engine with mount path '%s' not found",
|
|
|
|
backendPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimSlash(in string) string {
|
|
|
|
return strings.Trim(in, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
type keyPath struct {
|
2021-08-02 08:40:42 +00:00
|
|
|
backendPath string
|
|
|
|
isBackendV2 bool
|
|
|
|
namespace string
|
|
|
|
secretID string
|
|
|
|
isDestroyKey bool
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k keyPath) Path() string {
|
|
|
|
if k.isBackendV2 {
|
2021-08-02 08:40:42 +00:00
|
|
|
keyType := kvDataKey
|
|
|
|
if k.isDestroyKey {
|
|
|
|
keyType = kvMetadataKey
|
|
|
|
}
|
|
|
|
return path.Join(k.namespace, k.backendPath, keyType, k.secretID)
|
2020-11-19 07:52:04 +00:00
|
|
|
}
|
|
|
|
return path.Join(k.namespace, k.backendPath, k.secretID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k keyPath) Namespace() string {
|
|
|
|
return k.namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k keyPath) String() string {
|
|
|
|
return fmt.Sprintf("backendPath=%s, backendV2=%t, namespace=%s, secretID=%s", k.backendPath, k.isBackendV2, k.namespace, k.secretID)
|
|
|
|
}
|