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 hostNetwork with pod security policies #1090

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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}]' ]
}