Skip to content

fix: de-duplicate dnsNames when multiple Gateway/ListenerSet listeners share a Secret - #8978

Merged
cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
speer:fix-dedup
Jul 11, 2026
Merged

cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
speer:fix-dedup

Conversation

@speer

@speer speer commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Pull Request Motivation

Listeners referencing the same Secret and hostname produced repeated entries in Certificate.spec.dnsNames, so the CSR carried more SANs than the ACME order had identifiers and finalize failed. De-duplicate hosts before building dnsNames/ipAddresses.

Failed to finalize Order: 400 urn:ietf:params:acme:error:badCSR: Unexpected number of identifiers found in CSR. 3 instead of 1 

Kind

/kind bug

Release Note

De-duplicate dnsNames when multiple Gateway/ListenerSet listeners share a Secret

@cert-manager-prow cert-manager-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 7, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

Hi @speer. Thanks for your PR.

I'm waiting for a cert-manager member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@cert-manager-prow cert-manager-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 7, 2026

@kunalworldwide kunalworldwide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

De-duplicating hosts before splitting into DNS names and IPs is the right fix. The regression test clearly shows how two listeners with the same hostname and Secret previously produced two identical dnsNames, causing ACME finalize to fail.

Using a map-based dedup before extractCertificateInput is clean and preserves the existing order semantics. One minor suggestion: consider also asserting in the test that the resulting Certificate has exactly the expected SANs and no extras, so future regressions in the split logic are caught too.

LGTM.

…s share a Secret

Listeners referencing the same Secret and hostname produced repeated
entries in Certificate.spec.dnsNames, so the CSR carried more SANs than
the ACME order had identifiers and finalize failed. De-duplicate hosts
before building dnsNames/ipAddresses.

Signed-off-by: Stefan Peer <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a Gateway API certificate-shim edge case where multiple listeners referencing the same Secret and hostname could cause duplicated entries in Certificate.spec.dnsNames, leading to ACME finalize failures due to a CSR SAN count mismatch.

Changes:

  • Refactors host processing into a new splitHosts helper that de-duplicates host entries and splits them into DNS names vs IP addresses while preserving first-seen order.
  • Updates certificate construction to use splitHosts instead of inline host splitting logic.
  • Adds regression coverage in TestSync for the “multiple listeners share Secret+hostname” scenario and adds focused unit tests for splitHosts.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/controller/certificate-shim/sync.go Introduces splitHosts to de-duplicate and split host inputs, and uses it during Certificate spec construction.
pkg/controller/certificate-shim/sync_test.go Adds a Gateway regression test to ensure no duplicated dnsNames and a dedicated splitHosts unit test table.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ThatsMrTalbot

Copy link
Copy Markdown
Contributor

Thanks for contributing! Seems like a sensible fix

/ok-to-test

@cert-manager-prow cert-manager-prow Bot added ok-to-test and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 11, 2026
@speer

speer commented Jul 11, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@ThatsMrTalbot

Copy link
Copy Markdown
Contributor

All tests are passing, this is good to merge. Thanks!

/lgtm
/approve

@cert-manager-prow cert-manager-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Jul 11, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ThatsMrTalbot

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 11, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit f94d3ea into cert-manager:master Jul 11, 2026
@speer
speer deleted the fix-dedup branch July 11, 2026 13:02
@wallrj wallrj mentioned this pull request Jul 29, 2026
13 tasks done
@wallrj

wallrj commented Sep 1, 2026

Copy link
Copy Markdown
Member

/cherry-pick release-1.21

with claude fable-5

@cert-manager-bot

Copy link
Copy Markdown
Contributor

@wallrj: new pull request created: #9234

Details

In response to this:

/cherry-pick release-1.21

with claude fable-5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@wallrj

wallrj commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

Pre-backport safety review

Before merging the cherry-pick to release-1.21 (#9234) I reviewed this change as merged (f94d3ea). Verdict: safe to backport. The refactor faithfully preserves the behaviour of the code it replaced (ordering, nil-slice semantics, field mapping — so no mass Certificate rewrite on upgrade), and the certificate-shim test suites pass on release-1.21 with the change applied. The findings below are completeness gaps and pre-existing bugs, not regressions, so they don't block the backport — but they're worth follow-ups on master.

1. The dedup can be undone by annotations

translateAnnotations appends cert-manager.io/alt-names and cert-manager.io/ip-sans to crt.Spec.DNSNames/IPAddresses after splitHosts has de-duplicated. An Ingress with host example.com plus annotation cert-manager.io/alt-names: "example.com" reproduces the same badCSR failure this PR fixes. The dedup would be more robust as a single normalisation pass on the assembled crt.Spec, after translateAnnotations.

2. certNeedsUpdate never compares IPAddresses

certNeedsUpdate compares DNSNames element-wise but never reads Spec.IPAddresses. A Certificate created by a pre-fix cert-manager whose only duplicates are IP SANs is never rewritten after upgrade — it stays wedged on badCSR until deleted manually. Legitimate IP-SAN changes are also silently ignored. (Pre-existing, but it limits what this fix repairs.)

3. Pre-existing: buildCertificates mutates the informer-cached object's labels

labels := ingLike.GetLabels() returns the live map of the lister object (the ingress and gateway controllers pass it in without a DeepCopy), and delete(labels, applysetLabel) mutates it — stripping the label from the shared informer cache, with a concurrent map read/write surface since the gateways and listenerset controllers share the Gateway informer.

4. Pre-existing: dead setIssuerSpecificConfig call on the update path

setIssuerSpecificConfig(crt, ingLike) in the update branch mutates crt, which is then discarded — updateCrt only receives crt.Spec and labels. Issuer-specific annotations (e.g. from acme.cert-manager.io/http01-edit-in-place) never reach the API server on updates. The call presumably meant to target updateCrt.

5. The root cause lives below the shim

GenerateCSR copies Spec.DNSNames into the CSR verbatim while acmeorders set-collapses the identifiers, and nothing in validation rejects duplicate dnsNames/ipAddresses. Any other producer of a Certificate with duplicates (including a user writing one by hand) still hits badCSR. A field.Duplicate check at admission would fix every producer. (A naive dedup inside GenerateCSR alone would be worse: RequestMatchesSpec compares length-sensitively and would loop recreating CertificateRequests.)

6. Latent: the dedup is textual, not semantic

splitHosts keys the seen map on the raw string, so two spellings of one IP (2001:db8::1 vs 2001:0db8::1) would both survive and recreate the mismatch. Unreachable today (Ingress/Gateway host formats can't express alternate spellings), but keying on net.ParseIP(h).String() with fallback to h would make it semantic.

Minor (tests)

  • The new Gateway regression test spells out three byte-identical listener blocks; two prove the behaviour, and Test_splitHosts covers the N-way case.
  • func() *gwapi.Group { g := gwapi.Group("core"); return &g }() closures where the adjacent line already uses new(...), and k8s.io/utils/ptr is available.
  • Test_splitHosts hand-rolls reflect.DeepEqual + %#v where sibling tables use assert.Equal.

with claude fable-5

KR-Ravindra added a commit to KR-Ravindra/cert-manager that referenced this pull request Sep 8, 2026
certNeedsUpdate compared dnsNames, secretName, issuerRef and the private
key settings of the desired and existing Certificate, but never looked at
spec.ipAddresses. buildCertificates does populate ipAddresses from the
IP-literal hosts of an Ingress, Gateway or ListenerSet, so a change to the
IP SANs was computed and then dropped because the shim decided the
existing Certificate was already up to date. The same gap kept the
de-duplication from cert-manager#8978 from repairing Certificates that already carried
duplicate IP SANs.

Compare ipAddresses by length and then element by element, exactly like
dnsNames, so IP SAN additions, removals, re-orderings and de-duplication
reach the existing Certificate.

Add a unit test for certNeedsUpdate covering equal, nil-vs-empty, changed,
added, removed, de-duplicated and re-ordered ipAddresses, and a TestSync
case that changes the IP-literal host of an Ingress and expects the
existing Certificate to be updated.

Signed-off-by: KR-Ravindra <[email protected]>
@wallrj

wallrj commented Sep 11, 2026

Copy link
Copy Markdown
Member

@speer This change has been included in v1.21.2, which is now published.

If you are able to install the release and verify that the change works as expected in your environment, that would be much appreciated. Thank you for the contribution.

David-Uka added a commit to David-Uka/cert-manager that referenced this pull request Sep 11, 2026
certNeedsUpdate compared dnsNames, secretName, the issuer reference and
the private key settings, but never read Spec.IPAddresses. buildCertificates
does set IPAddresses on the desired Certificate, so the new value was
computed and then discarded: a change to the IP SANs of an Ingress or a
Gateway never reached an existing Certificate.

This also limited what cert-manager#8978 repairs. A Certificate created by an older
cert-manager with duplicate IP SANs kept its duplicates after the upgrade,
leaving issuance stuck on badCSR until the Certificate was deleted by hand.

Compare IPAddresses the same way DNSNames is compared.

Signed-off-by: David-Uka <[email protected]>
@speer

speer commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

@speer This change has been included in v1.21.2, which is now published.

If you are able to install the release and verify that the change works as expected in your environment, that would be much appreciated. Thank you for the contribution.

@wallrj Thank you for the release! We tested the version in our environment and I can confirm that it works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. ok-to-test release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants