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

Add VAP-CEL to K8sPSPFlexVolumes #536

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 1.1.0
name: k8spspflexvolumes
displayName: FlexVolumes
createdAt: "2024-05-29T23:34:09Z"
description: Controls the allowlist of FlexVolume drivers. Corresponds to the `allowedFlexVolumes` field in PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#flexvolume-drivers
digest: d76c22a6c2f63b7421fe7b14da4668dbc27d93a0337884ddad79492802ce4e0f
license: Apache-2.0
homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/flexvolume-drivers
keywords:
- gatekeeper
- open-policy-agent
- policies
readme: |-
# FlexVolumes
Controls the allowlist of FlexVolume drivers. Corresponds to the `allowedFlexVolumes` field in PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#flexvolume-drivers
install: |-
### Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/flexvolume-drivers/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,13 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFlexVolumes
metadata:
name: psp-flexvolume-drivers
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
allowedFlexVolumes: #[]
- driver: "example/lvm"
- driver: "example/cifs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx-flexvolume-driver-allowed
labels:
app: nginx-flexvolume-driver
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: /test
name: test-volume
readOnly: true
volumes:
- name: test-volume
flexVolume:
driver: "example/lvm"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx-flexvolume-driver-disallowed
labels:
app: nginx-flexvolume-driver
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: /test
name: test-volume
readOnly: true
volumes:
- name: test-volume
flexVolume:
driver: "example/testdriver" #"example/lvm"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
kind: AdmissionReview
apiVersion: admission.k8s.io/v1beta1
request:
operation: "UPDATE"
object:
apiVersion: v1
kind: Pod
metadata:
name: nginx-flexvolume-driver-disallowed
labels:
app: nginx-flexvolume-driver
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: /test
name: test-volume
readOnly: true
volumes:
- name: test-volume
flexVolume:
driver: "example/testdriver" #"example/lvm"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
metadata:
name: flexvolume-drivers
tests:
- name: flexvolume-drivers
template: template.yaml
constraint: samples/psp-flexvolume-drivers/constraint.yaml
cases:
- name: example-allowed
object: samples/psp-flexvolume-drivers/example_allowed.yaml
assertions:
- violations: no
- name: example-disallowed
object: samples/psp-flexvolume-drivers/example_disallowed.yaml
assertions:
- violations: yes
- name: update
object: samples/psp-flexvolume-drivers/update.yaml
assertions:
- violations: no
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8spspflexvolumes
annotations:
metadata.gatekeeper.sh/title: "FlexVolumes"
metadata.gatekeeper.sh/version: 1.1.0
description: >-
Controls the allowlist of FlexVolume drivers. Corresponds to the
`allowedFlexVolumes` field in PodSecurityPolicy. For more information,
see
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#flexvolume-drivers
spec:
crd:
spec:
names:
kind: K8sPSPFlexVolumes
validation:
# Schema for the `parameters` field
openAPIV3Schema:
type: object
description: >-
Controls the allowlist of FlexVolume drivers. Corresponds to the
`allowedFlexVolumes` field in PodSecurityPolicy. For more information,
see
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#flexvolume-drivers
properties:
allowedFlexVolumes:
type: array
description: "An array of AllowedFlexVolume objects."
items:
type: object
properties:
driver:
description: "The name of the FlexVolume driver."
type: string
targets:
- target: admission.k8s.gatekeeper.sh
code:
- engine: K8sNativeValidation
source:
variables:
- name: flexVolumes
expression: |
!has(variables.anyObject.spec.volumes) ? [] :
variables.anyObject.spec.volumes.filter(volume, has(volume.flexVolume))
- name: allowedFlexVolumeDrivers
expression: variables.params.allowedFlexVolumes.map(volume, volume.driver)
- name: badFlexVolumeNames
expression: |
variables.flexVolumes.map(volume,
!has(volume.flexVolume.driver) || !(volume.flexVolume.driver in variables.allowedFlexVolumeDrivers),
volume.name
)
validations:
- expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badFlexVolumeNames) == 0'
messageExpression: |
"FlexVolumes [" + variables.badFlexVolumeNames.join(", ") + "] not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed drivers: [" + variables.allowedFlexVolumeDrivers.join(", ") + "]"
- engine: Rego
source:
rego: |
package k8spspflexvolumes

import data.lib.exclude_update.is_update

violation[{"msg": msg, "details": {}}] {
# spec.volumes field is immutable.
not is_update(input.review)

volume := input_flexvolumes[_]
not input_flexvolumes_allowed(volume)
msg := sprintf("FlexVolume %v is not allowed, pod: %v. Allowed drivers: %v", [volume, input.review.object.metadata.name, input.parameters.allowedFlexVolumes])
}

input_flexvolumes_allowed(volume) {
input.parameters.allowedFlexVolumes[_].driver == volume.flexVolume.driver
}

input_flexvolumes[v] {
v := input.review.object.spec.volumes[_]
has_field(v, "flexVolume")
}

# 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"
}
78 changes: 50 additions & 28 deletions library/pod-security-policy/flexvolume-drivers/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: k8spspflexvolumes
annotations:
metadata.gatekeeper.sh/title: "FlexVolumes"
metadata.gatekeeper.sh/version: 1.0.1
metadata.gatekeeper.sh/version: 1.1.0
description: >-
Controls the allowlist of FlexVolume drivers. Corresponds to the
`allowedFlexVolumes` field in PodSecurityPolicy. For more information,
Expand Down Expand Up @@ -36,37 +36,59 @@ spec:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8spspflexvolumes
code:
- engine: K8sNativeValidation
source:
variables:
- name: flexVolumes
expression: |
!has(variables.anyObject.spec.volumes) ? [] :
variables.anyObject.spec.volumes.filter(volume, has(volume.flexVolume))
- name: allowedFlexVolumeDrivers
expression: variables.params.allowedFlexVolumes.map(volume, volume.driver)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expression: variables.params.allowedFlexVolumes.map(volume, volume.driver)
expression: !has(variables.params.allowedFlexVolumes) ? [] : variables.params.allowedFlexVolumes.map(volume, volume.driver)

to avoid null pointers

- name: badFlexVolumeNames
expression: |
variables.flexVolumes.map(volume,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
variables.flexVolumes.map(volume,
!has(variables.flexVolumes) ? [] : variables.flexVolumes.map(volume,

to avoid null pointers

!has(volume.flexVolume.driver) || !(volume.flexVolume.driver in variables.allowedFlexVolumeDrivers),
volume.name
)
validations:
- expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badFlexVolumeNames) == 0'
messageExpression: |
"FlexVolumes [" + variables.badFlexVolumeNames.join(", ") + "] not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed drivers: [" + variables.allowedFlexVolumeDrivers.join(", ") + "]"
- engine: Rego
source:
rego: |
package k8spspflexvolumes

import data.lib.exclude_update.is_update
import data.lib.exclude_update.is_update

violation[{"msg": msg, "details": {}}] {
# spec.volumes field is immutable.
not is_update(input.review)
violation[{"msg": msg, "details": {}}] {
# spec.volumes field is immutable.
not is_update(input.review)

volume := input_flexvolumes[_]
not input_flexvolumes_allowed(volume)
msg := sprintf("FlexVolume %v is not allowed, pod: %v. Allowed drivers: %v", [volume, input.review.object.metadata.name, input.parameters.allowedFlexVolumes])
}
volume := input_flexvolumes[_]
not input_flexvolumes_allowed(volume)
msg := sprintf("FlexVolume %v is not allowed, pod: %v. Allowed drivers: %v", [volume, input.review.object.metadata.name, input.parameters.allowedFlexVolumes])
}

input_flexvolumes_allowed(volume) {
input.parameters.allowedFlexVolumes[_].driver == volume.flexVolume.driver
}
input_flexvolumes_allowed(volume) {
input.parameters.allowedFlexVolumes[_].driver == volume.flexVolume.driver
}

input_flexvolumes[v] {
v := input.review.object.spec.volumes[_]
has_field(v, "flexVolume")
}
input_flexvolumes[v] {
v := input.review.object.spec.volumes[_]
has_field(v, "flexVolume")
}

# has_field returns whether an object has a field
has_field(object, field) = true {
object[field]
}
libs:
- |
package lib.exclude_update
# 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"
}
is_update(review) {
review.operation == "UPDATE"
}
18 changes: 12 additions & 6 deletions src/pod-security-policy/flexvolume-drivers/constraint.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: k8spspflexvolumes
annotations:
metadata.gatekeeper.sh/title: "FlexVolumes"
metadata.gatekeeper.sh/version: 1.0.1
metadata.gatekeeper.sh/version: 1.1.0
description: >-
Controls the allowlist of FlexVolume drivers. Corresponds to the
`allowedFlexVolumes` field in PodSecurityPolicy. For more information,
Expand Down Expand Up @@ -36,8 +36,14 @@ spec:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
{{ file.Read "src/pod-security-policy/flexvolume-drivers/src.rego" | strings.Indent 8 | strings.TrimSuffix "\n" }}
libs:
- |
{{ file.Read "src/pod-security-policy/flexvolume-drivers/lib_exclude_update.rego" | strings.Indent 10 | strings.TrimSuffix "\n" }}
code:
- engine: K8sNativeValidation
source:
{{ file.Read "src/pod-security-policy/flexvolume-drivers/src.cel" | strings.Indent 10 | strings.TrimSuffix "\n" }}
- engine: Rego
source:
rego: |
{{ file.Read "src/pod-security-policy/flexvolume-drivers/src.rego" | strings.Indent 12 | strings.TrimSuffix "\n" }}
libs:
- |
{{ file.Read "src/pod-security-policy/flexvolume-drivers/lib_exclude_update.rego" | strings.Indent 12 | strings.TrimSuffix "\n" }}
17 changes: 17 additions & 0 deletions src/pod-security-policy/flexvolume-drivers/src.cel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variables:
- name: flexVolumes
expression: |
!has(variables.anyObject.spec.volumes) ? [] :
variables.anyObject.spec.volumes.filter(volume, has(volume.flexVolume))
- name: allowedFlexVolumeDrivers
expression: variables.params.allowedFlexVolumes.map(volume, volume.driver)
- name: badFlexVolumeNames
expression: |
variables.flexVolumes.map(volume,
!has(volume.flexVolume.driver) || !(volume.flexVolume.driver in variables.allowedFlexVolumeDrivers),
volume.name
)
validations:
- expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badFlexVolumeNames) == 0'
messageExpression: |
"FlexVolumes [" + variables.badFlexVolumeNames.join(", ") + "] not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed drivers: [" + variables.allowedFlexVolumeDrivers.join(", ") + "]"
Loading
Loading