From 1f9c1b549e956395b77b063874094a5f46f80d48 Mon Sep 17 00:00:00 2001 From: Lavish pal Date: Tue, 6 Aug 2024 13:50:38 +0530 Subject: [PATCH] Uodate the cel suing variables Signed-off-by: Lavish pal --- .../disallow-host-ports/artifacthub-pkg.yml | 2 +- .../disallow-host-ports.yaml | 31 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pod-security-cel/baseline/disallow-host-ports/artifacthub-pkg.yml b/pod-security-cel/baseline/disallow-host-ports/artifacthub-pkg.yml index ddb7a1027..59028778c 100644 --- a/pod-security-cel/baseline/disallow-host-ports/artifacthub-pkg.yml +++ b/pod-security-cel/baseline/disallow-host-ports/artifacthub-pkg.yml @@ -19,5 +19,5 @@ annotations: kyverno/category: "Pod Security Standards (Baseline) in CEL" kyverno/kubernetesVersion: "1.26-1.27" kyverno/subject: "Pod" -digest: 5380fdcd91c8bc331f46c6df2f4ae05e079a8e367ba071b3f3345858440a6fce +digest: acd23ec8b3bd13a00e143757b60d027eb084a566839c9cf4caafdb06144b88fb createdAt: "2023-12-03T00:22:34Z" diff --git a/pod-security-cel/baseline/disallow-host-ports/disallow-host-ports.yaml b/pod-security-cel/baseline/disallow-host-ports/disallow-host-ports.yaml index c43889b44..963eff8f2 100644 --- a/pod-security-cel/baseline/disallow-host-ports/disallow-host-ports.yaml +++ b/pod-security-cel/baseline/disallow-host-ports/disallow-host-ports.yaml @@ -28,18 +28,19 @@ spec: - UPDATE validate: cel: - expressions: - - expression: >- - [ - object.spec.containers, - object.spec.initContainers, - object.spec.ephemeralContainers - ].all(containers, !has(containers) || - containers.all(container, !has(container.ports) || - container.ports.all(port, !has(port.hostPort) || port.hostPort == 0) - ) - ) - message: |- - Use of host ports is disallowed. The field spec.containers[*].ports[*].hostPort, - spec.initContainers[*].ports[*].hostPort, and spec.ephemeralContainers[*].ports[*].hostPort - must either be unset or set to `0`. \ No newline at end of file + variables: + - name: allContainers + expression: >- + object.spec.containers + + object.spec.?initContainers.orValue([]) + + object.spec.?ephemeralContainers.orValue([]) + expressions: + - expression: >- + variables.allContainers.all(container, + container.?ports.orValue([]).all(port, port.?hostPort.orValue(0) == 0)) + message: |- + Use of host ports is disallowed. The field spec.containers[*].ports[*].hostPort, + spec.initContainers[*].ports[*].hostPort, and spec.ephemeralContainers[*].ports[*].hostPort + must either be unset or set to `0`. + + \ No newline at end of file