Skip to content

Commit

Permalink
Add cel to fsgroup
Browse files Browse the repository at this point in the history
Signed-off-by: Rita Zhang <[email protected]>
  • Loading branch information
ritazh committed May 13, 2024
1 parent 823c3bb commit 3b3f636
Show file tree
Hide file tree
Showing 21 changed files with 670 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Deprecated

**This Policy is deprecated**

Please use the FSGroup settings on the users policy to enforce FSGroup Settings.

[Users Policy](../users)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 1.1.0
name: k8spspfsgroup
displayName: FS Group
createdAt: "2024-05-11T00:52:05Z"
description: Controls allocating an FSGroup that owns the Pod's volumes. Corresponds to the `fsGroup` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems
digest: f0713e26915694de463897cb6106668df57fabf174499aecfc87fa21567644aa
license: Apache-2.0
homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/fsgroup
keywords:
- gatekeeper
- open-policy-agent
- policies
readme: |-
# FS Group
Controls allocating an FSGroup that owns the Pod's volumes. Corresponds to the `fsGroup` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems
install: |-
### Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/fsgroup/1.1.0/template.yaml
```
provider:
name: Gatekeeper Library
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFSGroup
metadata:
name: psp-fsgroup
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
rule: "MayRunAs" #"MustRunAs" #"MayRunAs", "RunAsAny"
ranges:
- min: 1
max: 1000
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFSGroup
metadata:
name: psp-fsgroup
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
rule: "MustRunAs" #"MustRunAs" #"MayRunAs", "RunAsAny"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFSGroup
metadata:
name: psp-fsgroup
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
rule: "MayRunAs" #"MustRunAs" #"MayRunAs", "RunAsAny"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: v1
kind: Pod
metadata:
name: fsgroup-allowed
spec:
securityContext:
fsGroup: 500 # directory will have group ID 500
volumes:
- name: fsgroup-demo-vol
emptyDir: {}
containers:
- name: fsgroup-demo
image: busybox
command: ["sh", "-c", "sleep 1h"]
volumeMounts:
- name: fsgroup-demo-vol
mountPath: /data/demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: fsgroup-allowed
spec:
volumes:
- name: fsgroup-demo-vol
emptyDir: {}
containers:
- name: fsgroup-demo
image: busybox
command: ["sh", "-c", "sleep 1h"]
volumeMounts:
- name: fsgroup-demo-vol
mountPath: /data/demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: v1
kind: Pod
metadata:
name: fsgroup-disallowed
spec:
securityContext:
fsGroup: 2000 # directory will have group ID 2000
volumes:
- name: fsgroup-demo-vol
emptyDir: {}
containers:
- name: fsgroup-demo
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
volumeMounts:
- name: fsgroup-demo-vol
mountPath: /data/demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
kind: AdmissionReview
apiVersion: admission.k8s.io/v1beta1
request:
operation: "UPDATE"
object:
apiVersion: v1
kind: Pod
metadata:
name: fsgroup-disallowed
spec:
securityContext:
fsGroup: 2000 # directory will have group ID 2000
volumes:
- name: fsgroup-demo-vol
emptyDir: {}
containers:
- name: fsgroup-demo
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
volumeMounts:
- name: fsgroup-demo-vol
mountPath: /data/demo
41 changes: 41 additions & 0 deletions artifacthub/library/pod-security-policy/fsgroup/1.1.0/suite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
metadata:
name: fsgroup
tests:
- name: fsgroup
template: template.yaml
constraint: samples/psp-fsgroup/constraint.yaml
cases:
- name: example-disallowed
object: samples/psp-fsgroup/example_disallowed.yaml
assertions:
- violations: yes
- name: example-allowed
object: samples/psp-fsgroup/example_allowed.yaml
assertions:
- violations: no
- name: example-allowed2-nofsgroup
object: samples/psp-fsgroup/example_allowed2.yaml
assertions:
- violations: no
- name: update
object: samples/psp-fsgroup/update.yaml
assertions:
- violations: no
- name: fsgroup2
template: template.yaml
constraint: samples/psp-fsgroup/constraint2.yaml
cases:
- name: example-allowed-is-disallowed-constraint2
object: samples/psp-fsgroup/example_allowed2.yaml
assertions:
- violations: yes
- name: fsgroup3
template: template.yaml
constraint: samples/psp-fsgroup/constraint3.yaml
cases:
- name: example-allowed-constraint3
object: samples/psp-fsgroup/example_allowed2.yaml
assertions:
- violations: no
119 changes: 119 additions & 0 deletions artifacthub/library/pod-security-policy/fsgroup/1.1.0/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8spspfsgroup
annotations:
metadata.gatekeeper.sh/title: "FS Group"
metadata.gatekeeper.sh/version: 1.1.0
description: >-
Controls allocating an FSGroup that owns the Pod's volumes. Corresponds
to the `fsGroup` field in a PodSecurityPolicy. For more information, see
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems
spec:
crd:
spec:
names:
kind: K8sPSPFSGroup
validation:
# Schema for the `parameters` field
openAPIV3Schema:
type: object
description: >-
Controls allocating an FSGroup that owns the Pod's volumes. Corresponds
to the `fsGroup` field in a PodSecurityPolicy. For more information, see
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems
properties:
rule:
description: "An FSGroup rule name."
enum:
- MayRunAs
- MustRunAs
- RunAsAny
type: string
ranges:
type: array
description: "GID ranges affected by the rule."
items:
type: object
properties:
min:
description: "The minimum GID in the range, inclusive."
type: integer
max:
description: "The maximum GID in the range, inclusive."
type: integer
targets:
- target: admission.k8s.gatekeeper.sh
code:
- engine: K8sNativeValidation
source:
variables:
- name: fsGroup
expression: '!has(object.spec.securityContext) ? "" : !has(object.spec.securityContext.fsGroup) ? "" : object.spec.securityContext.fsGroup'
- name: input_fsGroup_allowed
expression: |
!has(variables.params.rule) ? true : variables.params.rule == "RunAsAny" ? true : variables.params.rule == "MayRunAs" && variables.fsGroup == "" ? true : (variables.params.rule == "MayRunAs" || variables.params.rule == "MustRunAs") && has(variables.params.ranges) && size(variables.params.ranges) > 0 ? variables.params.ranges.all(range, range.min <= variables.fsGroup && range.max >= variables.fsGroup) : false
validations:
- expression: '(has(request.operation) && request.operation == "UPDATE") || variables.input_fsGroup_allowed'
messageExpression: '"The provided pod spec fsGroup is not allowed, pod: " + object.metadata.name + ". Allowed fsGroup: " + variables.params.rule'
- engine: Rego
source:
rego: |
package k8spspfsgroup
import data.lib.exclude_update.is_update
violation[{"msg": msg, "details": {}}] {
# spec.securityContext.fsGroup field is immutable.
not is_update(input.review)
spec := input.review.object.spec
not input_fsGroup_allowed(spec)
msg := sprintf("The provided pod spec fsGroup is not allowed, pod: %v. Allowed fsGroup: %v", [input.review.object.metadata.name, input.parameters])
}
input_fsGroup_allowed(_) {
# RunAsAny - No range is required. Allows any fsGroup ID to be specified.
input.parameters.rule == "RunAsAny"
}
input_fsGroup_allowed(spec) {
# MustRunAs - Validates pod spec fsgroup against all ranges
input.parameters.rule == "MustRunAs"
fg := spec.securityContext.fsGroup
count(input.parameters.ranges) > 0
range := input.parameters.ranges[_]
value_within_range(range, fg)
}
input_fsGroup_allowed(spec) {
# MayRunAs - Validates pod spec fsgroup against all ranges or allow pod spec fsgroup to be left unset
input.parameters.rule == "MayRunAs"
not has_field(spec, "securityContext")
}
input_fsGroup_allowed(spec) {
# MayRunAs - Validates pod spec fsgroup against all ranges or allow pod spec fsgroup to be left unset
input.parameters.rule == "MayRunAs"
not spec.securityContext.fsGroup
}
input_fsGroup_allowed(spec) {
# MayRunAs - Validates pod spec fsgroup against all ranges or allow pod spec fsgroup to be left unset
input.parameters.rule == "MayRunAs"
fg := spec.securityContext.fsGroup
count(input.parameters.ranges) > 0
range := input.parameters.ranges[_]
value_within_range(range, fg)
}
value_within_range(range, value) {
range.min <= value
range.max >= value
}
# has_field returns whether an object has a field
has_field(object, field) = true {
object[field]
}
libs:
- |
package lib.exclude_update
is_update(review) {
review.operation == "UPDATE"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFSGroup
metadata:
name: psp-fsgroup
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
rule: "MustRunAs" #"MustRunAs" #"MayRunAs", "RunAsAny"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFSGroup
metadata:
name: psp-fsgroup
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
rule: "MayRunAs" #"MustRunAs" #"MayRunAs", "RunAsAny"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v1
kind: Pod
metadata:
name: fsgroup-disallowed
name: fsgroup-allowed
spec:
securityContext:
fsGroup: 500 # directory will have group ID 500
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: fsgroup-allowed
spec:
volumes:
- name: fsgroup-demo-vol
emptyDir: {}
containers:
- name: fsgroup-demo
image: busybox
command: ["sh", "-c", "sleep 1h"]
volumeMounts:
- name: fsgroup-demo-vol
mountPath: /data/demo
20 changes: 20 additions & 0 deletions library/pod-security-policy/fsgroup/suite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,27 @@ tests:
object: samples/psp-fsgroup/example_allowed.yaml
assertions:
- violations: no
- name: example-allowed2-nofsgroup
object: samples/psp-fsgroup/example_allowed2.yaml
assertions:
- violations: no
- name: update
object: samples/psp-fsgroup/update.yaml
assertions:
- violations: no
- name: fsgroup2
template: template.yaml
constraint: samples/psp-fsgroup/constraint2.yaml
cases:
- name: example-allowed-is-disallowed-constraint2
object: samples/psp-fsgroup/example_allowed2.yaml
assertions:
- violations: yes
- name: fsgroup3
template: template.yaml
constraint: samples/psp-fsgroup/constraint3.yaml
cases:
- name: example-allowed-constraint3
object: samples/psp-fsgroup/example_allowed2.yaml
assertions:
- violations: no
Loading

0 comments on commit 3b3f636

Please sign in to comment.