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