Skip to content

WIP: use the secrets engine session for Docker Hub and dhi.io credentials - #7431

Draft
Benehiko wants to merge 4 commits into
docker:masterfrom
Benehiko:secrets-engine-desktop-auth
Draft

Benehiko wants to merge 4 commits into
docker:masterfrom
Benehiko:secrets-engine-desktop-auth

Conversation

@Benehiko

@Benehiko Benehiko commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Warning

Work in progress. See Open items for what is left.

Note

This PR was created with alki, an AI coding agent, running Claude Opus 5.5: the code, the tests, the benchmarks, and this description.

Summary

When a Docker secrets engine is running and holds a Docker Hub session, use that session as the registry credentials for Docker Hub and Docker Hardened Images, as docker/compose does. Signing in to Docker Desktop stores this session.

  • A new internal/secretsengine package reads the default Docker Hub session through github.com/docker/secrets-engine/client v1.1.0. secretsengine.New() returns a *Provider with AuthConfig(ctx, registryHostname) (types.AuthConfig, bool).

  • internal/registryauth.Encoded (added in cli/command: look up registry credentials through internal/registryauth #7572) asks for credentials in this order:

    1. DOCKER_AUTH_CONFIG, for the registries it defines.
    2. The secrets engine session.
    3. The credential store, credential helpers, and configuration file, as before.

    This covers docker pull, push, run, create, plugin install, upgrade and push, service create and update, and stack deploy.

  • Only the docker binary turns the lookup on, with registryauth.UseSecretsEngine() in dockerMain. Unit tests, test.FakeCli, command.RetrieveAuthTokenFromImage, and any other user of the package never contact a secrets engine. DockerCli is not changed.

  • Choosing the engine: the secrets engine of Docker Desktop is used when its socket exists, and the standalone secrets engine otherwise. Only one of them is contacted. A missing socket is detected with one os.Lstat, without dialing. On Linux the standalone engine listens on an abstract socket, which cannot be checked without dialing, so it is dialed directly.

  • Registries:

    • docker.io, index.docker.io, and registry-1.docker.io use the Docker Hub session.
    • dhi.io uses the Docker Hub session, with ServerAddress set to dhi.io.
    • registry-1-stage.docker.io uses the staging session.
    • Every other registry never contacts the secrets engine.
  • docker login, docker logout, and stored credentials are not changed. A session token is never written to the credential store.

Authorizing with the secrets engine

Docker Desktop prompts the user before it gives a binary that is not signed by Docker, such as a local build or a test binary, access to the Docker Hub session. Docker-signed binaries get no prompt. Following docker/compose#14542, before the first session read the CLI:

step timeout
asks the engine for its version (Version) 1 s
calls Authorize on docker/auth/**, once Version has answered 1 min
reads the session, and the profile if the session has no username 5 s
  • Every timeout applies to the context of the command, so one Ctrl+C stops the wait for the prompt and the command exits.
  • Version is not gated by a prompt, so it tells an engine that answers from one that hangs. Authorize gets the time for the prompt only once Version has answered, so an engine that accepts connections and never answers is given up on after 1 second.
  • docker/auth/** covers docker/auth/hub/**, docker/auth/metadata/hub/**, and their hub-staging counterparts.
  • The decision is kept until the expiry the engine returns; a zero expiry is kept for the life of the process. An expired decision is cleared, and Version and Authorize run again.
  • A Version timeout is reported as a seclient.ConnectError with ReasonTimeout and the socket path, so it is logged at debug level like a missing socket.

Falling back to the CLI's own credentials

  • At debug level: no socket, nothing listening, an engine that does not answer Version within 1 second, signed out, an expired session, or a canceled command.
  • With a warning: a denial, an access-denied error, an unanswered prompt, a session read that does not answer within 5 seconds, or a session without a username.
  • After a failed lookup, the secrets engine is not asked again for a minute. A session is reused until 30 seconds before it expires, and for one minute if it has no expiry.

Tests

  • The unit tests use the real SDK client with seclient.WithHTTPClient. Its transport is a fake engine that answers the Connect requests (GetVersion, Authorize, GetSecrets) in memory, with the same protobuf bodies and error JSON as the real engine. No socket is created and no request leaves the process.
  • Session expiry, retry, timeouts, cancellation, and the expiry of the authorization decision are tested with testing/synctest and the production durations.
  • The choice of socket is tested with HOME, XDG_CACHE_HOME, and LOCALAPPDATA pointed at a temporary directory.

Commits

  1. cli/command: look up registry credentials through internal/registryauth: cli/command: look up registry credentials through internal/registryauth #7572, no behaviour change.
  2. vendor: github.com/docker/secrets-engine/client v1.1.0: the client module requires go1.26.11, which raises the go directive in vendor.mod from 1.26.0.
  3. internal/secretsengine: read the Docker Hub session from the secrets engine
  4. internal/registryauth: use the secrets engine session for Docker Hub

Latency

Important

These measurements were taken on an earlier revision: client v0.2.2, the Docker Desktop socket only, and no Version or Authorize request. The row for an engine that never answers is now expected to be about 1 s instead of 5 s. They have not been repeated on the current revision.

Extra time per docker pull of a Docker Hub or dhi.io image (p50, this branch minus master), and the cost of one lookup against the engine:

Linux Windows macOS
No Docker Desktop (no socket) +0.05 to +0.14 ms +0.48 to +0.99 ms¹ +0.24 to +0.25 ms
Docker Desktop not running (nothing listening) +0.20 to +0.26 ms +1.15 to +1.24 ms¹ +0.04 to +0.67 ms
Engine running, signed out +15.2 to +15.4 ms³ +1.2 to +1.6 ms +3.8 to +4.2 ms
Engine running, signed in +15.3 to +16.6 ms³ +1.8 to +2.4 ms +3.7 to +4.5 ms
Engine adds 50 ms per request +197 to +200 ms² +153 to +177 ms² +99 to +104 ms
DOCKER_AUTH_CONFIG set for Docker Hub +0.26 ms +0.21 ms −0.13 ms
Engine accepts but never answers +5.01 s +5.04 s +5.00 s
docker --version (p50, before → after) 5.11 → 5.24 ms 28.33 → 28.60 ms 16.17 → 17.10 ms
One lookup, new connection to the engine 6.95 ms 0.68 ms 2.68 ms
One lookup, reused connection 0.19 ms 0.21 to 0.37 ms 0.12 ms

¹ ghcr.io, which never contacts the engine, differs by +0.58 to +0.79 ms in the same runs.
² A lookup made two requests to the engine when this was measured; it now makes four on the first lookup of a process (Version, Authorize, profile, session). The VMs oversleep: a fake engine that sleeps 10 ms per request takes 44.7 ms per lookup on Linux and 62.0 ms on Windows, against 22.8 ms on macOS.
³ The cost is in the engine, not the CLI. On Linux the engine verifies the binary of every connecting process, and the cost grows with the size of that binary. Signed out costs the same as signed in.

Every case sent the expected credentials: the Docker Desktop session for Docker Hub and dhi.io, DOCKER_AUTH_CONFIG over Docker Desktop, and the configuration file everywhere else.

How this was measured:

  • master and the branch were built with CGO_ENABLED=0.
  • The engine is a standalone secrets engine built on the engine runtime, with a seeded Docker Hub session. A fake Docker daemon on 127.0.0.1 recorded the X-Registry-Auth of every pull.
  • 30 measured runs per case, 3 for the engine that never answers. HOME, the Docker config, and the Docker Desktop socket path were isolated in a temporary directory.
  • Linux: Ubuntu 24.04.4 arm64 VM, 4 vCPUs. Windows: Windows 11 (26100) arm64 VM, 4 vCPUs. macOS: an Apple M4 Max host, since a macOS VM needs interactive provisioning.
  • The rows for one lookup come from BenchmarkExternalEngine, against the same engine. It was used for these measurements and is not part of this PR.

Binary size

docker built from master and from the branch with -tags grpcnotrace and CGO_ENABLED=0, both with Go 1.26.11; stripped means -ldflags '-s -w'. 1 MB = 1,000,000 bytes. Measured with client v1.0.0, before the rebase onto the current master; v1.1.0 adds only WithHTTPClient.

platform unstripped stripped
linux/arm64 39.90 → 40.29 MB (+0.39 MB, +1.0%) 27.66 → 27.92 MB (+0.26 MB, +0.9%)
linux/amd64 42.54 → 42.93 MB (+0.39 MB, +0.9%) 29.70 → 29.97 MB (+0.27 MB, +0.9%)
darwin/arm64 41.53 → 41.96 MB (+0.43 MB, +1.0%) 28.58 → 28.86 MB (+0.29 MB, +1.0%)
windows/amd64 43.30 → 43.70 MB (+0.40 MB, +0.9%) 30.60 → 30.87 MB (+0.27 MB, +0.9%)
  • Newly linked modules: github.com/docker/secrets-engine/client and github.com/docker/secrets-engine/secrets. Protobuf and net/http were already linked.
  • The added symbols total about 0.09 MB. The largest are client/dockerhub (0.023 MB) and the plugin management messages in client/api/plugins/v1 (0.013 MB), which the CLI never calls.
  • With client v0.2.2, which linked connectrpc.com/connect, github.com/docker/secrets-engine/x, and golang.org/x/mod, the increase was +1.27 MB unstripped and +0.85 MB stripped on linux/arm64. v1.0.0 drops all three.
  • client: read-only client without plugin management or connect (client v0.2.2 adds 1.3 MB to the Docker CLI) secrets-engine#709 proposes a read-only client without plugin management. Prototypes of that client brought the increase on linux/arm64 to +0.20 MB unstripped and +0.13 MB stripped.
  • Release builds also use cgo and -buildmode=pie on Linux and macOS, which these builds did not.

Open items

  • Decide whether the remaining size is acceptable, or reduce it further through client: read-only client without plugin management or connect (client v0.2.2 adds 1.3 MB to the Docker CLI) secrets-engine#709 by dropping plugin management from the client.
  • Bump Go to 1.26.11 in the Dockerfiles, .golangci.yml, and the GitHub workflows, which the client requires.
  • Ctrl+C while the prompt is open: the lookup now takes the context of the command, and one Ctrl+C stops the wait.
  • DockerCli.RegistryAuthProvider: removed, and only dockerMain turns the lookup on.
  • command.RetrieveAuthTokenFromImage wraps registryauth.Encoded (cli/command: look up registry credentials through internal/registryauth #7572), so cli/command now imports internal/secretsengine through registryauth, and projects that import cli/command build the secrets engine client too. It never contacts the engine for them. If that dependency should be avoided, cmd/docker could register the lookup with registryauth instead of registryauth importing internal/secretsengine.
  • Engine-side: reduce the per-connection cost on Linux (+15 ms per docker pull).
  • Test the Authorize prompt against Docker Desktop: with a binary not signed by Docker, with a Docker-signed binary (no prompt expected), and on Linux and Windows.
  • Does the standalone secrets engine hold a Docker Hub session under docker/auth/hub/**, or only Docker Desktop? This decides whether falling back to the standalone socket is useful.
  • Is 1 minute right for the prompt? It applies only after Version has answered, and Ctrl+C now stops it.
  • Is docker/auth/** the realm to authorize? It also covers docker/auth/registry/**, which the CLI does not read.
  • The Docker Hub and staging sessions authorize separately, so a process that pulls from both could be prompted twice.
  • No unit test shows that registryauth puts the secrets engine before the configuration file. It needs the real engine or a test-only hook, and the order is three lines in lookup.
  • Measure with a Docker-signed binary against Docker Desktop, and on macOS in a VM.
  • Repeat the latency and size measurements on the current revision.
  • Windows: in one benchmark run against the engine, a lookup on a reused connection failed with wsarecv: The I/O operation has been aborted and fell back to the CLI's credentials. This was not reproduced in 10 further runs, and no docker pull failed.
  • Decide whether access denied from the secrets engine should warn on every pull (as docker/compose does) or log at debug level.

Release notes (optional)

Use the Docker Hub session from the Docker secrets engine (Docker Desktop or the standalone engine) for Docker Hub and Docker Hardened Images credentials. `DOCKER_AUTH_CONFIG` still takes precedence.

@codecov-commenter

codecov-commenter commented Oct 22, 2026 •

Copy link
Copy Markdown

@Benehiko
Benehiko force-pushed the secrets-engine-desktop-auth branch from 7c99af5 to a4adc90 Compare October 22, 2026 09:23

@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: 🟢 APPROVE

Comment thread internal/desktopauth/desktopauth.go Outdated
s.hub = hub
}

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

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.

[low] Shared 5-second context split across two sequential RPC calls

fetch() creates a single context.WithTimeout(context.Background(), s.timeout) (5 s) and passes it to both GetDefaultSession and — when userSession.Claims.Username is empty — a subsequent GetDefaultProfile call. If the first call consumes the majority of the budget the second call gets only the remainder and may time out immediately, causing a spurious failure and a 1-minute retry backoff.

In practice the benchmarks show lookups take 2–15 ms (far below 5 s), so both calls complete well within the budget under normal conditions. The consequence of a timeout is also graceful: auth() sets retryAt and falls back to the CLI's own credentials. Consider using two separate timeouts (one per call) to make the intent explicit and to avoid a theoretical starvation when the engine is under load.

Confidence Score
🟡 moderate 62/100


userSession, err := s.hub.GetDefaultSession(ctx)
if err != nil {
return types.AuthConfig{}, time.Time{}, err

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.

[low] Stale s.hub client is never reset after a connection failure

s.hub is set once in fetch() and never cleared when GetDefaultSession returns an error. After the 1-minute retryAt window elapses, fetch() skips the if s.hub == nil { s.connect() } branch and retries on the same client object.

For the common case of a Docker Desktop restart at the same socket path, the existing HTTP/1.1 keep-alive transport will auto-redial and the retry succeeds. However, if the socket path were to change (e.g., Docker Desktop is reinstalled at a different path), the stale transport continues dialling the old path and retries will keep failing until the process restarts.

Resetting s.hub = nil on the error path inside auth() (or inside fetch() when a *seclient.ConnectError is returned) would guarantee a clean reconnect on every retry:

Suggested change
return types.AuthConfig{}, time.Time{}, err
userSession, err := s.hub.GetDefaultSession(ctx)
if err != nil {
s.hub = nil
return types.AuthConfig{}, time.Time{}, err
}
Confidence Score
🟡 moderate 62/100

for _, item := range resp.Msg.GetEnvelopes() {
id, err := secrets.ParseID(item.GetId())
if err != nil {
continue

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.

[low] ParseID errors are silently swallowed in resolverClient.GetSecrets

When the server returns envelopes whose IDs cannot be parsed by secrets.ParseID, the loop continues without logging or propagating the error. If every envelope in the response has an unparseable ID, the function returns (nil, nil) — an empty slice with no error — instead of surfacing the failure.

Callers in desktopauth treat an empty/nil result as "no session found" and fall through to the next credential source, hiding a potential protocol mismatch between the client library and the secrets engine.

In normal operation the server serializes IDs that were previously validated, so this path should not trigger. However, a format change or server/client version mismatch would silently degrade rather than report an actionable error. Consider logging at debug level when an ID cannot be parsed, or returning a partial-result error.

Confidence Score
🟡 moderate 62/100

@Benehiko
Benehiko force-pushed the secrets-engine-desktop-auth branch from a4adc90 to 5e1a655 Compare November 20, 2026 13:04
@Benehiko
Benehiko force-pushed the secrets-engine-desktop-auth branch 4 times, most recently from d5dd509 to 3195b92 Compare December 4, 2026 09:59
@Benehiko Benehiko changed the title WIP: use the Docker Desktop session for Docker Hub and dhi.io credentials WIP: use the secrets engine session for Docker Hub and dhi.io credentials Dec 4, 2026
@Benehiko
Benehiko force-pushed the secrets-engine-desktop-auth branch from 3195b92 to 27fa2f0 Compare December 4, 2026 13:01
Comment thread vendor.mod
// around SemVer, which this repo cannot abide by as it uses CalVer.

go 1.26.0
go 1.26.11

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.

Can we drop patch versions from the module? We really shouldn't have them.

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.

@Benehiko
Benehiko force-pushed the secrets-engine-desktop-auth branch 4 times, most recently from 7a99cad to 7257c2b Compare December 4, 2026 14:41
Move the lookup of the encoded registry credentials for an image into the new internal/registryauth package, and use it where commands send credentials to the daemon: docker pull, push, run, create, plugin install, upgrade and push, service create and update, and stack deploy. command.RetrieveAuthTokenFromImage is kept for compatibility, as a wrapper around registryauth.Encoded, and its tests move to the new package.

registryauth.Encoded takes the context of the command, so that a lookup that needs to wait, such as one that asks another process for credentials, can be canceled with the command. It takes any AuthConfigProvider instead of a configfile.ConfigFile. The credentials do not change: they come from DOCKER_AUTH_CONFIG, the credential store, credential helpers, and the configuration file.

Document how the daemon uses the Password, Auth, IdentityToken, and RegistryToken fields of types.AuthConfig, which answers the TODO that asked whether the encoded credentials should include them.

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
Add the Docker secrets engine client, used to read the Docker Hub session from the secrets engine. It brings in github.com/docker/secrets-engine/secrets v1.0.0.

The client module requires go1.26.11, which raises the go directive in vendor.mod from 1.26.0.

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

Add a provider that reads the Docker Hub session from the Docker secrets engine and returns it as the credentials for Docker Hub (docker.io) and Docker Hardened Images (dhi.io), keeping the production and staging sessions apart. Other registries are not looked up.

The secrets engine of Docker Desktop is used when its socket exists, and the standalone secrets engine otherwise; only one of them is contacted. A missing socket is detected without dialing.

Before the first session read, ask the engine for its version within 1 second, then call Authorize on docker/auth/** with up to 1 minute to answer. Docker Desktop prompts the user before it gives a binary that is not signed by Docker access to the session; builds signed by Docker get no prompt. Authorize only gets the longer timeout once the engine has answered, so an engine that hangs is given up on after 1 second. Every request is bound to the caller's context, so canceling the command stops the wait for the prompt. Session reads time out after 5 seconds.

The authorization is kept until the expiry the engine returns, or for the life of the process if it returns none. The session is cached until it expires. When no session is usable (no engine, signed out, access denied, an unanswered prompt, or a canceled lookup), no credentials are returned and the engine is not asked again for a minute.

The tests run the secrets engine client against an in-memory engine that answers like the real one.

Add registry.DefaultRegistryHost, so the Docker Hub registry host is defined once.

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
Send the Docker Hub session from the secrets engine as the registry credentials for Docker Hub (docker.io) and Docker Hardened Images (dhi.io) when pulling or pushing images through the daemon: docker pull, push, run, create, plugin install, upgrade and push, service create and update, and stack deploy.

Credentials in DOCKER_AUTH_CONFIG take precedence for the registries they cover. Without a usable session, the CLI uses its credential store, credential helpers, and configuration file as before. docker login and stored credentials are not changed.

Only the docker binary turns on the secrets engine lookup, so tests and other users of the package never contact a secrets engine on the machine they run on.

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
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.

4 participants