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

admission-control: validate that Ingress and RouteGroup hosts in hosted zone domain #8436

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
93 changes: 93 additions & 0 deletions cluster/manifests/01-admission-control/host-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# {{ $hosted_zone_parent_domain := slice (split .Values.hosted_zone ".") 1 | join "." }}

# {{ if eq .Cluster.ConfigItems.ingresses_validation "enabled" }}
Copy link
Member Author

@AlexanderYastrebov AlexanderYastrebov Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need another new config to toggle theses policies off?

---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: ingress-host-policy.teapot.zalan.do
annotations:
kubernetes.io/description: |
Validates that Ingress hosts from {{ $hosted_zone_parent_domain }} domain are in {{ .Values.hosted_zone }} domain.
spec:
failurePolicy: Fail
matchConstraints:
resourceRules:
- apiGroups: ["networking.k8s.io"]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["ingresses"]
matchConditions:
# exclude owned resources, e.g. created by StackSet and FabricGateway controllers.
- name: exclude-owned-resources
expression: |
!has(object.metadata.ownerReferences)
validations:
- expression: |
object.spec.rules
.map(r, r.host)
.filter(h, h.endsWith(".{{ $hosted_zone_parent_domain }}"))
.all(h, h.endsWith(".{{ .Values.hosted_zone }}"))
# show the first invalid host in the error message
messageExpression: |
"Ingress host must be in {{ .Values.hosted_zone }} domain but " +
object.spec.rules
.map(r, r.host)
.filter(h, h.endsWith(".{{ $hosted_zone_parent_domain }}"))
.filter(h, !h.endsWith(".{{ .Values.hosted_zone }}"))[0] +
" found"
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: ingress-host-policy-binding.teapot.zalan.do
spec:
policyName: ingress-host-policy.teapot.zalan.do
validationActions: [Deny]
# {{ end }}

# {{ if eq .Cluster.ConfigItems.routegroups_validation "enabled" }}
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: routegroup-host-policy.teapot.zalan.do
annotations:
kubernetes.io/description: |
Validates that RouteGroup hosts from {{ $hosted_zone_parent_domain }} domain are in {{ .Values.hosted_zone }} domain.
spec:
failurePolicy: Fail
matchConstraints:
resourceRules:
- apiGroups: ["zalando.org"]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["routegroups"]
matchConditions:
# exclude owned resources, e.g. created by StackSet and FabricGateway controllers.
- name: exclude-owned-resources
expression: |
!has(object.metadata.ownerReferences)
validations:
- expression: |
object.spec.hosts
.filter(h, h.endsWith(".{{ $hosted_zone_parent_domain }}"))
.all(h, h.endsWith(".{{ .Values.hosted_zone }}"))
# show the first invalid host in the error message
messageExpression: |
"RouteGroup host must be in {{ .Values.hosted_zone }} domain but " +
object.spec.hosts
.filter(h, h.endsWith(".{{ $hosted_zone_parent_domain }}"))
.filter(h, !h.endsWith(".{{ .Values.hosted_zone }}"))[0] +
" found"
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: routegroup-host-policy-binding.teapot.zalan.do
spec:
policyName: routegroup-host-policy.teapot.zalan.do
validationActions: [Deny]
# {{ end }}
14 changes: 14 additions & 0 deletions cluster/manifests/deletions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,17 @@ post_apply:
kind: Service
namespace: kube-system
{{- end }}

# {{ if ne .Cluster.ConfigItems.ingresses_validation "enabled" }}
- kind: ValidatingAdmissionPolicyBinding
name: ingress-host-policy-binding.teapot.zalan.do
- kind: ValidatingAdmissionPolicy
name: ingress-host-policy.teapot.zalan.do
# {{ end }}

# {{ if ne .Cluster.ConfigItems.routegroups_validation "enabled" }}
- kind: ValidatingAdmissionPolicyBinding
name: routegroup-host-policy-binding.teapot.zalan.do
- kind: ValidatingAdmissionPolicy
name: routegroup-host-policy.teapot.zalan.do
# {{ end }}