diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.2/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a2a502b16f70b016cc22e7992ad5dfed7df83d35 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.2/template.yaml @@ -0,0 +1,144 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspselinuxv2 + annotations: + metadata.gatekeeper.sh/title: "SELinux V2" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Defines an allow-list of seLinuxOptions configurations for pod + containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. + For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux +spec: + crd: + spec: + names: + kind: K8sPSPSELinuxV2 + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Defines an allow-list of seLinuxOptions configurations for pod + containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. + For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux + 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 + allowedSELinuxOptions: + type: array + description: "An allow-list of SELinux options configurations." + items: + type: object + description: "An allowed configuration of SELinux options for a pod container." + properties: + level: + type: string + description: "An SELinux level." + role: + type: string + description: "An SELinux role." + type: + type: string + description: "An SELinux type." + user: + type: string + description: "An SELinux user." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspselinux + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + # Disallow top level custom SELinux options + violation[{"msg": msg, "details": {}}] { + # spec.securityContext.seLinuxOptions field is immutable. + not is_update(input.review) + + has_field(input.review.object.spec.securityContext, "seLinuxOptions") + not input_seLinuxOptions_allowed(input.review.object.spec.securityContext.seLinuxOptions) + msg := sprintf("SELinux options is not allowed, pod: %v. Allowed options: %v", [input.review.object.metadata.name, input.parameters.allowedSELinuxOptions]) + } + # Disallow container level custom SELinux options + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.seLinuxOptions field is immutable. + not is_update(input.review) + + c := input_security_context[_] + not is_exempt(c) + has_field(c.securityContext, "seLinuxOptions") + not input_seLinuxOptions_allowed(c.securityContext.seLinuxOptions) + msg := sprintf("SELinux options is not allowed, pod: %v, container %v. Allowed options: %v", [input.review.object.metadata.name, c.name, input.parameters.allowedSELinuxOptions]) + } + + input_seLinuxOptions_allowed(options) { + params := input.parameters.allowedSELinuxOptions[_] + field_allowed("level", options, params) + field_allowed("role", options, params) + field_allowed("type", options, params) + field_allowed("user", options, params) + } + + field_allowed(field, options, params) { + params[field] == options[field] + } + field_allowed(field, options, _) { + not has_field(options, field) + } + + input_security_context[c] { + c := input.review.object.spec.containers[_] + has_field(c.securityContext, "seLinuxOptions") + } + input_security_context[c] { + c := input.review.object.spec.initContainers[_] + has_field(c.securityContext, "seLinuxOptions") + } + input_security_context[c] { + c := input.review.object.spec.ephemeralContainers[_] + has_field(c.securityContext, "seLinuxOptions") + } + + # 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..c6e8437d65327915f31bacc16cf9ceddbebebb69 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.3 +name: k8spspselinuxv2 +displayName: SELinux V2 +createdAt: "2024-03-20T21:15:11Z" +description: Defines an allow-list of seLinuxOptions configurations for pod containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux +digest: f66b071de52853a43ef0bcfda6fd88db1ed6d24e0c9536dd92ee92df6921e9c7 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/selinux +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # SELinux V2 + Defines an allow-list of seLinuxOptions configurations for pod containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/selinux/1.0.3/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f88bbcd69a705b0db24b292accda70796dd1af1e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSELinuxV2 +metadata: + name: psp-selinux-v2 +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + allowedSELinuxOptions: + - level: s0:c123,c456 + role: object_r + type: svirt_sandbox_file_t + user: system_u diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a35fc737fe71150a2bcad733a97ccc85ed41fff --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/disallowed_ephemeral.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-selinux-disallowed + labels: + app: nginx-selinux +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + level: s1:c234,c567 + user: sysadm_u + role: sysadm_r + type: svirt_lxc_net_t diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4eaf2dc929cf706cbc98d5ea09856d627e582263 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/example_allowed.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-selinux-allowed + labels: + app: nginx-selinux +spec: + containers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + level: s0:c123,c456 + role: object_r + type: svirt_sandbox_file_t + user: system_u diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7eb7fee115ec55bcd1b5978e44362378871b661d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/example_disallowed.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-selinux-disallowed + labels: + app: nginx-selinux +spec: + containers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + level: s1:c234,c567 + user: sysadm_u + role: sysadm_r + type: svirt_lxc_net_t diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..581419e9df5d426485e489a6b755591b8d545e92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/samples/psp-selinux-v2/update.yaml @@ -0,0 +1,21 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-selinux-disallowed + labels: + app: nginx-selinux + spec: + containers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + level: s1:c234,c567 + user: sysadm_u + role: sysadm_r + type: svirt_lxc_net_t diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1bbaf360ede8fd308d913c96d38117f6c6069d2a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: selinux +tests: +- name: require-matching-selinux-options + template: template.yaml + constraint: samples/psp-selinux-v2/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-selinux-v2/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-selinux-v2/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-selinux-v2/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-selinux-v2/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6ecc70675e6e7f1fb561303f81e175678144e6b6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.3/template.yaml @@ -0,0 +1,144 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspselinuxv2 + annotations: + metadata.gatekeeper.sh/title: "SELinux V2" + metadata.gatekeeper.sh/version: 1.0.3 + description: >- + Defines an allow-list of seLinuxOptions configurations for pod + containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. + For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux +spec: + crd: + spec: + names: + kind: K8sPSPSELinuxV2 + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Defines an allow-list of seLinuxOptions configurations for pod + containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. + For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux + 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 + allowedSELinuxOptions: + type: array + description: "An allow-list of SELinux options configurations." + items: + type: object + description: "An allowed configuration of SELinux options for a pod container." + properties: + level: + type: string + description: "An SELinux level." + role: + type: string + description: "An SELinux role." + type: + type: string + description: "An SELinux type." + user: + type: string + description: "An SELinux user." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspselinux + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + # Disallow top level custom SELinux options + violation[{"msg": msg, "details": {}}] { + # spec.securityContext.seLinuxOptions field is immutable. + not is_update(input.review) + + has_field(input.review.object.spec.securityContext, "seLinuxOptions") + not input_seLinuxOptions_allowed(input.review.object.spec.securityContext.seLinuxOptions) + msg := sprintf("SELinux options is not allowed, pod: %v. Allowed options: %v", [input.review.object.metadata.name, input.parameters.allowedSELinuxOptions]) + } + # Disallow container level custom SELinux options + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.seLinuxOptions field is immutable. + not is_update(input.review) + + c := input_security_context[_] + not is_exempt(c) + has_field(c.securityContext, "seLinuxOptions") + not input_seLinuxOptions_allowed(c.securityContext.seLinuxOptions) + msg := sprintf("SELinux options is not allowed, pod: %v, container: %v. Allowed options: %v", [input.review.object.metadata.name, c.name, input.parameters.allowedSELinuxOptions]) + } + + input_seLinuxOptions_allowed(options) { + params := input.parameters.allowedSELinuxOptions[_] + field_allowed("level", options, params) + field_allowed("role", options, params) + field_allowed("type", options, params) + field_allowed("user", options, params) + } + + field_allowed(field, options, params) { + params[field] == options[field] + } + field_allowed(field, options, _) { + not has_field(options, field) + } + + input_security_context[c] { + c := input.review.object.spec.containers[_] + has_field(c.securityContext, "seLinuxOptions") + } + input_security_context[c] { + c := input.review.object.spec.initContainers[_] + has_field(c.securityContext, "seLinuxOptions") + } + input_security_context[c] { + c := input.review.object.spec.ephemeralContainers[_] + has_field(c.securityContext, "seLinuxOptions") + } + + # 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..f5b9172671272267aedfe1469a04e43d6914cf88 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.4 +name: k8spspselinuxv2 +displayName: SELinux V2 +createdAt: "2026-01-08T18:35:12Z" +description: Defines an allow-list of seLinuxOptions configurations for pod containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux +digest: 6a9b290911d14e6561ba9d0c49c976d0e6fc999231dab02fdeec93fb0780885c +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/selinux +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # SELinux V2 + Defines an allow-list of seLinuxOptions configurations for pod containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/selinux/1.0.4/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f38381182dcbec9668a80ebca9142d4d5a273637 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSELinuxV2 +metadata: + name: psp-selinux-v2 +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + allowedSELinuxOptions: + - type: container_t + - type: container_init_t + - type: container_kvm_t + - type: container_engine_t diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0cce95eac0e4af1a7877e1a065fc04f1a30598cc --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/disallowed_ephemeral.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-selinux-disallowed + labels: + app: nginx-selinux +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + type: svirt_lxc_net_t + user: sysadm_u + role: sysadm_r diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8d914f2be0ba460b52dd57749efa5fa91e74bbdb --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-selinux-allowed + labels: + app: nginx-selinux +spec: + containers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + type: container_t diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7c715f8ee4590a97df283f625cf79debe2171e75 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/example_disallowed.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-selinux-disallowed + labels: + app: nginx-selinux +spec: + containers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + type: svirt_lxc_net_t + user: sysadm_u + role: sysadm_r diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..698fc6f75fb846db08a58c0f775a13bc7bb1e165 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/samples/psp-selinux-v2/update.yaml @@ -0,0 +1,20 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-selinux-disallowed + labels: + app: nginx-selinux + spec: + containers: + - name: nginx + image: nginx + securityContext: + seLinuxOptions: + type: svirt_lxc_net_t + user: sysadm_u + role: sysadm_r diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1bbaf360ede8fd308d913c96d38117f6c6069d2a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: selinux +tests: +- name: require-matching-selinux-options + template: template.yaml + constraint: samples/psp-selinux-v2/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-selinux-v2/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-selinux-v2/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-selinux-v2/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-selinux-v2/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5c8730e740d4687695c6e5e56a8773780146b089 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/selinux/1.0.4/template.yaml @@ -0,0 +1,145 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspselinuxv2 + annotations: + metadata.gatekeeper.sh/title: "SELinux V2" + metadata.gatekeeper.sh/version: 1.0.4 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + description: >- + Defines an allow-list of seLinuxOptions configurations for pod + containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. + For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux +spec: + crd: + spec: + names: + kind: K8sPSPSELinuxV2 + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Defines an allow-list of seLinuxOptions configurations for pod + containers. Corresponds to a PodSecurityPolicy requiring SELinux configs. + For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux + 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 + allowedSELinuxOptions: + type: array + description: "An allow-list of SELinux options configurations." + items: + type: object + description: "An allowed configuration of SELinux options for a pod container." + properties: + level: + type: string + description: "An SELinux level." + role: + type: string + description: "An SELinux role." + type: + type: string + description: "An SELinux type." + user: + type: string + description: "An SELinux user." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspselinux + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + # Disallow top level custom SELinux options + violation[{"msg": msg, "details": {}}] { + # spec.securityContext.seLinuxOptions field is immutable. + not is_update(input.review) + + has_field(input.review.object.spec.securityContext, "seLinuxOptions") + not input_seLinuxOptions_allowed(input.review.object.spec.securityContext.seLinuxOptions) + msg := sprintf("SELinux options is not allowed, pod: %v. Allowed options: %v", [input.review.object.metadata.name, input.parameters.allowedSELinuxOptions]) + } + # Disallow container level custom SELinux options + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.seLinuxOptions field is immutable. + not is_update(input.review) + + c := input_security_context[_] + not is_exempt(c) + has_field(c.securityContext, "seLinuxOptions") + not input_seLinuxOptions_allowed(c.securityContext.seLinuxOptions) + msg := sprintf("SELinux options is not allowed, pod: %v, container: %v. Allowed options: %v", [input.review.object.metadata.name, c.name, input.parameters.allowedSELinuxOptions]) + } + + input_seLinuxOptions_allowed(options) { + params := input.parameters.allowedSELinuxOptions[_] + field_allowed("level", options, params) + field_allowed("role", options, params) + field_allowed("type", options, params) + field_allowed("user", options, params) + } + + field_allowed(field, options, params) { + params[field] == options[field] + } + field_allowed(field, options, _) { + not has_field(options, field) + } + + input_security_context[c] { + c := input.review.object.spec.containers[_] + has_field(c.securityContext, "seLinuxOptions") + } + input_security_context[c] { + c := input.review.object.spec.initContainers[_] + has_field(c.securityContext, "seLinuxOptions") + } + input_security_context[c] { + c := input.review.object.spec.ephemeralContainers[_] + has_field(c.securityContext, "seLinuxOptions") + } + + # 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..f335274c37dac1e26a9cebfad4f19a4ba9644c30 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspallowedusers +displayName: Allowed Users +createdAt: "2022-09-26T17:28:27Z" +description: Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +digest: 20622440dce047425e4e26b7442de4928d7b6f83f5f32d502259232940669b12 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/users +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Allowed Users + Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/users/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e699745784c431527f8e27b3d9a75e995119e41d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/constraint.yaml @@ -0,0 +1,30 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPAllowedUsers +metadata: + name: psp-pods-allowed-user-ranges +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + runAsUser: + rule: MustRunAs # MustRunAsNonRoot # RunAsAny + ranges: + - min: 100 + max: 200 + runAsGroup: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 + supplementalGroups: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 + fsGroup: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6297f0dfd4fb9384df37e97d0ee5304c3ee55191 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..79899ed985a45eb879530a8918f9c599a260d6f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/example_allowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-allowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 199 + fsGroup: 199 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 199 + runAsGroup: 199 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..516cce14bc2ccb44b836ad4841c7e6335dc5849b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..20528f68c8e50f5ca99dbefa736e7502b950ebc4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: users +tests: +- name: users-and-groups-together + template: template.yaml + constraint: samples/psp-pods-allowed-user-ranges/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-pods-allowed-user-ranges/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-pods-allowed-user-ranges/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0d22f23bf7bc3c4d080b40b3ccbf49174e7188d5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.0/template.yaml @@ -0,0 +1,282 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspallowedusers + annotations: + metadata.gatekeeper.sh/title: "Allowed Users" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +spec: + crd: + spec: + names: + kind: K8sPSPAllowedUsers + validation: + openAPIV3Schema: + type: object + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups + 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 + runAsUser: + type: object + description: "Controls which user ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsUser restriction." + enum: + - MustRunAs + - MustRunAsNonRoot + - RunAsAny + ranges: + type: array + description: "A list of user ID ranges affected by the rule." + items: + type: object + description: "The range of user IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum user ID in the range, inclusive." + max: + type: integer + description: "The maximum user ID in the range, inclusive." + runAsGroup: + type: object + description: "Controls which group ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + supplementalGroups: + type: object + description: "Controls the supplementalGroups values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the supplementalGroups restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + fsGroup: + type: object + description: "Controls the fsGroup values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the fsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspallowedusers + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + fields := ["runAsUser", "runAsGroup", "supplementalGroups", "fsGroup"] + field := fields[_] + container := input_containers[_] + not is_exempt(container) + msg := get_type_violation(field, container) + } + + get_type_violation(field, container) = msg { + field == "runAsUser" + params := input.parameters[field] + msg := get_user_violation(params, container) + } + + get_type_violation(field, container) = msg { + field != "runAsUser" + params := input.parameters[field] + msg := get_violation(field, params, container) + } + + # RunAsUser (separate due to "MustRunAsNonRoot") + get_user_violation(params, container) = msg { + rule := params.rule + provided_user := get_field_value("runAsUser", container, input.review) + not accept_users(rule, provided_user) + msg := sprintf("Container %v is attempting to run as disallowed user %v. Allowed runAsUser: %v", [container.name, provided_user, params]) + } + + get_user_violation(params, container) = msg { + not get_field_value("runAsUser", container, input.review) + params.rule = "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsUser", [container.name]) + } + + get_user_violation(params, container) = msg { + params.rule = "MustRunAsNonRoot" + not get_field_value("runAsUser", container, input.review) + not get_field_value("runAsNonRoot", container, input.review) + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsNonRoot or securityContext/runAsUser != 0", [container.name]) + } + + accept_users("RunAsAny", provided_user) {true} + + accept_users("MustRunAsNonRoot", provided_user) = res {res := provided_user != 0} + + accept_users("MustRunAs", provided_user) = res { + ranges := input.parameters.runAsUser.ranges + res := is_in_range(provided_user, ranges) + } + + # Group Options + get_violation(field, params, container) = msg { + rule := params.rule + provided_value := get_field_value(field, container, input.review) + not is_array(provided_value) + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run as disallowed group %v. Allowed %v: %v", [container.name, provided_value, field, params]) + } + # SupplementalGroups is array value + get_violation(field, params, container) = msg { + rule := params.rule + array_value := get_field_value(field, container, input.review) + is_array(array_value) + provided_value := array_value[_] + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run with disallowed supplementalGroups %v. Allowed %v: %v", [container.name, array_value, field, params]) + } + + get_violation(field, params, container) = msg { + not get_field_value(field, container, input.review) + params.rule == "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/%v. Allowed %v: %v", [container.name, field, field, params]) + } + + accept_value("RunAsAny", provided_value, ranges) {true} + + accept_value("MayRunAs", provided_value, ranges) = res { res := is_in_range(provided_value, ranges)} + + accept_value("MustRunAs", provided_value, ranges) = res { res := is_in_range(provided_value, ranges)} + + + # If container level is provided, that takes precedence + get_field_value(field, container, review) = out { + container_value := get_seccontext_field(field, container) + out := container_value + } + + # If no container level exists, use pod level + get_field_value(field, container, review) = out { + not has_seccontext_field(field, container) + review.kind.kind == "Pod" + pod_value := get_seccontext_field(field, review.object.spec) + out := pod_value + } + + # Helper Functions + is_in_range(val, ranges) = res { + matching := {1 | val >= ranges[j].min; val <= ranges[j].max} + res := count(matching) > 0 + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) == false + } + + get_seccontext_field(field, obj) = out { + out = obj.securityContext[field] + } + + 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[_] + } + libs: + - | + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..338f2ee947fd63ebfdf3886a0ba9d9a18772429a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspallowedusers +displayName: Allowed Users +createdAt: "2023-05-23T09:47:32Z" +description: Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +digest: 4145c820284a655c6dfded52eb983df65e0eb5f76c4086e4edf03cbe6da2caf2 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/users +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Allowed Users + Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/users/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e699745784c431527f8e27b3d9a75e995119e41d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/constraint.yaml @@ -0,0 +1,30 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPAllowedUsers +metadata: + name: psp-pods-allowed-user-ranges +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + runAsUser: + rule: MustRunAs # MustRunAsNonRoot # RunAsAny + ranges: + - min: 100 + max: 200 + runAsGroup: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 + supplementalGroups: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 + fsGroup: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6297f0dfd4fb9384df37e97d0ee5304c3ee55191 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..79899ed985a45eb879530a8918f9c599a260d6f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/example_allowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-allowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 199 + fsGroup: 199 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 199 + runAsGroup: 199 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..516cce14bc2ccb44b836ad4841c7e6335dc5849b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6f4d3ed7224c994b7a5f3fdf8c7e12ff1c5fb682 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/samples/psp-pods-allowed-user-ranges/update.yaml @@ -0,0 +1,22 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-users-disallowed + labels: + app: nginx-users + spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5c6e49640204906e63545a83dc52d995fcf43037 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: users +tests: +- name: users-and-groups-together + template: template.yaml + constraint: samples/psp-pods-allowed-user-ranges/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-pods-allowed-user-ranges/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-pods-allowed-user-ranges/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-pods-allowed-user-ranges/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1e572443e396c2c06a27d79181f047e8e9b71316 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.1/template.yaml @@ -0,0 +1,292 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspallowedusers + annotations: + metadata.gatekeeper.sh/title: "Allowed Users" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +spec: + crd: + spec: + names: + kind: K8sPSPAllowedUsers + validation: + openAPIV3Schema: + type: object + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups + 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 + runAsUser: + type: object + description: "Controls which user ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsUser restriction." + enum: + - MustRunAs + - MustRunAsNonRoot + - RunAsAny + ranges: + type: array + description: "A list of user ID ranges affected by the rule." + items: + type: object + description: "The range of user IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum user ID in the range, inclusive." + max: + type: integer + description: "The maximum user ID in the range, inclusive." + runAsGroup: + type: object + description: "Controls which group ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + supplementalGroups: + type: object + description: "Controls the supplementalGroups values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the supplementalGroups restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + fsGroup: + type: object + description: "Controls the fsGroup values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the fsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspallowedusers + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + # runAsUser, runAsGroup, supplementalGroups, fsGroup fields are immutable. + not is_update(input.review) + + fields := ["runAsUser", "runAsGroup", "supplementalGroups", "fsGroup"] + field := fields[_] + container := input_containers[_] + not is_exempt(container) + msg := get_type_violation(field, container) + } + + get_type_violation(field, container) = msg { + field == "runAsUser" + params := input.parameters[field] + msg := get_user_violation(params, container) + } + + get_type_violation(field, container) = msg { + field != "runAsUser" + params := input.parameters[field] + msg := get_violation(field, params, container) + } + + # RunAsUser (separate due to "MustRunAsNonRoot") + get_user_violation(params, container) = msg { + rule := params.rule + provided_user := get_field_value("runAsUser", container, input.review) + not accept_users(rule, provided_user) + msg := sprintf("Container %v is attempting to run as disallowed user %v. Allowed runAsUser: %v", [container.name, provided_user, params]) + } + + get_user_violation(params, container) = msg { + not get_field_value("runAsUser", container, input.review) + params.rule = "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsUser", [container.name]) + } + + get_user_violation(params, container) = msg { + params.rule = "MustRunAsNonRoot" + not get_field_value("runAsUser", container, input.review) + not get_field_value("runAsNonRoot", container, input.review) + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsNonRoot or securityContext/runAsUser != 0", [container.name]) + } + + accept_users("RunAsAny", provided_user) {true} + + accept_users("MustRunAsNonRoot", provided_user) = res {res := provided_user != 0} + + accept_users("MustRunAs", provided_user) = res { + ranges := input.parameters.runAsUser.ranges + res := is_in_range(provided_user, ranges) + } + + # Group Options + get_violation(field, params, container) = msg { + rule := params.rule + provided_value := get_field_value(field, container, input.review) + not is_array(provided_value) + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run as disallowed group %v. Allowed %v: %v", [container.name, provided_value, field, params]) + } + # SupplementalGroups is array value + get_violation(field, params, container) = msg { + rule := params.rule + array_value := get_field_value(field, container, input.review) + is_array(array_value) + provided_value := array_value[_] + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run with disallowed supplementalGroups %v. Allowed %v: %v", [container.name, array_value, field, params]) + } + + get_violation(field, params, container) = msg { + not get_field_value(field, container, input.review) + params.rule == "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/%v. Allowed %v: %v", [container.name, field, field, params]) + } + + accept_value("RunAsAny", provided_value, ranges) {true} + + accept_value("MayRunAs", provided_value, ranges) = res { res := is_in_range(provided_value, ranges)} + + accept_value("MustRunAs", provided_value, ranges) = res { res := is_in_range(provided_value, ranges)} + + + # If container level is provided, that takes precedence + get_field_value(field, container, review) = out { + container_value := get_seccontext_field(field, container) + out := container_value + } + + # If no container level exists, use pod level + get_field_value(field, container, review) = out { + not has_seccontext_field(field, container) + review.kind.kind == "Pod" + pod_value := get_seccontext_field(field, review.object.spec) + out := pod_value + } + + # Helper Functions + is_in_range(val, ranges) = res { + matching := {1 | val >= ranges[j].min; val <= ranges[j].max} + res := count(matching) > 0 + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) == false + } + + get_seccontext_field(field, obj) = out { + out = obj.securityContext[field] + } + + 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[_] + } + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..5cfa65e70e9fc1711cb1b331fda43f68b1d494ef --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.2 +name: k8spspallowedusers +displayName: Allowed Users +createdAt: "2023-10-30T21:00:02Z" +description: Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +digest: 485bfc85639175c748da7632863dd3c91ad7f4dbd2544d2579cb82732ee795ea +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/users +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Allowed Users + Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/users/1.0.2/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e699745784c431527f8e27b3d9a75e995119e41d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/constraint.yaml @@ -0,0 +1,30 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPAllowedUsers +metadata: + name: psp-pods-allowed-user-ranges +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + runAsUser: + rule: MustRunAs # MustRunAsNonRoot # RunAsAny + ranges: + - min: 100 + max: 200 + runAsGroup: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 + supplementalGroups: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 + fsGroup: + rule: MustRunAs # MayRunAs # RunAsAny + ranges: + - min: 100 + max: 200 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6297f0dfd4fb9384df37e97d0ee5304c3ee55191 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..79899ed985a45eb879530a8918f9c599a260d6f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/example_allowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-allowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 199 + fsGroup: 199 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 199 + runAsGroup: 199 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..516cce14bc2ccb44b836ad4841c7e6335dc5849b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6f4d3ed7224c994b7a5f3fdf8c7e12ff1c5fb682 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/samples/psp-pods-allowed-user-ranges/update.yaml @@ -0,0 +1,22 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-users-disallowed + labels: + app: nginx-users + spec: + securityContext: + supplementalGroups: + - 250 + fsGroup: 250 + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 250 + runAsGroup: 250 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5c6e49640204906e63545a83dc52d995fcf43037 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: users +tests: +- name: users-and-groups-together + template: template.yaml + constraint: samples/psp-pods-allowed-user-ranges/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-pods-allowed-user-ranges/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-pods-allowed-user-ranges/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-pods-allowed-user-ranges/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..613fc9307d91b9e689523acfe52c84decba47990 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.2/template.yaml @@ -0,0 +1,289 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspallowedusers + annotations: + metadata.gatekeeper.sh/title: "Allowed Users" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +spec: + crd: + spec: + names: + kind: K8sPSPAllowedUsers + validation: + openAPIV3Schema: + type: object + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups + 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 + runAsUser: + type: object + description: "Controls which user ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsUser restriction." + enum: + - MustRunAs + - MustRunAsNonRoot + - RunAsAny + ranges: + type: array + description: "A list of user ID ranges affected by the rule." + items: + type: object + description: "The range of user IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum user ID in the range, inclusive." + max: + type: integer + description: "The maximum user ID in the range, inclusive." + runAsGroup: + type: object + description: "Controls which group ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + supplementalGroups: + type: object + description: "Controls the supplementalGroups values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the supplementalGroups restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + fsGroup: + type: object + description: "Controls the fsGroup values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the fsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspallowedusers + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + # runAsUser, runAsGroup, supplementalGroups, fsGroup fields are immutable. + not is_update(input.review) + + fields := ["runAsUser", "runAsGroup", "supplementalGroups", "fsGroup"] + field := fields[_] + container := input_containers[_] + not is_exempt(container) + msg := get_type_violation(field, container) + } + + get_type_violation(field, container) = msg { + field == "runAsUser" + params := input.parameters[field] + msg := get_user_violation(params, container) + } + + get_type_violation(field, container) = msg { + field != "runAsUser" + params := input.parameters[field] + msg := get_violation(field, params, container) + } + + # RunAsUser (separate due to "MustRunAsNonRoot") + get_user_violation(params, container) = msg { + rule := params.rule + provided_user := get_field_value("runAsUser", container, input.review) + not accept_users(rule, provided_user) + msg := sprintf("Container %v is attempting to run as disallowed user %v. Allowed runAsUser: %v", [container.name, provided_user, params]) + } + + get_user_violation(params, container) = msg { + not get_field_value("runAsUser", container, input.review) + params.rule = "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsUser", [container.name]) + } + + get_user_violation(params, container) = msg { + params.rule = "MustRunAsNonRoot" + not get_field_value("runAsUser", container, input.review) + not get_field_value("runAsNonRoot", container, input.review) + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsNonRoot or securityContext/runAsUser != 0", [container.name]) + } + + accept_users("RunAsAny", _) + + accept_users("MustRunAsNonRoot", provided_user) := provided_user != 0 + + accept_users("MustRunAs", provided_user) := res { + ranges := input.parameters.runAsUser.ranges + res := is_in_range(provided_user, ranges) + } + + # Group Options + get_violation(field, params, container) = msg { + rule := params.rule + provided_value := get_field_value(field, container, input.review) + not is_array(provided_value) + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run as disallowed group %v. Allowed %v: %v", [container.name, provided_value, field, params]) + } + # SupplementalGroups is array value + get_violation(field, params, container) = msg { + rule := params.rule + array_value := get_field_value(field, container, input.review) + is_array(array_value) + provided_value := array_value[_] + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run with disallowed supplementalGroups %v. Allowed %v: %v", [container.name, array_value, field, params]) + } + + get_violation(field, params, container) = msg { + not get_field_value(field, container, input.review) + params.rule == "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/%v. Allowed %v: %v", [container.name, field, field, params]) + } + + accept_value("RunAsAny", _, _) + + accept_value("MayRunAs", provided_value, ranges) := is_in_range(provided_value, ranges) + + accept_value("MustRunAs", provided_value, ranges) := is_in_range(provided_value, ranges) + + + # If container level is provided, that takes precedence + get_field_value(field, container, _) := get_seccontext_field(field, container) + + # If no container level exists, use pod level + get_field_value(field, container, review) = out { + not has_seccontext_field(field, container) + review.kind.kind == "Pod" + pod_value := get_seccontext_field(field, review.object.spec) + out := pod_value + } + + # Helper Functions + is_in_range(val, ranges) = res { + matching := {1 | val >= ranges[j].min; val <= ranges[j].max} + res := count(matching) > 0 + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) == false + } + + get_seccontext_field(field, obj) = out { + out = obj.securityContext[field] + } + + 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[_] + } + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..5fefcf13f063e555a280b8c2320f3434ed4eec04 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.3 +name: k8spspallowedusers +displayName: Allowed Users +createdAt: "2026-01-08T18:35:12Z" +description: Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +digest: 67262095158ebed7f44092b8267356ba0805c94e698e268c1d1cf69eb79c8569 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/users +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Allowed Users + Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/users/1.0.3/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..90fbd308c2ac658c467711802004721a4e865e04 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/constraint.yaml @@ -0,0 +1,18 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPAllowedUsers +metadata: + name: psp-pods-allowed-user-ranges +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + runAsUser: + rule: MustRunAsNonRoot + runAsGroup: + rule: RunAsAny + supplementalGroups: + rule: RunAsAny + fsGroup: + rule: RunAsAny diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..66156be8407fd449ec16f7c5bafdc404fa8590af --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + runAsUser: 0 + runAsGroup: 0 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8028ef49c974c3733cbaa39100f173b5c4755620 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/example_allowed.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-allowed + labels: + app: nginx-users +spec: + securityContext: + runAsNonRoot: true + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 1000 + runAsGroup: 1000 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ce7e633fb5e6095a368e6e1b257a8dd359be92a6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-users-disallowed + labels: + app: nginx-users +spec: + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 0 + runAsGroup: 0 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ac182bf4c8b2bb9d47d25cb77ec18d7f99ec6303 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/samples/psp-pods-allowed-user-ranges/update.yaml @@ -0,0 +1,18 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-users-disallowed + labels: + app: nginx-users + spec: + containers: + - name: nginx + image: nginx + securityContext: + runAsUser: 0 + runAsGroup: 0 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5c6e49640204906e63545a83dc52d995fcf43037 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: users +tests: +- name: users-and-groups-together + template: template.yaml + constraint: samples/psp-pods-allowed-user-ranges/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-pods-allowed-user-ranges/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-pods-allowed-user-ranges/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-pods-allowed-user-ranges/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..631da2fe8ebb70893e584e6a92fbcf0df1bf7f31 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/users/1.0.3/template.yaml @@ -0,0 +1,290 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspallowedusers + annotations: + metadata.gatekeeper.sh/title: "Allowed Users" + metadata.gatekeeper.sh/version: 1.0.3 + metadata.gatekeeper.sh/bundle: "pod-security-restricted" + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups +spec: + crd: + spec: + names: + kind: K8sPSPAllowedUsers + validation: + openAPIV3Schema: + type: object + description: >- + Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and + `fsGroup` fields in a PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups + 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 + runAsUser: + type: object + description: "Controls which user ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsUser restriction." + enum: + - MustRunAs + - MustRunAsNonRoot + - RunAsAny + ranges: + type: array + description: "A list of user ID ranges affected by the rule." + items: + type: object + description: "The range of user IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum user ID in the range, inclusive." + max: + type: integer + description: "The maximum user ID in the range, inclusive." + runAsGroup: + type: object + description: "Controls which group ID values are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the runAsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + supplementalGroups: + type: object + description: "Controls the supplementalGroups values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the supplementalGroups restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + fsGroup: + type: object + description: "Controls the fsGroup values that are allowed in a Pod or container-level SecurityContext." + properties: + rule: + type: string + description: "A strategy for applying the fsGroup restriction." + enum: + - MustRunAs + - MayRunAs + - RunAsAny + ranges: + type: array + description: "A list of group ID ranges affected by the rule." + items: + type: object + description: "The range of group IDs affected by the rule." + properties: + min: + type: integer + description: "The minimum group ID in the range, inclusive." + max: + type: integer + description: "The maximum group ID in the range, inclusive." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspallowedusers + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + # runAsUser, runAsGroup, supplementalGroups, fsGroup fields are immutable. + not is_update(input.review) + + fields := ["runAsUser", "runAsGroup", "supplementalGroups", "fsGroup"] + field := fields[_] + container := input_containers[_] + not is_exempt(container) + msg := get_type_violation(field, container) + } + + get_type_violation(field, container) = msg { + field == "runAsUser" + params := input.parameters[field] + msg := get_user_violation(params, container) + } + + get_type_violation(field, container) = msg { + field != "runAsUser" + params := input.parameters[field] + msg := get_violation(field, params, container) + } + + # RunAsUser (separate due to "MustRunAsNonRoot") + get_user_violation(params, container) = msg { + rule := params.rule + provided_user := get_field_value("runAsUser", container, input.review) + not accept_users(rule, provided_user) + msg := sprintf("Container %v is attempting to run as disallowed user %v. Allowed runAsUser: %v", [container.name, provided_user, params]) + } + + get_user_violation(params, container) = msg { + not get_field_value("runAsUser", container, input.review) + params.rule = "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsUser", [container.name]) + } + + get_user_violation(params, container) = msg { + params.rule = "MustRunAsNonRoot" + not get_field_value("runAsUser", container, input.review) + not get_field_value("runAsNonRoot", container, input.review) + msg := sprintf("Container %v is attempting to run without a required securityContext/runAsNonRoot or securityContext/runAsUser != 0", [container.name]) + } + + accept_users("RunAsAny", _) + + accept_users("MustRunAsNonRoot", provided_user) := provided_user != 0 + + accept_users("MustRunAs", provided_user) := res { + ranges := input.parameters.runAsUser.ranges + res := is_in_range(provided_user, ranges) + } + + # Group Options + get_violation(field, params, container) = msg { + rule := params.rule + provided_value := get_field_value(field, container, input.review) + not is_array(provided_value) + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run as disallowed group %v. Allowed %v: %v", [container.name, provided_value, field, params]) + } + # SupplementalGroups is array value + get_violation(field, params, container) = msg { + rule := params.rule + array_value := get_field_value(field, container, input.review) + is_array(array_value) + provided_value := array_value[_] + not accept_value(rule, provided_value, params.ranges) + msg := sprintf("Container %v is attempting to run with disallowed supplementalGroups %v. Allowed %v: %v", [container.name, array_value, field, params]) + } + + get_violation(field, params, container) = msg { + not get_field_value(field, container, input.review) + params.rule == "MustRunAs" + msg := sprintf("Container %v is attempting to run without a required securityContext/%v. Allowed %v: %v", [container.name, field, field, params]) + } + + accept_value("RunAsAny", _, _) + + accept_value("MayRunAs", provided_value, ranges) := is_in_range(provided_value, ranges) + + accept_value("MustRunAs", provided_value, ranges) := is_in_range(provided_value, ranges) + + + # If container level is provided, that takes precedence + get_field_value(field, container, _) := get_seccontext_field(field, container) + + # If no container level exists, use pod level + get_field_value(field, container, review) = out { + not has_seccontext_field(field, container) + review.kind.kind == "Pod" + pod_value := get_seccontext_field(field, review.object.spec) + out := pod_value + } + + # Helper Functions + is_in_range(val, ranges) = res { + matching := {1 | val >= ranges[j].min; val <= ranges[j].max} + res := count(matching) > 0 + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) + } + + has_seccontext_field(field, obj) { + get_seccontext_field(field, obj) == false + } + + get_seccontext_field(field, obj) = out { + out = obj.securityContext[field] + } + + 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[_] + } + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..03baebfab703299f6c46eb180cdad53230eee9d9 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspvolumetypes +displayName: Volume Types +createdAt: "2022-09-26T17:28:27Z" +description: Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: 78eb0333d3d9b0385907b394920e58e6bc90d1108cce359cc56b1d438dacec51 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/volumes +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Volume Types + Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` 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/volumes/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0638df7cb75ab784316ae501c919538a1d4ec1ce --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/constraint.yaml @@ -0,0 +1,20 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPVolumeTypes +metadata: + name: psp-volume-types +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + volumes: + # - "*" # * may be used to allow all volume types + - configMap + - emptyDir + - projected + - secret + - downwardAPI + - persistentVolumeClaim + #- hostPath #required for allowedHostPaths + - flexVolume #required for allowedFlexVolumes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..df6251e7d4ce369dde5a878c03a735c8f96c794e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/example_allowed.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-allowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + emptyDir: {} + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..562cf59d8abfa521e36e948c8681e5e89b5f1808 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/samples/psp-volume-types/example_disallowed.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-disallowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + hostPath: + path: /tmp # directory location on host + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b8f91b5a4a31b9c15c41495cf8654ff0429495f8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: volumes +tests: +- name: host-path-disallowed + template: template.yaml + constraint: samples/psp-volume-types/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-volume-types/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-volume-types/example_allowed.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..53f6346326f2311f982802d2c9e21a99a762b629 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.0/template.yaml @@ -0,0 +1,52 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspvolumetypes + annotations: + metadata.gatekeeper.sh/title: "Volume Types" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` 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: K8sPSPVolumeTypes + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` field in a PodSecurityPolicy. For more + information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + volumes: + description: "`volumes` is an array of volume types. All volume types can be enabled using `*`." + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspvolumetypes + + violation[{"msg": msg, "details": {}}] { + volume_fields := {x | input.review.object.spec.volumes[_][x]; x != "name"} + field := volume_fields[_] + not input_volume_type_allowed(field) + msg := sprintf("The volume type %v is not allowed, pod: %v. Allowed volume types: %v", [field, input.review.object.metadata.name, input.parameters.volumes]) + } + + # * may be used to allow all volume types + input_volume_type_allowed(field) { + input.parameters.volumes[_] == "*" + } + + input_volume_type_allowed(field) { + field == input.parameters.volumes[_] + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..2630a20fcf75c7eadf50170537a5367a07050ba6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspvolumetypes +displayName: Volume Types +createdAt: "2023-05-23T09:47:32Z" +description: Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: 95c71f8cfca302fcc4a122dc7f824884ea1f58d0f13b321b9f00a7976272764e +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/volumes +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Volume Types + Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` 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/volumes/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0638df7cb75ab784316ae501c919538a1d4ec1ce --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/constraint.yaml @@ -0,0 +1,20 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPVolumeTypes +metadata: + name: psp-volume-types +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + volumes: + # - "*" # * may be used to allow all volume types + - configMap + - emptyDir + - projected + - secret + - downwardAPI + - persistentVolumeClaim + #- hostPath #required for allowedHostPaths + - flexVolume #required for allowedFlexVolumes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..df6251e7d4ce369dde5a878c03a735c8f96c794e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/example_allowed.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-allowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + emptyDir: {} + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..562cf59d8abfa521e36e948c8681e5e89b5f1808 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/example_disallowed.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-disallowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + hostPath: + path: /tmp # directory location on host + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f25f0726720605792c5e643c845902cb49bc4fee --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/samples/psp-volume-types/update.yaml @@ -0,0 +1,29 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-volume-types-disallowed + labels: + app: nginx-volume-types + spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + hostPath: + path: /tmp # directory location on host + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..083aad6ebaacae20b6b9c0c62014fdc59293339b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: volumes +tests: +- name: host-path-disallowed + template: template.yaml + constraint: samples/psp-volume-types/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-volume-types/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-volume-types/example_allowed.yaml + assertions: + - violations: no + - name: update + object: samples/psp-volume-types/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..458cb3178bdbca6a4f983e6d86015cebe3a0a480 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.1/template.yaml @@ -0,0 +1,64 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspvolumetypes + annotations: + metadata.gatekeeper.sh/title: "Volume Types" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` 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: K8sPSPVolumeTypes + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` field in a PodSecurityPolicy. For more + information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + volumes: + description: "`volumes` is an array of volume types. All volume types can be enabled using `*`." + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspvolumetypes + + import data.lib.exclude_update.is_update + + violation[{"msg": msg, "details": {}}] { + # spec.volumes field is immutable. + not is_update(input.review) + + volume_fields := {x | input.review.object.spec.volumes[_][x]; x != "name"} + field := volume_fields[_] + not input_volume_type_allowed(field) + msg := sprintf("The volume type %v is not allowed, pod: %v. Allowed volume types: %v", [field, input.review.object.metadata.name, input.parameters.volumes]) + } + + # * may be used to allow all volume types + input_volume_type_allowed(field) { + input.parameters.volumes[_] == "*" + } + + input_volume_type_allowed(field) { + field == input.parameters.volumes[_] + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..39688ea1ad88b94453688296a55e7f0a7014388e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.2 +name: k8spspvolumetypes +displayName: Volume Types +createdAt: "2023-10-30T21:00:02Z" +description: Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: e94d48090afb1d63dc8fdf84ffaa64e650a14d02244bbc9099eeab73d89ec5c6 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/volumes +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Volume Types + Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` 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/volumes/1.0.2/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0638df7cb75ab784316ae501c919538a1d4ec1ce --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/constraint.yaml @@ -0,0 +1,20 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPVolumeTypes +metadata: + name: psp-volume-types +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + volumes: + # - "*" # * may be used to allow all volume types + - configMap + - emptyDir + - projected + - secret + - downwardAPI + - persistentVolumeClaim + #- hostPath #required for allowedHostPaths + - flexVolume #required for allowedFlexVolumes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..df6251e7d4ce369dde5a878c03a735c8f96c794e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/example_allowed.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-allowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + emptyDir: {} + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..562cf59d8abfa521e36e948c8681e5e89b5f1808 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/example_disallowed.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-disallowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + hostPath: + path: /tmp # directory location on host + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f25f0726720605792c5e643c845902cb49bc4fee --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/samples/psp-volume-types/update.yaml @@ -0,0 +1,29 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-volume-types-disallowed + labels: + app: nginx-volume-types + spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + hostPath: + path: /tmp # directory location on host + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..083aad6ebaacae20b6b9c0c62014fdc59293339b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: volumes +tests: +- name: host-path-disallowed + template: template.yaml + constraint: samples/psp-volume-types/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-volume-types/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-volume-types/example_allowed.yaml + assertions: + - violations: no + - name: update + object: samples/psp-volume-types/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..74165c8f44d07e41db40685a2d1c6784c53c834e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.2/template.yaml @@ -0,0 +1,64 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspvolumetypes + annotations: + metadata.gatekeeper.sh/title: "Volume Types" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` 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: K8sPSPVolumeTypes + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` field in a PodSecurityPolicy. For more + information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + volumes: + description: "`volumes` is an array of volume types. All volume types can be enabled using `*`." + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspvolumetypes + + import data.lib.exclude_update.is_update + + violation[{"msg": msg, "details": {}}] { + # spec.volumes field is immutable. + not is_update(input.review) + + volume_fields := {x | input.review.object.spec.volumes[_][x]; x != "name"} + field := volume_fields[_] + not input_volume_type_allowed(field) + msg := sprintf("The volume type %v is not allowed, pod: %v. Allowed volume types: %v", [field, input.review.object.metadata.name, input.parameters.volumes]) + } + + # * may be used to allow all volume types + input_volume_type_allowed(_) { + input.parameters.volumes[_] == "*" + } + + input_volume_type_allowed(field) { + field == input.parameters.volumes[_] + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..8b4b8266a564e3e9f98e7bdb935b4b6889776c33 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.3 +name: k8spspvolumetypes +displayName: Volume Types +createdAt: "2026-01-08T18:35:12Z" +description: Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: e454a7796f1c2fe8d00e345f635ad95dede53d671dc1ff9c3a133cf92135e7ed +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/volumes +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Volume Types + Restricts mountable volume types to those specified by the user. Corresponds to the `volumes` 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/volumes/1.0.3/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..57a19fe6356db0d130b9ba682c1160de68062565 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/constraint.yaml @@ -0,0 +1,20 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPVolumeTypes +metadata: + name: psp-volume-types +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + volumes: + # - "*" # * may be used to allow all volume types + - configMap + - csi + - downwardAPI + - emptyDir + - ephemeral + - persistentVolumeClaim + - projected + - secret diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..df6251e7d4ce369dde5a878c03a735c8f96c794e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/example_allowed.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-allowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + emptyDir: {} + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..562cf59d8abfa521e36e948c8681e5e89b5f1808 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/example_disallowed.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-volume-types-disallowed + labels: + app: nginx-volume-types +spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + hostPath: + path: /tmp # directory location on host + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f25f0726720605792c5e643c845902cb49bc4fee --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/samples/psp-volume-types/update.yaml @@ -0,0 +1,29 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-volume-types-disallowed + labels: + app: nginx-volume-types + spec: + containers: + - name: nginx + image: nginx + volumeMounts: + - mountPath: /cache + name: cache-volume + - name: nginx2 + image: nginx + volumeMounts: + - mountPath: /cache2 + name: demo-vol + volumes: + - name: cache-volume + hostPath: + path: /tmp # directory location on host + - name: demo-vol + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..083aad6ebaacae20b6b9c0c62014fdc59293339b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: volumes +tests: +- name: host-path-disallowed + template: template.yaml + constraint: samples/psp-volume-types/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-volume-types/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-volume-types/example_allowed.yaml + assertions: + - violations: no + - name: update + object: samples/psp-volume-types/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b32d468bf172630231ea82be7ae5a634b02ad2b5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/artifacthub/library/pod-security-policy/volumes/1.0.3/template.yaml @@ -0,0 +1,65 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspvolumetypes + annotations: + metadata.gatekeeper.sh/title: "Volume Types" + metadata.gatekeeper.sh/version: 1.0.3 + metadata.gatekeeper.sh/bundle: "pod-security-restricted" + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` 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: K8sPSPVolumeTypes + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Restricts mountable volume types to those specified by the user. + Corresponds to the `volumes` field in a PodSecurityPolicy. For more + information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + volumes: + description: "`volumes` is an array of volume types. All volume types can be enabled using `*`." + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspvolumetypes + + import data.lib.exclude_update.is_update + + violation[{"msg": msg, "details": {}}] { + # spec.volumes field is immutable. + not is_update(input.review) + + volume_fields := {x | input.review.object.spec.volumes[_][x]; x != "name"} + field := volume_fields[_] + not input_volume_type_allowed(field) + msg := sprintf("The volume type %v is not allowed, pod: %v. Allowed volume types: %v", [field, input.review.object.metadata.name, input.parameters.volumes]) + } + + # * may be used to allow all volume types + input_volume_type_allowed(_) { + input.parameters.volumes[_] == "*" + } + + input_volume_type_allowed(field) { + field == input.parameters.volumes[_] + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/build/gator/Dockerfile b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/build/gator/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..36d4505e1ded3702fe49e6954e3acc11594016d6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/build/gator/Dockerfile @@ -0,0 +1,17 @@ +FROM alpine + +ARG GATOR_VERSION + +# Install dependencies. libc6-compat is for CGO support. +RUN apk --no-cache add curl tar libc6-compat + +# Download and unpack gator +WORKDIR /bin +RUN curl -OL https://github.com/open-policy-agent/gatekeeper/releases/download/v${GATOR_VERSION}/gator-v${GATOR_VERSION}-linux-amd64.tar.gz +RUN tar xvf gator-v${GATOR_VERSION}-linux-amd64.tar.gz && rm gator-v${GATOR_VERSION}-linux-amd64.tar.gz + +# Create the mount point for the library +RUN mkdir /gatekeeper-library +WORKDIR /gatekeeper-library + +ENTRYPOINT ["/bin/gator"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/build/gomplate/Dockerfile b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/build/gomplate/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..1147b0f119d43b4ebe5bab365784eba532adbf67 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/build/gomplate/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.27@sha256:3680233e3204827fbdc66088528ae6d4b3d034f51d03a99d454f6de034888244 + +ARG GOMPLATE_VERSION + +# Install gomplate +RUN go install github.com/hairyhenderson/gomplate/v3/cmd/gomplate@v${GOMPLATE_VERSION} + +# Create the mount point for the library +RUN mkdir /gatekeeper-library +WORKDIR /gatekeeper-library + +ENTRYPOINT ["/bin/bash"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/catalog.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/catalog.yaml new file mode 100644 index 0000000000000000000000000000000000000000..73a77e51d3f2868bdc8dc68f995fd67f787e674f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/catalog.yaml @@ -0,0 +1,482 @@ +# Policy Catalog for Gatekeeper Library +# +# This file is auto-generated. Do not edit directly. +# Generated at: 2026-02-12T23:49:33Z +# +# To regenerate, run: +# gator policy generate-catalog --library-path=. --output=catalog.yaml +# +apiVersion: gator.gatekeeper.sh/v1alpha1 +bundles: +- description: |- + Enforces Pod Security Standards at Baseline level. Prevents known privilege escalations. + See https://kubernetes.io/docs/concepts/security/pod-security-standards/ + name: pod-security-baseline + policies: + - k8spspapparmor + - k8spspcapabilities + - k8spspforbiddensysctls + - k8spsphostfilesystem + - k8spsphostnamespace + - k8spsphostnetworkingports + - k8spsphostprobeslifecycle + - k8spsphostprocess + - k8spspprivilegedcontainer + - k8spspprocmount + - k8spspseccompv2 + - k8spspselinuxv2 +- description: |- + Enforces Pod Security Standards at Restricted level. Includes all Baseline controls plus additional hardening. + See https://kubernetes.io/docs/concepts/security/pod-security-standards/ + name: pod-security-restricted + policies: + - k8spspallowedusers + - k8spspallowprivilegeescalationcontainer + - k8spspapparmor + - k8spspcapabilities + - k8spspforbiddensysctls + - k8spsphostfilesystem + - k8spsphostnamespace + - k8spsphostnetworkingports + - k8spsphostprobeslifecycle + - k8spsphostprocess + - k8spspprivilegedcontainer + - k8spspprocmount + - k8spspseccompv2 + - k8spspselinuxv2 + - k8spspvolumetypes +kind: PolicyCatalog +metadata: + name: gatekeeper-library + repository: https://github.com/open-policy-agent/gatekeeper-library + updatedAt: "2026-02-12T23:49:33.304069431Z" + version: v1.0.0 +policies: +- category: general + description: Requires container images to begin with a string from the specified + list. To prevent bypasses, ensure a '/' is added when specifying DockerHub repositories + or custom registries. If exact matches or glob-like syntax are preferred, use + the k8sallowedreposv2 policy. + name: k8sallowedrepos + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/allowedrepos/template.yaml + version: v1.0.2 +- category: general + description: 'This policy enforces that container images must begin with a string + from a specified list. The updated version, K8sAllowedReposv2, introduces support + for exact match and glob-like syntax to enhance security: 1. Exact Match: By default, + if the * character is not specified, the policy strictly checks for an exact match + of the full registry, repository, and/or the image name. 2. Glob-like Syntax: + Adding * at the end of a prefix allows prefix-based matching (e.g., registry.example.com/project/*). + Only the * wildcard at the end of a string is supported. 3. Security Note: To + avoid bypasses scenarios, ensure prefixes include a trailing / where appropriate + (e.g., registry.example.com/project/*).' + name: k8sallowedreposv2 + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/allowedreposv2/template.yaml + version: v1.0.0 +- category: general + description: |- + Many Kubernetes installations by default have a system:aggregate-to-edit ClusterRole which does not properly restrict access to editing Endpoints. This ConstraintTemplate forbids the system:aggregate-to-edit ClusterRole from granting permission to create/patch/update Endpoints. + ClusterRole/system:aggregate-to-edit should not allow Endpoint edit permissions due to CVE-2021-25740, Endpoint & EndpointSlice permissions allow cross-Namespace forwarding, https://github.com/kubernetes/kubernetes/issues/103675 + name: k8sblockendpointeditdefaultrole + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/block-endpoint-edit-default-role/template.yaml + version: v1.0.0 +- category: general + description: |- + Disallows all Services with type LoadBalancer. + https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer + name: k8sblockloadbalancer + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/block-loadbalancer-services/template.yaml + version: v1.0.0 +- category: general + description: |- + Disallows all Services with type NodePort. + https://kubernetes.io/docs/concepts/services-networking/service/#nodeport + name: k8sblocknodeport + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/block-nodeport-services/template.yaml + version: v1.0.0 +- category: general + description: Users should not be able to create Ingresses with a blank or wildcard + (*) hostname since that would enable them to intercept traffic for other services + in the cluster, even if they don't have access to those services. + name: k8sblockwildcardingress + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/block-wildcard-ingress/template.yaml + version: v1.0.1 +- category: general + description: |- + Requires containers to have an ephemeral storage limit set and constrains the limit to be within the specified maximum values. + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ + name: k8scontainerephemeralstoragelimit + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/ephemeralstoragelimit/template.yaml + version: v1.0.2 +- category: general + description: |- + Requires containers to have memory and CPU limits set and constrains limits to be within the specified maximum values. + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ + name: k8scontainerlimits + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/containerlimits/template.yaml + version: v1.1.0 +- category: general + description: |- + Sets a maximum ratio for container resource limits to requests. + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ + name: k8scontainerratios + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/containerresourceratios/template.yaml + version: v1.0.1 +- category: general + description: |- + Requires containers to have memory and CPU requests set and constrains requests to be within the specified maximum values. + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ + name: k8scontainerrequests + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/containerrequests/template.yaml + version: v1.0.1 +- category: general + description: Disallows associating ClusterRole and Role resources to the system:anonymous + user and system:unauthenticated group. + name: k8sdisallowanonymous + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/disallowanonymous/template.yaml + version: v1.1.0 +- category: general + description: Disallowed container repositories that begin with a string from the + specified list. + name: k8sdisallowedrepos + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/disallowedrepos/template.yaml + version: v1.0.0 +- category: general + description: |- + Requires container images to have an image tag different from the ones in the specified list. + https://kubernetes.io/docs/concepts/containers/images/#image-names + name: k8sdisallowedtags + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/disallowedtags/template.yaml + version: v1.0.2 +- category: general + description: Requires that objects have the fields `spec.tty` and `spec.stdin` set + to false or unset. + name: k8sdisallowinteractivetty + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/disallowinteractive/template.yaml + version: v1.0.0 +- category: general + description: |- + Restricts Service externalIPs to an allowed list of IP addresses. + https://kubernetes.io/docs/concepts/services-networking/service/#external-ips + name: k8sexternalips + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/externalip/template.yaml + version: v1.0.0 +- category: general + description: Disallow the following scenarios when deploying `HorizontalPodAutoscalers` + 1. Deployment of HorizontalPodAutoscalers with `.spec.minReplicas` or `.spec.maxReplicas` + outside the ranges defined in the constraint 2. Deployment of HorizontalPodAutoscalers + where the difference between `.spec.minReplicas` and `.spec.maxReplicas` is less + than the configured `minimumReplicaSpread` 3. Deployment of HorizontalPodAutoscalers + that do not reference a valid `scaleTargetRef` (e.g. Deployment, ReplicationController, + ReplicaSet, StatefulSet). + name: k8shorizontalpodautoscaler + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/horizontalpodautoscaler/template.yaml + version: v1.0.1 +- category: general + description: |- + Requires Ingress resources to be HTTPS only. Ingress resources must include the `kubernetes.io/ingress.allow-http` annotation, set to `false`. By default a valid TLS {} configuration is required, this can be made optional by setting the `tlsOptional` parameter to `true`. + https://kubernetes.io/docs/concepts/services-networking/ingress/#tls + name: k8shttpsonly + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/httpsonly/template.yaml + version: v1.0.2 +- category: general + description: |- + Requires container images to contain a digest. + https://kubernetes.io/docs/concepts/containers/images/ + name: k8simagedigests + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/imagedigests/template.yaml + version: v1.0.1 +- category: general + description: |- + Disallow the following scenarios when deploying PodDisruptionBudgets or resources that implement the replica subresource (e.g. Deployment, ReplicationController, ReplicaSet, StatefulSet): 1. Deployment of PodDisruptionBudgets with .spec.maxUnavailable == 0 2. Deployment of PodDisruptionBudgets with .spec.minAvailable == .spec.replicas of the resource with replica subresource This will prevent PodDisruptionBudgets from blocking voluntary disruptions such as node draining. + https://kubernetes.io/docs/concepts/workloads/pods/disruptions/ + name: k8spoddisruptionbudget + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/poddisruptionbudget/template.yaml + version: v1.0.4 +- bundleConstraints: + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/users/samples/psp-pods-allowed-user-ranges/constraint.yaml + bundles: + - pod-security-restricted + category: pod-security + description: Controls the user and group IDs of the container and some volumes. + Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` + fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups + name: k8spspallowedusers + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/users/template.yaml + version: v1.0.3 +- bundleConstraints: + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/constraint.yaml + bundles: + - pod-security-restricted + category: pod-security + description: Controls restricting escalation to root privileges. Corresponds to + the `allowPrivilegeEscalation` field in a PodSecurityPolicy. For more information, + see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privilege-escalation + name: k8spspallowprivilegeescalationcontainer + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/allow-privilege-escalation/template.yaml + version: v1.1.1 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/apparmor/samples/psp-apparmor/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/apparmor/samples/psp-apparmor/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Configures an allow-list of AppArmor profiles for use by containers. + This corresponds to specific annotations applied to a PodSecurityPolicy. For information + on AppArmor, see https://kubernetes.io/docs/tutorials/clusters/apparmor/ + name: k8spspapparmor + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/apparmor/template.yaml + version: v1.1.1 +- category: general + description: Controls the ability of any Pod to enable automountServiceAccountToken. + name: k8spspautomountserviceaccounttokenpod + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/automount-serviceaccount-token/template.yaml + version: v1.0.1 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/capabilities/samples/psp-capabilities-restricted/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Controls Linux capabilities on containers. Corresponds to the `allowedCapabilities` + and `requiredDropCapabilities` fields in a PodSecurityPolicy. For more information, + see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#capabilities + name: k8spspcapabilities + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/capabilities/template.yaml + version: v1.1.3 +- category: pod-security + 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 + name: k8spspflexvolumes + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/flexvolume-drivers/template.yaml + version: v1.0.1 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/forbidden-sysctls/samples/psp-forbidden-sysctls/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/forbidden-sysctls/samples/psp-forbidden-sysctls/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Controls the `sysctl` profile used by containers. Corresponds to the + `allowedUnsafeSysctls` and `forbiddenSysctls` fields in a PodSecurityPolicy. When + specified, any sysctl not in the `allowedSysctls` parameter is considered to be + forbidden. The `forbiddenSysctls` parameter takes precedence over the `allowedSysctls` + parameter. For more information, see https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/ + name: k8spspforbiddensysctls + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/forbidden-sysctls/template.yaml + version: v1.2.1 +- category: pod-security + 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 + name: k8spspfsgroup + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/fsgroup/template.yaml + version: v1.1.0 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-filesystem/samples/no-host-paths/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-filesystem/samples/no-host-paths/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Controls usage of the host filesystem. Corresponds to the `allowedHostPaths` + field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + name: k8spsphostfilesystem + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-filesystem/template.yaml + version: v1.1.2 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-namespaces/samples/psp-host-namespace/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-namespaces/samples/psp-host-namespace/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Disallows sharing of host PID and IPC namespaces by pod containers. + Corresponds to the `hostPID` and `hostIPC` fields in a PodSecurityPolicy. For + more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#host-namespaces + name: k8spsphostnamespace + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-namespaces/template.yaml + version: v1.1.1 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-network-ports/samples/block_host_network/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-network-ports/samples/block_host_network/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Controls usage of host network namespace by pod containers. HostNetwork + verification happens without exception for exemptImages. Specific ports must be + specified. Corresponds to the `hostNetwork` and `hostPorts` fields in a PodSecurityPolicy. + For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#host-namespaces + name: k8spsphostnetworkingports + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-network-ports/template.yaml + version: v1.1.5 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-probes-lifecycle/samples/psp-host-probes-lifecycle/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-probes-lifecycle/samples/psp-host-probes-lifecycle/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Disallows specifying the host field in probes and lifecycle hooks. + The Baseline profile (v1.34+) requires that probes (livenessProbe, readinessProbe, + startupProbe) and lifecycle hooks (postStart, preStop) must not specify a host + field. This prevents containers from executing network requests to the host node. + For more information, see https://kubernetes.io/docs/concepts/security/pod-security-standards/ + name: k8spsphostprobeslifecycle + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-probes-lifecycle/template.yaml + version: v1.0.1 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-process/samples/psp-host-process/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-process/samples/psp-host-process/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Disallows HostProcess containers for Windows pods. HostProcess containers + enable privileged access on Windows nodes and must be disallowed in Baseline and + Restricted policies. Corresponds to the windowsOptions.hostProcess field in a + Pod's securityContext or container securityContext. For more information, see + https://kubernetes.io/docs/concepts/security/pod-security-standards/ + name: k8spsphostprocess + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/host-process/template.yaml + version: v1.0.1 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/privileged-containers/samples/psp-privileged-container/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/privileged-containers/samples/psp-privileged-container/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Controls the ability of any container to enable privileged mode. Corresponds + to the `privileged` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privileged + name: k8spspprivilegedcontainer + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/privileged-containers/template.yaml + version: v1.1.3 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/proc-mount/samples/psp-proc-mount/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/proc-mount/samples/psp-proc-mount/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Controls the allowed `procMount` types for the container. Corresponds + to the `allowedProcMountTypes` field in a PodSecurityPolicy. For more information, + see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#allowedprocmounttypes + name: k8spspprocmount + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/proc-mount/template.yaml + version: v1.1.3 +- category: pod-security + 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 + name: k8spspreadonlyrootfilesystem + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/read-only-root-filesystem/template.yaml + version: v1.1.1 +- category: pod-security + description: Controls the seccomp profile used by containers. Corresponds to the + `seccomp.security.alpha.kubernetes.io/allowedProfileNames` annotation on a PodSecurityPolicy. + For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#seccomp + name: k8spspseccomp + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/seccomp/template.yaml + version: v1.1.0 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/seccompv2/samples/psp-seccomp-baseline/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/seccompv2/samples/psp-seccomp-restricted/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Controls the seccomp profile used by containers. Corresponds to the + `securityContext.seccompProfile` field. Security contexts from the annotation + is not considered as Kubernetes no longer reads security contexts from the annotation. + name: k8spspseccompv2 + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/seccompv2/template.yaml + version: v1.0.1 +- bundleConstraints: + pod-security-baseline: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/selinux/samples/psp-selinux-v2/constraint.yaml + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/selinux/samples/psp-selinux-v2/constraint.yaml + bundles: + - pod-security-baseline + - pod-security-restricted + category: pod-security + description: Defines an allow-list of seLinuxOptions configurations for pod containers. + Corresponds to a PodSecurityPolicy requiring SELinux configs. For more information, + see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#selinux + name: k8spspselinuxv2 + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/selinux/template.yaml + version: v1.0.4 +- bundleConstraints: + pod-security-restricted: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/volumes/samples/psp-volume-types/constraint.yaml + bundles: + - pod-security-restricted + category: pod-security + description: Restricts mountable volume types to those specified by the user. Corresponds + to the `volumes` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + name: k8spspvolumetypes + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/pod-security-policy/volumes/template.yaml + version: v1.0.3 +- category: general + description: Requires that objects with the field `spec.replicas` (Deployments, + ReplicaSets, etc.) specify a number of replicas within defined ranges. + name: k8sreplicalimits + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/replicalimits/template.yaml + version: v1.0.2 +- category: general + description: Requires resources to contain specified annotations, with values matching + provided regular expressions. + name: k8srequiredannotations + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/requiredannotations/template.yaml + version: v1.0.1 +- category: general + description: Requires resources to contain specified labels, with values matching + provided regular expressions. + name: k8srequiredlabels + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/requiredlabels/template.yaml + version: v1.1.2 +- category: general + description: Requires Pods to have readiness and/or liveness probes. + name: k8srequiredprobes + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/requiredprobes/template.yaml + version: v1.0.1 +- category: general + description: |- + Requires containers to have defined resources set. + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ + name: k8srequiredresources + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/containerresources/template.yaml + version: v1.0.1 +- category: general + description: Requires storage classes to be specified when used. Only Gatekeeper + 3.9+ and non-ephemeral containers are supported. + name: k8sstorageclass + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/storageclass/template.yaml + version: v1.1.2 +- category: general + description: |- + Requires all Ingress rule hosts to be unique. + Does not handle hostname wildcards: https://kubernetes.io/docs/concepts/services-networking/ingress/ + name: k8suniqueingresshost + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/uniqueingresshost/template.yaml + version: v1.0.4 +- category: general + description: |- + Requires Services to have unique selectors within a namespace. Selectors are considered the same if they have identical keys and values. Selectors may share a key/value pair so long as there is at least one distinct key/value pair between them. + https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service + name: k8suniqueserviceselector + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/uniqueserviceselector/template.yaml + version: v1.0.2 +- category: general + description: Blocks updating the service account on resources that abstract over + Pods. This policy is ignored in audit mode. + name: noupdateserviceaccount + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/noupdateserviceaccount/template.yaml + version: v1.0.1 +- category: general + description: Verifies deprecated Kubernetes APIs to ensure all the API versions + are up to date. This template does not apply to audit as audit looks at the resources + which are already present in the cluster with non-deprecated API versions. + name: verifydeprecatedapi + templatePath: https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/refs/heads/master/library/general/verifydeprecatedapi/template.yaml + version: v1.0.0 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/docs/design/README.md b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/docs/design/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3cacf718b7dcc99aec858d8a08dc3a0695566894 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/docs/design/README.md @@ -0,0 +1,12 @@ +# Design Docs +Generally, design docs are on Google docs: + +## Conventions +* [Gatekeeper Library Policy Versioning](https://docs.google.com/document/d/1IYiypA-mRcdfSVfmoeyuaeG8XtA1u4GkcqH3kEkv2uw/edit) + +## In Development + +## Proposed + +## Implemented +* [Gatekeeper Library Website and Artifact Hub Listing](https://docs.google.com/document/d/1IYiypA-mRcdfSVfmoeyuaeG8XtA1u4GkcqH3kEkv2uw/edit) \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/go.work b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/go.work new file mode 100644 index 0000000000000000000000000000000000000000..0866d5e29ce91c4a055dddeef777b8afc491642b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/go.work @@ -0,0 +1,8 @@ +go 1.20 + +use ( + ./scripts/artifacthub + ./scripts/require-sync + ./scripts/validate + ./scripts/website +) diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..be500b5ae9721b3662e0fcdddaedc906266449df --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/constraint.yaml @@ -0,0 +1,14 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sAllowedRepos +metadata: + name: repo-is-openpolicyagent +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + namespaces: + - "default" + parameters: + repos: + - "openpolicyagent/" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/disallowed_all.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/disallowed_all.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bec477884eb21f2308004730a4d57eb2fe9cd054 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/disallowed_all.yaml @@ -0,0 +1,26 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + initContainers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + containers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + ephemeralContainers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2dc3cd2467ae0e1605a402550144ba984f44a3d7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_allowed.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4d5f3ba642a84a7112f622d25ccc602d45560721 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + initContainers: + - name: nginxinit + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + containers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml new file mode 100644 index 0000000000000000000000000000000000000000..face5057ec19fad07f75233ab3b45e67a72039b6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + containers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6f7a886c6d08e572bfe20bb873fbbe6e361064ea --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + initContainers: + - name: nginxinit + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..819b8457c61bfb45986031f181d00b305627f97f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/suite.yaml @@ -0,0 +1,43 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: allowedrepos +tests: +- name: allowed-repos + template: template.yaml + constraint: samples/repo-must-be-openpolicyagent/constraint.yaml + cases: + - name: example-allowed + object: samples/repo-must-be-openpolicyagent/example_allowed.yaml + assertions: + - violations: no + - name: container-disallowed + object: samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml + assertions: + - violations: yes + message: container + - name: initcontainer-disallowed + object: samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml + assertions: + - violations: 1 + message: initContainer + - violations: 0 + message: container + - name: both-disallowed + object: samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml + assertions: + - violations: 2 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - name: all-disallowed + object: samples/repo-must-be-openpolicyagent/disallowed_all.yaml + assertions: + - violations: 3 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - message: ephemeralContainer + violations: 1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2848908a5f95885f8318944c266d91368dc791e8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedrepos/template.yaml @@ -0,0 +1,48 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sallowedrepos + annotations: + metadata.gatekeeper.sh/title: "Allowed Repositories" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Requires container images to begin with a string from the specified list. + To prevent bypasses, ensure a '/' is added when specifying DockerHub repositories or custom registries. + If exact matches or glob-like syntax are preferred, use the k8sallowedreposv2 policy. +spec: + crd: + spec: + names: + kind: K8sAllowedRepos + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + properties: + repos: + description: The list of prefixes a container image is allowed to have. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sallowedrepos + + violation[{"msg": msg}] { + container := input.review.object.spec.containers[_] + not strings.any_prefix_match(container.image, input.parameters.repos) + msg := sprintf("container <%v> has an invalid image repo <%v>, allowed repos are %v", [container.name, container.image, input.parameters.repos]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.initContainers[_] + not strings.any_prefix_match(container.image, input.parameters.repos) + msg := sprintf("initContainer <%v> has an invalid image repo <%v>, allowed repos are %v", [container.name, container.image, input.parameters.repos]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.ephemeralContainers[_] + not strings.any_prefix_match(container.image, input.parameters.repos) + msg := sprintf("ephemeralContainer <%v> has an invalid image repo <%v>, allowed repos are %v", [container.name, container.image, input.parameters.repos]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9ecfce40bad8862f20e4f9cacaa529828147d48f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/constraint.yaml @@ -0,0 +1,18 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sAllowedReposv2 +metadata: + name: repo-is-openpolicyagent +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + namespaces: + - "default" + parameters: + allowedImages: + - "openpolicyagent/*" + - "myregistry.azurecr.io/*" + - "mydockerhub/*" + - "ubuntu:20.14" + - "123456789123.dkr.ecr.eu-west-1.amazonaws.com/postgres" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/disallowed_all.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/disallowed_all.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bec477884eb21f2308004730a4d57eb2fe9cd054 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/disallowed_all.yaml @@ -0,0 +1,26 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + initContainers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + containers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + ephemeralContainers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2dc3cd2467ae0e1605a402550144ba984f44a3d7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_allowed.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_allowed_images.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_allowed_images.yaml new file mode 100644 index 0000000000000000000000000000000000000000..623fddb7b444094833de7fae8073ee64b3c0563f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_allowed_images.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed +spec: + containers: + - name: image + image: ubuntu:20.14 + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4d5f3ba642a84a7112f622d25ccc602d45560721 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + initContainers: + - name: nginxinit + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + containers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml new file mode 100644 index 0000000000000000000000000000000000000000..face5057ec19fad07f75233ab3b45e67a72039b6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + containers: + - name: nginx + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_images.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_images.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cff761c7f1b23aa97c19e24c72797535379c7930 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_images.yaml @@ -0,0 +1,30 @@ +apiVersion: v1 +kind: Pod +metadata: + name: image-disallowed +spec: + containers: + - name: image-1-malicious-basic-image-disallow + image: ubuntumalicious + resources: + limits: + cpu: "100m" + memory: "30Mi" + - name: image-2-basic-image-allow + image: ubuntu:20.14 + resources: + limits: + cpu: "200m" + memory: "50Mi" + - name: image-3-malicious-image-with-registry-disallow + image: 123456789123.dkr.ecr.eu-west-1.amazonaws.com/postgresmalicious + resources: + limits: + cpu: "50m" + memory: "10Mi" + - name: image-4-image-with-registry-allow + image: 123456789123.dkr.ecr.eu-west-1.amazonaws.com/postgres + resources: + limits: + cpu: "50m" + memory: "10Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6f7a886c6d08e572bfe20bb873fbbe6e361064ea --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-disallowed +spec: + initContainers: + - name: nginxinit + image: nginx + resources: + limits: + cpu: "100m" + memory: "30Mi" + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_registry_and_repository.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_registry_and_repository.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7af13ad59a605491ab9fcdee58ba66749bc0a445 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/samples/repo-must-be-openpolicyagent/example_disallowed_registry_and_repository.yaml @@ -0,0 +1,30 @@ +apiVersion: v1 +kind: Pod +metadata: + name: registry-repository-disallowed +spec: + containers: + - name: image-1-malicious-registry-disallow + image: myregistry.azurecr.io.malicious.com/malicious-image + resources: + limits: + cpu: "100m" + memory: "30Mi" + - name: image-2-registry-allow + image: myregistry.azurecr.io/nginx + resources: + limits: + cpu: "200m" + memory: "50Mi" + - name: image-3-malicious-image-with-registry-disallow + image: mydockerhubmalicious/python + resources: + limits: + cpu: "50m" + memory: "10Mi" + - name: image-4-image-with-registry-allow + image: mydockerhub/python + resources: + limits: + cpu: "50m" + memory: "10Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2183a0b7f0a1e94831e8a86f908e1bfe64389638 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/suite.yaml @@ -0,0 +1,57 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: allowedreposv2 +tests: + - name: allowed-reposv2 + template: template.yaml + constraint: samples/repo-must-be-openpolicyagent/constraint.yaml + cases: + - name: example-allowed + object: samples/repo-must-be-openpolicyagent/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-images + object: samples/repo-must-be-openpolicyagent/example_allowed_images.yaml + assertions: + - violations: no + - name: container-disallowed + object: samples/repo-must-be-openpolicyagent/example_disallowed_container.yaml + assertions: + - violations: yes + message: container + - name: initcontainer-disallowed + object: samples/repo-must-be-openpolicyagent/example_disallowed_initcontainer.yaml + assertions: + - violations: 1 + message: initContainer + - violations: 0 + message: container + - name: both-disallowed + object: samples/repo-must-be-openpolicyagent/example_disallowed_both.yaml + assertions: + - violations: 2 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - name: all-disallowed + object: samples/repo-must-be-openpolicyagent/disallowed_all.yaml + assertions: + - violations: 3 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - message: ephemeralContainer + violations: 1 + - name: disallowed-repository-and-registry + object: samples/repo-must-be-openpolicyagent/example_disallowed_registry_and_repository.yaml + assertions: + - violations: 2 + - message: container + - name: disallowed-repository-images + object: samples/repo-must-be-openpolicyagent/example_disallowed_images.yaml + assertions: + - violations: 2 + - message: container diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dd8fa9dc3670c89bc97e72f562b320c0c0376646 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/allowedreposv2/template.yaml @@ -0,0 +1,63 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sallowedreposv2 + annotations: + metadata.gatekeeper.sh/title: "Allowed Images" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + This policy enforces that container images must begin with a string from a specified list. + The updated version, K8sAllowedReposv2, introduces support for exact match and glob-like syntax to enhance security: + 1. Exact Match: By default, if the * character is not specified, the policy strictly checks for an exact match of the full registry, repository, and/or the image name. + 2. Glob-like Syntax: Adding * at the end of a prefix allows prefix-based matching (e.g., registry.example.com/project/*). Only the * wildcard at the end of a string is supported. + 3. Security Note: To avoid bypasses scenarios, ensure prefixes include a trailing / where appropriate (e.g., registry.example.com/project/*). +spec: + crd: + spec: + names: + kind: K8sAllowedReposv2 + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + properties: + allowedImages: + description: A list of allowed container image prefixes. Supports exact matches and prefixes ending with '*'. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sallowedreposv2 + + violation[{"msg": msg}] { + container := input.review.object.spec.containers[_] + not image_matches(container.image, input.parameters.allowedImages) + msg := sprintf("container <%v> has an invalid image <%v>, allowed images are %v", [container.name, container.image, input.parameters.allowedImages]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.initContainers[_] + not image_matches(container.image, input.parameters.allowedImages) + msg := sprintf("initContainer <%v> has an invalid image <%v>, allowed images are %v", [container.name, container.image, input.parameters.allowedImages]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.ephemeralContainers[_] + not image_matches(container.image, input.parameters.allowedImages) + msg := sprintf("ephemeralContainer <%v> has an invalid image <%v>, allowed images are %v", [container.name, container.image, input.parameters.allowedImages]) + } + + image_matches(image, images) { + i_image := images[_] # Iterate through all images in the allowed list + not endswith(i_image, "*") # Check for exact match if the image does not end with * + i_image == image + } + + image_matches(image, images) { + i_image := images[_] # Iterate through all images in the allowed list + endswith(i_image, "*") # Check for prefix match if the image ends with * + prefix := trim_suffix(i_image, "*") + startswith(image, prefix) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1f70295fd675695bc60e64c20fb75413f408be0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/constraint.yaml @@ -0,0 +1,10 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPAutomountServiceAccountTokenPod +metadata: + name: psp-automount-serviceaccount-token-pod +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + excludedNamespaces: ["kube-system"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7ceba9c34eeb829a8c4bb905a395711b5ada0b3d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/example_allowed.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-automountserviceaccounttoken-allowed + labels: + app: nginx-not-automountserviceaccounttoken +spec: + automountServiceAccountToken: false + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6184264eceb59f6e694651be6249b986050a20ab --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/example_disallowed.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-automountserviceaccounttoken-disallowed + labels: + app: nginx-automountserviceaccounttoken +spec: + automountServiceAccountToken: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0e9030f27974a1c006beb1811b78ba651f5bbd8e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/samples/automount-serviceaccount-token/update.yaml @@ -0,0 +1,16 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-automountserviceaccounttoken-update + labels: + app: nginx-automountserviceaccounttoken + spec: + automountServiceAccountToken: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cf257d08f00a7901808867a890791f2bc5363dc8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: automount-serviceaccount-token +tests: + - name: automount-serviceaccount-token + template: template.yaml + constraint: samples/automount-serviceaccount-token/constraint.yaml + cases: + - name: example-allowed + object: samples/automount-serviceaccount-token/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/automount-serviceaccount-token/example_disallowed.yaml + assertions: + - violations: yes + - name: update + object: samples/automount-serviceaccount-token/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..82e69ff9c5200cad92d2a7360216dba61f123f83 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/automount-serviceaccount-token/template.yaml @@ -0,0 +1,66 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspautomountserviceaccounttokenpod + annotations: + metadata.gatekeeper.sh/title: "Automount Service Account Token for Pod" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Controls the ability of any Pod to enable automountServiceAccountToken. +spec: + crd: + spec: + names: + kind: K8sPSPAutomountServiceAccountTokenPod + validation: + openAPIV3Schema: + type: object + description: >- + Controls the ability of any Pod to enable automountServiceAccountToken. + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sautomountserviceaccounttoken + + import data.lib.exclude_update.is_update + + violation[{"msg": msg}] { + # spec.automountServiceAccountToken and spec.containers.volumeMounts fields are immutable. + not is_update(input.review) + + obj := input.review.object + mountServiceAccountToken(obj.spec) + msg := sprintf("Automounting service account token is disallowed, pod: %v", [obj.metadata.name]) + } + + mountServiceAccountToken(spec) { + spec.automountServiceAccountToken == true + } + + # if there is no automountServiceAccountToken spec, check on volumeMount in containers. Service Account token is mounted on /var/run/secrets/kubernetes.io/serviceaccount + # https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/#serviceaccount-admission-controller + mountServiceAccountToken(spec) { + not has_key(spec, "automountServiceAccountToken") + "/var/run/secrets/kubernetes.io/serviceaccount" == input_containers[_].volumeMounts[_].mountPath + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + } + + input_containers[c] { + c := input.review.object.spec.initContainers[_] + } + + # Ephemeral containers not checked as it is not possible to set field. + + has_key(x, k) { + _ = x[k] + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..afb69dc5d3a447312d6129d37a313300782c197a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sBlockEndpointEditDefaultRole +metadata: + name: block-endpoint-edit-default-role +spec: + match: + kinds: + - apiGroups: ["rbac.authorization.k8s.io"] + kinds: ["ClusterRole"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3390770c2085cff81039affeeb719c11480bee22 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/example_allowed.yaml @@ -0,0 +1,138 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + annotations: + rbac.authorization.kubernetes.io/autoupdate: "true" + creationTimestamp: null + labels: + kubernetes.io/bootstrapping: rbac-defaults + rbac.authorization.k8s.io/aggregate-to-edit: "true" + name: system:aggregate-to-edit +rules: +- apiGroups: + - "" + resources: + - pods/attach + - pods/exec + - pods/portforward + - pods/proxy + - secrets + - services/proxy + verbs: + - get + - list + - watch +- apiGroups: + - "" + resources: + - serviceaccounts + verbs: + - impersonate +- apiGroups: + - "" + resources: + - pods + - pods/attach + - pods/exec + - pods/portforward + - pods/proxy + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - "" + resources: + - configmaps + - persistentvolumeclaims + - replicationcontrollers + - replicationcontrollers/scale + - secrets + - serviceaccounts + - services + - services/proxy + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - apps + resources: + - daemonsets + - deployments + - deployments/rollback + - deployments/scale + - replicasets + - replicasets/scale + - statefulsets + - statefulsets/scale + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - autoscaling + resources: + - horizontalpodautoscalers + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - batch + resources: + - cronjobs + - jobs + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - extensions + resources: + - daemonsets + - deployments + - deployments/rollback + - deployments/scale + - ingresses + - networkpolicies + - replicasets + - replicasets/scale + - replicationcontrollers/scale + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - policy + resources: + - poddisruptionbudgets + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - networking.k8s.io + resources: + - ingresses + - networkpolicies + verbs: + - create + - delete + - deletecollection + - patch + - update diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..65755efe68f83a9a1bc1d6af7f56edfbda05ec8f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/samples/block-endpoint-edit-default-role/example_disallowed.yaml @@ -0,0 +1,79 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + annotations: + rbac.authorization.kubernetes.io/autoupdate: "true" + creationTimestamp: null + labels: + kubernetes.io/bootstrapping: rbac-defaults + rbac.authorization.k8s.io/aggregate-to-edit: "true" + name: system:aggregate-to-edit +rules: +- apiGroups: + - "" + resources: + - pods/attach + - pods/exec + - pods/portforward + - pods/proxy + - secrets + - services/proxy + verbs: + - get + - list + - watch +- apiGroups: + - "" + resources: + - serviceaccounts + verbs: + - impersonate +- apiGroups: + - "" + resources: + - pods + - pods/attach + - pods/exec + - pods/portforward + - pods/proxy + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - "" + resources: + - configmaps + - persistentvolumeclaims + - replicationcontrollers + - replicationcontrollers/scale + - secrets + - serviceaccounts + - services + - services/proxy + verbs: + - create + - delete + - deletecollection + - patch + - update +- apiGroups: + - apps + resources: + - daemonsets + - deployments + - deployments/rollback + - deployments/scale + - endpoints + - replicasets + - replicasets/scale + - statefulsets + - statefulsets/scale + verbs: + - create + - delete + - deletecollection + - patch + - update diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..632c48ee78e4e058e8fa87880b32c24dde506d80 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: block-endpoint-edit-default-role +tests: +- name: block-endpoint-default-role + template: template.yaml + constraint: samples/block-endpoint-edit-default-role/constraint.yaml + cases: + - name: example-allowed + object: samples/block-endpoint-edit-default-role/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/block-endpoint-edit-default-role/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5e4b3b1621da8322bcc747f8f135dc23ad71726c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-endpoint-edit-default-role/template.yaml @@ -0,0 +1,49 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sblockendpointeditdefaultrole + annotations: + metadata.gatekeeper.sh/title: "Block Endpoint Edit Default Role" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Many Kubernetes installations by default have a system:aggregate-to-edit + ClusterRole which does not properly restrict access to editing Endpoints. + This ConstraintTemplate forbids the system:aggregate-to-edit ClusterRole + from granting permission to create/patch/update Endpoints. + + ClusterRole/system:aggregate-to-edit should not allow + Endpoint edit permissions due to CVE-2021-25740, Endpoint & EndpointSlice + permissions allow cross-Namespace forwarding, + https://github.com/kubernetes/kubernetes/issues/103675 +spec: + crd: + spec: + names: + kind: K8sBlockEndpointEditDefaultRole + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sblockendpointeditdefaultrole + + violation[{"msg": msg}] { + input.review.object.metadata.name == "system:aggregate-to-edit" + endpointRule(input.review.object.rules[_]) + msg := "ClusterRole system:aggregate-to-edit should not allow endpoint edit permissions. For k8s version < 1.22, the Cluster Role should be annotated with rbac.authorization.kubernetes.io/autoupdate=false to prevent autoreconciliation back to default permissions for this role." + } + + endpointRule(rule) { + "endpoints" == rule.resources[_] + hasEditVerb(rule.verbs) + } + + hasEditVerb(verbs) { + "create" == verbs[_] + } + + hasEditVerb(verbs) { + "patch" == verbs[_] + } + + hasEditVerb(verbs) { + "update" == verbs[_] + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5d7cef2a33afe02b9b6f7320058f373bb89cb5c1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sBlockLoadBalancer +metadata: + name: block-load-balancer +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Service"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3de12200afa3a1735dcdff283d474fd364e77f7f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/example_allowed.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Service +metadata: + name: my-service-allowed +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fe2484a9b01d833e2c99e3c942f88e94f305b314 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/samples/block-load-balancer/example_disallowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: my-service-disallowed +spec: + type: LoadBalancer + ports: + - port: 80 + targetPort: 80 + nodePort: 30007 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f5c46366b916b82c36cf8a0f353dc1f3ed07d2d5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: block-loadbalancer-services +tests: +- name: block-loadbalancer-services + template: template.yaml + constraint: samples/block-load-balancer/constraint.yaml + cases: + - name: example-allowed + object: samples/block-load-balancer/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/block-load-balancer/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6a0ce7847db8508f1ff64cbc01b2826095fa2622 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-loadbalancer-services/template.yaml @@ -0,0 +1,26 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sblockloadbalancer + annotations: + metadata.gatekeeper.sh/title: "Block Services with type LoadBalancer" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Disallows all Services with type LoadBalancer. + + https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer +spec: + crd: + spec: + names: + kind: K8sBlockLoadBalancer + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sblockloadbalancer + + violation[{"msg": msg}] { + input.review.kind.kind == "Service" + input.review.object.spec.type == "LoadBalancer" + msg := "User is not allowed to create service of type LoadBalancer" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/samples/block-node-port/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/samples/block-node-port/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8606f130e4b2183a20c0cad16d1d94fe3a8c2fe6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/samples/block-node-port/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sBlockNodePort +metadata: + name: block-node-port +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Service"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/samples/block-node-port/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/samples/block-node-port/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bbe2cc1ae219ae3964f991695c0428d504fe3f11 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/samples/block-node-port/example_disallowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: my-service-disallowed +spec: + type: NodePort + ports: + - port: 80 + targetPort: 80 + nodePort: 30007 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..056ffaf84a7b514b14fa1ed172798dbce399ac8a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/suite.yaml @@ -0,0 +1,16 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: block-nodeport-services +tests: +- name: block-nodeport-services + template: template.yaml + constraint: samples/block-node-port/constraint.yaml + cases: +# TODO: Add allowed example. +# - name: example-allowed +# object: samples/block-node-port/example_allowed.yaml + - name: example-disallowed + object: samples/block-node-port/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1bd74f30bad4ce124d500dd28f24d1a707d78b34 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-nodeport-services/template.yaml @@ -0,0 +1,26 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sblocknodeport + annotations: + metadata.gatekeeper.sh/title: "Block NodePort" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Disallows all Services with type NodePort. + + https://kubernetes.io/docs/concepts/services-networking/service/#nodeport +spec: + crd: + spec: + names: + kind: K8sBlockNodePort + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sblocknodeport + + violation[{"msg": msg}] { + input.review.kind.kind == "Service" + input.review.object.spec.type == "NodePort" + msg := "User is not allowed to create service of type NodePort" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a4dc827dc7bee1a0ad1ee51790232f3dc8038ce --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sBlockWildcardIngress +metadata: + name: block-wildcard-ingress +spec: + match: + kinds: + - apiGroups: ["extensions", "networking.k8s.io"] + kinds: ["Ingress"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/blank_host.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/blank_host.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f3c420607c30517aeaf9379c8d5689b8e7b8acd1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/blank_host.yaml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: wildcard-ingress +spec: + rules: + - host: '' + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: example + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/host_omitted.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/host_omitted.yaml new file mode 100644 index 0000000000000000000000000000000000000000..18e303849413a387481682041e013cecc9bd8468 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/host_omitted.yaml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: wildcard-ingress +spec: + rules: + # Omitted host field counts as a wildcard too + - http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: example + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/wildcard_host.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/wildcard_host.yaml new file mode 100644 index 0000000000000000000000000000000000000000..463049057d50acdf4eb904c549f71a151cb832a6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/disallowed/wildcard_host.yaml @@ -0,0 +1,27 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: wildcard-ingress +spec: + rules: + - host: '*.example.com' + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: example + port: + number: 80 + # Extra test to ensure the rule still detects invalid hosts in files containing valid hosts + - host: 'valid.example.com' + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: example + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ae3e19d77490329974a16b5d73d96eb5e9e38b4f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/samples/block-wildcard-ingress/example_allowed.yaml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: non-wildcard-ingress +spec: + rules: + - host: 'myservice.example.com' + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: example + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c15f0e1ce298485ea25975d1b33fd41c1fa245c3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: block-wildcard-ingress +tests: +- name: block-wildcard-ingress + template: template.yaml + constraint: samples/block-wildcard-ingress/constraint.yaml + cases: + - name: example-allowed + object: samples/block-wildcard-ingress/example_allowed.yaml + assertions: + - violations: 0 + - name: blank-host + object: samples/block-wildcard-ingress/disallowed/blank_host.yaml + assertions: + - violations: 1 + - name: host-omitted + object: samples/block-wildcard-ingress/disallowed/host_omitted.yaml + assertions: + - violations: 1 + - name: wildcard-host + object: samples/block-wildcard-ingress/disallowed/wildcard_host.yaml + assertions: + - violations: 1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a6301bae2b72e9c86496bdaf46bd0dc7c79d451f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/block-wildcard-ingress/template.yaml @@ -0,0 +1,34 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sblockwildcardingress + annotations: + metadata.gatekeeper.sh/title: "Block Wildcard Ingress" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Users should not be able to create Ingresses with a blank or wildcard (*) hostname since that would enable them to intercept traffic for other services in the cluster, even if they don't have access to those services. +spec: + crd: + spec: + names: + kind: K8sBlockWildcardIngress + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package K8sBlockWildcardIngress + + contains_wildcard(hostname) = true { + hostname == "" + } + + contains_wildcard(hostname) = true { + contains(hostname, "*") + } + + violation[{"msg": msg}] { + input.review.kind.kind == "Ingress" + # object.get is required to detect omitted host fields + hostname := object.get(input.review.object.spec.rules[_], "host", "") + contains_wildcard(hostname) + msg := sprintf("Hostname '%v' is not allowed since it counts as a wildcard, which can be used to intercept traffic from other applications.", [hostname]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..edeea3e416152a9efca01ab73a2f1948c4bef760 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sContainerLimits +metadata: + name: container-must-have-limits +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + cpu: "-1" + memory: "1Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0b82857318b7e02c69a4ec8130ca92d5811ce252 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/example_allowed.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + memory: "1Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..31218680a689cc5b9cd671ea313e2470dad08567 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-ignore-cpu-limits/example_disallowed.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + memory: "2Gi" \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2b06fd8d18a55b369ef197b2b67854ff706172bc --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sContainerLimits +metadata: + name: container-must-have-limits +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + cpu: "200m" + memory: "1Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c77c3668f0430087425c56d4d34ea7c9f4ac946c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/example_allowed.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a5bd508a7c036e5619bf6de93fa9d4fb1d57987a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/samples/container-must-have-limits/example_disallowed.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "2Gi" \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e7bda65cdfed7c9d57584aa177335850f3262e00 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: containerlimits +tests: +- name: container-limits + template: template.yaml + constraint: samples/container-must-have-limits/constraint.yaml + cases: + - name: example-allowed + object: samples/container-must-have-limits/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/container-must-have-limits/example_disallowed.yaml + assertions: + - violations: yes +- name: container-limits-ignore-cpu + template: template.yaml + constraint: samples/container-ignore-cpu-limits/constraint.yaml + cases: + - name: example-allowed + object: samples/container-ignore-cpu-limits/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/container-ignore-cpu-limits/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c9b2838afacf561c9bdfa8ca9b52ccdd90473d46 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerlimits/template.yaml @@ -0,0 +1,267 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8scontainerlimits + annotations: + metadata.gatekeeper.sh/title: "Container Limits" + metadata.gatekeeper.sh/version: 1.1.0 + description: >- + Requires containers to have memory and CPU limits set and constrains + limits to be within the specified maximum values. + + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ +spec: + crd: + spec: + names: + kind: K8sContainerLimits + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + cpu: + description: "The maximum allowed cpu limit on a Pod, exclusive. Set to -1 to disable." + type: string + memory: + description: "The maximum allowed memory limit on a Pod, exclusive." + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8scontainerlimits + + import data.lib.exempt_container.is_exempt + + missing(obj, field) = true { + not obj[field] + } + + missing(obj, field) = true { + obj[field] == "" + } + + canonify_cpu(orig) = new { + is_number(orig) + new := orig * 1000 + } + + canonify_cpu(orig) = new { + not is_number(orig) + endswith(orig, "m") + new := to_number(replace(orig, "m", "")) + } + + canonify_cpu(orig) = new { + not is_number(orig) + not endswith(orig, "m") + regex.match("^[0-9]+(\\.[0-9]+)?$", orig) + new := to_number(orig) * 1000 + } + + # 10 ** 21 + mem_multiple("E") = 1000000000000000000000 { true } + + # 10 ** 18 + mem_multiple("P") = 1000000000000000000 { true } + + # 10 ** 15 + mem_multiple("T") = 1000000000000000 { true } + + # 10 ** 12 + mem_multiple("G") = 1000000000000 { true } + + # 10 ** 9 + mem_multiple("M") = 1000000000 { true } + + # 10 ** 6 + mem_multiple("k") = 1000000 { true } + + # 10 ** 3 + mem_multiple("") = 1000 { true } + + # Kubernetes accepts millibyte precision when it probably shouldn't. + # https://github.com/kubernetes/kubernetes/issues/28741 + # 10 ** 0 + mem_multiple("m") = 1 { true } + + # 1000 * 2 ** 10 + mem_multiple("Ki") = 1024000 { true } + + # 1000 * 2 ** 20 + mem_multiple("Mi") = 1048576000 { true } + + # 1000 * 2 ** 30 + mem_multiple("Gi") = 1073741824000 { true } + + # 1000 * 2 ** 40 + mem_multiple("Ti") = 1099511627776000 { true } + + # 1000 * 2 ** 50 + mem_multiple("Pi") = 1125899906842624000 { true } + + # 1000 * 2 ** 60 + mem_multiple("Ei") = 1152921504606846976000 { true } + + get_suffix(mem) = suffix { + not is_string(mem) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 0 + suffix := substring(mem, count(mem) - 1, -1) + mem_multiple(suffix) + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 1 + suffix := substring(mem, count(mem) - 2, -1) + mem_multiple(suffix) + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 1 + not mem_multiple(substring(mem, count(mem) - 1, -1)) + not mem_multiple(substring(mem, count(mem) - 2, -1)) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) == 1 + not mem_multiple(substring(mem, count(mem) - 1, -1)) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) == 0 + suffix := "" + } + + canonify_mem(orig) = new { + is_number(orig) + new := orig * 1000 + } + + canonify_mem(orig) = new { + not is_number(orig) + suffix := get_suffix(orig) + raw := replace(orig, suffix, "") + regex.match("^[0-9]+(\\.[0-9]+)?$", raw) + new := to_number(raw) * mem_multiple(suffix) + } + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "containers"}] + } + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "initContainers"}] + } + + # Ephemeral containers not checked as it is not possible to set field. + + general_violation[{"msg": msg, "field": field}] { + input.parameters.cpu != "-1" + container := input.review.object.spec[field][_] + not is_exempt(container) + cpu_orig := container.resources.limits.cpu + not canonify_cpu(cpu_orig) + msg := sprintf("container <%v> cpu limit <%v> could not be parsed", [container.name, cpu_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + mem_orig := container.resources.limits.memory + not canonify_mem(mem_orig) + msg := sprintf("container <%v> memory limit <%v> could not be parsed", [container.name, mem_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources + msg := sprintf("container <%v> has no resource limits", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources.limits + msg := sprintf("container <%v> has no resource limits", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + input.parameters.cpu != "-1" + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.limits, "cpu") + msg := sprintf("container <%v> has no cpu limit", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.limits, "memory") + msg := sprintf("container <%v> has no memory limit", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + cpu_orig := container.resources.limits.cpu + cpu := canonify_cpu(cpu_orig) + max_cpu_orig := input.parameters.cpu + max_cpu_orig != "-1" + max_cpu := canonify_cpu(max_cpu_orig) + cpu > max_cpu + msg := sprintf("container <%v> cpu limit <%v> is higher than the maximum allowed of <%v>", [container.name, cpu_orig, max_cpu_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + mem_orig := container.resources.limits.memory + mem := canonify_mem(mem_orig) + max_mem_orig := input.parameters.memory + max_mem := canonify_mem(max_mem_orig) + mem > max_mem + msg := sprintf("container <%v> memory limit <%v> is higher than the maximum allowed of <%v>", [container.name, mem_orig, max_mem_orig]) + } + libs: + - | + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1bcc3572f71fc1014700abadf2a16978bd7293a9 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sContainerRequests +metadata: + name: container-must-have-requests +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + cpu: "200m" + memory: "1Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..48af2a4b3af1397ab7b7575db03cb3ea6a810c9c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/example_allowed.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + requests: + cpu: "100m" + memory: "1Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..126b8631b7ced4e7db4da17debb8caace0ee7bde --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/samples/container-must-have-requests/example_disallowed.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + requests: + cpu: "100m" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1c4b224eba16498ebce5e763d038d67d751a2770 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: containerrequests +tests: +- name: container-requests + template: template.yaml + constraint: samples/container-must-have-requests/constraint.yaml + cases: + - name: example-allowed + object: samples/container-must-have-requests/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/container-must-have-requests/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a67e8b786caa425fbfd94b9e0e26c0f872cb2b4b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerrequests/template.yaml @@ -0,0 +1,264 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8scontainerrequests + annotations: + metadata.gatekeeper.sh/title: "Container Requests" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Requires containers to have memory and CPU requests set and constrains + requests to be within the specified maximum values. + + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ +spec: + crd: + spec: + names: + kind: K8sContainerRequests + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + cpu: + description: "The maximum allowed cpu request on a Pod, exclusive." + type: string + memory: + description: "The maximum allowed memory request on a Pod, exclusive." + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8scontainerrequests + + import data.lib.exempt_container.is_exempt + + missing(obj, field) = true { + not obj[field] + } + + missing(obj, field) = true { + obj[field] == "" + } + + canonify_cpu(orig) = new { + is_number(orig) + new := orig * 1000 + } + + canonify_cpu(orig) = new { + not is_number(orig) + endswith(orig, "m") + new := to_number(replace(orig, "m", "")) + } + + canonify_cpu(orig) = new { + not is_number(orig) + not endswith(orig, "m") + regex.match("^[0-9]+(\\.[0-9]+)?$", orig) + new := to_number(orig) * 1000 + } + + # 10 ** 21 + mem_multiple("E") = 1000000000000000000000 { true } + + # 10 ** 18 + mem_multiple("P") = 1000000000000000000 { true } + + # 10 ** 15 + mem_multiple("T") = 1000000000000000 { true } + + # 10 ** 12 + mem_multiple("G") = 1000000000000 { true } + + # 10 ** 9 + mem_multiple("M") = 1000000000 { true } + + # 10 ** 6 + mem_multiple("k") = 1000000 { true } + + # 10 ** 3 + mem_multiple("") = 1000 { true } + + # Kubernetes accepts millibyte precision when it probably shouldn't. + # https://github.com/kubernetes/kubernetes/issues/28741 + # 10 ** 0 + mem_multiple("m") = 1 { true } + + # 1000 * 2 ** 10 + mem_multiple("Ki") = 1024000 { true } + + # 1000 * 2 ** 20 + mem_multiple("Mi") = 1048576000 { true } + + # 1000 * 2 ** 30 + mem_multiple("Gi") = 1073741824000 { true } + + # 1000 * 2 ** 40 + mem_multiple("Ti") = 1099511627776000 { true } + + # 1000 * 2 ** 50 + mem_multiple("Pi") = 1125899906842624000 { true } + + # 1000 * 2 ** 60 + mem_multiple("Ei") = 1152921504606846976000 { true } + + get_suffix(mem) = suffix { + not is_string(mem) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 0 + suffix := substring(mem, count(mem) - 1, -1) + mem_multiple(suffix) + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 1 + suffix := substring(mem, count(mem) - 2, -1) + mem_multiple(suffix) + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 1 + not mem_multiple(substring(mem, count(mem) - 1, -1)) + not mem_multiple(substring(mem, count(mem) - 2, -1)) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) == 1 + not mem_multiple(substring(mem, count(mem) - 1, -1)) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) == 0 + suffix := "" + } + + canonify_mem(orig) = new { + is_number(orig) + new := orig * 1000 + } + + canonify_mem(orig) = new { + not is_number(orig) + suffix := get_suffix(orig) + raw := replace(orig, suffix, "") + regex.match("^[0-9]+(\\.[0-9]+)?$", raw) + new := to_number(raw) * mem_multiple(suffix) + } + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "containers"}] + } + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "initContainers"}] + } + + # Ephemeral containers not checked as it is not possible to set field. + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + cpu_orig := container.resources.requests.cpu + not canonify_cpu(cpu_orig) + msg := sprintf("container <%v> cpu request <%v> could not be parsed", [container.name, cpu_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + mem_orig := container.resources.requests.memory + not canonify_mem(mem_orig) + msg := sprintf("container <%v> memory request <%v> could not be parsed", [container.name, mem_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources + msg := sprintf("container <%v> has no resource requests", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources.requests + msg := sprintf("container <%v> has no resource requests", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.requests, "cpu") + msg := sprintf("container <%v> has no cpu request", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.requests, "memory") + msg := sprintf("container <%v> has no memory request", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + cpu_orig := container.resources.requests.cpu + cpu := canonify_cpu(cpu_orig) + max_cpu_orig := input.parameters.cpu + max_cpu := canonify_cpu(max_cpu_orig) + cpu > max_cpu + msg := sprintf("container <%v> cpu request <%v> is higher than the maximum allowed of <%v>", [container.name, cpu_orig, max_cpu_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + mem_orig := container.resources.requests.memory + mem := canonify_mem(mem_orig) + max_mem_orig := input.parameters.memory + max_mem := canonify_mem(max_mem_orig) + mem > max_mem + msg := sprintf("container <%v> memory request <%v> is higher than the maximum allowed of <%v>", [container.name, mem_orig, max_mem_orig]) + } + libs: + - | + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c49c5bd2cb648fc6defe8d58fabe5775cb3ce8fa --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sContainerRatios +metadata: + name: container-must-meet-memory-and-cpu-ratio +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + ratio: "1" + cpuRatio: "10" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..700786f9ee0077e7a7e410cd168c723f01596912 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/example_allowed.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "4" + memory: "2Gi" + requests: + cpu: "1" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bf8b3e87ce05d3dd861f38da9badff3922b68d36 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-memory-and-cpu-ratio/example_disallowed.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "4" + memory: "2Gi" + requests: + cpu: "100m" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..db7ac53e6d1bebcff84b4c0204ff79cd296e87e0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sContainerRatios +metadata: + name: container-must-meet-ratio +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + ratio: "2" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..eb8ba6860fdec9aa8b361adf9c0f8368edbb5073 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/example_allowed.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "200m" + memory: "200Mi" + requests: + cpu: "100m" + memory: "100Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4b3b1271553b3171ecd5d623dd0729a6e407cce2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/samples/container-must-meet-ratio/example_disallowed.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "800m" + memory: "2Gi" + requests: + cpu: "100m" + memory: "100Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8df0686b7ef9e5451e509fad26d42dfa806f1f8e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: containerresourceratios +tests: + - name: memory-ratio-only + template: template.yaml + constraint: samples/container-must-meet-ratio/constraint.yaml + cases: + - name: example-allowed + object: samples/container-must-meet-ratio/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/container-must-meet-ratio/example_disallowed.yaml + assertions: + - violations: yes + - name: memory-and-cpu-ratios + template: template.yaml + constraint: samples/container-must-meet-memory-and-cpu-ratio/constraint.yaml + cases: + - name: example-allowed + object: samples/container-must-meet-memory-and-cpu-ratio/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/container-must-meet-memory-and-cpu-ratio/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..94a9753aa22a3f13e6864b8e3c2400259cf4b6b9 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresourceratios/template.yaml @@ -0,0 +1,313 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8scontainerratios + annotations: + metadata.gatekeeper.sh/title: "Container Ratios" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Sets a maximum ratio for container resource limits to requests. + + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ +spec: + crd: + spec: + names: + kind: K8sContainerRatios + validation: + openAPIV3Schema: + type: object + 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 + ratio: + type: string + description: >- + The maximum allowed ratio of `resources.limits` to + `resources.requests` on a container. + cpuRatio: + type: string + description: >- + The maximum allowed ratio of `resources.limits.cpu` to + `resources.requests.cpu` on a container. If not specified, + equal to `ratio`. + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8scontainerratios + + import data.lib.exempt_container.is_exempt + + missing(obj, field) = true { + not obj[field] + } + + missing(obj, field) = true { + obj[field] == "" + } + + canonify_cpu(orig) = new { + is_number(orig) + new := orig * 1000 + } + + canonify_cpu(orig) = new { + not is_number(orig) + endswith(orig, "m") + new := to_number(replace(orig, "m", "")) + } + + canonify_cpu(orig) = new { + not is_number(orig) + not endswith(orig, "m") + regex.match("^[0-9]+$", orig) + new := to_number(orig) * 1000 + } + + canonify_cpu(orig) = new { + not is_number(orig) + not endswith(orig, "m") + regex.match("^[0-9]+[.][0-9]+$", orig) + new := to_number(orig) * 1000 + } + + # 10 ** 21 + mem_multiple("E") = 1000000000000000000000 { true } + + # 10 ** 18 + mem_multiple("P") = 1000000000000000000 { true } + + # 10 ** 15 + mem_multiple("T") = 1000000000000000 { true } + + # 10 ** 12 + mem_multiple("G") = 1000000000000 { true } + + # 10 ** 9 + mem_multiple("M") = 1000000000 { true } + + # 10 ** 6 + mem_multiple("k") = 1000000 { true } + + # 10 ** 3 + mem_multiple("") = 1000 { true } + + # Kubernetes accepts millibyte precision when it probably shouldn't. + # https://github.com/kubernetes/kubernetes/issues/28741 + # 10 ** 0 + mem_multiple("m") = 1 { true } + + # 1000 * 2 ** 10 + mem_multiple("Ki") = 1024000 { true } + + # 1000 * 2 ** 20 + mem_multiple("Mi") = 1048576000 { true } + + # 1000 * 2 ** 30 + mem_multiple("Gi") = 1073741824000 { true } + + # 1000 * 2 ** 40 + mem_multiple("Ti") = 1099511627776000 { true } + + # 1000 * 2 ** 50 + mem_multiple("Pi") = 1125899906842624000 { true } + + # 1000 * 2 ** 60 + mem_multiple("Ei") = 1152921504606846976000 { true } + + get_suffix(mem) = suffix { + not is_string(mem) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 0 + suffix := substring(mem, count(mem) - 1, -1) + mem_multiple(suffix) + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 1 + suffix := substring(mem, count(mem) - 2, -1) + mem_multiple(suffix) + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) > 1 + not mem_multiple(substring(mem, count(mem) - 1, -1)) + not mem_multiple(substring(mem, count(mem) - 2, -1)) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) == 1 + not mem_multiple(substring(mem, count(mem) - 1, -1)) + suffix := "" + } + + get_suffix(mem) = suffix { + is_string(mem) + count(mem) == 0 + suffix := "" + } + + canonify_mem(orig) = new { + is_number(orig) + new := orig * 1000 + } + + canonify_mem(orig) = new { + not is_number(orig) + suffix := get_suffix(orig) + raw := replace(orig, suffix, "") + regex.match("^[0-9]+(\\.[0-9]+)?$", raw) + new := to_number(raw) * mem_multiple(suffix) + } + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "containers"}] + } + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "initContainers"}] + } + + # Ephemeral containers not checked as it is not possible to set field. + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + cpu_orig := container.resources.limits.cpu + not canonify_cpu(cpu_orig) + msg := sprintf("container <%v> cpu limit <%v> could not be parsed", [container.name, cpu_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + mem_orig := container.resources.limits.memory + not canonify_mem(mem_orig) + msg := sprintf("container <%v> memory limit <%v> could not be parsed", [container.name, mem_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + cpu_orig := container.resources.requests.cpu + not canonify_cpu(cpu_orig) + msg := sprintf("container <%v> cpu request <%v> could not be parsed", [container.name, cpu_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + mem_orig := container.resources.requests.memory + not canonify_mem(mem_orig) + msg := sprintf("container <%v> memory request <%v> could not be parsed", [container.name, mem_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources + msg := sprintf("container <%v> has no resource limits", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources.limits + msg := sprintf("container <%v> has no resource limits", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.limits, "cpu") + msg := sprintf("container <%v> has no cpu limit", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.limits, "memory") + msg := sprintf("container <%v> has no memory limit", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources.requests + msg := sprintf("container <%v> has no resource requests", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.requests, "cpu") + msg := sprintf("container <%v> has no cpu request", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.requests, "memory") + msg := sprintf("container <%v> has no memory request", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + cpu_limits_orig := container.resources.limits.cpu + cpu_limits := canonify_cpu(cpu_limits_orig) + cpu_requests_orig := container.resources.requests.cpu + cpu_requests := canonify_cpu(cpu_requests_orig) + cpu_ratio := object.get(input.parameters, "cpuRatio", input.parameters.ratio) + to_number(cpu_limits) > to_number(cpu_ratio) * to_number(cpu_requests) + msg := sprintf("container <%v> cpu limit <%v> is higher than the maximum allowed ratio of <%v>", [container.name, cpu_limits_orig, cpu_ratio]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + mem_limits_orig := container.resources.limits.memory + mem_requests_orig := container.resources.requests.memory + mem_limits := canonify_mem(mem_limits_orig) + mem_requests := canonify_mem(mem_requests_orig) + mem_ratio := input.parameters.ratio + to_number(mem_limits) > to_number(mem_ratio) * to_number(mem_requests) + msg := sprintf("container <%v> memory limit <%v> is higher than the maximum allowed ratio of <%v>", [container.name, mem_limits_orig, mem_ratio]) + } + libs: + - | + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0b353a4e3b7e2b1f00929abcd2c9895223acfce8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredResources +metadata: + name: container-must-have-cpu-requests-memory-limits-and-requests +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + limits: + - memory + requests: + - cpu + - memory diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/empty-resources-disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/empty-resources-disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c1cc731aaee170e67ecc66d997ab5d808522c3ff --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/empty-resources-disallowed.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/limits-and-requests-defined-allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/limits-and-requests-defined-allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..186cc0c90e7d121368da290c7fe135855de54e70 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/limits-and-requests-defined-allowed.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + requests: + cpu: "100m" + memory: "1Gi" + diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-cpu-requests-and-memory-limits-and-requests-defined-allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-cpu-requests-and-memory-limits-and-requests-defined-allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1719a473986f903caebca06f878e2261e38c8418 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-cpu-requests-and-memory-limits-and-requests-defined-allowed.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + memory: "2Gi" + requests: + cpu: "100m" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-memory-limits-defined-disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-memory-limits-defined-disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..404c9bcf29890cd92edb5eb3c34ab37758f20291 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-memory-limits-defined-disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-requests-defined-disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-requests-defined-disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..126b8631b7ced4e7db4da17debb8caace0ee7bde --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-cpu-requests-memory-limits-and-requests/only-requests-defined-disallowed.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + requests: + cpu: "100m" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fc9ed7916c806603b0fd13e910bc076a92066545 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/constraint.yaml @@ -0,0 +1,16 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredResources +metadata: + name: container-must-have-limits-and-requests +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + limits: + - cpu + - memory + requests: + - cpu + - memory diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/limits-and-requests-defined-allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/limits-and-requests-defined-allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..186cc0c90e7d121368da290c7fe135855de54e70 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/limits-and-requests-defined-allowed.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + requests: + cpu: "100m" + memory: "1Gi" + diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-cpu-requests-and-memory-limits-defined-disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-cpu-requests-and-memory-limits-defined-disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c1252a740d45eec9a274c722bb359635d71e71eb --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-cpu-requests-and-memory-limits-defined-disallowed.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + requests: + cpu: "100m" + limits: + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-memory-limits-defined-disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-memory-limits-defined-disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..404c9bcf29890cd92edb5eb3c34ab37758f20291 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-memory-limits-defined-disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-requests-defined-disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-requests-defined-disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..126b8631b7ced4e7db4da17debb8caace0ee7bde --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/container-must-have-limits-and-requests/only-requests-defined-disallowed.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + requests: + cpu: "100m" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..272e123ebbf8da972a5329649588d2380b538a3d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredResources +metadata: + name: no-enforcements +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/empty-resources-allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/empty-resources-allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c1cc731aaee170e67ecc66d997ab5d808522c3ff --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/empty-resources-allowed.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/limits-and-requests-defined-allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/limits-and-requests-defined-allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..186cc0c90e7d121368da290c7fe135855de54e70 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/limits-and-requests-defined-allowed.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + requests: + cpu: "100m" + memory: "1Gi" + diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/only-cpu-requests-and-memory-limits-defined-allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/only-cpu-requests-and-memory-limits-defined-allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c1252a740d45eec9a274c722bb359635d71e71eb --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/only-cpu-requests-and-memory-limits-defined-allowed.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + requests: + cpu: "100m" + limits: + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/only-requests-defined-allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/only-requests-defined-allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..126b8631b7ced4e7db4da17debb8caace0ee7bde --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/samples/no-enforcements/only-requests-defined-allowed.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + requests: + cpu: "100m" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..758832feaf69834f73fc1a83626193f0b6b81043 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/suite.yaml @@ -0,0 +1,69 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: containerresources +tests: + - name: container-limits-and-requests + template: template.yaml + constraint: samples/container-must-have-limits-and-requests/constraint.yaml + cases: + - name: limits-and-requests-defined-allowed + object: samples/container-must-have-limits-and-requests/limits-and-requests-defined-allowed.yaml + assertions: + - violations: no + - name: only-requests-defined-disallowed + object: samples/container-must-have-limits-and-requests/only-requests-defined-disallowed.yaml + assertions: + - violations: yes + - name: only-cpu-requests-and-memory-limits-defined-disallowed + object: samples/container-must-have-limits-and-requests/only-cpu-requests-and-memory-limits-defined-disallowed.yaml + assertions: + - violations: yes + - name: only-memory-limits-defined-disallowed + object: samples/container-must-have-limits-and-requests/only-memory-limits-defined-disallowed.yaml + assertions: + - violations: yes + - name: container-cpu-requests-memory-limits-and-requests + template: template.yaml + constraint: samples/container-must-have-cpu-requests-memory-limits-and-requests/constraint.yaml + cases: + - name: limits-and-requests-defined-allowed + object: samples/container-must-have-cpu-requests-memory-limits-and-requests/limits-and-requests-defined-allowed.yaml + assertions: + - violations: no + - name: only-cpu-requests-and-memory-limits-and-requests-defined-allowed + object: samples/container-must-have-cpu-requests-memory-limits-and-requests/only-cpu-requests-and-memory-limits-and-requests-defined-allowed.yaml + assertions: + - violations: no + - name: only-requests-defined-disallowed + object: samples/container-must-have-cpu-requests-memory-limits-and-requests/only-requests-defined-disallowed.yaml + assertions: + - violations: yes + - name: only-memory-limits-defined-disallowed + object: samples/container-must-have-cpu-requests-memory-limits-and-requests/only-memory-limits-defined-disallowed.yaml + assertions: + - violations: yes + - name: empty-resources-disallowed + object: samples/container-must-have-cpu-requests-memory-limits-and-requests/empty-resources-disallowed.yaml + assertions: + - violations: yes + - name: no-enforcements + template: template.yaml + constraint: samples/no-enforcements/constraint.yaml + cases: + - name: limits-and-requests-defined-allowed + object: samples/no-enforcements/limits-and-requests-defined-allowed.yaml + assertions: + - violations: no + - name: only-requests-defined-allowed + object: samples/no-enforcements/only-requests-defined-allowed.yaml + assertions: + - violations: no + - name: only-cpu-requests-and-memory-limits-defined-allowed + object: samples/no-enforcements/only-cpu-requests-and-memory-limits-defined-allowed.yaml + assertions: + - violations: no + - name: empty-resources-allowed + object: samples/no-enforcements/empty-resources-allowed.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bace49ac9334ae361baee5be0c726249a7e1fb6c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/containerresources/template.yaml @@ -0,0 +1,102 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8srequiredresources + annotations: + metadata.gatekeeper.sh/title: "Required Resources" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Requires containers to have defined resources set. + + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ +spec: + crd: + spec: + names: + kind: K8sRequiredResources + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + limits: + type: array + description: "A list of limits that should be enforced (`cpu`, `memory`, or both)." + items: + type: string + enum: + - cpu + - memory + requests: + type: array + description: "A list of requests that should be enforced (`cpu`, `memory`, or both)." + items: + type: string + enum: + - cpu + - memory + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8srequiredresources + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "containers"}] + } + + violation[{"msg": msg}] { + general_violation[{"msg": msg, "field": "initContainers"}] + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + provided := {resource_type | container.resources.limits[resource_type]} + required := {resource_type | resource_type := input.parameters.limits[_]} + missing := required - provided + count(missing) > 0 + msg := sprintf("container <%v> does not have <%v> limits defined", [container.name, missing]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + provided := {resource_type | container.resources.requests[resource_type]} + required := {resource_type | resource_type := input.parameters.requests[_]} + missing := required - provided + count(missing) > 0 + msg := sprintf("container <%v> does not have <%v> requests defined", [container.name, missing]) + } + libs: + - | + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1d14ae1333965d76f7e9568b2ce38007e3076cef --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/constraint.yaml @@ -0,0 +1,14 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sDisallowAnonymous +metadata: + name: no-anonymous +spec: + match: + kinds: + - apiGroups: ["rbac.authorization.k8s.io"] + kinds: ["ClusterRoleBinding"] + - apiGroups: ["rbac.authorization.k8s.io"] + kinds: ["RoleBinding"] + parameters: + allowedRoles: + - cluster-role-1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f8889d941d78d8890d64749090e4c9bb65f8dea4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/example_allowed.yaml @@ -0,0 +1,15 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: cluster-role-binding-1 +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: cluster-role-1 +subjects: +- apiGroup: rbac.authorization.k8s.io + kind: Group + name: system:authenticated +- apiGroup: rbac.authorization.k8s.io + kind: Group + name: system:unauthenticated diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a1fc9d050a908e0986489d8bb8e815287de7592 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-anonymous-bindings/example_disallowed.yaml @@ -0,0 +1,18 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: cluster-role-binding-2 +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: cluster-role-2 +subjects: +- apiGroup: rbac.authorization.k8s.io + kind: Group + name: system:authenticated +- apiGroup: rbac.authorization.k8s.io + kind: Group + name: system:unauthenticated +- apiGroup: rbac.authorization.k8s.io + kind: Group + name: system:anonymous diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-authenticated/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-authenticated/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f4a74f4fbb2f8d57baf22bfe6fb89b8368179433 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/samples/no-authenticated/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sDisallowAnonymous +metadata: + name: no-anonymous +spec: + match: + kinds: + - apiGroups: ["rbac.authorization.k8s.io"] + kinds: ["ClusterRoleBinding"] + - apiGroups: ["rbac.authorization.k8s.io"] + kinds: ["RoleBinding"] + parameters: + disallowAuthenticated: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e4664ec71ed4445fa14567b35b1ad8489ee96a51 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/suite.yaml @@ -0,0 +1,35 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: disallowanonymous +tests: +- name: disallow-anonymous + template: template.yaml + constraint: samples/no-anonymous-bindings/constraint.yaml + cases: + - name: example-allowed + object: samples/no-anonymous-bindings/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/no-anonymous-bindings/example_disallowed.yaml + assertions: + - message: "system:unauthenticated" + violations: 1 + - message: "system:anonymous" + violations: 1 + - message: "system:authenticated" + violations: 0 +- name: disallow-authenticated + template: template.yaml + constraint: samples/no-authenticated/constraint.yaml + cases: + - name: authenticated-disallowed-with-parameter-true + object: samples/no-anonymous-bindings/example_disallowed.yaml + assertions: + - message: "system:unauthenticated" + violations: 1 + - message: "system:anonymous" + violations: 1 + - message: "system:authenticated" + violations: 1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..deb410d6c432f2b4079e1b40bb06501a97dc7f9e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowanonymous/template.yaml @@ -0,0 +1,68 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sdisallowanonymous + annotations: + metadata.gatekeeper.sh/title: "Disallow Anonymous Access" + metadata.gatekeeper.sh/version: 1.1.0 + description: Disallows associating ClusterRole and Role resources to the system:anonymous user and system:unauthenticated group. +spec: + crd: + spec: + names: + kind: K8sDisallowAnonymous + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + properties: + allowedRoles: + description: >- + The list of ClusterRoles and Roles that may be associated + with the `system:unauthenticated` group and `system:anonymous` + user. + type: array + items: + type: string + disallowAuthenticated: + description: >- + A boolean indicating whether `system:authenticated` should also + be disallowed by this policy. + type: boolean + default: false + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sdisallowanonymous + + violation[{"msg": msg}] { + not is_allowed(input.review.object.roleRef, object.get(input, ["parameters", "allowedRoles"], [])) + + group := ["system:unauthenticated", "system:anonymous"][_] + subject_is(input.review.object.subjects[_], group) + + msg := message(group) + } + + violation[{"msg": msg}] { + not is_allowed(input.review.object.roleRef, object.get(input, ["parameters", "allowedRoles"], [])) + + object.get(input, ["parameters", "disallowAuthenticated"], false) + + group := "system:authenticated" + subject_is(input.review.object.subjects[_], group) + + msg := message(group) + } + + is_allowed(role, allowedRoles) { + role.name == allowedRoles[_] + } + + subject_is(subject, expected) { + subject.name == expected + } + + message(name) := val { + val := sprintf("%v is not allowed as a subject name in %v %v", [name, input.review.object.kind, input.review.object.metadata.name]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3da15a5de70a779c179a2931ffb594f970ad5940 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sDisallowedRepos +metadata: + name: repo-must-not-be-k8s-gcr-io +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + repos: + - "k8s.gcr.io/" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/disallowed_all.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/disallowed_all.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ec4bfc904ea845ca9bdb16b2e32c6331e0a7899a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/disallowed_all.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: kustomize-disallowed +spec: + initContainers: + - name: kustomize + image: k8s.gcr.io/kustomize/kustomize:v3.8.9 + containers: + - name: kustomize + image: k8s.gcr.io/kustomize/kustomize:v3.8.9 + ephemeralContainers: + - name: kustomize + image: k8s.gcr.io/kustomize/kustomize:v3.8.9 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..145d903fbc5b973ef9d226ad042e7feb302e68e4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_allowed.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Pod +metadata: + name: kustomize-allowed +spec: + containers: + - name: kustomize + image: registry.k8s.io/kustomize/kustomize:v3.8.9 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_both.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_both.yaml new file mode 100644 index 0000000000000000000000000000000000000000..860e00d7164a36f2b5b45b7d9bc0c11c95f4a72e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_both.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: kustomize-disallowed +spec: + initContainers: + - name: kustomizeinit + image: k8s.gcr.io/kustomize/kustomize:v3.8.9 + containers: + - name: kustomize + image: k8s.gcr.io/kustomize/kustomize:v3.8.9 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_container.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_container.yaml new file mode 100644 index 0000000000000000000000000000000000000000..efae1b4173a20b057a5435f61e4cbc9aaa22ac9a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_container.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Pod +metadata: + name: kustomize-disallowed +spec: + containers: + - name: kustomize + image: k8s.gcr.io/kustomize/kustomize:v3.8.9 + diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_initcontainer.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_initcontainer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..93a419d642807960c986880e09c57ee7bd35657a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/samples/repo-must-not-be-k8s-gcr-io/example_disallowed_initcontainer.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: kustomize-disallowed +spec: + initContainers: + - name: kustomizeinit + image: k8s.gcr.io/kustomize/kustomize:v3.8.9 + containers: + - name: kustomize + image: registry.k8s.io/kustomize/kustomize:v3.8.9 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bcc77de960d7fae2df6028ba260c6fd14f08ba20 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/suite.yaml @@ -0,0 +1,43 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: disallowedrepos +tests: +- name: repo-must-not-be-k8s-gcr-io + template: template.yaml + constraint: samples/repo-must-not-be-k8s-gcr-io/constraint.yaml + cases: + - name: example-allowed + object: samples/repo-must-not-be-k8s-gcr-io/example_allowed.yaml + assertions: + - violations: no + - name: container-disallowed + object: samples/repo-must-not-be-k8s-gcr-io/example_disallowed_container.yaml + assertions: + - violations: yes + message: container + - name: initcontainer-disallowed + object: samples/repo-must-not-be-k8s-gcr-io/example_disallowed_initcontainer.yaml + assertions: + - violations: 1 + message: initContainer + - violations: 0 + message: container + - name: both-disallowed + object: samples/repo-must-not-be-k8s-gcr-io/example_disallowed_both.yaml + assertions: + - violations: 2 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - name: all-disallowed + object: samples/repo-must-not-be-k8s-gcr-io/disallowed_all.yaml + assertions: + - violations: 3 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - message: ephemeralContainer + violations: 1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c4a2722570e7190bfffcd09807c06c5e3eaa5bee --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedrepos/template.yaml @@ -0,0 +1,49 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sdisallowedrepos + annotations: + metadata.gatekeeper.sh/title: "Disallowed Repositories" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Disallowed container repositories that begin with a string from the specified list. +spec: + crd: + spec: + names: + kind: K8sDisallowedRepos + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + properties: + repos: + description: The list of prefixes a container image is not allowed to have. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sdisallowedrepos + + violation[{"msg": msg}] { + container := input.review.object.spec.containers[_] + image := container.image + startswith(image, input.parameters.repos[_]) + msg := sprintf("container <%v> has an invalid image repo <%v>, disallowed repos are %v", [container.name, container.image, input.parameters.repos]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.initContainers[_] + image := container.image + startswith(image, input.parameters.repos[_]) + msg := sprintf("initContainer <%v> has an invalid image repo <%v>, disallowed repos are %v", [container.name, container.image, input.parameters.repos]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.ephemeralContainers[_] + image := container.image + startswith(image, input.parameters.repos[_]) + msg := sprintf("ephemeralContainer <%v> has an invalid image repo <%v>, disallowed repos are %v", [container.name, container.image, input.parameters.repos]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a2469fa0a1717a47074af28e533884093dd93ff2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/kustomization.yaml @@ -0,0 +1,4 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cceb50565b7ab49869abaf4cbf1bb763606dc092 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/constraint.yaml @@ -0,0 +1,14 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sDisallowedTags +metadata: + name: container-image-must-not-have-latest-tag +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + namespaces: + - "default" + parameters: + tags: ["latest"] + exemptImages: ["openpolicyagent/opa-exp:latest", "openpolicyagent/opa-exp2:latest"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/disallowed_tag_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/disallowed_tag_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..66db83c15f4b93a9acb12f290a17f6c76a6230ec --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/disallowed_tag_ephemeral.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed-ephemeral +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + ephemeralContainers: + - name: opa + image: openpolicyagent/opa:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cc6407401c5aae9aa0203f5b77f642e6e262954c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_disallowed_tag.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_disallowed_tag.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1c689315f19074bb387d6aa04051bd249efcb831 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_disallowed_tag.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed-2 +spec: + containers: + - name: opa + image: openpolicyagent/opa:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_exempt_image_w_disallowed_tag.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_exempt_image_w_disallowed_tag.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2bd249fec28cc4f7be98fd8ceae7b8cfe8b4f95a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_exempt_image_w_disallowed_tag.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-exempt-allowed +spec: + containers: + - name: opa-exp + image: openpolicyagent/opa-exp:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" + - name: opa-init + image: openpolicyagent/init:v1 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + - name: opa-exp2 + image: openpolicyagent/opa-exp2:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_no_tag.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_no_tag.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3da276bf82150264b34370780630baf0556c769f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_no_tag.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed +spec: + containers: + - name: opa + image: openpolicyagent/opa + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_no_tag_w_port.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_no_tag_w_port.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f567b845d53d11a2053a8a78288b46e7bbce3dce --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_no_tag_w_port.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed-4 +spec: + containers: + - name: opa + image: openpolicyagent:443/opa + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_some_disallowed_tags.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_some_disallowed_tags.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3596fc0c465f56a19b1eb5f9c90edc2bf3bbfa89 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/samples/container-image-must-not-have-latest-tag/example_some_disallowed_tags.yaml @@ -0,0 +1,30 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed-3 +spec: + containers: + - name: opa + image: openpolicyagent/opa-exp:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" + - name: opa-init + image: openpolicyagent/init:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" + - name: opa-exp2 + image: openpolicyagent/opa-exp2:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" + - name: opa-monitor + image: openpolicyagent/monitor:latest + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7c80f37b88acfaa04ac7b454db2ed00e1029b0d7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/suite.yaml @@ -0,0 +1,37 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: disallowedtags +tests: +- name: disallowed-tags + template: template.yaml + constraint: samples/container-image-must-not-have-latest-tag/constraint.yaml + cases: + - name: allowed + object: samples/container-image-must-not-have-latest-tag/example_allowed.yaml + assertions: + - violations: no + - name: exempt-images-with-disallowed-tags + object: samples/container-image-must-not-have-latest-tag/example_exempt_image_w_disallowed_tag.yaml + assertions: + - violations: no + - name: no-tag + object: samples/container-image-must-not-have-latest-tag/example_no_tag.yaml + assertions: + - violations: yes + - name: no-tag-with-port + object: samples/container-image-must-not-have-latest-tag/example_no_tag_w_port.yaml + assertions: + - violations: yes + - name: single-disallowed-tag + object: samples/container-image-must-not-have-latest-tag/example_disallowed_tag.yaml + assertions: + - violations: yes + - name: single-disallowed-tag-ephemeral + object: samples/container-image-must-not-have-latest-tag/disallowed_tag_ephemeral.yaml + assertions: + - violations: yes + - name: some-disallow-tags + object: samples/container-image-must-not-have-latest-tag/example_some_disallowed_tags.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ba2c41bf2b55e669f7f496beb5651fcb9b87d919 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowedtags/template.yaml @@ -0,0 +1,90 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sdisallowedtags + annotations: + metadata.gatekeeper.sh/title: "Disallow tags" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Requires container images to have an image tag different from the ones in + the specified list. + + https://kubernetes.io/docs/concepts/containers/images/#image-names +spec: + crd: + spec: + names: + kind: K8sDisallowedTags + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + tags: + type: array + description: Disallowed container image tags. + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sdisallowedtags + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + container := input_containers[_] + not is_exempt(container) + tags := [tag_with_prefix | tag := input.parameters.tags[_]; tag_with_prefix := concat(":", ["", tag])] + strings.any_suffix_match(container.image, tags) + msg := sprintf("container <%v> uses a disallowed tag <%v>; disallowed tags are %v", [container.name, container.image, input.parameters.tags]) + } + + violation[{"msg": msg}] { + container := input_containers[_] + not is_exempt(container) + parts := split(container.image, "/") + not contains(parts[count(parts) - 1], ":") + msg := sprintf("container <%v> didn't specify an image tag <%v>", [container.name, container.image]) + } + + 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[_] + } + libs: + - | + 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) + } + diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cf5eac82b7bf4d89115f71e4d84a39c5a28038ef --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sDisallowInteractiveTTY +metadata: + name: no-interactive-tty-containers +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bed6b595414724489b637aa7005f2ab5567fc9e0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-interactive-tty-allowed + labels: + app: nginx-interactive-tty +spec: + containers: + - name: nginx + image: nginx + stdin: false + tty: false diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..aa49493233d078a415b357c048852c5e64b50f42 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/samples/no-interactive-containers/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privilege-escalation-disallowed + labels: + app: nginx-privilege-escalation +spec: + containers: + - name: nginx + image: nginx + stdin: true + tty: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ed9acb08f19c79ae5e9551f1f75205df9ac7ed60 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: disallowinteractive +tests: +- name: disallow-interactive + template: template.yaml + constraint: samples/no-interactive-containers/constraint.yaml + cases: + - name: example-allowed + object: samples/no-interactive-containers/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/no-interactive-containers/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..63bc5d179656ab6dc35a60e832845901ed6ed0bd --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/disallowinteractive/template.yaml @@ -0,0 +1,88 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sdisallowinteractivetty + annotations: + metadata.gatekeeper.sh/title: "Disallow Interactive TTY Containers" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Requires that objects have the fields `spec.tty` and `spec.stdin` set to false or unset. +spec: + crd: + spec: + names: + kind: K8sDisallowInteractiveTTY + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Controls use of fields related to gaining an interactive session. Corresponds to the `tty` and + `stdin` fields in the Pod `spec.containers`, `spec.ephemeralContainers`, and `spec.initContainers`. + 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 + rego: | + package k8sdisallowinteractivetty + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + c := input_containers[_] + not is_exempt(c) + input_allow_interactive_fields(c) + msg := sprintf("Containers using tty or stdin (%v) are not allowed running image: %v", [c.name, c.image]) + } + + input_allow_interactive_fields(c) { + has_field(c, "stdin") + not c.stdin == false + } + input_allow_interactive_fields(c) { + has_field(c, "tty") + not c.tty == false + } + input_containers[c] { + c := input.review.object.spec.containers[_] + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + } + # has_field returns whether an object has a field + has_field(object, field) = true { + object[field] + } + libs: + - | + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4575bdf605c8ec4ddeecc44c3a18fb223f0f504c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sContainerEphemeralStorageLimit +metadata: + name: container-ephemeral-storage-limit +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + ephemeral-storage: "500Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage-initContainer.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage-initContainer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fe1b7bac061c696bbe86d5b607f4d00097117ee5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage-initContainer.yaml @@ -0,0 +1,33 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + initContainers: + - name: init-opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + ephemeral-storage: "100Mi" + + + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + ephemeral-storage: "100Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3c1f5ce9e05ca9a2d6c280849b592bbbcd371de8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + + ephemeral-storage: "100Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi-initContainer.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi-initContainer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..05d512d3cc02775cb234b8e212623ccf9263c81b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi-initContainer.yaml @@ -0,0 +1,31 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + initContainers: + - name: init-opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + ephemeral-storage: "1Pi" + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + ephemeral-storage: "100Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7dae0395b3a142422076d14dcbab28c103a2411a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + + ephemeral-storage: "1Pi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_unspecified.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_unspecified.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6e81b11189ebc74969b421c5996d41d13e252153 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_unspecified.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "2Gi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c0ff278930795967e5615945d7ce26130e02fa44 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/samples/container-must-have-ephemeral-storage-limit/update.yaml @@ -0,0 +1,24 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo + spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + resources: + limits: + cpu: "100m" + memory: "1Gi" + ephemeral-storage: "1Pi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a6b99410d52687d0230e9c0c69fc02675c5a2a1c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/suite.yaml @@ -0,0 +1,33 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: ephemeral-storage-limit +tests: +- name: ephemeral-storage-limit + template: template.yaml + constraint: samples/container-must-have-ephemeral-storage-limit/constraint.yaml + cases: + - name: ephemeral-storage-limit-100Mi + object: samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage.yaml + assertions: + - violations: no + - name: ephemeral-storage-limit-initContainer-100Mi + object: samples/container-must-have-ephemeral-storage-limit/example_allowed_ephemeral-storage-initContainer.yaml + assertions: + - violations: no + - name: ephemeral-storage-limit-unspecified + object: samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_unspecified.yaml + assertions: + - violations: yes + - name: ephemeral-storage-limit-1Pi + object: samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi.yaml + assertions: + - violations: yes + - name: ephemeral-storage-limit-initContainer-1Pi + object: samples/container-must-have-ephemeral-storage-limit/example_disallowed_ephemeral_storage_limit_1Pi-initContainer.yaml + assertions: + - violations: yes + - name: ephemeral-storage-limit-update + object: samples/container-must-have-ephemeral-storage-limit/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a9dca71d281ae70ee57ca0f311477c3094c3d990 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/ephemeralstoragelimit/template.yaml @@ -0,0 +1,232 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8scontainerephemeralstoragelimit + annotations: + metadata.gatekeeper.sh/title: "Container ephemeral storage limit" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Requires containers to have an ephemeral storage limit set and constrains + the limit to be within the specified maximum values. + + https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ +spec: + crd: + spec: + names: + kind: K8sContainerEphemeralStorageLimit + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + ephemeral-storage: + description: "The maximum allowed ephemeral storage limit on a Pod, exclusive." + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8scontainerephemeralstoragelimit + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + missing(obj, field) = true { + not obj[field] + } + + missing(obj, field) = true { + obj[field] == "" + } + + has_field(object, field) = true { + object[field] + } + + # 10 ** 21 + storage_multiple("E") = 1000000000000000000000 { true } + + # 10 ** 18 + storage_multiple("P") = 1000000000000000000 { true } + + # 10 ** 15 + storage_multiple("T") = 1000000000000000 { true } + + # 10 ** 12 + storage_multiple("G") = 1000000000000 { true } + + # 10 ** 9 + storage_multiple("M") = 1000000000 { true } + + # 10 ** 6 + storage_multiple("k") = 1000000 { true } + + # 10 ** 3 + storage_multiple("") = 1000 { true } + + # Kubernetes accepts millibyte precision when it probably shouldn't. + # https://github.com/kubernetes/kubernetes/issues/28741 + # 10 ** 0 + storage_multiple("m") = 1 { true } + + # 1000 * 2 ** 10 + storage_multiple("Ki") = 1024000 { true } + + # 1000 * 2 ** 20 + storage_multiple("Mi") = 1048576000 { true } + + # 1000 * 2 ** 30 + storage_multiple("Gi") = 1073741824000 { true } + + # 1000 * 2 ** 40 + storage_multiple("Ti") = 1099511627776000 { true } + + # 1000 * 2 ** 50 + storage_multiple("Pi") = 1125899906842624000 { true } + + # 1000 * 2 ** 60 + storage_multiple("Ei") = 1152921504606846976000 { true } + + get_suffix(storage) = suffix { + not is_string(storage) + suffix := "" + } + + get_suffix(storage) = suffix { + is_string(storage) + count(storage) > 0 + suffix := substring(storage, count(storage) - 1, -1) + storage_multiple(suffix) + } + + get_suffix(storage) = suffix { + is_string(storage) + count(storage) > 1 + suffix := substring(storage, count(storage) - 2, -1) + storage_multiple(suffix) + } + + get_suffix(storage) = suffix { + is_string(storage) + count(storage) > 1 + not storage_multiple(substring(storage, count(storage) - 1, -1)) + not storage_multiple(substring(storage, count(storage) - 2, -1)) + suffix := "" + } + + get_suffix(storage) = suffix { + is_string(storage) + count(storage) == 1 + not storage_multiple(substring(storage, count(storage) - 1, -1)) + suffix := "" + } + + get_suffix(storage) = suffix { + is_string(storage) + count(storage) == 0 + suffix := "" + } + + canonify_storage(orig) = new { + is_number(orig) + new := orig * 1000 + } + + canonify_storage(orig) = new { + not is_number(orig) + suffix := get_suffix(orig) + raw := replace(orig, suffix, "") + regex.match("^[0-9]+(\\.[0-9]+)?$", raw) + new := to_number(raw) * storage_multiple(suffix) + } + + violation[{"msg": msg}] { + # spec.containers.resources.limits["ephemeral-storage"] field is immutable. + not is_update(input.review) + + general_violation[{"msg": msg, "field": "containers"}] + } + + violation[{"msg": msg}] { + not is_update(input.review) + general_violation[{"msg": msg, "field": "initContainers"}] + } + + # Ephemeral containers not checked as it is not possible to set field. + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + storage_orig := container.resources.limits["ephemeral-storage"] + not canonify_storage(storage_orig) + msg := sprintf("container <%v> ephemeral-storage limit <%v> could not be parsed", [container.name, storage_orig]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources + msg := sprintf("container <%v> has no resource limits", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + not container.resources.limits + msg := sprintf("container <%v> has no resource limits", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + missing(container.resources.limits, "ephemeral-storage") + msg := sprintf("container <%v> has no ephemeral-storage limit", [container.name]) + } + + general_violation[{"msg": msg, "field": field}] { + container := input.review.object.spec[field][_] + not is_exempt(container) + storage_orig := container.resources.limits["ephemeral-storage"] + storage := canonify_storage(storage_orig) + max_storage_orig := input.parameters["ephemeral-storage"] + max_storage := canonify_storage(max_storage_orig) + storage > max_storage + msg := sprintf("container <%v> ephemeral-storage limit <%v> is higher than the maximum allowed of <%v>", [container.name, storage_orig, max_storage_orig]) + } + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..40227302a6dd4b56d72dda998cfd964b45caff22 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sExternalIPs +metadata: + name: external-ips +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Service"] + parameters: + allowedIPs: + - "203.0.113.0" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4f859185736ae653c4dabd5e0ca39a2ff2832342 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/example_allowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: allowed-external-ip +spec: + selector: + app: MyApp + ports: + - name: http + protocol: TCP + port: 80 + targetPort: 8080 + externalIPs: + - 203.0.113.0 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..16d531890e8ae3d218d1bafd194c0b7431e6efa1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/samples/allowed-ip/example_disallowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: disallowed-external-ip +spec: + selector: + app: MyApp + ports: + - name: http + protocol: TCP + port: 80 + targetPort: 8080 + externalIPs: + - 1.1.1.1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..629aaa801ae5dca20f812c95b4c4455b7ddf2a9c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: externalip +tests: +- name: allowed-ip + template: template.yaml + constraint: samples/allowed-ip/constraint.yaml + cases: + - name: example-allowed + object: samples/allowed-ip/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/allowed-ip/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3e001284596e34b4f5d589c3a36e788f31af6af4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/externalip/template.yaml @@ -0,0 +1,40 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sexternalips + annotations: + metadata.gatekeeper.sh/title: "External IPs" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Restricts Service externalIPs to an allowed list of IP addresses. + + https://kubernetes.io/docs/concepts/services-networking/service/#external-ips +spec: + crd: + spec: + names: + kind: K8sExternalIPs + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + properties: + allowedIPs: + type: array + description: "An allow-list of external IP addresses." + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sexternalips + + violation[{"msg": msg}] { + input.review.kind.kind == "Service" + input.review.kind.group == "" + allowedIPs := {ip | ip := input.parameters.allowedIPs[_]} + externalIPs := {ip | ip := input.review.object.spec.externalIPs[_]} + forbiddenIPs := externalIPs - allowedIPs + count(forbiddenIPs) > 0 + msg := sprintf("service has forbidden external IPs: %v", [forbiddenIPs]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..80d2c9a93d30afb26b282039cf53d72133cf4e27 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/constraint.yaml @@ -0,0 +1,16 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sHorizontalPodAutoscaler +metadata: + name: horizontal-pod-autoscaler +spec: + enforcementAction: deny + match: + kinds: + - apiGroups: ["autoscaling"] + kinds: ["HorizontalPodAutoscaler"] + parameters: + minimumReplicaSpread: 1 + enforceScaleTargetRef: true + ranges: + - min_replicas: 3 + max_replicas: 6 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_allowed_hpa.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_allowed_hpa.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5c0f0dc42363d76463cbd6ec9306048e9e2ef1b6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_allowed_hpa.yaml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: nginx-hpa-allowed + namespace: default +spec: + minReplicas: 3 + maxReplicas: 6 + metrics: + - resource: + name: cpu + target: + averageUtilization: 900 + type: Utilization + type: Resource + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: nginx-deployment diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_replicas.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_replicas.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e7b8a984b71858ff12d5ee0f63e1db153a09567e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_replicas.yaml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: nginx-hpa-disallowed-replicas + namespace: default +spec: + minReplicas: 2 + maxReplicas: 7 + metrics: + - resource: + name: cpu + target: + averageUtilization: 900 + type: Utilization + type: Resource + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: nginx-deployment diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_replicaspread.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_replicaspread.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ee915d1ba9de51dd0981a52aaae3d12adcd2da90 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_replicaspread.yaml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: nginx-hpa-disallowed-replicaspread + namespace: default +spec: + minReplicas: 4 + maxReplicas: 4 + metrics: + - resource: + name: cpu + target: + averageUtilization: 900 + type: Utilization + type: Resource + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: nginx-deployment diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_scaletarget.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_scaletarget.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4c390f6742316274febffeedccf0c9a04ea43ed8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_disallowed_hpa_scaletarget.yaml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: nginx-hpa-disallowed-scaletarget + namespace: default +spec: + minReplicas: 3 + maxReplicas: 6 + metrics: + - resource: + name: cpu + target: + averageUtilization: 900 + type: Utilization + type: Resource + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: nginx-deployment-missing diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_inventory.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_inventory.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5071039314f7665a6e8e68b374f05479db9f74fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/samples/horizontalpodautoscaler/example_inventory.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + namespace: default + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + example: allowed-deployment + template: + metadata: + labels: + app: nginx + example: allowed-deployment + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1500708b05a14d0bb24eea48c533a726bd450d79 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/suite.yaml @@ -0,0 +1,33 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: horizontalpodautoscaler +tests: +- name: horizontal-pod-autoscaler + template: template.yaml + constraint: samples/horizontalpodautoscaler/constraint.yaml + cases: + - name: example-allowed-hpa + object: samples/horizontalpodautoscaler/example_allowed_hpa.yaml + inventory: + - samples/horizontalpodautoscaler/example_inventory.yaml + assertions: + - violations: no + - name: example-disallowed-hpa-replicas + object: samples/horizontalpodautoscaler/example_disallowed_hpa_replicas.yaml + inventory: + - samples/horizontalpodautoscaler/example_inventory.yaml + assertions: + - violations: yes + - name: example-disallowed-hpa-replicaspread + object: samples/horizontalpodautoscaler/example_disallowed_hpa_replicaspread.yaml + inventory: + - samples/horizontalpodautoscaler/example_inventory.yaml + assertions: + - violations: yes + - name: example-disallowed-scaletarget + object: samples/horizontalpodautoscaler/example_disallowed_hpa_scaletarget.yaml + inventory: + - samples/horizontalpodautoscaler/example_inventory.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/sync.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/sync.yaml new file mode 100644 index 0000000000000000000000000000000000000000..abfbe21691bd2d3cbfb4e893665c893156e46e83 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/sync.yaml @@ -0,0 +1,14 @@ +apiVersion: config.gatekeeper.sh/v1alpha1 +kind: Config +metadata: + name: config + namespace: "gatekeeper-system" +spec: + sync: + syncOnly: + - group: "apps" + version: "v1" + kind: "Deployment" + - group: "apps" + version: "v1" + kind: "StatefulSet" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3e43f28a701590d9818397a5481dfc047e7537a8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/horizontalpodautoscaler/template.yaml @@ -0,0 +1,102 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8shorizontalpodautoscaler + annotations: + metadata.gatekeeper.sh/title: "Horizontal Pod Autoscaler" + metadata.gatekeeper.sh/version: 1.0.1 + metadata.gatekeeper.sh/requires-sync-data: | + "[ + [ + { + "groups":["apps"], + "versions": ["v1"], + "kinds": ["Deployment"] + }, + { + "groups":["apps"], + "versions": ["v1"], + "kinds": ["StatefulSet"] + } + ] + ]" + description: >- + Disallow the following scenarios when deploying `HorizontalPodAutoscalers` + 1. Deployment of HorizontalPodAutoscalers with `.spec.minReplicas` or `.spec.maxReplicas` outside the ranges defined in the constraint + 2. Deployment of HorizontalPodAutoscalers where the difference between `.spec.minReplicas` and `.spec.maxReplicas` is less than the configured `minimumReplicaSpread` + 3. Deployment of HorizontalPodAutoscalers that do not reference a valid `scaleTargetRef` (e.g. Deployment, ReplicationController, ReplicaSet, StatefulSet). +spec: + crd: + spec: + names: + kind: K8sHorizontalPodAutoscaler + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + properties: + enforceScaleTargetRef: + description: If set to true it validates the HPA scaleTargetRef exists + type: boolean + minimumReplicaSpread: + description: If configured it enforces the minReplicas and maxReplicas in an HPA must have a spread of at least this many replicas + type: integer + ranges: + type: array + description: Allowed ranges for numbers of replicas. Values are inclusive. + items: + type: object + description: A range of allowed replicas. Values are inclusive. + properties: + min_replicas: + description: The minimum number of replicas allowed, inclusive. + type: integer + max_replicas: + description: The maximum number of replicas allowed, inclusive. + type: integer + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8shorizontalpodautoscaler + + violation[{"msg": msg}] { + input.review.kind.kind == "HorizontalPodAutoscaler" + hpa := input.review.object + + not input_replica_limit(hpa) + msg := sprintf("The %v <%v> minReplicas %v or maxReplicas %v is not allowed: %v. Allowed ranges: %v", [hpa.kind, hpa.metadata.name, hpa.spec.minReplicas, hpa.spec.maxReplicas, input.parameters.ranges]) + } + + violation[{"msg": msg}] { + input.review.kind.kind == "HorizontalPodAutoscaler" + hpa := input.review.object + + not input_replica_spread(hpa) + + msg := sprintf("The %v <%v> is configured with minReplicas %v and maxReplicas %v which is a spread of %v replica(s). The spread must be at least %v replica(s)", [hpa.kind, hpa.metadata.name, hpa.spec.minReplicas, hpa.spec.maxReplicas, hpa.spec.maxReplicas - hpa.spec.minReplicas, input.parameters.minimumReplicaSpread]) + } + + violation[{"msg": msg}] { + input.review.kind.kind == "HorizontalPodAutoscaler" + hpa := input.review.object + input.parameters.enforceScaleTargetRef + + not data.inventory.namespace[hpa.metadata.namespace][hpa.spec.scaleTargetRef.apiVersion][hpa.spec.scaleTargetRef.kind][hpa.spec.scaleTargetRef.name] + msg := sprintf("The HorizontalPodAutoscaler <%v> has a scaleTargetRef of <%v/%v> but it does not exist. The scaleTargetRef for the HorizontalPodAutoscaler must exist", [hpa.metadata.name, hpa.spec.scaleTargetRef.kind, hpa.spec.scaleTargetRef.name]) + } + + input_replica_limit(hpa) { + count(input.parameters.ranges) > 0 + range := input.parameters.ranges[_] + value_within_range(range, hpa.spec.minReplicas, hpa.spec.maxReplicas) + } + + value_within_range(range, min_provided, max_provided) { + range.min_replicas <= min_provided + range.max_replicas >= max_provided + } + + input_replica_spread(hpa) { + input.parameters.minimumReplicaSpread + (hpa.spec.maxReplicas - hpa.spec.minReplicas) >= input.parameters.minimumReplicaSpread + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a92540d91e8c5c8cf21644bede14e50c19367f46 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sHttpsOnly +metadata: + name: ingress-https-only-tls-optional +spec: + match: + kinds: + - apiGroups: ["extensions", "networking.k8s.io"] + kinds: ["Ingress"] + parameters: + tlsOptional: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..27a94ee8b9ca1244e6ee2bbbe5fc54a65920ad4f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/example_allowed.yaml @@ -0,0 +1,18 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-demo-allowed-tls-optional + annotations: + kubernetes.io/ingress.allow-http: "false" +spec: + rules: + - host: example-host.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c6097a9f7c9817d1af73daf9437689a515f5f80f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only-tls-optional/example_disallowed.yaml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-demo-disallowed-tls-optional +spec: + rules: + - host: example-host.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e3bd9ca97b6dbd20aaaabf553ab5ddddd1684312 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sHttpsOnly +metadata: + name: ingress-https-only +spec: + match: + kinds: + - apiGroups: ["extensions", "networking.k8s.io"] + kinds: ["Ingress"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4d48fdc3a5d0a4e98715e08c4ff287294f2978e1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/example_allowed.yaml @@ -0,0 +1,19 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-demo-allowed + annotations: + kubernetes.io/ingress.allow-http: "false" +spec: + tls: [{}] + rules: + - host: example-host.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..730ec1dc465c80f817a8d0a6814078b94da8d3ba --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/samples/ingress-https-only/example_disallowed.yaml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-demo-disallowed +spec: + rules: + - host: example-host.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a403955242541b53ec3379cdbd3ab3689e372f27 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: httpsonly +tests: +- name: tls-required + template: template.yaml + constraint: samples/ingress-https-only/constraint.yaml + cases: + - name: example-allowed + object: samples/ingress-https-only/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/ingress-https-only/example_disallowed.yaml + assertions: + - violations: yes +- name: tls-optional + template: template.yaml + constraint: samples/ingress-https-only-tls-optional/constraint.yaml + cases: + - name: example-allowed-tls-optional + object: samples/ingress-https-only-tls-optional/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed-tls-optional + object: samples/ingress-https-only-tls-optional/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/sync.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/sync.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b5f00c4dda5ec09f1c0d2ab18a436caead03e77f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/sync.yaml @@ -0,0 +1,14 @@ +apiVersion: config.gatekeeper.sh/v1alpha1 +kind: Config +metadata: + name: config + namespace: "gatekeeper-system" +spec: + sync: + syncOnly: + - group: "extensions" + version: "v1beta1" + kind: "Ingress" + - group: "networking.k8s.io" + version: "v1beta1" + kind: "Ingress" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5293fe72c66cdb9ad79beb6ecc4e16fc55fa0d6d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/httpsonly/template.yaml @@ -0,0 +1,71 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8shttpsonly + annotations: + metadata.gatekeeper.sh/title: "HTTPS Only" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Requires Ingress resources to be HTTPS only. Ingress resources must + include the `kubernetes.io/ingress.allow-http` annotation, set to `false`. + By default a valid TLS {} configuration is required, this can be made + optional by setting the `tlsOptional` parameter to `true`. + + https://kubernetes.io/docs/concepts/services-networking/ingress/#tls +spec: + crd: + spec: + names: + kind: K8sHttpsOnly + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Requires Ingress resources to be HTTPS only. Ingress resources must + include the `kubernetes.io/ingress.allow-http` annotation, set to + `false`. By default a valid TLS {} configuration is required, this + can be made optional by setting the `tlsOptional` parameter to + `true`. + properties: + tlsOptional: + type: boolean + description: "When set to `true` the TLS {} is optional, defaults + to false." + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8shttpsonly + + violation[{"msg": msg}] { + input.review.object.kind == "Ingress" + regex.match("^(extensions|networking.k8s.io)/", input.review.object.apiVersion) + ingress := input.review.object + not https_complete(ingress) + not tls_is_optional + msg := sprintf("Ingress should be https. tls configuration and allow-http=false annotation are required for %v", [ingress.metadata.name]) + } + + violation[{"msg": msg}] { + input.review.object.kind == "Ingress" + regex.match("^(extensions|networking.k8s.io)/", input.review.object.apiVersion) + ingress := input.review.object + not annotation_complete(ingress) + tls_is_optional + msg := sprintf("Ingress should be https. The allow-http=false annotation is required for %v", [ingress.metadata.name]) + } + + https_complete(ingress) = true { + ingress.spec["tls"] + count(ingress.spec.tls) > 0 + ingress.metadata.annotations["kubernetes.io/ingress.allow-http"] == "false" + } + + annotation_complete(ingress) = true { + ingress.metadata.annotations["kubernetes.io/ingress.allow-http"] == "false" + } + + tls_is_optional { + parameters := object.get(input, "parameters", {}) + object.get(parameters, "tlsOptional", false) == true + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e1cae7cdfd1e2ece3ffcd0b9ed1c5d656eeb09d6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sImageDigests +metadata: + name: container-image-must-have-digest +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + namespaces: + - "default" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/disallowed_all.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/disallowed_all.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b576d4301ae257070a68dc36de2456477fa73171 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/disallowed_all.yaml @@ -0,0 +1,26 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed +spec: + initContainers: + - name: opainit + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + ephemeralContainers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..81b3fea48f6adb736c93f59e5c050c71ac15e1a8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2@sha256:04ff8fce2afd1a3bc26260348e5b290e8d945b1fad4b4c16d22834c2f3a1814a + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a19eef5c6cc207e3e92bd00f15078c8962617e41 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/samples/container-image-must-have-digest/example_disallowed.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed +spec: + initContainers: + - name: opainit + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3e853c8f6b594b8d27bd5e11d14078fbbd2674dc --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/suite.yaml @@ -0,0 +1,31 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: imagedigests +tests: +- name: container-image-must-have-digest + template: template.yaml + constraint: samples/container-image-must-have-digest/constraint.yaml + cases: + - name: example-allowed + object: samples/container-image-must-have-digest/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/container-image-must-have-digest/example_disallowed.yaml + assertions: + - violations: 2 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - name: disallowed-all + object: samples/container-image-must-have-digest/disallowed_all.yaml + assertions: + - violations: 3 + - message: initContainer + violations: 1 + - message: container + violations: 1 + - message: ephemeralContainer + violations: 1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e00a4b6085881fe2774fd23985a25794321603c8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/imagedigests/template.yaml @@ -0,0 +1,82 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8simagedigests + annotations: + metadata.gatekeeper.sh/title: "Image Digests" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Requires container images to contain a digest. + + https://kubernetes.io/docs/concepts/containers/images/ +spec: + crd: + spec: + names: + kind: K8sImageDigests + validation: + openAPIV3Schema: + type: object + description: >- + Requires container images to contain a digest. + + https://kubernetes.io/docs/concepts/containers/images/ + 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 + rego: | + package k8simagedigests + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + container := input.review.object.spec.containers[_] + not is_exempt(container) + not regex.match("@[a-z0-9]+([+._-][a-z0-9]+)*:[a-zA-Z0-9=_-]+", container.image) + msg := sprintf("container <%v> uses an image without a digest <%v>", [container.name, container.image]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.initContainers[_] + not is_exempt(container) + not regex.match("@[a-z0-9]+([+._-][a-z0-9]+)*:[a-zA-Z0-9=_-]+", container.image) + msg := sprintf("initContainer <%v> uses an image without a digest <%v>", [container.name, container.image]) + } + + violation[{"msg": msg}] { + container := input.review.object.spec.ephemeralContainers[_] + not is_exempt(container) + not regex.match("@[a-z0-9]+([+._-][a-z0-9]+)*:[a-zA-Z0-9=_-]+", container.image) + msg := sprintf("ephemeralContainer <%v> uses an image without a digest <%v>", [container.name, container.image]) + } + libs: + - | + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6223d8f454876d2d5372a6bc6e03732ec59d6185 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/kustomization.yaml @@ -0,0 +1,33 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- allowedrepos +- automount-serviceaccount-token +- block-endpoint-edit-default-role +- block-loadbalancer-services +- block-nodeport-services +- block-wildcard-ingress +- containerlimits +- containerrequests +- containerresourceratios +- containerresources +- disallowanonymous +- disallowedrepos +- disallowedtags +- disallowinteractive +- ephemeralstoragelimit +- externalip +- horizontalpodautoscaler +- httpsonly +- imagedigests +- noupdateserviceaccount +- poddisruptionbudget +- replicalimits +- requiredannotations +- requiredlabels +- requiredprobes +- uniqueingresshost +- uniqueserviceselector +- verifydeprecatedapi +- storageclass +- allowedreposv2 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/README.md b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4843dc5dc394a7fd0d9ae6c7cb75189969aba9bc --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/README.md @@ -0,0 +1,13 @@ +# NoUpdateServiceAccount + +**NOTE:** This policy is ignored in `audit` mode, because it only blocks +updates to existing resources, not specific configurations. + +The `NoUpdateServiceAccount` constraint blocks updating the service account on +resources that abstract over Pods. + +This policy helps prevent workloads with "update-self" permissions from +escalating further in the cluster by selecting a new service account to +run as. It is especially useful for workloads running in sensitive +namespaces like `kube-system`, where nearby service accounts are likely to +have cluster admin rights. diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/samples/noupdateserviceaccount/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/samples/noupdateserviceaccount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d8920759e028b921cd730db3389339efab314018 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/samples/noupdateserviceaccount/constraint.yaml @@ -0,0 +1,33 @@ +# IMPORTANT: Before deploying this policy, make sure you allow-list any groups +# or users that need to deploy workloads to kube-system, such as cluster- +# lifecycle controllers, addon managers, etc. Such controllers may need to +# update service account names during automated rollouts (e.g. of refactored +# configurations). You can allow-list them with the allowedGroups and +# allowedUsers properties of the NoUpdateServiceAccount Constraint. +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: NoUpdateServiceAccount +metadata: + name: no-update-kube-system-service-account +spec: + match: + namespaces: ["kube-system"] + kinds: + - apiGroups: [""] + kinds: + # You can optionally add "Pod" here, but it is unnecessary because + # Pod service account immutability is enforced by the Kubernetes API. + - "ReplicationController" + - apiGroups: ["apps"] + kinds: + - "ReplicaSet" + - "Deployment" + - "StatefulSet" + - "DaemonSet" + - apiGroups: ["batch"] + kinds: + # You can optionally add "Job" here, but it is unnecessary because + # Job service account immutability is enforced by the Kubernetes API. + - "CronJob" + parameters: + allowedGroups: [] + allowedUsers: [] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/samples/noupdateserviceaccount/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/samples/noupdateserviceaccount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c6bbde27b871c4b79f554a9920c61d0547f007c4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/samples/noupdateserviceaccount/example_allowed.yaml @@ -0,0 +1,31 @@ +# Note: The gator tests currently require exactly one object per example file. +# Since this is an update-triggered policy, at least two objects are technically +# required to demonstrate it. Due to the gator requirement, we only have one +# object below. The policy should allow changing everything but the +# serviceAccountName field. +kind: Deployment +apiVersion: apps/v1 +metadata: + name: policy-test + namespace: kube-system + labels: + app: policy-test +spec: + replicas: 1 + selector: + matchLabels: + app: policy-test-deploy + template: + metadata: + labels: + app: policy-test-deploy + spec: + # Changing anything except this field should be allowed by the policy. + serviceAccountName: policy-test-sa-1 + containers: + - name: policy-test + image: ubuntu + command: + - /bin/bash + - -c + - sleep 99999 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8d4a0ec036f468d497d2f907b29dd0e76abcd9b9 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/suite.yaml @@ -0,0 +1,20 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: noupdateserviceaccount +tests: + - name: noupdateserviceaccount + template: template.yaml + constraint: samples/noupdateserviceaccount/constraint.yaml + cases: + - name: example-allowed + object: samples/noupdateserviceaccount/example_allowed.yaml + assertions: + - violations: no + # Since the tests are currently stateless, they can't exercise level-triggered + # events like changing a field. For now, manually test example_disallowed.yaml + # instead. + # - name: example-disallowed + # object: samples/noupdateserviceaccount/example_disallowed.yaml + # assertions: + # - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..782a0d313730349e0fd31db288fae024e27b369d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/noupdateserviceaccount/template.yaml @@ -0,0 +1,106 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: noupdateserviceaccount + annotations: + metadata.gatekeeper.sh/title: "Block updating Service Account" + metadata.gatekeeper.sh/version: 1.0.1 + description: "Blocks updating the service account on resources that abstract over Pods. This policy is ignored in audit mode." +spec: + crd: + spec: + names: + kind: NoUpdateServiceAccount + validation: + openAPIV3Schema: + type: object + properties: + allowedGroups: + description: Groups that should be allowed to bypass the policy. + type: array + items: + type: string + allowedUsers: + description: Users that should be allowed to bypass the policy. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package noupdateserviceaccount + + privileged(userInfo, allowedUsers, _) { + # Allow if the user is in allowedUsers. + # Use object.get so omitted parameters can't cause policy bypass by + # evaluating to undefined. + username := object.get(userInfo, "username", "") + allowedUsers[_] == username + } + + privileged(userInfo, _, allowedGroups) { + # Allow if the user's groups intersect allowedGroups. + # Use object.get so omitted parameters can't cause policy bypass by + # evaluating to undefined. + userGroups := object.get(userInfo, "groups", []) + groups := {g | g := userGroups[_]} + allowed := {g | g := allowedGroups[_]} + intersection := groups & allowed + count(intersection) > 0 + } + + get_service_account(obj) = spec { + obj.kind == "Pod" + spec := obj.spec.serviceAccountName + } { + obj.kind == "ReplicationController" + spec := obj.spec.template.spec.serviceAccountName + } { + obj.kind == "ReplicaSet" + spec := obj.spec.template.spec.serviceAccountName + } { + obj.kind == "Deployment" + spec := obj.spec.template.spec.serviceAccountName + } { + obj.kind == "StatefulSet" + spec := obj.spec.template.spec.serviceAccountName + } { + obj.kind == "DaemonSet" + spec := obj.spec.template.spec.serviceAccountName + } { + obj.kind == "Job" + spec := obj.spec.template.spec.serviceAccountName + } { + obj.kind == "CronJob" + spec := obj.spec.jobTemplate.spec.template.spec.serviceAccountName + } + + violation[{"msg": msg}] { + # This policy only applies to updates of existing resources. + input.review.operation == "UPDATE" + + # Use object.get so omitted parameters can't cause policy bypass by + # evaluating to undefined. + params := object.get(input, "parameters", {}) + allowedUsers := object.get(params, "allowedUsers", []) + allowedGroups := object.get(params, "allowedGroups", []) + + # Extract the service account. + oldKSA := get_service_account(input.review.oldObject) + newKSA := get_service_account(input.review.object) + + # Deny unprivileged users and groups from changing serviceAccountName. + not privileged(input.review.userInfo, allowedUsers, allowedGroups) + oldKSA != newKSA + msg := "user does not have permission to modify serviceAccountName" + } { + # Defensively require object to have a serviceAccountName. + input.review.operation == "UPDATE" + not get_service_account(input.review.object) + msg := "missing serviceAccountName field in object under review" + } { + # Defensively require oldObject to have a serviceAccountName. + input.review.operation == "UPDATE" + not get_service_account(input.review.oldObject) + msg := "missing serviceAccountName field in oldObject under review" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d378a1841c344f2800f76aa279de65efe443a1b0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPodDisruptionBudget +metadata: + name: pod-distruption-budget +spec: + match: + kinds: + - apiGroups: ["apps"] + kinds: ["Deployment", "StatefulSet"] + - apiGroups: ["policy"] + kinds: ["PodDisruptionBudget"] + - apiGroups: [""] + kinds: ["ReplicationController"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment1.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment1.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6b70a89327d65eb193cbd3dad6bf15fb02b0ab32 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment1.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment-allowed-1 + namespace: default + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + example: allowed-deployment-1 + template: + metadata: + labels: + app: nginx + example: allowed-deployment-1 + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment2.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ae96d99a9c211ec31a063a0cdcfb7449770b8f8e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment2.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment-allowed-2 + namespace: default + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + example: allowed-deployment-2 + template: + metadata: + labels: + app: nginx + example: allowed-deployment-2 + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment3.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment3.yaml new file mode 100644 index 0000000000000000000000000000000000000000..154f444841b47d2fe7aa1addf78e7f28753b28d4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment3.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment-allowed-3 + namespace: default + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + example: allowed-deployment-3 + template: + metadata: + labels: + app: nginx + example: allowed-deployment-3 + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment4.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment4.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7c2d2e8ed7732b207c6602622428610850bdefa3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_deployment4.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment-allowed-4 + namespace: default + labels: + app: non-matching-nginx +spec: + replicas: 1 + selector: + matchLabels: + app: non-matching-nginx + example: allowed-deployment-4 + template: + metadata: + labels: + app: non-matching-nginx + example: allowed-deployment-4 + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_pdb.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_pdb.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f860777fbf3eeab41cffca28b83d5f9face0bd96 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_allowed_pdb.yaml @@ -0,0 +1,10 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: nginx-pdb-allowed + namespace: default +spec: + maxUnavailable: 1 + selector: + matchLabels: + foo: bar diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_disallowed_deployment.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_disallowed_deployment.yaml new file mode 100644 index 0000000000000000000000000000000000000000..76044a6c92d3083c667ef9183d185a9f6208d3ae --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_disallowed_deployment.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment-disallowed + namespace: default + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + example: disallowed-deployment + template: + metadata: + labels: + app: nginx + example: disallowed-deployment + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_disallowed_pdb.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_disallowed_pdb.yaml new file mode 100644 index 0000000000000000000000000000000000000000..41d0b916230c6962da1127f3d8ceadd7a50cff36 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_disallowed_pdb.yaml @@ -0,0 +1,10 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: nginx-pdb-disallowed + namespace: default +spec: + maxUnavailable: 0 + selector: + matchLabels: + foo: bar diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed1.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed1.yaml new file mode 100644 index 0000000000000000000000000000000000000000..df1cd233e96a15055d554ad60fdb0509567afa69 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed1.yaml @@ -0,0 +1,11 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: inventory-nginx-pdb-allowed-1 + namespace: default +spec: + minAvailable: 2 + selector: + matchLabels: + app: nginx + example: allowed-deployment-1 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed2.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e4ccb63be750212852bae5bb3aaa949dae5ea6ba --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed2.yaml @@ -0,0 +1,11 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: inventory-nginx-pdb-allowed-2 + namespace: default +spec: + maxUnavailable: 1 + selector: + matchLabels: + app: nginx + example: allowed-deployment-2 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed3.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed3.yaml new file mode 100644 index 0000000000000000000000000000000000000000..18a0eeb47d30923cac56fef2effeeb559888d0ae --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed3.yaml @@ -0,0 +1,10 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: inventory-nginx-pdb-allowed-3 + namespace: default +spec: + minAvailable: 2 + selector: + matchLabels: + app: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed4.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed4.yaml new file mode 100644 index 0000000000000000000000000000000000000000..90f1501633af55cabe0865391ff2f6aea5719e14 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_allowed4.yaml @@ -0,0 +1,11 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: inventory-mongo-pdb-allowed-3 + namespace: default +spec: + minAvailable: 2 + selector: + matchLabels: + app: mongo + example: non-matching-deployment-3 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a35c58b4859753b5c408f4f911073b3c4bb32274 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/samples/poddisruptionbudget/example_inventory_disallowed.yaml @@ -0,0 +1,11 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: inventory-nginx-pdb-disallowed + namespace: default +spec: + minAvailable: 3 + selector: + matchLabels: + app: nginx + example: disallowed-deployment diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0e30d6b3504fa623a00db64d37531dfb948143e2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/suite.yaml @@ -0,0 +1,47 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: poddisruptionbudget +tests: +- name: pod-disruption-budget + template: template.yaml + constraint: samples/poddisruptionbudget/constraint.yaml + cases: + - name: example-allowed-pdb + object: samples/poddisruptionbudget/example_allowed_pdb.yaml + assertions: + - violations: no + - name: example-disallowed-pdb + object: samples/poddisruptionbudget/example_disallowed_pdb.yaml + assertions: + - violations: yes + - name: example-allowed-min-available + object: samples/poddisruptionbudget/example_allowed_deployment1.yaml + inventory: + - samples/poddisruptionbudget/example_inventory_allowed1.yaml + assertions: + - violations: no + - name: example-allowed-max-unavailable + object: samples/poddisruptionbudget/example_allowed_deployment2.yaml + inventory: + - samples/poddisruptionbudget/example_inventory_allowed2.yaml + assertions: + - violations: no + - name: example-allowed-subset-selector + object: samples/poddisruptionbudget/example_allowed_deployment3.yaml + inventory: + - samples/poddisruptionbudget/example_inventory_allowed3.yaml + assertions: + - violations: no + - name: example-allowed-nomatch-selector + object: samples/poddisruptionbudget/example_allowed_deployment4.yaml + inventory: + - samples/poddisruptionbudget/example_inventory_allowed4.yaml + assertions: + - violations: no + - name: example-disallowed-min-available + object: samples/poddisruptionbudget/example_disallowed_deployment.yaml + inventory: + - samples/poddisruptionbudget/example_inventory_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/sync.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/sync.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0977d29047cc12204e889816c8979fd3581778cd --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/sync.yaml @@ -0,0 +1,11 @@ +apiVersion: config.gatekeeper.sh/v1alpha1 +kind: Config +metadata: + name: config + namespace: "gatekeeper-system" +spec: + sync: + syncOnly: + - group: "policy" + version: "v1" + kind: "PodDisruptionBudget" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c06d3d6474803975559ac5e55db40643bc0e37d3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/poddisruptionbudget/template.yaml @@ -0,0 +1,90 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spoddisruptionbudget + annotations: + metadata.gatekeeper.sh/title: "Pod Disruption Budget" + metadata.gatekeeper.sh/version: 1.0.4 + metadata.gatekeeper.sh/requires-sync-data: | + "[ + [ + { + "groups":["policy"], + "versions": ["v1"], + "kinds": ["PodDisruptionBudget"] + } + ] + ]" + description: >- + Disallow the following scenarios when deploying PodDisruptionBudgets or resources that implement the replica subresource (e.g. Deployment, ReplicationController, ReplicaSet, StatefulSet): + 1. Deployment of PodDisruptionBudgets with .spec.maxUnavailable == 0 + 2. Deployment of PodDisruptionBudgets with .spec.minAvailable == .spec.replicas of the resource with replica subresource + This will prevent PodDisruptionBudgets from blocking voluntary disruptions such as node draining. + + https://kubernetes.io/docs/concepts/workloads/pods/disruptions/ +spec: + crd: + spec: + names: + kind: K8sPodDisruptionBudget + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spoddisruptionbudget + + violation[{"msg": msg}] { + input.review.kind.kind == "PodDisruptionBudget" + pdb := input.review.object + + not valid_pdb_max_unavailable(pdb) + msg := sprintf( + "PodDisruptionBudget <%v> has maxUnavailable of 0, only positive integers are allowed for maxUnavailable", + [pdb.metadata.name], + ) + } + + violation[{"msg": msg}] { + obj := input.review.object + pdb := data.inventory.namespace[obj.metadata.namespace]["policy/v1"].PodDisruptionBudget[_] + + matchLabels := { [label, value] | some label; value := pdb.spec.selector.matchLabels[label] } + labels := { [label, value] | some label; value := obj.spec.selector.matchLabels[label] } + count(matchLabels - labels) == 0 + + not valid_pdb_max_unavailable(pdb) + msg := sprintf( + "%v <%v> has been selected by PodDisruptionBudget <%v> but has maxUnavailable of 0, only positive integers are allowed for maxUnavailable", + [obj.kind, obj.metadata.name, pdb.metadata.name], + ) + } + + violation[{"msg": msg}] { + obj := input.review.object + pdb := data.inventory.namespace[obj.metadata.namespace]["policy/v1"].PodDisruptionBudget[_] + + matchLabels := { [label, value] | some label; value := pdb.spec.selector.matchLabels[label] } + labels := { [label, value] | some label; value := obj.spec.selector.matchLabels[label] } + count(matchLabels - labels) == 0 + + not valid_pdb_min_available(obj, pdb) + msg := sprintf( + "%v <%v> has %v replica(s) but PodDisruptionBudget <%v> has minAvailable of %v, PodDisruptionBudget count should always be lower than replica(s), and not used when replica(s) is set to 1", + [obj.kind, obj.metadata.name, obj.spec.replicas, pdb.metadata.name, pdb.spec.minAvailable], + ) + } + + valid_pdb_min_available(obj, pdb) { + # default to -1 if minAvailable is not set so valid_pdb_min_available is always true + # for objects with >= 0 replicas. If minAvailable defaults to >= 0, objects with + # replicas field might violate this constraint if they are equal to the default set here + min_available := object.get(pdb.spec, "minAvailable", -1) + obj.spec.replicas > min_available + } + + valid_pdb_max_unavailable(pdb) { + # default to 1 if maxUnavailable is not set so valid_pdb_max_unavailable always returns true. + # If maxUnavailable defaults to 0, it violates this constraint because all pods needs to be + # available and no pods can be evicted voluntarily + max_unavailable := object.get(pdb.spec, "maxUnavailable", 1) + max_unavailable > 0 + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b496235f3abecf1f55185ee32974d0b6bf849b28 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sReplicaLimits +metadata: + name: replica-limits +spec: + match: + kinds: + - apiGroups: ["apps"] + kinds: ["Deployment"] + parameters: + ranges: + - min_replicas: 3 + max_replicas: 50 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f5a2b1d8c2fd4efcb4323e0eb6dd7148e45a8446 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/example_allowed.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: allowed-deployment +spec: + selector: + matchLabels: + app: nginx + replicas: 3 + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1c4899d20e766d2cbea6152ca77febbaf97544bf --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/samples/replicalimits/example_disallowed.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: disallowed-deployment +spec: + selector: + matchLabels: + app: nginx + replicas: 100 + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..598efb81493053c2a1364d030f70c88e75467a14 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: replicalimits +tests: +- name: replica-limit + template: template.yaml + constraint: samples/replicalimits/constraint.yaml + cases: + - name: example-allowed + object: samples/replicalimits/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/replicalimits/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2e44fb2c12693eb66dcb5ddbdb7378ee59d90699 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/replicalimits/template.yaml @@ -0,0 +1,58 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sreplicalimits + annotations: + metadata.gatekeeper.sh/title: "Replica Limits" + metadata.gatekeeper.sh/version: 1.0.2 + description: >- + Requires that objects with the field `spec.replicas` (Deployments, + ReplicaSets, etc.) specify a number of replicas within defined ranges. +spec: + crd: + spec: + names: + kind: K8sReplicaLimits + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + properties: + ranges: + type: array + description: Allowed ranges for numbers of replicas. Values are inclusive. + items: + type: object + description: A range of allowed replicas. Values are inclusive. + properties: + min_replicas: + description: The minimum number of replicas allowed, inclusive. + type: integer + max_replicas: + description: The maximum number of replicas allowed, inclusive. + type: integer + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sreplicalimits + + object_name = input.review.object.metadata.name + object_kind = input.review.kind.kind + + violation[{"msg": msg}] { + spec := input.review.object.spec + not input_replica_limit(spec) + msg := sprintf("The provided number of replicas is not allowed for %v: %v. Allowed ranges: %v", [object_kind, object_name, input.parameters]) + } + + input_replica_limit(spec) { + provided := spec.replicas + count(input.parameters.ranges) > 0 + range := input.parameters.ranges[_] + value_within_range(range, provided) + } + + value_within_range(range, value) { + range.min_replicas <= value + range.max_replicas >= value + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..033700136def31d98a72719095fc1cbba23d0d3a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/constraint.yaml @@ -0,0 +1,18 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredAnnotations +metadata: + name: all-must-have-certain-set-of-annotations +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Service"] + parameters: + message: "All services must have a `a8r.io/owner` and `a8r.io/runbook` annotations." + annotations: + - key: a8r.io/owner + # Matches email address or github user + allowedRegex: ^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}|[a-z]{1,39})$ + - key: a8r.io/runbook + # Matches urls including or not http/https + allowedRegex: ^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$ diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b3b9f95ade50729e64e7e9df0b66151da183461b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/example_allowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: allowed-service + annotations: + a8r.io/owner: "dev-team-alfa@contoso.com" + a8r.io/runbook: "https://confluence.contoso.com/dev-team-alfa/runbooks" +spec: + ports: + - name: http + port: 80 + targetPort: 8080 + selector: + app: foo diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..62c01b87756f34cdc69e7b420ee2c1824ba5c37c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/samples/all-must-have-certain-set-of-annotations/example_disallowed.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: disallowed-service +spec: + ports: + - name: http + port: 80 + targetPort: 8080 + selector: + app: foo diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..48bf7303a725e43c517e38390677e57182eba6ec --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/suite.yaml @@ -0,0 +1,17 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: requiredannotations +tests: +- name: must-have-set-of-annotations + template: template.yaml + constraint: samples/all-must-have-certain-set-of-annotations/constraint.yaml + cases: + - name: example-allowed + object: samples/all-must-have-certain-set-of-annotations/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/all-must-have-certain-set-of-annotations/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..634acb1812c29e6b6f2a57cdaa5e923b8b1d6827 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredannotations/template.yaml @@ -0,0 +1,59 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8srequiredannotations + annotations: + metadata.gatekeeper.sh/title: "Required Annotations" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Requires resources to contain specified annotations, with values matching + provided regular expressions. +spec: + crd: + spec: + names: + kind: K8sRequiredAnnotations + validation: + openAPIV3Schema: + type: object + properties: + message: + type: string + annotations: + type: array + description: >- + A list of annotations and values the object must specify. + items: + type: object + properties: + key: + type: string + description: >- + The required annotation. + allowedRegex: + type: string + description: >- + If specified, a regular expression the annotation's value + must match. The value must contain at least one match for + the regular expression. + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8srequiredannotations + + violation[{"msg": msg, "details": {"missing_annotations": missing}}] { + provided := {annotation | input.review.object.metadata.annotations[annotation]} + required := {annotation | annotation := input.parameters.annotations[_].key} + missing := required - provided + count(missing) > 0 + msg := sprintf("you must provide annotation(s): %v", [missing]) + } + + violation[{"msg": msg}] { + value := input.review.object.metadata.annotations[key] + expected := input.parameters.annotations[_] + expected.key == key + expected.allowedRegex != "" + not regex.match(expected.allowedRegex, value) + msg := sprintf("Annotation <%v: %v> does not satisfy allowed regex: %v", [key, value, expected.allowedRegex]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..806e9862ffc696b1ac6b12813501be854cbccdaf --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/constraint.yaml @@ -0,0 +1,14 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredLabels +metadata: + name: all-must-have-owner +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Namespace"] + parameters: + message: "All namespaces must have an `owner` label that points to your company username" + labels: + - key: owner + allowedRegex: "^[a-zA-Z]+.agilebank.demo$" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e2d3b9c032ad16eea45ffd913e6632cb60c1875f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_allowed.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: allowed-namespace + labels: + owner: user.agilebank.demo diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a7a53610a762f25250aa2ea9ddbf13aa900996d3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_disallowed.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: disallowed-namespace diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_disallowed_label_value.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_disallowed_label_value.yaml new file mode 100644 index 0000000000000000000000000000000000000000..36ae7717667a79bae5b5688957df4a70333b2520 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/all-must-have-owner/example_disallowed_label_value.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: disallowed-namespace + labels: + owner: user diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ead40234a5fdec6916fe88ee3f4d6cfd47c54d53 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredLabels +metadata: + name: must-have-pizza +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + message: "All pods must have label of key `pizza` regardless of the label's value" + labels: + - key: pizza diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a904e3336b84ec43021e70c7da04790862a9195 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/example_allowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: has-pizza + labels: + pizza: is-great +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fcf5b3e9404af94a7e62a036bda93cd87d2f4926 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/samples/verify-label-key-only/example_disallowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: does-not-have-pizza + labels: + taco: is-great +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a0d2d75739d0093c5d12bb20e50a288228ade37 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/suite.yaml @@ -0,0 +1,33 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: requiredlabels +tests: +- name: must-have-owner + template: template.yaml + constraint: samples/all-must-have-owner/constraint.yaml + cases: + - name: example-allowed + object: samples/all-must-have-owner/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/all-must-have-owner/example_disallowed.yaml + assertions: + - violations: yes + - name: example-disallowed-label-value + object: samples/all-must-have-owner/example_disallowed_label_value.yaml + assertions: + - violations: yes +- name: must-have-key + template: template.yaml + constraint: samples/verify-label-key-only/constraint.yaml + cases: + - name: label-present + object: samples/verify-label-key-only/example_allowed.yaml + assertions: + - violations: no + - name: label-missing + object: samples/verify-label-key-only/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5404fadc723e25d2928797a27b9def9c6eef5c45 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredlabels/template.yaml @@ -0,0 +1,79 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8srequiredlabels + annotations: + metadata.gatekeeper.sh/title: "Required Labels" + metadata.gatekeeper.sh/version: 1.1.2 + description: >- + Requires resources to contain specified labels, with values matching + provided regular expressions. +spec: + crd: + spec: + names: + kind: K8sRequiredLabels + validation: + openAPIV3Schema: + type: object + properties: + message: + type: string + labels: + type: array + description: >- + A list of labels and values the object must specify. + items: + type: object + properties: + key: + type: string + description: >- + The required label. + allowedRegex: + type: string + description: >- + If specified, a regular expression the annotation's value + must match. The value must contain at least one match for + the regular expression. + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + validations: + - expression: '(has(variables.anyObject.metadata) && variables.params.labels.all(entry, has(variables.anyObject.metadata.labels) && entry.key in variables.anyObject.metadata.labels))' + messageExpression: '"missing required label, requires all of: " + variables.params.labels.map(entry, entry.key).join(", ")' + - expression: '(has(variables.anyObject.metadata) && variables.params.labels.all(entry, has(variables.anyObject.metadata.labels) && entry.key in variables.anyObject.metadata.labels && (!has(entry.allowedRegex) || string(variables.anyObject.metadata.labels[entry.key]).matches(string(entry.allowedRegex)))))' + message: "regex mismatch" + - engine: Rego + source: + rego: | + package k8srequiredlabels + + get_message(parameters, _default) := _default { + not parameters.message + } + + get_message(parameters, _) := parameters.message + + violation[{"msg": msg, "details": {"missing_labels": missing}}] { + provided := {label | input.review.object.metadata.labels[label]} + required := {label | label := input.parameters.labels[_].key} + missing := required - provided + count(missing) > 0 + def_msg := sprintf("you must provide labels: %v", [missing]) + msg := get_message(input.parameters, def_msg) + } + + violation[{"msg": msg}] { + value := input.review.object.metadata.labels[key] + expected := input.parameters.labels[_] + expected.key == key + # do not match if allowedRegex is not defined, or is an empty string + expected.allowedRegex != "" + not regex.match(expected.allowedRegex, value) + def_msg := sprintf("Label <%v: %v> does not satisfy allowed regex: %v", [key, value, expected.allowedRegex]) + msg := get_message(input.parameters, def_msg) + } + diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..84fde016ae01f909c4c8c85df13032cdfe8311ee --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredProbes +metadata: + name: must-have-probes +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + probes: ["readinessProbe", "livenessProbe"] + probeTypes: ["tcpSocket", "httpGet", "exec"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4248b67dd3f55a5defb97030397869522662bcb2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_allowed.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Pod +metadata: + name: test-pod1 +spec: + containers: + - name: tomcat + image: tomcat + ports: + - containerPort: 8080 + livenessProbe: + tcpSocket: + port: 80 + initialDelaySeconds: 5 + periodSeconds: 10 + readinessProbe: + tcpSocket: + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 10 + volumes: + - name: cache-volume + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6db251904afa8d12fd57eaf9514f42312b08c837 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_disallowed.yaml @@ -0,0 +1,30 @@ +apiVersion: v1 +kind: Pod +metadata: + name: test-pod1 +spec: + containers: + - name: nginx-1 + image: nginx:1.7.9 + ports: + - containerPort: 80 + livenessProbe: + # tcpSocket: + # port: 80 + # initialDelaySeconds: 5 + # periodSeconds: 10 + volumeMounts: + - mountPath: /tmp/cache + name: cache-volume + - name: tomcat + image: tomcat + ports: + - containerPort: 8080 + readinessProbe: + tcpSocket: + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 10 + volumes: + - name: cache-volume + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6e05364873aebe16690bf10b8bc9109f9137e70d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/example_disallowed2.yaml @@ -0,0 +1,41 @@ +apiVersion: v1 +kind: Pod +metadata: + name: test-pod2 +spec: + containers: + - name: nginx-1 + image: nginx:1.7.9 + ports: + - containerPort: 80 + readinessProbe: + # httpGet: + # path: / + # port: 80 + # initialDelaySeconds: 5 + # periodSeconds: 10 + livenessProbe: + tcpSocket: + port: 80 + initialDelaySeconds: 5 + periodSeconds: 10 + volumeMounts: + - mountPath: /tmp/cache + name: cache-volume + - name: tomcat + image: tomcat + ports: + - containerPort: 8080 + readinessProbe: + tcpSocket: + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 10 + # livenessProbe: + # tcpSocket: + # port: 8080 + # initialDelaySeconds: 5 + # periodSeconds: 10 + volumes: + - name: cache-volume + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ff5e91103749cb482f2747543987121009aafba6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/samples/must-have-probes/update.yaml @@ -0,0 +1,35 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: test-pod1 + spec: + containers: + - name: nginx-1 + image: nginx:1.7.9 + ports: + - containerPort: 80 + livenessProbe: + # tcpSocket: + # port: 80 + # initialDelaySeconds: 5 + # periodSeconds: 10 + volumeMounts: + - mountPath: /tmp/cache + name: cache-volume + - name: tomcat + image: tomcat + ports: + - containerPort: 8080 + readinessProbe: + tcpSocket: + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 10 + volumes: + - name: cache-volume + emptyDir: {} diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..86c2a229e687707a12fd4afac886e5095fbc8a9e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: requiredprobes +tests: +- name: required-probes + template: template.yaml + constraint: samples/must-have-probes/constraint.yaml + cases: + - name: example-allowed + object: samples/must-have-probes/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/must-have-probes/example_disallowed.yaml + assertions: + - violations: yes + - name: example-disallowed2 + object: samples/must-have-probes/example_disallowed2.yaml + assertions: + - violations: yes + - name: update + object: samples/must-have-probes/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4115a6d98985bdf0a1b9d2492b9a2025bcc17a1c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/requiredprobes/template.yaml @@ -0,0 +1,72 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8srequiredprobes + annotations: + metadata.gatekeeper.sh/title: "Required Probes" + metadata.gatekeeper.sh/version: 1.0.1 + description: Requires Pods to have readiness and/or liveness probes. +spec: + crd: + spec: + names: + kind: K8sRequiredProbes + validation: + openAPIV3Schema: + type: object + properties: + probes: + description: "A list of probes that are required (ex: `readinessProbe`)" + type: array + items: + type: string + probeTypes: + description: "The probe must define a field listed in `probeType` in order to satisfy the constraint (ex. `tcpSocket` satisfies `['tcpSocket', 'exec']`)" + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8srequiredprobes + + import data.lib.exclude_update.is_update + + probe_type_set = probe_types { + probe_types := {type | type := input.parameters.probeTypes[_]} + } + + violation[{"msg": msg}] { + # Probe fields are immutable. + not is_update(input.review) + + container := input.review.object.spec.containers[_] + probe := input.parameters.probes[_] + probe_is_missing(container, probe) + msg := get_violation_message(container, input.review, probe) + } + + probe_is_missing(ctr, probe) = true { + not ctr[probe] + } + + probe_is_missing(ctr, probe) = true { + probe_field_empty(ctr, probe) + } + + probe_field_empty(ctr, probe) = true { + probe_fields := {field | ctr[probe][field]} + diff_fields := probe_type_set - probe_fields + count(diff_fields) == count(probe_type_set) + } + + get_violation_message(container, review, probe) = msg { + msg := sprintf("Container <%v> in your <%v> <%v> has no <%v>", [container.name, review.kind.kind, review.object.metadata.name, probe]) + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/README.md b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f398931a96ea9234e25619dee13af924e1063961 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/README.md @@ -0,0 +1,17 @@ +# StorageClass + +The `StorageClass` constraint blocks the creation of PVCs or StatefulSets +where the specified storage class doesn't exist on the cluster, or that no +storage class at all is specified. + +This policy helps prevent workloads from getting stuck indefinitely waiting +for a storage class to provision the persistent storage that will never +happen. This often causes users to get confused as to why their pods are stuck +pending, and requires deleting the StatefulSet and any PVCs it has created along +with redeploying the workload in order to fix. Blocking it up front makes it +much easier to fix before there is a mess to clean up. + +Optionally accepts an `allowedStorageClasses` parameter to restrict PVCs and +StatefulSets to a subset list of allowed storage classes. + +> Please note that this policy requires Gatekeeper v3.9.0 or later. diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ddd6acaa5d8bea08e129cb21362c61ae6be69df7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sStorageClass +metadata: + name: allowed-storageclass +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["PersistentVolumeClaim"] + - apiGroups: ["apps"] + kinds: ["StatefulSet"] + parameters: + includeStorageClassesInMessage: true + allowedStorageClasses: + - allowed-storage-class diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7982eca543622cede2d9556323de76d61b52101a --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: allowed-storage-class-pvc +spec: + accessModes: + - ReadWriteOnce + volumeMode: Filesystem + resources: + requests: + storage: 8Gi + storageClassName: allowed-storage-class diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7f679bb30afcc2ca5ad4f0c6b4e3c123331074e0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: disallowed-storage-class-pvc +spec: + accessModes: + - ReadWriteOnce + volumeMode: Filesystem + resources: + requests: + storage: 8Gi + storageClassName: disallowed-storage-class diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_inventory_allowed_storageclass.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_inventory_allowed_storageclass.yaml new file mode 100644 index 0000000000000000000000000000000000000000..930822f4a48b60dcfd46da9b450c0ad45601efac --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass-allowlist/example_inventory_allowed_storageclass.yaml @@ -0,0 +1,7 @@ +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: allowed-storage-class +provisioner: foo +parameters: +allowVolumeExpansion: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6b49e5dad7012b1bcece4f403fd2ae7c54f877b7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sStorageClass +metadata: + name: storageclass +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["PersistentVolumeClaim"] + - apiGroups: ["apps"] + kinds: ["StatefulSet"] + parameters: + includeStorageClassesInMessage: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_allowed_pvc.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_allowed_pvc.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a8608f53e8f291693d898d77d8dace168ff674b6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_allowed_pvc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: ok +spec: + accessModes: + - ReadWriteOnce + volumeMode: Filesystem + resources: + requests: + storage: 8Gi + storageClassName: somestorageclass diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_allowed_ss.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_allowed_ss.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b1bd5db795668c8d223c863890eec673346a364f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_allowed_ss.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: volumeclaimstorageclass +spec: + selector: + matchLabels: + app: volumeclaimstorageclass + serviceName: volumeclaimstorageclass + replicas: 1 + template: + metadata: + labels: + app: volumeclaimstorageclass + spec: + containers: + - name: main + image: registry.k8s.io/nginx-slim:0.8 + volumeMounts: + - name: data + mountPath: /usr/share/nginx/html + volumeClaimTemplates: + - metadata: + name: data + spec: + accessModes: ["ReadWriteOnce"] + storageClassName: "somestorageclass" + resources: + requests: + storage: 1Gi diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_pvc_badname.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_pvc_badname.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ec75200c432f180b0c205bd27ce205286f92e438 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_pvc_badname.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: badstorageclass +spec: + accessModes: + - ReadWriteOnce + volumeMode: Filesystem + resources: + requests: + storage: 8Gi + storageClassName: badstorageclass diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_pvc_nonamename.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_pvc_nonamename.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2f9cc12ec6aabe17cc8fba85b70c3f40c08866bb --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_pvc_nonamename.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: nostorageclass +spec: + accessModes: + - ReadWriteOnce + volumeMode: Filesystem + resources: + requests: + storage: 8Gi diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_ssvct_badnamename.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_ssvct_badnamename.yaml new file mode 100644 index 0000000000000000000000000000000000000000..58c98fd7d7471efe189dbcce640ec9bd6128cfa3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_ssvct_badnamename.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: badvolumeclaimstorageclass +spec: + selector: + matchLabels: + app: badvolumeclaimstorageclass + serviceName: badvolumeclaimstorageclass + replicas: 1 + template: + metadata: + labels: + app: badvolumeclaimstorageclass + spec: + containers: + - name: main + image: registry.k8s.io/nginx-slim:0.8 + volumeMounts: + - name: data + mountPath: /usr/share/nginx/html + volumeClaimTemplates: + - metadata: + name: data + spec: + accessModes: ["ReadWriteOnce"] + storageClassName: "badstorageclass" + resources: + requests: + storage: 1Gi diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_ssvct_nonamename.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_ssvct_nonamename.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ece7c23dd071ccfe0d4345952aaa8a67e872b61b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_disallowed_ssvct_nonamename.yaml @@ -0,0 +1,29 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: novolumeclaimstorageclass +spec: + selector: + matchLabels: + app: novolumeclaimstorageclass + serviceName: novolumeclaimstorageclass + replicas: 1 + template: + metadata: + labels: + app: novolumeclaimstorageclass + spec: + containers: + - name: main + image: registry.k8s.io/nginx-slim:0.8 + volumeMounts: + - name: data + mountPath: /usr/share/nginx/html + volumeClaimTemplates: + - metadata: + name: data + spec: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 1Gi diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_inventory_allowed_storageclass.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_inventory_allowed_storageclass.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0cdee0aa32d49726217bd0e4c1603a7138b40003 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/samples/storageclass/example_inventory_allowed_storageclass.yaml @@ -0,0 +1,7 @@ +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: somestorageclass +provisioner: foo +parameters: +allowVolumeExpansion: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9cccc2ebb76108a213e41bc88a0aede0ff3facd8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/suite.yaml @@ -0,0 +1,53 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: storageclass +tests: +- name: storageclass + template: template.yaml + constraint: samples/storageclass/constraint.yaml + cases: + - name: example-allowed-pvc + object: samples/storageclass/example_allowed_pvc.yaml + inventory: + - samples/storageclass/example_inventory_allowed_storageclass.yaml + assertions: + - violations: no + - name: example-allowed-ss + object: samples/storageclass/example_allowed_ss.yaml + inventory: + - samples/storageclass/example_inventory_allowed_storageclass.yaml + assertions: + - violations: no + - name: example-disallowed-pvc-badname + object: samples/storageclass/example_disallowed_pvc_badname.yaml + assertions: + - violations: yes + - name: example-disallowed-ssvct-badnamename + object: samples/storageclass/example_disallowed_ssvct_badnamename.yaml + assertions: + - violations: yes + - name: example-disallowed-pvc-nonamename + object: samples/storageclass/example_disallowed_pvc_nonamename.yaml + assertions: + - violations: yes + - name: example-disallowed-ssvct-nonamename + object: samples/storageclass/example_disallowed_ssvct_nonamename.yaml + assertions: + - violations: yes +- name: storageclass-allowlist + template: template.yaml + constraint: samples/storageclass-allowlist/constraint.yaml + cases: + - name: allowed-storage-class-pvc + object: samples/storageclass-allowlist/example_allowed.yaml + inventory: + - samples/storageclass-allowlist/example_inventory_allowed_storageclass.yaml + assertions: + - violations: no + - name: disallowed-storage-class-pvc + object: samples/storageclass-allowlist/example_disallowed.yaml + inventory: + - samples/storageclass-allowlist/example_inventory_allowed_storageclass.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/sync.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/sync.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9d7fd5e1664611605c9a90fe542e627c90e5d0de --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/sync.yaml @@ -0,0 +1,11 @@ +apiVersion: config.gatekeeper.sh/v1alpha1 +kind: Config +metadata: + name: config + namespace: "gatekeeper-system" +spec: + sync: + syncOnly: + - group: "storage.k8s.io" + version: "v1" + kind: "StorageClass" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8c11c1bc8920d96b874c741a5543764be4f877f7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/storageclass/template.yaml @@ -0,0 +1,153 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8sstorageclass + annotations: + metadata.gatekeeper.sh/title: "Storage Class" + metadata.gatekeeper.sh/version: 1.1.2 + metadata.gatekeeper.sh/requires-sync-data: | + "[ + [ + { + "groups":["storage.k8s.io"], + "versions": ["v1"], + "kinds": ["StorageClass"] + } + ] + ]" + description: >- + Requires storage classes to be specified when used. Only Gatekeeper 3.9+ and non-ephemeral containers are supported. +spec: + crd: + spec: + names: + kind: K8sStorageClass + validation: + openAPIV3Schema: + type: object + description: >- + Requires storage classes to be specified when used. + properties: + includeStorageClassesInMessage: + type: boolean + default: true + allowedStorageClasses: + type: array + description: "An optional allow-list of storage classes. If specified, any storage class not in the `allowedStorageClasses` parameter is disallowed." + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8sstorageclass + + is_pvc(obj) { + obj.apiVersion == "v1" + obj.kind == "PersistentVolumeClaim" + } + + is_statefulset(obj) { + obj.apiVersion == "apps/v1" + obj.kind == "StatefulSet" + } + + violation[{"msg": msg}] { + not data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"] + msg := sprintf("StorageClasses not synced. Gatekeeper may be misconfigured. Please have a cluster-admin consult the documentation.", []) + } + + storageclass_allowed(name) { + data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][name] + # support both direct use of * and as the default value + object.get(input.parameters, "allowedStorageClasses", ["*"])[_] == "*" + } + + storageclass_allowed(name) { + data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][name] + input.parameters.allowedStorageClasses[_] == name + } + + violation[{"msg": pvc_storageclass_badname_msg}] { + is_pvc(input.review.object) + not storageclass_allowed(input.review.object.spec.storageClassName) + } + pvc_storageclass_badname_msg := sprintf("pvc did not specify a valid storage class name <%v>. Must be one of [%v]", args) { + input.parameters.includeStorageClassesInMessage + object.get(input.parameters, "allowedStorageClasses", null) == null + args := [ + input.review.object.spec.storageClassName, + concat(", ", [n | data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][n]]) + ] + } else := sprintf("pvc did not specify an allowed and valid storage class name <%v>. Must be one of [%v]", args) { + input.parameters.includeStorageClassesInMessage + object.get(input.parameters, "allowedStorageClasses", null) != null + sc := {n | data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][n]} & {x | x = object.get(input.parameters, "allowedStorageClasses", [])[_]} + args := [ + input.review.object.spec.storageClassName, + concat(", ", sc) + ] + } else := sprintf( + "pvc did not specify a valid storage class name <%v>.", + [input.review.object.spec.storageClassName] + ) + + violation[{"msg": pvc_storageclass_noname_msg}] { + is_pvc(input.review.object) + not input.review.object.spec.storageClassName + } + pvc_storageclass_noname_msg := sprintf("pvc did not specify a storage class name. Must be one of [%v]", args) { + input.parameters.includeStorageClassesInMessage + args := [ + concat(", ", [n | data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][n]]) + ] + } else := sprintf( + "pvc did not specify a storage class name.", + [] + ) + + violation[{"msg": statefulset_vct_badname_msg(vct)}] { + is_statefulset(input.review.object) + vct := input.review.object.spec.volumeClaimTemplates[_] + not storageclass_allowed(vct.spec.storageClassName) + } + statefulset_vct_badname_msg(vct) := msg { + input.parameters.includeStorageClassesInMessage + object.get(input.parameters, "allowedStorageClasses", null) == null + msg := sprintf( + "statefulset did not specify a valid storage class name <%v>. Must be one of [%v]", [ + vct.spec.storageClassName, + concat(", ", [n | data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][n]]) + ]) + } + statefulset_vct_badname_msg(vct) := msg { + input.parameters.includeStorageClassesInMessage + object.get(input.parameters, "allowedStorageClasses", null) != null + sc := {n | data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][n]} & {x | x = object.get(input.parameters, "allowedStorageClasses", [])[_]} + msg := sprintf( + "statefulset did not specify an allowed and valid storage class name <%v>. Must be one of [%v]", [ + vct.spec.storageClassName, + concat(", ", sc) + ]) + } + statefulset_vct_badname_msg(vct) := msg { + not input.parameters.includeStorageClassesInMessage + msg := sprintf( + "statefulset did not specify a valid storage class name <%v>.", [ + vct.spec.storageClassName + ]) + } + + violation[{"msg": statefulset_vct_noname_msg}] { + is_statefulset(input.review.object) + vct := input.review.object.spec.volumeClaimTemplates[_] + not vct.spec.storageClassName + } + statefulset_vct_noname_msg := sprintf("statefulset did not specify a storage class name. Must be one of [%v]", args) { + input.parameters.includeStorageClassesInMessage + args := [ + concat(", ", [n | data.inventory.cluster["storage.k8s.io/v1"]["StorageClass"][n]]) + ] + } else := sprintf( + "statefulset did not specify a storage class name.", + [] + ) diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b4d7f852f1582c7eb62617d3dc2c902b90db1561 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sUniqueIngressHost +metadata: + name: unique-ingress-host +spec: + match: + kinds: + - apiGroups: ["extensions", "networking.k8s.io"] + kinds: ["Ingress"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1e819e200ef6052aa0e32b8a499314d5c07806f0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_allowed.yaml @@ -0,0 +1,27 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-host-allowed + namespace: default +spec: + rules: + - host: example-allowed-host.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 + - host: example-allowed-host1.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx2 + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c61893ed691e8e571d35a3b9bd1d7690e4c2e0ad --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-host-disallowed + namespace: default +spec: + rules: + - host: example-host.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1ca131f1307da94b9b6decff7ef50e5e9f1a06fc --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_disallowed2.yaml @@ -0,0 +1,27 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-host-disallowed2 + namespace: default +spec: + rules: + - host: example-host2.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 + - host: example-host3.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx2 + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_inventory_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_inventory_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..06bc70922c6a906cfb729e8da5ee92366bd43162 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_inventory_disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-host-example + namespace: default +spec: + rules: + - host: example-host.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_inventory_disallowed2.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_inventory_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3f826aeebb5d4fbda9556f0d777cb8d10730a5ac --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/samples/unique-ingress-host/example_inventory_disallowed2.yaml @@ -0,0 +1,17 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-host-example2 + namespace: default +spec: + rules: + - host: example-host2.example.com + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: nginx + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..630838d1d6f51f658b377500d651e45cb5c68a4e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: uniqueingresshost +tests: +- name: unique-ingress-host + template: template.yaml + constraint: samples/unique-ingress-host/constraint.yaml + cases: + - name: example-allowed + object: samples/unique-ingress-host/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/unique-ingress-host/example_disallowed.yaml + inventory: + - samples/unique-ingress-host/example_inventory_disallowed.yaml + assertions: + - violations: yes + - name: example-disallowed2 + object: samples/unique-ingress-host/example_disallowed2.yaml + inventory: + - samples/unique-ingress-host/example_inventory_disallowed2.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/sync.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/sync.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ce25235acfa0b71b9a0675c7fda144a2ca67d346 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/sync.yaml @@ -0,0 +1,17 @@ +apiVersion: config.gatekeeper.sh/v1alpha1 +kind: Config +metadata: + name: config + namespace: "gatekeeper-system" +spec: + sync: + syncOnly: + - group: "extensions" + version: "v1beta1" + kind: "Ingress" + - group: "networking.k8s.io" + version: "v1beta1" + kind: "Ingress" + - group: "networking.k8s.io" + version: "v1" + kind: "Ingress" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c534b2a206302e1df875d2ae216d3420c69c1c26 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueingresshost/template.yaml @@ -0,0 +1,52 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8suniqueingresshost + annotations: + metadata.gatekeeper.sh/title: "Unique Ingress Host" + metadata.gatekeeper.sh/version: 1.0.4 + metadata.gatekeeper.sh/requires-sync-data: | + "[ + [ + { + "groups": ["extensions"], + "versions": ["v1beta1"], + "kinds": ["Ingress"] + }, + { + "groups": ["networking.k8s.io"], + "versions": ["v1beta1", "v1"], + "kinds": ["Ingress"] + } + ] + ]" + description: >- + Requires all Ingress rule hosts to be unique. + + Does not handle hostname wildcards: + https://kubernetes.io/docs/concepts/services-networking/ingress/ +spec: + crd: + spec: + names: + kind: K8sUniqueIngressHost + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8suniqueingresshost + + identical(obj, review) { + obj.metadata.namespace == review.object.metadata.namespace + obj.metadata.name == review.object.metadata.name + } + + violation[{"msg": msg}] { + input.review.kind.kind == "Ingress" + regex.match("^(extensions|networking.k8s.io)$", input.review.kind.group) + host := input.review.object.spec.rules[_].host + other := data.inventory.namespace[_][otherapiversion]["Ingress"][name] + regex.match("^(extensions|networking.k8s.io)/.+$", otherapiversion) + other.spec.rules[_].host == host + not identical(other, input.review) + msg := sprintf("ingress host conflicts with an existing ingress <%v>", [host]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a00cd8b1bdf03072c6ecfe825cda65f31f616b5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/constraint.yaml @@ -0,0 +1,6 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sUniqueServiceSelector +metadata: + name: unique-service-selector + labels: + owner: admin.agilebank.demo diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1d69847d93c2fe0728338442b164f56216bde5ae --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_allowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: gatekeeper-test-service-disallowed + namespace: default +spec: + ports: + - port: 443 + selector: + key: other-value diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6062ce7ff85327372e064760f504fcaee656a8c9 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_disallowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: gatekeeper-test-service-disallowed + namespace: default +spec: + ports: + - port: 443 + selector: + key: value diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_inventory_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_inventory_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..19c1aa7ea0541864a063d4a568fd86c2d8fc8efe --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/samples/unique-service-selector/example_inventory_disallowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: gatekeeper-test-service-example + namespace: default +spec: + ports: + - port: 443 + selector: + key: value diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..abefd5d0e805c2c870aca8069178fbb454b03fb2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/suite.yaml @@ -0,0 +1,19 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: uniqueserviceselector +tests: +- name: unique-service-selector + template: template.yaml + constraint: samples/unique-service-selector/constraint.yaml + cases: + - name: example-allowed + object: samples/unique-service-selector/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/unique-service-selector/example_disallowed.yaml + inventory: + - samples/unique-service-selector/example_inventory_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/sync.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/sync.yaml new file mode 100644 index 0000000000000000000000000000000000000000..433294b0fd6d4d2f87f25485d9060f71e0308caf --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/sync.yaml @@ -0,0 +1,11 @@ +apiVersion: config.gatekeeper.sh/v1alpha1 +kind: Config +metadata: + name: config + namespace: "gatekeeper-system" +spec: + sync: + syncOnly: + - group: "" + version: "v1" + kind: "Service" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8fb9eb2f453a612ae6d4af388e9ace90b3229451 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/uniqueserviceselector/template.yaml @@ -0,0 +1,69 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8suniqueserviceselector + annotations: + metadata.gatekeeper.sh/title: "Unique Service Selector" + metadata.gatekeeper.sh/version: 1.0.2 + metadata.gatekeeper.sh/requires-sync-data: | + "[ + [ + { + "groups":[""], + "versions": ["v1"], + "kinds": ["Service"] + } + ] + ]" + description: >- + Requires Services to have unique selectors within a namespace. + Selectors are considered the same if they have identical keys and values. + Selectors may share a key/value pair so long as there is at least one + distinct key/value pair between them. + + https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service +spec: + crd: + spec: + names: + kind: K8sUniqueServiceSelector + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8suniqueserviceselector + + make_apiversion(kind) = apiVersion { + g := kind.group + v := kind.version + g != "" + apiVersion = sprintf("%v/%v", [g, v]) + } + + make_apiversion(kind) = apiVersion { + kind.group == "" + apiVersion = kind.version + } + + identical(obj, review) { + obj.metadata.namespace == review.namespace + obj.metadata.name == review.name + obj.kind == review.kind.kind + obj.apiVersion == make_apiversion(review.kind) + } + + flatten_selector(obj) = flattened { + selectors := [s | s = concat(":", [key, val]); val = obj.spec.selector[key]] + flattened := concat(",", sort(selectors)) + } + + violation[{"msg": msg}] { + input.review.kind.kind == "Service" + input.review.kind.version == "v1" + input.review.kind.group == "" + input_selector := flatten_selector(input.review.object) + other := data.inventory.namespace[namespace][_]["Service"][name] + not identical(other, input.review) + other_selector := flatten_selector(other) + input_selector == other_selector + msg := sprintf("same selector as service <%v> in namespace <%v>", [name, namespace]) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e4deb1367a382c5d1d4ff157753492bc98e9e062 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/constraint.yaml @@ -0,0 +1,29 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: VerifyDeprecatedAPI +metadata: + name: verify-1.16 +spec: + match: + kinds: + - apiGroups: ["apps"] + kinds: ["Deployment", "ReplicaSet", "StatefulSet", "DaemonSet"] + - apiGroups: ["extensions"] + kinds: ["PodSecurityPolicy", "ReplicaSet", "Deployment", "DaemonSet", "NetworkPolicy"] + parameters: + kvs: + - deprecatedAPI: "apps/v1beta1" + kinds: ["Deployment", "ReplicaSet", "StatefulSet"] + targetAPI: "apps/v1" + - deprecatedAPI: "extensions/v1beta1" + kinds: ["ReplicaSet", "Deployment", "DaemonSet"] + targetAPI: "apps/v1" + - deprecatedAPI: "extensions/v1beta1" + kinds: ["PodSecurityPolicy"] + targetAPI: "policy/v1beta1" + - deprecatedAPI: "apps/v1beta2" + kinds: ["ReplicaSet", "StatefulSet", "Deployment", "DaemonSet"] + targetAPI: "apps/v1" + - deprecatedAPI: "extensions/v1beta1" + kinds: ["NetworkPolicy"] + targetAPI: "networking.k8s.io/v1" + k8sVersion: 1.16 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..55cab6d3b321939712679395f8ab64dec76df285 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/example_allowed.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: allowed-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..16a1958900220af7dc9bda05dffba3025f73e855 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.16/example_disallowed.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: disallowed-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..02b669174dcfdfc94ecbb1b524cb78d976cc9150 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/constraint.yaml @@ -0,0 +1,70 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: VerifyDeprecatedAPI +metadata: + name: verify-1.22 +spec: + match: + kinds: + - apiGroups: ["admissionregistration.k8s.io"] + kinds: ["MutatingWebhookConfiguration", "ValidatingWebhookConfiguration"] + - apiGroups: ["apiextensions.k8s.io"] + kinds: ["CustomResourceDefinition"] + - apiGroups: ["apiregistration.k8s.io"] + kinds: ["APIService"] + - apiGroups: ["authentication.k8s.io"] + kinds: ["TokenReview"] + - apiGroups: ["authorization.k8s.io"] + kinds: ["SubjectAccessReview"] + - apiGroups: ["certificates.k8s.io"] + kinds: ["CertificateSigningRequest"] + - apiGroups: ["coordination.k8s.io"] + kinds: ["Lease"] + - apiGroups: ["extensions", "networking.k8s.io"] + kinds: ["Ingress"] + - apiGroups: ["networking.k8s.io"] + kinds: ["IngressClass"] + - apiGroups: ["rbac.authorization.k8s.io"] + kinds: ["ClusterRole", "ClusterRoleBinding", "Role", "RoleBinding"] + - apiGroups: ["scheduling.k8s.io"] + kinds: ["PriorityClass"] + - apiGroups: ["storage.k8s.io"] + kinds: ["CSIDriver", "CSINode", "StorageClass", "VolumeAttachment"] + parameters: + kvs: + - deprecatedAPI: "admissionregistration.k8s.io/v1beta1" + kinds: ["MutatingWebhookConfiguration", "ValidatingWebhookConfiguration"] + targetAPI: "admissionregistration.k8s.io/v1" + - deprecatedAPI: "apiextensions.k8s.io/v1beta1" + kinds: ["CustomResourceDefinition"] + targetAPI: "apiextensions.k8s.io/v1" + - deprecatedAPI: "apiregistration.k8s.io/v1beta1" + kinds: ["APIService"] + targetAPI: "apiregistration.k8s.io/v1" + - deprecatedAPI: "authentication.k8s.io/v1beta1" + kinds: ["TokenReview"] + targetAPI: "authentication.k8s.io/v1" + - deprecatedAPI: "authorization.k8s.io/v1beta1" + kinds: ["SubjectAccessReview"] + targetAPI: "authorization.k8s.io/v1" + - deprecatedAPI: "certificates.k8s.io/v1beta1" + kinds: ["CertificateSigningRequest"] + targetAPI: "certificates.k8s.io/v1" + - deprecatedAPI: "coordination.k8s.io/v1beta1" + kinds: ["Lease"] + targetAPI: "coordination.k8s.io/v1" + - deprecatedAPI: "extensions/v1beta1" + kinds: ["Ingress"] + targetAPI: "networking.k8s.io/v1" + - deprecatedAPI: "networking.k8s.io/v1beta1" + kinds: ["Ingress", "IngressClass"] + targetAPI: "networking.k8s.io/v1" + - deprecatedAPI: "rbac.authorization.k8s.io/v1beta1" + kinds: ["ClusterRole", "ClusterRoleBinding", "Role", "RoleBinding"] + targetAPI: "rbac.authorization.k8s.io/v1" + - deprecatedAPI: "scheduling.k8s.io/v1beta1" + kinds: ["PriorityClass"] + targetAPI: "scheduling.k8s.io/v1" + - deprecatedAPI: "storage.k8s.io/v1beta1" + kinds: ["CSIDriver", "CSINode", "StorageClass", "VolumeAttachment"] + targetAPI: "storage.k8s.io/v1" + k8sVersion: 1.22 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1989ac9f0d2bc4cbd6c888406ff707cfe98535d3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/example_allowed.yaml @@ -0,0 +1,18 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: allowed-ingress + annotations: + nginx.ingress.kubernetes.io/rewrite-target: / +spec: + ingressClassName: nginx-example + rules: + - http: + paths: + - path: /testpath + pathType: Prefix + backend: + service: + name: test + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2080e87497a641a8104968c45dfdb7b35c1b1728 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.22/example_disallowed.yaml @@ -0,0 +1,18 @@ +apiVersion: networking.k8s.io/v1beta1 +kind: Ingress +metadata: + name: disallowed-ingress + annotations: + nginx.ingress.kubernetes.io/rewrite-target: / +spec: + ingressClassName: nginx-example + rules: + - http: + paths: + - path: /testpath + pathType: Prefix + backend: + service: + name: test + port: + number: 80 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bf1a64e01f5c3b723925449927cec7e9d447a7e8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/constraint.yaml @@ -0,0 +1,43 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: VerifyDeprecatedAPI +metadata: + name: verify-1.25 +spec: + match: + kinds: + - apiGroups: ["batch"] + kinds: ["CronJob"] + - apiGroups: ["discovery.k8s.io"] + kinds: ["EndpointSlice"] + - apiGroups: ["events.k8s.io"] + kinds: ["Event"] + - apiGroups: ["autoscaling"] + kinds: ["HorizontalPodAutoscaler"] + - apiGroups: ["policy"] + kinds: ["PodDisruptionBudget", "PodSecurityPolicy"] + - apiGroups: ["node.k8s.io"] + kinds: ["RuntimeClass"] + parameters: + kvs: + - deprecatedAPI: "batch/v1beta1" + kinds: ["CronJob"] + targetAPI: "batch/v1" + - deprecatedAPI: "discovery.k8s.io/v1beta1" + kinds: ["EndpointSlice"] + targetAPI: "discovery.k8s.io/v1" + - deprecatedAPI: "events.k8s.io/v1beta1" + kinds: ["Event"] + targetAPI: "events.k8s.io/v1" + - deprecatedAPI: "autoscaling/v2beta1" + kinds: ["HorizontalPodAutoscaler"] + targetAPI: "autoscaling/v2" + - deprecatedAPI: "policy/v1beta1" + kinds: ["PodDisruptionBudget"] + targetAPI: "policy/v1" + - deprecatedAPI: "policy/v1beta1" + kinds: ["PodSecurityPolicy"] + targetAPI: "None" + - deprecatedAPI: "node.k8s.io/v1beta1" + kinds: ["RuntimeClass"] + targetAPI: "node.k8s.io/v1" + k8sVersion: 1.25 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4c21ba3372b7ea91b98becb2a8dbc2fb0ac14244 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/example_allowed.yaml @@ -0,0 +1,20 @@ +apiVersion: batch/v1 +kind: CronJob +metadata: + name: allowed-cronjob + namespace: default +spec: + schedule: "* * * * *" + jobTemplate: + spec: + template: + spec: + containers: + - name: hello + image: busybox:1.28 + imagePullPolicy: IfNotPresent + command: + - /bin/sh + - -c + - date; echo Hello from the Kubernetes cluster + restartPolicy: OnFailure diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..26e048335b1f16ca640cf68a7709d5af2fc1f93b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.25/example_disallowed.yaml @@ -0,0 +1,20 @@ +apiVersion: batch/v1beta1 +kind: CronJob +metadata: + name: disallowed-cronjob + namespace: default +spec: + schedule: "* * * * *" + jobTemplate: + spec: + template: + spec: + containers: + - name: hello + image: busybox:1.28 + imagePullPolicy: IfNotPresent + command: + - /bin/sh + - -c + - date; echo Hello from the Kubernetes cluster + restartPolicy: OnFailure diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b23a3cd4a2b89bad6133b1cc572e954eaecabd17 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/constraint.yaml @@ -0,0 +1,20 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: VerifyDeprecatedAPI +metadata: + name: verify-1.26 +spec: + match: + kinds: + - apiGroups: ["flowcontrol.apiserver.k8s.io"] + kinds: ["FlowSchema", "PriorityLevelConfiguration"] + - apiGroups: ["autoscaling"] + kinds: ["HorizontalPodAutoscaler"] + parameters: + kvs: + - deprecatedAPI: "flowcontrol.apiserver.k8s.io/v1beta1" + kinds: ["FlowSchema", "PriorityLevelConfiguration"] + targetAPI: "flowcontrol.apiserver.k8s.io/v1beta3" + - deprecatedAPI: "autoscaling/v2beta2" + kinds: ["HorizontalPodAutoscaler"] + targetAPI: "autoscaling/v2" + k8sVersion: 1.26 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..316a45d338ac4a5f13f9ba3472df1e26a113afc8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/example_allowed.yaml @@ -0,0 +1,21 @@ +apiVersion: flowcontrol.apiserver.k8s.io/v1beta3 +kind: FlowSchema +metadata: + name: allowed-flowcontrol + namespace: default +spec: + matchingPrecedence: 1000 + priorityLevelConfiguration: + name: exempt + rules: + - nonResourceRules: + - nonResourceURLs: + - "/healthz" + - "/livez" + - "/readyz" + verbs: + - "*" + subjects: + - kind: Group + group: + name: "system:unauthenticated" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..340d30096dfa4dbc16a3971444e05af3cb26aca1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.26/example_disallowed.yaml @@ -0,0 +1,21 @@ +apiVersion: flowcontrol.apiserver.k8s.io/v1beta1 +kind: FlowSchema +metadata: + name: disallowed-flowcontrol + namespace: default +spec: + matchingPrecedence: 1000 + priorityLevelConfiguration: + name: exempt + rules: + - nonResourceRules: + - nonResourceURLs: + - "/healthz" + - "/livez" + - "/readyz" + verbs: + - "*" + subjects: + - kind: Group + group: + name: "system:unauthenticated" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ca016c737be4014fbb6eaa46058ca984ed781885 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: VerifyDeprecatedAPI +metadata: + name: verify-1.27 +spec: + match: + kinds: + - apiGroups: ["storage.k8s.io"] + kinds: ["CSIStorageCapacity"] + parameters: + kvs: + - deprecatedAPI: "storage.k8s.io/v1beta1" + kinds: ["CSIStorageCapacity"] + targetAPI: "storage.k8s.io/v1" + k8sVersion: 1.27 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..89f979991fb874ab8ce152d2d72b09540005b38c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/example_allowed.yaml @@ -0,0 +1,5 @@ +apiVersion: storage.k8s.io/v1 +kind: CSIStorageCapacity +metadata: + name: allowed-csistoragecapacity +storageClassName: standard diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e1fe3bb4b5298e5922d619b4eb4e1426a0d015cb --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.27/example_disallowed.yaml @@ -0,0 +1,6 @@ +apiVersion: storage.k8s.io/v1beta1 +kind: CSIStorageCapacity +metadata: + name: allowed-csistoragecapacity + namespace: default +storageClassName: standard diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e9f88278567352c0272665ea1f904879a5a7c259 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: VerifyDeprecatedAPI +metadata: + name: verify-1.29 +spec: + match: + kinds: + - apiGroups: ["flowcontrol.apiserver.k8s.io"] + kinds: ["FlowSchema", "PriorityLevelConfiguration"] + parameters: + kvs: + - deprecatedAPI: "flowcontrol.apiserver.k8s.io/v1beta2" + kinds: ["FlowSchema", "PriorityLevelConfiguration"] + targetAPI: "flowcontrol.apiserver.k8s.io/v1beta3" + k8sVersion: 1.29 diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..316a45d338ac4a5f13f9ba3472df1e26a113afc8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/example_allowed.yaml @@ -0,0 +1,21 @@ +apiVersion: flowcontrol.apiserver.k8s.io/v1beta3 +kind: FlowSchema +metadata: + name: allowed-flowcontrol + namespace: default +spec: + matchingPrecedence: 1000 + priorityLevelConfiguration: + name: exempt + rules: + - nonResourceRules: + - nonResourceURLs: + - "/healthz" + - "/livez" + - "/readyz" + verbs: + - "*" + subjects: + - kind: Group + group: + name: "system:unauthenticated" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0c92b20061d7dc34e52926853a4c93d4a84164eb --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/samples/verify-1.29/example_disallowed.yaml @@ -0,0 +1,21 @@ +apiVersion: flowcontrol.apiserver.k8s.io/v1beta2 +kind: FlowSchema +metadata: + name: disallowed-flowcontrol + namespace: default +spec: + matchingPrecedence: 1000 + priorityLevelConfiguration: + name: exempt + rules: + - nonResourceRules: + - nonResourceURLs: + - "/healthz" + - "/livez" + - "/readyz" + verbs: + - "*" + subjects: + - kind: Group + group: + name: "system:unauthenticated" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..293035c470ad2fec4df5b917d128771365733fbd --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/suite.yaml @@ -0,0 +1,77 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: verifydeprecatedapi +tests: +- name: verifydeprecatedapi-1.16 + template: template.yaml + constraint: samples/verify-1.16/constraint.yaml + cases: + - name: example-allowed + object: samples/verify-1.16/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/verify-1.16/example_disallowed.yaml + assertions: + - violations: yes +- name: verifydeprecatedapi-1.22 + template: template.yaml + constraint: samples/verify-1.22/constraint.yaml + cases: + - name: example-allowed + object: samples/verify-1.22/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/verify-1.22/example_disallowed.yaml + assertions: + - violations: yes +- name: verifydeprecatedapi-1.25 + template: template.yaml + constraint: samples/verify-1.25/constraint.yaml + cases: + - name: example-allowed + object: samples/verify-1.25/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/verify-1.25/example_disallowed.yaml + assertions: + - violations: yes +- name: verifydeprecatedapi-1.26 + template: template.yaml + constraint: samples/verify-1.26/constraint.yaml + cases: + - name: example-allowed + object: samples/verify-1.26/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/verify-1.26/example_disallowed.yaml + assertions: + - violations: yes +- name: verifydeprecatedapi-1.27 + template: template.yaml + constraint: samples/verify-1.27/constraint.yaml + cases: + - name: example-allowed + object: samples/verify-1.27/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/verify-1.27/example_disallowed.yaml + assertions: + - violations: yes +- name: verifydeprecatedapi-1.29 + template: template.yaml + constraint: samples/verify-1.29/constraint.yaml + cases: + - name: example-allowed + object: samples/verify-1.29/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/verify-1.29/example_disallowed.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..073a8767b53ca2b12f4efbb185c4844c3a33c6cf --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/general/verifydeprecatedapi/template.yaml @@ -0,0 +1,67 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: verifydeprecatedapi + annotations: + metadata.gatekeeper.sh/title: "Verify deprecated APIs" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Verifies deprecated Kubernetes APIs to ensure all the API versions are up to date. This template does not apply to audit as audit looks at the resources which are already present in the cluster with non-deprecated API versions. +spec: + crd: + spec: + names: + kind: VerifyDeprecatedAPI + validation: + openAPIV3Schema: + type: object + properties: + kvs: + type: array + description: Deprecated api versions and corresponding kinds + items: + type: object + properties: + deprecatedAPI: + type: string + description: deprecated api + example: flowcontrol.apiserver.k8s.io/v1beta2 + kinds: + type: array + items: + type: string + description: impacted list of kinds + example: '["FlowSchema", "PriorityLevelConfiguration"]' + targetAPI: + type: string + description: target api + example: flowcontrol.apiserver.k8s.io/v1beta3 + k8sVersion: + type: number + description: kubernetes version + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package verifydeprecatedapi + + violation[{"msg": msg}] { + kvs := input.parameters.kvs[_] + kvs.deprecatedAPI == input.review.object.apiVersion + k := kvs.kinds[_] + k == input.review.object.kind + msg := get_message(input.review.object.kind, input.review.object.apiVersion, input.parameters.k8sVersion, kvs.targetAPI) + } + + get_message(kind, apiVersion, k8sVersion, targetAPI) = msg { + not match(targetAPI) + msg := sprintf("API %v for %v is deprecated in Kubernetes version %v, please use %v instead", [kind, apiVersion, k8sVersion, targetAPI]) + } + + get_message(kind, apiVersion, k8sVersion, targetAPI) = msg { + match(targetAPI) + msg := sprintf("API %v for %v is deprecated in Kubernetes version %v, please see Kubernetes API deprecation guide", [kind, apiVersion, k8sVersion]) + } + + match(api) { + api == "None" + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a46b3b512d3a1e1cfd08d9abf5fc95c9b23eca8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/kustomization.yaml @@ -0,0 +1,5 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- general +- pod-security-policy diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/Makefile b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..ed58969f4ddfbc34fc209894db8e572f3d512e85 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/Makefile @@ -0,0 +1,5 @@ +all: test + +.PHONY: test +test: + sh test.sh diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/README.md b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/README.md new file mode 100644 index 0000000000000000000000000000000000000000..bcd38a3669b772b41f5c05263f146828939be35b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/README.md @@ -0,0 +1,43 @@ +# Pod Security Standards + +This library provides Gatekeeper policies that implement the [Kubernetes Pod Security Standards](https://kubernetes.io/docs/concepts/security/pod-security-standards/). These standards define three profiles that cover the security spectrum: + +- **Privileged**: Unrestricted policy, providing the widest possible level of permissions. +- **Baseline**: Minimally restrictive policy which prevents known privilege escalations. Allows the default (minimally specified) Pod configuration. +- **Restricted**: Heavily restricted policy, following current Pod hardening best practices. + +> **Note:** The profiles are **cumulative** - the Restricted profile includes all policies from the Baseline profile, plus additional restrictions. + +These policies were originally based on the deprecated [Pod Security Policy](https://kubernetes.io/docs/concepts/policy/pod-security-policy/) but have been updated to align with the modern Pod Security Standards. + +An administrator can control the following by deploying the corresponding Gatekeeper constraint and constraint templates: + +## Baseline Profile + +These policies prevent known privilege escalations and are recommended as the minimum security configuration for most workloads. + +| Control Aspect | Gatekeeper Constraint and Constraint Template | +|---------------------------------------------------|----------------------------------------------------------| +| Running of privileged containers | [privileged-containers](privileged-containers) | +| Usage of host namespaces | [host-namespaces](host-namespaces) | +| Usage of host networking and ports | [host-network-ports](host-network-ports) | +| Usage of the host filesystem | [host-filesystem](host-filesystem) | +| Linux capabilities | [capabilities](capabilities) | +| The SELinux context of the container | [selinux](selinux) | +| The allowed Proc mount types for the container | [proc-mount](proc-mount) | +| The AppArmor profile used by containers | [apparmor](apparmor) | +| The seccomp profile used by containers | [seccompv2](seccompv2) | +| The sysctl profile used by containers | [forbidden-sysctls](forbidden-sysctls) | + +## Restricted Profile + +These policies provide additional hardening on top of the Baseline profile. Deploy these **in addition to** the Baseline policies for maximum security. + +| Control Aspect | Gatekeeper Constraint and Constraint Template | +|---------------------------------------------------|----------------------------------------------------------| +| Restricting escalation to root privileges | [allow-privilege-escalation](allow-privilege-escalation) | +| Approved list of flex-volume drivers | [flexvolume-drivers](flexvolume-drivers) | +| Allocating an FSGroup that owns the Pod's volumes | [fsgroup](fsgroup) | +| Requiring the use of a read only root file system | [read-only-root-filesystem](read-only-root-filesystem) | +| The user and group IDs of the container | [users](users) | +| Usage of volume types | [volumes](volumes) | diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cfa512d9d66d45816a14b7370b7054063545057d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPAllowPrivilegeEscalationContainer +metadata: + name: psp-allow-privilege-escalation-container +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: ["safeimages.com/*"] diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5992f96b328ed7bd7b45bf42bbabc9e2f66528a6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privilege-escalation-disallowed + labels: + app: nginx-privilege-escalation +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + allowPrivilegeEscalation: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..26c8dd8798b7d0e72405897ea144cfadddc9787f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privilege-escalation-allowed + labels: + app: nginx-privilege-escalation +spec: + containers: + - name: nginx + image: nginx + securityContext: + allowPrivilegeEscalation: false diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_allowed_exempt.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_allowed_exempt.yaml new file mode 100644 index 0000000000000000000000000000000000000000..841e56f89234e9b0f6a3edf3e4635313a71eae20 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_allowed_exempt.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privilege-escalation-disallowed + labels: + app: nginx-privilege-escalation +spec: + containers: + - name: nginx + image: "safeimages.com/nginx" + securityContext: + allowPrivilegeEscalation: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d3648d2f2c4fa299480f8c6234ef5f8b4ae2af9e --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privilege-escalation-disallowed + labels: + app: nginx-privilege-escalation +spec: + containers: + - name: nginx + image: nginx + securityContext: + allowPrivilegeEscalation: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/update.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a79d40a1b4e562bc720570119cce286f1c470f2f --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/samples/psp-allow-privilege-escalation-container/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-privilege-escalation-disallowed + labels: + app: nginx-privilege-escalation + spec: + containers: + - name: nginx + image: nginx + securityContext: + allowPrivilegeEscalation: true diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..345a7582ca929d56aa40eb5d0c35ac37b8b46e07 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: allow-privilege-escalation +tests: + - name: allow-privilege-escalation + template: template.yaml + constraint: samples/psp-allow-privilege-escalation-container/constraint.yaml + cases: + - name: example-allowed + object: samples/psp-allow-privilege-escalation-container/example_allowed.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/psp-allow-privilege-escalation-container/example_disallowed.yaml + assertions: + - violations: yes + - name: disallowed-ephemeral + object: samples/psp-allow-privilege-escalation-container/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-allow-privilege-escalation-container/update.yaml + assertions: + - violations: no + - name: exempted-path + object: samples/psp-allow-privilege-escalation-container/example_allowed_exempt.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a180bc4f0689715cd5a11edb280de95bf6a4da61 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/allow-privilege-escalation/template.yaml @@ -0,0 +1,139 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspallowprivilegeescalationcontainer + annotations: + metadata.gatekeeper.sh/title: "Allow Privilege Escalation in Container" + metadata.gatekeeper.sh/version: 1.1.1 + metadata.gatekeeper.sh/bundle: "pod-security-restricted" + description: >- + Controls restricting escalation to root privileges. Corresponds to the + `allowPrivilegeEscalation` field in a PodSecurityPolicy. For more + information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privilege-escalation +spec: + crd: + spec: + names: + kind: K8sPSPAllowPrivilegeEscalationContainer + validation: + openAPIV3Schema: + type: object + description: >- + Controls restricting escalation to root privileges. Corresponds to the + `allowPrivilegeEscalation` field in a PodSecurityPolicy. For more + information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privilege-escalation + 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)) + ).map(container, container.image) + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && ( + !has(container.securityContext) || + !has(container.securityContext.allowPrivilegeEscalation) || + container.securityContext.allowPrivilegeEscalation != false + ) + ) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: '"Privilege escalation container is not allowed: " + variables.badContainers.map(c, c.image).join(", ")' + - engine: Rego + source: + rego: | + package k8spspallowprivilegeescalationcontainer + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.allowPrivilegeEscalation field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + input_allow_privilege_escalation(c) + msg := sprintf("Privilege escalation container is not allowed: %v", [c.name]) + } + + input_allow_privilege_escalation(c) { + not has_field(c, "securityContext") + } + input_allow_privilege_escalation(c) { + not c.securityContext.allowPrivilegeEscalation == false + } + 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) + } diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6371a1deea9c5e10dcf72f477255684cd88aba9d --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPAppArmor +metadata: + name: psp-apparmor +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + allowedProfiles: + - runtime/default + - localhost/* diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cd6fc5f8102a860f6a8287a86f79d089fcde7593 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-disallowed + annotations: + # apparmor.security.beta.kubernetes.io/pod: unconfined # runtime/default + container.apparmor.security.beta.kubernetes.io/nginx: unconfined + labels: + app: nginx-apparmor +spec: + ephemeralContainers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c9da73f88e2bf0d50baeddb92b1d97d2d3eb4e02 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-allowed + annotations: + container.apparmor.security.beta.kubernetes.io/nginx: runtime/default + labels: + app: nginx-apparmor +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_container.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_container.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a2049533fec7de3b7a4ad3588faa13f772b85a48 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_container.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-allowed + labels: + app: nginx-apparmor +spec: + containers: + - name: nginx + image: nginx + securityContext: + appArmorProfile: + type: "RuntimeDefault" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_override.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_override.yaml new file mode 100644 index 0000000000000000000000000000000000000000..829414322402f478350531169590f668d7d6e8a1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_override.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-allowed + labels: + app: nginx-apparmor +spec: + securityContext: + appArmorProfile: + type: "Unconfined" + containers: + - name: nginx + image: nginx + securityContext: + appArmorProfile: + type: "RuntimeDefault" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_pod.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_pod.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2818d14bcad9a8947eedb9ea9ec8efba937e1012 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_allowed_pod.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-allowed + labels: + app: nginx-apparmor +spec: + securityContext: + appArmorProfile: + type: "RuntimeDefault" + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8ffdaf76896f9868ee7e0c2d6b90cd892a3af238 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-disallowed + annotations: + # apparmor.security.beta.kubernetes.io/pod: unconfined # runtime/default + container.apparmor.security.beta.kubernetes.io/nginx: unconfined + labels: + app: nginx-apparmor +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed_no_profile.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed_no_profile.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a4fd34ce9fb2ec01184895f6582db3787dfaf0d9 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed_no_profile.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-disallowed + labels: + app: nginx-apparmor +spec: + securityContext: + appArmorProfile: + type: "Unconfined" + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed_override.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed_override.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1de74e7d7bec70b6f5771e0d84e1a2344bc3b37c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/samples/psp-apparmor/example_disallowed_override.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-apparmor-allowed + labels: + app: nginx-apparmor +spec: + securityContext: + appArmorProfile: + type: "RuntimeDefault" + containers: + - name: nginx + image: nginx + securityContext: + appArmorProfile: + type: "Unconfined" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/suite.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..861048ba98e1abe3112b23583e54011b283525b3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/suite.yaml @@ -0,0 +1,41 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: apparmor +tests: + - name: apparmor + template: template.yaml + constraint: samples/psp-apparmor/constraint.yaml + cases: + - name: example-allowed + object: samples/psp-apparmor/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-container + object: samples/psp-apparmor/example_allowed_container.yaml + assertions: + - violations: no + - name: example-allowed-pod + object: samples/psp-apparmor/example_allowed_pod.yaml + assertions: + - violations: no + - name: example-allowed-override + object: samples/psp-apparmor/example_allowed_override.yaml + assertions: + - violations: no + - name: example-disallowed + object: samples/psp-apparmor/example_disallowed.yaml + assertions: + - violations: yes + - name: example-disallowed-override + object: samples/psp-apparmor/example_disallowed_override.yaml + assertions: + - violations: yes + - name: example-disallowed-no-profile + object: samples/psp-apparmor/example_disallowed_no_profile.yaml + assertions: + - violations: yes + - name: disallowed-ephemeral + object: samples/psp-apparmor/disallowed_ephemeral.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/template.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c32c546d1d43f8e4b0e0c074f7cbbf890f208df7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/apparmor/template.yaml @@ -0,0 +1,219 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspapparmor + annotations: + metadata.gatekeeper.sh/title: "App Armor" + metadata.gatekeeper.sh/version: 1.1.1 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + description: >- + Configures an allow-list of AppArmor profiles for use by containers. + This corresponds to specific annotations applied to a PodSecurityPolicy. + For information on AppArmor, see + https://kubernetes.io/docs/tutorials/clusters/apparmor/ +spec: + crd: + spec: + names: + kind: K8sPSPAppArmor + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Configures an allow-list of AppArmor profiles for use by containers. + This corresponds to specific annotations applied to a PodSecurityPolicy. + For information on AppArmor, see + https://kubernetes.io/docs/tutorials/clusters/apparmor/ + 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 + allowedProfiles: + description: "An array of AppArmor profiles. Examples: `runtime/default`, `unconfined`." + 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: podAppArmor + expression: 'has(variables.anyObject.spec.securityContext) && has(variables.anyObject.spec.securityContext.appArmorProfile) ? variables.anyObject.spec.securityContext.appArmorProfile : null' + - name: canonicalPodAppArmor + expression: | + variables.podAppArmor == null ? "runtime/default" : + variables.podAppArmor.type == "RuntimeDefault" ? "runtime/default" : + variables.podAppArmor.type == "Unconfined" ? "unconfined" : + variables.podAppArmor.type == "Localhost" ? "localhost/" + variables.podAppArmor.localhostProfile : "" + # break this mapping up by container type (regular/init/ephemeral) to avoid problems with name collisions, + # which may be a problem when running shift-left (no K8s API server to enforce uniqueness of container names) + - name: appArmorByContainer + expression: | + variables.containers.map(container, [container.name, + has(container.securityContext) && has(container.securityContext.appArmorProfile) ? + (container.securityContext.appArmorProfile.type == "RuntimeDefault" ? "runtime/default" : + container.securityContext.appArmorProfile.type == "Unconfined" ? "unconfined" : + container.securityContext.appArmorProfile.type == "Localhost" ? "localhost/" + container.securityContext.appArmorProfile.localhostProfile : "") : + has(variables.anyObject.metadata.annotations) && ("container.apparmor.security.beta.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations ? + variables.anyObject.metadata.annotations["container.apparmor.security.beta.kubernetes.io/" + container.name] : + variables.canonicalPodAppArmor + ]) + - name: appArmorByInitContainer + expression: | + variables.initContainers.map(container, [container.name, + has(container.securityContext) && has(container.securityContext.appArmorProfile) ? + (container.securityContext.appArmorProfile.type == "RuntimeDefault" ? "runtime/default" : + container.securityContext.appArmorProfile.type == "Unconfined" ? "unconfined" : + container.securityContext.appArmorProfile.type == "Localhost" ? "localhost/" + container.securityContext.appArmorProfile.localhostProfile : "") : + has(variables.anyObject.metadata.annotations) && ("container.apparmor.security.beta.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations ? + variables.anyObject.metadata.annotations["container.apparmor.security.beta.kubernetes.io/" + container.name] : + variables.canonicalPodAppArmor + ]) + - name: appArmorByEphemeralContainer + expression: | + variables.ephemeralContainers.map(container, [container.name, + has(container.securityContext) && has(container.securityContext.appArmorProfile) ? + (container.securityContext.appArmorProfile.type == "RuntimeDefault" ? "runtime/default" : + container.securityContext.appArmorProfile.type == "Unconfined" ? "unconfined" : + container.securityContext.appArmorProfile.type == "Localhost" ? "localhost/" + container.securityContext.appArmorProfile.localhostProfile : "") : + has(variables.anyObject.metadata.annotations) && ("container.apparmor.security.beta.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations ? + variables.anyObject.metadata.annotations["container.apparmor.security.beta.kubernetes.io/" + container.name] : + variables.canonicalPodAppArmor + ]) + - 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)) + ).map(container, container.image) + validations: + - expression: | + variables.containers.all(container, + (container.image in variables.exemptImages) || + variables.appArmorByContainer.exists(pair, pair[0] == container.name && pair[1] in variables.params.allowedProfiles) + ) + messageExpression: '"AppArmor profile is not allowed. Allowed Profiles: " + variables.params.allowedProfiles.join(", ")' + - expression: | + variables.initContainers.all(container, + (container.image in variables.exemptImages) || + variables.appArmorByInitContainer.exists(pair, pair[0] == container.name && pair[1] in variables.params.allowedProfiles) + ) + messageExpression: '"AppArmor profile is not allowed. Allowed Profiles: " + variables.params.allowedProfiles.join(", ")' + - expression: | + variables.ephemeralContainers.all(container, + (container.image in variables.exemptImages) || + variables.appArmorByEphemeralContainer.exists(pair, pair[0] == container.name && pair[1] in variables.params.allowedProfiles) + ) + messageExpression: '"AppArmor profile is not allowed. Allowed Profiles: " + variables.params.allowedProfiles.join(", ")' + - engine: Rego + source: + rego: | + package k8spspapparmor + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + container := input_containers[_] + not is_exempt(container) + not input_apparmor_allowed(input.review.object, container) + msg := sprintf("AppArmor profile is not allowed, pod: %v, container: %v. Allowed profiles: %v", [input.review.object.metadata.name, container.name, input.parameters.allowedProfiles]) + } + + input_apparmor_allowed(pod, container) { + get_apparmor_profile(pod, container) == input.parameters.allowedProfiles[_] + } + + 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[_] + } + + get_apparmor_profile(_, container) = out { + profile := object.get(container, ["securityContext", "appArmorProfile"], null) + profile != null + out := canonicalize_apparmor_profile(profile) + } + + get_apparmor_profile(pod, container) = out { + profile := object.get(container, ["securityContext", "appArmorProfile"], null) + profile == null + out := pod.metadata.annotations[sprintf("container.apparmor.security.beta.kubernetes.io/%v", [container.name])] + } + + get_apparmor_profile(pod, container) = out { + profile := object.get(container, ["securityContext", "appArmorProfile"], null) + profile == null + not pod.metadata.annotations[sprintf("container.apparmor.security.beta.kubernetes.io/%v", [container.name])] + out := canonicalize_apparmor_profile(object.get(pod, ["spec", "securityContext", "appArmorProfile"], null)) + } + + canonicalize_apparmor_profile(profile) = out { + profile.type == "RuntimeDefault" + out := "runtime/default" + } + + canonicalize_apparmor_profile(profile) = out { + profile.type == "Unconfined" + out := "unconfined" + } + + canonicalize_apparmor_profile(profile) = out { + profile.type = "Localhost" + out := sprintf("localhost/%s", [profile.localhostProfile]) + } + + canonicalize_apparmor_profile(profile) = out { + profile == null + out := "runtime/default" + } + libs: + - | + 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) + } + diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/kustomization.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/constraint.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9e80bb3a1e157f480350a8ac9938352b71727890 --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/constraint.yaml @@ -0,0 +1,25 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPCapabilities +metadata: + name: psp-capabilities-baseline +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + allowedCapabilities: + - AUDIT_WRITE + - CHOWN + - DAC_OVERRIDE + - FOWNER + - FSETID + - KILL + - MKNOD + - NET_BIND_SERVICE + - NET_RAW + - SETFCAP + - SETGID + - SETPCAP + - SETUID + - SYS_CHROOT diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f7bafdb4d25f8ae85d3f9f4102e2d69a8511576b --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/disallowed_ephemeral.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-disallowed + labels: + owner: me.agilebank.demo +spec: + ephemeralContainers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + securityContext: + capabilities: + add: ["SYS_ADMIN"] + resources: + limits: + cpu: "100m" + memory: "30Mi" diff --git a/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/example_allowed.yaml b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a4a201ba72e6cd9a68a47dcb758fd2c8ae38094c --- /dev/null +++ b/source_data/pinned/gatekeeper-library-repin-1946bc28/1946bc28e133/gatekeeper-library-1946bc28e1337b90b7e675e3f0fa94aa2e92fd6e/library/pod-security-policy/capabilities/samples/psp-capabilities-baseline/example_allowed.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Pod +metadata: + name: opa-allowed + labels: + owner: me.agilebank.demo +spec: + containers: + - name: opa + image: openpolicyagent/opa:0.9.2 + args: + - "run" + - "--server" + - "--addr=localhost:8080" + securityContext: + capabilities: + add: ["NET_BIND_SERVICE"] + resources: + limits: + cpu: "100m" + memory: "30Mi"