Skip to content

Commit

Permalink
Support restricted PSA enforcement in basic setup
Browse files Browse the repository at this point in the history
This enables running Consul in a basic configuration with PSA enforcement
set to restricted on the namespace where Consul is deployed. (This
requires deploying the CNI to a different privileged namespace).

On OpenShift, we have the option to set the security context or not. If
the security context is unset, then it is set automatically by OpenShift
SCCs. However, we prefer to set the security context to avoid useless
warnings on OpenShift and to reduce the config difference between
OpenShift and plain Kube. By default, OpenShift namespaces have the
audit and warn PSA labels set to restricted, so we receive pod security
warnings when deploying Consul to OpenShift even though the pods will be
able to run.
  • Loading branch information
Paul Glass committed Jul 14, 2023
1 parent df0e649 commit 45d3af0
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 1 deletion.
15 changes: 14 additions & 1 deletion charts/consul/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ as well as the global.name setting.
{{- end -}}
{{- end -}}

{{- define "consul.restrictedSecurityContext" -}}
{{- if not .Values.global.enablePodSecurityPolicies -}}
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
{{- end -}}
{{- end -}}

{{- define "consul.vaultSecretTemplate" -}}
|
{{ "{{" }}- with secret "{{ .secretName }}" -{{ "}}" }}
Expand Down Expand Up @@ -422,4 +435,4 @@ Usage: {{ template "consul.validateTelemetryCollectorCloud" . }}
{{- if or (and .Values.telemetryCollector.cloud.clientSecret.secretName .Values.telemetryCollector.cloud.clientSecret.secretKey .Values.telemetryCollector.cloud.clientId.secretName .Values.telemetryCollector.cloud.clientId.secretKey (not .Values.global.cloud.resourceId.secretKey)) }}
{{fail "When telemetryCollector has clientId and clientSecret .global.cloud.resourceId.secretKey must be set"}}
{{- end }}
{{- end -}}
{{- end -}}
8 changes: 8 additions & 0 deletions charts/consul/templates/connect-inject-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ spec:
- containerPort: 8080
name: webhook-server
protocol: TCP
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
env:
- name: NAMESPACE
valueFrom:
Expand Down
1 change: 1 addition & 0 deletions charts/consul/templates/gateway-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ spec:
containers:
- name: gateway-cleanup
image: {{ .Values.global.imageK8S }}
{{- include "consul.restrictedSecurityContext" . | nindent 10 }}
command:
- consul-k8s-control-plane
args:
Expand Down
1 change: 1 addition & 0 deletions charts/consul/templates/gateway-resources-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ spec:
containers:
- name: gateway-resources
image: {{ .Values.global.imageK8S }}
{{- include "consul.restrictedSecurityContext" . | nindent 10 }}
command:
- consul-k8s-control-plane
args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
containers:
- name: gossip-encryption-autogen
image: "{{ .Values.global.imageK8S }}"
{{- include "consul.restrictedSecurityContext" . | nindent 10 }}
command:
- "/bin/sh"
- "-ec"
Expand Down
1 change: 1 addition & 0 deletions charts/consul/templates/server-acl-init-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ spec:
containers:
- name: server-acl-init-cleanup
image: {{ .Values.global.imageK8S }}
{{- include "consul.restrictedSecurityContext" . | nindent 10 }}
command:
- consul-k8s-control-plane
args:
Expand Down
1 change: 1 addition & 0 deletions charts/consul/templates/server-acl-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ spec:
containers:
- name: server-acl-init-job
image: {{ .Values.global.imageK8S }}
{{- include "consul.restrictedSecurityContext" . | nindent 8 }}
env:
- name: NAMESPACE
valueFrom:
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 @@ -238,6 +238,7 @@ spec:
volumeMounts:
- name: extra-config
mountPath: /consul/extra-config
{{- include "consul.restrictedSecurityContext" . | nindent 8 }}
containers:
- name: consul
image: "{{ default .Values.global.image .Values.server.image }}"
Expand Down Expand Up @@ -530,6 +531,8 @@ spec:
{{- if not .Values.global.openshift.enabled }}
securityContext:
{{- toYaml .Values.server.containerSecurityContext.server | nindent 12 }}
{{- else }}
{{- include "consul.restrictedSecurityContext" . | nindent 10 }}
{{- end }}
{{- if .Values.server.extraContainers }}
{{ toYaml .Values.server.extraContainers | nindent 8 }}
Expand Down
1 change: 1 addition & 0 deletions charts/consul/templates/tls-init-cleanup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
containers:
- name: tls-init-cleanup
image: "{{ .Values.global.image }}"
{{- include "consul.restrictedSecurityContext" . | nindent 10 }}
env:
- name: NAMESPACE
valueFrom:
Expand Down
1 change: 1 addition & 0 deletions charts/consul/templates/tls-init-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ spec:
containers:
- name: tls-init
image: "{{ .Values.global.imageK8S }}"
{{- include "consul.restrictedSecurityContext" . | nindent 10 }}
env:
- name: NAMESPACE
valueFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ spec:
-deployment-namespace={{ .Release.Namespace }}
image: {{ .Values.global.imageK8S }}
name: webhook-cert-manager
{{- include "consul.restrictedSecurityContext" . | nindent 8 }}
resources:
limits:
cpu: 100m
Expand Down

0 comments on commit 45d3af0

Please sign in to comment.