Created
April 7, 2021 01:19
-
-
Save rdemoraes/de174794d747f1d8ce3d210477a0896b to your computer and use it in GitHub Desktop.
OPA ConstraintTemplate
This file contains hidden or 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
| apiVersion: templates.gatekeeper.sh/v1beta1 | |
| kind: ConstraintTemplate | |
| metadata: | |
| name: k8swhitelistedimages | |
| spec: | |
| crd: | |
| spec: | |
| names: | |
| kind: k8sWhitelistedImages | |
| validation: | |
| # Schema for the `parameters` field | |
| openAPIV3Schema: | |
| properties: | |
| images: | |
| type: array | |
| items: string | |
| targets: | |
| - target: admission.k8s.gatekeeper.sh | |
| rego: | | |
| package k8swhitelistedimages | |
| whitelisted_images = {images | | |
| images = input.parameters.images[_] | |
| } | |
| images_whitelisted(str, patterns) { | |
| image_matches(str, patterns[_]) | |
| } | |
| image_matches(str, pattern) { | |
| contains(str, pattern) | |
| } | |
| violation[{"msg": msg}] { | |
| input.review.object | |
| image := input.review.object.spec.containers[_].image | |
| name := input.review.object.metadata.name | |
| not images_whitelisted(image, whitelisted_images) | |
| msg := sprintf("pod %q has invalid image %q. Please, contact your DevOps. Follow the whitelisted images %v", [name, image, whitelisted_images]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment