Repository navigation
fix(digitalocean): paginate TXT record lookups in findTxtRecord - #9101
Conversation
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]>
|
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 Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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. |
There was a problem hiding this comment.
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
findTxtRecordto acceptzoneNameand to iterate through all paginated TXT record results from the DigitalOcean API. - Introduce an explicit
per_page(viagodo.ListOptions) to avoid the API’s default page size of 20 and reduce request count for large zones. - Add unit tests using an
httptest.Serverthat 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
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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() { |
There was a problem hiding this comment.
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.
| if resp == nil || resp.Links == nil || resp.Links.IsLastPage() { | |
| if resp.Links == nil || resp.Links.IsLastPage() { |
IsLastPage already handles nil Pages.
There was a problem hiding this comment.
Took your suggestion, thanks. Removed the resp == nil check. Applied in 18580a5.
| break | ||
| } | ||
|
|
||
| page, err := resp.Links.CurrentPage() |
There was a problem hiding this comment.
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++.
| return httptest.NewServer(mux) | ||
| } | ||
|
|
||
| func parsePage(s string) (int, error) { |
There was a problem hiding this comment.
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.
|
/ok-to-test |
- 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]>
|
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 with claude fable-5 |
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/label tide/merge-method-squash |
|
/cherry-pick release-1.21 with claude fable-5 |
|
@wallrj: #9101 failed to apply on top of branch "release-1.21": DetailsIn response to this:
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. |
…-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]>
Pull Request Motivation
Fixes #9099.
The DigitalOcean DNS-01 solver's
findTxtRecordlists a zone's TXTrecords with
nilgodo.ListOptions, so the DigitalOcean API silentlycaps 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:
CleanUpdeletes nothing andPresent's duplicate-record check never finds an existing record — withno error surfaced anywhere. This is the long-standing root cause of #1661.
findTxtRecordnow pages through the full result set using an explicitper_pageand a loop overResponse.Links, so it always inspects everyTXT 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/CleanUpalready know it), whichavoids a redundant lookup and makes the function directly unit-testable
against a mocked DigitalOcean API.
Testing: added
TestFindTxtRecordPaginationandTestFindTxtRecordPaginationNoMatch, which spin up anhttptest.Serversimulating 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
findTxtRecordstill finds/doesn't find recordscorrectly across all pages.
Kind
/kind bug