Skip to content

fix(digitalocean): paginate TXT record lookups in findTxtRecord - #9101

Merged
cert-manager-prow[bot] merged 3 commits into
cert-manager:masterfrom
Abmun:fix/digitalocean-txt-record-pagination-9099
Aug 10, 2026
Merged

cert-manager-prow[bot] merged 3 commits into
cert-manager:masterfrom
Abmun:fix/digitalocean-txt-record-pagination-9099

Conversation

@Abmun

@Abmun Abmun commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Pull Request Motivation

Fixes #9099.

The DigitalOcean DNS-01 solver's findTxtRecord lists a zone's TXT
records with nil godo.ListOptions, so the DigitalOcean API silently
caps the response at its default page size of 20 and only the first
page is ever inspected. Challenge records are always the newest
(highest-ID) records in a zone, so any zone holding more than ~20 TXT
records never matches a challenge record: CleanUp deletes nothing and
Present's duplicate-record check never finds an existing record — with
no error surfaced anywhere. This is the long-standing root cause of #1661.

findTxtRecord now pages through the full result set using an explicit
per_page and a loop over Response.Links, so it always inspects every
TXT record in the zone regardless of size.

It also now takes the zone name as a parameter instead of re-resolving
it via DNS internally (Present/CleanUp already know it), which
avoids a redundant lookup and makes the function directly unit-testable
against a mocked DigitalOcean API.

Testing: added TestFindTxtRecordPagination and
TestFindTxtRecordPaginationNoMatch, which spin up an httptest.Server
simulating a zone with more TXT records than fit on a single page (the
challenge record is placed on the last page) and a fake util.Resolver,
and assert that findTxtRecord still finds/doesn't find records
correctly across all pages.

Kind

/kind bug

The DigitalOcean DNS-01 solver no longer leaves stale `_acme-challenge` TXT records behind when a zone has more than 20 TXT records. Previously, `CleanUp` silently failed to find (and therefore delete) challenge records in zones above that size because record lookups were not paginated.

The DigitalOcean DNS-01 solver listed a zone's TXT records with nil
godo.ListOptions, so the DigitalOcean API silently capped the response
at its default page size of 20 and only the first page was ever
inspected. Challenge records are always the newest (highest-ID)
records in a zone, so any zone holding more than ~20 TXT records would
never match a challenge record: CleanUp deleted nothing and Present's
duplicate-record check never found an existing record, with no error
surfaced anywhere.

findTxtRecord now pages through the full result set using an explicit
per_page and a loop over Response.Links, so it always inspects every
TXT record in the zone regardless of size.

It also now takes the zone name as a parameter instead of re-resolving
it via DNS internally (Present/CleanUp already know it), which avoids
a redundant lookup and makes the function directly unit-testable
against a mocked DigitalOcean API.

Fixes cert-manager#9099

Signed-off-by: Abmun <[email protected]>
@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. area/acme Indicates a PR directly modifies the ACME Issuer code area/acme/dns01 Indicates a PR modifies ACME DNS01 provider code labels Aug 5, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

Hi @Abmun. 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 needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 5, 2026
@wallrj
wallrj requested a lite review from Copilot August 9, 2026 05:49

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 long-standing DigitalOcean DNS-01 cleanup/present bug where TXT record lookups only inspected the API’s default first page (20 records), causing challenge records in zones with more than ~20 TXT records to never be found (and therefore never cleaned up). The change updates the DigitalOcean solver to paginate TXT record listings and makes findTxtRecord take the already-resolved zone name to avoid redundant DNS lookups and enable focused unit testing.

Changes:

  • Update findTxtRecord to accept zoneName and to iterate through all paginated TXT record results from the DigitalOcean API.
  • Introduce an explicit per_page (via godo.ListOptions) to avoid the API’s default page size of 20 and reduce request count for large zones.
  • Add unit tests using an httptest.Server that simulates multi-page TXT record listings to ensure matches can be found beyond the first page.

Reviewed changes

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

File Description
pkg/issuer/acme/dns/digitalocean/digitalocean.go Pass zoneName into findTxtRecord, add explicit PerPage, and loop through all pages to ensure TXT records are fully inspected.
pkg/issuer/acme/dns/digitalocean/digitalocean_test.go Add regression tests with a paginated fake DigitalOcean API server to validate pagination behavior for both match and no-match cases.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@wallrj wallrj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The pagination fix itself looks right and the regression test is good. Two correctness concerns inline — the main one is that CleanUp deletes every name-matching TXT record regardless of value, and this PR widens that from the first 20 records to the whole zone. Also worth considering godo's RecordsByTypeAndName (server-side name filter), which would make each lookup a single request and sidestep most of the pagination pitfalls noted inline. The remaining comments are nits.

with claude fable-5

}

records, err := c.findTxtRecord(ctx, fqdn)
records, err := c.findTxtRecord(ctx, zoneName, fqdn)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This fix widens the blast radius of a pre-existing bug: CleanUp ignores its value parameter and deletes every TXT record whose name matches, not just the one holding this challenge's value. With pagination, records that used to be hidden beyond page 1 (the very case this PR targets) are now deleted too. If two challenges share a record name (example.com + *.example.com, or two orders racing), whichever finishes first deletes the sibling's record mid-validation.

Other solvers (route53, cloudflare) only delete the record whose data matches value. Worth filtering on record.Data == value here too — in this PR or a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@wallrj Good catch, fixed in 18580a5. CleanUp now skips any record where record.Data != value, so it only ever deletes the one holding this challenge's value. Added TestCleanUpOnlyDeletesMatchingValue to cover the sibling-record case you described.

func (c *DNSProvider) findTxtRecord(ctx context.Context, zoneName, fqdn string) ([]godo.DomainRecord, error) {
// The record Name doesn't contain the zoneName, so
// lets remove it before filtering the array of record
targetName := strings.TrimSuffix(fqdn, zoneName)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pre-existing (this line just moved), but worth noting while it's in flight: when the DigitalOcean zone is the challenge FQDN (a delegated _acme-challenge.example.com zone), targetName becomes empty, and DigitalOcean returns apex records with name @ — so util.ToFqdn(record.Name) == targetName never matches. Present then creates a duplicate record on every retry, and CleanUp silently deletes nothing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@wallrj Agreed this is a real bug, but I don't think I can fix it correctly in this PRm I don't have a way to verify how DigitalOcean's API actually resolves apex records (@) against a name filter query without live credentials, and I'd rather not guess at that behavior in something that deletes/creates DNS records. Switching to RecordsByTypeAndName may or may not incidentally fix it depending on how DO's server-side filter handles apex names, I haven't been able to confirm either way. Happy to file a follow-up issue for this rather than leave it silently unaddressed, let me know if you'd rather I take a swing at it here instead.

var records []godo.DomainRecord

opt := &godo.ListOptions{PerPage: recordsPerPage}
for {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Offset pagination without a stable snapshot: if a record on an earlier page is deleted while this loop is walking later pages (e.g. two challenges in the same zone cleaning up concurrently), every subsequent record shifts left by one, and a matching record straddling a page boundary is skipped — CleanUp then reports success while leaving the record behind (the #9099 symptom, now intermittent instead of deterministic). The server-side filter suggested below shrinks the window to near zero.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@wallrj Addressed by switching to the server-side name filter below (18580a5) , since findTxtRecord now only ever gets back records that share this name, the window for a concurrent create/delete to shift a match across a page boundary is effectively closed (would need >200 records under one name to even matter). Kept the pagination loop as a defensive fallback for that case rather than assuming a single page is always enough.


opt := &godo.ListOptions{PerPage: recordsPerPage}
for {
allRecords, resp, err := c.client.Domains.RecordsByType(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

godo has Domains.RecordsByTypeAndName, which passes the API's server-side name filter — it returns just the matching records in one request instead of walking every TXT record in the zone:

c.client.Domains.RecordsByTypeAndName(ctx, util.UnFqdn(zoneName), "TXT", util.UnFqdn(fqdn), opt)

(The name filter takes the full record FQDN, not the trimmed prefix.) For the large zones this PR targets, that avoids ceil(N/200) requests per Present/CleanUp against DigitalOcean's rate limit, and the client-side TrimSuffix/ToFqdn filtering can then be deleted. The pagination loop becomes a rarely-taken safety net rather than the hot path.

@Abmun Abmun Aug 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@wallrj Done in 18580a5 ,switched to RecordsByTypeAndName(ctx, util.UnFqdn(zoneName), "TXT", util.UnFqdn(fqdn), opt) and dropped the client-side TrimSuffix/ToFqdn matching entirely. Also updated the test fake server to enforce the name filter server-side (mirroring the real API) rather than have the client filter, so the tests actually exercise this path.

}
}

if resp == nil || resp.Links == nil || resp.Links.IsLastPage() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

resp is non-nil whenever err is nil (godo's contract), so the resp == nil arm is dead — and if it ever did fire, break would silently return a partial result as if it were complete.

Suggested change
if resp == nil || resp.Links == nil || resp.Links.IsLastPage() {
if resp.Links == nil || resp.Links.IsLastPage() {

IsLastPage already handles nil Pages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Took your suggestion, thanks. Removed the resp == nil check. Applied in 18580a5.

break
}

page, err := resp.Links.CurrentPage()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Links.CurrentPage() derives the page number from the prev link (pageForURL(prev) + 1), so a response with next set but prev missing would pin this at 2 forever and the loop would never terminate. Unlikely against the real API, but a local counter is simpler and unconditionally bounded: initialise opt with Page: 1 and replace these four lines with opt.Page++.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@wallrj Also taken, replaced resp.Links.CurrentPage() with a plain opt.Page++ counter initialized at Page: 1, so it's unconditionally bounded regardless of what prev/next the response does or doesn't set. In 18580a5.

return httptest.NewServer(mux)
}

func parsePage(s string) (int, error) {

@wallrj wallrj Aug 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

strconv.Atoi does this in one line and is stricter — Sscanf accepts leading whitespace/signs and ignores trailing garbage, so the fake server is quietly laxer than the real API. godo itself parses page numbers with strconv.Atoi.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@wallrj Switched to strconv.Atoi in 18580a5, matches how godo itself parses page numbers as you noted.

@wallrj

wallrj commented Aug 9, 2026

Copy link
Copy Markdown
Member

/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 Aug 9, 2026
Abmun added 2 commits August 9, 2026 13:57
- CleanUp now only deletes the TXT record whose value matches the
  challenge being cleaned up, instead of every record sharing its
  name. With pagination now surfacing records beyond page 1, the
  previous behaviour would delete a sibling challenge's record (e.g.
  concurrent example.com + *.example.com orders) out from under it.

- findTxtRecord now uses godo's server-side RecordsByTypeAndName
  filter instead of listing every TXT record in the zone and matching
  client side. This cuts requests against large zones down to
  ~1 per lookup and closes a race where an unsnapshotted, unfiltered
  paginated listing could skip a record if another challenge
  concurrently created/deleted records in the same zone while the
  loop was walking pages.

- Removed the resp == nil dead branch in the pagination loop (godo
  guarantees resp is non-nil whenever err is nil, so this arm could
  only ever silently truncate results were it ever reachable) and
  replaced reliance on Links.CurrentPage() (which pins at 2 forever
  if a page ever has next but no prev) with a simple incrementing
  page counter.

- Test fake DigitalOcean server now enforces the type+name filter
  server-side rather than returning every record for the client to
  filter, so the tests actually exercise production code paths.
  Replaced fmt.Sscanf-based page parsing with strconv.Atoi to match
  how godo itself parses page numbers.

- Added TestCleanUpOnlyDeletesMatchingValue and
  TestFindTxtRecordFiltersServerSide regression tests; adapted the
  pagination test to the server-side-filtered scenario (many records
  sharing one name) since a name filter makes cross-page misses on
  unrelated records impossible by construction.

Signed-off-by: Abmun <[email protected]>
CI flagged three issues in the new test helpers:

- pkg/issuer/acme/dns/digitalocean/digitalocean_test.go:354: for loop
  can use Go 1.22+ integer range (intrange). Switched
  'for i := 0; i < 25; i++' to 'for i := range 25'.

- newFakeDOServer's domain parameter and newTestProvider's zone
  parameter always received the same value at every call site
  (unparam), since every test in this file uses the same fake zone.
  Replaced both parameters with shared testDomain/testZone constants
  and dropped fakeDORecord.fullName's domain parameter for the same
  reason.

Signed-off-by: Abmun <[email protected]>
@Abmun
Abmun requested a review from wallrj August 10, 2026 04:25
@wallrj

wallrj commented Aug 10, 2026

Copy link
Copy Markdown
Member

Thanks @Abmun, the latest changes address all the review comments nicely — the value-filtered CleanUp with its regression test and the switch to the server-side name filter are exactly right, and CI is green.

/lgtm
/approve

with claude fable-5

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

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wallrj

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 Aug 10, 2026
@wallrj

wallrj commented Aug 10, 2026

Copy link
Copy Markdown
Member

/label tide/merge-method-squash

@cert-manager-prow cert-manager-prow Bot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Aug 10, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit 578dc72 into cert-manager:master Aug 10, 2026
6 checks passed
@Abmun
Abmun deleted the fix/digitalocean-txt-record-pagination-9099 branch August 10, 2026 15:18
@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: #9101 failed to apply on top of branch "release-1.21":

Applying: fix(digitalocean): paginate TXT record lookups in findTxtRecord
Using index info to reconstruct a base tree...
M	pkg/issuer/acme/dns/digitalocean/digitalocean.go
M	pkg/issuer/acme/dns/digitalocean/digitalocean_test.go
Falling back to patching base and 3-way merge...
Auto-merging pkg/issuer/acme/dns/digitalocean/digitalocean_test.go
CONFLICT (content): Merge conflict in pkg/issuer/acme/dns/digitalocean/digitalocean_test.go
Auto-merging pkg/issuer/acme/dns/digitalocean/digitalocean.go
CONFLICT (content): Merge conflict in pkg/issuer/acme/dns/digitalocean/digitalocean.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 fix(digitalocean): paginate TXT record lookups in findTxtRecord

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 mentioned this pull request Sep 1, 2026
39 of 41 tasks
wallrj pushed a commit to wallrj/cert-manager that referenced this pull request Sep 1, 2026
…-manager#9101)

* fix(digitalocean): paginate TXT record lookups in findTxtRecord

The DigitalOcean DNS-01 solver listed a zone's TXT records with nil
godo.ListOptions, so the DigitalOcean API silently capped the response
at its default page size of 20 and only the first page was ever
inspected. Challenge records are always the newest (highest-ID)
records in a zone, so any zone holding more than ~20 TXT records would
never match a challenge record: CleanUp deleted nothing and Present's
duplicate-record check never found an existing record, with no error
surfaced anywhere.

findTxtRecord now pages through the full result set using an explicit
per_page and a loop over Response.Links, so it always inspects every
TXT record in the zone regardless of size.

It also now takes the zone name as a parameter instead of re-resolving
it via DNS internally (Present/CleanUp already know it), which avoids
a redundant lookup and makes the function directly unit-testable
against a mocked DigitalOcean API.

Fixes cert-manager#9099

Signed-off-by: Abmun <[email protected]>

* fixup: address review feedback from @wallrj

- CleanUp now only deletes the TXT record whose value matches the
  challenge being cleaned up, instead of every record sharing its
  name. With pagination now surfacing records beyond page 1, the
  previous behaviour would delete a sibling challenge's record (e.g.
  concurrent example.com + *.example.com orders) out from under it.

- findTxtRecord now uses godo's server-side RecordsByTypeAndName
  filter instead of listing every TXT record in the zone and matching
  client side. This cuts requests against large zones down to
  ~1 per lookup and closes a race where an unsnapshotted, unfiltered
  paginated listing could skip a record if another challenge
  concurrently created/deleted records in the same zone while the
  loop was walking pages.

- Removed the resp == nil dead branch in the pagination loop (godo
  guarantees resp is non-nil whenever err is nil, so this arm could
  only ever silently truncate results were it ever reachable) and
  replaced reliance on Links.CurrentPage() (which pins at 2 forever
  if a page ever has next but no prev) with a simple incrementing
  page counter.

- Test fake DigitalOcean server now enforces the type+name filter
  server-side rather than returning every record for the client to
  filter, so the tests actually exercise production code paths.
  Replaced fmt.Sscanf-based page parsing with strconv.Atoi to match
  how godo itself parses page numbers.

- Added TestCleanUpOnlyDeletesMatchingValue and
  TestFindTxtRecordFiltersServerSide regression tests; adapted the
  pagination test to the server-side-filtered scenario (many records
  sharing one name) since a name filter makes cross-page misses on
  unrelated records impossible by construction.

Signed-off-by: Abmun <[email protected]>

* fixup: satisfy golangci-lint (intrange, unparam)

CI flagged three issues in the new test helpers:

- pkg/issuer/acme/dns/digitalocean/digitalocean_test.go:354: for loop
  can use Go 1.22+ integer range (intrange). Switched
  'for i := 0; i < 25; i++' to 'for i := range 25'.

- newFakeDOServer's domain parameter and newTestProvider's zone
  parameter always received the same value at every call site
  (unparam), since every test in this file uses the same fake zone.
  Replaced both parameters with shared testDomain/testZone constants
  and dropped fakeDORecord.fullName's domain parameter for the same
  reason.

Signed-off-by: Abmun <[email protected]>

---------

Signed-off-by: Abmun <[email protected]>
(cherry picked from commit 578dc72)
Signed-off-by: Richard Wall <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
@wallrj-cyberark wallrj-cyberark mentioned this pull request Oct 23, 2026
19 tasks done
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. area/acme/dns01 Indicates a PR modifies ACME DNS01 provider code area/acme Indicates a PR directly modifies the ACME Issuer code 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. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DigitalOcean DNS-01 solver never cleans up TXT records in zones with >20 TXT records (findTxtRecord does not paginate)

4 participants