@@ -12,7 +12,7 @@ metadata:
1212 policies.kyverno.io/description : >-
1313 Access to host ports allows potential snooping of network traffic and should not be
1414 allowed, or at minimum restricted to a known list. This policy ensures the `hostPort`
15- field is unset or set to `0`.
15+ field is unset or set to `0`.
1616spec :
1717 validationFailureAction : Audit
1818 background : true
@@ -28,26 +28,17 @@ spec:
2828 - UPDATE
2929 validate :
3030 cel :
31+ variables :
32+ - name : allContainers
33+ expression : >-
34+ object.spec.containers +
35+ object.spec.?initContainers.orValue([]) +
36+ object.spec.?ephemeralContainers.orValue([])
3137 expressions :
32- - expression : >-
33- object.spec.containers. all(container, !has(container.ports) ||
34- container.ports.all(port, !has( port.hostPort) || port.hostPort == 0))
38+ - expression : >-
39+ variables.allContainers. all(container,
40+ container.? ports.orValue([]). all(port, port.? hostPort.orValue(0) == 0))
3541 message: >-
36- Use of host ports is disallowed. The field spec.containers[*].ports[*].hostPort
37- must either be unset or set to `0`.
38-
39- - expression : >-
40- !has(object.spec.initContainers) ||
41- object.spec.initContainers.all(container, !has(container.ports) ||
42- container.ports.all(port, !has(port.hostPort) || port.hostPort == 0))
43- message: >-
44- Use of host ports is disallowed. The field spec.initContainers[*].ports[*].hostPort
45- must either be unset or set to `0`.
46-
47- - expression : >-
48- !has(object.spec.ephemeralContainers) ||
49- object.spec.ephemeralContainers.all(container, !has(container.ports) ||
50- container.ports.all(port, !has(port.hostPort) || port.hostPort == 0))
51- message: >-
52- Use of host ports is disallowed. The field spec.ephemeralContainers[*].ports[*].hostPort
53- must either be unset or set to `0`.
42+ Use of host ports is disallowed. The fields spec.containers[*].ports[*].hostPort,
43+ spec.initContainers[*].ports[*].hostPort, and spec.ephemeralContainers[*].ports[*].hostPort
44+ must either be unset or set to `0`.
0 commit comments