Skip to content

Commit

Permalink
First take on adding namespace support to vault. Aim to close #2806
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Alves Macambira <tmacam@burocrata.org>
  • Loading branch information
tmacam committed Apr 25, 2023
1 parent 3fbaad8 commit 15cd838
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
9 changes: 9 additions & 0 deletions secretstores/hashicorp/vault/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ metadata:
Vault value type. map means to parse the value into map[string]string, text means to use the value as a string. "map" sets the multipleKeyValuesPerSecret behavior. text makes Vault behave as a secret store with name/value semantics. Defaults to "map"
example: "map"
type: string
- name: vaultNamespace
type: string
required: false
description: "TBD. Refer to multi-tenancy"
example: "map"
default: ""
url:
title: "Vault Enterprise Namespaces"
url: "https://developer.hashicorp.com/vault/docs/enterprise/namespaces"
25 changes: 19 additions & 6 deletions secretstores/hashicorp/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
componentVaultKVUsePrefix string = "vaultKVUsePrefix"
defaultVaultKVPrefix string = "dapr"
vaultHTTPHeader string = "X-Vault-Token"
vaultHTTPNamespaceHeader string = "X-Vault-Namespace"
vaultHTTPRequestHeader string = "X-Vault-Request"
vaultEnginePath string = "enginePath"
vaultValueType string = "vaultValueType"
Expand Down Expand Up @@ -83,6 +84,8 @@ type vaultSecretStore struct {
vaultKVPrefix string
vaultEnginePath string
vaultValueType valueType
// TBD update tests and certification tests
vaultNamespace string

json jsoniter.API

Expand All @@ -102,6 +105,7 @@ type VaultMetadata struct {
VaultTokenMountPath string
EnginePath string
VaultValueType string
VaultNamespace string
}

// tlsConfig is TLS configuration to interact with HashiCorp Vault.
Expand Down Expand Up @@ -185,6 +189,8 @@ func (v *vaultSecretStore) Init(_ context.Context, meta secretstores.Metadata) e
}
v.vaultKVPrefix = vaultKVPrefix

v.vaultNamespace = m.VaultNamespace

// Generate TLS config
tlsConf := metadataToTLSConfig(&m)

Expand Down Expand Up @@ -231,9 +237,8 @@ func (v *vaultSecretStore) getSecret(ctx context.Context, secret, version string
return nil, fmt.Errorf("couldn't generate request: %w", err)
}
// Set vault token.
httpReq.Header.Set(vaultHTTPHeader, v.vaultToken)
// Set X-Vault-Request header
httpReq.Header.Set(vaultHTTPRequestHeader, "true")
v.setHttpRequestHeaders(httpReq)

httpresp, err := v.client.Do(httpReq)
if err != nil {
Expand Down Expand Up @@ -277,6 +282,17 @@ func (v *vaultSecretStore) getSecret(ctx context.Context, secret, version string
return &d, nil
}

func (v *vaultSecretStore) setHttpRequestHeaders(httpReq *http.Request) {
// Set vault token.
httpReq.Header.Set(vaultHTTPHeader, v.vaultToken)
// Set X-Vault-Request header
httpReq.Header.Set(vaultHTTPRequestHeader, "true")
// Set X-Vault-Namespace header if configured to do so
if v.vaultNamespace != "" {
httpReq.Header.Set(vaultHTTPNamespaceHeader, v.vaultNamespace)
}
}

// GetSecret retrieves a secret using a key and returns a map of decrypted string/string values.
func (v *vaultSecretStore) GetSecret(ctx context.Context, req secretstores.GetSecretRequest) (secretstores.GetSecretResponse, error) {
// version 0 represent for latest version
Expand Down Expand Up @@ -349,10 +365,7 @@ func (v *vaultSecretStore) listKeysUnderPath(ctx context.Context, path string) (
if err != nil {
return nil, fmt.Errorf("couldn't generate request: %s", err)
}
// Set vault token.
httpReq.Header.Set(vaultHTTPHeader, v.vaultToken)
// Set X-Vault-Request header
httpReq.Header.Set(vaultHTTPRequestHeader, "true")
v.setHttpRequestHeaders(httpReq)
httpresp, err := v.client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("couldn't get secret: %s", err)
Expand Down

0 comments on commit 15cd838

Please sign in to comment.