Skip to content

Commit

Permalink
Fix PSPs for clients/mesh gateways for hostPorts
Browse files Browse the repository at this point in the history
When hostNetwork is true, Kube sets all container ports as host ports.
  • Loading branch information
lkysow committed Mar 9, 2022
1 parent 27aeda3 commit a650467
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ IMPROVEMENTS:
* Control Plane
* Upgrade Docker image Alpine version from 3.14 to 3.15. [[GH-1058](https://github.com/hashicorp/consul-k8s/pull/1058)]

BUG FIXES:
* Helm
* Fix PodSecurityPolicies for clients/mesh gateways when hostNetwork is used. [[GH-1090](https://github.com/hashicorp/consul-k8s/pull/1090)]

## 0.41.1 (February 24, 2022)

BUG FIXES:
Expand Down
6 changes: 5 additions & 1 deletion charts/consul/templates/client-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ spec:
- min: 8502
max: 8502
{{- end }}
{{- if .Values.client.exposeGossipPorts }}
{{- if (or .Values.client.exposeGossipPorts .Values.client.hostNetwork) }}
- min: 8301
max: 8301
{{- end }}
{{- if .Values.client.hostNetwork }}
- min: 8600
max: 8600
{{- end }}
hostIPC: false
hostPID: false
runAsUser:
Expand Down
8 changes: 8 additions & 0 deletions charts/consul/templates/mesh-gateway-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ spec:
{{- else }}
hostNetwork: false
{{- end }}
hostPorts:
{{- if .Values.meshGateway.hostPort }}
- min: {{ .Values.meshGateway.hostPort }}
max: {{ .Values.meshGateway.hostPort }}
{{- else if .Values.meshGateway.hostNetwork }}
- min: {{ .Values.meshGateway.containerPort }}
max: {{ .Values.meshGateway.containerPort }}
{{- end }}
hostIPC: false
hostPID: false
runAsUser:
Expand Down
14 changes: 14 additions & 0 deletions charts/consul/test/unit/client-podsecuritypolicy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,21 @@ load _helpers
[ "${actual}" = "true" ]
}

@test "client/PodSecurityPolicy: hostPorts when hostNetwork=true" {
# hostPorts must be allowed because when Kube sets all container ports as host ports when hostNetwork is true.
cd `chart_dir`
local actual=$(helm template \
-s templates/client-podsecuritypolicy.yaml \
--set 'global.enablePodSecurityPolicies=true' \
--set 'client.hostNetwork=true' \
. | tee /dev/stderr |
yq -c '.spec.hostPorts' | tee /dev/stderr)
[ "${actual}" = '[{"min":8500,"max":8500},{"min":8502,"max":8502},{"min":8301,"max":8301},{"min":8600,"max":8600}]' ]
}

#--------------------------------------------------------------------
# client.hostNetwork = false

@test "client/PodSecurityPolicy: enabled with global.enablePodSecurityPolicies=true and default hostNetwork=false" {
cd `chart_dir`
local actual=$(helm template \
Expand Down
26 changes: 26 additions & 0 deletions charts/consul/test/unit/mesh-gateway-podsecuritypolicy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,29 @@ load _helpers
yq '.spec.hostNetwork' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "meshGateway/PodSecurityPolicy: hostPorts are allowed when setting hostPort" {
cd `chart_dir`
local actual=$(helm template \
-s templates/mesh-gateway-podsecuritypolicy.yaml \
--set 'meshGateway.enabled=true' \
--set 'connectInject.enabled=true' \
--set 'global.enablePodSecurityPolicies=true' \
--set 'meshGateway.hostPort=9999' \
. | tee /dev/stderr |
yq -c '.spec.hostPorts' | tee /dev/stderr)
[ "${actual}" = '[{"min":9999,"max":9999}]' ]
}

@test "meshGateway/PodSecurityPolicy: hostPorts are allowed when hostNetwork=true" {
cd `chart_dir`
local actual=$(helm template \
-s templates/mesh-gateway-podsecuritypolicy.yaml \
--set 'meshGateway.enabled=true' \
--set 'connectInject.enabled=true' \
--set 'global.enablePodSecurityPolicies=true' \
--set 'meshGateway.hostNetwork=true' \
. | tee /dev/stderr |
yq -c '.spec.hostPorts' | tee /dev/stderr)
[ "${actual}" = '[{"min":8443,"max":8443}]' ]
}

0 comments on commit a650467

Please sign in to comment.