Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set VaultConnection headers on the vault client #629

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions controllers/vaultconnection_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (r *VaultConnectionReconciler) Reconcile(ctx context.Context, req ctrl.Requ
Address: o.Spec.Address,
SkipTLSVerify: o.Spec.SkipTLSVerify,
TLSServerName: o.Spec.TLSServerName,
Headers: o.Spec.Headers,
}

var errs error
Expand Down
1 change: 1 addition & 0 deletions internal/vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ func (c *defaultClient) init(ctx context.Context, client ctrlclient.Client,
VaultNamespace: authObj.Spec.Namespace,
K8sNamespace: connObj.Namespace,
CACertSecretRef: connObj.Spec.CACertSecretRef,
Headers: connObj.Spec.Headers,
}

vc, err := MakeVaultClient(ctx, cfg, client)
Expand Down
11 changes: 10 additions & 1 deletion internal/vault/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"fmt"

"github.com/hashicorp/vault/api"
"k8s.io/api/core/v1"
vconsts "github.com/hashicorp/vault/sdk/helper/consts"
v1 "k8s.io/api/core/v1"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

Expand All @@ -35,6 +36,8 @@ type ClientConfig struct {
TLSServerName string
// VaultNamespace is the namespace in Vault to auth to
VaultNamespace string
// Headers are http headers to set on the Vault client
Headers map[string]string
}

// MakeVaultClient creates a Vault api.Client from a ClientConfig.
Expand Down Expand Up @@ -93,6 +96,12 @@ func MakeVaultClient(ctx context.Context, cfg *ClientConfig, client ctrlclient.C
l.Error(err, "error setting up Vault API client")
return nil, err
}
if _, exists := cfg.Headers[vconsts.NamespaceHeaderName]; exists {
benashz marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("setting header %q on VaultConnection is not permitted", vconsts.NamespaceHeaderName)
}
for k, v := range cfg.Headers {
tvoran marked this conversation as resolved.
Show resolved Hide resolved
c.AddHeader(k, v)
}
if cfg.VaultNamespace != "" {
c.SetNamespace(cfg.VaultNamespace)
}
Expand Down
43 changes: 42 additions & 1 deletion internal/vault/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ func TestMakeVaultClient(t *testing.T) {
CACert: nil,
expectedError: nil,
},
"headers": {
tvoran marked this conversation as resolved.
Show resolved Hide resolved
vaultConfig: &ClientConfig{
Headers: map[string]string{
"X-Proxy-Setting": "yes",
"Y-Proxy-Setting": "no",
},
VaultNamespace: "vault-test-namespace",
},
CACert: nil,
expectedError: nil,
},
"headers can't override namespace": {
vaultConfig: &ClientConfig{
Headers: map[string]string{
"X-Proxy-Setting": "yes",
"Y-Proxy-Setting": "no",
vconsts.NamespaceHeaderName: "nope",
},
VaultNamespace: "vault-test-namespace",
},
CACert: nil,
expectedError: fmt.Errorf(`setting header "X-Vault-Namespace" on VaultConnection is not permitted`),
},
}

for name, tc := range tests {
Expand All @@ -97,7 +120,7 @@ func TestMakeVaultClient(t *testing.T) {
assert.Nil(t, vaultClient)
} else {
assert.NoError(t, err)
assert.NotNil(t, vaultClient)
require.NotNil(t, vaultClient)
vaultClient.SetCloneHeaders(true)
vaultConfig := vaultClient.CloneConfig()

Expand All @@ -116,7 +139,25 @@ func TestMakeVaultClient(t *testing.T) {
require.NoError(t, err)
assert.True(t, tlsConfig.RootCAs.Equal(expectedCertPool), "The CA cert in the client doesn't match the expected cert")
}

expectedHeaders := makeVaultHttpHeaders(t, tc.vaultConfig.VaultNamespace, tc.vaultConfig.Headers)
assert.Equal(t, expectedHeaders, vaultClient.Headers(), "The headers in the client don't match the expected headers")
}
})
}
}

func makeVaultHttpHeaders(t *testing.T, namespace string, headers map[string]string) http.Header {
t.Helper()

h := make(http.Header)
for k, v := range headers {
h.Set(k, v)
}
h.Set("X-Vault-Request", "true")
if len(namespace) > 0 {
tvoran marked this conversation as resolved.
Show resolved Hide resolved
h.Set(vconsts.NamespaceHeaderName, namespace)
}

return h
}
Loading