Skip to content
Merged
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
111 changes: 91 additions & 20 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/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,44 @@ 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
socketPath string
}

func (h desktopHub) Ping(ctx context.Context) error {
_, err := h.engine.Version(ctx)
if err == nil {
return nil
}
if _, ok := errors.AsType[*seclient.ConnectError](err); ok {
return err
}
if errors.Is(err, context.DeadlineExceeded) {
return &seclient.ConnectError{Reason: seclient.ReasonTimeout, SocketPath: h.socketPath, Err: err}
}
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 +103,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 +121,12 @@ func NewDesktopAuthProvider(fallback AuthProvider) AuthProvider {
}

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

type desktopAuthProvider struct {
Expand Down Expand Up @@ -129,18 +166,48 @@ 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 {
Comment thread
Benehiko marked this conversation as resolved.
Resolved
if s.authorized && (s.authorizedUntil.IsZero() || s.now().Before(s.authorizedUntil)) {
return nil
}
s.authorized, s.authorizedUntil = false, time.Time{}
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)
Comment thread
Benehiko marked this conversation as resolved.
Resolved
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
Comment thread
Benehiko marked this conversation as resolved.
Resolved
return nil
}

func (s *desktopSession) auth() (clitypes.AuthConfig, bool) {
Expand Down Expand Up @@ -175,6 +242,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