Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addding new Safeguards #364

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ metadata:
spec:
match:
kinds:
- apiGroups: ["apps"]
kinds: ["Deployment"]
- apiGroups: [""]
kinds: ["Pod"]
parameters:
imageRegex: ".*"
excludedContainers: []
imageRegex: {{ .Values.imageRegex }}
excludedContainers: {{ .Values.excludedContainers }}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ spec:
}

input_containers[c] {
c := input.review.object.spec.template.spec.containers[_]
c := input.review.object.spec.containers[_]
}
input_containers[c] {
c := input.review.object.spec.template.spec.initContainers[_]
c := input.review.object.spec.initContainers[_]
}
input_containers[c] {
c := input.review.object.spec.template.spec.ephemeralContainers[_]
c := input.review.object.spec.ephemeralContainers[_]
}
input_container_excluded(field) {
field == input.parameters.excludedContainers[_]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ metadata:
spec:
match:
kinds:
- apiGroups: ["apps"]
kinds: ["Deployment"]
- apiGroups: [""]
kinds: ["Pod"]
parameters:
enforceProbes : ["livenessProbe", "readinessProbe"]
excludedContainers: []
excludedImages: []
enforceProbes : {{ .Values.enforceProbes }}
excludedContainers: {{ .Values.excludedContainers }}
excludedImages: {{ .Values.excludedImages }}
185 changes: 90 additions & 95 deletions pkg/safeguards/lib/v1.0.0/container-enforce-probes/template.yaml
Original file line number Diff line number Diff line change
@@ -1,95 +1,90 @@
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: k8sazurev2containerenforceprobes
spec:
crd:
spec:
names:
kind: K8sAzureV2ContainerEnforceProbes
validation:
openAPIV3Schema:
properties:
enforceProbes:
type: array
items:
type: string
excludedContainers:
type: array
items:
type: string
excludedImages:
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 excluding images from an untrusted repository.
type: array
items:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8sazurev2containerenforceprobes

import data.lib.exclude_container_image.is_excluded

# Rule:
## Parameter enforceProbes is one string array that will define which kinds of probes to be enforced for all the containers (init container excludes). The allowed values could be livenessProbes and readinessProbes for now
## Once certain probe is enforces, e.g. livenessProbes, the policy will check on all containers(except init) if they have livenessProbes field. Besides, the probes should at least have defined one of the probe_types, "tcpSocket", "httpGet" or "exec"

probe_type_set = probe_types {
probe_types := {type | type := ["tcpSocket", "httpGet", "exec"][_]}
}
violation[{"msg": msg}] {
container := input_containers[_]
not input_container_excluded(container.name)
not is_excluded(container)
probe := input.parameters.enforceProbes[_]
probe_is_missing(container, probe)
msg := get_violation_message(container, input.review, probe)
}
probe_is_missing(ctr, probe) = true {
not ctr[probe]
}
probe_is_missing(ctr, probe) = true {
probe_field_empty(ctr, probe)
}
probe_field_empty(ctr, probe) = true {
probe_fields := {field | ctr[probe][field]}
diff_fields := probe_type_set - probe_fields
count(diff_fields) == count(probe_type_set)
}
get_violation_message(container, review, probe) = msg {
msg := sprintf("Container <%v> in your Deployment <%v> has no <%v>. Required probes: %v", [container.name, review.object.metadata.name, probe, input.parameters.enforceProbes])
}

input_containers[c] {
c := input.review.object.spec.template.spec.containers[_]
}

input_containers[c] {
c := input.review.object.spec.containers[_]
}

input_container_excluded(field) {
field == input.parameters.excludedContainers[_]
}
libs:
- |
package lib.exclude_container_image
is_excluded(container) {
exclude_images := object.get(object.get(input, "parameters", {}), "excludedImages", [])
img := container.image
exclusion := exclude_images[_]
_matches_exclusion(img, exclusion)
}
_matches_exclusion(img, exclusion) {
not endswith(exclusion, "*")
exclusion == img
}
_matches_exclusion(img, exclusion) {
endswith(exclusion, "*")
prefix := trim_suffix(exclusion, "*")
startswith(img, prefix)
}
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: k8sazurev2containerenforceprobes
spec:
crd:
spec:
names:
kind: K8sAzureV2ContainerEnforceProbes
validation:
openAPIV3Schema:
properties:
enforceProbes:
type: array
items:
type: string
excludedContainers:
type: array
items:
type: string
excludedImages:
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 excluding images from an untrusted repository.
type: array
items:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8sazurev2containerenforceprobes

import data.lib.exclude_container_image.is_excluded

# Rule:
## Parameter enforceProbes is one string array that will define which kinds of probes to be enforced for all the containers (init container excludes). The allowed values could be livenessProbes and readinessProbes for now
## Once certain probe is enforces, e.g. livenessProbes, the policy will check on all containers(except init) if they have livenessProbes field. Besides, the probes should at least have defined one of the probe_types, "tcpSocket", "httpGet" or "exec"

probe_type_set = probe_types {
probe_types := {type | type := ["tcpSocket", "httpGet", "exec"][_]}
}
violation[{"msg": msg}] {
container := input_containers[_]
not input_container_excluded(container.name)
not is_excluded(container)
probe := input.parameters.enforceProbes[_]
probe_is_missing(container, probe)
msg := get_violation_message(container, input.review, probe)
}
probe_is_missing(ctr, probe) = true {
not ctr[probe]
}
probe_is_missing(ctr, probe) = true {
probe_field_empty(ctr, probe)
}
probe_field_empty(ctr, probe) = true {
probe_fields := {field | ctr[probe][field]}
diff_fields := probe_type_set - probe_fields
count(diff_fields) == count(probe_type_set)
}
get_violation_message(container, review, probe) = msg {
msg := sprintf("Container <%v> in your Pod <%v> has no <%v>. Required probes: %v", [container.name, review.object.metadata.name, probe, input.parameters.enforceProbes])
}

input_containers[c] {
c := input.review.object.spec.containers[_]
}
input_container_excluded(field) {
field == input.parameters.excludedContainers[_]
}
libs:
- |
package lib.exclude_container_image
is_excluded(container) {
exclude_images := object.get(object.get(input, "parameters", {}), "excludedImages", [])
img := container.image
exclusion := exclude_images[_]
_matches_exclusion(img, exclusion)
}
_matches_exclusion(img, exclusion) {
not endswith(exclusion, "*")
exclusion == img
}
_matches_exclusion(img, exclusion) {
endswith(exclusion, "*")
prefix := trim_suffix(exclusion, "*")
startswith(img, prefix)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
cpuLimit : "200m"
memoryLimit: "1Gi"
excludedContainers: []
excludedImages: []
cpuLimit : "{{ .Values.cpuLimit }}"
memoryLimit: "{{ .Values.memoryLimit }}"
excludedContainers: {{ .Values.excludedContainers }}
excludedImages: {{ .Values.excludedImages }}
Loading