Skip to content

Commit

Permalink
feat: adding security context and annotations to tls and acl init/cle…
Browse files Browse the repository at this point in the history
…anup jobs
  • Loading branch information
Chinikins authored and DanStough committed Jul 10, 2023
1 parent cbcbdc5 commit d8ea0c0
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 8 deletions.
3 changes: 3 additions & 0 deletions charts/consul/templates/gateway-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ spec:
{{- end }}
annotations:
"consul.hashicorp.com/connect-inject": "false"
{{- if .Values.global.acls.annotations }}
{{- tpl .Values.global.acls.annotations . | nindent 8 }}
{{- end }}
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-gateway-cleanup
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/gateway-resources-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ spec:
{{- end }}
annotations:
"consul.hashicorp.com/connect-inject": "false"
{{- if .Values.global.acls.annotations }}
{{- tpl .Values.global.acls.annotations . | nindent 8 }}
{{- end }}
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-gateway-resources
Expand Down
7 changes: 7 additions & 0 deletions charts/consul/templates/server-acl-init-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ spec:
{{- end }}
annotations:
"consul.hashicorp.com/connect-inject": "false"
{{- if .Values.global.acls.annotations }}
{{- tpl .Values.global.acls.annotations . | nindent 8 }}
{{- end }}
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-server-acl-init-cleanup
{{- if .Values.server.containerSecurityContext.aclInit }}
securityContext:
{{- toYaml .Values.server.containerSecurityContext.aclInit | nindent 8 }}
{{- end }}
containers:
- name: server-acl-init-cleanup
image: {{ .Values.global.imageK8S }}
Expand Down
7 changes: 7 additions & 0 deletions charts/consul/templates/server-acl-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ spec:
{{- end }}
annotations:
"consul.hashicorp.com/connect-inject": "false"
{{- if .Values.global.acls.annotations }}
{{- tpl .Values.global.acls.annotations . | nindent 8 }}
{{- end }}
{{- if .Values.global.secretsBackend.vault.enabled }}

{{- /* Run the Vault agent as both an init container and sidecar.
Expand Down Expand Up @@ -94,6 +97,10 @@ spec:
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-server-acl-init
{{- if .Values.server.containerSecurityContext.aclInit }}
securityContext:
{{- toYaml .Values.server.containerSecurityContext.aclInit | nindent 8 }}
{{- end }}
{{- if (or .Values.global.tls.enabled .Values.global.acls.replicationToken.secretName .Values.global.acls.bootstrapToken.secretName) }}
volumes:
{{- if and .Values.global.tls.enabled (not .Values.global.secretsBackend.vault.enabled) }}
Expand Down
7 changes: 7 additions & 0 deletions charts/consul/templates/tls-init-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ spec:
{{- end }}
annotations:
"consul.hashicorp.com/connect-inject": "false"
{{- if .Values.global.tls.annotations }}
{{- tpl .Values.global.tls.annotations . | nindent 8 }}
{{- end }}
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-tls-init-cleanup
{{- if .Values.server.containerSecurityContext.tlsInit }}
securityContext:
{{- toYaml .Values.server.containerSecurityContext.tlsInit | nindent 8 }}
{{- end }}
containers:
- name: tls-init-cleanup
image: "{{ .Values.global.image }}"
Expand Down
7 changes: 7 additions & 0 deletions charts/consul/templates/tls-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ spec:
{{- end }}
annotations:
"consul.hashicorp.com/connect-inject": "false"
{{- if .Values.global.tls.annotations }}
{{- tpl .Values.global.tls.annotations . | nindent 8 }}
{{- end }}
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-tls-init
{{- if .Values.server.containerSecurityContext.tlsInit }}
securityContext:
{{- toYaml .Values.server.containerSecurityContext.tlsInit | nindent 8 }}
{{- end }}
{{- if (and .Values.global.tls.caCert.secretName .Values.global.tls.caKey.secretName) }}
volumes:
- name: consul-ca-cert
Expand Down
24 changes: 23 additions & 1 deletion charts/consul/test/unit/gateway-cleanup-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ target=templates/gateway-cleanup-job.yaml
assert_empty helm template \
-s $target \
--set 'connectInject.enabled=false' \
.
.
}


#--------------------------------------------------------------------
# annotations

@test "gatewaycleanup/Job: no annotations defined by default" {
cd `chart_dir`
local actual=$(helm template \
-s $target \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations | del(."consul.hashicorp.com/connect-inject") | del(."consul.hashicorp.com/config-checksum")' | tee /dev/stderr)
[ "${actual}" = "{}" ]
}

@test "gatewaycleanup/Job: annotations can be set" {
cd `chart_dir`
local actual=$(helm template \
-s $target \
--set 'global.acls.annotations=foo: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}
25 changes: 24 additions & 1 deletion charts/consul/test/unit/gateway-resources-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ target=templates/gateway-resources-job.yaml
assert_empty helm template \
-s $target \
--set 'connectInject.enabled=false' \
.
.
}

@test "gatewayresources/Job: imageK8S set properly" {
Expand Down Expand Up @@ -116,3 +116,26 @@ target=templates/gateway-resources-job.yaml
local actual=$(echo "$spec" | jq '.[14]')
[ "${actual}" = "\"-service-annotations=- bingo\"" ]
}


#--------------------------------------------------------------------
# annotations

@test "gatewayresources/Job: no annotations defined by default" {
cd `chart_dir`
local actual=$(helm template \
-s $target \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations | del(."consul.hashicorp.com/connect-inject") | del(."consul.hashicorp.com/config-checksum")' | tee /dev/stderr)
[ "${actual}" = "{}" ]
}

@test "gatewayresources/Job: annotations can be set" {
cd `chart_dir`
local actual=$(helm template \
-s $target \
--set 'global.acls.annotations=foo: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}
39 changes: 39 additions & 0 deletions charts/consul/test/unit/server-acl-init-cleanup-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,42 @@ load _helpers
yq -r '.spec.template.spec.containers[0].resources.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}

#--------------------------------------------------------------------
# server.containerSecurityContext.aclInit

@test "serverACLInitCleanup/Job: securityContext is set when server.containerSecurityContext.aclInit is set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/server-acl-init-cleanup-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'server.containerSecurityContext.aclInit.runAsUser=100' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.securityContext.runAsUser' | tee /dev/stderr)

[ "${actual}" = "100" ]
}

#--------------------------------------------------------------------
# annotations

@test "serverACLInitCleanup/Job: no annotations defined by default" {
cd `chart_dir`
local actual=$(helm template \
-s templates/server-acl-init-cleanup-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations | del(."consul.hashicorp.com/connect-inject") | del(."consul.hashicorp.com/config-checksum")' | tee /dev/stderr)
[ "${actual}" = "{}" ]
}

@test "serverACLInitCleanup/Job: annotations can be set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/server-acl-init-cleanup-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'global.acls.annotations=foo: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}
52 changes: 46 additions & 6 deletions charts/consul/test/unit/server-acl-init-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,22 @@ load _helpers
[ "${actual}" = "key" ]
}

#--------------------------------------------------------------------
# server.containerSecurityContext.aclInit

@test "serverACLInit/Job: securityContext is set when server.containerSecurityContext.aclInit is set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'server.containerSecurityContext.aclInit.runAsUser=100' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.securityContext.runAsUser' | tee /dev/stderr)

[ "${actual}" = "100" ]

}

#--------------------------------------------------------------------
# Vault

Expand Down Expand Up @@ -2030,7 +2046,7 @@ load _helpers
--set 'global.cloud.authUrl.secretName=auth-url-name' \
.
[ "$status" -eq 1 ]

[[ "$output" =~ "When either global.cloud.authUrl.secretName or global.cloud.authUrl.secretKey is defined, both must be set." ]]
}

Expand All @@ -2050,7 +2066,7 @@ load _helpers
--set 'global.cloud.authUrl.secretKey=auth-url-key' \
.
[ "$status" -eq 1 ]

[[ "$output" =~ "When either global.cloud.authUrl.secretName or global.cloud.authUrl.secretKey is defined, both must be set." ]]
}

Expand All @@ -2070,7 +2086,7 @@ load _helpers
--set 'global.cloud.apiHost.secretName=auth-url-name' \
.
[ "$status" -eq 1 ]

[[ "$output" =~ "When either global.cloud.apiHost.secretName or global.cloud.apiHost.secretKey is defined, both must be set." ]]
}

Expand All @@ -2090,7 +2106,7 @@ load _helpers
--set 'global.cloud.apiHost.secretKey=auth-url-key' \
.
[ "$status" -eq 1 ]

[[ "$output" =~ "When either global.cloud.apiHost.secretName or global.cloud.apiHost.secretKey is defined, both must be set." ]]
}

Expand All @@ -2110,7 +2126,7 @@ load _helpers
--set 'global.cloud.scadaAddress.secretName=scada-address-name' \
.
[ "$status" -eq 1 ]

[[ "$output" =~ "When either global.cloud.scadaAddress.secretName or global.cloud.scadaAddress.secretKey is defined, both must be set." ]]
}

Expand All @@ -2130,7 +2146,7 @@ load _helpers
--set 'global.cloud.scadaAddress.secretKey=scada-address-key' \
.
[ "$status" -eq 1 ]

[[ "$output" =~ "When either global.cloud.scadaAddress.secretName or global.cloud.scadaAddress.secretKey is defined, both must be set." ]]
}

Expand Down Expand Up @@ -2226,3 +2242,27 @@ load _helpers
yq -r '.spec.template.spec.containers[0].resources.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}

#--------------------------------------------------------------------
# annotations

@test "serverACLInit/Job: no annotations defined by default" {
cd `chart_dir`
local actual=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations | del(."consul.hashicorp.com/connect-inject") | del(."consul.hashicorp.com/config-checksum")' | tee /dev/stderr)
[ "${actual}" = "{}" ]
}

@test "serverACLInit/Job: annotations can be set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/server-acl-init-job.yaml \
--set 'global.acls.manageSystemACLs=true' \
--set 'global.acls.annotations=foo: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}
40 changes: 40 additions & 0 deletions charts/consul/test/unit/tls-init-cleanup-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,43 @@ load _helpers
[ "${actualTemplateFoo}" = "bar" ]
[ "${actualTemplateBaz}" = "qux" ]
}

#--------------------------------------------------------------------
# server.containerSecurityContext.tlsInit

@test "tlsInitCleanup/Job: securityContext is set when server.containerSecurityContext.tlsInit is set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/tls-init-cleanup-job.yaml \
--set 'global.tls.enabled=true' \
--set 'server.containerSecurityContext.tlsInit.runAsUser=100' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.securityContext.runAsUser' | tee /dev/stderr)

[ "${actual}" = "100" ]
}


#--------------------------------------------------------------------
# annotations

@test "tlsInitCleanup/Job: no annotations defined by default" {
cd `chart_dir`
local actual=$(helm template \
-s templates/tls-init-cleanup-job.yaml \
--set 'global.tls.enabled=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations | del(."consul.hashicorp.com/connect-inject") | del(."consul.hashicorp.com/config-checksum")' | tee /dev/stderr)
[ "${actual}" = "{}" ]
}

@test "tlsInitCleanup/Job: annotations can be set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/tls-init-cleanup-job.yaml \
--set 'global.tls.enabled=true' \
--set 'global.tls.annotations=foo: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}
39 changes: 39 additions & 0 deletions charts/consul/test/unit/tls-init-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,42 @@ load _helpers
[ "${actualTemplateFoo}" = "bar" ]
[ "${actualTemplateBaz}" = "qux" ]
}

#--------------------------------------------------------------------
# server.containerSecurityContext.tlsInit

@test "tlsInit/Job: securityContext is set when server.containerSecurityContext.tlsInit is set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/tls-init-job.yaml \
--set 'global.tls.enabled=true' \
--set 'server.containerSecurityContext.tlsInit.runAsUser=100' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.securityContext.runAsUser' | tee /dev/stderr)

[ "${actual}" = "100" ]
}

#--------------------------------------------------------------------
# annotations

@test "tlsInit/Job: no annotations defined by default" {
cd `chart_dir`
local actual=$(helm template \
-s templates/tls-init-job.yaml \
--set 'global.tls.enabled=true' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations | del(."consul.hashicorp.com/connect-inject") | del(."consul.hashicorp.com/config-checksum")' | tee /dev/stderr)
[ "${actual}" = "{}" ]
}

@test "tlsInit/Job: annotations can be set" {
cd `chart_dir`
local actual=$(helm template \
-s templates/tls-init-job.yaml \
--set 'global.tls.enabled=true' \
--set 'global.tls.annotations=foo: bar' \
. | tee /dev/stderr |
yq -r '.spec.template.metadata.annotations.foo' | tee /dev/stderr)
[ "${actual}" = "bar" ]
}
Loading

0 comments on commit d8ea0c0

Please sign in to comment.