Skip to content

Commit a2eca38

Browse files
Simplify Disallow hostPorts in CEL expressions (#1108)
* Update artifacthub-pkg.yml Signed-off-by: Lavish Pal <[email protected]> (cherry picked from commit 8119432) Signed-off-by: Lavish Pal <[email protected]> * refactor: simplify disallow-host-ports CEL expressions using allContainers variable Signed-off-by: Lavish Pal <[email protected]> --------- Signed-off-by: Lavish Pal <[email protected]> Co-authored-by: ShutingZhao <[email protected]>
1 parent a0c586a commit a2eca38

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

pod-security-cel/baseline/disallow-host-ports/artifacthub-pkg.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ annotations:
1919
kyverno/category: "Pod Security Standards (Baseline) in CEL"
2020
kyverno/kubernetesVersion: "1.26-1.27"
2121
kyverno/subject: "Pod"
22-
digest: b95cfe16e11be0b9507736687bd99b5ea78c455f8fc35194220326ea5ff3913c
23-
createdAt: "2023-12-03T00:22:34Z"
22+
digest: 80013655376b57e3f079c5c748013f6b2f04c16eb32f8c8bd0c6208e4a4f71db
23+
createdAt: "2025-10-22T00:05:34Z"

pod-security-cel/baseline/disallow-host-ports/disallow-host-ports.yaml

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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`.
1616
spec:
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

Comments
 (0)