diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/constraint_block_host_network.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/constraint_block_host_network.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d468802feda6be3a234dc464f9a6ef855798861 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/constraint_block_host_network.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7a4fa311441a0ec30a4cd6a984a5ac09f00f8c92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/disallowed_ephemeral.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + ephemeralContainers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08b321fe58e5135afc466d4cd4626ff3e5592a34 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/example_allowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-allowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: false + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9000 + hostPort: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a496cd6062640e1765b322a0173c4b38a80f3fa --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/example_disallowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..231096430ee6f449b3d92d6bc5e2cb85aecf8809 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/samples/psp-host-network-ports/update.yaml @@ -0,0 +1,19 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports + spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb7e60a568013303fcf2f1e74ce036fa8033d33a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/suite.yaml @@ -0,0 +1,45 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-network-ports +tests: +- name: use-of-host-networking-ports-blocked + template: template.yaml + constraint: samples/psp-host-network-ports/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-host-network-ports/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no +- name: use-of-host-network-blocked + template: template.yaml + constraint: samples/psp-host-network-ports/constraint_block_host_network.yaml + cases: + - name: example-disallowed + object: samples/psp-host-network-ports/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d697e74e667310d85634493cfcf6ce85eb756091 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.0/template.yaml @@ -0,0 +1,156 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostnetworkingports + annotations: + metadata.gatekeeper.sh/title: "Host Networking Ports" + metadata.gatekeeper.sh/version: 1.1.0 + description: >- + Controls usage of host network namespace by pod containers. 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 +spec: + crd: + spec: + names: + kind: K8sPSPHostNetworkingPorts + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Controls usage of host network namespace by pod containers. 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 + 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 + hostNetwork: + description: "Determines if the policy allows the use of HostNetwork in the pod spec." + type: boolean + min: + description: "The start of the allowed port range, inclusive." + type: integer + max: + description: "The end of the allowed port range, inclusive." + type: integer + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + ( + (!has(variables.params.hostNetwork) || !variables.params.hostNetwork ? (has(variables.anyObject.spec.hostNetwork) && variables.anyObject.spec.hostNetwork) : false) || + (container.ports.all(port, has(port.hostPort) && has(variables.params.min) && port.hostPort < variables.params.min)) || + (container.ports.all(port, has(port.hostPort) && has(variables.params.max) && port.hostPort > variables.params.max)) + ) + ) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed values: " + variables.params' + - engine: Rego + source: + rego: | + package k8spsphostnetworkingports + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.hostNetwork field is immutable. + not is_update(input.review) + + input_share_hostnetwork(input.review.object) + msg := sprintf("The specified hostNetwork and hostPort are not allowed, pod: %v. Allowed values: %v", [input.review.object.metadata.name, input.parameters]) + } + + input_share_hostnetwork(o) { + not input.parameters.hostNetwork + o.spec.hostNetwork + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort < input.parameters.min + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort > input.parameters.max + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.initContainers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + not is_exempt(c) + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..ed3f65eb5f53902591b4fbbf0e3ff30f64134703 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.1 +name: k8spsphostnetworkingports +displayName: Host Networking Ports +createdAt: "2024-06-25T22:51:27Z" +description: Controls usage of host network namespace by pod containers. 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 +digest: e97c59805103b9bcdeec7d92aace619039df7262feb78a91d5657240c3d2700a +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-network-ports +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Networking Ports + Controls usage of host network namespace by pod containers. 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..aba7c24e7660916602abd25aa9c09e1c47bda2a6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: true + min: 80 + max: 9000 \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/constraint_block_host_network.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/constraint_block_host_network.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7ef87dbb392ab89ed18762cb24161faf41f7123e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/constraint_block_host_network.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false + exemptImages: + - "nginx" \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7a4fa311441a0ec30a4cd6a984a5ac09f00f8c92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/disallowed_ephemeral.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + ephemeralContainers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08b321fe58e5135afc466d4cd4626ff3e5592a34 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/example_allowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-allowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: false + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9000 + hostPort: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a496cd6062640e1765b322a0173c4b38a80f3fa --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/example_disallowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..231096430ee6f449b3d92d6bc5e2cb85aecf8809 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/samples/psp-host-network-ports/update.yaml @@ -0,0 +1,19 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports + spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb7e60a568013303fcf2f1e74ce036fa8033d33a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/suite.yaml @@ -0,0 +1,45 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-network-ports +tests: +- name: use-of-host-networking-ports-blocked + template: template.yaml + constraint: samples/psp-host-network-ports/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-host-network-ports/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no +- name: use-of-host-network-blocked + template: template.yaml + constraint: samples/psp-host-network-ports/constraint_block_host_network.yaml + cases: + - name: example-disallowed + object: samples/psp-host-network-ports/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c04d03938cdb8b3c61978dfeed1ff4a5a01fa7d6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.1/template.yaml @@ -0,0 +1,159 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostnetworkingports + annotations: + metadata.gatekeeper.sh/title: "Host Networking Ports" + metadata.gatekeeper.sh/version: 1.1.1 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPHostNetworkingPorts + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + hostNetwork: + description: "Determines if the policy allows the use of HostNetwork in the pod spec." + type: boolean + min: + description: "The start of the allowed port range, inclusive." + type: integer + max: + description: "The end of the allowed port range, inclusive." + type: integer + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + ( + (container.ports.all(port, has(port.hostPort) && has(variables.params.min) && port.hostPort < variables.params.min)) || + (container.ports.all(port, has(port.hostPort) && has(variables.params.max) && port.hostPort > variables.params.max)) + ) + ) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed values: " + variables.params' + - expression: | + (has(request.operation) && request.operation == "UPDATE") || + (!has(variables.params.hostNetwork) || !variables.params.hostNetwork ? (has(variables.anyObject.spec.hostNetwork) && !variables.anyObject.spec.hostNetwork) : true) + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed values: " + variables.params' + - engine: Rego + source: + rego: | + package k8spsphostnetworkingports + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.hostNetwork field is immutable. + not is_update(input.review) + + input_share_hostnetwork(input.review.object) + msg := sprintf("The specified hostNetwork and hostPort are not allowed, pod: %v. Allowed values: %v", [input.review.object.metadata.name, input.parameters]) + } + + input_share_hostnetwork(o) { + not input.parameters.hostNetwork + o.spec.hostNetwork + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort < input.parameters.min + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort > input.parameters.max + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.initContainers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + not is_exempt(c) + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..9900db87a2a4679c327c9ecd8b956ecbb17b4030 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.2 +name: k8spsphostnetworkingports +displayName: Host Networking Ports +createdAt: "2024-08-26T20:25:17Z" +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 +digest: 29aa57aacfb5f7fa92cd7cdc241250ed0ae1619d1706228f03f058e4efe7cabe +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-network-ports +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Networking Ports + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..aba7c24e7660916602abd25aa9c09e1c47bda2a6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: true + min: 80 + max: 9000 \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/constraint_block_host_network.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/constraint_block_host_network.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7ef87dbb392ab89ed18762cb24161faf41f7123e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/constraint_block_host_network.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false + exemptImages: + - "nginx" \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7a4fa311441a0ec30a4cd6a984a5ac09f00f8c92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/disallowed_ephemeral.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + ephemeralContainers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08b321fe58e5135afc466d4cd4626ff3e5592a34 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_allowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-allowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: false + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9000 + hostPort: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_allowed_no_ports.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_allowed_no_ports.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e009decf9069da65cdde451d5541cbc518db6a38 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_allowed_no_ports.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a496cd6062640e1765b322a0173c4b38a80f3fa --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/example_disallowed.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..231096430ee6f449b3d92d6bc5e2cb85aecf8809 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/samples/psp-host-network-ports/update.yaml @@ -0,0 +1,19 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports + spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b0c7f7816cf2d6385154c8e9456614fe67a20699 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/suite.yaml @@ -0,0 +1,50 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-network-ports +tests: +- name: use-of-host-networking-ports-blocked + template: template.yaml + constraint: samples/psp-host-network-ports/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-host-network-ports/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no + - name: no-ports-specified + object: samples/psp-host-network-ports/example_allowed_no_ports.yaml + assertions: + - violations: no +- name: use-of-host-network-blocked + template: template.yaml + constraint: samples/psp-host-network-ports/constraint_block_host_network.yaml + cases: + - name: example-disallowed + object: samples/psp-host-network-ports/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no + diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..218cb36e7d1cee2c14b8437593ba5c646ffc19db --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.2/template.yaml @@ -0,0 +1,159 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostnetworkingports + annotations: + metadata.gatekeeper.sh/title: "Host Networking Ports" + metadata.gatekeeper.sh/version: 1.1.2 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPHostNetworkingPorts + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + hostNetwork: + description: "Determines if the policy allows the use of HostNetwork in the pod spec." + type: boolean + min: + description: "The start of the allowed port range, inclusive." + type: integer + max: + description: "The end of the allowed port range, inclusive." + type: integer + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && has(container.ports) && + ( + (container.ports.all(port, has(port.hostPort) && has(variables.params.min) && port.hostPort < variables.params.min)) || + (container.ports.all(port, has(port.hostPort) && has(variables.params.max) && port.hostPort > variables.params.max)) + ) + ) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed values: " + variables.params' + - expression: | + (has(request.operation) && request.operation == "UPDATE") || + (!has(variables.params.hostNetwork) || !variables.params.hostNetwork ? (has(variables.anyObject.spec.hostNetwork) && !variables.anyObject.spec.hostNetwork) : true) + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name + ". Allowed values: " + variables.params' + - engine: Rego + source: + rego: | + package k8spsphostnetworkingports + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.hostNetwork field is immutable. + not is_update(input.review) + + input_share_hostnetwork(input.review.object) + msg := sprintf("The specified hostNetwork and hostPort are not allowed, pod: %v. Allowed values: %v", [input.review.object.metadata.name, input.parameters]) + } + + input_share_hostnetwork(o) { + not input.parameters.hostNetwork + o.spec.hostNetwork + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort < input.parameters.min + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort > input.parameters.max + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.initContainers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + not is_exempt(c) + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..878f809ba908826248ba9bb46f33f93995d64783 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.3 +name: k8spsphostnetworkingports +displayName: Host Networking Ports +createdAt: "2024-08-29T21:28:18Z" +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 +digest: 751752950daeb4002a10cad6cbeba6a4afe03b98605f32885ae3fe0179eaff67 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-network-ports +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Networking Ports + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/block_host_network/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/block_host_network/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b5f2e9f44a0fd5edbad02b586de7ab2ef5c296f0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/block_host_network/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/port_range_block_host_network/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/port_range_block_host_network/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..46e16454c7d7475546e5bda0375411c3ef3cd1be --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/port_range_block_host_network/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false + min: 80 + max: 9000 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a496cd6062640e1765b322a0173c4b38a80f3fa --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..aba7c24e7660916602abd25aa9c09e1c47bda2a6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: true + min: 80 + max: 9000 \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7a4fa311441a0ec30a4cd6a984a5ac09f00f8c92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/disallowed_ephemeral.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + ephemeralContainers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_in_range.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_in_range.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2b4f7c92660b8a889ef3d995009f524852128686 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_in_range.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-allowed + labels: + app: nginx-host-networking-ports +spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9000 + hostPort: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e009decf9069da65cdde451d5541cbc518db6a38 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8c0b0ef577e15523bc5eaf7678cb35b12c4d9de1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-network-false +spec: + hostNetwork: false + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml new file mode 100644 index 0000000000000000000000000000000000000000..91cd7f4cd7e708d2099ada8bbea7bf312e164f44 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-network-true +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..231096430ee6f449b3d92d6bc5e2cb85aecf8809 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/samples/psp-host-network-ports/update.yaml @@ -0,0 +1,19 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports + spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..44bbab1bea405a348fe76db6ef12fbbe826656ba --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/suite.yaml @@ -0,0 +1,62 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-network-ports +tests: +- name: port-range-with-host-network-allowed + template: template.yaml + constraint: samples/psp-host-network-ports/constraint.yaml + cases: + - name: out-of-range + object: samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed_in_range.yaml + assertions: + - violations: no + - name: out-of-range-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no + - name: no-ports-specified + object: samples/psp-host-network-ports/example_allowed_no_ports.yaml + assertions: + - violations: no +- name: host-network-forbidden + template: template.yaml + constraint: samples/block_host_network/constraint.yaml + cases: + - name: hostnetwork-true + object: samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml + assertions: + - violations: yes + - name: hostnetwork-false + object: samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml + assertions: + - violations: no +- name: port-range-with-host-network-forbidden + template: template.yaml + constraint: samples/port_range_block_host_network/constraint.yaml + cases: + - name: out-of-range-and-host-network-true + object: samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml + assertions: + - violations: yes + - name: in-range-host-network-false + object: samples/psp-host-network-ports/example_allowed_in_range.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no + diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..036a3e045ae25253a4976ca1a73556c11f02e4d7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.3/template.yaml @@ -0,0 +1,165 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostnetworkingports + annotations: + metadata.gatekeeper.sh/title: "Host Networking Ports" + metadata.gatekeeper.sh/version: 1.1.3 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPHostNetworkingPorts + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + hostNetwork: + description: "Determines if the policy allows the use of HostNetwork in the pod spec." + type: boolean + min: + description: "The start of the allowed port range, inclusive." + type: integer + max: + description: "The end of the allowed port range, inclusive." + type: integer + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && has(container.ports) && + ( + (container.ports.all(port, has(port.hostPort) && has(variables.params.min) && port.hostPort < variables.params.min)) || + (container.ports.all(port, has(port.hostPort) && has(variables.params.max) && port.hostPort > variables.params.max)) + ) + ) + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + - name: hostNetworkAllowed + expression: has(variables.params.hostNetwork) && variables.params.hostNetwork + - name: hostNetworkEnabled + expression: has(variables.anyObject.spec.hostNetwork) && variables.anyObject.spec.hostNetwork + - name: hostNetworkViolation + expression: variables.hostNetworkEnabled && !variables.hostNetworkAllowed + validations: + - expression: 'variables.isUpdate || size(variables.badContainers) == 0' + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name' + - expression: variables.isUpdate || !variables.hostNetworkViolation + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name' + - engine: Rego + source: + rego: | + package k8spsphostnetworkingports + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.hostNetwork field is immutable. + not is_update(input.review) + + input_share_hostnetwork(input.review.object) + msg := sprintf("The specified hostNetwork and hostPort are not allowed, pod: %v. Allowed values: %v", [input.review.object.metadata.name, input.parameters]) + } + + input_share_hostnetwork(o) { + not input.parameters.hostNetwork + o.spec.hostNetwork + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort < input.parameters.min + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort > input.parameters.max + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.initContainers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + not is_exempt(c) + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..24366190fb61c67f3a7846d0d131f016a67b2225 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.4 +name: k8spsphostnetworkingports +displayName: Host Networking Ports +createdAt: "2024-08-30T22:03:40Z" +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 +digest: 5e295f3ee2762996e89f926faae128ca3ae86166aac8fb9e518433ba1300deec +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-network-ports +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Networking Ports + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/block_host_network/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/block_host_network/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b5f2e9f44a0fd5edbad02b586de7ab2ef5c296f0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/block_host_network/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c315f9e23706224395cb195fd7faa1c79eacb856 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false + min: 80 + max: 9000 + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0056f9b1d356e4d03473eec89eb174e9ac999cc8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-hn-ok-bad-port + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: safeimages.com/nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a496cd6062640e1765b322a0173c4b38a80f3fa --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b6176404c801611804a72e1a64ad2ddb30fea533 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: true + min: 80 + max: 9000 + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7a4fa311441a0ec30a4cd6a984a5ac09f00f8c92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/disallowed_ephemeral.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + ephemeralContainers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_in_range.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_in_range.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2b4f7c92660b8a889ef3d995009f524852128686 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_in_range.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-allowed + labels: + app: nginx-host-networking-ports +spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9000 + hostPort: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e009decf9069da65cdde451d5541cbc518db6a38 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8c0b0ef577e15523bc5eaf7678cb35b12c4d9de1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-network-false +spec: + hostNetwork: false + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml new file mode 100644 index 0000000000000000000000000000000000000000..91cd7f4cd7e708d2099ada8bbea7bf312e164f44 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-network-true +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e4d013447e4d7629688e7a13dd841b79e88ca682 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-exempted + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: safeimages.com/nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..231096430ee6f449b3d92d6bc5e2cb85aecf8809 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/samples/psp-host-network-ports/update.yaml @@ -0,0 +1,19 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports + spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8879f7fc99cc2d3c378ca1bc597962c70c57e888 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/suite.yaml @@ -0,0 +1,69 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-network-ports +tests: +- name: port-range-with-host-network-allowed + template: template.yaml + constraint: samples/psp-host-network-ports/constraint.yaml + cases: + - name: out-of-range + object: samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed_in_range.yaml + assertions: + - violations: no + - name: out-of-range-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no + - name: no-ports-specified + object: samples/psp-host-network-ports/example_allowed_no_ports.yaml + assertions: + - violations: no + - name: port-violation-exempted + object: samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml + assertions: + - violations: no +- name: host-network-forbidden + template: template.yaml + constraint: samples/block_host_network/constraint.yaml + cases: + - name: hostnetwork-true + object: samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml + assertions: + - violations: yes + - name: hostnetwork-false + object: samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml + assertions: + - violations: no +- name: port-range-with-host-network-forbidden + template: template.yaml + constraint: samples/port_range_block_host_network/constraint.yaml + cases: + - name: out-of-range-and-host-network-true + object: samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml + assertions: + - violations: yes + - name: exempted-image-still-violates-on-hostnetwork + object: samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml + assertions: + - violations: yes + - name: in-range-host-network-false + object: samples/psp-host-network-ports/example_allowed_in_range.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c310ffcbbca5d228fbdb63a9c900951adbfdb8e0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.4/template.yaml @@ -0,0 +1,166 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostnetworkingports + annotations: + metadata.gatekeeper.sh/title: "Host Networking Ports" + metadata.gatekeeper.sh/version: 1.1.4 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPHostNetworkingPorts + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + hostNetwork: + description: "Determines if the policy allows the use of HostNetwork in the pod spec." + type: boolean + min: + description: "The start of the allowed port range, inclusive." + type: integer + max: + description: "The end of the allowed port range, inclusive." + type: integer + 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.ports) && + ( + (container.ports.all(port, has(port.hostPort) && has(variables.params.min) && port.hostPort < variables.params.min)) || + (container.ports.all(port, has(port.hostPort) && has(variables.params.max) && port.hostPort > variables.params.max)) + ) + ) + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + - name: hostNetworkAllowed + expression: has(variables.params.hostNetwork) && variables.params.hostNetwork + - name: hostNetworkEnabled + expression: has(variables.anyObject.spec.hostNetwork) && variables.anyObject.spec.hostNetwork + - name: hostNetworkViolation + expression: variables.hostNetworkEnabled && !variables.hostNetworkAllowed + validations: + - expression: 'variables.isUpdate || size(variables.badContainers) == 0' + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name' + - expression: variables.isUpdate || !variables.hostNetworkViolation + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name' + - engine: Rego + source: + rego: | + package k8spsphostnetworkingports + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.hostNetwork field is immutable. + not is_update(input.review) + + input_share_hostnetwork(input.review.object) + msg := sprintf("The specified hostNetwork and hostPort are not allowed, pod: %v. Allowed values: %v", [input.review.object.metadata.name, input.parameters]) + } + + input_share_hostnetwork(o) { + not input.parameters.hostNetwork + o.spec.hostNetwork + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort < input.parameters.min + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort > input.parameters.max + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.initContainers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + not is_exempt(c) + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..fd1841ad6e88e89ec967c3d3ecb3d057dc85f7c8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.5 +name: k8spsphostnetworkingports +displayName: Host Networking Ports +createdAt: "2026-01-08T18:35:12Z" +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 +digest: 78b9867bb6275bbf243deaec425b42c410b34e6f31cea267ee67017c5aeb1a11 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-network-ports +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Networking Ports + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/block_host_network/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/block_host_network/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b5f2e9f44a0fd5edbad02b586de7ab2ef5c296f0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/block_host_network/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c315f9e23706224395cb195fd7faa1c79eacb856 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: false + min: 80 + max: 9000 + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0056f9b1d356e4d03473eec89eb174e9ac999cc8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-hn-ok-bad-port + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: safeimages.com/nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9a496cd6062640e1765b322a0173c4b38a80f3fa --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b6176404c801611804a72e1a64ad2ddb30fea533 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostNetworkingPorts +metadata: + name: psp-host-network-ports +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + hostNetwork: true + min: 80 + max: 9000 + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7a4fa311441a0ec30a4cd6a984a5ac09f00f8c92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/disallowed_ephemeral.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + ephemeralContainers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_in_range.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_in_range.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2b4f7c92660b8a889ef3d995009f524852128686 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_in_range.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-allowed + labels: + app: nginx-host-networking-ports +spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9000 + hostPort: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e009decf9069da65cdde451d5541cbc518db6a38 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8c0b0ef577e15523bc5eaf7678cb35b12c4d9de1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-network-false +spec: + hostNetwork: false + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml new file mode 100644 index 0000000000000000000000000000000000000000..91cd7f4cd7e708d2099ada8bbea7bf312e164f44 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-network-true +spec: + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e4d013447e4d7629688e7a13dd841b79e88ca682 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-networking-ports-exempted + labels: + app: nginx-host-networking-ports +spec: + hostNetwork: true + containers: + - name: nginx + image: safeimages.com/nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..231096430ee6f449b3d92d6bc5e2cb85aecf8809 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/samples/psp-host-network-ports/update.yaml @@ -0,0 +1,19 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-networking-ports-disallowed + labels: + app: nginx-host-networking-ports + spec: + hostNetwork: true + containers: + - name: nginx + image: nginx + ports: + - containerPort: 9001 + hostPort: 9001 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8879f7fc99cc2d3c378ca1bc597962c70c57e888 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/suite.yaml @@ -0,0 +1,69 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-network-ports +tests: +- name: port-range-with-host-network-allowed + template: template.yaml + constraint: samples/psp-host-network-ports/constraint.yaml + cases: + - name: out-of-range + object: samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-network-ports/example_allowed_in_range.yaml + assertions: + - violations: no + - name: out-of-range-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no + - name: no-ports-specified + object: samples/psp-host-network-ports/example_allowed_no_ports.yaml + assertions: + - violations: no + - name: port-violation-exempted + object: samples/psp-host-network-ports/example_allowed_out_of_range_exempted.yaml + assertions: + - violations: no +- name: host-network-forbidden + template: template.yaml + constraint: samples/block_host_network/constraint.yaml + cases: + - name: hostnetwork-true + object: samples/psp-host-network-ports/example_allowed_no_ports_host_network_true.yaml + assertions: + - violations: yes + - name: hostnetwork-false + object: samples/psp-host-network-ports/example_allowed_no_ports_host_network_false.yaml + assertions: + - violations: no +- name: port-range-with-host-network-forbidden + template: template.yaml + constraint: samples/port_range_block_host_network/constraint.yaml + cases: + - name: out-of-range-and-host-network-true + object: samples/port_range_block_host_network/example_disallowed_out_of_range_host_network_true.yaml + assertions: + - violations: yes + - name: exempted-image-still-violates-on-hostnetwork + object: samples/port_range_block_host_network/example_disallowed_exempted_container_host_network_enabled.yaml + assertions: + - violations: yes + - name: in-range-host-network-false + object: samples/psp-host-network-ports/example_allowed_in_range.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-network-ports/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-network-ports/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f45d1879be9db3ebbfe6ef8c5492a92a0014a793 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-network-ports/1.1.5/template.yaml @@ -0,0 +1,167 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostnetworkingports + annotations: + metadata.gatekeeper.sh/title: "Host Networking Ports" + metadata.gatekeeper.sh/version: 1.1.5 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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 +spec: + crd: + spec: + names: + kind: K8sPSPHostNetworkingPorts + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + hostNetwork: + description: "Determines if the policy allows the use of HostNetwork in the pod spec." + type: boolean + min: + description: "The start of the allowed port range, inclusive." + type: integer + max: + description: "The end of the allowed port range, inclusive." + type: integer + 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.ports) && + ( + (container.ports.all(port, has(port.hostPort) && has(variables.params.min) && port.hostPort < variables.params.min)) || + (container.ports.all(port, has(port.hostPort) && has(variables.params.max) && port.hostPort > variables.params.max)) + ) + ) + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + - name: hostNetworkAllowed + expression: has(variables.params.hostNetwork) && variables.params.hostNetwork + - name: hostNetworkEnabled + expression: has(variables.anyObject.spec.hostNetwork) && variables.anyObject.spec.hostNetwork + - name: hostNetworkViolation + expression: variables.hostNetworkEnabled && !variables.hostNetworkAllowed + validations: + - expression: 'variables.isUpdate || size(variables.badContainers) == 0' + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name' + - expression: variables.isUpdate || !variables.hostNetworkViolation + messageExpression: '"The specified hostNetwork and hostPort are not allowed, pod: " + variables.anyObject.metadata.name' + - engine: Rego + source: + rego: | + package k8spsphostnetworkingports + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.hostNetwork field is immutable. + not is_update(input.review) + + input_share_hostnetwork(input.review.object) + msg := sprintf("The specified hostNetwork and hostPort are not allowed, pod: %v. Allowed values: %v", [input.review.object.metadata.name, input.parameters]) + } + + input_share_hostnetwork(o) { + not input.parameters.hostNetwork + o.spec.hostNetwork + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort < input.parameters.min + } + + input_share_hostnetwork(_) { + hostPort := input_containers[_].ports[_].hostPort + hostPort > input.parameters.max + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.initContainers[_] + not is_exempt(c) + } + + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + not is_exempt(c) + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..10224a87da231e884c70be392c3c626dd91452a2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spsphostprobeslifecycle +displayName: Host Probes and Lifecycle Hooks +createdAt: "2026-01-13T23:17:46Z" +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/ +digest: 214e494c7dcdd252e8338a81a98cba26937dbdafd66fa5b79a64e3b986b58281 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-probes-lifecycle +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Probes and Lifecycle Hooks + 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/ +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9d8a5d702e501727561311267955be23a65ddc32 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostProbesLifecycle +metadata: + name: psp-host-probes-lifecycle +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml new file mode 100644 index 0000000000000000000000000000000000000000..67cb7a40de52c3e5bbdfc70312f6130049b08880 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-ephemeral-lifecycle-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + ephemeralContainers: + - name: debug + image: nginx + lifecycle: + postStart: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7990b9389e2cfa4635ec7369d0e84fa3f609f600 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-ephemeral-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + ephemeralContainers: + - name: debug + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3756d860a690ed54d4ae81b5714234c962b72976 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-init-lifecycle-host-disallowed + labels: + app: nginx +spec: + initContainers: + - name: init + image: nginx + lifecycle: + postStart: + httpGet: + path: / + port: 80 + host: 127.0.0.1 + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c045fe349f354b91d3b4f587f301132b14aaa65c --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-init-probe-host-disallowed + labels: + app: nginx +spec: + initContainers: + - name: init + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b90345fa5b942efdd39f2b7338d96be62d7cffba --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-no-probes-allowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f4b2c73ed4b12626a709aed71b2dc80f94a0ae1c --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-probe-no-host-allowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8a120dc8b6cac0974f8378f29b762e957a117e7a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-lifecycle-poststart-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + lifecycle: + postStart: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d49fcc9cb7a09c65d249c19fdf94175536805d08 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-lifecycle-prestop-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + lifecycle: + preStop: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..148cd5a41abd21ddd37f520daa73f92666f50ae3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-liveness-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2ae0e90612608853cd0f1193cc80092e58faf02d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readiness-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + readinessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8cb8a92c193f01d04e4f0814c6d38ff219cb9120 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-startup-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + startupProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..183cab49e1545c071586d0aa2b87cb757db9e62f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-tcp-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + livenessProbe: + tcpSocket: + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0397a404abf8a7be1b709c003bde99377e67abbe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/samples/psp-host-probes-lifecycle/update.yaml @@ -0,0 +1,20 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-probe-host-update + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e1eea1065813155d2b6e5ac98493836d1df3b3c7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/suite.yaml @@ -0,0 +1,61 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-probes-lifecycle +tests: +- name: host-probes-lifecycle-disallowed + template: template.yaml + constraint: samples/psp-host-probes-lifecycle/constraint.yaml + cases: + - name: example-allowed-no-probes + object: samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml + assertions: + - violations: no + - name: example-allowed-probe-no-host + object: samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml + assertions: + - violations: no + - name: example-disallowed-liveness-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml + assertions: + - violations: yes + - name: example-disallowed-readiness-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml + assertions: + - violations: yes + - name: example-disallowed-startup-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml + assertions: + - violations: yes + - name: example-disallowed-lifecycle-poststart + object: samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml + assertions: + - violations: yes + - name: example-disallowed-lifecycle-prestop + object: samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml + assertions: + - violations: yes + - name: example-disallowed-tcp-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml + assertions: + - violations: yes + - name: disallowed-init-probe + object: samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml + assertions: + - violations: yes + - name: disallowed-ephemeral-probe + object: samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml + assertions: + - violations: yes + - name: disallowed-init-lifecycle + object: samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml + assertions: + - violations: yes + - name: disallowed-ephemeral-lifecycle + object: samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-probes-lifecycle/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7729e4f38ae535a330fc3509d12d780438f22486 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.0/template.yaml @@ -0,0 +1,208 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostprobeslifecycle + annotations: + metadata.gatekeeper.sh/title: "Host Probes and Lifecycle Hooks" + metadata.gatekeeper.sh/version: 1.0.0 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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/ +spec: + crd: + spec: + names: + kind: K8sPSPHostProbesLifecycle + validation: + openAPIV3Schema: + type: object + 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/ + 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: allContainers + expression: 'variables.containers + variables.initContainers + variables.ephemeralContainers' + - name: badProbeContainers + expression: | + variables.allContainers.filter(container, + !(container.image in variables.exemptImages) && + ( + (has(container.livenessProbe) && has(container.livenessProbe.httpGet) && has(container.livenessProbe.httpGet.host) && container.livenessProbe.httpGet.host != "") || + (has(container.livenessProbe) && has(container.livenessProbe.tcpSocket) && has(container.livenessProbe.tcpSocket.host) && container.livenessProbe.tcpSocket.host != "") || + (has(container.readinessProbe) && has(container.readinessProbe.httpGet) && has(container.readinessProbe.httpGet.host) && container.readinessProbe.httpGet.host != "") || + (has(container.readinessProbe) && has(container.readinessProbe.tcpSocket) && has(container.readinessProbe.tcpSocket.host) && container.readinessProbe.tcpSocket.host != "") || + (has(container.startupProbe) && has(container.startupProbe.httpGet) && has(container.startupProbe.httpGet.host) && container.startupProbe.httpGet.host != "") || + (has(container.startupProbe) && has(container.startupProbe.tcpSocket) && has(container.startupProbe.tcpSocket.host) && container.startupProbe.tcpSocket.host != "") + ) + ).map(container, "Container " + container.name + " has probe with host field set") + - name: badLifecycleContainers + expression: | + variables.allContainers.filter(container, + !(container.image in variables.exemptImages) && + ( + (has(container.lifecycle) && has(container.lifecycle.postStart) && has(container.lifecycle.postStart.httpGet) && has(container.lifecycle.postStart.httpGet.host) && container.lifecycle.postStart.httpGet.host != "") || + (has(container.lifecycle) && has(container.lifecycle.postStart) && has(container.lifecycle.postStart.tcpSocket) && has(container.lifecycle.postStart.tcpSocket.host) && container.lifecycle.postStart.tcpSocket.host != "") || + (has(container.lifecycle) && has(container.lifecycle.preStop) && has(container.lifecycle.preStop.httpGet) && has(container.lifecycle.preStop.httpGet.host) && container.lifecycle.preStop.httpGet.host != "") || + (has(container.lifecycle) && has(container.lifecycle.preStop) && has(container.lifecycle.preStop.tcpSocket) && has(container.lifecycle.preStop.tcpSocket.host) && container.lifecycle.preStop.tcpSocket.host != "") + ) + ).map(container, "Container " + container.name + " has lifecycle hook with host field set") + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + validations: + - expression: variables.isUpdate || size(variables.badProbeContainers) == 0 + messageExpression: 'variables.badProbeContainers.join(", ")' + - expression: variables.isUpdate || size(variables.badLifecycleContainers) == 0 + messageExpression: 'variables.badLifecycleContainers.join(", ")' + - engine: Rego + source: + rego: | + package k8spsphostprobeslifecycle + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + probe := get_probe(c) + probe.httpGet.host != "" + msg := sprintf("Container %v has probe with host field set: %v", [c.name, probe.httpGet.host]) + } + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + probe := get_probe(c) + probe.tcpSocket.host != "" + msg := sprintf("Container %v has probe with host field set: %v", [c.name, probe.tcpSocket.host]) + } + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + hook := get_lifecycle_hook(c) + hook.httpGet.host != "" + msg := sprintf("Container %v has lifecycle hook with host field set: %v", [c.name, hook.httpGet.host]) + } + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + hook := get_lifecycle_hook(c) + hook.tcpSocket.host != "" + msg := sprintf("Container %v has lifecycle hook with host field set: %v", [c.name, hook.tcpSocket.host]) + } + + get_probe(c) = probe { + probe := c.livenessProbe + } + + get_probe(c) = probe { + probe := c.readinessProbe + } + + get_probe(c) = probe { + probe := c.startupProbe + } + + get_lifecycle_hook(c) = hook { + hook := c.lifecycle.postStart + } + + get_lifecycle_hook(c) = hook { + hook := c.lifecycle.preStop + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..c224ff1444f7f3c359934f8dd1bbfa83c6532321 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spsphostprobeslifecycle +displayName: Host Probes and Lifecycle Hooks +createdAt: "2026-02-12T23:45:37Z" +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/ +digest: 2ecdfecf68750b0d9878a46ca836723bec0d6e7c8c71c25784814bd5aad25997 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-probes-lifecycle +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Probes and Lifecycle Hooks + 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/ +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9d8a5d702e501727561311267955be23a65ddc32 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostProbesLifecycle +metadata: + name: psp-host-probes-lifecycle +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml new file mode 100644 index 0000000000000000000000000000000000000000..67cb7a40de52c3e5bbdfc70312f6130049b08880 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-ephemeral-lifecycle-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + ephemeralContainers: + - name: debug + image: nginx + lifecycle: + postStart: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7990b9389e2cfa4635ec7369d0e84fa3f609f600 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-ephemeral-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + ephemeralContainers: + - name: debug + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3756d860a690ed54d4ae81b5714234c962b72976 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-init-lifecycle-host-disallowed + labels: + app: nginx +spec: + initContainers: + - name: init + image: nginx + lifecycle: + postStart: + httpGet: + path: / + port: 80 + host: 127.0.0.1 + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c045fe349f354b91d3b4f587f301132b14aaa65c --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-init-probe-host-disallowed + labels: + app: nginx +spec: + initContainers: + - name: init + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b90345fa5b942efdd39f2b7338d96be62d7cffba --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-no-probes-allowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f4b2c73ed4b12626a709aed71b2dc80f94a0ae1c --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-probe-no-host-allowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8a120dc8b6cac0974f8378f29b762e957a117e7a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-lifecycle-poststart-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + lifecycle: + postStart: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d49fcc9cb7a09c65d249c19fdf94175536805d08 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-lifecycle-prestop-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + lifecycle: + preStop: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..148cd5a41abd21ddd37f520daa73f92666f50ae3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-liveness-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2ae0e90612608853cd0f1193cc80092e58faf02d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readiness-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + readinessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8cb8a92c193f01d04e4f0814c6d38ff219cb9120 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-startup-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + startupProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml new file mode 100644 index 0000000000000000000000000000000000000000..183cab49e1545c071586d0aa2b87cb757db9e62f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-tcp-probe-host-disallowed + labels: + app: nginx +spec: + containers: + - name: nginx + image: nginx + livenessProbe: + tcpSocket: + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0397a404abf8a7be1b709c003bde99377e67abbe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/samples/psp-host-probes-lifecycle/update.yaml @@ -0,0 +1,20 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-probe-host-update + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx + livenessProbe: + httpGet: + path: / + port: 80 + host: 127.0.0.1 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e1eea1065813155d2b6e5ac98493836d1df3b3c7 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/suite.yaml @@ -0,0 +1,61 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-probes-lifecycle +tests: +- name: host-probes-lifecycle-disallowed + template: template.yaml + constraint: samples/psp-host-probes-lifecycle/constraint.yaml + cases: + - name: example-allowed-no-probes + object: samples/psp-host-probes-lifecycle/example_allowed_no_probes.yaml + assertions: + - violations: no + - name: example-allowed-probe-no-host + object: samples/psp-host-probes-lifecycle/example_allowed_probe_no_host.yaml + assertions: + - violations: no + - name: example-disallowed-liveness-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_liveness_probe.yaml + assertions: + - violations: yes + - name: example-disallowed-readiness-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_readiness_probe.yaml + assertions: + - violations: yes + - name: example-disallowed-startup-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_startup_probe.yaml + assertions: + - violations: yes + - name: example-disallowed-lifecycle-poststart + object: samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_poststart.yaml + assertions: + - violations: yes + - name: example-disallowed-lifecycle-prestop + object: samples/psp-host-probes-lifecycle/example_disallowed_lifecycle_prestop.yaml + assertions: + - violations: yes + - name: example-disallowed-tcp-probe + object: samples/psp-host-probes-lifecycle/example_disallowed_tcp_probe.yaml + assertions: + - violations: yes + - name: disallowed-init-probe + object: samples/psp-host-probes-lifecycle/disallowed_init_probe.yaml + assertions: + - violations: yes + - name: disallowed-ephemeral-probe + object: samples/psp-host-probes-lifecycle/disallowed_ephemeral_probe.yaml + assertions: + - violations: yes + - name: disallowed-init-lifecycle + object: samples/psp-host-probes-lifecycle/disallowed_init_lifecycle.yaml + assertions: + - violations: yes + - name: disallowed-ephemeral-lifecycle + object: samples/psp-host-probes-lifecycle/disallowed_ephemeral_lifecycle.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-probes-lifecycle/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..00fd835e35b999073779f9f17614ea43357c2881 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-probes-lifecycle/1.0.1/template.yaml @@ -0,0 +1,208 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostprobeslifecycle + annotations: + metadata.gatekeeper.sh/title: "Host Probes and Lifecycle Hooks" + metadata.gatekeeper.sh/version: 1.0.1 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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/ +spec: + crd: + spec: + names: + kind: K8sPSPHostProbesLifecycle + validation: + openAPIV3Schema: + type: object + 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/ + 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: allContainers + expression: 'variables.containers + variables.initContainers + variables.ephemeralContainers' + - name: badProbeContainers + expression: | + variables.allContainers.filter(container, + !(container.image in variables.exemptImages) && + ( + (has(container.livenessProbe) && has(container.livenessProbe.httpGet) && has(container.livenessProbe.httpGet.host) && container.livenessProbe.httpGet.host != "") || + (has(container.livenessProbe) && has(container.livenessProbe.tcpSocket) && has(container.livenessProbe.tcpSocket.host) && container.livenessProbe.tcpSocket.host != "") || + (has(container.readinessProbe) && has(container.readinessProbe.httpGet) && has(container.readinessProbe.httpGet.host) && container.readinessProbe.httpGet.host != "") || + (has(container.readinessProbe) && has(container.readinessProbe.tcpSocket) && has(container.readinessProbe.tcpSocket.host) && container.readinessProbe.tcpSocket.host != "") || + (has(container.startupProbe) && has(container.startupProbe.httpGet) && has(container.startupProbe.httpGet.host) && container.startupProbe.httpGet.host != "") || + (has(container.startupProbe) && has(container.startupProbe.tcpSocket) && has(container.startupProbe.tcpSocket.host) && container.startupProbe.tcpSocket.host != "") + ) + ).map(container, "Container " + container.name + " has probe with host field set") + - name: badLifecycleContainers + expression: | + variables.allContainers.filter(container, + !(container.image in variables.exemptImages) && + ( + (has(container.lifecycle) && has(container.lifecycle.postStart) && has(container.lifecycle.postStart.httpGet) && has(container.lifecycle.postStart.httpGet.host) && container.lifecycle.postStart.httpGet.host != "") || + (has(container.lifecycle) && has(container.lifecycle.postStart) && has(container.lifecycle.postStart.tcpSocket) && has(container.lifecycle.postStart.tcpSocket.host) && container.lifecycle.postStart.tcpSocket.host != "") || + (has(container.lifecycle) && has(container.lifecycle.preStop) && has(container.lifecycle.preStop.httpGet) && has(container.lifecycle.preStop.httpGet.host) && container.lifecycle.preStop.httpGet.host != "") || + (has(container.lifecycle) && has(container.lifecycle.preStop) && has(container.lifecycle.preStop.tcpSocket) && has(container.lifecycle.preStop.tcpSocket.host) && container.lifecycle.preStop.tcpSocket.host != "") + ) + ).map(container, "Container " + container.name + " has lifecycle hook with host field set") + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + validations: + - expression: variables.isUpdate || size(variables.badProbeContainers) == 0 + messageExpression: 'variables.badProbeContainers.join(", ")' + - expression: variables.isUpdate || size(variables.badLifecycleContainers) == 0 + messageExpression: 'variables.badLifecycleContainers.join(", ")' + - engine: Rego + source: + rego: | + package k8spsphostprobeslifecycle + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + probe := get_probe(c) + probe.httpGet.host != "" + msg := sprintf("Container %v has probe with host field set: %v", [c.name, probe.httpGet.host]) + } + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + probe := get_probe(c) + probe.tcpSocket.host != "" + msg := sprintf("Container %v has probe with host field set: %v", [c.name, probe.tcpSocket.host]) + } + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + hook := get_lifecycle_hook(c) + hook.httpGet.host != "" + msg := sprintf("Container %v has lifecycle hook with host field set: %v", [c.name, hook.httpGet.host]) + } + + violation[{"msg": msg, "details": {}}] { + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + hook := get_lifecycle_hook(c) + hook.tcpSocket.host != "" + msg := sprintf("Container %v has lifecycle hook with host field set: %v", [c.name, hook.tcpSocket.host]) + } + + get_probe(c) = probe { + probe := c.livenessProbe + } + + get_probe(c) = probe { + probe := c.readinessProbe + } + + get_probe(c) = probe { + probe := c.startupProbe + } + + get_lifecycle_hook(c) = hook { + hook := c.lifecycle.postStart + } + + get_lifecycle_hook(c) = hook { + hook := c.lifecycle.preStop + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..198314eef57ab712a56270132460a5d98432e305 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spsphostprocess +displayName: Host Process +createdAt: "2026-01-13T23:17:46Z" +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/ +digest: 2303642cbb54bd0fe2dd666cc64e2f3a5f651e71cdb06158b8b53e2bfde895e4 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-process +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Process + 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/ +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-process/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a6d8f00b0650de3b907e74c5147225a6aa261d26 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostProcess +metadata: + name: psp-host-process +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ea829ffe0d08e2000f57de7bf9a1c0b3872df47a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/disallowed_init.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/disallowed_init.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8d71f9adaad3e631a211a2b996b16b03c9e09b12 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/disallowed_init.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + containers: + - name: nginx + image: nginx + initContainers: + - name: nginx-init + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e9b3c53cc62542cc5014855bd9bdf9e553f6f1ef --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_allowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-allowed + labels: + app: nginx-host-process +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_disallowed_container_level.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_disallowed_container_level.yaml new file mode 100644 index 0000000000000000000000000000000000000000..67c7d96b426b284cf66ad39aa48487d2dc433b22 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_disallowed_container_level.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + containers: + - name: nginx + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_disallowed_pod_level.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_disallowed_pod_level.yaml new file mode 100644 index 0000000000000000000000000000000000000000..533259916b9ff537ce42ccb05f79c51a094af251 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/example_disallowed_pod_level.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + securityContext: + windowsOptions: + hostProcess: true + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f244a763dae7674c1fb7deae0d28ea4d9d016526 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/samples/psp-host-process/update.yaml @@ -0,0 +1,18 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process + spec: + containers: + - name: nginx + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..60194040fdf46ec021bd1c49162672d8c8d66449 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/suite.yaml @@ -0,0 +1,33 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-process +tests: +- name: host-process-disallowed + template: template.yaml + constraint: samples/psp-host-process/constraint.yaml + cases: + - name: example-disallowed-pod-level + object: samples/psp-host-process/example_disallowed_pod_level.yaml + assertions: + - violations: yes + - name: example-disallowed-container-level + object: samples/psp-host-process/example_disallowed_container_level.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-process/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-process/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: disallowed-init + object: samples/psp-host-process/disallowed_init.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-process/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..44448cc6d4fd686ad4c3008c14e5e48a8c53190f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.0/template.yaml @@ -0,0 +1,156 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostprocess + annotations: + metadata.gatekeeper.sh/title: "Host Process" + metadata.gatekeeper.sh/version: 1.0.0 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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/ +spec: + crd: + spec: + names: + kind: K8sPSPHostProcess + validation: + openAPIV3Schema: + type: object + 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/ + 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: podHostProcess + expression: | + has(variables.anyObject.spec.securityContext) && + has(variables.anyObject.spec.securityContext.windowsOptions) && + has(variables.anyObject.spec.securityContext.windowsOptions.hostProcess) && + variables.anyObject.spec.securityContext.windowsOptions.hostProcess + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + has(container.securityContext) && + has(container.securityContext.windowsOptions) && + has(container.securityContext.windowsOptions.hostProcess) && + container.securityContext.windowsOptions.hostProcess + ).map(container, "HostProcess container is not allowed: " + container.name + ", securityContext.windowsOptions.hostProcess: true") + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + validations: + - expression: 'variables.isUpdate || !variables.podHostProcess' + messageExpression: '"HostProcess is not allowed at pod level: " + variables.anyObject.metadata.name' + - expression: variables.isUpdate || size(variables.badContainers) == 0 + messageExpression: 'variables.badContainers.join(", ")' + - engine: Rego + source: + rego: | + package k8spsphostprocess + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.securityContext.windowsOptions.hostProcess field is immutable. + not is_update(input.review) + + # Check pod-level securityContext + input.review.object.spec.securityContext.windowsOptions.hostProcess == true + msg := sprintf("HostProcess is not allowed at pod level: %v", [input.review.object.metadata.name]) + } + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.windowsOptions.hostProcess field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + c.securityContext.windowsOptions.hostProcess == true + msg := sprintf("HostProcess container is not allowed: %v, securityContext.windowsOptions.hostProcess: true", [c.name]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..6d767ec13a5a8775290c091479f4e234e9011a28 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spsphostprocess +displayName: Host Process +createdAt: "2026-02-12T23:45:37Z" +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/ +digest: 88dc62d9b5aaabafa7a72976b23a7b5fb5d0af184c1afadd625f4b9877b366c0 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/host-process +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Host Process + 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/ +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/host-process/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a6d8f00b0650de3b907e74c5147225a6aa261d26 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPHostProcess +metadata: + name: psp-host-process +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ea829ffe0d08e2000f57de7bf9a1c0b3872df47a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/disallowed_init.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/disallowed_init.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8d71f9adaad3e631a211a2b996b16b03c9e09b12 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/disallowed_init.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + containers: + - name: nginx + image: nginx + initContainers: + - name: nginx-init + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e9b3c53cc62542cc5014855bd9bdf9e553f6f1ef --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_allowed.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-allowed + labels: + app: nginx-host-process +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_disallowed_container_level.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_disallowed_container_level.yaml new file mode 100644 index 0000000000000000000000000000000000000000..67c7d96b426b284cf66ad39aa48487d2dc433b22 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_disallowed_container_level.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + containers: + - name: nginx + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_disallowed_pod_level.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_disallowed_pod_level.yaml new file mode 100644 index 0000000000000000000000000000000000000000..533259916b9ff537ce42ccb05f79c51a094af251 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/example_disallowed_pod_level.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process +spec: + securityContext: + windowsOptions: + hostProcess: true + hostNetwork: true + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f244a763dae7674c1fb7deae0d28ea4d9d016526 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/samples/psp-host-process/update.yaml @@ -0,0 +1,18 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-host-process-disallowed + labels: + app: nginx-host-process + spec: + containers: + - name: nginx + image: nginx + securityContext: + windowsOptions: + hostProcess: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..60194040fdf46ec021bd1c49162672d8c8d66449 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/suite.yaml @@ -0,0 +1,33 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: host-process +tests: +- name: host-process-disallowed + template: template.yaml + constraint: samples/psp-host-process/constraint.yaml + cases: + - name: example-disallowed-pod-level + object: samples/psp-host-process/example_disallowed_pod_level.yaml + assertions: + - violations: yes + - name: example-disallowed-container-level + object: samples/psp-host-process/example_disallowed_container_level.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-host-process/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-host-process/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: disallowed-init + object: samples/psp-host-process/disallowed_init.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-host-process/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dd0469ffa1eda5a47bf67139ade5a96fe91f34c4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/host-process/1.0.1/template.yaml @@ -0,0 +1,156 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spsphostprocess + annotations: + metadata.gatekeeper.sh/title: "Host Process" + metadata.gatekeeper.sh/version: 1.0.1 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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/ +spec: + crd: + spec: + names: + kind: K8sPSPHostProcess + validation: + openAPIV3Schema: + type: object + 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/ + 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: podHostProcess + expression: | + has(variables.anyObject.spec.securityContext) && + has(variables.anyObject.spec.securityContext.windowsOptions) && + has(variables.anyObject.spec.securityContext.windowsOptions.hostProcess) && + variables.anyObject.spec.securityContext.windowsOptions.hostProcess + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + has(container.securityContext) && + has(container.securityContext.windowsOptions) && + has(container.securityContext.windowsOptions.hostProcess) && + container.securityContext.windowsOptions.hostProcess + ).map(container, "HostProcess container is not allowed: " + container.name + ", securityContext.windowsOptions.hostProcess: true") + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + validations: + - expression: 'variables.isUpdate || !variables.podHostProcess' + messageExpression: '"HostProcess is not allowed at pod level: " + variables.anyObject.metadata.name' + - expression: variables.isUpdate || size(variables.badContainers) == 0 + messageExpression: 'variables.badContainers.join(", ")' + - engine: Rego + source: + rego: | + package k8spsphostprocess + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.securityContext.windowsOptions.hostProcess field is immutable. + not is_update(input.review) + + # Check pod-level securityContext + input.review.object.spec.securityContext.windowsOptions.hostProcess == true + msg := sprintf("HostProcess is not allowed at pod level: %v", [input.review.object.metadata.name]) + } + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.windowsOptions.hostProcess field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + c.securityContext.windowsOptions.hostProcess == true + msg := sprintf("HostProcess container is not allowed: %v, securityContext.windowsOptions.hostProcess: true", [c.name]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..ef28bc00557397feee7fb2bcfb2919251823bb82 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspprivilegedcontainer +displayName: Privileged Container +createdAt: "2022-09-26T17:28:27Z" +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 +digest: 59c6df06dd8c6655b879dd2aa8fd50dc759e7a5c0df8dfd49de7830ce1d6374c +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/privileged-containers +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Privileged Container + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b246b244a5bf39f961f201ee486db6d87ff5133d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/constraint.yaml @@ -0,0 +1,10 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPPrivilegedContainer +metadata: + name: psp-privileged-container +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + excludedNamespaces: ["kube-system"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e8c8b99453ab07993a68d8c9fef39b7dacc8d442 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb65a2c0e7e975598e22224a8813d9e83085a371 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..936a24f8ed59ef170c6b07f7f96b9c051c5b4475 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/samples/psp-privileged-container/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..593f960156054fc1ebfb58927289a1ebfac7b180 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: privileged-containers +tests: +- name: privileged-containers-disallowed + template: template.yaml + constraint: samples/psp-privileged-container/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-privileged-container/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-privileged-container/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-privileged-container/disallowed_ephemeral.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e1434ebdd7ad05fca0acc6b2a427317f7fce90f2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.0/template.yaml @@ -0,0 +1,82 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprivilegedcontainer + annotations: + metadata.gatekeeper.sh/title: "Privileged Container" + metadata.gatekeeper.sh/version: 1.0.0 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPPrivilegedContainer + validation: + openAPIV3Schema: + type: object + 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 + 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 k8spspprivileged + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + c := input_containers[_] + not is_exempt(c) + c.securityContext.privileged + msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..12b37abb7d65508f8420ad56fefabf4f4701608a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspprivilegedcontainer +displayName: Privileged Container +createdAt: "2023-05-23T09:47:31Z" +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 +digest: c8e6071091c7442d217106f83cc8b63cfe3bf48f49196435c5f974ec5cc094d5 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/privileged-containers +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Privileged Container + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b246b244a5bf39f961f201ee486db6d87ff5133d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/constraint.yaml @@ -0,0 +1,10 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPPrivilegedContainer +metadata: + name: psp-privileged-container +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + excludedNamespaces: ["kube-system"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e8c8b99453ab07993a68d8c9fef39b7dacc8d442 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb65a2c0e7e975598e22224a8813d9e83085a371 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..936a24f8ed59ef170c6b07f7f96b9c051c5b4475 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-container/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08f36044ca9c28b6ef4da177e5ebadcdb9b56fe2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/samples/psp-privileged-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-privileged-disallowed + labels: + app: nginx-privileged + spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c2e484fc5fef9b5b660532441f5af9a922cc0c7c --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: privileged-containers +tests: +- name: privileged-containers-disallowed + template: template.yaml + constraint: samples/psp-privileged-container/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-privileged-container/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-privileged-container/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-privileged-container/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-privileged-container/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c552c193d1722f7b9638199c6ef6f24bcace4c28 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.0.1/template.yaml @@ -0,0 +1,92 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprivilegedcontainer + annotations: + metadata.gatekeeper.sh/title: "Privileged Container" + metadata.gatekeeper.sh/version: 1.0.1 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPPrivilegedContainer + validation: + openAPIV3Schema: + type: object + 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 + 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 k8spspprivileged + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.privileged field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + c.securityContext.privileged + msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..0409a125956082b89fb1466b957363edc2136461 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.0 +name: k8spspprivilegedcontainer +displayName: Privileged Container +createdAt: "2024-06-03T23:35:27Z" +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 +digest: 967e9d9abf48b1686b497dd3e2f3a870b9f5b51c5ab1d837ea0ff680fbfaa7c6 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/privileged-containers +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Privileged Container + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b246b244a5bf39f961f201ee486db6d87ff5133d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/constraint.yaml @@ -0,0 +1,10 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPPrivilegedContainer +metadata: + name: psp-privileged-container +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + excludedNamespaces: ["kube-system"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e8c8b99453ab07993a68d8c9fef39b7dacc8d442 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb65a2c0e7e975598e22224a8813d9e83085a371 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..936a24f8ed59ef170c6b07f7f96b9c051c5b4475 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-container/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08f36044ca9c28b6ef4da177e5ebadcdb9b56fe2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/samples/psp-privileged-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-privileged-disallowed + labels: + app: nginx-privileged + spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c2e484fc5fef9b5b660532441f5af9a922cc0c7c --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: privileged-containers +tests: +- name: privileged-containers-disallowed + template: template.yaml + constraint: samples/psp-privileged-container/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-privileged-container/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-privileged-container/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-privileged-container/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-privileged-container/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bd8452e6791826423c6ccb1b8ece2ac503d524e1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.0/template.yaml @@ -0,0 +1,126 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprivilegedcontainer + annotations: + metadata.gatekeeper.sh/title: "Privileged Container" + metadata.gatekeeper.sh/version: 1.1.0 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPPrivilegedContainer + validation: + openAPIV3Schema: + type: object + 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 + properties: + exemptImages: + description: >- + Any container that uses an image that matches an entry in this list will be excluded + from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`. + + It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name) + in order to avoid unexpectedly exempting images from an untrusted repository. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + (has(container.securityContext) && has(container.securityContext.privileged) && container.securityContext.privileged == true) + ).map(container, "Privileged container is not allowed: " + container.name +", securityContext: " + container.securityContext) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: 'variables.badContainers.join("\n")' + - engine: Rego + source: + rego: | + package k8spspprivileged + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.privileged field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + c.securityContext.privileged + msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..5f2dffd891d10a89e40033f743087027cf2cfa84 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.1 +name: k8spspprivilegedcontainer +displayName: Privileged Container +createdAt: "2024-08-30T22:14:08Z" +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 +digest: 5d9b2b840bb1f530d3e66cb44d4ab170e7d4b7895d722a51999134a032b61c6f +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/privileged-containers +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Privileged Container + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bf2fe519d6ff8181b67b06385da3203a0e34582f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPPrivilegedContainer +metadata: + name: psp-privileged-container +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + excludedNamespaces: ["kube-system"] + parameters: + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e8c8b99453ab07993a68d8c9fef39b7dacc8d442 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb65a2c0e7e975598e22224a8813d9e83085a371 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_allowed_exempt.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_allowed_exempt.yaml new file mode 100644 index 0000000000000000000000000000000000000000..782a6036a1e9e818b8e0f971b6c0bd3da8192781 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_allowed_exempt.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed-exempt + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: safeimages.com/nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..936a24f8ed59ef170c6b07f7f96b9c051c5b4475 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-container/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08f36044ca9c28b6ef4da177e5ebadcdb9b56fe2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/samples/psp-privileged-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-privileged-disallowed + labels: + app: nginx-privileged + spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3dd4ff86393b54a58ee0edb86e919007dce00536 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: privileged-containers +tests: +- name: privileged-containers-disallowed + template: template.yaml + constraint: samples/psp-privileged-container/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-privileged-container/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-privileged-container/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-privileged-container/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-privileged-container/update.yaml + assertions: + - violations: no + - name: exempted-image + object: samples/psp-privileged-container/example_allowed_exempt.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c5a09674c6ea6f25a223dcc8299a3f3d4253200e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.1/template.yaml @@ -0,0 +1,129 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprivilegedcontainer + annotations: + metadata.gatekeeper.sh/title: "Privileged Container" + metadata.gatekeeper.sh/version: 1.1.1 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPPrivilegedContainer + validation: + openAPIV3Schema: + type: object + 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 + 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.privileged) && container.securityContext.privileged == true) + ).map(container, "Privileged container is not allowed: " + container.name +", securityContext: " + container.securityContext) + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + validations: + - expression: variables.isUpdate || size(variables.badContainers) == 0 + messageExpression: 'variables.badContainers.join("\n")' + - engine: Rego + source: + rego: | + package k8spspprivileged + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.privileged field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + c.securityContext.privileged + msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..1780929e6ed4943a69059c905798cc4d867043cc --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.2 +name: k8spspprivilegedcontainer +displayName: Privileged Container +createdAt: "2024-09-19T20:41:16Z" +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 +digest: c59b8fdc5827901a5d35fb33825275427b6cf27e38d5f2d975ac52fcc022deff +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/privileged-containers +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Privileged Container + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bf2fe519d6ff8181b67b06385da3203a0e34582f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPPrivilegedContainer +metadata: + name: psp-privileged-container +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + excludedNamespaces: ["kube-system"] + parameters: + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e8c8b99453ab07993a68d8c9fef39b7dacc8d442 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb65a2c0e7e975598e22224a8813d9e83085a371 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_allowed_exempt.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_allowed_exempt.yaml new file mode 100644 index 0000000000000000000000000000000000000000..782a6036a1e9e818b8e0f971b6c0bd3da8192781 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_allowed_exempt.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed-exempt + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: safeimages.com/nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..92353f5b3dc2db701b825abf9f99ee48062e5972 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/example_disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true + initContainers: + - name: nginx-init + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-container/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08f36044ca9c28b6ef4da177e5ebadcdb9b56fe2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/samples/psp-privileged-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-privileged-disallowed + labels: + app: nginx-privileged + spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3dd4ff86393b54a58ee0edb86e919007dce00536 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: privileged-containers +tests: +- name: privileged-containers-disallowed + template: template.yaml + constraint: samples/psp-privileged-container/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-privileged-container/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-privileged-container/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-privileged-container/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-privileged-container/update.yaml + assertions: + - violations: no + - name: exempted-image + object: samples/psp-privileged-container/example_allowed_exempt.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..68adef487e25f2cd01c4e9470604d1d0265c5e47 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.2/template.yaml @@ -0,0 +1,129 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprivilegedcontainer + annotations: + metadata.gatekeeper.sh/title: "Privileged Container" + metadata.gatekeeper.sh/version: 1.1.2 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPPrivilegedContainer + validation: + openAPIV3Schema: + type: object + 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 + 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.privileged) && container.securityContext.privileged) + ).map(container, "Privileged container is not allowed: " + container.name +", securityContext.privileged: true") + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + validations: + - expression: variables.isUpdate || size(variables.badContainers) == 0 + messageExpression: 'variables.badContainers.join(", ")' + - engine: Rego + source: + rego: | + package k8spspprivileged + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.privileged field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + c.securityContext.privileged + msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..cea29bc7f0ecb5931949e30dfcfbf78dac699b99 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.3 +name: k8spspprivilegedcontainer +displayName: Privileged Container +createdAt: "2026-01-08T18:35:12Z" +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 +digest: 176359f27d5604fdd0d69d98c0817f0d03886ac4f6c88546b5d82543c327b8e1 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/privileged-containers +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Privileged Container + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bf2fe519d6ff8181b67b06385da3203a0e34582f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPPrivilegedContainer +metadata: + name: psp-privileged-container +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + excludedNamespaces: ["kube-system"] + parameters: + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e8c8b99453ab07993a68d8c9fef39b7dacc8d442 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb65a2c0e7e975598e22224a8813d9e83085a371 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_allowed_exempt.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_allowed_exempt.yaml new file mode 100644 index 0000000000000000000000000000000000000000..782a6036a1e9e818b8e0f971b6c0bd3da8192781 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_allowed_exempt.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-allowed-exempt + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: safeimages.com/nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..92353f5b3dc2db701b825abf9f99ee48062e5972 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/example_disallowed.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-privileged-disallowed + labels: + app: nginx-privileged +spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true + initContainers: + - name: nginx-init + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-container/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..08f36044ca9c28b6ef4da177e5ebadcdb9b56fe2 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/samples/psp-privileged-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-privileged-disallowed + labels: + app: nginx-privileged + spec: + containers: + - name: nginx + image: nginx + securityContext: + privileged: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3dd4ff86393b54a58ee0edb86e919007dce00536 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: privileged-containers +tests: +- name: privileged-containers-disallowed + template: template.yaml + constraint: samples/psp-privileged-container/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-privileged-container/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-privileged-container/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-privileged-container/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-privileged-container/update.yaml + assertions: + - violations: no + - name: exempted-image + object: samples/psp-privileged-container/example_allowed_exempt.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..aa548b6b5bb25744756e08f780694ea740c3dafe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/privileged-containers/1.1.3/template.yaml @@ -0,0 +1,130 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprivilegedcontainer + annotations: + metadata.gatekeeper.sh/title: "Privileged Container" + metadata.gatekeeper.sh/version: 1.1.3 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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 +spec: + crd: + spec: + names: + kind: K8sPSPPrivilegedContainer + validation: + openAPIV3Schema: + type: object + 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 + 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.privileged) && container.securityContext.privileged) + ).map(container, "Privileged container is not allowed: " + container.name +", securityContext.privileged: true") + - name: isUpdate + expression: has(request.operation) && request.operation == "UPDATE" + validations: + - expression: variables.isUpdate || size(variables.badContainers) == 0 + messageExpression: 'variables.badContainers.join(", ")' + - engine: Rego + source: + rego: | + package k8spspprivileged + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.privileged field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + c.securityContext.privileged + msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) + } + + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..3db599c84c1114f207802df44c9c6b510649dc09 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2022-09-26T17:28:27Z" +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 +digest: 205db6e87daecb31605633637df1cd5410dd05d7c4f1fba949e93cba1f5e53fd +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1d7434ac0157a0509d22fe02919e30a431475846 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4be38f45df09eef32c519ebf3dd993cb774d2af3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c9b13ac7135945e4dfa3a8dd99d52585d5361e54 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..403c7cb2a2bba9e0d9803a18aefef9d11259266a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..26dd5eb0a6c45c13e36d3fb0b624d10438e0fc2a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e68bb7a54a6f37eff37cc724e9f37b6aa4084ee5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.0/template.yaml @@ -0,0 +1,126 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.0.0 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspprocmount + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..e140f1882c3a1dbc4c459345cb924db4e160e587 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2023-04-10T04:42:01Z" +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 +digest: a1ca20d562a44acdd9c2167e14220ab29833be6ea56aacb799de341d3b7294b7 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1d7434ac0157a0509d22fe02919e30a431475846 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4be38f45df09eef32c519ebf3dd993cb774d2af3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c9b13ac7135945e4dfa3a8dd99d52585d5361e54 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..403c7cb2a2bba9e0d9803a18aefef9d11259266a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..26dd5eb0a6c45c13e36d3fb0b624d10438e0fc2a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1f238a7d30cdcc079e156be5c2c996150b091a2e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.1/template.yaml @@ -0,0 +1,128 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.0.1 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspprocmount + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + arg.parameters.procMount + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + valid_proc_mount(arg.parameters.procMount) + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..61b7bb3807f677e0fb532f833d47b91ded226cd4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.2 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2023-05-23T09:47:31Z" +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 +digest: 9a792a8c5d3a0559d877b673315598390a44d602fc2dcbe7c4f69f734dd94c97 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.0.2/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1d7434ac0157a0509d22fe02919e30a431475846 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4be38f45df09eef32c519ebf3dd993cb774d2af3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c9b13ac7135945e4dfa3a8dd99d52585d5361e54 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..403c7cb2a2bba9e0d9803a18aefef9d11259266a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dc21b1142b2c3125990fe2b47ce49c3d900cf873 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/samples/psp-proc-mount/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount + spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..501493e1418dca30e355d23e57ed5414ff8f1783 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-proc-mount/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bbf2244da53abee65048ff997e51a75abb9ec23e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.2/template.yaml @@ -0,0 +1,138 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.0.2 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspprocmount + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.procMount field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + arg.parameters.procMount + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + valid_proc_mount(arg.parameters.procMount) + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..cd2ef88557352de0a2bca3f98ca5b4cfe2eefc21 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.3 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2023-10-30T21:00:01Z" +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 +digest: f143a4f9759f7ee3e2b987948a74df1b534b55e5b30c6304aef417647cb04aef +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.0.3/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1d7434ac0157a0509d22fe02919e30a431475846 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4be38f45df09eef32c519ebf3dd993cb774d2af3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c9b13ac7135945e4dfa3a8dd99d52585d5361e54 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..403c7cb2a2bba9e0d9803a18aefef9d11259266a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dc21b1142b2c3125990fe2b47ce49c3d900cf873 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/samples/psp-proc-mount/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount + spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..501493e1418dca30e355d23e57ed5414ff8f1783 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-proc-mount/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5fb816793b89bee412fcc997a0f5cb88b6d57f9e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.0.3/template.yaml @@ -0,0 +1,138 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.0.3 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspprocmount + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.procMount field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, _) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + arg.parameters.procMount + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + valid_proc_mount(arg.parameters.procMount) + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..2f466494e4fac5c16be36ba660bbb4b7add98a04 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.0 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2024-06-04T00:44:32Z" +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 +digest: e42186f9f55856caa0970db8069acb9df8d844bf4f969297389581f214931978 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.1.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1d7434ac0157a0509d22fe02919e30a431475846 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,11 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..74e034148d03627f1ae6060512e5f4ddfce5ba83 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cc272bafb81969457edae9d0d833c81d3466fe8b --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-allowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f0c3b030a8db824fa56e8aee3ae4ff76429c10b8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dc21b1142b2c3125990fe2b47ce49c3d900cf873 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/samples/psp-proc-mount/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount + spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..501493e1418dca30e355d23e57ed5414ff8f1783 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-proc-mount/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1007de10762c4dfe232de17d83163446e2047c5a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.0/template.yaml @@ -0,0 +1,180 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.1.0 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) + - name: allowedProcMount + expression: | + !has(variables.params) ? "default" : + !has(variables.params.procMount) ? "default" : + (variables.params.procMount.lowerAscii() == "default" || variables.params.procMount.lowerAscii() == "unmasked") ? variables.params.procMount.lowerAscii() : "default" + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + !( + (variables.allowedProcMount == "unmasked") || + (variables.allowedProcMount == "default" && has(container.securityContext) && has(container.securityContext.procMount) && container.securityContext.procMount.lowerAscii() == "default") + ) + ).map(container, "ProcMount type is not allowed, container: " + container.name +". Allowed procMount types: " + variables.allowedProcMount) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: 'variables.badContainers.join("\n")' + - engine: Rego + source: + rego: | + package k8spspprocmount + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.procMount field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, _) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + arg.parameters.procMount + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + valid_proc_mount(arg.parameters.procMount) + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..5ae52d10c5cb69f018c25190e27c5579a06d8447 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.1 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2024-08-29T21:24:52Z" +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 +digest: e3a4bd2b1bf1c2401a90292cac4a479fe102f0a78dbeb6ac2bf8808f14f4ea2d +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.1.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..79ad221ab31deea16685231d675652585f4a10b3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..74e034148d03627f1ae6060512e5f4ddfce5ba83 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cc272bafb81969457edae9d0d833c81d3466fe8b --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-allowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_allowed_exempt_image.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_allowed_exempt_image.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c197740a4ec1dd0b559dc0e0e3950fee463892fb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_allowed_exempt_image.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-exempt-image + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: safeimages.com/nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f0c3b030a8db824fa56e8aee3ae4ff76429c10b8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dc21b1142b2c3125990fe2b47ce49c3d900cf873 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/samples/psp-proc-mount/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount + spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..282246b5ad72296aa585462605880d4e1219ea02 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/suite.yaml @@ -0,0 +1,29 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-proc-mount/update.yaml + assertions: + - violations: no + - name: image-exempt-prefix-match + object: samples/psp-proc-mount/example_allowed_exempt_image.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ed32cb38329c8d7ad933523d4b57a1726b58cf36 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.1/template.yaml @@ -0,0 +1,185 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.1.1 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + 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: allowedProcMount + expression: | + !has(variables.params) ? "default" : + !has(variables.params.procMount) ? "default" : + (variables.params.procMount.lowerAscii() == "default" || variables.params.procMount.lowerAscii() == "unmasked") ? variables.params.procMount.lowerAscii() : "default" + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + !( + (variables.allowedProcMount == "unmasked") || + (variables.allowedProcMount == "default" && has(container.securityContext) && has(container.securityContext.procMount) && container.securityContext.procMount.lowerAscii() == "default") + ) + ).map(container, "ProcMount type is not allowed, container: " + container.name +". Allowed procMount types: " + variables.allowedProcMount) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: 'variables.badContainers.join("\n")' + - engine: Rego + source: + rego: | + package k8spspprocmount + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.procMount field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, _) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + arg.parameters.procMount + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + valid_proc_mount(arg.parameters.procMount) + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..ae697709da8a5ba9f02685cbaa7444aac04ccfa1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.2 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2025-01-10T16:19:17Z" +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 +digest: 685daa66bfa0a323fe8d0263e8d145a49ee097edb3fbff9d09d2b67497f92a9d +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.1.2/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..79ad221ab31deea16685231d675652585f4a10b3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..74e034148d03627f1ae6060512e5f4ddfce5ba83 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cc272bafb81969457edae9d0d833c81d3466fe8b --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-allowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed_exempt_image.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed_exempt_image.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c197740a4ec1dd0b559dc0e0e3950fee463892fb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed_exempt_image.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-exempt-image + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: safeimages.com/nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed_missing.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed_missing.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cf5f273865aa3dc688a05e2a617ccf0f5ee61311 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_allowed_missing.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: no-proc-mount-value + image: nginx + securityContext: + procMount: null + - name: no-proc-mount + image: nginx + securityContext: {} + - name: no-context + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f0c3b030a8db824fa56e8aee3ae4ff76429c10b8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dc21b1142b2c3125990fe2b47ce49c3d900cf873 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/samples/psp-proc-mount/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount + spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..066655c04c80ff87cf468a7ef2df23d9be5b76e6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/suite.yaml @@ -0,0 +1,33 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-missing + object: samples/psp-proc-mount/example_allowed_missing.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-proc-mount/update.yaml + assertions: + - violations: no + - name: image-exempt-prefix-match + object: samples/psp-proc-mount/example_allowed_exempt_image.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..57af3d0e7f022e213e9570ad81202288dd3707c5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.2/template.yaml @@ -0,0 +1,186 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.1.2 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + 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: allowedProcMount + expression: | + !has(variables.params) ? "default" : + !has(variables.params.procMount) ? "default" : + (variables.params.procMount.lowerAscii() == "default" || variables.params.procMount.lowerAscii() == "unmasked") ? variables.params.procMount.lowerAscii() : "default" + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + !( + (variables.allowedProcMount == "unmasked") || + (variables.allowedProcMount == "default" && (!has(container.securityContext) || !has(container.securityContext.procMount) || container.securityContext.procMount == null || container.securityContext.procMount.lowerAscii() == "default")) + ) + ).map(container, "ProcMount type is not allowed, container: " + container.name +". Allowed procMount types: " + variables.allowedProcMount) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: 'variables.badContainers.join("\n")' + - engine: Rego + source: + rego: | + package k8spspprocmount + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.procMount field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, _) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount != null + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount != null + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount != null + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + arg.parameters.procMount + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + valid_proc_mount(arg.parameters.procMount) + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/README.md b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9e45b72071f32f9f1d15b9758e8b7e2b95328d0a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/README.md @@ -0,0 +1,12 @@ +# ProcMount security context policy + +`procMount` denotes the type of proc mount to use for the containers. The default is `DefaultProcMount` which uses the container runtime defaults for readonly paths and masked paths. + +Types of proc mount are: + +- `DefaultProcMount` uses the container runtime default ProcType. Most container runtimes mask certain paths in /proc to avoid accidental security exposure of special devices or information. + +- `UnmaskedProcMount` bypasses the default masking behavior of the container runtime and ensures the newly created /proc the container stays in tact with no modifications. + +This requires the `ProcMountType` feature flag to be enabled. Set `--feature-gates=ProcMountType=true` in Kubernetes API Server to be able to use `Unmasked` procMount type (requires v1.12 and above). For more information, see +https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/#options and https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/. diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..267bbdc65e98a64ae8668cdfbd163e02eee0c1eb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.3 +name: k8spspprocmount +displayName: Proc Mount +createdAt: "2026-01-08T18:35:12Z" +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 +digest: 4b048b67c7406a331b164c5941dab2c450c9220c421d372e635632a1ee958e30 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/proc-mount +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Proc Mount + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/proc-mount/1.1.3/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..79ad221ab31deea16685231d675652585f4a10b3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPProcMount +metadata: + name: psp-proc-mount +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + procMount: Default + exemptImages: + - "safeimages.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..74e034148d03627f1ae6060512e5f4ddfce5ba83 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/disallowed_ephemeral.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cc272bafb81969457edae9d0d833c81d3466fe8b --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-allowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed_exempt_image.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed_exempt_image.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c197740a4ec1dd0b559dc0e0e3950fee463892fb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed_exempt_image.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-exempt-image + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: safeimages.com/nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed_missing.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed_missing.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cf5f273865aa3dc688a05e2a617ccf0f5ee61311 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_allowed_missing.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: no-proc-mount-value + image: nginx + securityContext: + procMount: null + - name: no-proc-mount + image: nginx + securityContext: {} + - name: no-context + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f0c3b030a8db824fa56e8aee3ae4ff76429c10b8 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount +spec: + hostUsers: false + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dc21b1142b2c3125990fe2b47ce49c3d900cf873 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/samples/psp-proc-mount/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-proc-mount-disallowed + labels: + app: nginx-proc-mount + spec: + containers: + - name: nginx + image: nginx + securityContext: + procMount: Unmasked #Default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..066655c04c80ff87cf468a7ef2df23d9be5b76e6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/suite.yaml @@ -0,0 +1,33 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: proc-mount +tests: +- name: default-proc-mount-required + template: template.yaml + constraint: samples/psp-proc-mount/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-proc-mount/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-proc-mount/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-missing + object: samples/psp-proc-mount/example_allowed_missing.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-proc-mount/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-proc-mount/update.yaml + assertions: + - violations: no + - name: image-exempt-prefix-match + object: samples/psp-proc-mount/example_allowed_exempt_image.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e7f7384b56798448221ed012ef05e534714fdcc4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/proc-mount/1.1.3/template.yaml @@ -0,0 +1,186 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspprocmount + annotations: + metadata.gatekeeper.sh/title: "Proc Mount" + metadata.gatekeeper.sh/version: 1.1.3 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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 +spec: + crd: + spec: + names: + kind: K8sPSPProcMount + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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 + procMount: + type: string + description: >- + Defines the strategy for the security exposure of certain paths + in `/proc` by the container runtime. Setting to `Default` uses + the runtime defaults, where `Unmasked` bypasses the default + behavior. + enum: + - Default + - Unmasked + 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: allowedProcMount + expression: | + !has(variables.params) ? "default" : + !has(variables.params.procMount) ? "default" : + (variables.params.procMount.lowerAscii() == "default" || variables.params.procMount.lowerAscii() == "unmasked") ? variables.params.procMount.lowerAscii() : "default" + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + !( + (variables.allowedProcMount == "unmasked") || + (variables.allowedProcMount == "default" && (!has(container.securityContext) || !has(container.securityContext.procMount) || container.securityContext.procMount == null || container.securityContext.procMount.lowerAscii() == "default")) + ) + ).map(container, "ProcMount type is not allowed, container: " + container.name +". Allowed procMount types: " + variables.allowedProcMount) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: 'variables.badContainers.join("\n")' + - engine: Rego + source: + rego: | + package k8spspprocmount + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.securityContext.procMount field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + allowedProcMount := get_allowed_proc_mount(input) + not input_proc_mount_type_allowed(allowedProcMount, c) + msg := sprintf("ProcMount type is not allowed, container: %v. Allowed procMount types: %v", [c.name, allowedProcMount]) + } + + input_proc_mount_type_allowed(allowedProcMount, c) { + allowedProcMount == "default" + lower(c.securityContext.procMount) == "default" + } + input_proc_mount_type_allowed(allowedProcMount, _) { + allowedProcMount == "unmasked" + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + c.securityContext.procMount != null + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + c.securityContext.procMount != null + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + c.securityContext.procMount != null + } + + get_allowed_proc_mount(arg) = out { + not arg.parameters + out = "default" + } + get_allowed_proc_mount(arg) = out { + not arg.parameters.procMount + out = "default" + } + get_allowed_proc_mount(arg) = out { + arg.parameters.procMount + not valid_proc_mount(arg.parameters.procMount) + out = "default" + } + get_allowed_proc_mount(arg) = out { + valid_proc_mount(arg.parameters.procMount) + out = lower(arg.parameters.procMount) + } + + valid_proc_mount(str) { + lower(str) == "default" + } + valid_proc_mount(str) { + lower(str) == "unmasked" + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..10c3b46f433c266d5aceec6a2fab7c39314e269c --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspreadonlyrootfilesystem +displayName: Read Only Root Filesystem +createdAt: "2022-09-26T17:28:27Z" +description: Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: 9ff5f4f86c010f6d3e997fda96726a65cd31f3b6510219dcc62e3de240c5a200 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/read-only-root-filesystem +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Read Only Root Filesystem + Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..66d6bdabe9b92aa0650a833e917ff255b2cef79e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPReadOnlyRootFilesystem +metadata: + name: psp-readonlyrootfilesystem +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d0ce2c4dc5f6e27106561a6de45fde33839741fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9c96bd18cdc0e6a15d51b672fd80d507e7c319c0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-allowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7571bfd9fbe441a2b3d27dea453b96e711127f19 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/samples/psp-readonlyrootfilesystem/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4df3de82fc6cb80f3ff803569eb73e2e6a3dbbf0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/suite.yaml @@ -0,0 +1,21 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: read-only-root-filesystem +tests: +- name: require-read-only-root-filesystem + template: template.yaml + constraint: samples/psp-readonlyrootfilesystem/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-readonlyrootfilesystem/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-readonlyrootfilesystem/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a98ab0094d602044b845b399f97b273562776301 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.0/template.yaml @@ -0,0 +1,93 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspreadonlyrootfilesystem + annotations: + metadata.gatekeeper.sh/title: "Read Only Root Filesystem" + metadata.gatekeeper.sh/version: 1.0.0 + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +spec: + crd: + spec: + names: + kind: K8sPSPReadOnlyRootFilesystem + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + exemptImages: + description: >- + Any container that uses an image that matches an entry in this list will be excluded + from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`. + + It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name) + in order to avoid unexpectedly exempting images from an untrusted repository. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspreadonlyrootfilesystem + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + c := input_containers[_] + not is_exempt(c) + input_read_only_root_fs(c) + msg := sprintf("only read-only root filesystem container is allowed: %v", [c.name]) + } + + input_read_only_root_fs(c) { + not has_field(c, "securityContext") + } + input_read_only_root_fs(c) { + not c.securityContext.readOnlyRootFilesystem == true + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + } + + # has_field returns whether an object has a field + has_field(object, field) = true { + object[field] + } + libs: + - | + package lib.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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..76a2fbd749e5b8c0b51d223fa9c334463131d0c0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspreadonlyrootfilesystem +displayName: Read Only Root Filesystem +createdAt: "2023-05-23T09:47:31Z" +description: Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: b2b009150e5eb99c2746d79e0de085d11b2bddb1c5e0613bdefcc6d27d8a221d +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/read-only-root-filesystem +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Read Only Root Filesystem + Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..66d6bdabe9b92aa0650a833e917ff255b2cef79e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPReadOnlyRootFilesystem +metadata: + name: psp-readonlyrootfilesystem +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d0ce2c4dc5f6e27106561a6de45fde33839741fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9c96bd18cdc0e6a15d51b672fd80d507e7c319c0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-allowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7571bfd9fbe441a2b3d27dea453b96e711127f19 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b31ae5e3a8412e84b6fd9baffbec73d76e655710 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/samples/psp-readonlyrootfilesystem/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem + spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..db736886f4d556a2885ff2f770dfe108c566423f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/suite.yaml @@ -0,0 +1,25 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: read-only-root-filesystem +tests: +- name: require-read-only-root-filesystem + template: template.yaml + constraint: samples/psp-readonlyrootfilesystem/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-readonlyrootfilesystem/example_disallowed.yaml + assertions: + - violations: yes + - name: example-allowed + object: samples/psp-readonlyrootfilesystem/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml + assertions: + - violations: yes + - name: update + object: samples/psp-readonlyrootfilesystem/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bca12eeae47f7e757455bccd97a67fa4628f4f45 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.0.1/template.yaml @@ -0,0 +1,103 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspreadonlyrootfilesystem + annotations: + metadata.gatekeeper.sh/title: "Read Only Root Filesystem" + metadata.gatekeeper.sh/version: 1.0.1 + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +spec: + crd: + spec: + names: + kind: K8sPSPReadOnlyRootFilesystem + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + exemptImages: + description: >- + Any container that uses an image that matches an entry in this list will be excluded + from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`. + + It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name) + in order to avoid unexpectedly exempting images from an untrusted repository. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspreadonlyrootfilesystem + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.readOnlyRootFilesystem field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + input_read_only_root_fs(c) + msg := sprintf("only read-only root filesystem container is allowed: %v", [c.name]) + } + + input_read_only_root_fs(c) { + not has_field(c, "securityContext") + } + input_read_only_root_fs(c) { + not c.securityContext.readOnlyRootFilesystem == true + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + } + + # has_field returns whether an object has a field + has_field(object, field) = true { + object[field] + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } + - | + package lib.exempt_container + + is_exempt(container) { + exempt_images := object.get(object.get(input, "parameters", {}), "exemptImages", []) + img := container.image + exemption := exempt_images[_] + _matches_exemption(img, exemption) + } + + _matches_exemption(img, exemption) { + not endswith(exemption, "*") + exemption == img + } + + _matches_exemption(img, exemption) { + endswith(exemption, "*") + prefix := trim_suffix(exemption, "*") + startswith(img, prefix) + } diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..4fd0ebda9af0c6ec569dde1940562d655d45d994 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.0 +name: k8spspreadonlyrootfilesystem +displayName: Read Only Root Filesystem +createdAt: "2024-06-03T22:57:22Z" +description: Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: a7501371f8cd7769d36035a8a0369731ff846844ad3ed76ef6ec00ef19b7767c +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/read-only-root-filesystem +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Read Only Root Filesystem + Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..66d6bdabe9b92aa0650a833e917ff255b2cef79e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/constraint.yaml @@ -0,0 +1,9 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPReadOnlyRootFilesystem +metadata: + name: psp-readonlyrootfilesystem +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d0ce2c4dc5f6e27106561a6de45fde33839741fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9c96bd18cdc0e6a15d51b672fd80d507e7c319c0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-allowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7571bfd9fbe441a2b3d27dea453b96e711127f19 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b31ae5e3a8412e84b6fd9baffbec73d76e655710 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/samples/psp-readonlyrootfilesystem/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem + spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fb52faa0074f04a22393f02d599af03f8e7767e1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/suite.yaml @@ -0,0 +1,27 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: read-only-root-filesystem +tests: +- name: require-read-only-root-filesystem + template: template.yaml + constraint: samples/psp-readonlyrootfilesystem/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-readonlyrootfilesystem/example_disallowed.yaml + assertions: + - violations: yes + message: "only read-only root filesystem container is allowed: nginx" + - name: example-allowed + object: samples/psp-readonlyrootfilesystem/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml + assertions: + - violations: yes + message: "only read-only root filesystem container is allowed: nginx" + - name: update + object: samples/psp-readonlyrootfilesystem/update.yaml + assertions: + - violations: no diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..361ff999d53e2faa39ed7410c2fe0565f5470d01 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.0/template.yaml @@ -0,0 +1,140 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspreadonlyrootfilesystem + annotations: + metadata.gatekeeper.sh/title: "Read Only Root Filesystem" + metadata.gatekeeper.sh/version: 1.1.0 + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +spec: + crd: + spec: + names: + kind: K8sPSPReadOnlyRootFilesystem + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + exemptImages: + description: >- + Any container that uses an image that matches an entry in this list will be excluded + from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`. + + It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name) + in order to avoid unexpectedly exempting images from an untrusted repository. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))) + - name: badContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !(container.image in variables.exemptImages) && + (!has(container.securityContext) || + !has(container.securityContext.readOnlyRootFilesystem) || + container.securityContext.readOnlyRootFilesystem != true) + ).map(container, container.name) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: '"only read-only root filesystem container is allowed: " + variables.badContainers.join(", ")' + + - engine: Rego + source: + rego: | + package k8spspreadonlyrootfilesystem + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.readOnlyRootFilesystem field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + input_read_only_root_fs(c) + msg := sprintf("only read-only root filesystem container is allowed: %v", [c.name]) + } + + input_read_only_root_fs(c) { + not has_field(c, "securityContext") + } + input_read_only_root_fs(c) { + not c.securityContext.readOnlyRootFilesystem == true + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + } + + # has_field returns whether an object has a field + has_field(object, field) = true { + object[field] + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } + - | + package lib.exempt_container + + is_exempt(container) { + exempt_images := object.get(object.get(input, "parameters", {}), "exemptImages", []) + img := container.image + exemption := exempt_images[_] + _matches_exemption(img, exemption) + } + + _matches_exemption(img, exemption) { + not endswith(exemption, "*") + exemption == img + } + + _matches_exemption(img, exemption) { + endswith(exemption, "*") + prefix := trim_suffix(exemption, "*") + startswith(img, prefix) + } diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..297b5006029f32a89c5d0d1383ac763a0705aee4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.1 +name: k8spspreadonlyrootfilesystem +displayName: Read Only Root Filesystem +createdAt: "2024-08-29T20:26:02Z" +description: Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +digest: 9ec2743dce71b385161873179a1d70b6f801f72b077bd178334f75f2ee1023bd +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/read-only-root-filesystem +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Read Only Root Filesystem + Requires the use of a read-only root file system by pod containers. Corresponds to the `readOnlyRootFilesystem` field in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/full_wildcard/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/full_wildcard/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b5497e7611f438d1727eed9bfd8c0db19ea99363 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/full_wildcard/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPReadOnlyRootFilesystem +metadata: + name: psp-readonlyrootfilesystem +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: + - "*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d32dc33ab7292783f4eee16840d48f5e8ab3a610 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPReadOnlyRootFilesystem +metadata: + name: psp-readonlyrootfilesystem +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: + - "specialprogram" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d0ce2c4dc5f6e27106561a6de45fde33839741fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + ephemeralContainers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9c96bd18cdc0e6a15d51b672fd80d507e7c319c0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-allowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: true diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_allowed_exempted.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_allowed_exempted.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1f86b59aed4db20e9b371a6e2bbbc12b55fcd1f0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_allowed_exempted.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: specialprogram + image: specialprogram + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7571bfd9fbe441a2b3d27dea453b96e711127f19 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b31ae5e3a8412e84b6fd9baffbec73d76e655710 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/psp-readonlyrootfilesystem/update.yaml @@ -0,0 +1,17 @@ +kind: AdmissionReview +apiVersion: admission.k8s.io/v1beta1 +request: + operation: "UPDATE" + object: + apiVersion: v1 + kind: Pod + metadata: + name: nginx-readonlyrootfilesystem-disallowed + labels: + app: nginx-readonlyrootfilesystem + spec: + containers: + - name: nginx + image: nginx + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8bac93140907ba90d7702072818ee130daa32162 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/constraint.yaml @@ -0,0 +1,12 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPReadOnlyRootFilesystem +metadata: + name: psp-readonlyrootfilesystem +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: + - "safe-images.com/*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/example_allowed_safe_prefix.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/example_allowed_safe_prefix.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6c4c60d104dba90a7ff220760c74d8167b6e612f --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/example_allowed_safe_prefix.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-allowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: "safe-images.com/nginx" + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/example_disallowed_unsafe_prefix.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/example_disallowed_unsafe_prefix.yaml new file mode 100644 index 0000000000000000000000000000000000000000..563abb05f7b54dbcffd1f27d391ff9dd0430d118 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/samples/wildcard-prefix/example_disallowed_unsafe_prefix.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-readonlyrootfilesystem-allowed + labels: + app: nginx-readonlyrootfilesystem +spec: + containers: + - name: nginx + image: "unsafe-images.com/nginx" + securityContext: + readOnlyRootFilesystem: false diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bb064b179fec31460be3aec5672e23d6ae22f6a1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/suite.yaml @@ -0,0 +1,51 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: read-only-root-filesystem +tests: +- name: require-read-only-root-filesystem + template: template.yaml + constraint: samples/psp-readonlyrootfilesystem/constraint.yaml + cases: + - name: example-disallowed + object: samples/psp-readonlyrootfilesystem/example_disallowed.yaml + assertions: + - violations: yes + message: "only read-only root filesystem container is allowed: nginx" + - name: example-allowed + object: samples/psp-readonlyrootfilesystem/example_allowed.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-readonlyrootfilesystem/disallowed_ephemeral.yaml + assertions: + - violations: yes + message: "only read-only root filesystem container is allowed: nginx" + - name: update + object: samples/psp-readonlyrootfilesystem/update.yaml + assertions: + - violations: no + - name: exact-exemption + object: samples/psp-readonlyrootfilesystem/example_allowed_exempted.yaml + assertions: + - violations: no +- name: full-wildcard + template: template.yaml + constraint: samples/full_wildcard/constraint.yaml + cases: + - name: allow-normally-disallowed + object: samples/psp-readonlyrootfilesystem/example_disallowed.yaml + assertions: + - violations: no +- name: wildcard-prefix + template: template.yaml + constraint: samples/wildcard-prefix/constraint.yaml + cases: + - name: image-with-exempt-prefix-readOnlyRootFilesystem-not-required + object: samples/wildcard-prefix/example_allowed_safe_prefix.yaml + assertions: + - violations: no + - name: image-with-different-prefix-must-set-readOnlyRootFilesystem + object: samples/wildcard-prefix/example_disallowed_unsafe_prefix.yaml + assertions: + - violations: yes diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7f3a402e80070590deb34f77eed3401fb86630c4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/read-only-root-filesystem/1.1.1/template.yaml @@ -0,0 +1,140 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspreadonlyrootfilesystem + annotations: + metadata.gatekeeper.sh/title: "Read Only Root Filesystem" + metadata.gatekeeper.sh/version: 1.1.1 + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems +spec: + crd: + spec: + names: + kind: K8sPSPReadOnlyRootFilesystem + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + description: >- + Requires the use of a read-only root file system by pod containers. + Corresponds to the `readOnlyRootFilesystem` field in a + PodSecurityPolicy. For more information, see + https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems + properties: + exemptImages: + description: >- + Any container that uses an image that matches an entry in this list will be excluded + from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`. + + It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name) + in order to avoid unexpectedly exempting images from an untrusted repository. + type: array + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + code: + - engine: K8sNativeValidation + source: + variables: + - name: containers + expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []' + - name: initContainers + expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []' + - name: ephemeralContainers + expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []' + - name: exemptImagePrefixes + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", "")) + - name: exemptImageExplicit + expression: | + !has(variables.params.exemptImages) ? [] : + variables.params.exemptImages.filter(image, !image.endsWith("*")) + - name: exemptImages + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + container.image in variables.exemptImageExplicit || + variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))).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.readOnlyRootFilesystem) || + container.securityContext.readOnlyRootFilesystem != true) + ).map(container, container.name) + validations: + - expression: '(has(request.operation) && request.operation == "UPDATE") || size(variables.badContainers) == 0' + messageExpression: '"only read-only root filesystem container is allowed: " + variables.badContainers.join(", ")' + + - engine: Rego + source: + rego: | + package k8spspreadonlyrootfilesystem + + import data.lib.exclude_update.is_update + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg, "details": {}}] { + # spec.containers.readOnlyRootFilesystem field is immutable. + not is_update(input.review) + + c := input_containers[_] + not is_exempt(c) + input_read_only_root_fs(c) + msg := sprintf("only read-only root filesystem container is allowed: %v", [c.name]) + } + + input_read_only_root_fs(c) { + not has_field(c, "securityContext") + } + input_read_only_root_fs(c) { + not c.securityContext.readOnlyRootFilesystem == true + } + + input_containers[c] { + c := input.review.object.spec.containers[_] + } + input_containers[c] { + c := input.review.object.spec.initContainers[_] + } + input_containers[c] { + c := input.review.object.spec.ephemeralContainers[_] + } + + # has_field returns whether an object has a field + has_field(object, field) = true { + object[field] + } + libs: + - | + package lib.exclude_update + + is_update(review) { + review.operation == "UPDATE" + } + - | + package lib.exempt_container + + is_exempt(container) { + exempt_images := object.get(object.get(input, "parameters", {}), "exemptImages", []) + img := container.image + exemption := exempt_images[_] + _matches_exemption(img, exemption) + } + + _matches_exemption(img, exemption) { + not endswith(exemption, "*") + exemption == img + } + + _matches_exemption(img, exemption) { + endswith(exemption, "*") + prefix := trim_suffix(exemption, "*") + startswith(img, prefix) + } diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..c731089ba42cb061ab37a67ef939950854834794 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspseccomp +displayName: Seccomp +createdAt: "2022-09-26T17:28:27Z" +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 +digest: 84dc712cca546cecd23e269d76e53dbd946e440ff61bbdd82f7a5decd14c7521 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/seccomp +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Seccomp + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/seccomp/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d26af154ef86b8b6dbaea2c9f488814c537dfa12 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSeccomp +metadata: + name: psp-seccomp +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + allowedProfiles: + - runtime/default + - docker/default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1555d700e9161c24432f12d92fa5705cb722e737 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: unconfined + labels: + app: nginx-seccomp +spec: + ephemeralContainers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2ff43d307a8406d08609715be2cf8dc92677f978 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: runtime/default + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_allowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_allowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f8766e7744026875dd30c888b9b7afd1b3b18c1d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_allowed2.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed2 + annotations: + seccomp.security.alpha.kubernetes.io/pod: runtime/default + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8e94ca7e6454beff60d521187e7bee4409c8d018 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: unconfined + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6008d8f72909aaaeed7487b39e5529dcec55f6fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/samples/psp-seccomp/example_disallowed2.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed2 + annotations: + seccomp.security.alpha.kubernetes.io/pod: unconfined + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..62336b26e08848db27f369dc0603b48dd34c7572 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/suite.yaml @@ -0,0 +1,32 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: seccomp +tests: +- name: default-seccomp-required + template: template.yaml + constraint: samples/psp-seccomp/constraint.yaml + cases: + - name: example-disallowed-global + object: samples/psp-seccomp/example_disallowed2.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation seccomp.security.alpha.kubernetes.io/pod" + - name: example-disallowed-container + object: samples/psp-seccomp/example_disallowed.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation container.seccomp.security.alpha.kubernetes.io/nginx" + - name: example-allowed-container + object: samples/psp-seccomp/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-global + object: samples/psp-seccomp/example_allowed2.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-seccomp/disallowed_ephemeral.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation container.seccomp.security.alpha.kubernetes.io/nginx" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3ed313929ad8f8f9f4f320bd919b32bec99a72d6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.0/template.yaml @@ -0,0 +1,281 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspseccomp + annotations: + metadata.gatekeeper.sh/title: "Seccomp" + metadata.gatekeeper.sh/version: 1.0.0 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPSeccomp + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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: + type: array + description: >- + An array of allowed profile values for seccomp on Pods/Containers. + + Can use the annotation naming scheme: `runtime/default`, `docker/default`, `unconfined` and/or + `localhost/some-profile.json`. The item `localhost/*` will allow any localhost based profile. + + Can also use the securityContext naming scheme: `RuntimeDefault`, `Unconfined` + and/or `Localhost`. For securityContext `Localhost`, use the parameter `allowedLocalhostProfiles` + to list the allowed profile JSON files. + + The policy code will translate between the two schemes so it is not necessary to use both. + + Putting a `*` in this array allows all Profiles to be used. + + This field is required since with an empty list this policy will block all workloads. + items: + type: string + allowedLocalhostFiles: + type: array + description: >- + When using securityContext naming scheme for seccomp and including `Localhost` this array holds + the allowed profile JSON files. + + Putting a `*` in this array will allows all JSON files to be used. + + This field is required to allow `Localhost` in securityContext as with an empty list it will block. + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspseccomp + + import data.lib.exempt_container.is_exempt + + container_annotation_key_prefix = "container.seccomp.security.alpha.kubernetes.io/" + + pod_annotation_key = "seccomp.security.alpha.kubernetes.io/pod" + + naming_translation = { + # securityContext -> annotation + "RuntimeDefault": ["runtime/default", "docker/default"], + "Unconfined": ["unconfined"], + "Localhost": ["localhost"], + # annotation -> securityContext + "runtime/default": ["RuntimeDefault"], + "docker/default": ["RuntimeDefault"], + "unconfined": ["Unconfined"], + "localhost": ["Localhost"], + } + + violation[{"msg": msg}] { + not input_wildcard_allowed_profiles + allowed_profiles := get_allowed_profiles + container := input_containers[name] + not is_exempt(container) + result := get_profile(container) + not allowed_profile(result.profile, result.file, allowed_profiles) + msg := get_message(result.profile, result.file, name, result.location, allowed_profiles) + } + + get_message(profile, file, name, location, allowed_profiles) = message { + not profile == "Localhost" + message := sprintf("Seccomp profile '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, name, location, allowed_profiles]) + } + + get_message(profile, file, name, location, allowed_profiles) = message { + profile == "Localhost" + message := sprintf("Seccomp profile '%v' with file '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, file, name, location, allowed_profiles]) + } + + input_wildcard_allowed_profiles { + input.parameters.allowedProfiles[_] == "*" + } + + input_wildcard_allowed_files { + input.parameters.allowedLocalhostFiles[_] == "*" + } + + input_wildcard_allowed_files { + "localhost/*" == input.parameters.allowedProfiles[_] + } + + # Simple allowed Profiles + allowed_profile(profile, file, allowed) { + not startswith(lower(profile), "localhost") + profile == allowed[_] + } + + # seccomp Localhost without wildcard + allowed_profile(profile, file, allowed) { + profile == "Localhost" + not input_wildcard_allowed_files + profile == allowed[_] + allowed_files := {x | x := object.get(input.parameters, "allowedLocalhostFiles", [])[_]} | get_annotation_localhost_files + file == allowed_files[_] + } + + # seccomp Localhost with wildcard + allowed_profile(profile, file, allowed) { + profile == "Localhost" + input_wildcard_allowed_files + profile == allowed[_] + } + + # annotation localhost with wildcard + allowed_profile(profile, file, allowed) { + "localhost/*" == allowed[_] + startswith(profile, "localhost/") + } + + # annotation localhost without wildcard + allowed_profile(profile, file, allowed) { + startswith(profile, "localhost/") + profile == allowed[_] + } + + # Localhost files from annotation scheme + get_annotation_localhost_files[file] { + profile := input.parameters.allowedProfiles[_] + startswith(profile, "localhost/") + file := replace(profile, "localhost/", "") + } + + # The profiles explicitly in the list + get_allowed_profiles[allowed] { + allowed := input.parameters.allowedProfiles[_] + } + + # The simply translated profiles + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + not startswith(lower(profile), "localhost") + allowed := naming_translation[profile][_] + } + + # Seccomp Localhost to annotation translation + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + profile == "Localhost" + file := object.get(input.parameters, "allowedLocalhostFiles", [])[_] + allowed := sprintf("%v/%v", [naming_translation[profile][_], file]) + } + + # Annotation localhost to Seccomp translation + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + startswith(profile, "localhost") + allowed := naming_translation.localhost[_] + } + + # Container profile as defined in pod annotation + get_profile(container) = {"profile": profile, "file": "", "location": location} { + not has_securitycontext_container(container) + not has_annotation(get_container_annotation_key(container.name)) + not has_securitycontext_pod + profile := input.review.object.metadata.annotations[pod_annotation_key] + location := sprintf("annotation %v", [pod_annotation_key]) + } + + # Container profile as defined in container annotation + get_profile(container) = {"profile": profile, "file": "", "location": location} { + not has_securitycontext_container(container) + not has_securitycontext_pod + container_annotation := get_container_annotation_key(container.name) + has_annotation(container_annotation) + profile := input.review.object.metadata.annotations[container_annotation] + location := sprintf("annotation %v", [container_annotation]) + } + + # Container profile as defined in pods securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + not has_securitycontext_container(container) + profile := input.review.object.spec.securityContext.seccompProfile.type + file := object.get(input.review.object.spec.securityContext.seccompProfile, "localhostProfile", "") + location := "pod securityContext" + } + + # Container profile as defined in containers securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + has_securitycontext_container(container) + profile := container.securityContext.seccompProfile.type + file := object.get(container.securityContext.seccompProfile, "localhostProfile", "") + location := "container securityContext" + } + + # Container profile missing + get_profile(container) = {"profile": "not configured", "file": "", "location": "no explicit profile found"} { + not has_annotation(get_container_annotation_key(container.name)) + not has_annotation(pod_annotation_key) + not has_securitycontext_pod + not has_securitycontext_container(container) + } + + has_annotation(annotation) { + input.review.object.metadata.annotations[annotation] + } + + has_securitycontext_pod { + input.review.object.spec.securityContext.seccompProfile + } + + has_securitycontext_container(container) { + container.securityContext.seccompProfile + } + + get_container_annotation_key(name) = annotation { + annotation := concat("", [container_annotation_key_prefix, name]) + } + + input_containers[container.name] = container { + container := input.review.object.spec.containers[_] + } + + input_containers[container.name] = container { + container := input.review.object.spec.initContainers[_] + } + + input_containers[container.name] = container { + container := 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..8b14fcfcb4d8b6941d13bf3f9b0da424e5294ce6 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspseccomp +displayName: Seccomp +createdAt: "2023-10-30T21:00:02Z" +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 +digest: cfa37037755930c6c076fd04a0124baa5b0918548ecccd51b1fa3a3365d5c8a1 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/seccomp +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Seccomp + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/seccomp/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d26af154ef86b8b6dbaea2c9f488814c537dfa12 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/constraint.yaml @@ -0,0 +1,13 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSeccomp +metadata: + name: psp-seccomp +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + allowedProfiles: + - runtime/default + - docker/default diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1555d700e9161c24432f12d92fa5705cb722e737 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: unconfined + labels: + app: nginx-seccomp +spec: + ephemeralContainers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2ff43d307a8406d08609715be2cf8dc92677f978 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: runtime/default + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_allowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_allowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f8766e7744026875dd30c888b9b7afd1b3b18c1d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_allowed2.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed2 + annotations: + seccomp.security.alpha.kubernetes.io/pod: runtime/default + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8e94ca7e6454beff60d521187e7bee4409c8d018 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: unconfined + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6008d8f72909aaaeed7487b39e5529dcec55f6fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/samples/psp-seccomp/example_disallowed2.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed2 + annotations: + seccomp.security.alpha.kubernetes.io/pod: unconfined + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..62336b26e08848db27f369dc0603b48dd34c7572 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/suite.yaml @@ -0,0 +1,32 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: seccomp +tests: +- name: default-seccomp-required + template: template.yaml + constraint: samples/psp-seccomp/constraint.yaml + cases: + - name: example-disallowed-global + object: samples/psp-seccomp/example_disallowed2.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation seccomp.security.alpha.kubernetes.io/pod" + - name: example-disallowed-container + object: samples/psp-seccomp/example_disallowed.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation container.seccomp.security.alpha.kubernetes.io/nginx" + - name: example-allowed-container + object: samples/psp-seccomp/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-global + object: samples/psp-seccomp/example_allowed2.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-seccomp/disallowed_ephemeral.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation container.seccomp.security.alpha.kubernetes.io/nginx" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d252a1ace74f73cded25c88cc5cb5aef0aff988d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.0.1/template.yaml @@ -0,0 +1,281 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspseccomp + annotations: + metadata.gatekeeper.sh/title: "Seccomp" + metadata.gatekeeper.sh/version: 1.0.1 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPSeccomp + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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: + type: array + description: >- + An array of allowed profile values for seccomp on Pods/Containers. + + Can use the annotation naming scheme: `runtime/default`, `docker/default`, `unconfined` and/or + `localhost/some-profile.json`. The item `localhost/*` will allow any localhost based profile. + + Can also use the securityContext naming scheme: `RuntimeDefault`, `Unconfined` + and/or `Localhost`. For securityContext `Localhost`, use the parameter `allowedLocalhostProfiles` + to list the allowed profile JSON files. + + The policy code will translate between the two schemes so it is not necessary to use both. + + Putting a `*` in this array allows all Profiles to be used. + + This field is required since with an empty list this policy will block all workloads. + items: + type: string + allowedLocalhostFiles: + type: array + description: >- + When using securityContext naming scheme for seccomp and including `Localhost` this array holds + the allowed profile JSON files. + + Putting a `*` in this array will allows all JSON files to be used. + + This field is required to allow `Localhost` in securityContext as with an empty list it will block. + items: + type: string + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8spspseccomp + + import data.lib.exempt_container.is_exempt + + container_annotation_key_prefix = "container.seccomp.security.alpha.kubernetes.io/" + + pod_annotation_key = "seccomp.security.alpha.kubernetes.io/pod" + + naming_translation = { + # securityContext -> annotation + "RuntimeDefault": ["runtime/default", "docker/default"], + "Unconfined": ["unconfined"], + "Localhost": ["localhost"], + # annotation -> securityContext + "runtime/default": ["RuntimeDefault"], + "docker/default": ["RuntimeDefault"], + "unconfined": ["Unconfined"], + "localhost": ["Localhost"], + } + + violation[{"msg": msg}] { + not input_wildcard_allowed_profiles + allowed_profiles := get_allowed_profiles + container := input_containers[name] + not is_exempt(container) + result := get_profile(container) + not allowed_profile(result.profile, result.file, allowed_profiles) + msg := get_message(result.profile, result.file, name, result.location, allowed_profiles) + } + + get_message(profile, _, name, location, allowed_profiles) = message { + not profile == "Localhost" + message := sprintf("Seccomp profile '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, name, location, allowed_profiles]) + } + + get_message(profile, file, name, location, allowed_profiles) = message { + profile == "Localhost" + message := sprintf("Seccomp profile '%v' with file '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, file, name, location, allowed_profiles]) + } + + input_wildcard_allowed_profiles { + input.parameters.allowedProfiles[_] == "*" + } + + input_wildcard_allowed_files { + input.parameters.allowedLocalhostFiles[_] == "*" + } + + input_wildcard_allowed_files { + "localhost/*" == input.parameters.allowedProfiles[_] + } + + # Simple allowed Profiles + allowed_profile(profile, _, allowed) { + not startswith(lower(profile), "localhost") + profile == allowed[_] + } + + # seccomp Localhost without wildcard + allowed_profile(profile, file, allowed) { + profile == "Localhost" + not input_wildcard_allowed_files + profile == allowed[_] + allowed_files := {x | x := object.get(input.parameters, "allowedLocalhostFiles", [])[_]} | get_annotation_localhost_files + file == allowed_files[_] + } + + # seccomp Localhost with wildcard + allowed_profile(profile, _, allowed) { + profile == "Localhost" + input_wildcard_allowed_files + profile == allowed[_] + } + + # annotation localhost with wildcard + allowed_profile(profile, _, allowed) { + "localhost/*" == allowed[_] + startswith(profile, "localhost/") + } + + # annotation localhost without wildcard + allowed_profile(profile, _, allowed) { + startswith(profile, "localhost/") + profile == allowed[_] + } + + # Localhost files from annotation scheme + get_annotation_localhost_files[file] { + profile := input.parameters.allowedProfiles[_] + startswith(profile, "localhost/") + file := replace(profile, "localhost/", "") + } + + # The profiles explicitly in the list + get_allowed_profiles[allowed] { + allowed := input.parameters.allowedProfiles[_] + } + + # The simply translated profiles + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + not startswith(lower(profile), "localhost") + allowed := naming_translation[profile][_] + } + + # Seccomp Localhost to annotation translation + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + profile == "Localhost" + file := object.get(input.parameters, "allowedLocalhostFiles", [])[_] + allowed := sprintf("%v/%v", [naming_translation[profile][_], file]) + } + + # Annotation localhost to Seccomp translation + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + startswith(profile, "localhost") + allowed := naming_translation.localhost[_] + } + + # Container profile as defined in pod annotation + get_profile(container) = {"profile": profile, "file": "", "location": location} { + not has_securitycontext_container(container) + not has_annotation(get_container_annotation_key(container.name)) + not has_securitycontext_pod + profile := input.review.object.metadata.annotations[pod_annotation_key] + location := sprintf("annotation %v", [pod_annotation_key]) + } + + # Container profile as defined in container annotation + get_profile(container) = {"profile": profile, "file": "", "location": location} { + not has_securitycontext_container(container) + not has_securitycontext_pod + container_annotation := get_container_annotation_key(container.name) + has_annotation(container_annotation) + profile := input.review.object.metadata.annotations[container_annotation] + location := sprintf("annotation %v", [container_annotation]) + } + + # Container profile as defined in pods securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + not has_securitycontext_container(container) + profile := input.review.object.spec.securityContext.seccompProfile.type + file := object.get(input.review.object.spec.securityContext.seccompProfile, "localhostProfile", "") + location := "pod securityContext" + } + + # Container profile as defined in containers securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + has_securitycontext_container(container) + profile := container.securityContext.seccompProfile.type + file := object.get(container.securityContext.seccompProfile, "localhostProfile", "") + location := "container securityContext" + } + + # Container profile missing + get_profile(container) = {"profile": "not configured", "file": "", "location": "no explicit profile found"} { + not has_annotation(get_container_annotation_key(container.name)) + not has_annotation(pod_annotation_key) + not has_securitycontext_pod + not has_securitycontext_container(container) + } + + has_annotation(annotation) { + input.review.object.metadata.annotations[annotation] + } + + has_securitycontext_pod { + input.review.object.spec.securityContext.seccompProfile + } + + has_securitycontext_container(container) { + container.securityContext.seccompProfile + } + + get_container_annotation_key(name) = annotation { + annotation := concat("", [container_annotation_key_prefix, name]) + } + + input_containers[container.name] = container { + container := input.review.object.spec.containers[_] + } + + input_containers[container.name] = container { + container := input.review.object.spec.initContainers[_] + } + + input_containers[container.name] = container { + container := 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..318f91b02ba0c24b75b258587ea0f283ba5511b0 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.1.0 +name: k8spspseccomp +displayName: Seccomp +createdAt: "2024-06-03T13:44:11Z" +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 +digest: 483941dab0df9cb51189b131e309bf927928b69b46ed51986d2f51e30fe758af +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/seccomp +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Seccomp + 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 +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/seccomp/1.1.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0d28be8d9160aad4965fa55c5880d40e10010792 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/constraint.yaml @@ -0,0 +1,15 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSeccomp +metadata: + name: psp-seccomp +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: + - nginx-exempt + allowedProfiles: + - runtime/default + - localhost/profile.json \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1555d700e9161c24432f12d92fa5705cb722e737 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/disallowed_ephemeral.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: unconfined + labels: + app: nginx-seccomp +spec: + ephemeralContainers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2ff43d307a8406d08609715be2cf8dc92677f978 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: runtime/default + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f8766e7744026875dd30c888b9b7afd1b3b18c1d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed2.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed2 + annotations: + seccomp.security.alpha.kubernetes.io/pod: runtime/default + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed_exempt_image.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed_exempt_image.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d5f42987d6eedc58577fc724ffaceb1c89e2e97e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed_exempt_image.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx-exempt + securityContext: + seccompProfile: + type: Unconfined diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed_localhost.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed_localhost.yaml new file mode 100644 index 0000000000000000000000000000000000000000..856f3217b1f9e9e7bd63d7036d75a1993671d754 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_allowed_localhost.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed-localhost + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Localhost + localhostProfile: profile.json diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8e94ca7e6454beff60d521187e7bee4409c8d018 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + annotations: + container.seccomp.security.alpha.kubernetes.io/nginx: unconfined + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6008d8f72909aaaeed7487b39e5529dcec55f6fe --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed2.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed2 + annotations: + seccomp.security.alpha.kubernetes.io/pod: unconfined + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed_localhost.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed_localhost.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c566f7ca442755020ab91627b96145ca7d4c1e1b --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/samples/psp-seccomp/example_disallowed_localhost.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed-localhost + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Localhost + localhostProfile: profile.log diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0347d33fbdeb871e45bd5c9ee3ab7d029ae230eb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/suite.yaml @@ -0,0 +1,45 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: seccomp +tests: +- name: default-seccomp-required + template: template.yaml + constraint: samples/psp-seccomp/constraint.yaml + cases: + - name: example-disallowed-global + object: samples/psp-seccomp/example_disallowed2.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation seccomp.security.alpha.kubernetes.io/pod" + - name: example-disallowed-container + object: samples/psp-seccomp/example_disallowed.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation container.seccomp.security.alpha.kubernetes.io/nginx" + - name: example-allowed-container + object: samples/psp-seccomp/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-global + object: samples/psp-seccomp/example_allowed2.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-seccomp/disallowed_ephemeral.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'unconfined' is not allowed for container 'nginx'. Found at: annotation container.seccomp.security.alpha.kubernetes.io/nginx" + - name: example-allowed-container-exempt-image + object: samples/psp-seccomp/example_allowed_exempt_image.yaml + assertions: + - violations: no + - name: example-allowed-container-localhost-profile + object: samples/psp-seccomp/example_allowed_localhost.yaml + assertions: + - violations: no + - name: example-disallowed-container-localhost-profile + object: samples/psp-seccomp/example_disallowed_localhost.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'localhost/profile.log' is not allowed for container 'nginx'. Found at: container securityContext." \ No newline at end of file diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d1b676d97e22193295a089f2a17fe57f06451b09 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccomp/1.1.0/template.yaml @@ -0,0 +1,398 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspseccomp + annotations: + metadata.gatekeeper.sh/title: "Seccomp" + metadata.gatekeeper.sh/version: 1.1.0 + 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 +spec: + crd: + spec: + names: + kind: K8sPSPSeccomp + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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 + 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: + type: array + description: >- + An array of allowed profile values for seccomp on Pods/Containers. + + Can use the annotation naming scheme: `runtime/default`, `docker/default`, `unconfined` and/or + `localhost/some-profile.json`. The item `localhost/*` will allow any localhost based profile. + + Can also use the securityContext naming scheme: `RuntimeDefault`, `Unconfined` + and/or `Localhost`. For securityContext `Localhost`, use the parameter `allowedLocalhostProfiles` + to list the allowed profile JSON files. + + The policy code will translate between the two schemes so it is not necessary to use both. + + Putting a `*` in this array allows all Profiles to be used. + + This field is required since with an empty list this policy will block all workloads. + items: + type: string + allowedLocalhostFiles: + type: array + description: >- + When using securityContext naming scheme for seccomp and including `Localhost` this array holds + the allowed profile JSON files. + + Putting a `*` in this array will allows all JSON files to be used. + + This field is required to allow `Localhost` in securityContext as with an empty list it will block. + 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: allowAllProfiles + expression: | + has(variables.params.allowedProfiles) && variables.params.allowedProfiles.exists(profile, profile == "*") + - 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: unverifiedContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !variables.allowAllProfiles && + !(container.image in variables.exemptImages)) + - name: inputAllowedProfiles + expression: | + !has(variables.params.allowedProfiles) ? [] : variables.params.allowedProfiles + - name: allowedLocalhostFiles + expression: | + has(variables.params.allowedLocalhostFiles) ? variables.params.allowedLocalhostFiles : [] + - name: allowedProfilesTranslation + expression: | + (variables.inputAllowedProfiles.filter(profile, + profile != "Localhost").map(profile, profile == "Unconfined" ? "unconfined" : profile)) + + (variables.inputAllowedProfiles.exists(profile, profile == "RuntimeDefault") ? ["runtime/default", "docker/default"] : []) + - name: allowSecurityContextLocalhost + expression: | + variables.inputAllowedProfiles.exists(profile, profile == "Localhost") + - name: derivedAllowedLocalhostFiles + expression: | + variables.allowSecurityContextLocalhost ? variables.params.allowedLocalhostFiles.map(file, "localhost/" + file) : [] + - name: localhostWildcardAllowed + expression: | + variables.inputAllowedProfiles.exists(profile, profile == "localhost/*") || variables.derivedAllowedLocalhostFiles.exists(profile, profile == "localhost/*") + - name: allowedProfiles + expression: | + (variables.allowedProfilesTranslation + variables.derivedAllowedLocalhostFiles) + - name: hasPodSeccomp + expression: | + has(variables.anyObject.spec.securityContext) && has(variables.anyObject.spec.securityContext.seccompProfile) + - name: hasPodAnnotations + expression: | + has(variables.anyObject.metadata.annotations) && ("seccomp.security.alpha.kubernetes.io/pod" in variables.anyObject.metadata.annotations) + - name: podAnnotationsProfiles + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + !(has(variables.anyObject.metadata.annotations) && (("container.seccomp.security.alpha.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations)) && + !variables.hasPodSeccomp && + variables.hasPodAnnotations + ).map(container, { + "container" : container.name, + "profile" : variables.anyObject.metadata.annotations["seccomp.security.alpha.kubernetes.io/pod"], + "file" : dyn(""), + "location" : dyn("annotation seccomp.security.alpha.kubernetes.io/pod"), + }) + - name: containerAnnotationsProfiles + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + !variables.hasPodSeccomp && + has(variables.anyObject.metadata.annotations) && (("container.seccomp.security.alpha.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations) + ).map(container, { + "container" : container.name, + "profile" : variables.anyObject.metadata.annotations["container.seccomp.security.alpha.kubernetes.io/" + container.name], + "file" : dyn(""), + "location" : dyn("annotation container.seccomp.security.alpha.kubernetes.io/" + container.name), + }) + - name: podLocalHostProfile + expression: | + variables.hasPodSeccomp && has(variables.anyObject.spec.securityContext.seccompProfile.localhostProfile) ? variables.anyObject.spec.securityContext.seccompProfile.localhostProfile : "" + - name: canonicalPodSecurityContextProfile + expression: | + has(variables.hasPodSeccomp) && has(variables.anyObject.spec.securityContext.seccompProfile.type) ? + (variables.anyObject.spec.securityContext.seccompProfile.type == "RuntimeDefault" ? ( + variables.allowedProfiles.exists(profile, profile == "runtime/default") ? "runtime/default" : variables.allowedProfiles.exists(profile, profile == "docker/default") ? "docker/default" : "runtime/default") : + variables.anyObject.spec.securityContext.seccompProfile.type == "Unconfined" ? "unconfined" : variables.anyObject.spec.securityContext.seccompProfile.type == "Localhost" ? "localhost/" + variables.podLocalHostProfile : "") + : "" + - name: podSecurityContextProfiles + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + variables.hasPodSeccomp + ).map(container, { + "container" : container.name, + "profile" : dyn(variables.canonicalPodSecurityContextProfile), + "file" : variables.podLocalHostProfile, + "location" : dyn("pod securityContext"), + }) + - name: containerSecurityContextProfiles + expression: | + variables.unverifiedContainers.filter(container, + has(container.securityContext) && has(container.securityContext.seccompProfile) + ).map(container, { + "container" : container.name, + "profile" : dyn(has(container.securityContext.seccompProfile.type) ? (container.securityContext.seccompProfile.type == "RuntimeDefault" ? ( + variables.allowedProfiles.exists(profile, profile == "runtime/default") ? "runtime/default" : variables.allowedProfiles.exists(profile, profile == "docker/default") ? "docker/default" : "runtime/default") : + container.securityContext.seccompProfile.type == "Unconfined" ? "unconfined" : container.securityContext.seccompProfile.type == "Localhost" ? "localhost/" + container.securityContext.seccompProfile.localhostProfile : "") + : ""), + "file" : has(container.securityContext.seccompProfile.localhostProfile) ? container.securityContext.seccompProfile.localhostProfile : dyn(""), + "location" : dyn("container securityContext"), + }) + - name: containerProfilesMissing + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + !(has(variables.anyObject.metadata.annotations) && (("container.seccomp.security.alpha.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations)) && + !variables.hasPodSeccomp && + !variables.hasPodAnnotations + ).map(container, { + "container" : container.name, + "profile" : dyn("not configured"), + "file" : dyn(""), + "location" : dyn("no explicit profile found"), + }) + - name: allContainerProfiles + expression: | + variables.podAnnotationsProfiles + variables.containerAnnotationsProfiles + variables.podSecurityContextProfiles + variables.containerSecurityContextProfiles + variables.containerProfilesMissing + - name: badContainerProfiles + expression: | + variables.allContainerProfiles.filter(badContainerProfile, + !((badContainerProfile.profile in variables.allowedProfiles) || (badContainerProfile.profile.startsWith("localhost/") && variables.localhostWildcardAllowed)) + ).map(badProfile, "Seccomp profile '" + badProfile.profile + "' is not allowed for container '" + badProfile.container + "'. Found at: " + badProfile.location + ". Allowed profiles: " + variables.allowedProfiles.join(", ")) + validations: + - expression: 'size(variables.badContainerProfiles) == 0' + messageExpression: | + variables.badContainerProfiles.join(", ") + - engine: Rego + source: + rego: | + package k8spspseccomp + + import data.lib.exempt_container.is_exempt + + container_annotation_key_prefix = "container.seccomp.security.alpha.kubernetes.io/" + + pod_annotation_key = "seccomp.security.alpha.kubernetes.io/pod" + + violation[{"msg": msg}] { + not input_wildcard_allowed_profiles + allowed_profiles := get_allowed_profiles + container := input_containers[name] + not is_exempt(container) + result := get_profile(container) + not allowed_profile(result.profile, result.file, allowed_profiles) + msg := get_message(result.profile, result.file, name, result.location, allowed_profiles) + } + + get_message(profile, _, name, location, allowed_profiles) = message { + message := sprintf("Seccomp profile '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, name, location, allowed_profiles]) + } + + input_wildcard_allowed_profiles { + input.parameters.allowedProfiles[_] == "*" + } + + input_wildcard_allowed_files { + input.parameters.allowedLocalhostFiles[_] == "*" + } + + input_wildcard_allowed_files { + "localhost/*" == input.parameters.allowedProfiles[_] + } + + # Simple allowed Profiles + allowed_profile(profile, _, allowed) { + not startswith(profile, "localhost/") + profile == allowed[_] + } + + # annotation localhost with wildcard + allowed_profile(profile, _, allowed) { + "localhost/*" == allowed[_] + startswith(profile, "localhost/") + } + + # annotation localhost without wildcard + allowed_profile(profile, _, allowed) { + startswith(profile, "localhost/") + profile == allowed[_] + } + + # The profiles explicitly in the list + get_allowed_profiles[allowed] { + allowed := input.parameters.allowedProfiles[_] + } + + # Seccomp Localhost to annotation translation + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + not contains(profile, "/") + file := object.get(input.parameters, "allowedLocalhostFiles", [])[_] + allowed := canonicalize_seccomp_profile({"type": profile, "localhostProfile": file}, "")[_] + } + + # Container profile as defined in pod annotation + get_profile(container) = {"profile": profile, "file": "", "location": location} { + not has_securitycontext_container(container) + not has_annotation(get_container_annotation_key(container.name)) + not has_securitycontext_pod + profile := input.review.object.metadata.annotations[pod_annotation_key] + location := sprintf("annotation %v", [pod_annotation_key]) + } + + # Container profile as defined in container annotation + get_profile(container) = {"profile": profile, "file": "", "location": location} { + not has_securitycontext_container(container) + not has_securitycontext_pod + container_annotation := get_container_annotation_key(container.name) + has_annotation(container_annotation) + profile := input.review.object.metadata.annotations[container_annotation] + location := sprintf("annotation %v", [container_annotation]) + } + + # Container profile as defined in pods securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + not has_securitycontext_container(container) + profile := canonicalize_seccomp_profile(input.review.object.spec.securityContext.seccompProfile, canonicalize_runtime_default_profile)[_] + file := object.get(input.review.object.spec.securityContext.seccompProfile, "localhostProfile", "") + location := "pod securityContext" + } + + # Container profile as defined in containers securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + has_securitycontext_container(container) + profile := canonicalize_seccomp_profile(container.securityContext.seccompProfile, canonicalize_runtime_default_profile)[_] + file := object.get(container.securityContext.seccompProfile, "localhostProfile", "") + location := "container securityContext" + } + + # Container profile missing + get_profile(container) = {"profile": "not configured", "file": "", "location": "no explicit profile found"} { + not has_securitycontext_container(container) + not has_securitycontext_pod + not has_annotation(get_container_annotation_key(container.name)) + not has_annotation(pod_annotation_key) + } + + has_annotation(annotation) { + input.review.object.metadata.annotations[annotation] + } + + has_securitycontext_pod { + input.review.object.spec.securityContext.seccompProfile + } + + has_securitycontext_container(container) { + container.securityContext.seccompProfile + } + + get_container_annotation_key(name) = annotation { + annotation := concat("", [container_annotation_key_prefix, name]) + } + + input_containers[container.name] = container { + container := input.review.object.spec.containers[_] + } + + input_containers[container.name] = container { + container := input.review.object.spec.initContainers[_] + } + + input_containers[container.name] = container { + container := input.review.object.spec.ephemeralContainers[_] + } + + canonicalize_runtime_default_profile() = out { + "runtime/default" == input.parameters.allowedProfiles[_] + out := "runtime/default" + } else = out { + "docker/default" == input.parameters.allowedProfiles[_] + out := "docker/default" + } else = out { + out := "runtime/default" + } + + canonicalize_seccomp_profile(profile, def) = out { + profile.type == "RuntimeDefault" + def == "" + out := ["runtime/default", "docker/default"] + } else = out { + profile.type == "RuntimeDefault" + def != "" + out := [def] + } else = out { + profile.type == "Localhost" + out := [sprintf("localhost/%s", [profile.localhostProfile])] + } else = out { + profile.type == "Unconfined" + out := ["unconfined"] + } + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..f573badafb41b27c7cf5ff1ae2e84a1bc0f3860e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspseccompv2 +displayName: Seccomp V2 +createdAt: "2024-09-05T01:36:31Z" +description: Controls the seccomp profile used by containers. Corresponds to the `securityContext.seccompProfile` field. +digest: c6dbfe96ca7a4be156bee4bf42aef07a1424127fd9dc7d222b4e934c1919811f +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/seccompv2 +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Seccomp V2 + Controls the seccomp profile used by containers. Corresponds to the `securityContext.seccompProfile` field. +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/seccompv2/1.0.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a851414928756d413477e540bff5bc4d7e9544b5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/constraint.yaml @@ -0,0 +1,17 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSeccompV2 +metadata: + name: psp-seccomp +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: + - nginx-exempt + allowedProfiles: + - RuntimeDefault + - Localhost + allowedLocalhostFiles: + - "*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4b32243a9d797dad930c168396f7b542a3706de4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/disallowed_ephemeral.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + ephemeralContainers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..65dfc5be89dcd8a20628cbd9cdea9eb7cf9effbb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: RuntimeDefault diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed_exempt_image.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed_exempt_image.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d5f42987d6eedc58577fc724ffaceb1c89e2e97e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed_exempt_image.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx-exempt + securityContext: + seccompProfile: + type: Unconfined diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed_localhost.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed_localhost.yaml new file mode 100644 index 0000000000000000000000000000000000000000..856f3217b1f9e9e7bd63d7036d75a1993671d754 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_allowed_localhost.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed-localhost + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Localhost + localhostProfile: profile.json diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..40c115b86f62ee914b10359b433f31434105b738 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Unconfined diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e08463f18064f42ad280de1cd98725763b85bee3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/samples/psp-seccomp/example_disallowed2.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed2 + labels: + app: nginx-seccomp +spec: + securityContext: + seccompProfile: + type: Unconfined + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f9daa264d0ab5062e556e4e0ea5e186c8a076b39 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/suite.yaml @@ -0,0 +1,36 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: seccomp +tests: +- name: default-seccomp-required + template: template.yaml + constraint: samples/psp-seccomp/constraint.yaml + cases: + - name: example-disallowed-global + object: samples/psp-seccomp/example_disallowed2.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'Unconfined' is not allowed for container 'nginx'. Found at: pod securityContext." + - name: example-disallowed-container + object: samples/psp-seccomp/example_disallowed.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'Unconfined' is not allowed for container 'nginx'. Found at: container securityContext." + - name: example-allowed-container + object: samples/psp-seccomp/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-container + object: samples/psp-seccomp/example_allowed_localhost.yaml + assertions: + - violations: no + - name: example-allowed-container-exempt-image + object: samples/psp-seccomp/example_allowed_exempt_image.yaml + assertions: + - violations: no + - name: disallowed-ephemeral + object: samples/psp-seccomp/disallowed_ephemeral.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'not configured' is not allowed for container 'nginx'. Found at: no explicit profile found" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ce0a532026d97de594888e3514b77142e744d2d1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.0/template.yaml @@ -0,0 +1,301 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspseccompv2 + annotations: + metadata.gatekeeper.sh/title: "Seccomp V2" + metadata.gatekeeper.sh/version: 1.0.0 + 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. +spec: + crd: + spec: + names: + kind: K8sPSPSeccompV2 + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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. + 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: + type: array + description: >- + An array of allowed profile values for seccomp on Pods/Containers. + + Can use the securityContext naming scheme: `RuntimeDefault`, `Unconfined` + and/or `Localhost`. For securityContext `Localhost`, use the parameter `allowedLocalhostFiles` + to list the allowed profile JSON files. + + The policy code will translate between the two schemes so it is not necessary to use both. + + Putting a `*` in this array allows all Profiles to be used. + + This field is required since with an empty list this policy will block all workloads. + items: + type: string + allowedLocalhostFiles: + type: array + description: >- + When using securityContext naming scheme for seccomp and including `Localhost` this array holds + the allowed profile JSON files. + + Putting a `*` in this array will allows all JSON files to be used. + + This field is required to allow `Localhost` in securityContext as with an empty list it will block. + 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: allowAllProfiles + expression: | + has(variables.params.allowedProfiles) && variables.params.allowedProfiles.exists(profile, profile == "*") + - 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: unverifiedContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !variables.allowAllProfiles && + !(container.image in variables.exemptImages)) + - name: inputNonLocalHostProfiles + expression: | + variables.params.allowedProfiles.filter(profile, profile != "Localhost").map(profile, {"type": profile}) + - name: inputLocalHostProfiles + expression: | + variables.params.allowedProfiles.exists(profile, profile == "Localhost") ? variables.params.allowedLocalhostFiles.map(file, {"type": "Localhost", "localHostProfile": string(file)}) : [] + - name: inputAllowedProfiles + expression: | + variables.inputNonLocalHostProfiles + variables.inputLocalHostProfiles + - name: hasPodSeccomp + expression: | + has(variables.anyObject.spec.securityContext) && has(variables.anyObject.spec.securityContext.seccompProfile) + - name: podLocalHostProfile + expression: | + variables.hasPodSeccomp && has(variables.anyObject.spec.securityContext.seccompProfile.localhostProfile) ? variables.anyObject.spec.securityContext.seccompProfile.localhostProfile : "" + - name: podSecurityContextProfileType + expression: | + has(variables.hasPodSeccomp) && has(variables.anyObject.spec.securityContext.seccompProfile.type) ? variables.anyObject.spec.securityContext.seccompProfile.type + : "" + - name: podSecurityContextProfiles + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + variables.hasPodSeccomp + ).map(container, { + "container" : container.name, + "profile" : dyn(variables.podSecurityContextProfileType), + "file" : variables.podLocalHostProfile, + "location" : dyn("pod securityContext"), + }) + - name: containerSecurityContextProfiles + expression: | + variables.unverifiedContainers.filter(container, + has(container.securityContext) && has(container.securityContext.seccompProfile) + ).map(container, { + "container" : container.name, + "profile" : dyn(container.securityContext.seccompProfile.type), + "file" : has(container.securityContext.seccompProfile.localhostProfile) ? container.securityContext.seccompProfile.localhostProfile : dyn(""), + "location" : dyn("container securityContext"), + }) + - name: containerProfilesMissing + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + !variables.hasPodSeccomp + ).map(container, { + "container" : container.name, + "profile" : dyn("not configured"), + "file" : dyn(""), + "location" : dyn("no explicit profile found"), + }) + - name: allContainerProfiles + expression: | + variables.podSecurityContextProfiles + variables.containerSecurityContextProfiles + variables.containerProfilesMissing + - name: badContainerProfilesWithoutFiles + expression: | + variables.allContainerProfiles.filter(container, + container.profile != "Localhost" && + !variables.inputAllowedProfiles.exists(profile, profile.type == container.profile) + ).map(badProfile, "Seccomp profile '" + badProfile.profile + "' is not allowed for container '" + badProfile.container + "'. Found at: " + badProfile.location + ". Allowed profiles: " + variables.inputAllowedProfiles.map(profile, "{\"type\": \"" + profile.type + "\"" + (has(profile.localHostProfile) ? ", \"localHostProfile\": \"" + profile.localHostProfile + "\"}" : "}")).join(", ")) + - name: badContainerProfilesWithFiles + expression: | + variables.allContainerProfiles.filter(container, + container.profile == "Localhost" && + !variables.inputAllowedProfiles.exists(profile, profile.type == "Localhost" && (has(profile.localHostProfile) && (profile.localHostProfile == container.file || profile.localHostProfile == "*"))) + ).map(badProfile, "Seccomp profile '" + badProfile.profile + "' With file '" + badProfile.file + "' is not allowed for container '" + badProfile.container + "'. Found at: " + badProfile.location + ". Allowed profiles: " + variables.inputAllowedProfiles.map(profile, "{\"type\": \"" + profile.type + "\"" + (has(profile.localHostProfile) ? ", \"localHostProfile\": \"" + profile.localHostProfile + "\"}" : "}")).join(", ")) + validations: + - expression: 'size(variables.badContainerProfilesWithoutFiles) == 0' + messageExpression: | + variables.badContainerProfilesWithoutFiles.join(", ") + - expression: 'size(variables.badContainerProfilesWithFiles) == 0' + messageExpression: | + variables.badContainerProfilesWithFiles.join(", ") + - engine: Rego + source: + rego: | + package k8spspseccomp + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + not input_wildcard_allowed_profiles + allowed_profiles := get_allowed_profiles + container := input_containers[name] + not is_exempt(container) + result := get_profile(container) + not allowed_profile(result.profile, result.file, allowed_profiles) + msg := get_message(result.profile, result.file, name, result.location, allowed_profiles) + } + + get_message(profile, _, name, location, allowed_profiles) = message { + profile != "Localhost" + message := sprintf("Seccomp profile '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, name, location, allowed_profiles]) + } + + get_message(profile, file, name, location, allowed_profiles) = message { + profile == "Localhost" + message := sprintf("Seccomp profile '%v' with file '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, file, name, location, allowed_profiles]) + } + + input_wildcard_allowed_profiles { + input.parameters.allowedProfiles[_] == "*" + } + + input_wildcard_allowed_files { + input.parameters.allowedLocalhostFiles[_] == "*" + } + + allowed_profile(_, _, _) { + input_wildcard_allowed_profiles + } + + allowed_profile(profile, _, _) { + profile == "Localhost" + input_wildcard_allowed_files + } + + # Simple allowed Profiles + allowed_profile(profile, _, allowed) { + profile != "Localhost" + allow_profile = allowed[_] + profile == allow_profile.type + } + + # annotation localhost without wildcard + allowed_profile(profile, file, allowed) { + profile == "Localhost" + allow_profile = allowed[_] + allow_profile.type == "Localhost" + file == allow_profile.localHostProfile + } + + # The profiles explicitly in the list + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + profile != "Localhost" + allowed := {"type": profile} + } + + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + profile == "Localhost" + file := object.get(input.parameters, "allowedLocalhostFiles", [""])[_] + allowed := {"type": "Localhost", "localHostProfile": file} + } + + # Container profile as defined in containers securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + has_securitycontext_container(container) + profile := container.securityContext.seccompProfile.type + file := object.get(container.securityContext.seccompProfile, "localhostProfile", "") + location := "container securityContext" + } + + # Container profile as defined in pods securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + not has_securitycontext_container(container) + profile := input.review.object.spec.securityContext.seccompProfile.type + file := object.get(input.review.object.spec.securityContext.seccompProfile, "localhostProfile", "") + location := "pod securityContext" + } + + # Container profile missing + get_profile(container) = {"profile": "not configured", "file": "", "location": "no explicit profile found"} { + not has_securitycontext_container(container) + not has_securitycontext_pod + } + + has_securitycontext_pod { + input.review.object.spec.securityContext.seccompProfile + } + + has_securitycontext_container(container) { + container.securityContext.seccompProfile + } + + input_containers[container.name] = container { + container := input.review.object.spec.containers[_] + } + + input_containers[container.name] = container { + container := input.review.object.spec.initContainers[_] + } + + input_containers[container.name] = container { + container := 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..64860964c73d7bbcf9fe20dbe4cacec8826f5a4a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspseccompv2 +displayName: Seccomp V2 +createdAt: "2026-01-08T18:35:12Z" +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. +digest: 0c373e75e2bc9f10bf8480dc4a3c0ee79e731a574e0527e6818dcbe8cff29669 +license: Apache-2.0 +homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/seccompv2 +keywords: + - gatekeeper + - open-policy-agent + - policies +readme: |- + # Seccomp V2 + 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. +install: |- + ### Usage + ```shell + kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/pod-security-policy/seccompv2/1.0.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/allowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/allowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5588b285cbc3e34d7631b1b55ce53009a94d9c56 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/allowed_ephemeral.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed-ephemeral + labels: + app: nginx-seccomp +spec: + ephemeralContainers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..24e7f75446d94b155cc7a75a12db37f510bd113e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/constraint.yaml @@ -0,0 +1,18 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSeccompV2 +metadata: + name: psp-seccomp-baseline +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: + - nginx-exempt + allowedProfiles: + - RuntimeDefault + - Localhost + - not configured + allowedLocalhostFiles: + - "*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..65dfc5be89dcd8a20628cbd9cdea9eb7cf9effbb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: RuntimeDefault diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_exempt_image.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_exempt_image.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d5f42987d6eedc58577fc724ffaceb1c89e2e97e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_exempt_image.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx-exempt + securityContext: + seccompProfile: + type: Unconfined diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_localhost.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_localhost.yaml new file mode 100644 index 0000000000000000000000000000000000000000..856f3217b1f9e9e7bd63d7036d75a1993671d754 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_localhost.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed-localhost + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Localhost + localhostProfile: profile.json diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_no_profile.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_no_profile.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8271d3c6bcfa791c969cca446fcb7ef86016a865 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_allowed_no_profile.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed-no-profile + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..40c115b86f62ee914b10359b433f31434105b738 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Unconfined diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e08463f18064f42ad280de1cd98725763b85bee3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-baseline/example_disallowed2.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed2 + labels: + app: nginx-seccomp +spec: + securityContext: + seccompProfile: + type: Unconfined + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..81c54929e71c437100d7296e769f517f22b703a3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/constraint.yaml @@ -0,0 +1,17 @@ +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sPSPSeccompV2 +metadata: + name: psp-seccomp-restricted +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + exemptImages: + - nginx-exempt + allowedProfiles: + - RuntimeDefault + - Localhost + allowedLocalhostFiles: + - "*" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4b32243a9d797dad930c168396f7b542a3706de4 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/disallowed_ephemeral.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + ephemeralContainers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..65dfc5be89dcd8a20628cbd9cdea9eb7cf9effbb --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: RuntimeDefault diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed_exempt_image.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed_exempt_image.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d5f42987d6eedc58577fc724ffaceb1c89e2e97e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed_exempt_image.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx-exempt + securityContext: + seccompProfile: + type: Unconfined diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed_localhost.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed_localhost.yaml new file mode 100644 index 0000000000000000000000000000000000000000..856f3217b1f9e9e7bd63d7036d75a1993671d754 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_allowed_localhost.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-allowed-localhost + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Localhost + localhostProfile: profile.json diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..40c115b86f62ee914b10359b433f31434105b738 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_disallowed.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed + labels: + app: nginx-seccomp +spec: + containers: + - name: nginx + image: nginx + securityContext: + seccompProfile: + type: Unconfined diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_disallowed2.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_disallowed2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e08463f18064f42ad280de1cd98725763b85bee3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/samples/psp-seccomp-restricted/example_disallowed2.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: nginx-seccomp-disallowed2 + labels: + app: nginx-seccomp +spec: + securityContext: + seccompProfile: + type: Unconfined + containers: + - name: nginx + image: nginx diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a55a6cc020f4c078ad00793dae058197ad20b39e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/suite.yaml @@ -0,0 +1,70 @@ +kind: Suite +apiVersion: test.gatekeeper.sh/v1alpha1 +metadata: + name: seccomp +tests: +- name: seccomp-baseline + template: template.yaml + constraint: samples/psp-seccomp-baseline/constraint.yaml + cases: + - name: example-disallowed-global + object: samples/psp-seccomp-baseline/example_disallowed2.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'Unconfined' is not allowed for container 'nginx'. Found at: pod securityContext." + - name: example-disallowed-container + object: samples/psp-seccomp-baseline/example_disallowed.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'Unconfined' is not allowed for container 'nginx'. Found at: container securityContext." + - name: example-allowed-container + object: samples/psp-seccomp-baseline/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-localhost + object: samples/psp-seccomp-baseline/example_allowed_localhost.yaml + assertions: + - violations: no + - name: example-allowed-no-profile + object: samples/psp-seccomp-baseline/example_allowed_no_profile.yaml + assertions: + - violations: no + - name: example-allowed-exempt-image + object: samples/psp-seccomp-baseline/example_allowed_exempt_image.yaml + assertions: + - violations: no + - name: allowed-ephemeral-no-profile + object: samples/psp-seccomp-baseline/allowed_ephemeral.yaml + assertions: + - violations: no +- name: seccomp-restricted + template: template.yaml + constraint: samples/psp-seccomp-restricted/constraint.yaml + cases: + - name: example-disallowed-global + object: samples/psp-seccomp-restricted/example_disallowed2.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'Unconfined' is not allowed for container 'nginx'. Found at: pod securityContext." + - name: example-disallowed-container + object: samples/psp-seccomp-restricted/example_disallowed.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'Unconfined' is not allowed for container 'nginx'. Found at: container securityContext." + - name: example-allowed-container + object: samples/psp-seccomp-restricted/example_allowed.yaml + assertions: + - violations: no + - name: example-allowed-localhost + object: samples/psp-seccomp-restricted/example_allowed_localhost.yaml + assertions: + - violations: no + - name: example-allowed-exempt-image + object: samples/psp-seccomp-restricted/example_allowed_exempt_image.yaml + assertions: + - violations: no + - name: disallowed-ephemeral-no-profile + object: samples/psp-seccomp-restricted/disallowed_ephemeral.yaml + assertions: + - violations: 1 + message: "Seccomp profile 'not configured' is not allowed for container 'nginx'. Found at: no explicit profile found" diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cba0c36c595d83fb86dbcd89d0289f40885ea639 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/seccompv2/1.0.1/template.yaml @@ -0,0 +1,302 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspseccompv2 + annotations: + metadata.gatekeeper.sh/title: "Seccomp V2" + metadata.gatekeeper.sh/version: 1.0.1 + metadata.gatekeeper.sh/bundle: "pod-security-baseline, pod-security-restricted" + 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. +spec: + crd: + spec: + names: + kind: K8sPSPSeccompV2 + validation: + # Schema for the `parameters` field + openAPIV3Schema: + type: object + 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. + 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: + type: array + description: >- + An array of allowed profile values for seccomp on Pods/Containers. + + Can use the securityContext naming scheme: `RuntimeDefault`, `Unconfined` + and/or `Localhost`. For securityContext `Localhost`, use the parameter `allowedLocalhostFiles` + to list the allowed profile JSON files. + + The policy code will translate between the two schemes so it is not necessary to use both. + + Putting a `*` in this array allows all Profiles to be used. + + This field is required since with an empty list this policy will block all workloads. + items: + type: string + allowedLocalhostFiles: + type: array + description: >- + When using securityContext naming scheme for seccomp and including `Localhost` this array holds + the allowed profile JSON files. + + Putting a `*` in this array will allows all JSON files to be used. + + This field is required to allow `Localhost` in securityContext as with an empty list it will block. + 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: allowAllProfiles + expression: | + has(variables.params.allowedProfiles) && variables.params.allowedProfiles.exists(profile, profile == "*") + - 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: unverifiedContainers + expression: | + (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container, + !variables.allowAllProfiles && + !(container.image in variables.exemptImages)) + - name: inputNonLocalHostProfiles + expression: | + variables.params.allowedProfiles.filter(profile, profile != "Localhost").map(profile, {"type": profile}) + - name: inputLocalHostProfiles + expression: | + variables.params.allowedProfiles.exists(profile, profile == "Localhost") ? variables.params.allowedLocalhostFiles.map(file, {"type": "Localhost", "localHostProfile": string(file)}) : [] + - name: inputAllowedProfiles + expression: | + variables.inputNonLocalHostProfiles + variables.inputLocalHostProfiles + - name: hasPodSeccomp + expression: | + has(variables.anyObject.spec.securityContext) && has(variables.anyObject.spec.securityContext.seccompProfile) + - name: podLocalHostProfile + expression: | + variables.hasPodSeccomp && has(variables.anyObject.spec.securityContext.seccompProfile.localhostProfile) ? variables.anyObject.spec.securityContext.seccompProfile.localhostProfile : "" + - name: podSecurityContextProfileType + expression: | + has(variables.hasPodSeccomp) && has(variables.anyObject.spec.securityContext.seccompProfile.type) ? variables.anyObject.spec.securityContext.seccompProfile.type + : "" + - name: podSecurityContextProfiles + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + variables.hasPodSeccomp + ).map(container, { + "container" : container.name, + "profile" : dyn(variables.podSecurityContextProfileType), + "file" : variables.podLocalHostProfile, + "location" : dyn("pod securityContext"), + }) + - name: containerSecurityContextProfiles + expression: | + variables.unverifiedContainers.filter(container, + has(container.securityContext) && has(container.securityContext.seccompProfile) + ).map(container, { + "container" : container.name, + "profile" : dyn(container.securityContext.seccompProfile.type), + "file" : has(container.securityContext.seccompProfile.localhostProfile) ? container.securityContext.seccompProfile.localhostProfile : dyn(""), + "location" : dyn("container securityContext"), + }) + - name: containerProfilesMissing + expression: | + variables.unverifiedContainers.filter(container, + !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && + !variables.hasPodSeccomp + ).map(container, { + "container" : container.name, + "profile" : dyn("not configured"), + "file" : dyn(""), + "location" : dyn("no explicit profile found"), + }) + - name: allContainerProfiles + expression: | + variables.podSecurityContextProfiles + variables.containerSecurityContextProfiles + variables.containerProfilesMissing + - name: badContainerProfilesWithoutFiles + expression: | + variables.allContainerProfiles.filter(container, + container.profile != "Localhost" && + !variables.inputAllowedProfiles.exists(profile, profile.type == container.profile) + ).map(badProfile, "Seccomp profile '" + badProfile.profile + "' is not allowed for container '" + badProfile.container + "'. Found at: " + badProfile.location + ". Allowed profiles: " + variables.inputAllowedProfiles.map(profile, "{\"type\": \"" + profile.type + "\"" + (has(profile.localHostProfile) ? ", \"localHostProfile\": \"" + profile.localHostProfile + "\"}" : "}")).join(", ")) + - name: badContainerProfilesWithFiles + expression: | + variables.allContainerProfiles.filter(container, + container.profile == "Localhost" && + !variables.inputAllowedProfiles.exists(profile, profile.type == "Localhost" && (has(profile.localHostProfile) && (profile.localHostProfile == container.file || profile.localHostProfile == "*"))) + ).map(badProfile, "Seccomp profile '" + badProfile.profile + "' With file '" + badProfile.file + "' is not allowed for container '" + badProfile.container + "'. Found at: " + badProfile.location + ". Allowed profiles: " + variables.inputAllowedProfiles.map(profile, "{\"type\": \"" + profile.type + "\"" + (has(profile.localHostProfile) ? ", \"localHostProfile\": \"" + profile.localHostProfile + "\"}" : "}")).join(", ")) + validations: + - expression: 'size(variables.badContainerProfilesWithoutFiles) == 0' + messageExpression: | + variables.badContainerProfilesWithoutFiles.join(", ") + - expression: 'size(variables.badContainerProfilesWithFiles) == 0' + messageExpression: | + variables.badContainerProfilesWithFiles.join(", ") + - engine: Rego + source: + rego: | + package k8spspseccomp + + import data.lib.exempt_container.is_exempt + + violation[{"msg": msg}] { + not input_wildcard_allowed_profiles + allowed_profiles := get_allowed_profiles + container := input_containers[name] + not is_exempt(container) + result := get_profile(container) + not allowed_profile(result.profile, result.file, allowed_profiles) + msg := get_message(result.profile, result.file, name, result.location, allowed_profiles) + } + + get_message(profile, _, name, location, allowed_profiles) = message { + profile != "Localhost" + message := sprintf("Seccomp profile '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, name, location, allowed_profiles]) + } + + get_message(profile, file, name, location, allowed_profiles) = message { + profile == "Localhost" + message := sprintf("Seccomp profile '%v' with file '%v' is not allowed for container '%v'. Found at: %v. Allowed profiles: %v", [profile, file, name, location, allowed_profiles]) + } + + input_wildcard_allowed_profiles { + input.parameters.allowedProfiles[_] == "*" + } + + input_wildcard_allowed_files { + input.parameters.allowedLocalhostFiles[_] == "*" + } + + allowed_profile(_, _, _) { + input_wildcard_allowed_profiles + } + + allowed_profile(profile, _, _) { + profile == "Localhost" + input_wildcard_allowed_files + } + + # Simple allowed Profiles + allowed_profile(profile, _, allowed) { + profile != "Localhost" + allow_profile = allowed[_] + profile == allow_profile.type + } + + # annotation localhost without wildcard + allowed_profile(profile, file, allowed) { + profile == "Localhost" + allow_profile = allowed[_] + allow_profile.type == "Localhost" + file == allow_profile.localHostProfile + } + + # The profiles explicitly in the list + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + profile != "Localhost" + allowed := {"type": profile} + } + + get_allowed_profiles[allowed] { + profile := input.parameters.allowedProfiles[_] + profile == "Localhost" + file := object.get(input.parameters, "allowedLocalhostFiles", [""])[_] + allowed := {"type": "Localhost", "localHostProfile": file} + } + + # Container profile as defined in containers securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + has_securitycontext_container(container) + profile := container.securityContext.seccompProfile.type + file := object.get(container.securityContext.seccompProfile, "localhostProfile", "") + location := "container securityContext" + } + + # Container profile as defined in pods securityContext + get_profile(container) = {"profile": profile, "file": file, "location": location} { + not has_securitycontext_container(container) + profile := input.review.object.spec.securityContext.seccompProfile.type + file := object.get(input.review.object.spec.securityContext.seccompProfile, "localhostProfile", "") + location := "pod securityContext" + } + + # Container profile missing + get_profile(container) = {"profile": "not configured", "file": "", "location": "no explicit profile found"} { + not has_securitycontext_container(container) + not has_securitycontext_pod + } + + has_securitycontext_pod { + input.review.object.spec.securityContext.seccompProfile + } + + has_securitycontext_container(container) { + container.securityContext.seccompProfile + } + + input_containers[container.name] = container { + container := input.review.object.spec.containers[_] + } + + input_containers[container.name] = container { + container := input.review.object.spec.initContainers[_] + } + + input_containers[container.name] = container { + container := 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..b7c77551cc912494f2ac73a3b29710dd4991e8af --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.0 +name: k8spspselinuxv2 +displayName: SELinux V2 +createdAt: "2022-09-26T17:28:27Z" +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: 9d1a7715fda4e529af61e3d1b1d4017a7b546bfe3c9d7b98ea2ac6573fc10e63 +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.0/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f88bbcd69a705b0db24b292accda70796dd1af1e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a35fc737fe71150a2bcad733a97ccc85ed41fff --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4eaf2dc929cf706cbc98d5ea09856d627e582263 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/samples/psp-selinux-v2/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7eb7fee115ec55bcd1b5978e44362378871b661d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f35a2f6a7346170561d2f6692a8a28b0dd28e2da --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/suite.yaml @@ -0,0 +1,21 @@ +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 diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fff3cda75fb886dfc4f97482d28b34a1396875d5 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.0/template.yaml @@ -0,0 +1,131 @@ +apiVersion: templates.gatekeeper.sh/v1 +kind: ConstraintTemplate +metadata: + name: k8spspselinuxv2 + annotations: + metadata.gatekeeper.sh/title: "SELinux V2" + metadata.gatekeeper.sh/version: 1.0.0 + 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.exempt_container.is_exempt + + # Disallow top level custom SELinux options + violation[{"msg": msg, "details": {}}] { + 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": {}}] { + 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, params) { + 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.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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..b41f55e841364f773822c65e2abf78fe59c1c0cf --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.1 +name: k8spspselinuxv2 +displayName: SELinux V2 +createdAt: "2023-05-23T09:47:32Z" +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: 4b123118ccfaccae4f0f895db926a46e41414e58cd779179a0767c6ab216055d +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.1/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f88bbcd69a705b0db24b292accda70796dd1af1e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a35fc737fe71150a2bcad733a97ccc85ed41fff --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4eaf2dc929cf706cbc98d5ea09856d627e582263 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7eb7fee115ec55bcd1b5978e44362378871b661d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/samples/psp-selinux-v2/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..581419e9df5d426485e489a6b755591b8d545e92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1bbaf360ede8fd308d913c96d38117f6c6069d2a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/template.yaml new file mode 100644 index 0000000000000000000000000000000000000000..61729ecebe6094fa5f4a4c57c6fb1fdf672ba264 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.1/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.1 + 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, params) { + 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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/artifacthub-pkg.yml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/artifacthub-pkg.yml new file mode 100644 index 0000000000000000000000000000000000000000..4b7ae97a0c8b75e88980cdaae5332aec55ebbef1 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/artifacthub-pkg.yml @@ -0,0 +1,22 @@ +version: 1.0.2 +name: k8spspselinuxv2 +displayName: SELinux V2 +createdAt: "2023-10-30T21:00:02Z" +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: a472297f77af518459c368a2bf1c38c0fe6023f492fc6796fde75ad7a4ea131f +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.2/template.yaml + ``` +provider: + name: Gatekeeper Library diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/kustomization.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/kustomization.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d70d11b719ca4d91f8218f217c28b961662a7f3 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/kustomization.yaml @@ -0,0 +1,2 @@ +resources: + - template.yaml diff --git a/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/constraint.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/constraint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f88bbcd69a705b0db24b292accda70796dd1af1e --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/disallowed_ephemeral.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/disallowed_ephemeral.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3a35fc737fe71150a2bcad733a97ccc85ed41fff --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/example_allowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/example_allowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4eaf2dc929cf706cbc98d5ea09856d627e582263 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/example_disallowed.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/example_disallowed.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7eb7fee115ec55bcd1b5978e44362378871b661d --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/update.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/samples/psp-selinux-v2/update.yaml new file mode 100644 index 0000000000000000000000000000000000000000..581419e9df5d426485e489a6b755591b8d545e92 --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/suite.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/suite.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1bbaf360ede8fd308d913c96d38117f6c6069d2a --- /dev/null +++ b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/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/commit-a8d4799d8daf/artifacthub/library/pod-security-policy/selinux/1.0.2/template.yaml b/source_data/pinned/gatekeeper-library/commit-a8d4799d8daf/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/commit-a8d4799d8daf/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) + }