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

feat: tetrate policies #197

Draft
wants to merge 2 commits into
base: main
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
46 changes: 46 additions & 0 deletions tetrate/TIS0303/check-duplicate-certificate-gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: enforce-unique-gateway-tls
annotations:
policies.kyverno.io/title: Enforce Unique Gateway TLS Credentials
policies.kyverno.io/category: Istio Best Practices
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Gateway
policies.kyverno.io/description: >-
Ensures that the same TLS credentialName is not reused across multiple Gateways
in the same namespace to prevent 404 errors when clients reuse HTTP2 connections.
spec:
validationFailureAction: Enforce
rules:
- name: enforce-unique-gateway-tls
match:
any:
- resources:
kinds:
- Gateway
operations:
- CREATE
- UPDATE
context:
- name: manifestNamespace
variable:
value: "{{ request.object.metadata.namespace }}"
- name: manifestTLS
variable:
jmesPath: "request.object.spec.servers[].tls.credentialName | [?@ != null] | [*]"
- name: existingTLS
apiCall:
urlPath: "/apis/networking.istio.io/v1/namespaces/{{ manifestNamespace }}/gateways"
jmesPath: "items[].spec.servers[].tls.credentialName | [?@ != null] | [*]"
validate:
message: "TLS credentials are being reused across Gateways in namespace '{{ manifestNamespace }}'. This may cause 404 errors when clients reuse HTTP2 connections. TLS used in manifest: {{ manifestTLS }}, existingTLS: {{ existingTLS }}"
deny:
conditions:
all:
- key: "{{ manifestTLS }}"
operator: AnyIn
value: "{{ existingTLS }}"
- key: "{{ existingTLS }}"
operator: AnyIn
value: "{{ manifestTLS }}"
46 changes: 46 additions & 0 deletions tetrate/TIS0303/resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: good-resource-1
namespace: test
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https
protocol: HTTPS
hosts:
- "example.com"
tls:
mode: SIMPLE
credentialName: example-cert-2
---
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: bad-resource-1
namespace: test
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https
protocol: HTTPS
hosts:
- "example.com"
tls:
mode: SIMPLE
credentialName: example-cert-1
- port:
number: 443
name: https
protocol: HTTPS
hosts:
- "example.com"
tls:
mode: SIMPLE
credentialName: example-cert-3
42 changes: 42 additions & 0 deletions tetrate/TIS1402/enforce-valid-service-refs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: enforce-valid-service-refs
annotations:
policies.kyverno.io/title: Enforce Valid Service References
policies.kyverno.io/category: Istio Best Practices
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: HTTPRoute
policies.kyverno.io/description: >-
Ensures that backendRefs in HTTPRoute point to existing services within the same namespace.
spec:
validationFailureAction: Enforce
rules:
- name: enforce-valid-service-refs
match:
any:
- resources:
kinds:
- HTTPRoute
operations:
- CREATE
- UPDATE
context:
- name: namespace
variable:
value: "{{ request.object.metadata.namespace }}"
- name: serviceNames
variable:
jmesPath: "request.object.spec.rules[*].backendRefs[?kind=='Service' || kind==null].name | []"
- name: existingServices
apiCall:
urlPath: "/api/v1/namespaces/{{ namespace }}/services"
jmesPath: "items[*].metadata.name"
validate:
message: "One or more referenced services do not exist in namespace '{{ namespace }}'. Referenced: '{{ serviceNames }}', Existing: '{{ existingServices }}'."
deny:
conditions:
any:
- key: "{{ serviceNames }}"
operator: AnyNotIn
value: "{{ existingServices }}"
45 changes: 45 additions & 0 deletions tetrate/TIS1402/resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: good-resource-1
namespace: test
spec:
parentRefs:
- group: gateway.networking.k8s.io
kind: Gateway
name: eg
rules:
- backendRefs:
- group: ""
kind: Service
name: existing-service
namespace: test
port: 443
weight: 1
matches:
- path:
type: PathPrefix
value: /
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: bad-resource-1
namespace: test
spec:
parentRefs:
- group: gateway.networking.k8s.io
kind: Gateway
name: eg
rules:
- backendRefs:
- group: ""
kind: Service
name: non-existing-service
namespace: test
port: 443
weight: 1
matches:
- path:
type: PathPrefix
value: /