Skip to content

Commit

Permalink
NET-581 - Added vault namespace in helm (#2841)
Browse files Browse the repository at this point in the history
* added namespace

* namespace in connect ca

* updated tests

* fix test desc

* changelog

* Update .changelog/2841.txt

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* Update charts/consul/values.yaml

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* removed new line added

* fix templates

* bats test

* fix double colon

* fix template

* added 2 more tests

* fixes bats tests

* fix json in api gateway

* updated bats test

* Update charts/consul/values.yaml

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* fix client daemon set bats

* fix bats test

* fix bats

* api gateway fix

* fix bats

* fix clientdaemon set and api gateway controller

* fix connect inject deployment

* fix mesh gateway deployment

* added tests for partition init job

* server acl init job tests added

* fix server stateful bats

* fix sync catalog

* fix includes check

* bats test fixes

* fix connect inject

* fix yaml

* fix yaml

* fix assertions in bats

* fix client daemon set bats

* Update charts/consul/values.yaml

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* Update charts/consul/templates/server-config-configmap.yaml

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* change yaml

* added addional config test

* fix tests

* added more tests

* fix bats

* Update charts/consul/test/unit/server-config-configmap.bats

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* Update charts/consul/test/unit/server-config-configmap.bats

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* Update .changelog/2841.txt

Co-authored-by: David Yu <dyu@hashicorp.com>

* Update .changelog/2841.txt

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>

* added dummy commit to run CI

* fix change log

* fix comment

---------

Co-authored-by: Michael Zalimeni <michael.zalimeni@hashicorp.com>
Co-authored-by: David Yu <dyu@hashicorp.com>
  • Loading branch information
3 people committed Sep 8, 2023
1 parent a1f4916 commit 053357d
Show file tree
Hide file tree
Showing 23 changed files with 737 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changelog/2841.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```release-note:improvement
vault: Adds `namespace` to `secretsBackend.vault.connectCA` in Helm chart and annotation: "vault.hashicorp.com/namespace: namespace" to
secretsBackend.vault.agentAnnotations, if "vault.hashicorp.com/namespace" annotation is not present.
This provides a more convenient way to specify the Vault namespace than nested JSON in `connectCA.additionalConfig`.
```
6 changes: 2 additions & 4 deletions acceptance/tests/vault/vault_namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// TestVault_VaultNamespace installs Vault, configures a Vault namespace, and then bootstraps it
// with secrets, policies, and Kube Auth Method.
// It then configures Consul to use vault as the backend and checks that it works
// with the vault namespace.
// with the vault namespace. Namespace is added in this via global.secretsBackend.vault.vaultNamespace.
func TestVault_VaultNamespace(t *testing.T) {
cfg := suite.Config()
ctx := suite.Environment().DefaultContext(t)
Expand Down Expand Up @@ -192,9 +192,7 @@ func TestVault_VaultNamespace(t *testing.T) {
"global.secretsBackend.vault.connectCA.address": vaultCluster.Address(),
"global.secretsBackend.vault.connectCA.rootPKIPath": connectCARootPath,
"global.secretsBackend.vault.connectCA.intermediatePKIPath": connectCAIntermediatePath,
"global.secretsBackend.vault.connectCA.additionalConfig": fmt.Sprintf(`"{\"connect\": [{ \"ca_config\": [{ \"namespace\": \"%s\"}]}]}"`, vaultNamespacePath),

"global.secretsBackend.vault.agentAnnotations": fmt.Sprintf("\"vault.hashicorp.com/namespace\": \"%s\"", vaultNamespacePath),
"global.secretsBackend.vault.vaultNamespace": vaultNamespacePath,

"global.acls.manageSystemACLs": "true",
"global.acls.bootstrapToken.secretName": bootstrapTokenSecret.Path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ spec:
"vault.hashicorp.com/agent-inject-template-serverca.crt": {{ template "consul.serverTLSCATemplate" . }}
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{ end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- if and .Values.global.secretsBackend.vault.ca.secretName .Values.global.secretsBackend.vault.ca.secretKey }}
"vault.hashicorp.com/agent-extra-secret": "{{ .Values.global.secretsBackend.vault.ca.secretName }}"
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/client-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- if and .Values.global.enterpriseLicense.secretName (not .Values.global.acls.manageSystemACLs) }}
{{- with .Values.global.enterpriseLicense }}
"vault.hashicorp.com/agent-inject-secret-enterpriselicense.txt": "{{ .secretName }}"
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/connect-inject-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- end }}
spec:
serviceAccountName: {{ template "consul.fullname" . }}-connect-injector
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/mesh-gateway-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- end }}
{{- if (and .Values.global.metrics.enabled .Values.global.metrics.enableGatewayMetrics) }}
"prometheus.io/scrape": "true"
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/partition-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- end }}
spec:
restartPolicy: Never
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/server-acl-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- end }}
spec:
restartPolicy: Never
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/server-config-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ data:
"ca_file": "/consul/vault-ca/tls.crt",
{{- end }}
"intermediate_pki_path": "{{ .connectCA.intermediatePKIPath }}",
{{- if (and (.vaultNamespace) (not (contains "namespace" (default "" .connectCA.additionalConfig)))) }}
"namespace": "{{ .vaultNamespace }}",
{{- end }}
"root_pki_path": "{{ .connectCA.rootPKIPath }}",
"auth_method": {
"type": "kubernetes",
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/server-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- if .Values.global.enterpriseLicense.secretName }}
{{- with .Values.global.enterpriseLicense }}
"vault.hashicorp.com/agent-inject-secret-enterpriselicense.txt": "{{ .secretName }}"
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/sync-catalog-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- end }}
spec:
serviceAccountName: {{ template "consul.fullname" . }}-sync-catalog
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/telemetry-collector-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ spec:
{{- if .Values.global.secretsBackend.vault.agentAnnotations }}
{{ tpl .Values.global.secretsBackend.vault.agentAnnotations . | nindent 8 | trim }}
{{- end }}
{{- if (and (.Values.global.secretsBackend.vault.vaultNamespace) (not (hasKey (default "" .Values.global.secretsBackend.vault.agentAnnotations | fromYaml) "vault.hashicorp.com/namespace")))}}
"vault.hashicorp.com/namespace": "{{ .Values.global.secretsBackend.vault.vaultNamespace }}"
{{- end }}
{{- end }}

labels:
Expand Down
68 changes: 68 additions & 0 deletions charts/consul/test/unit/api-gateway-controller-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,74 @@ load _helpers
[ "${actual}" = "test" ]
}

@test "apiGateway/Deployment: vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is set" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/api-gateway-controller-deployment.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "vns" ]
}

@test "apiGateway/Deployment: correct vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is set and agentAnnotations are set without vaultNamespace annotation" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/api-gateway-controller-deployment.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.secretsBackend.vault.agentAnnotations=vault.hashicorp.com/agent-extra-secret: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "vns" ]
}

@test "apiGateway/Deployment: correct vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is set and agentAnnotations are also set with vaultNamespace annotation" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/api-gateway-controller-deployment.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.secretsBackend.vault.agentAnnotations="vault.hashicorp.com/namespace": bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "bar" ]
}

@test "apiGateway/Deployment: vault agent annotations can be set" {
cd `chart_dir`
local actual=$(helm template \
Expand Down
65 changes: 65 additions & 0 deletions charts/consul/test/unit/client-daemonset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,71 @@ rollingUpdate:
[ "${actual}" = "foo" ]
}

@test "client/DaemonSet: vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is set" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/client-daemonset.yaml \
--set 'client.enabled=true' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "vns" ]
}

@test "client/DaemonSet: correct vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is see and agentAnnotations are set without vaultNamespace annotation" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/client-daemonset.yaml \
--set 'client.enabled=true' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.secretsBackend.vault.agentAnnotations=vault.hashicorp.com/agent-extra-secret: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "vns" ]
}

@test "client/DaemonSet: correct vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is set and agentAnnotations are also set with vaultNamespace annotation" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/client-daemonset.yaml \
--set 'client.enabled=true' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.secretsBackend.vault.agentAnnotations=vault.hashicorp.com/namespace: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "bar" ]
}

@test "client/DaemonSet: vault gossip annotations are set when gossip encryption enabled" {
cd `chart_dir`
local object=$(helm template \
Expand Down
65 changes: 65 additions & 0 deletions charts/consul/test/unit/connect-inject-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,71 @@ load _helpers
[ "${actual}" = "" ]
}

@test "connectInject/Deployment: vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is set" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/connect-inject-deployment.yaml \
--set 'connectInject.enabled=true' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "vns" ]
}

@test "connectInject/Deployment: correct vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is see and agentAnnotations are set without vaultNamespace annotation" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/connect-inject-deployment.yaml \
--set 'connectInject.enabled=true' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.secretsBackend.vault.agentAnnotations=vault.hashicorp.com/agent-extra-secret: bar' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "vns" ]
}

@test "connectInject/Deployment: correct vault namespace annotations is set when global.secretsBackend.vault.vaultNamespace is set and agentAnnotations are also set with vaultNamespace annotation" {
cd `chart_dir`
local cmd=$(helm template \
-s templates/connect-inject-deployment.yaml \
--set 'client.enabled=true' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=bar' \
--set 'global.secretsBackend.vault.consulCARole=test' \
--set 'global.secretsBackend.vault.vaultNamespace=vns' \
--set 'global.secretsBackend.vault.agentAnnotations=vault.hashicorp.com/namespace: bar' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.tls.enableAutoEncrypt=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata' | tee /dev/stderr)

local actual="$(echo $cmd |
yq -r '.annotations["vault.hashicorp.com/namespace"]' | tee /dev/stderr)"
[ "${actual}" = "bar" ]
}

#--------------------------------------------------------------------
# enable-webhook-ca-update

Expand Down
Loading

0 comments on commit 053357d

Please sign in to comment.