Created
February 19, 2020 08:21
-
-
Save onelittlenightmusic/89c2d3d11a404a0e7b09a47e53a3ad8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kubernetes.admission | |
import data.kubernetes.namespaces | |
operations = {"CREATE", "UPDATE"} | |
deny[msg] { | |
input.request.kind.kind == "Ingress" | |
operations[input.request.operation] | |
host := input.request.object.spec.rules[_].host | |
not fqdn_matches_any(host, valid_ingress_hosts) | |
msg := sprintf("invalid ingress host %q", [host]) | |
} | |
valid_ingress_hosts = {host | | |
whitelist := namespaces[input.request.namespace].metadata.annotations["ingress-whitelist"] | |
hosts := split(whitelist, ",") | |
host := hosts[_] | |
} | |
fqdn_matches_any(str, patterns) { | |
fqdn_matches(str, patterns[_]) | |
} | |
fqdn_matches(str, pattern) { | |
pattern_parts := split(pattern, ".") | |
pattern_parts[0] == "*" | |
str_parts := split(str, ".") | |
n_pattern_parts := count(pattern_parts) | |
n_str_parts := count(str_parts) | |
suffix := trim(pattern, "*.") | |
endswith(str, suffix) | |
} | |
fqdn_matches(str, pattern) { | |
not contains(pattern, "*") | |
str == pattern | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment