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 miscellaneous policies in CEL expressions - Part 4 #1033

Merged
merged 30 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
89818a4
copy check-supplemental-groups
Chandan-DK May 23, 2024
e67976e
convert check-supplemental-groups
Chandan-DK May 23, 2024
b228744
copy restrict-adding-capabilities
Chandan-DK May 23, 2024
ba3302e
convert restrict-adding-capabilities
Chandan-DK May 23, 2024
bfdac7b
copy restrict-runtimeClassName
Chandan-DK May 23, 2024
4417f30
convert restrict-runtimeClassName
Chandan-DK May 23, 2024
e9f2ca3
copy block-velero-restore
Chandan-DK May 23, 2024
60ae20e
convert block-velero-restore
Chandan-DK May 23, 2024
5f31c73
convert block-velero-restore
Chandan-DK May 23, 2024
84694b8
copy validate-cron-schedule
Chandan-DK May 23, 2024
fb42b9a
convert validate-cron-schedule
Chandan-DK May 23, 2024
b561189
copy block-tekton-task-runs
Chandan-DK May 23, 2024
d19aa20
add kyverno tests for block-tekton-task-runs
Chandan-DK May 23, 2024
4c1938f
remove unused resources
Chandan-DK May 23, 2024
3b74aab
convert block-tekton-task-runs
Chandan-DK May 23, 2024
497af0e
copy require-tekton-bundle
Chandan-DK May 24, 2024
0adec41
add kyverno tests for require-tekton-bundle
Chandan-DK May 24, 2024
cbbfad7
convert require-tekton-bundle
Chandan-DK May 24, 2024
23a7e26
copy require-tekton-namespace-pipelinerun
Chandan-DK May 24, 2024
7eae74e
add kyverno tests for require-tekton-namespace-pipelinerun
Chandan-DK May 24, 2024
13ff7d8
convert require-tekton-namespace-pipelinerun
Chandan-DK May 24, 2024
c54db64
rename files for clarity
Chandan-DK May 24, 2024
fa4ab55
add CI tests for cel folders
Chandan-DK May 24, 2024
3efbff1
remove require-tekton-namespace-pipelinerun
Chandan-DK May 26, 2024
95d1873
Merge branch 'main' into miscellaneous-policies-cel-part-4
Chandan-DK Jul 9, 2024
41280a5
explicitly specify CREATE and UPDATE operations
Chandan-DK Jul 9, 2024
4afca52
Merge branch 'main' into miscellaneous-policies-cel-part-4
Chandan-DK Jul 15, 2024
e662208
Merge branch 'main' into miscellaneous-policies-cel-part-4
Chandan-DK Jul 31, 2024
5422b69
Merge branch 'main' into miscellaneous-policies-cel-part-4
chipzoller Aug 2, 2024
b96ed31
Merge branch 'main' into miscellaneous-policies-cel-part-4
chipzoller Aug 2, 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
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ jobs:
- ^pod-security$
- ^psa$
- ^psp-migration$
- ^psp-migration-cel$
- ^tekton$
- ^tekton-cel$
- ^traefik$
- ^velero$
- ^velero-cel$
runs-on: ubuntu-latest
name: ${{ matrix.k8s-version.name }} - ${{ matrix.tests }}
steps:
Expand All @@ -72,4 +75,4 @@ jobs:
- name: Run Tests
uses: ./.github/actions/run-tests
with:
tests: ${{ matrix.tests }}
tests: ${{ matrix.tests }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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: check-supplemental-groups
spec:
steps:
- name: step-01
try:
- apply:
file: ../check-supplemental-groups.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: psp-check-supplemental-groups
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
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,55 @@
apiVersion: v1
kind: Pod
metadata:
name: badpod01
spec:
securityContext:
supplementalGroups:
- 120
- 230
- 550
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: badpod02
spec:
securityContext:
supplementalGroups:
- 1000
- 120
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: badpod03
spec:
securityContext:
runAsGroup: 0
supplementalGroups:
- 580
- 0
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: badpod04
spec:
securityContext:
supplementalGroups:
- 100
- 601
- 600
runAsGroup: 0
containers:
- name: busybox01
image: busybox:1.35
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
spec:
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod02
spec:
securityContext:
supplementalGroups:
- 150
- 100
- 500
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod03
spec:
securityContext:
supplementalGroups:
- 550
- 600
- 120
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod04
spec:
securityContext:
runAsGroup: 0
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod05
spec:
securityContext:
supplementalGroups:
- 600
runAsGroup: 0
containers:
- name: busybox01
image: busybox:1.35
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: baddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
securityContext:
supplementalGroups:
- 100
- 601
- 600
runAsGroup: 0
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: badcronjob01
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
securityContext:
supplementalGroups:
- 1000
- 120
containers:
- name: busybox01
image: busybox:1.35
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: gooddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
securityContext:
supplementalGroups:
- 150
- 100
- 500
containers:
- name: busybox01
image: busybox:1.35
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: goodcronjob01
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
securityContext:
supplementalGroups:
- 550
- 600
- 120
containers:
- name: busybox01
image: busybox:1.35
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: psp-check-supplemental-groups
status:
ready: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: psp-check-supplemental-groups
policies:
- ../check-supplemental-groups.yaml
resources:
- resource.yaml
results:
- kind: Pod
policy: psp-check-supplemental-groups
resources:
- badpod01
result: fail
rule: supplementalgroup-ranges
- kind: Pod
policy: psp-check-supplemental-groups
resources:
- goodpod01
result: pass
rule: supplementalgroup-ranges
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
apiVersion: v1
kind: Pod
metadata:
name: badpod01
spec:
containers:
- name: container01
image: dummyimagename
securityContext:
supplementalGroups:
- 0
---
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
spec:
containers:
- name: container01
image: dummyimagename
securityContext:
supplementalGroups:
- 100
23 changes: 23 additions & 0 deletions psp-migration-cel/check-supplemental-groups/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: check-supplemental-groups-cel
version: 1.0.0
displayName: Check supplementalGroups in CEL expressions
description: >-
Supplemental groups control which group IDs containers add and can coincide with restricted groups on the host. Pod Security Policies (PSP) allowed a range of these group IDs to be specified which were allowed. This policy ensures any Pod may only specify supplementalGroup IDs between 100-200 or 500-600.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/psp-migration-cel/check-supplemental-groups/check-supplemental-groups.yaml
```
keywords:
- kyverno
- PSP Migration
- CEL Expressions
readme: |
Supplemental groups control which group IDs containers add and can coincide with restricted groups on the host. Pod Security Policies (PSP) allowed a range of these group IDs to be specified which were allowed. This policy ensures any Pod may only specify supplementalGroup IDs between 100-200 or 500-600.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "PSP Migration in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Pod"
digest: 05135ed92926031b15d782552af3f8dbf8776014401328e186987344079fcc66
createdAt: "2024-05-23T13:57:56Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: psp-check-supplemental-groups
annotations:
policies.kyverno.io/title: Check supplementalGroups in CEL expressions
policies.kyverno.io/category: PSP Migration 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: >-
Supplemental groups control which group IDs containers add and can coincide with
restricted groups on the host. Pod Security Policies (PSP) allowed a range of
these group IDs to be specified which were allowed. This policy ensures any Pod
may only specify supplementalGroup IDs between 100-200 or 500-600.
spec:
background: false
validationFailureAction: Audit
rules:
- name: supplementalgroup-ranges
match:
any:
- resources:
kinds:
- Pod
operations:
- CREATE
- UPDATE
validate:
cel:
expressions:
- expression: >-
!has(object.spec.securityContext) ||
!has(object.spec.securityContext.supplementalGroups) ||
object.spec.securityContext.supplementalGroups.all(supplementalGroup, (supplementalGroup >= 100 && supplementalGroup <= 200) || (supplementalGroup >= 500 && supplementalGroup <= 600))
message: Any supplementalGroup ID must be within the range 100-200 or 500-600.

Loading
Loading