@@ -15,7 +15,6 @@ import (
1515 "github.com/docker/cli/cli/streams"
1616 "github.com/docker/cli/internal/tui"
1717 registrytypes "github.com/docker/docker/api/types/registry"
18- "github.com/docker/docker/registry"
1918 "github.com/morikuni/aec"
2019 "github.com/pkg/errors"
2120)
@@ -28,16 +27,22 @@ const (
2827 "for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
2928)
3029
30+ // authConfigKey is the key used to store credentials for Docker Hub. It is
31+ // a copy of [registry.IndexServer].
32+ //
33+ // [registry.IndexServer]: https://pkg.go.dev/github.com/docker/docker/registry#IndexServer
34+ const authConfigKey = "https:/index.docker.io/v1/"
35+
3136// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
3237// for the given command.
3338func RegistryAuthenticationPrivilegedFunc (cli Cli , index * registrytypes.IndexInfo , cmdName string ) registrytypes.RequestAuthConfig {
39+ configKey := getAuthConfigKey (index .Name )
40+ isDefaultRegistry := configKey == authConfigKey || index .Official
3441 return func (ctx context.Context ) (string , error ) {
3542 _ , _ = fmt .Fprintf (cli .Out (), "\n Login prior to %s:\n " , cmdName )
36- indexServer := registry .GetAuthConfigKey (index )
37- isDefaultRegistry := indexServer == registry .IndexServer
38- authConfig , err := GetDefaultAuthConfig (cli .ConfigFile (), true , indexServer , isDefaultRegistry )
43+ authConfig , err := GetDefaultAuthConfig (cli .ConfigFile (), true , configKey , isDefaultRegistry )
3944 if err != nil {
40- _ , _ = fmt .Fprintf (cli .Err (), "Unable to retrieve stored credentials for %s, error: %s.\n " , indexServer , err )
45+ _ , _ = fmt .Fprintf (cli .Err (), "Unable to retrieve stored credentials for %s, error: %s.\n " , authConfigKey , err )
4146 }
4247
4348 select {
@@ -46,7 +51,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
4651 default :
4752 }
4853
49- authConfig , err = PromptUserForCredentials (ctx , cli , "" , "" , authConfig .Username , indexServer )
54+ authConfig , err = PromptUserForCredentials (ctx , cli , "" , "" , authConfig .Username , authConfigKey )
5055 if err != nil {
5156 return "" , err
5257 }
@@ -63,7 +68,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
6368func ResolveAuthConfig (cfg * configfile.ConfigFile , index * registrytypes.IndexInfo ) registrytypes.AuthConfig {
6469 configKey := index .Name
6570 if index .Official {
66- configKey = registry . IndexServer
71+ configKey = authConfigKey
6772 }
6873
6974 a , _ := cfg .GetAuthConfig (configKey )
@@ -132,7 +137,7 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword
132137
133138 argUser = strings .TrimSpace (argUser )
134139 if argUser == "" {
135- if serverAddress == registry . IndexServer {
140+ if serverAddress == authConfigKey {
136141 // When signing in to the default (Docker Hub) registry, we display
137142 // hints for creating an account, and (if hints are enabled), using
138143 // a token instead of a password.
@@ -225,9 +230,25 @@ func resolveAuthConfigFromImage(cfg *configfile.ConfigFile, image string) (regis
225230 if err != nil {
226231 return registrytypes.AuthConfig {}, err
227232 }
228- repoInfo , err := registry .ParseRepositoryInfo (registryRef )
233+ configKey := getAuthConfigKey (reference .Domain (registryRef ))
234+ a , err := cfg .GetAuthConfig (configKey )
229235 if err != nil {
230236 return registrytypes.AuthConfig {}, err
231237 }
232- return ResolveAuthConfig (cfg , repoInfo .Index ), nil
238+ return registrytypes .AuthConfig (a ), nil
239+ }
240+
241+ // getAuthConfigKey special-cases using the full index address of the official
242+ // index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
243+ //
244+ // It is similar to [registry.GetAuthConfigKey], but does not require on
245+ // [registrytypes.IndexInfo] as intermediate.
246+ //
247+ // [registry.GetAuthConfigKey]: https://pkg.go.dev/github.com/docker/docker/registry#GetAuthConfigKey
248+ // [registrytypes.IndexInfo]:https://pkg.go.dev/github.com/docker/docker/api/types/registry#IndexInfo
249+ func getAuthConfigKey (domainName string ) string {
250+ if domainName == "docker.io" || domainName == "index.docker.io" {
251+ return authConfigKey
252+ }
253+ return domainName
233254}
0 commit comments