Skip to content

Commit 66a166e

Browse files
committed
added basic policy
1 parent 063fed8 commit 66a166e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package kubernetes.admission
2+
import data.kubernetes.namespaces
3+
4+
operations = {"CREATE", "UPDATE"}
5+
6+
deny[msg] {
7+
input.request.kind.kind == "Ingress"
8+
operations[input.request.operation]
9+
host := input.request.object.spec.rules[_].host
10+
not fqdn_matches_any(host, valid_ingress_hosts)
11+
msg := sprintf("invalid ingress host %q", [host])
12+
}
13+
14+
valid_ingress_hosts = {host |
15+
whitelist := namespaces[input.request.namespace].metadata.annotations["ingress-whitelist"]
16+
hosts := split(whitelist, ",")
17+
host := hosts[_]
18+
}
19+
20+
fqdn_matches_any(str, patterns) {
21+
fqdn_matches(str, patterns[_])
22+
}
23+
24+
fqdn_matches(str, pattern) {
25+
pattern_parts := split(pattern, ".")
26+
pattern_parts[0] == "*"
27+
str_parts := split(str, ".")
28+
n_pattern_parts := count(pattern_parts)
29+
n_str_parts := count(str_parts)
30+
suffix := trim(pattern, "*.")
31+
endswith(str, suffix)
32+
}
33+
34+
fqdn_matches(str, pattern) {
35+
not contains(pattern, "*")
36+
str == pattern
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package kubernetes.admission
2+
3+
deny[msg] {
4+
input.request.kind.kind == "Deployment"
5+
input.request.namespace == "default"
6+
7+
msg := "Not allowed to create deployments in default namespace"
8+
}

0 commit comments

Comments
 (0)