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

Support TLS with vault for the server-acl-init job #889

Merged
merged 5 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions acceptance/tests/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,13 @@ func TestVault(t *testing.T) {
"global.secretsBackend.vault.connectCA.rootPKIPath": "connect_root",
"global.secretsBackend.vault.connectCA.intermediatePKIPath": "connect_inter",

//"global.acls.manageSystemACLs": "true",
"global.acls.manageSystemACLs": "true",
"global.tls.enabled": "true",
"global.gossipEncryption.secretName": "consul/data/secret/gossip",
"global.gossipEncryption.secretKey": "gossip",

"server.serverCert.secretName": "pki_int/issue/consul-server",
"global.tls.caCert.secretName": "pki_int/cert/ca",
"global.tls.httpsOnly": "false",
"global.tls.enableAutoEncrypt": "true",
}
logger.Log(t, "Installing Consul")
Expand All @@ -175,7 +174,7 @@ func TestVault(t *testing.T) {

// Validate that the gossip encryption key is set correctly.
logger.Log(t, "Validating the gossip key has been set correctly.")
consulClient := consulCluster.SetupConsulClient(t, false)
consulClient := consulCluster.SetupConsulClient(t, true)
keys, err := consulClient.Operator().KeyringList(nil)
require.NoError(t, err)
// There are two identical keys for LAN and WAN since there is only 1 dc.
Expand Down
19 changes: 17 additions & 2 deletions charts/consul/templates/server-acl-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,23 @@ spec:
component: server-acl-init
annotations:
"consul.hashicorp.com/connect-inject": "false"
{{- if (and .Values.global.secretsBackend.vault.enabled .Values.global.tls.enabled) }}
"vault.hashicorp.com/agent-pre-populate-only": "true"
kschoche marked this conversation as resolved.
Show resolved Hide resolved
"vault.hashicorp.com/agent-inject": "true"
"vault.hashicorp.com/role": {{ .Values.global.secretsBackend.vault.consulCARole }}
"vault.hashicorp.com/agent-inject-secret-serverca": {{ .Values.global.tls.caCert.secretName }}
"vault.hashicorp.com/agent-inject-template-serverca": {{ template "consul.serverTLSCATemplate" . }}
{{- 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 }}"
"vault.hashicorp.com/ca-cert": "/vault/custom/{{ .Values.global.secretsBackend.vault.ca.secretKey }}"
{{- end }}
{{- end }}
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-server-acl-init
{{- if (or .Values.global.tls.enabled (and .Values.global.acls.replicationToken.secretName .Values.global.acls.replicationToken.secretKey) (and .Values.global.acls.bootstrapToken.secretName .Values.global.acls.bootstrapToken.secretKey)) }}
volumes:
{{- if .Values.global.tls.enabled }}
{{- if and .Values.global.tls.enabled (not .Values.global.secretsBackend.vault.enabled) }}
- name: consul-ca-cert
secret:
{{- if .Values.global.tls.caCert.secretName }}
Expand Down Expand Up @@ -76,7 +87,7 @@ spec:
fieldPath: metadata.namespace
{{- if (or .Values.global.tls.enabled (and .Values.global.acls.replicationToken.secretName .Values.global.acls.replicationToken.secretKey) (and .Values.global.acls.bootstrapToken.secretName .Values.global.acls.bootstrapToken.secretKey)) }}
volumeMounts:
{{- if .Values.global.tls.enabled }}
{{- if and .Values.global.tls.enabled (not .Values.global.secretsBackend.vault.enabled) }}
- name: consul-ca-cert
mountPath: /consul/tls/ca
readOnly: true
Expand Down Expand Up @@ -118,8 +129,12 @@ spec:
{{- if .Values.global.tls.enabled }}
-use-https \
{{- if not (and .Values.externalServers.enabled .Values.externalServers.useSystemRoots) }}
{{- if .Values.global.secretsBackend.vault.enabled }}
-consul-ca-cert=/vault/secrets/serverca \
{{- else }}
-consul-ca-cert=/consul/tls/ca/tls.crt \
{{- end }}
{{- end }}
{{- if not .Values.externalServers.enabled }}
-server-port=8501 \
{{- end }}
Expand Down
124 changes: 124 additions & 0 deletions charts/consul/test/unit/server-acl-init-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,130 @@ load _helpers
[ "${actual}" = "key" ]
}

@test "serverACLInit/Job: configures server CA to come from vault when vault is enabled" {
cd `chart_dir`
local object=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'global.tls.enabled=true' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=test' \
--set 'global.secretsBackend.vault.consulServerRole=foo' \
--set 'global.secretsBackend.vault.consulCARole=carole' \
. | tee /dev/stderr |
yq -r '.spec.template' | tee /dev/stderr)

# Check annotations
local actual
actual=$(echo $object | jq -r '.metadata.annotations["vault.hashicorp.com/agent-pre-populate-only"]' | tee /dev/stderr)
[ "${actual}" = "true" ]
local actual
actual=$(echo $object | jq -r '.metadata.annotations["vault.hashicorp.com/agent-inject"]' | tee /dev/stderr)
[ "${actual}" = "true" ]
local actual
actual=$(echo $object | jq -r '.metadata.annotations["vault.hashicorp.com/role"]' | tee /dev/stderr)
[ "${actual}" = "carole" ]
local actual
actual=$(echo $object | jq -r '.metadata.annotations["vault.hashicorp.com/agent-inject-secret-serverca"]' | tee /dev/stderr)
[ "${actual}" = "foo" ]
local actual
actual=$(echo $object | jq -r '.metadata.annotations["vault.hashicorp.com/agent-inject-template-serverca"]' | tee /dev/stderr)
[ "${actual}" = $'{{- with secret \"foo\" -}}\n{{- .Data.certificate -}}\n{{- end -}}' ]

# Check that the consul-ca-cert volume is not attached
local actual=$(echo $object | jq -r '.spec.volumes')
[ "${actual}" = "null" ]

local actual=$(echo $object | jq -r '.spec.containers[] | select(.name="post-install-job").volumeMounts')
[ "${actual}" = "null" ]
}

@test "serverACLInit/Job: vault CA is not configured by default" {
cd `chart_dir`
local object=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'global.tls.enabled=true' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=test' \
. | tee /dev/stderr |
yq -r '.spec.template' | tee /dev/stderr)

local actual=$(echo $object | yq -r '.metadata.annotations | has("vault.hashicorp.com/agent-extra-secret")')
[ "${actual}" = "false" ]
local actual=$(echo $object | yq -r '.metadata.annotations | has("vault.hashicorp.com/ca-cert")')
[ "${actual}" = "false" ]
}

@test "serverACLInit/Job: vault CA is not configured when secretName is set but secretKey is not" {
cd `chart_dir`
local object=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'global.tls.enabled=true' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=test' \
--set 'global.secretsBackend.vault.ca.secretName=ca' \
. | tee /dev/stderr |
yq -r '.spec.template' | tee /dev/stderr)

local actual=$(echo $object | yq -r '.metadata.annotations | has("vault.hashicorp.com/agent-extra-secret")')
[ "${actual}" = "false" ]
local actual=$(echo $object | yq -r '.metadata.annotations | has("vault.hashicorp.com/ca-cert")')
[ "${actual}" = "false" ]
}

@test "serverACLInit/Job: vault CA is not configured when secretKey is set but secretName is not" {
cd `chart_dir`
local object=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'global.tls.enabled=true' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=test' \
--set 'global.secretsBackend.vault.ca.secretKey=tls.crt' \
. | tee /dev/stderr |
yq -r '.spec.template' | tee /dev/stderr)

local actual=$(echo $object | yq -r '.metadata.annotations | has("vault.hashicorp.com/agent-extra-secret")')
[ "${actual}" = "false" ]
local actual=$(echo $object | yq -r '.metadata.annotations | has("vault.hashicorp.com/ca-cert")')
[ "${actual}" = "false" ]
}

@test "serverACLInit/Job: vault CA is configured when both secretName and secretKey are set" {
cd `chart_dir`
local object=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'global.tls.enabled=true' \
--set 'global.tls.enableAutoEncrypt=true' \
--set 'global.tls.caCert.secretName=foo' \
--set 'global.secretsBackend.vault.enabled=true' \
--set 'global.secretsBackend.vault.consulClientRole=foo' \
--set 'global.secretsBackend.vault.consulServerRole=test' \
--set 'global.secretsBackend.vault.ca.secretName=ca' \
--set 'global.secretsBackend.vault.ca.secretKey=tls.crt' \
. | tee /dev/stderr |
yq -r '.spec.template' | tee /dev/stderr)

local actual=$(echo $object | yq -r '.metadata.annotations."vault.hashicorp.com/agent-extra-secret"')
[ "${actual}" = "ca" ]
local actual=$(echo $object | yq -r '.metadata.annotations."vault.hashicorp.com/ca-cert"')
[ "${actual}" = "/vault/custom/tls.crt" ]
}

#--------------------------------------------------------------------
# namespaces

Expand Down