Skip to content

Commit efb7509

Browse files
committed
feat: add more cncf project validation modules
Signed-off-by: peefy <[email protected]>
1 parent 0f38211 commit efb7509

File tree

44 files changed

+373
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+373
-0
lines changed

add-castai-removal-disabled/README.md

+16

add-castai-removal-disabled/kcl.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "add-castai-removal-disabled"
3+
edition = "*"
4+
version = "0.1.0"
5+
description = "`add-castai-removal-disabled` is a kcl mutation package."

add-castai-removal-disabled/main.k

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
labels = option("labels") or {
2+
"autoscaling.cast.ai/removal-disabled" = "true"
3+
}
4+
items = [item | {
5+
if item.kind == "Job":
6+
spec.template.metadata.labels: labels
7+
elif item.kind == "CronJob":
8+
jobTemplate.template.metadata.labels: labels
9+
} for item in option("items") or []]

add-network-policy-dns/README.md

+7

add-network-policy-dns/kcl.mod

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "add-network-policy-dns"
3+
edition = "*"
4+
version = "0.1.0"
5+
description = "`add-network-policy-dns` is a KCL mutation module"
6+

add-network-policy-dns/kcl.mod.lock

Whitespace-only changes.

add-network-policy-dns/main.k

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ns_list = [item.metadata.name for item in option("items") or [] if item.kind == "Namespace"]
2+
items = (option("items") or []) + [{
3+
apiVersion: "networking.k8s.io/v1"
4+
kind: "NetworkPolicy"
5+
name: "allow-dns"
6+
namespace: "${ns.metadata.name}"
7+
synchronize: False
8+
data.spec: {
9+
# select all pods in the namespace
10+
podSelector.matchLabels: {}
11+
# deny all traffic
12+
policyTypes: ["Egress"]
13+
egress: [{
14+
to: [{namespaceSelector.matchLabels.name = "kube-system"}]
15+
ports: [{
16+
protocol: "UDP"
17+
port = 53
18+
}]
19+
}]
20+
}
21+
} for ns in ns_list]

add-network-policy/README.md

+7

add-network-policy/kcl.mod

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "add-network-policy"
3+
edition = "*"
4+
version = "0.1.0"
5+
description = "`add-network-policy` is a KCL mutation module"
6+

add-network-policy/kcl.mod.lock

Whitespace-only changes.

add-network-policy/main.k

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ns_list = [item.metadata.name for item in option("items") or [] if item.kind == "Namespace"]
2+
items = (option("items") or []) + [{
3+
apiVersion: "networking.k8s.io/v1"
4+
kind: "NetworkPolicy"
5+
name: "default-deny"
6+
namespace: "${ns.metadata.name}"
7+
synchronize: True
8+
data.spec: {
9+
# select all pods in the namespace
10+
podSelector: {}
11+
# deny all traffic
12+
policyTypes: ["Ingress", "Egress"]
13+
}
14+
} for ns in ns_list]

add-safe-to-evict/README.md

+7

add-safe-to-evict/kcl.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "add-safe-to-evict"
3+
edition = "*"
4+
version = "0.1.0"
5+
description = "`add-safe-to-evict` is a KCL mutation module"

add-safe-to-evict/main.k

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
items = [item | {
2+
if item.kind == "Pod":
3+
metadata.annotations: {
4+
"cluster-autoscaler.kubernetes.io/safe-to-evict": "true"
5+
}
6+
} for item in option("items") or []]
+7

cert-manager-limit-dns-names/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "cert-manager-limit-dns-names"
3+
version = "0.1.0"
4+
description = "`cert-manager-limit-dns-names` is a KCL validation module"

cert-manager-limit-dns-names/main.k

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Define the validation function
2+
validate = lambda item {
3+
if item.kind == "Certificate":
4+
assert len(item?.spec?.dnsNames or []) <= 1, "Only one dnsNames entry allowed per certificate request."
5+
item
6+
}
7+
# Validate All resource
8+
items = [validate(i) for i in option("items") or []]

cert-manager-limit-duration/README.md

+7

cert-manager-limit-duration/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "cert-manager-limit-duration"
3+
version = "0.1.0"
4+
description = "`cert-manager-limit-duration` is a KCL validation module"

cert-manager-limit-duration/main.k

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import regex
2+
# Define the validation function
3+
validate = lambda item {
4+
if item.kind == "Certificate":
5+
if "letsencrypt" in item.spec.issuerRef.name and item.spec?.duration:
6+
duration = int(regex.replace(item.spec?.duration, "h.*", ""))
7+
assert 0 <= duration <= 2400, "certificate duration must be < than 2400h (100 days)"
8+
item
9+
}
10+
# Validate All resource
11+
items = [validate(i) for i in option("items") or []]
+7

cert-manager-restrict-issuer/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "cert-manager-restrict-issuer"
3+
version = "0.1.0"
4+
description = "`cert-manager-restrict-issuer` is a KCL validation module"

cert-manager-restrict-issuer/main.k

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Define the validation function
2+
validate = lambda item {
3+
if item.kind == "Certificate":
4+
if any n in item.spec.dnsNames {
5+
n.endswith(".corp.com")
6+
}:
7+
issuerRef = item.spec.issuerRef
8+
condition = issuerRef.name == "our-corp-issuer" and issuerRef.kind == "ClusterIssuer" and issuerRef.group == "cert-manager.io"
9+
assert condition, "When requesting a cert for this domain, you must use our corporate issuer."
10+
item
11+
}
12+
# Validate All resource
13+
items = [validate(i) for i in option("items") or []]
+7
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "consul-enforce-min-tls-version"
3+
version = "0.1.0"
4+
description = "`consul-enforce-min-tls-version` is a KCL validation module"

consul-enforce-min-tls-version/main.k

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Define the validation function
2+
validate = lambda item {
3+
if item.kind == "Mesh":
4+
assert item.spec.tls.incoming.tlsMinVersion == "TLSv1_2", "The minimum version of TLS is TLS v1_2"
5+
item
6+
}
7+
# Validate All resource
8+
items = [validate(i) for i in option("items") or []]

disallow-cri-sock-mount/README.md

+7

disallow-cri-sock-mount/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "disallow-cri-sock-mount"
3+
version = "0.1.0"
4+
description = "`disallow-cri-sock-mount` is a KCL validation module"

disallow-cri-sock-mount/main.k

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Container daemon socket bind mounts allows access to the container engine on the
2+
node. This access can be used for privilege escalation and to manage containers
3+
outside of Kubernetes, and hence should not be allowed. This policy validates that
4+
the sockets used for CRI engines Docker, Containerd, and CRI-O are not used.
5+
"""
6+
7+
# Define the validation function
8+
validate = lambda item {
9+
if item.kind == "Pod":
10+
paths = [p.path for v in item?.spec?.volumes or [] for p in v.hostPath]
11+
assert all p in paths {
12+
p not in [
13+
"/var/run/docker.sock"
14+
"/var/run/containerd.sock"
15+
"/var/run/crio.sock"
16+
"/var/run/cri-dockerd.sock"
17+
]
18+
} if paths, "Use of the Docker Unix socket, Containerd Unix socket, CRI-O Unix socket and Docker CRI socket are not allowed."
19+
item
20+
}
21+
# Validate All resource
22+
items = [validate(i) for i in option("items") or []]

disallow-default-namespace/README.md

+7

disallow-default-namespace/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "disallow-default-namespace"
3+
version = "0.1.0"
4+
description = "`disallow-default-namespace` is a KCL validation module"

disallow-default-namespace/main.k

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
kinds: [str] = option("params")?.kinds or option("kinds") or [
2+
"Pod"
3+
"DaemonSet"
4+
"Deployment"
5+
"Job"
6+
"StatefulSet"
7+
]
8+
9+
# Define the validation function
10+
validate = lambda item {
11+
if item.kind in kinds:
12+
ns = item?.matadata?.namespace or "default"
13+
assert ns != "Using 'default' namespace is not allowed for ${item.kind}: ${item.metadata.name}"
14+
item
15+
}
16+
# Validate All resource
17+
items = [validate(i) for i in option("items") or []]

disallow-empty-ingress-host/README.md

+7

disallow-empty-ingress-host/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "disallow-empty-ingress-host"
3+
version = "0.1.0"
4+
description = "`disallow-empty-ingress-host` is a KCL validation module"

disallow-empty-ingress-host/main.k

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""An ingress resource needs to define an actual host name
2+
in order to be valid. This policy ensures that there is a
3+
hostname for each rule defined.
4+
"""
5+
6+
# Define the validation function
7+
validate = lambda item {
8+
if item.kind == "Ingress":
9+
host_list = [r.host for r in item?.spec?.rules if not r.host]
10+
assert len(host_list) == 0, "The Ingress host name must be defined, not empty."
11+
item
12+
}
13+
# Validate All resource
14+
items = [validate(i) for i in option("items") or []]

disallow-helm-tiller/README.md

+7

disallow-helm-tiller/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "disallow-helm-tiller"
3+
version = "0.1.0"
4+
description = "`disallow-helm-tiller` is a KCL validation module"

disallow-helm-tiller/main.k

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Tiller, found in Helm v2, has known security challenges. It requires administrative privileges and acts as a shared
2+
resource accessible to any authenticated user. Tiller can lead to privilege escalation as
3+
restricted users can impact other users. It is recommend to use Helm v3+ which does not contain
4+
Tiller for these reasons. This policy validates that there is not an image
5+
containing the name `tiller`.
6+
"""
7+
8+
# Define the validation function
9+
validate = lambda item: {str:} {
10+
containers: [{str:}] = []
11+
if item.kind == "Pod":
12+
containers = (item?.spec?.containers or []) + (item?.spec?.phemeralContainers or []) + (item?.spec?.initContainers or [])
13+
elif item.kind == "Deployment":
14+
containers = (item?.spec?.template?.spec?.containers or []) + (item?.spec?.template?.spec?.phemeralContainers or []) + (item?.spec?.template?.spec?.initContainers or [])
15+
images: [str] = [c.image for c in containers]
16+
assert all image in images {
17+
"tiller" not in image
18+
} if images, """Helm Tiller is not allowed for ${item.kind}: ${item.metadata.name}"""
19+
item
20+
}
21+
# Validate All resource
22+
items = [validate(i) for i in option("items")]

disallow-image-repos/README.md

+5

disallow-image-repos/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "disallow-image-repos"
3+
version = "0.1.0"
4+
description = "`disallow-image-repos` is a kcl validation package"

disallow-image-repos/main.k

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Disallowed container image repositories that begin with a string from the specified list.
2+
"""
3+
4+
# The list of prefixes a container image is allowed to have.
5+
repos: [str] = option("params").repos or []
6+
7+
# Define the validation function
8+
validate = lambda item {
9+
containers = []
10+
if item.kind == "Pod" and repos:
11+
containers = (item.spec.containers or []) + (item.spec.phemeralContainers or []) + (item.spec.initContainers or [])
12+
elif item.kind == "Deployment":
13+
containers = (item.spec.template.spec.containers or []) + (item.spec.template.spec.phemeralContainers or []) + (item.spec.template.spec.initContainers or [])
14+
images: [str] = [c.image for c in containers]
15+
assert all image in images {
16+
all repo in repos {
17+
not image.startswith(repo)
18+
}
19+
} if images and repos, """Use of image is disallowed for ${item.kind}: ${item.metadata.name}, valid repos ${repos}"""
20+
item
21+
}
22+
# Validate All resource
23+
items = [validate(i) for i in option("items")]

disallow-latest-tag/README.md

+7

disallow-latest-tag/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "disallow-latest-tag"
3+
version = "0.1.0"
4+
description = "`disallow-latest-tag` is a KCL validation module"

0 commit comments

Comments
 (0)