Skip to content

client/dockerhub: expired-session error, single "no usable session" check, optional caching helper #682

Open

Description

@Benehiko

Summary

Docker Compose uses the Docker Desktop Docker Hub session (via client/dockerhub) to authenticate pulls. To turn a session into a credential it can use, Compose wraps the SDK with its own code. Most of that code is not specific to Compose: any consumer that wants "the signed-in user Docker Hub credential" (the CLI, buildx, other tools) would write the same thing.

This issue tracks what client/dockerhub still lacks, checked at client/v1.0.0. Items 1, 2, and 5 of the original list are done; see Resolved.

Compose implementation (on client/v1.0.0, docker/compose#14541): internal/registry/desktop.go


Open

3. An expired session is returned as if it were valid

GetDefaultSession returns the stored token even after it has expired. Every caller checks the expiry itself and defines its own error. Compose, in desktopSession.fetch:

var errSessionExpired = errors.New("the session has expired (not signed in to Docker Desktop?)")
// ...
if exp := session.Claims.ExpiresAt; exp != nil {
	validUntil = exp.Add(-sessionExpirySkew)
	if !now.Before(validUntil) {
		return ..., errSessionExpired
	}
}

UserSession.ExpiresWithin (item 2) makes the check shorter, but each caller still has to make it and still needs its own error.

Ask: a dockerhub.ErrSessionExpired that wraps ErrNoSession, returned for an expired stored token, ideally with a configurable clock-skew margin.

4. No single check for "no usable session"

To choose between quietly falling back to other credentials and warning the user, Compose checks three unrelated errors, in isSessionUnavailable:

func isSessionUnavailable(err error) bool {
	if _, ok := errors.AsType[*seclient.ConnectError](err); ok {
		return true
	}
	return errors.Is(err, dockerhub.ErrNoSession) || errors.Is(err, errSessionExpired)
}

Since client/v0.2.0, a failed connection is a *client.ConnectError that matches client.ErrSecretsEngineNotRunning, ErrSecretsEnginePermissionDenied, or ErrSecretsEngineTimeout. That is an improvement, but there is still no single check that covers "cannot reach the engine", "nobody signed in", and "session expired".

Ask: with item 3 in place, document one check for this case, or provide a helper such as dockerhub.IsUnavailable(err).

6. (Optional) A caching, retrying credential source

Compose wraps the lookup in desktopSession: one shared lookup for concurrent pulls, reuse of the credential until shortly before it expires, and a back-off after a failed lookup so it does not ask Docker Desktop on every pull.

This is generic too. A wrapper such as dockerhub.NewCachedAuth(auth, opts...) would help, but it is the most debatable item if the SDK wants to stay stateless.


Proposal

Item 3 and the username could become one call that returns what a registry client needs:

type Credential struct {
	Username    string
	AccessToken string
	ExpiresAt   time.Time // zero if unknown
}

// GetDefaultCredential returns the default account credential. It returns
// ErrNoDefaultProfile / ErrNoSession when nobody is signed in, and
// ErrSessionExpired (wrapping ErrNoSession) when the stored token has expired.
GetDefaultCredential(ctx context.Context) (Credential, error)

With that and item 4, the Compose side shrinks to building a types.AuthConfig from Credential: errSessionExpired, isSessionUnavailable, and the fallback to GetDefaultProfile for the username would go away.

Happy to send PRs for any of these.


Resolved

  • 1. GetDefaultSession does not give you a username. Not changing the API: UserSession.Claims.Username is the field to use. It was empty because Docker Desktop did not always parse the token claims when it stored the credential; that is fixed, and a migration makes it consistent for stored credentials. See the first comment.
  • 2. No token expiry helper. UserSession.Expired() and UserSession.ExpiresWithin(d) were added in client/v0.2.1 (feat(client/dockerhub): add UserSession expiry helpers #689).
  • 5. Connecting to Desktop requires the unstable x module. client/config.DesktopSocketPath() is available in client/v1.0.0, which no longer depends on github.com/docker/secrets-engine/x (refactor(client): move socket paths from x/api to client/config #731).

Activity

  1. Benehiko commented on Oct 9, 2026

    @Benehiko
    MemberAuthor

    Update on items 1 and 2: we won't add a separate Username field to UserSession. UserSession.Claims.Username is the field to use.

    Claims.Username came back empty because Docker Desktop didn't always parse the access token's claims when it stored the credential. That's fixed, so in most cases Claims is now filled in, including exp. A migration is underway that will make this consistent for all stored credentials. Once it's done, Compose's GetDefaultProfile fallback and its own JWT exp decoding shouldn't be needed.

    Closing #683, which added the field. Items 3–6 are unaffected.

  2. Benehiko commented on Nov 24, 2026

    @Benehiko
    MemberAuthor

    Status update: items 2 and 5 have shipped.

    Item 1 is covered by the earlier comment (Claims.Username).

    Still open:

    • 3. dockerhub.ErrSessionExpired, wrapping ErrNoSession, returned for an expired stored token.
    • 4. One documented check for "no usable session". Since client/v0.2.0, a failed connection is reported as *client.ConnectError, so today a caller checks for a ConnectError, for ErrNoSession, and for its own expiry error separately.
    • 6. An optional helper that shares one lookup between concurrent callers, reuses the credential until shortly before it expires, and backs off after a failed lookup.
  3. changed the title [-]client/dockerhub: return a ready-to-use Docker Hub credential (username, expiry, expired-session error, Desktop connect)[/-] [+]client/dockerhub: expired-session error, single "no usable session" check, optional caching helper[/+] on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions