@@ -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,13 +27,19 @@ const (
28
27
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
29
28
)
30
29
30
+ const (
31
+ // IndexHostname is the index hostname, used for authentication and image search.
32
+ indexHostname = "index.docker.io"
33
+ // IndexServer is used for user auth and image search
34
+ indexServer = "https://" + indexHostname + "/v1/"
35
+ )
36
+
31
37
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
32
38
// for the given command.
33
39
func RegistryAuthenticationPrivilegedFunc (cli Cli , index * registrytypes.IndexInfo , cmdName string ) registrytypes.RequestAuthConfig {
34
40
return func (ctx context.Context ) (string , error ) {
35
41
_ , _ = fmt .Fprintf (cli .Out (), "\n Login prior to %s:\n " , cmdName )
36
- indexServer := registry .GetAuthConfigKey (index )
37
- isDefaultRegistry := indexServer == registry .IndexServer
42
+ isDefaultRegistry := index .Official || index .Name == indexServer
38
43
authConfig , err := GetDefaultAuthConfig (cli .ConfigFile (), true , indexServer , isDefaultRegistry )
39
44
if err != nil {
40
45
_ , _ = fmt .Fprintf (cli .Err (), "Unable to retrieve stored credentials for %s, error: %s.\n " , indexServer , err )
@@ -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 = indexServer
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 == indexServer {
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.
@@ -219,15 +224,58 @@ func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (strin
219
224
return encodedAuth , nil
220
225
}
221
226
227
+ var IndexConfigs = map [string ]* registrytypes.IndexInfo {
228
+ "docker.io" : {
229
+ Name : "docker.io" ,
230
+ Mirrors : nil ,
231
+ Secure : true ,
232
+ Official : true ,
233
+ },
234
+ }
235
+
236
+ // newIndexInfo returns IndexInfo configuration from indexName
237
+ func newIndexInfo (indexName string ) (* registrytypes.IndexInfo , error ) {
238
+ var err error
239
+ indexName , err = validateIndexName (indexName )
240
+ if err != nil {
241
+ return nil , err
242
+ }
243
+
244
+ // Return any configured index info, first.
245
+ if index , ok := IndexConfigs [indexName ]; ok {
246
+ return index , nil
247
+ }
248
+
249
+ // Construct a non-configured index info.
250
+ return & registrytypes.IndexInfo {
251
+ Name : indexName ,
252
+ }, nil
253
+ }
254
+
255
+ // validateIndexName validates an index name. It is used by the daemon to
256
+ // validate the daemon configuration.
257
+ func validateIndexName (val string ) (string , error ) {
258
+ // TODO: upstream this to check to reference package
259
+ if val == "index.docker.io" {
260
+ val = "docker.io"
261
+ }
262
+ if strings .HasPrefix (val , "-" ) || strings .HasSuffix (val , "-" ) {
263
+ // return "", errdefs.InvalidParameter(fmt.Errorf("invalid index name (%s). Cannot begin or end with a hyphen", val))
264
+ return "" , fmt .Errorf ("invalid index name (%s). Cannot begin or end with a hyphen" , val )
265
+ }
266
+ return val , nil
267
+ }
268
+
222
269
// resolveAuthConfigFromImage retrieves that AuthConfig using the image string
223
270
func resolveAuthConfigFromImage (cfg * configfile.ConfigFile , image string ) (registrytypes.AuthConfig , error ) {
224
271
registryRef , err := reference .ParseNormalizedNamed (image )
225
272
if err != nil {
226
273
return registrytypes.AuthConfig {}, err
227
274
}
228
- repoInfo , err := registry .ParseRepositoryInfo (registryRef )
275
+ domainName := reference .Domain (registryRef )
276
+ idxInfo , err := newIndexInfo (domainName )
229
277
if err != nil {
230
278
return registrytypes.AuthConfig {}, err
231
279
}
232
- return ResolveAuthConfig (cfg , repoInfo . Index ), nil
280
+ return ResolveAuthConfig (cfg , idxInfo ), nil
233
281
}
0 commit comments