@@ -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,13 +27,19 @@ const (
2827 "for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
2928)
3029
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+
3137// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
3238// for the given command.
3339func RegistryAuthenticationPrivilegedFunc (cli Cli , index * registrytypes.IndexInfo , cmdName string ) registrytypes.RequestAuthConfig {
3440 return func (ctx context.Context ) (string , error ) {
3541 _ , _ = 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
3843 authConfig , err := GetDefaultAuthConfig (cli .ConfigFile (), true , indexServer , isDefaultRegistry )
3944 if err != nil {
4045 _ , _ = 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
6368func ResolveAuthConfig (cfg * configfile.ConfigFile , index * registrytypes.IndexInfo ) registrytypes.AuthConfig {
6469 configKey := index .Name
6570 if index .Official {
66- configKey = registry . IndexServer
71+ configKey = indexServer
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 == indexServer {
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.
@@ -219,15 +224,58 @@ func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (strin
219224 return encodedAuth , nil
220225}
221226
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+
222269// resolveAuthConfigFromImage retrieves that AuthConfig using the image string
223270func resolveAuthConfigFromImage (cfg * configfile.ConfigFile , image string ) (registrytypes.AuthConfig , error ) {
224271 registryRef , err := reference .ParseNormalizedNamed (image )
225272 if err != nil {
226273 return registrytypes.AuthConfig {}, err
227274 }
228- repoInfo , err := registry .ParseRepositoryInfo (registryRef )
275+ domainName := reference .Domain (registryRef )
276+ idxInfo , err := newIndexInfo (domainName )
229277 if err != nil {
230278 return registrytypes.AuthConfig {}, err
231279 }
232- return ResolveAuthConfig (cfg , repoInfo . Index ), nil
280+ return ResolveAuthConfig (cfg , idxInfo ), nil
233281}
0 commit comments