Skip to content

Commit 32241af

Browse files
committed
feat(gateway): drop tenant-* ban on gateway-attached-namespaces
Removes two gates that prevented child tenants from attaching to a parent's publishing Gateway: 1. Render-time fail() in templates/gateway-hostname-policy.yaml that refused to render the chart when _cluster.gateway-attached- namespaces contained any tenant-* entry. 2. cozystack-gateway-attached-namespaces-policy VAP that enforced the same rule at Package CR admission time. The original rationale — 'a tenant in attachedNamespaces can hijack the publishing tenant's hostnames' — assumed a static-name allowedRoutes whitelist where adding tenant-* literally widened the attach surface for arbitrary hostnames. Under the inheritance refactor (Stage 1/3/5) the attach surface is governed by the namespace.cozystack.io/gateway label selector, not by entries in AttachedNamespaces; and hostname hijack is independently blocked by three other layers: * Layer 4 — cozystack-tenant-host-policy VAP on Tenant.spec.host * Layer 5 — cozystack-namespace-host-label-policy VAP on the namespace label * Layer 7 — cozystack-route-hostname-policy VAP on HTTPRoute and TLSRoute hostnames Those three defend against the hijack vector regardless of what sits in attachedNamespaces, and they apply uniformly to every tenant (inheriting or owning a Gateway). The dropped gates added no extra defence, while their presence broke the inheritance flow kvaps reasonably asks for in the May 25 review. Doc count for gateway-hostname-policy.yaml drops from 8 to 6 (one VAP + one Binding removed); helm-unittest fixtures updated to match, with a regression-guard test asserting the removed VAP name no longer appears in any rendered document. Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 99fe080 commit 32241af

2 files changed

Lines changed: 54 additions & 112 deletions

File tree

‎packages/system/cozystack-basics/templates/gateway-hostname-policy.yaml‎

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
{{- $attachedNamespaces := (index .Values._cluster "gateway-attached-namespaces") | default "" }}
2-
{{- range splitList "," $attachedNamespaces }}
3-
{{- $ns := . | trim }}
4-
{{- if and $ns (hasPrefix "tenant-" $ns) }}
5-
{{- fail (printf "publishing.gateway.attachedNamespaces must not contain tenant-* namespaces (got %q). Adding a tenant namespace here lets that tenant attach HTTPRoutes to the publishing tenant's Gateway and hijack hostnames. Child tenants that need their own Gateway must opt in via tenant.spec.gateway=true instead." $ns) }}
6-
{{- end }}
7-
{{- end }}
81
---
92
apiVersion: admissionregistration.k8s.io/v1
103
kind: ValidatingAdmissionPolicy
@@ -58,46 +51,6 @@ spec:
5851
---
5952
apiVersion: admissionregistration.k8s.io/v1
6053
kind: ValidatingAdmissionPolicy
61-
metadata:
62-
name: cozystack-gateway-attached-namespaces-policy
63-
labels:
64-
internal.cozystack.io/managed-by-cozystack: ""
65-
spec:
66-
failurePolicy: Fail
67-
matchConstraints:
68-
resourceRules:
69-
- apiGroups: ["cozystack.io"]
70-
apiVersions: ["v1alpha1"]
71-
operations: ["CREATE", "UPDATE"]
72-
resources: ["packages"]
73-
matchConditions:
74-
- name: has-gateway-attached-namespaces
75-
expression: >-
76-
has(object.spec) &&
77-
has(object.spec.components) &&
78-
"platform" in object.spec.components &&
79-
has(object.spec.components.platform.values) &&
80-
"gateway" in object.spec.components.platform.values &&
81-
"attachedNamespaces" in object.spec.components.platform.values.gateway
82-
validations:
83-
- expression: >-
84-
object.spec.components.platform.values.gateway.attachedNamespaces.all(ns, !ns.startsWith("tenant-"))
85-
messageExpression: >-
86-
"spec.components.platform.values.gateway.attachedNamespaces must not contain any tenant-* namespace; use tenant.spec.gateway=true instead of adding a tenant namespace to the publishing Gateway's whitelist"
87-
reason: Forbidden
88-
---
89-
apiVersion: admissionregistration.k8s.io/v1
90-
kind: ValidatingAdmissionPolicyBinding
91-
metadata:
92-
name: cozystack-gateway-attached-namespaces-policy
93-
labels:
94-
internal.cozystack.io/managed-by-cozystack: ""
95-
spec:
96-
policyName: cozystack-gateway-attached-namespaces-policy
97-
validationActions: [Deny]
98-
---
99-
apiVersion: admissionregistration.k8s.io/v1
100-
kind: ValidatingAdmissionPolicy
10154
metadata:
10255
name: cozystack-tenant-host-policy
10356
labels:
Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
suite: gateway-hostname-policy security stack (Layers 2-5 of 7-layer model)
1+
suite: gateway-hostname-policy security stack (Layers 1+2+4+5 of the model)
22
templates:
33
- templates/gateway-hostname-policy.yaml
44

55
release:
66
name: cozystack-basics
77
namespace: cozy-system
88

9+
# After the inheritance refactor: every namespace with the label
10+
# namespace.cozystack.io/gateway=<owner> attaches to that owner's
11+
# Gateway, including tenant-* descendants. The previous render-time
12+
# guard + `cozystack-gateway-attached-namespaces-policy` VAP banned
13+
# `tenant-*` entries in publishing.gateway.attachedNamespaces under
14+
# the assumption that listing a tenant there would let it hijack
15+
# hostnames. That assumption no longer holds — Layer 4 (Tenant.spec.
16+
# host VAP), Layer 5 (namespace label immutability VAP), and Layer
17+
# 7 (HTTPRoute hostname VAP, in route-hostname-policy.yaml) already
18+
# defend against hostname hijack independently of attach-list shape.
19+
# Both gates are dropped; inheritance now flows via the label
20+
# selector on Gateway.spec.listeners[].allowedRoutes.
21+
922
tests:
10-
- it: renders 4 VAPs + 4 Bindings (8 documents total)
23+
- it: renders 3 VAPs + 3 Bindings (6 documents total)
1124
asserts:
1225
- hasDocuments:
13-
count: 8
26+
count: 6
1427

1528
- it: cozystack-gateway-hostname-policy targets gateway resources
1629
asserts:
@@ -76,126 +89,102 @@ tests:
7689
path: spec.validationActions
7790
value: [Deny]
7891

79-
- it: cozystack-gateway-attached-namespaces-policy targets Package CRs
80-
asserts:
81-
- documentIndex: 2
82-
equal:
83-
path: metadata.name
84-
value: cozystack-gateway-attached-namespaces-policy
85-
- documentIndex: 2
86-
equal:
87-
path: spec.matchConstraints.resourceRules[0].apiGroups
88-
value: ["cozystack.io"]
89-
- documentIndex: 2
90-
equal:
91-
path: spec.matchConstraints.resourceRules[0].resources
92-
value: ["packages"]
93-
- documentIndex: 2
94-
matchRegex:
95-
path: spec.validations[0].expression
96-
pattern: 'startsWith\("tenant-"\)'
97-
98-
- it: cozystack-gateway-attached-namespaces-policy Binding is fail-closed
99-
asserts:
100-
- documentIndex: 3
101-
equal:
102-
path: spec.validationActions
103-
value: [Deny]
104-
- documentIndex: 3
105-
equal:
106-
path: spec.policyName
107-
value: cozystack-gateway-attached-namespaces-policy
108-
10992
- it: cozystack-tenant-host-policy targets Tenant CRs and gates on trusted callers
11093
asserts:
111-
- documentIndex: 4
94+
- documentIndex: 2
11295
equal:
11396
path: metadata.name
11497
value: cozystack-tenant-host-policy
115-
- documentIndex: 4
98+
- documentIndex: 2
11699
equal:
117100
path: spec.matchConstraints.resourceRules[0].apiGroups
118101
value: ["apps.cozystack.io"]
119-
- documentIndex: 4
102+
- documentIndex: 2
120103
equal:
121104
path: spec.matchConstraints.resourceRules[0].resources
122105
value: ["tenants"]
123-
- documentIndex: 4
106+
- documentIndex: 2
124107
matchRegex:
125108
path: spec.variables[?(@.name=="trustedCaller")].expression
126109
pattern: 'system:masters'
127-
- documentIndex: 4
110+
- documentIndex: 2
128111
matchRegex:
129112
path: spec.variables[?(@.name=="trustedCaller")].expression
130113
pattern: 'cozy-system'
131-
- documentIndex: 4
114+
- documentIndex: 2
132115
matchRegex:
133116
path: spec.variables[?(@.name=="trustedCaller")].expression
134117
pattern: 'cozy-fluxcd'
135-
- documentIndex: 4
118+
- documentIndex: 2
136119
matchRegex:
137120
path: spec.variables[?(@.name=="trustedCaller")].expression
138121
pattern: 'cozy-cert-manager'
139122

140123
- it: cozystack-tenant-host-policy Binding is fail-closed
141124
asserts:
142-
- documentIndex: 5
125+
- documentIndex: 3
143126
equal:
144127
path: spec.validationActions
145128
value: [Deny]
146129

147130
- it: cozystack-namespace-host-label-policy targets core/v1 Namespace
148131
asserts:
149-
- documentIndex: 6
132+
- documentIndex: 4
150133
equal:
151134
path: metadata.name
152135
value: cozystack-namespace-host-label-policy
153-
- documentIndex: 6
136+
- documentIndex: 4
154137
equal:
155138
path: spec.matchConstraints.resourceRules[0].apiGroups
156139
value: [""]
157-
- documentIndex: 6
140+
- documentIndex: 4
158141
equal:
159142
path: spec.matchConstraints.resourceRules[0].resources
160143
value: ["namespaces"]
161-
- documentIndex: 6
144+
- documentIndex: 4
162145
matchRegex:
163146
path: spec.validations[0].messageExpression
164147
pattern: 'immutable'
165148

166149
- it: cozystack-namespace-host-label-policy Binding is fail-closed
167150
asserts:
168-
- documentIndex: 7
151+
- documentIndex: 5
169152
equal:
170153
path: spec.validationActions
171154
value: [Deny]
172-
- documentIndex: 7
155+
- documentIndex: 5
173156
equal:
174157
path: spec.policyName
175158
value: cozystack-namespace-host-label-policy
176159

177-
- it: render fails when _cluster.gateway-attached-namespaces contains a tenant-* entry
160+
- it: render passes when gateway-attached-namespaces contains a tenant-* entry
161+
# Inheritance refactor: tenant-* in the attach list is no longer
162+
# a hijack vector — the parent Gateway's allowedRoutes label
163+
# selector already includes every tenant labelled with
164+
# namespace.cozystack.io/gateway=<parent>, and hostname VAPs
165+
# (Layers 4/5/7) defend against any tenant attempting to claim
166+
# a hostname outside its own apex. Render must NOT fail.
178167
set:
179168
_cluster:
180169
gateway-attached-namespaces: "cozy-cert-manager,tenant-alice"
181-
asserts:
182-
- failedTemplate:
183-
errorMessage: 'publishing.gateway.attachedNamespaces must not contain tenant-* namespaces (got "tenant-alice"). Adding a tenant namespace here lets that tenant attach HTTPRoutes to the publishing tenant''s Gateway and hijack hostnames. Child tenants that need their own Gateway must opt in via tenant.spec.gateway=true instead.'
184-
185-
- it: render passes with empty gateway-attached-namespaces
186170
asserts:
187171
- hasDocuments:
188-
count: 8
172+
count: 6
189173

190-
- it: pins layer count — Layers 2-5 are exactly 4 VAP+Binding pairs
191-
# The gateway README documents a 7-layer security model:
192-
# - Layer 1 (listener allowedRoutes selector) — controller-owned
193-
# - Layers 2-5 — VAPs on Gateway / Package / Tenant / Namespace
194-
# rendered by THIS template (4 VAPs × 2 documents = 8 docs)
195-
# - Layer 6 (render-time fail) — also in this template (no extra docs)
196-
# - Layer 7 (route-hostname VAP) — separate template
197-
# If a maintainer trims one of the layer-2-5 VAPs without updating
198-
# README.md, this assertion fires before the change merges.
174+
- it: cozystack-gateway-attached-namespaces-policy VAP is no longer rendered
175+
# Regression guard: the dropped VAP must not reappear in a
176+
# future refactor that re-introduces the tenant-* ban.
199177
asserts:
200-
- hasDocuments:
201-
count: 8
178+
- notFailedTemplate: {}
179+
- documentIndex: 0
180+
notEqual:
181+
path: metadata.name
182+
value: cozystack-gateway-attached-namespaces-policy
183+
- documentIndex: 2
184+
notEqual:
185+
path: metadata.name
186+
value: cozystack-gateway-attached-namespaces-policy
187+
- documentIndex: 4
188+
notEqual:
189+
path: metadata.name
190+
value: cozystack-gateway-attached-namespaces-policy

0 commit comments

Comments
 (0)