-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adding cel for psp-root-filesystem (#542)
* chore: adding cel for psp-root-filesystem Signed-off-by: Jaydip Gabani <[email protected]> * correcting variable name Signed-off-by: Jaydip Gabani <[email protected]> --------- Signed-off-by: Jaydip Gabani <[email protected]> Co-authored-by: Rita Zhang <[email protected]>
- Loading branch information
1 parent
4c3567d
commit 71eacda
Showing
14 changed files
with
495 additions
and
128 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/artifacthub-pkg.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: 1.1.0 | ||
name: k8spspreadonlyrootfilesystem | ||
displayName: Read Only Root Filesystem | ||
createdAt: "2024-06-03T22:57:22Z" | ||
description: Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems | ||
digest: a7501371f8cd7769d36035a8a0369731ff846844ad3ed76ef6ec00ef19b7767c | ||
license: Apache-2.0 | ||
homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/read-only-root-filesystem | ||
keywords: | ||
- gatekeeper | ||
- open-policy-agent | ||
- policies | ||
readme: |- | ||
# Read Only Root Filesystem | ||
Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` 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/read-only-root-filesystem/1.1.0/template.yaml | ||
``` | ||
provider: | ||
name: Gatekeeper Library |
2 changes: 2 additions & 0 deletions
2
artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
resources: | ||
- template.yaml |
9 changes: 9 additions & 0 deletions
9
...policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/constraint.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: constraints.gatekeeper.sh/v1beta1 | ||
kind: K8sPSPReadOnlyRootFilesystem | ||
metadata: | ||
name: psp-readonlyrootfilesystem | ||
spec: | ||
match: | ||
kinds: | ||
- apiGroups: [""] | ||
kinds: ["Pod"] |
12 changes: 12 additions & 0 deletions
12
...d-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx-readonlyrootfilesystem-disallowed | ||
labels: | ||
app: nginx-readonlyrootfilesystem | ||
spec: | ||
ephemeralContainers: | ||
- name: nginx | ||
image: nginx | ||
securityContext: | ||
readOnlyRootFilesystem: false |
12 changes: 12 additions & 0 deletions
12
...y/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_allowed.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx-readonlyrootfilesystem-allowed | ||
labels: | ||
app: nginx-readonlyrootfilesystem | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
securityContext: | ||
readOnlyRootFilesystem: true |
12 changes: 12 additions & 0 deletions
12
...ead-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_disallowed.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx-readonlyrootfilesystem-disallowed | ||
labels: | ||
app: nginx-readonlyrootfilesystem | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
securityContext: | ||
readOnlyRootFilesystem: false |
17 changes: 17 additions & 0 deletions
17
...ity-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/update.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
kind: AdmissionReview | ||
apiVersion: admission.k8s.io/v1beta1 | ||
request: | ||
operation: "UPDATE" | ||
object: | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx-readonlyrootfilesystem-disallowed | ||
labels: | ||
app: nginx-readonlyrootfilesystem | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
securityContext: | ||
readOnlyRootFilesystem: false |
27 changes: 27 additions & 0 deletions
27
artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/suite.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
kind: Suite | ||
apiVersion: test.gatekeeper.sh/v1alpha1 | ||
metadata: | ||
name: read-only-root-filesystem | ||
tests: | ||
- name: require-read-only-root-filesystem | ||
template: template.yaml | ||
constraint: samples/psp-readonlyrootfilesystem/constraint.yaml | ||
cases: | ||
- name: example-disallowed | ||
object: samples/psp-readonlyrootfilesystem/example_disallowed.yaml | ||
assertions: | ||
- violations: yes | ||
message: "only read-only root filesystem container is allowed: nginx" | ||
- name: example-allowed | ||
object: samples/psp-readonlyrootfilesystem/example_allowed.yaml | ||
assertions: | ||
- violations: no | ||
- name: disallowed-ephemeral | ||
object: samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml | ||
assertions: | ||
- violations: yes | ||
message: "only read-only root filesystem container is allowed: nginx" | ||
- name: update | ||
object: samples/psp-readonlyrootfilesystem/update.yaml | ||
assertions: | ||
- violations: no |
140 changes: 140 additions & 0 deletions
140
artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/template.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
apiVersion: templates.gatekeeper.sh/v1 | ||
kind: ConstraintTemplate | ||
metadata: | ||
name: k8spspreadonlyrootfilesystem | ||
annotations: | ||
metadata.gatekeeper.sh/title: "Read Only Root Filesystem" | ||
metadata.gatekeeper.sh/version: 1.1.0 | ||
description: >- | ||
Requires the use of a read-only root file system by pod containers. | ||
Corresponds to the `readOnlyRootFilesystem` 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: K8sPSPReadOnlyRootFilesystem | ||
validation: | ||
# Schema for the `parameters` field | ||
openAPIV3Schema: | ||
type: object | ||
description: >- | ||
Requires the use of a read-only root file system by pod containers. | ||
Corresponds to the `readOnlyRootFilesystem` field in a | ||
PodSecurityPolicy. For more information, see | ||
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems | ||
properties: | ||
exemptImages: | ||
description: >- | ||
Any container that uses an image that matches an entry in this list will be excluded | ||
from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`. | ||
It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name) | ||
in order to avoid unexpectedly exempting images from an untrusted repository. | ||
type: array | ||
items: | ||
type: string | ||
targets: | ||
- target: admission.k8s.gatekeeper.sh | ||
code: | ||
- engine: K8sNativeValidation | ||
source: | ||
variables: | ||
- name: containers | ||
expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' | ||
- name: initContainers | ||
expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' | ||
- name: ephemeralContainers | ||
expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' | ||
- name: exemptImagePrefixes | ||
expression: | | ||
!has(variables.params.exemptImages) ? [] : | ||
variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) | ||
- name: exemptImageExplicit | ||
expression: | | ||
!has(variables.params.exemptImages) ? [] : | ||
variables.params.exemptImages.filter(image, !image.endsWith("*")) | ||
- name: exemptImages | ||
expression: | | ||
(variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, | ||
container.image in variables.exemptImageExplicit || | ||
variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) | ||
- name: badContainers | ||
expression: | | ||
(variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, | ||
!(container.image in variables.exemptImages) && | ||
(!has(container.securityContext) || | ||
!has(container.securityContext.readOnlyRootFilesystem) || | ||
container.securityContext.readOnlyRootFilesystem != true) | ||
).map(container, container.name) | ||
validations: | ||
- expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' | ||
messageExpression: '"only read-only root filesystem container is allowed: " + variables.badContainers.join(", ")' | ||
|
||
- engine: Rego | ||
source: | ||
rego: | | ||
package k8spspreadonlyrootfilesystem | ||
import data.lib.exclude_update.is_update | ||
import data.lib.exempt_container.is_exempt | ||
violation[{"msg": msg, "details": {}}] { | ||
# spec.containers.readOnlyRootFilesystem field is immutable. | ||
not is_update(input.review) | ||
c := input_containers[_] | ||
not is_exempt(c) | ||
input_read_only_root_fs(c) | ||
msg := sprintf("only read-only root filesystem container is allowed: %v", [c.name]) | ||
} | ||
input_read_only_root_fs(c) { | ||
not has_field(c, "securityContext") | ||
} | ||
input_read_only_root_fs(c) { | ||
not c.securityContext.readOnlyRootFilesystem == true | ||
} | ||
input_containers[c] { | ||
c := input.review.object.spec.containers[_] | ||
} | ||
input_containers[c] { | ||
c := input.review.object.spec.initContainers[_] | ||
} | ||
input_containers[c] { | ||
c := input.review.object.spec.ephemeralContainers[_] | ||
} | ||
# 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" | ||
} | ||
- | | ||
package lib.exempt_container | ||
is_exempt(container) { | ||
exempt_images := object.get(object.get(input, "parameters", {}), "exemptImages", []) | ||
img := container.image | ||
exemption := exempt_images[_] | ||
_matches_exemption(img, exemption) | ||
} | ||
_matches_exemption(img, exemption) { | ||
not endswith(exemption, "*") | ||
exemption == img | ||
} | ||
_matches_exemption(img, exemption) { | ||
endswith(exemption, "*") | ||
prefix := trim_suffix(exemption, "*") | ||
startswith(img, prefix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.