Files
cozystack/internal/controller/tenantgateway/rbac_test.go
T
Aleksei Sviridkin ad3f15d4f3 feat(gateway)!: reserve a passthrough listener's name by the spec alone
The TenantGateway controller withheld the HTTPS-terminate listener for a
name a passthrough listener answers only once a TLSRoute was attached
there and resolved a backend, mirroring how Cilium builds its filter
chains. That kept a served endpoint up on Cilium 1.19.5, but it tied
every route's status to one implementation's translator and needed
Service and ReferenceGrant watches to land the deferred decision.

The shipped operator accepts a wildcard terminate listener beside a
narrower passthrough listener on 443 and keeps conflicting every other
overlap. A terminate listener for the passthrough name itself is not
narrower, so on 443 the rule now protects the dataplane outright; on a
native port Gateway API would serve the pair, and the rule holds as a
policy that keeps a platform name with its passthrough listener. Reading
it off the spec makes the outcome the same on every implementation.

Under http01 an HTTPRoute claiming such a name gets no listener and no
certificate whether or not a TLSRoute is attached. A route whose every
hostname is reserved reads Accepted=False with
NoMatchingListenerHostname; one that also claims a free hostname stays
Accepted and is served there, as Gateway API accepts a route whose
hostnames intersect at least one listener. dns01 and existingSecret are
unchanged: their wildcard listener still covers such a name, and the
passthrough listener answers it by its exact server name.

The chart writes the passthrough services on the publishing tenant
alone, so the names reserved by default are the ones the platform
publishes there through its own TLSRoutes. The shape an upgrade
changes is an HTTPRoute on such a name whose platform TLSRoute forwards
nowhere, because it never attached (the package owning the service is
not installed) or because none of its backends resolve: that route kept
a terminate listener and now gets none.

The backend resolution, its watches and the RBAC rules that served them
go away.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-10 06:32:42 +03:00

110 lines
3.7 KiB
Go

/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tenantgateway
import (
"os"
"slices"
"testing"
rbacv1 "k8s.io/api/rbac/v1"
"sigs.k8s.io/yaml"
)
// clusterRolePath is the ClusterRole the cozystack-controller chart
// ships. It is written by hand: the kubebuilder rbac markers in this
// package document the grants but generate nothing, because
// hack/update-codegen.sh runs controller-gen over ./api/... only and
// writes no rbac artifact anywhere. Nothing but this test connects the
// two.
const clusterRolePath = "../../../packages/system/cozystack-controller/templates/rbac.yaml"
// TestShippedClusterRoleNamesEveryTypeThisControllerReads pins that
// each type this reconciler watches or reads has a rule naming its API
// group, rather than reaching the apiserver only through the
// read-everything rule at the foot of the ClusterRole.
//
// An informer that starts without a grant fails with Forbidden and
// takes the manager down with it, and the failure arrives whenever
// someone narrows that catch-all, long after the watch that needed it
// was added. A rule naming the group is what survives that edit.
//
// Resource wildcards inside a named group are accepted: the rule
// covering this project's own CRDs is written that way, and narrowing
// the catch-all does not touch it. An apiGroups entry of "*" is not,
// because it is the rule this test exists to stop the controller
// depending on.
//
// The template is parsed as YAML rather than rendered, which holds
// because it carries no Helm actions. Putting one in it, a
// {{/* */}} comment included, means rendering the chart here instead.
func TestShippedClusterRoleNamesEveryTypeThisControllerReads(t *testing.T) {
needed := []struct {
group string
resource string
}{
{"gateway.cozystack.io", "tenantgateways"},
{"gateway.networking.k8s.io", "gateways"},
{"gateway.networking.k8s.io", "httproutes"},
{"gateway.networking.k8s.io", "tlsroutes"},
{"gateway.networking.k8s.io", "gatewayclasses"},
{"cert-manager.io", "certificates"},
{"cert-manager.io", "issuers"},
{"", "namespaces"},
}
raw, err := os.ReadFile(clusterRolePath)
if err != nil {
t.Fatalf("read ClusterRole: %v", err)
}
var role rbacv1.ClusterRole
if err := yaml.Unmarshal(raw, &role); err != nil {
t.Fatalf("unmarshal ClusterRole: %v", err)
}
if len(role.Rules) == 0 {
t.Fatal("ClusterRole carries no rules; the path or the document shape changed")
}
for _, want := range needed {
granted := false
for _, rule := range role.Rules {
if !slices.Contains(rule.APIGroups, want.group) {
continue
}
if !slices.Contains(rule.Resources, want.resource) && !slices.Contains(rule.Resources, "*") {
continue
}
if !slices.Contains(rule.Verbs, "*") {
missing := false
for _, verb := range []string{"get", "list", "watch"} {
if !slices.Contains(rule.Verbs, verb) {
missing = true
}
}
if missing {
continue
}
}
granted = true
break
}
if !granted {
t.Errorf("no rule names apiGroup %q resource %q with get/list/watch; the controller reaches it only through the read-everything rule, and narrowing that breaks the informer with Forbidden", want.group, want.resource)
}
}
}