@@ -15,7 +15,6 @@ import (
15
15
"github.com/docker/cli/cli/streams"
16
16
"github.com/docker/cli/internal/tui"
17
17
registrytypes "github.com/docker/docker/api/types/registry"
18
- "github.com/docker/docker/registry"
19
18
"github.com/morikuni/aec"
20
19
"github.com/pkg/errors"
21
20
)
@@ -28,16 +27,22 @@ const (
28
27
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
29
28
)
30
29
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
+
31
36
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
32
37
// for the given command.
33
38
func RegistryAuthenticationPrivilegedFunc (cli Cli , index * registrytypes.IndexInfo , cmdName string ) registrytypes.RequestAuthConfig {
39
+ configKey := getAuthConfigKey (index .Name )
40
+ isDefaultRegistry := configKey == authConfigKey || index .Official
34
41
return func (ctx context.Context ) (string , error ) {
35
42
_ , _ = 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 )
39
44
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 )
41
46
}
42
47
43
48
select {
@@ -46,7 +51,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
46
51
default :
47
52
}
48
53
49
- authConfig , err = PromptUserForCredentials (ctx , cli , "" , "" , authConfig .Username , indexServer )
54
+ authConfig , err = PromptUserForCredentials (ctx , cli , "" , "" , authConfig .Username , authConfigKey )
50
55
if err != nil {
51
56
return "" , err
52
57
}
@@ -63,7 +68,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
63
68
func ResolveAuthConfig (cfg * configfile.ConfigFile , index * registrytypes.IndexInfo ) registrytypes.AuthConfig {
64
69
configKey := index .Name
65
70
if index .Official {
66
- configKey = registry . IndexServer
71
+ configKey = authConfigKey
67
72
}
68
73
69
74
a , _ := cfg .GetAuthConfig (configKey )
@@ -132,7 +137,7 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword
132
137
133
138
argUser = strings .TrimSpace (argUser )
134
139
if argUser == "" {
135
- if serverAddress == registry . IndexServer {
140
+ if serverAddress == authConfigKey {
136
141
// When signing in to the default (Docker Hub) registry, we display
137
142
// hints for creating an account, and (if hints are enabled), using
138
143
// a token instead of a password.
@@ -225,9 +230,25 @@ func resolveAuthConfigFromImage(cfg *configfile.ConfigFile, image string) (regis
225
230
if err != nil {
226
231
return registrytypes.AuthConfig {}, err
227
232
}
228
- repoInfo , err := registry .ParseRepositoryInfo (registryRef )
233
+ configKey := getAuthConfigKey (reference .Domain (registryRef ))
234
+ a , err := cfg .GetAuthConfig (configKey )
229
235
if err != nil {
230
236
return registrytypes.AuthConfig {}, err
231
237
}
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
233
254
}
0 commit comments