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

feat: add other policies in CEL expressions - Part 7 #974

Merged
merged 58 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
58f9d65
copy advanced-restrict-image-registries
Chandan-DK Apr 21, 2024
6ba0ddf
remove unused cm and remove glob pattern *
Chandan-DK Apr 21, 2024
6d12506
convert advanced-restrict-image-registries
Chandan-DK Apr 21, 2024
6506b98
add CI tests for directories starting with a
Chandan-DK Apr 21, 2024
05342f2
add CI tests for directories [b-d]
Chandan-DK Apr 21, 2024
f9520a7
copy block-pod-exec-by-namespace
Chandan-DK Apr 22, 2024
615fae1
convert block-pod-exec-by-namespace
Chandan-DK Apr 22, 2024
6fb001f
add CI test for directories e to l
Chandan-DK Apr 24, 2024
2fe0a50
copy exclude-namespaces-dynamically
Chandan-DK Apr 24, 2024
fe3f040
rename namespace for clarity
Chandan-DK Apr 24, 2024
8b0c265
convert exclude-namespaces-dynamically
Chandan-DK Apr 24, 2024
6df4f40
remove kyverno tests for exclude-namespaces-dynamically
Chandan-DK Apr 24, 2024
9915910
copy deny-commands-in-exec-probe
Chandan-DK Apr 25, 2024
25af46a
convert deny-commands-in-exec-probe
Chandan-DK Apr 25, 2024
28d39f6
copy limit-hostpath-vols
Chandan-DK Apr 26, 2024
f008ce6
convert limit-hostpath-vols
Chandan-DK Apr 26, 2024
bbc87b6
copy require-container-port-names
Chandan-DK Apr 27, 2024
968e0e3
add kyverno tests for require-container-port-names
Chandan-DK Apr 27, 2024
2ef7c29
convert require-container-port-names
Chandan-DK Apr 27, 2024
84b01ae
add CI tests for directories re[c-q]
Chandan-DK Apr 27, 2024
e261fd8
copy restrict-node-selection
Chandan-DK Apr 28, 2024
3651745
correct invalid nodeSelectors
Chandan-DK Apr 28, 2024
a0cf8b0
convert restrict-node-selection
Chandan-DK Apr 28, 2024
500fafd
correct invalid nodeSelectors
Chandan-DK Apr 28, 2024
d94b729
add CI tests for directories starting with res
Chandan-DK Apr 28, 2024
a6d2c70
copy topologyspreadconstraints-policy
Chandan-DK Apr 29, 2024
549a46d
rename resources for clarity in kyverno test
Chandan-DK Apr 29, 2024
7c2bc2e
convert topologyspreadconstraints-policy
Chandan-DK Apr 29, 2024
caf41c1
add CI tests for directories [s-z]
Chandan-DK Apr 29, 2024
1fb512d
copy require-emptydir-requests-limits
Chandan-DK May 18, 2024
0abfcf1
convert require-emptydir-requests-limits
Chandan-DK May 19, 2024
eed341b
copy require-non-root-groups
Chandan-DK May 19, 2024
e111888
convert require-non-root-groups
Chandan-DK May 19, 2024
c8b052e
copy require-qos-guaranteed
Chandan-DK May 19, 2024
12b368b
convert require-qos-guaranteed
Chandan-DK May 19, 2024
c335bf2
copy restrict-edit-for-endpoints
Chandan-DK May 19, 2024
e068ce1
convert restrict-edit-for-endpoints
Chandan-DK May 19, 2024
69665e2
copy restrict-jobs
Chandan-DK May 19, 2024
65f5416
add kyverno tests for restrict-jobs
Chandan-DK May 19, 2024
8ad5f49
remove unused files in chainsaw tests
Chandan-DK May 19, 2024
8149275
convert restrict-jobs
Chandan-DK May 19, 2024
ebc1055
Merge branch 'main' into other-policies-cel-part-7
Chandan-DK May 19, 2024
5bb10f6
remove repeated tests in CI
Chandan-DK May 19, 2024
16a11f2
copy restrict-node-label-creation
Chandan-DK May 20, 2024
7537a77
convert restrict-node-label-creation
Chandan-DK May 20, 2024
7b415d3
copy restrict-pod-controller-serviceaccount-updates
Chandan-DK May 20, 2024
bea97b1
convert restrict-pod-controller-serviceaccount-updates
Chandan-DK May 20, 2024
43d9613
rename files for clarity
Chandan-DK May 30, 2024
c7e6031
Merge branch 'main' into other-policies-cel-part-7
Chandan-DK May 30, 2024
445dfd5
fix cel policies tests caused by chainsaw templating
Chandan-DK May 30, 2024
1e4da09
Merge branch 'main' into other-policies-cel-part-7
Chandan-DK Jun 5, 2024
971ff3a
Merge branch 'main' into other-policies-cel-part-7
Chandan-DK Jun 9, 2024
b8efb8d
access namespace name using namespaceObject
Chandan-DK Jun 9, 2024
ebbc63b
Merge branch 'main' into other-policies-cel-part-7
MariamFahmy98 Jul 9, 2024
ea6275d
Merge branch 'main' into other-policies-cel-part-7
Chandan-DK Jul 9, 2024
3cbda29
add CREATE and UPDATE operations
Chandan-DK Jul 9, 2024
d55ac0a
delete block-pod-exec-by-namespace and restrict-node-selection as the…
Chandan-DK Jul 9, 2024
69fc62a
Merge branch 'main' into other-policies-cel-part-7
Chandan-DK Jul 9, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
- ^other$/^res
- ^other-cel$/^res
- ^other$/^[s-z]
- ^other-cel$/^res
- ^other-cel$/^[s-z]
- ^pod-security$
- ^pod-security-cel$
- ^psa$
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
creationTimestamp: null
name: advanced-restrict-image-registries
spec:
# disable templating because it can cause issues with CEL expressions
template: false
steps:
- name: step-01
try:
- apply:
file: ../advanced-restrict-image-registries.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: advanced-restrict-image-registries
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: ns-01.yaml
- apply:
file: ns-02.yaml
- apply:
file: cm.yaml
- name: step-03
try:
- apply:
file: pod-good.yaml
- apply:
expect:
- check:
($error != null): true
file: pod-bad.yaml
- apply:
file: podcontroller-good.yaml
- apply:
expect:
- check:
($error != null): true
file: podcontroller-bad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
data:
registries: ghcr.io/
kind: ConfigMap
metadata:
name: clusterregistries
namespace: default
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
annotations:
corp.com/allowed-registries: img.corp.com/
name: imageregistries-ns01
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
annotations:
corp.com/allowed-registries: docker.io/
name: imageregistries-ns02
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
apiVersion: v1
kind: Pod
metadata:
name: badpod01
spec:
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: badpod02
namespace: imageregistries-ns01
spec:
initContainers:
- name: busybox01-init
image: busybox:1.35
- name: busybox02-init
image: ghcr.io/busybox:1.35
containers:
- name: busybox01
image: ghcr.io/busybox:1.35
- name: busybox02
image: corp.img.io/busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: badpod03
namespace: imageregistries-ns02
spec:
initContainers:
- name: busybox01-init
image: corp.img.io/busybox:1.35
containers:
- name: busybox01
image: img.corp.com/busybox:1.35
- name: busybox02
image: docker.io/busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: badpod04
spec:
initContainers:
- name: busybox01-init
image: corp.img.io/busybox:1.35
containers:
- name: busybox01
image: ghcr.io/busybox:1.35
- name: busybox02
image: ghcr.io/busybox:1.35
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
namespace: imageregistries-ns01
spec:
initContainers:
- name: busybox01-init
image: img.corp.com/busybox:1.35
# - name: busybox02-init
# image: ghcr.io/busybox:1.35
containers:
# - name: busybox01
# image: ghcr.io/busybox:1.35
- name: busybox02
image: img.corp.com/busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod02
namespace: imageregistries-ns02
spec:
initContainers:
- name: busybox01-init
image: ghcr.io/busybox:1.35
containers:
- name: busybox01
image: docker.io/busybox:1.35
- name: busybox02
image: docker.io/busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod03
spec:
initContainers:
- name: busybox01-init
image: ghcr.io/busybox:1.35
containers:
- name: busybox01
image: ghcr.io/busybox:1.35
- name: busybox02
image: ghcr.io/busybox:1.35
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: baddeploy01
namespace: imageregistries-ns01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
initContainers:
- name: busybox01-init
image: docker.io/busybox:1.35
- name: busybox02-init
image: ghcr.io/busybox:1.35
containers:
- name: busybox01
image: ghcr.io/busybox:1.35
- name: busybox02
image: corp.img.io/busybox:1.35
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: badcronjob01
namespace: imageregistries-ns02
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
initContainers:
- name: busybox01-init
image: docker.io/busybox:1.35
- name: busybox02-init
image: ghcr.io/busybox:1.35
containers:
- name: busybox01
image: ghcr.io/busybox:1.35
- name: busybox02
image: corp.img.io/busybox:1.35
restartPolicy: OnFailure
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: gooddeploy01
namespace: imageregistries-ns01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
initContainers:
- name: busybox01-init
image: img.corp.com/busybox:1.35
- name: busybox02-init
image: ghcr.io/busybox:1.35
containers:
- name: busybox01
image: ghcr.io/busybox:1.35
- name: busybox02
image: img.corp.com/busybox:1.35
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: goodcronjob01
namespace: imageregistries-ns02
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
initContainers:
- name: busybox01-init
image: docker.io/busybox:1.35
- name: busybox02-init
image: ghcr.io/busybox:1.35
containers:
- name: busybox01
image: ghcr.io/busybox:1.35
- name: busybox02
image: docker.io/busybox:1.35
restartPolicy: OnFailure
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: advanced-restrict-image-registries
status:
ready: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: advanced-restrict-image-registries
annotations:
policies.kyverno.io/title: Advanced Restrict Image Registries in CEL expressions
policies.kyverno.io/category: Other in CEL
policies.kyverno.io/severity: medium
kyverno.io/kyverno-version: 1.11.0
policies.kyverno.io/minversion: 1.11.0
kyverno.io/kubernetes-version: "1.26-1.27"
policies.kyverno.io/subject: Pod
policies.kyverno.io/description: >-
In instances where a ClusterPolicy defines all the approved image registries
is insufficient, more granular control may be needed to set permitted registries,
especially in multi-tenant use cases where some registries may be based on
the Namespace. This policy shows an advanced version of the Restrict Image Registries
policy which gets a global approved registry from a ConfigMap and, based upon an
annotation at the Namespace level, gets the registry approved for that Namespace.
spec:
validationFailureAction: Audit
background: false
rules:
- name: validate-corp-registries
match:
any:
- resources:
kinds:
- Pod
validate:
cel:
paramKind:
apiVersion: v1
kind: ConfigMap
paramRef:
name: clusterregistries
namespace: default
parameterNotFoundAction: Deny
variables:
- name: allContainers
expression: "(object.spec.containers + (has(object.spec.initContainers) ? object.spec.initContainers : []) + (has(object.spec.ephemeralContainers) ? object.spec.ephemeralContainers : []))"
- name: nsregistries
expression: >-
(has(namespaceObject.metadata.annotations) && 'corp.com/allowed-registries' in namespaceObject.metadata.annotations) ?
namespaceObject.metadata.annotations['corp.com/allowed-registries'] : ' '
- name: clusterregistries
expression: "'registries' in params.data ? params.data['registries'] : ' '"
expressions:
- expression: "variables.allContainers.all(container, container.image.startsWith(variables.nsregistries) || container.image.startsWith(variables.clusterregistries))"
message: This Pod names an image that is not from an approved registry.

24 changes: 24 additions & 0 deletions other-cel/advanced-restrict-image-registries/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: advanced-restrict-image-registries-cel
version: 1.0.0
displayName: Advanced Restrict Image Registries in CEL expressions
description: >-
In instances where a ClusterPolicy defines all the approved image registries is insufficient, more granular control may be needed to set permitted registries, especially in multi-tenant use cases where some registries may be based on the Namespace. This policy shows an advanced version of the Restrict Image Registries policy which gets a global approved registry from a ConfigMap and, based upon an annotation at the Namespace level, gets the registry approved for that Namespace.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/other-cel/advanced-restrict-image-registries/advanced-restrict-image-registries.yaml
```
keywords:
- kyverno
- Other
- CEL Expressions
readme: |
In instances where a ClusterPolicy defines all the approved image registries is insufficient, more granular control may be needed to set permitted registries, especially in multi-tenant use cases where some registries may be based on the Namespace. This policy shows an advanced version of the Restrict Image Registries policy which gets a global approved registry from a ConfigMap and, based upon an annotation at the Namespace level, gets the registry approved for that Namespace.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "Other in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Pod"
digest: 53c660479027b9a7292871024ae9bcec7509e114553837b4027e29830d76ae04
createdAt: "2024-04-21T11:03:06Z"

Loading
Loading