Repository navigation
registry: authorize docker/auth/** before reading the Docker Desktop session #14542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
52d1b29
1c996b9
f09eb52
bb8956d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
|
@@ -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) | ||||||
|
|
@@ -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, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -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 { | ||||||
|
|
@@ -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 { | ||||||
| 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) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [medium]
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
|
||||||
| 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) { | ||||||
|
|
@@ -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() | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
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, whichisSessionUnavailabledoesn't recognise — emits a spuriousWarn-level log instead ofDebugWhen the 1-second ping to the secrets engine times out,
s.hub.Ping(pingCtx)returnscontext.DeadlineExceeded.authorize()wraps it and returns it toauth(), which passes it tologDesktopFallback().isSessionUnavailable()only matches*seclient.ConnectError,dockerhub.ErrNoSession, anderrSessionExpired— none of which matchcontext.DeadlineExceeded— sologDesktopFallbackemits aWarn-level message: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 testTestDesktopAuthProvider_FallsBackWhenTheEngineDoesNotAnsweralready exercisescontext.DeadlineExceededas a ping error, but doesn't assert the log level.The fix is to extend
isSessionUnavailableto also recognise deadline/timeout errors from the ping path: