From 9037cf175e7caebd1c8038b6f473b94ab2851e9c Mon Sep 17 00:00:00 2001 From: somaz Date: Mon, 23 Nov 2026 10:59:26 +0300 Subject: [PATCH] fix(gateway): skip a child apex the wildcard certificate already covers In DNS-01 mode every inheriting child tenant's apex went into the parent Gateway's wildcard Certificate. When a wildcard in the same Certificate already covers that apex one label up, Let's Encrypt refuses the whole order as redundant and the parent Gateway gets no certificate at all. Skip such a SAN, matching the single-label check Boulder runs in wildcardOverlap. A child may also use a broader custom domain than the gateway. Its wildcard then covers the gateway apex, so the same check drops the gateway SAN too. Closes: #5125 Co-authored-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Assisted-by: LLM Signed-off-by: somaz Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> (cherry picked from commit 8382ed1a92ec40bed270323dba2f8d522c41655d) Signed-off-by: Myasnikov Daniil --- .../tenantgateway/reconciler_test.go | 10 ++-- .../controller/tenantgateway/renderers.go | 48 +++++++++------ .../tenantgateway/renderers_test.go | 59 +++++++++++++++++++ packages/extra/gateway/README.md | 2 +- 4 files changed, 95 insertions(+), 24 deletions(-) diff --git a/internal/controller/tenantgateway/reconciler_test.go b/internal/controller/tenantgateway/reconciler_test.go index de51d74b2..53e4a98da 100644 --- a/internal/controller/tenantgateway/reconciler_test.go +++ b/internal/controller/tenantgateway/reconciler_test.go @@ -1813,9 +1813,9 @@ func TestReconcile_HTTP01CollectsHostnamesFromInheritingChildNamespaces(t *testi // the SAN-expansion contract: when a tenant inherits this Gateway's // publishing layer (its namespace is labelled namespace.cozystack. // io/gateway=), the wildcard Certificate that the owner -// issues for DNS-01 mode must also cover the child's apex — -// and *.. Let's Encrypt wildcards are -// single-level, so the parent's `*.` does not match a child +// issues for DNS-01 mode must also cover the child's apex and +// *.. Let's Encrypt wildcards are single-level, so the +// parent's `*.` does not match a child // hostname `harbor.alice.example.com` (two labels deep). Without // SAN expansion the inheritance flow renders Gateway listeners // referencing a cert that fails the SNI handshake — silently in @@ -1882,7 +1882,6 @@ func TestReconcile_DNS01WildcardCertCoversInheritingChildApexes(t *testing.T) { want := map[string]bool{ "example.org": false, "*.example.org": false, - "alice.example.org": false, "*.alice.example.org": false, } for _, n := range cert.Spec.DNSNames { @@ -1895,6 +1894,9 @@ func TestReconcile_DNS01WildcardCertCoversInheritingChildApexes(t *testing.T) { t.Errorf("missing DNS name %q in cert.spec.dnsNames=%v", n, cert.Spec.DNSNames) } } + if containsString(cert.Spec.DNSNames, "alice.example.org") { + t.Errorf("cert.spec.dnsNames=%v lists alice.example.org, which *.example.org already covers; Let's Encrypt refuses such an order", cert.Spec.DNSNames) + } } // TestReconcile_DNS01WildcardCertDeduplicatesChildApexEqualToParent diff --git a/internal/controller/tenantgateway/renderers.go b/internal/controller/tenantgateway/renderers.go index fbcb910c2..df3fda7b0 100644 --- a/internal/controller/tenantgateway/renderers.go +++ b/internal/controller/tenantgateway/renderers.go @@ -867,7 +867,8 @@ func buildSolver(tgw *gatewayv1alpha1.TenantGateway) (*cmacmev1.ACMEChallengeSol // renderWildcardCertificate builds the cert-manager Certificate that // covers and *., plus per-child-apex SANs for every -// tenant inheriting through this Gateway. Only used in DNS-01 mode; +// tenant inheriting through this Gateway, leaving out any apex that +// another apex's wildcard already covers. Only used in DNS-01 mode; // the listeners rendered by renderGateway reference its secretName. // // childApexes is the deduplicated, sorted list of apex hostnames @@ -878,29 +879,27 @@ func buildSolver(tgw *gatewayv1alpha1.TenantGateway) (*cmacmev1.ACMEChallengeSol // route's hostname (harbor.alice.example.com is two labels deeper // than the wildcard accepts). func (r *Reconciler) renderWildcardCertificate(tgw *gatewayv1alpha1.TenantGateway, childApexes []string) (*cmv1.Certificate, error) { - dnsNames := []string{tgw.Spec.Apex, "*." + tgw.Spec.Apex} - seen := map[string]struct{}{ - tgw.Spec.Apex: {}, - "*." + tgw.Spec.Apex: {}, + apexes := append([]string{tgw.Spec.Apex}, childApexes...) + // A child can use a custom domain whose wildcard covers the gateway apex. + wildcards := make(map[string]struct{}, len(apexes)) + for _, apex := range apexes { + if apex != "" { + wildcards["*."+apex] = struct{}{} + } } - for _, apex := range childApexes { - if apex == "" { + var dnsNames []string + seen := make(map[string]bool, len(apexes)) + for _, apex := range apexes { + if apex == "" || seen[apex] { continue } - // Skip a child whose host label collides with the parent - // apex (mis-labelled namespace, or two tenants briefly - // sharing an apex during a rename). cert-manager rejects - // duplicate dnsNames; the inheriting tenant still attaches - // via the parent SANs already present. - if _, dup := seen[apex]; !dup { + seen[apex] = true + // Let's Encrypt refuses the whole order when it lists a name + // that a wildcard in the same order already covers. + if !coveredByWildcard(wildcards, apex) { dnsNames = append(dnsNames, apex) - seen[apex] = struct{}{} - } - wildcard := "*." + apex - if _, dup := seen[wildcard]; !dup { - dnsNames = append(dnsNames, wildcard) - seen[wildcard] = struct{}{} } + dnsNames = append(dnsNames, "*."+apex) } cert := &cmv1.Certificate{ ObjectMeta: metav1.ObjectMeta{ @@ -925,6 +924,17 @@ func (r *Reconciler) renderWildcardCertificate(tgw *gatewayv1alpha1.TenantGatewa return cert, nil } +// coveredByWildcard mirrors Boulder's wildcardOverlap check. Unlike +// hostnameCovers, a certificate wildcard spans exactly one label. +func coveredByWildcard(wildcards map[string]struct{}, host string) bool { + _, parent, ok := strings.Cut(host, ".") + if !ok { + return false + } + _, covered := wildcards["*."+parent] + return covered +} + func ptrGroup(g string) *gatewayv1.Group { gg := gatewayv1.Group(g) return &gg diff --git a/internal/controller/tenantgateway/renderers_test.go b/internal/controller/tenantgateway/renderers_test.go index d7c60d40a..c0dc69fc0 100644 --- a/internal/controller/tenantgateway/renderers_test.go +++ b/internal/controller/tenantgateway/renderers_test.go @@ -22,6 +22,7 @@ import ( "io" "os" "regexp" + "slices" "testing" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -203,3 +204,61 @@ func TestPerListenerName_KeepsEveryNameThatWasAlreadyAdmissible(t *testing.T) { } } } + +// TestRenderWildcardCertificate_SkipsChildApexCoveredByWildcard pins the +// dnsNames against Let's Encrypt refusing a whole order that lists a name +// one of its wildcards covers, leaving the Gateway with no certificate. +// A wildcard spans one label, so an apex two labels below stays listed. +func TestRenderWildcardCertificate_SkipsChildApexCoveredByWildcard(t *testing.T) { + cases := []struct { + name string + childApexes []string + want []string + }{ + { + name: "an inheriting tenant uses the parent domain of the gateway apex", + childApexes: []string{"example.com"}, + want: []string{"*.mgmt.example.com", "example.com", "*.example.com"}, + }, + { + name: "child one label below the parent", + childApexes: []string{"ci.mgmt.example.com"}, + want: []string{"mgmt.example.com", "*.mgmt.example.com", "*.ci.mgmt.example.com"}, + }, + { + name: "child two labels below the parent", + childApexes: []string{"b.ci.mgmt.example.com"}, + want: []string{"mgmt.example.com", "*.mgmt.example.com", "b.ci.mgmt.example.com", "*.b.ci.mgmt.example.com"}, + }, + { + name: "covering child wildcard sorts after the apex it covers", + childApexes: []string{"a.z.mgmt.example.com", "z.mgmt.example.com"}, + want: []string{"mgmt.example.com", "*.mgmt.example.com", "*.a.z.mgmt.example.com", "*.z.mgmt.example.com"}, + }, + { + name: "empty apex and apex outside the parent", + childApexes: []string{"", "alice.example.org"}, + want: []string{"mgmt.example.com", "*.mgmt.example.com", "alice.example.org", "*.alice.example.org"}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + tgw := &gatewayv1alpha1.TenantGateway{ + ObjectMeta: metav1.ObjectMeta{Name: "cozystack", Namespace: "tenant-management"}, + Spec: gatewayv1alpha1.TenantGatewaySpec{ + Apex: "mgmt.example.com", + CertMode: gatewayv1alpha1.CertModeDNS01, + GatewayClassName: "cilium", + }, + } + r := &Reconciler{Scheme: newScheme(t)} + cert, err := r.renderWildcardCertificate(tgw, tc.childApexes) + if err != nil { + t.Fatalf("renderWildcardCertificate: %v", err) + } + if !slices.Equal(cert.Spec.DNSNames, tc.want) { + t.Errorf("dnsNames = %v, want %v", cert.Spec.DNSNames, tc.want) + } + }) + } +} diff --git a/packages/extra/gateway/README.md b/packages/extra/gateway/README.md index de2110479..cc6caa9c7 100644 --- a/packages/extra/gateway/README.md +++ b/packages/extra/gateway/README.md @@ -44,7 +44,7 @@ The platform chart writes those values into `_cluster.dns01-*` keys consumed by DNS-01 mode renders a single wildcard `Certificate` covering `` and `*.`, plus the corresponding `https` (`*.`) and `https-apex` (``) listeners. New apps published under the apex pick up the existing wildcard cert without per-listener provisioning. -For inheriting child tenants under this Gateway: the controller extends the same wildcard Certificate with `` + `*.` SANs per child, and adds one `*.` listener per child apex referencing the same cert. Child apex SANs are discovered by listing namespaces carrying `namespace.cozystack.io/gateway = ` and reading their `namespace.cozystack.io/host` label. The ACME challenge must succeed for every SAN, which means the DNS provider account configured at the platform layer must be able to write TXT records under each child apex zone — for deeply-nested children that requires either zone delegation or a provider account with apex-spanning permissions. +For inheriting child tenants under this Gateway: the controller extends the same wildcard Certificate with a `*.` SAN per child, plus `` itself unless a wildcard already in the Certificate covers it (Let's Encrypt refuses an order that lists both), and adds one `*.` listener per child apex referencing the same cert. Child apex SANs are discovered by listing namespaces carrying `namespace.cozystack.io/gateway = ` and reading their `namespace.cozystack.io/host` label. The ACME challenge must succeed for every SAN, which means the DNS provider account configured at the platform layer must be able to write TXT records under each child apex zone — for deeply-nested children that requires either zone delegation or a provider account with apex-spanning permissions. Pick DNS-01 when you specifically want a wildcard cert (e.g. a long-lived staging cluster with many short-lived apps and tight LE rate limits). Otherwise stay on HTTP-01.