Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ require (
github.com/docker/cli v29.10.0+incompatible
github.com/docker/cli-docs-tool v0.11.0
github.com/docker/go-units v0.5.0
github.com/docker/secrets-engine/client v0.2.2
github.com/docker/secrets-engine/x v0.8.5
github.com/docker/secrets-engine/client v1.0.0
github.com/docker/secrets-engine/secrets v1.0.0
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203
github.com/fsnotify/fsevents v0.2.0
github.com/google/go-cmp v0.7.0
Expand Down Expand Up @@ -63,7 +63,6 @@ require (
)

require (
connectrpc.com/connect v1.19.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20261006220739-8c912ac31dd4 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down Expand Up @@ -123,7 +122,6 @@ require (
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/crypto v0.57.0 // indirect
golang.org/x/mod v0.41.0 // indirect
golang.org/x/net v0.60.0 // indirect
golang.org/x/text v0.42.0 // indirect
golang.org/x/time v0.16.0 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
connectrpc.com/connect v1.19.1 h1:R5M57z05+90EfEvCY1b7hBxDVOUl45PrtXtAV2fOC14=
connectrpc.com/connect v1.19.1/go.mod h1:tN20fjdGlewnSFeZxLKb0xwIZ6ozc3OQs2hTXy4du9w=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/Azure/go-ansiterm v0.0.0-20261006220739-8c912ac31dd4 h1:m2xcjRABtG288vnlV3U/PKh/HnPURxZp50RKNUwBxM8=
Expand Down Expand Up @@ -90,10 +88,10 @@ github.com/docker/go-metrics v0.1.0 h1:r76KPNpstz+IvQKSWpYegSkkyzex0V3A1ZGVx6bhG
github.com/docker/go-metrics v0.1.0/go.mod h1:PciI3sONtB051kXALN1JoIlpcu54E1FuPh+4DuqEzyw=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/secrets-engine/client v0.2.2 h1:aPvmlzsxNA+C+xgp2F+WiiDbCSAXHC591HscFFOvPOE=
github.com/docker/secrets-engine/client v0.2.2/go.mod h1:c3l/N+fA0kiy9S8BE3kKrO5hOZu4+SLYZK9Jx+jc2cw=
github.com/docker/secrets-engine/x v0.8.5 h1:P7D/YESMjpX18S4ratBgoeJkAFIxgsPxe093uFmgiGc=
github.com/docker/secrets-engine/x v0.8.5/go.mod h1:jQ5CXCYCPn8Sw96TS8Ufq3oyIGH2GzUtVXIdwa2Q0VU=
github.com/docker/secrets-engine/client v1.0.0 h1:/zMCWoch4OOoMQoCo257H6siYUuUcQiTZBcX081uQm0=
github.com/docker/secrets-engine/client v1.0.0/go.mod h1:EGa8hWWhSWBQ4+Yn2BHO4xeXZ4wYCI9eAQ3U1Fy1sIA=
github.com/docker/secrets-engine/secrets v1.0.0 h1:29fYA7dLYYN6dbyCoDUX7VVu0usZ8bKvpJolCbs8TAw=
github.com/docker/secrets-engine/secrets v1.0.0/go.mod h1:xGWoZkaQpSd/PP5s8etspPkrs8Qa07jVblo2UEQZi5w=
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg=
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
Expand Down
101 changes: 80 additions & 21 deletions internal/registry/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ import (

clitypes "github.com/docker/cli/cli/config/types"
seclient "github.com/docker/secrets-engine/client"
"github.com/docker/secrets-engine/client/api/resolver"
"github.com/docker/secrets-engine/client/config"
"github.com/docker/secrets-engine/client/dockerhub"
"github.com/docker/secrets-engine/x/api"
"github.com/docker/secrets-engine/secrets"
"github.com/sirupsen/logrus"
)

const (
// desktopLookupTimeout limits connection time in case of hanging or
// unexpected request delay by the secrets engine.
desktopLookupTimeout = 5 * time.Second
sessionExpirySkew = 30 * time.Second
desktopLookupTimeout = 5 * time.Second
desktopPingTimeout = time.Second
desktopAuthorizeTimeout = time.Minute
sessionExpirySkew = 30 * time.Second
// sessionTTL is how long a session without an expiry claim is reused.
sessionTTL = time.Minute
// retryInterval is how long Desktop is not asked again after a failed lookup.
Expand All @@ -48,11 +52,34 @@ var (
)

type hubSessions interface {
Ping(ctx context.Context) error
Authorize(ctx context.Context) (seclient.AuthorizeResponse, error)
GetDefaultSession(ctx context.Context) (dockerhub.UserSession, error)
GetDefaultProfile(ctx context.Context) (dockerhub.Profile, error)
}

var _ hubSessions = dockerhub.ClientAuth(nil)
var dockerAuthRealm = secrets.MustParsePattern("docker/auth/**")

type secretsEngine interface {
resolver.Authorizer
Version(ctx context.Context) (seclient.DaemonVersion, error)
}

type desktopHub struct {
dockerhub.ClientAuth
engine secretsEngine
}

func (h desktopHub) Ping(ctx context.Context) error {
_, err := h.engine.Version(ctx)
return err
}

func (h desktopHub) Authorize(ctx context.Context) (seclient.AuthorizeResponse, error) {
return h.engine.Authorize(ctx, dockerAuthRealm)
}

var _ hubSessions = desktopHub{}

var (
sharedHubSession = newDesktopSession("Docker Hub", IndexServer)
Expand All @@ -66,8 +93,10 @@ func newDesktopSession(name, serverAddress string, opts ...dockerhub.Option) *de
connect: func() (hubSessions, error) {
return connectDesktop(opts...)
},
timeout: desktopLookupTimeout,
now: time.Now,
timeout: desktopLookupTimeout,
pingTimeout: desktopPingTimeout,
authorizeTimeout: desktopAuthorizeTimeout,
now: time.Now,
}
}

Expand All @@ -82,14 +111,11 @@ func NewDesktopAuthProvider(fallback AuthProvider) AuthProvider {
}

func connectDesktop(opts ...dockerhub.Option) (hubSessions, error) {
c, err := seclient.New(
seclient.WithSocketPath(api.DesktopSocketPath()),
seclient.WithTimeout(desktopLookupTimeout),
)
c, err := seclient.New(seclient.WithSocketPath(config.DesktopSocketPath()))
if err != nil {
return nil, err
}
return c.HubAuth(opts...), nil
return desktopHub{ClientAuth: c.HubAuth(opts...), engine: c}, nil
}

type desktopAuthProvider struct {
Expand Down Expand Up @@ -129,18 +155,47 @@ func registryHost(registryHostname string) string {

// desktopSession caches the Desktop session of one Docker Hub environment.
type desktopSession struct {
name string
serverAddress string
connect func() (hubSessions, error)
timeout time.Duration
now func() time.Time
name string
serverAddress string
connect func() (hubSessions, error)
timeout time.Duration
pingTimeout time.Duration
authorizeTimeout time.Duration
now func() time.Time

// mu serializes lookups so concurrent pulls share one request.
mu sync.Mutex
hub hubSessions
retryAt time.Time
cached *clitypes.AuthConfig
validUntil time.Time
mu sync.Mutex
hub hubSessions
authorized bool
authorizedUntil time.Time
retryAt time.Time
cached *clitypes.AuthConfig
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

if s.authorized && (s.authorizedUntil.IsZero() || s.now().Before(s.authorizedUntil)) {
return nil
}
pingCtx, cancelPing := context.WithTimeout(context.Background(), s.pingTimeout)
err := s.hub.Ping(pingCtx)
cancelPing()
if err != nil {
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

defer cancel()

resp, err := s.hub.Authorize(ctx)
if err != nil {
return fmt.Errorf("authorizing access to the session: %w", err)
}
if !resp.Allow {
return secrets.ErrAccessDenied
}
s.authorized, s.authorizedUntil = true, resp.Expiry
return nil
}

func (s *desktopSession) auth() (clitypes.AuthConfig, bool) {
Expand Down Expand Up @@ -175,6 +230,10 @@ func (s *desktopSession) fetch() (clitypes.AuthConfig, time.Time, error) {
s.hub = hub
}

if err := s.authorize(); err != nil {
return clitypes.AuthConfig{}, time.Time{}, err
}

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

Expand Down
Loading
Loading