Skip to content

Extract DCR resolver into pkg/auth/dcr#5198

Draft
tgrunnagle wants to merge 2 commits intodcr-3c_issue_5185from
dcr-4a_issue_5145
Draft

Extract DCR resolver into pkg/auth/dcr#5198
tgrunnagle wants to merge 2 commits intodcr-3c_issue_5185from
dcr-4a_issue_5145

Conversation

@tgrunnagle
Copy link
Copy Markdown
Contributor

@tgrunnagle tgrunnagle commented May 5, 2026

DRAFT - not ready for review

Summary

  • Why: Issue Consolidate the two RFC 7591 DCR client implementations into pkg/auth/dcr #5145 calls for consolidating two parallel RFC 7591 Dynamic Client Registration (DCR) implementations (pkg/authserver/runner/dcr.go and pkg/auth/discovery::PerformOAuthFlow) into a single shared pkg/auth/dcr package, so review-derived behaviours (S256 PKCE gate, RFC 7591 §3.2.1 expiry refetch, redirect refusal, panic recovery, singleflight) cannot diverge between the embedded authserver and the CLI flow as future consumers land. The full migration is too large for a single PR, so this slice (sub-issue 4a) does the package extraction and migrates the embedded authserver only.
  • What: Extracts the DCR resolver and credential store from pkg/authserver/runner into a new pkg/auth/dcr package and migrates EmbeddedAuthServer to consume it. File moves use git mv so the diff renders as renames rather than delete+add. Public symbols are renamed to drop the redundant DCR prefix on packagisation (e.g., DCRResolutiondcr.Resolution, DCRCredentialStoredcr.CredentialStore, resolveDCRCredentialsdcr.ResolveCredentials). Adds a parallel TestResolveSecret suite mirroring the runner-package twin so any future drift between the two resolveSecret copies fails CI.

Part of #5145 — does not close it. Acceptance criteria #1, #3 and #4 still depend on sub-issue 4b (CLI flow migration in pkg/auth/discovery::PerformOAuthFlow) before the issue can close.

Stacking

This branch is stacked on PR #5196 (dcr-3c_issue_5185 — "Wire persistent DCRCredentialStore into EmbeddedAuthServer"), which is itself stacked on PRs #5186 and #5195. The earlier commits on this branch (c0fed523 through 781a0b97) implement sub-issues #5183 / #5184 / #5185 (the persistent DCR store and Redis backend) and are shown for context only — review against PR #5196 once it merges, or review the two commits unique to this branch in isolation:

  • df84256f Extract DCR resolver into pkg/auth/dcr
  • 633ed9ed Address code review feedback

Type of change

  • Refactoring (no behavior change)

Test plan

  • Unit tests (task test) — go test -count=1 -race ./pkg/auth/dcr/... ./pkg/authserver/... all pass
  • Linting (task lint-fix) — 0 issues
  • License check (task license-check) — clean

Changes

File Change
pkg/auth/dcr/resolver.go Renamed from pkg/authserver/runner/dcr.go; public symbols renamed to drop the DCR prefix; package doc updated with a # Concurrency section documenting the process-global singleflight; new flightKeyOf(Key) helper makes the canonical key shape inspectable; endpointsFromMetadata gains a defensive nil check and an oauthproto contract note.
pkg/auth/dcr/store.go Renamed from pkg/authserver/runner/dcr_store.go; constructors renamed (NewInMemoryStore, NewStorageBackedStore); type renamed to CredentialStore.
pkg/auth/dcr/resolver_test.go Renamed from pkg/authserver/runner/dcr_test.go; updated for new package boundary and renamed symbols.
pkg/auth/dcr/store_test.go Renamed from pkg/authserver/runner/dcr_store_test.go; updated for new package boundary.
pkg/auth/dcr/secret_test.go New parallel resolveSecret suite that mirrors the runner-package twin's observable contract so future drift between the two duplicated helpers fails CI on at least one side.
pkg/authserver/runner/embeddedauthserver.go Migrated to consume pkg/auth/dcr; doc comments on buildUpstreamConfigs and DCRStore() updated to use the new public names.
pkg/authserver/runner/embeddedauthserver_test.go Updated import paths and constructor names for the moved types.

Does this introduce a user-facing change?

No. This is an internal refactor. The embedded authserver's externally-observable DCR behaviour (registration, caching, error reporting, structured logging, expiry refetch, S256 gating) is unchanged.

Implementation plan

Approved implementation plan

Sub-issue 4a of #5145 — extract the resolver into pkg/auth/dcr and migrate the embedded authserver only. Defer the pkg/auth/discovery::PerformOAuthFlow migration to sub-issue 4b so this slice stays under the 400-line / 10-file PR-size limit.

Profile-neutral input shape: the package's API still takes embedded-authserver types directly. Designing a profile-neutral input type with only one consumer in hand would be speculative; the right design moment is when 4b lands the CLI flow as the second consumer. The package doc comment in pkg/auth/dcr/resolver.go calls this coupling out explicitly and labels the profile-agnostic framing as a target state for 4b rather than the current API shape.

Duplication of resolveSecret: deliberately duplicated across pkg/authserver/runner and pkg/auth/dcr because the DCR package must stay reachable-from-runner-but-not-back. Promoting to a shared helper is deferred until 4b lands a third call site. Drift between the two copies is guarded by parallel test suites with identical observable contracts.

Special notes for reviewers

  • Why renames, not delete+add: file moves use git mv so the diff renders as renames. The percentage similarity in git log --stat --follow reflects that the bulk of the move is mechanical and the substantive deltas are confined to (a) the package doc rewrite at the top of resolver.go, (b) the flightKeyOf extraction and singleflight comment, (c) the endpointsFromMetadata nil guard, and (d) the import/symbol rename sweep in embeddedauthserver.go.
  • Singleflight is process-global, not per-instance: documented at the package surface and on the dcrFlight var. The cross-consumer caveat ("third consumer with colliding redirect URI" failure mode) is named explicitly so the next migration cannot silently re-introduce a collision.
  • Acceptance criteria status against Consolidate the two RFC 7591 DCR client implementations into pkg/auth/dcr #5145 (as per the commit message of df84256f):
    • AC#1 (pkg/auth/dcr exists, exports a stateful resolver, consumed by both call sites): partially met — package exists and is consumed by EmbeddedAuthServer. CLI flow migration is sub-issue 4b.
    • AC#2 (stateless RFC 7591 helpers in pkg/oauthproto): met (already satisfied before this PR).
    • AC#3 (no direct oauthproto.RegisterClientDynamically calls outside pkg/auth/dcr): not yet met — pkg/auth/discovery still calls it. Resolved by sub-issue 4b.
    • AC#4 (review-property behaviours apply to CLI flow): not yet met — same dependency on 4b.

tgrunnagle added 2 commits May 5, 2026 11:24
Sub-issue 4a of #5145. Creates the shared pkg/auth/dcr package and
migrates the embedded authserver to consume it. The CLI flow migration
(pkg/auth/discovery::PerformOAuthFlow) is left to a follow-up so this
slice stays under the project's PR-size limit.

Files moved (renamed via git mv so the diff shows as renames, not
deletions + additions):

  pkg/authserver/runner/dcr.go         -> pkg/auth/dcr/resolver.go
  pkg/authserver/runner/dcr_store.go   -> pkg/auth/dcr/store.go
  pkg/authserver/runner/dcr_test.go    -> pkg/auth/dcr/resolver_test.go
  pkg/authserver/runner/dcr_store_test.go -> pkg/auth/dcr/store_test.go

Public API surface (renamed from runner-package internals because they
now cross a package boundary):

  Resolution            (was DCRResolution)
  Key                   (was DCRKey, alias to storage.DCRKey unchanged)
  CredentialStore       (was DCRCredentialStore)
  ResolveCredentials    (was resolveDCRCredentials)
  NeedsDCR              (was needsDCR)
  ConsumeResolution     (was consumeResolution)
  ApplyResolutionToOAuth2Config (was applyResolutionToOAuth2Config)
  LogStepError          (was logDCRStepError)
  NewInMemoryStore      (was NewInMemoryDCRCredentialStore)
  NewStorageBackedStore (was newStorageBackedStore)

Names follow the pkg/auth/dcr.* form so the linter's stuttering check
(revive: dcr.DCRResolution would stutter) passes and the surface reads
as ordinary package API.

resolveSecret was duplicated into the dcr package because pkg/auth/dcr
must stay profile-agnostic and cannot reach back into pkg/authserver/
runner. The duplication is intentional and called out in a comment;
future consolidation can move it into a shared helper if a third
caller appears.

Acceptance criteria status (#5145):

  AC#1 (pkg/auth/dcr exists, exports a stateful resolver, consumed by
       both call sites): partially met — package exists and is consumed
       by EmbeddedAuthServer. CLI flow migration is sub-issue 4b.
  AC#2 (stateless RFC 7591 helpers in pkg/oauthproto): met (already
       satisfied before this PR).
  AC#3 (no direct oauthproto.RegisterClientDynamically calls outside
       pkg/auth/dcr): not yet met — pkg/auth/discovery still calls it.
       Resolved by sub-issue 4b.
  AC#4 (review-property behaviours apply to CLI flow): not yet met —
       same dependency on 4b.

Verified via task lint-fix (0 issues), go test -race ./pkg/auth/dcr/...
./pkg/authserver/... (all pass), and task license-check (clean).
Fixed issues from review of df84256 (Extract DCR resolver into
pkg/auth/dcr) for issue #5145 sub-issue 4a:

- HIGH: Stale identifier references in buildUpstreamConfigs doc comment
  in pkg/authserver/runner/embeddedauthserver.go named the pre-rename
  symbols (consumeResolution, applyResolutionToOAuth2Config,
  resolveDCRCredentials, logDCRStepError). Updated the comment to use
  the new public names (dcr.ConsumeResolution,
  dcr.ApplyResolutionToOAuth2Config, dcr.ResolveCredentials,
  dcr.LogStepError). Re-grepped the runner package; no other survivors.

- HIGH: pkg/auth/dcr's package doc claimed "profile-agnostic" while the
  public API takes embedded-authserver types directly. Revised the
  package doc comment in pkg/auth/dcr/resolver.go to call out the
  current coupling explicitly and labelled the profile-agnostic framing
  as a target state for sub-issue 4b rather than the current API shape.
  Designing a profile-neutral input type with only one consumer in hand
  would be speculative; the right design moment is when 4b lands the
  CLI flow as the second consumer.

- MEDIUM: DCRStore() accessor doc comment in
  pkg/authserver/runner/embeddedauthserver.go referenced the dropped
  "runner-DCRCredentialStore adapter" name. Replaced with
  "dcr.CredentialStore adapter".

- MEDIUM: resolveSecret is duplicated across pkg/authserver/runner
  and pkg/auth/dcr. Added a parallel TestResolveSecret /
  TestResolveSecretWithEnvVar suite in pkg/auth/dcr/secret_test.go
  that mirrors the runner-package twin's observable contract, so any
  future drift between the two copies will fail one of the two tests
  at CI time. Lifting into a shared helper is deferred until 4b lands
  a third call site.

- MEDIUM: Process-global singleflight was undocumented at the package
  surface. Added a "Concurrency" section to the package doc comment
  in pkg/auth/dcr/resolver.go. Extended the dcrFlight var doc with a
  cross-consumer caveat that names the "third consumer with colliding
  redirect URI" failure mode. Extracted the flight-key construction
  into a new flightKeyOf(Key) string helper so the canonical key shape
  is inspectable rather than buried in a string-concatenation literal.

- MEDIUM: endpointsFromMetadata dereferenced metadata without a
  defensive nil check. Added the guard immediately after the fetchErr
  check in pkg/auth/dcr/resolver.go::endpointsFromMetadata with an
  error message that names the cross-package contract being defended.
  Documented the oauthproto contract on the function's doc comment so
  the assumption is visible at the call site.

Verified via task lint-fix (0 issues), task license-check (clean),
and go test -count=1 -race ./pkg/auth/dcr/... ./pkg/authserver/...
(all pass).
@github-actions github-actions Bot added size/L Large PR: 600-999 lines changed and removed size/L Large PR: 600-999 lines changed labels May 5, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

❌ Patch coverage is 91.30435% with 4 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (dcr-3c_issue_5185@781a0b9). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pkg/auth/dcr/resolver.go 87.09% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@                 Coverage Diff                  @@
##             dcr-3c_issue_5185    #5198   +/-   ##
====================================================
  Coverage                     ?   67.60%           
====================================================
  Files                        ?      601           
  Lines                        ?    61436           
  Branches                     ?        0           
====================================================
  Hits                         ?    41535           
  Misses                       ?    16771           
  Partials                     ?     3130           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

size/L Large PR: 600-999 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant