diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl index 8a4888abb..4ca60ea35 100644 --- a/templates/_helpers.tpl +++ b/templates/_helpers.tpl @@ -117,9 +117,9 @@ for users looking to use this chart with Consul Helm. {{- define "vault.args" -}} {{ if or (eq .mode "standalone") (eq .mode "ha") }} - | - sed -E "s/HOST_IP/${HOST_IP?}/g" /vault/config/extraconfig-from-values.hcl > /tmp/storageconfig.hcl; - sed -Ei "s/POD_IP/${POD_IP?}/g" /tmp/storageconfig.hcl; - /usr/local/bin/docker-entrypoint.sh vault server -config=/tmp/storageconfig.hcl + sed -E "s/HOST_IP/${HOST_IP?}/g" /vault/config/extraconfig-from-values.hcl > /home/vault/storageconfig.hcl; + sed -Ei "s/POD_IP/${POD_IP?}/g" /home/vault/storageconfig.hcl; + /usr/local/bin/docker-entrypoint.sh vault server -config=/home/vault/storageconfig.hcl {{ end }} {{- end -}} diff --git a/templates/server-statefulset.yaml b/templates/server-statefulset.yaml index 48edf16cf..ef553e46a 100644 --- a/templates/server-statefulset.yaml +++ b/templates/server-statefulset.yaml @@ -45,10 +45,16 @@ spec: fsGroup: {{ .Values.server.gid | default 1000 }} volumes: {{ template "vault.volumes" . }} + - name: home-volume + emptyDir: + medium: Memory containers: - name: vault {{ template "vault.resources" . }} securityContext: + {{- if .Values.server.securityContext.readOnlyRootFilesystem }} + readOnlyRootFilesystem: true + {{- end }} capabilities: add: ["IPC_LOCK"] image: {{ .Values.server.image.repository }}:{{ .Values.server.image.tag | default "latest" }} @@ -77,6 +83,8 @@ spec: {{- include "vault.extraSecretEnvironmentVars" .Values.server | nindent 12 }} volumeMounts: {{ template "vault.mounts" . }} + - name: home-volume + mountPath: /home/vault ports: - containerPort: 8200 name: http @@ -122,7 +130,7 @@ spec: {{- end }} lifecycle: # Vault container doesn't receive SIGTERM from Kubernetes - # and after the grace period ends, Kube sends SIGKILL. This + # and after the grace period ends, Kube sends SIGKILL. This # causes issues with graceful shutdowns such as deregistering itself # from Consul (zombie services). preStop: diff --git a/test/acceptance/server-dev.bats b/test/acceptance/server-dev.bats index e6aecbed7..9c02b57cb 100644 --- a/test/acceptance/server-dev.bats +++ b/test/acceptance/server-dev.bats @@ -15,7 +15,7 @@ load _helpers # Volume Mounts local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.containers[0].volumeMounts | length') - [ "${volumeCount}" == "0" ] + [ "${volumeCount}" == "1" ] # Service local service=$(kubectl get service "$(name_prefix)" --output json | diff --git a/test/acceptance/server-ha.bats b/test/acceptance/server-ha.bats index 9e4d27e07..f02ebc28c 100644 --- a/test/acceptance/server-ha.bats +++ b/test/acceptance/server-ha.bats @@ -31,12 +31,12 @@ load _helpers # Volume Mounts local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.containers[0].volumeMounts | length') - [ "${volumeCount}" == "1" ] + [ "${volumeCount}" == "2" ] # Volumes local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.volumes | length') - [ "${volumeCount}" == "1" ] + [ "${volumeCount}" == "2" ] local volume=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.volumes[0].configMap.name') diff --git a/test/acceptance/server.bats b/test/acceptance/server.bats index 1ceef852e..13ae9ca64 100644 --- a/test/acceptance/server.bats +++ b/test/acceptance/server.bats @@ -34,7 +34,7 @@ load _helpers # Volume Mounts local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.containers[0].volumeMounts | length') - [ "${volumeCount}" == "2" ] + [ "${volumeCount}" == "3" ] local mountName=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.containers[0].volumeMounts[0].name') @@ -47,7 +47,7 @@ load _helpers # Volumes local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.volumes | length') - [ "${volumeCount}" == "1" ] + [ "${volumeCount}" == "2" ] local volume=$(kubectl get statefulset "$(name_prefix)" --output json | jq -r '.spec.template.spec.volumes[0].configMap.name') diff --git a/test/unit/server-dev-statefulset.bats b/test/unit/server-dev-statefulset.bats index 5f1e45a2f..1df7d5e80 100755 --- a/test/unit/server-dev-statefulset.bats +++ b/test/unit/server-dev-statefulset.bats @@ -390,3 +390,24 @@ load _helpers yq -r '.spec.template.spec.securityContext.fsGroup' | tee /dev/stderr) [ "${actual}" = "2000" ] } + +@test "server/dev-StatefulSet: readOnlyRootFilesystem default" { + cd `chart_dir` + local actual=$(helm template \ + -x templates/server-statefulset.yaml \ + --set 'server.dev.enabled=true' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem' | tee /dev/stderr) + [ "${actual}" = "true" ] +} + +@test "server/dev-StatefulSet: readOnlyRootFilesystem configurable" { + cd `chart_dir` + local actual=$(helm template \ + -x templates/server-statefulset.yaml \ + --set 'server.dev.enabled=true' \ + --set 'server.securityContext.readOnlyRootFilesystem=false' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem' | tee /dev/stderr) + [ "${actual}" = "null" ] +} diff --git a/test/unit/server-ha-statefulset.bats b/test/unit/server-ha-statefulset.bats index 833a304f2..300f9e705 100755 --- a/test/unit/server-ha-statefulset.bats +++ b/test/unit/server-ha-statefulset.bats @@ -578,3 +578,24 @@ load _helpers yq -r '.spec.template.spec.securityContext.fsGroup' | tee /dev/stderr) [ "${actual}" = "2000" ] } + +@test "server/ha-StatefulSet: readOnlyRootFilesystem default" { + cd `chart_dir` + local actual=$(helm template \ + -x templates/server-statefulset.yaml \ + --set 'server.ha.enabled=true' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem' | tee /dev/stderr) + [ "${actual}" = "true" ] +} + +@test "server/ha-StatefulSet: readOnlyRootFilesystem configurable" { + cd `chart_dir` + local actual=$(helm template \ + -x templates/server-statefulset.yaml \ + --set 'server.ha.enabled=true' \ + --set 'server.securityContext.readOnlyRootFilesystem=false' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem' | tee /dev/stderr) + [ "${actual}" = "null" ] +} diff --git a/test/unit/server-statefulset.bats b/test/unit/server-statefulset.bats index cfbbc7002..3f83ebb62 100755 --- a/test/unit/server-statefulset.bats +++ b/test/unit/server-statefulset.bats @@ -667,7 +667,7 @@ load _helpers local containers_count=$(echo $object | yq -r 'length' | tee /dev/stderr) - [ "${containers_count}" = 1 ] + [ "${containers_count}" = 1 ] } # extra labels @@ -742,6 +742,25 @@ load _helpers [ "${actual}" = "2000" ] } +@test "server/standalone-StatefulSet: readOnlyRootFilesystem default" { + cd `chart_dir` + local actual=$(helm template \ + -x templates/server-statefulset.yaml \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem' | tee /dev/stderr) + [ "${actual}" = "true" ] +} + +@test "server/standalone-StatefulSet: readOnlyRootFilesystem configurable" { + cd `chart_dir` + local actual=$(helm template \ + -x templates/server-statefulset.yaml \ + --set 'server.securityContext.readOnlyRootFilesystem=false' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem' | tee /dev/stderr) + [ "${actual}" = "null" ] +} + #-------------------------------------------------------------------- # health checks diff --git a/values.yaml b/values.yaml index 63939fee9..470ea54eb 100644 --- a/values.yaml +++ b/values.yaml @@ -16,6 +16,9 @@ server: # should map directly to the value of the resources field for a PodSpec. # By default no direct resource request is made. + securityContext: + readOnlyRootFilesystem: true + image: repository: "vault" tag: 1.3.0 @@ -31,7 +34,7 @@ server: # memory: 256Mi # cpu: 250m - # Ingress allows ingress services to be created to allow external access + # Ingress allows ingress services to be created to allow external access # from Kubernetes to access Vault pods. ingress: enabled: false @@ -55,7 +58,7 @@ server: # method. https://www.vaultproject.io/docs/auth/kubernetes.html authDelegator: enabled: false - + # extraContainers is a list of sidecar containers. Specified as a raw YAML string. extraContainers: null