Skip to content

registry: authorize docker/auth/** before reading the Docker Desktop session - #14542

Draft
Benehiko wants to merge 3 commits into
docker:mainfrom
Benehiko:registry/desktop-authorize
Draft

Benehiko wants to merge 3 commits into
docker:mainfrom
Benehiko:registry/desktop-authorize

Conversation

@Benehiko

@Benehiko Benehiko commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Warning

Draft. Depends on #14541, which bumps github.com/docker/secrets-engine/client to v1.0.0. Its commit is the first one here; review the last three. Tested against Docker Desktop on macOS with a binary that is not signed by Docker (see Docker Desktop test). Not yet tested with a Docker-signed build, on Linux or Windows, or with the E2E suite.

What this PR does

  • Calls Authorize on docker/auth/** before reading the Docker Desktop session, so Docker Desktop prompts at most once per process, and gives the user up to 1 minute to answer that prompt. Before this, every lookup from a binary not signed by Docker timed out after 5 seconds while the prompt was still open, and fell back to the Docker CLI credentials.
  • Gives Authorize that time only once the engine has answered a version request within 1 second, so an engine that hangs falls back after 1 second. Builds signed by Docker get no prompt, so for them Authorize answers without waiting on the user.

Related issue

No Compose issue. Depends on #14541.

Changes made

registry: authorize docker/auth/** before reading the Docker Desktop session:

  • Before the first session read, desktopSession calls Authorize(docker/auth/**). That pattern covers docker/auth/hub/**, docker/auth/metadata/hub/**, and their hub-staging counterparts, so one decision serves both the production and the staging session.
  • Authorize gets a 1 minute timeout (desktopAuthorizeTimeout), to leave time for the prompt. The session reads keep the 5 second desktopLookupTimeout, now applied through their contexts only: the client-wide WithTimeout(5s) is removed, since it would cut the prompt short.
  • The decision is kept until the expiry Docker Desktop returns; a zero expiry is kept for the life of the process.
  • A deny decision returns secrets.ErrAccessDenied and an Authorize error is wrapped; both fall back to the Docker CLI credentials and are not retried for retryInterval, like any other failed lookup.

registry: wait for the Docker Desktop prompt only when the secrets engine answers and registry: give the secrets engine version check 1 second:

  • The secrets engine API cannot say that it is waiting for the user: Authorize is a unary RPC and Decision is only DENY or ALLOW.
  • Before Authorize, desktopSession calls Version (desktopHub.Ping) within the 1 second desktopPingTimeout. Only if it answers does Authorize get the 1 minute desktopAuthorizeTimeout. Version is not gated by a prompt: it answered in 9.6 ms from a new process, against secrets engine v0.5.0 in Docker Desktop.
  • The ping runs once per authorization decision, not per lookup. A ping that fails, with a ConnectError or a timeout, falls back without calling Authorize or reading the session.
step timeout
Version 1 s
Authorize, after Version answered 1 min
session reads 5 s

Tests: the pattern sent is docker/auth/** and contains the four Hub realms; Ping calls Version; authorization and the ping happen once across session refetches; authorization happens again after the decision expires; a denial, an access-denied error, or an unanswered prompt falls back without reading the session; a failed ping falls back without authorizing; the ping gets at most 1 second and Authorize the longer timeout; the production timeouts are 1 s, 5 s, and 1 min.

Docker Desktop test

A throwaway program, not part of this PR, called registry.NewDesktopAuthProvider against the running Docker Desktop on macOS (darwin/arm64), with a fallback that marks the Docker CLI credentials. It looked up docker.io, index.docker.io, registry-1.docker.io, https://index.docker.io/v1/, dhi.io, registry-1-stage.docker.io, and ghcr.io, twice each. Tokens were never printed. The binary is built by go run, so it is not signed by Docker and Docker Desktop prompts for it.

#14541 alone this PR
Docker Hub addresses timeout after 5.0 s, Docker CLI credentials Docker Desktop session; 5.3 s, the prompt included
dhi.io Docker CLI credentials Docker Desktop session, ServerAddress https://dhi.io
registry-1-stage.docker.io timeout after 5.0 s no second prompt (25 ms); falls back because this account's staging session expired on 2026-10-07
ghcr.io Docker CLI credentials, Docker Desktop not contacted same
second round Docker CLI credentials, no new lookup all from cache, no new lookup

The same program, pointed at a temporary HOME so that it did not touch Docker Desktop:

Docker Hub lookup result
A fake engine socket that accepts connections and never answers 1.0 s warning, Docker CLI credentials; Authorize never called
No socket 0.8 ms debug log only, Docker CLI credentials
  • A process that looks up both Docker Hub and the staging registry against a hung engine waits 1 second for each; before this PR it waited 5 seconds for each.
  • Authorize returned an expiry about 11 hours ahead.
  • Docker Hub access tokens from Docker Desktop lasted 15 minutes.

Testing done

  • Unit tests pass for internal/registry, with -race
  • E2E tests pass: make e2e-compose or make e2e-compose-standalone
  • Linting passes: golangci-lint run ./internal/registry/... (not the full make lint)
  • Code formatted: golangci-lint fmt ./internal/registry/...
  • Manually tested (describe): against Docker Desktop on macOS with a binary not signed by Docker, and against a fake engine that never answers and a missing socket, see Docker Desktop test. Not tested with a Docker-signed build, or on Linux or Windows.

Open items

  • Is 1 minute right for desktopAuthorizeTimeout? It applies only after the engine has answered a version request, and builds signed by Docker get no prompt, but an engine that answers Version and then hangs on Authorize would still delay the first Docker Hub lookup by up to 1 minute.
  • Is docker/auth/** the realm to authorize? It also covers docker/auth/registry/**, which Compose does not read.
  • Secrets engine: a way for Authorize to report that it is waiting for the user, such as a streamed status or a pending decision, would let clients wait for the prompt and fail fast otherwise, instead of relying on Version.
  • Test with a Docker-signed build, which Docker Desktop does not prompt for, and on Linux and Windows.

AI tool used (if applicable)

AI Tool: alki, an AI coding agent, running Claude Opus 5.5. It wrote the code changes and the tests, ran the tests and the Docker Desktop test, and wrote this description.

@docker-agent docker-agent 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.

Assessment: 🟡 NEEDS ATTENTION

Lower-confidence findings (not posted inline)

  • [low] internal/registry/desktop.go:171 — s.authorized not reset to false after re-authorization fails (confidence: not verified, low severity)
  • [low] internal/registry/desktop_test.go:596 — TestDesktopAuthProvider_FallsBackWhenNotAuthorized does not assert pingCalls, leaving retry-on-denied behaviour partially untested (confidence: not verified, low severity)

validUntil time.Time
}

func (s *desktopSession) authorize() error {

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.

[medium] Ping timeout surfaces as context.DeadlineExceeded, which isSessionUnavailable doesn't recognise — emits a spurious Warn-level log instead of Debug

When the 1-second ping to the secrets engine times out, s.hub.Ping(pingCtx) returns context.DeadlineExceeded. authorize() wraps it and returns it to auth(), which passes it to logDesktopFallback(). isSessionUnavailable() only matches *seclient.ConnectError, dockerhub.ErrNoSession, and errSessionExpired — none of which match context.DeadlineExceeded — so logDesktopFallback emits a Warn-level message:

could not use the Docker Hub session from Docker Desktop, using Docker CLI credentials instead

instead of the quiet Debug-level message intended for "Desktop is not reachable". This is a user-visible regression: on any machine where the secrets engine is not running (most CI environments, non-Docker Desktop Linux), every Docker Hub credential lookup will log a warning. The existing test TestDesktopAuthProvider_FallsBackWhenTheEngineDoesNotAnswer already exercises context.DeadlineExceeded as a ping error, but doesn't assert the log level.

The fix is to extend isSessionUnavailable to also recognise deadline/timeout errors from the ping path:

func isSessionUnavailable(err error) bool {
	if _, ok := errors.AsType[*seclient.ConnectError](err); ok {
		return true
	}
	if errors.Is(err, context.DeadlineExceeded) {
		return true
	}
	return errors.Is(err, dockerhub.ErrNoSession) || errors.Is(err, errSessionExpired)
}
Confidence Score
🟢 strong 97/100

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Confirmed against the real client: a missing socket or a refused connection already returns a seclient.ConnectError and logs at Debug, but an engine that accepts the connection and never answers returned a bare rpc deadline_exceeded and logged at Warn. Fixed in cc2783f, though not by matching context.DeadlineExceeded in isSessionUnavailable, since that would also hide a timeout on the Desktop prompt and on the session lookup. Instead desktopHub.Ping wraps a version check timeout in a ConnectError with ReasonTimeout and the socket path, so it reads like the other unavailable cases and logs at Debug: checking the secrets engine: secrets engine did not respond in time at <socket>: deadline_exceeded: context deadline exceeded.

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.

Great fix — wrapping the timeout in ConnectError at the Ping boundary is cleaner than matching the bare error in isSessionUnavailable. That way you preserve specificity (timeout during ping vs during prompt/session lookup) while still letting isSessionUnavailable recognize it as unavailable. The resulting message is also more user-friendly and consistent with the other unavailability reasons.

Thanks for catching the nuance about where the deadline could come from — matching context.DeadlineExceeded globally would have swallowed timeouts that should be surfaced.

return fmt.Errorf("checking the secrets engine: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), s.authorizeTimeout)

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.

[medium] authorize() holds s.mu for up to 1 minute while awaiting a user prompt — all concurrent credential lookups are stalled

auth() takes s.mu and holds it for the entire fetch() → authorize() call chain. authorize() creates its Authorize context from context.Background() with a 1-minute ceiling, so it cannot be cancelled by a caller context (e.g., Ctrl+C / SIGINT). If the Desktop prompt is not answered, any concurrent goroutine calling auth() on the same desktopSession (e.g., parallel pulls of multiple images) will block for up to 1 minute.

This is an intentional trade-off (the user needs time to respond to the prompt) and is acknowledged in the PR description as an open item. However, it means that a Docker Compose run with many concurrent pulls can appear completely frozen while one goroutine is waiting for the prompt.

A partial mitigation is to call Authorize before taking the lock (or in a one-shot goroutine guarded by sync.Once) and to store only the result under s.mu. That way concurrent callers either fast-return with the cached decision or wait on the same Authorize call without holding a mutex the whole time.

Confidence Score
🟢 strong 97/100

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Leaving this as is for now. Holding s.mu across the prompt is what makes concurrent pulls share one Authorize call: calling Authorize outside the lock would show the user one prompt per pull, and a sync.Once around it would still make every caller wait for the same answer, just without the mutex. The part that does hold is cancellation: the prompt wait starts from context.Background(), so Ctrl+C cannot cut it short. Fixing that needs a context on AuthProvider.GetAuthConfig, which is a wider interface change than this PR, so it stays listed as an open item in the description.

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.

You're absolutely right — I misunderstood the trade-off. Holding the mutex across the prompt is what makes concurrent pulls share a single authorization instead of bombarding the user with N prompts. Moving Authorize outside the lock would indeed be worse (multiple prompts), and sync.Once would still block all callers, just without the mutex doing useful work.

The real issue is the uncancellable context.Background() in authorize(), as you've identified. That's what prevents Ctrl+C from unblocking stalled pulls. Threading a cancellable context through AuthProvider.GetAuthConfig is the right fix, and I agree it's too broad for this PR. Thanks for the clarification.

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…session

Docker Desktop can prompt the user before it gives a process access to a realm, and reading the default Docker Hub session reads from two realms, docker/auth/metadata/hub/** and docker/auth/hub/**. The 5 second lookup timeout expired before the user could answer, so the lookup fell back to the Docker CLI credentials.

Call Authorize once on docker/auth/**, which covers the production and staging realms, with a one minute timeout, and keep the decision until the expiry Docker Desktop returns. The session reads keep their 5 second timeout. A denied or unanswered authorization falls back to the Docker CLI credentials like any other failed lookup.

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
@Benehiko
Benehiko force-pushed the registry/desktop-authorize branch from bb8956d to e0d0f44 Compare November 25, 2026 11:46
…gine answers

Authorize has a one minute timeout, so the user can answer the Docker Desktop prompt. An engine that accepts the connection but never answers looked the same, and delayed the first Docker Hub lookup by a minute.

Ask the engine for its version first, within the 5 second lookup timeout, and only then call Authorize. The secrets engine API has no way to say that it is waiting for the user, but the version request is not gated by a prompt: it answered in about 10 ms from a new process. An engine that does not answer now falls back after 5 seconds, as before.

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
The version request that comes before Authorize is not gated by a prompt and answered in about 10 ms from Docker Desktop, so it does not need the 5 second lookup timeout. With 1 second, an engine that accepts the connection but does not answer falls back to the Docker CLI credentials after 1 second instead of 5. The session reads keep 5 seconds and Authorize keeps 1 minute.

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>

# Conflicts:
#	internal/registry/desktop.go
@Benehiko
Benehiko force-pushed the registry/desktop-authorize branch from e0d0f44 to bd3e8b7 Compare November 25, 2026 12:22
@Benehiko

Copy link
Copy Markdown
Member Author

On the two lower-confidence findings from the docker-agent review, both addressed in cc2783f: authorize() now clears s.authorized and s.authorizedUntil before asking again once the decision has expired (it was already re-checked through authorizedUntil, so this only makes the state explicit), and TestDesktopAuthProvider_FallsBackWhenNotAuthorized now asserts pingCalls.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants