-
Notifications
You must be signed in to change notification settings - Fork 321
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
maxsmythe
wants to merge
4
commits into
open-policy-agent:master
Choose a base branch
from
maxsmythe:cel-flexvolumes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
artifacthub/library/pod-security-policy/flexvolume-drivers/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: 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 |
2 changes: 2 additions & 0 deletions
2
artifacthub/library/pod-security-policy/flexvolume-drivers/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 |
13 changes: 13 additions & 0 deletions
13
...d-security-policy/flexvolume-drivers/1.1.0/samples/psp-flexvolume-drivers/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,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" |
18 changes: 18 additions & 0 deletions
18
...urity-policy/flexvolume-drivers/1.1.0/samples/psp-flexvolume-drivers/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,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" |
18 changes: 18 additions & 0 deletions
18
...ty-policy/flexvolume-drivers/1.1.0/samples/psp-flexvolume-drivers/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,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" |
23 changes: 23 additions & 0 deletions
23
...y/pod-security-policy/flexvolume-drivers/1.1.0/samples/psp-flexvolume-drivers/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,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" |
21 changes: 21 additions & 0 deletions
21
artifacthub/library/pod-security-policy/flexvolume-drivers/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,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 |
94 changes: 94 additions & 0 deletions
94
artifacthub/library/pod-security-policy/flexvolume-drivers/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,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" | ||
} |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
|
@@ -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) | ||||||
- name: badFlexVolumeNames | ||||||
expression: | | ||||||
variables.flexVolumes.map(volume, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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" | ||||||
} |
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
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 @@ | ||
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(", ") + "]" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid null pointers