@@ -27,9 +27,6 @@ import (
2727 "os"
2828 "strings"
2929
30- dockercliconfig "github.com/docker/cli/cli/config"
31- dockercliconfigtypes "github.com/docker/cli/cli/config/types"
32- "github.com/docker/docker/api/types/registry"
3330 "golang.org/x/net/context/ctxhttp"
3431 "golang.org/x/term"
3532
@@ -48,106 +45,64 @@ Configure a credential helper to remove this warning. See
4845https://docs.docker.com/engine/reference/commandline/login/#credentials-store
4946`
5047
51- type isFileStore interface {
52- IsFileStore () bool
53- GetFilename () string
54- }
55-
5648func Login (ctx context.Context , options types.LoginCommandOptions , stdout io.Writer ) error {
57- var serverAddress string
58- if options .ServerAddress == "" || options .ServerAddress == "docker.io" || options .ServerAddress == "index.docker.io" || options .ServerAddress == "registry-1.docker.io" {
59- serverAddress = dockerconfigresolver .IndexServer
60- } else {
61- serverAddress = options .ServerAddress
49+ registryURL , err := dockerconfigresolver .Parse (options .ServerAddress )
50+ if err != nil {
51+ return err
52+ }
53+
54+ credStore , err := dockerconfigresolver .NewCredentialsStore ("" )
55+ if err != nil {
56+ return err
6257 }
6358
6459 var responseIdentityToken string
65- isDefaultRegistry := serverAddress == dockerconfigresolver .IndexServer
6660
67- authConfig , err := GetDefaultAuthConfig (options .Username == "" && options .Password == "" , serverAddress , isDefaultRegistry )
68- if authConfig == nil {
69- authConfig = & registry.AuthConfig {ServerAddress : serverAddress }
70- }
71- if err == nil && authConfig .Username != "" && authConfig .Password != "" {
72- // login With StoreCreds
73- responseIdentityToken , err = loginClientSide (ctx , options .GOptions , * authConfig )
61+ credentials , err := credStore .Retrieve (registryURL , options .Username == "" && options .Password == "" )
62+ credentials .IdentityToken = ""
63+
64+ if err == nil && credentials .Username != "" && credentials .Password != "" {
65+ responseIdentityToken , err = loginClientSide (ctx , options .GOptions , registryURL , credentials )
7466 }
7567
76- if err != nil || authConfig .Username == "" || authConfig .Password == "" {
77- err = ConfigureAuthentication ( authConfig , options .Username , options .Password )
68+ if err != nil || credentials .Username == "" || credentials .Password == "" {
69+ err = configureAuthentication ( credentials , options .Username , options .Password )
7870 if err != nil {
7971 return err
8072 }
8173
82- responseIdentityToken , err = loginClientSide (ctx , options .GOptions , * authConfig )
74+ responseIdentityToken , err = loginClientSide (ctx , options .GOptions , registryURL , credentials )
8375 if err != nil {
8476 return err
8577 }
8678 }
8779
8880 if responseIdentityToken != "" {
89- authConfig .Password = ""
90- authConfig .IdentityToken = responseIdentityToken
91- }
92-
93- dockerConfigFile , err := dockercliconfig .Load ("" )
94- if err != nil {
95- return err
81+ credentials .Password = ""
82+ credentials .IdentityToken = responseIdentityToken
9683 }
9784
98- creds := dockerConfigFile .GetCredentialsStore (serverAddress )
99-
100- store , isFile := creds .(isFileStore )
10185 // Display a warning if we're storing the users password (not a token) and credentials store type is file.
102- if isFile && authConfig .Password != "" {
103- _ , err = fmt .Fprintln (stdout , fmt .Sprintf (unencryptedPasswordWarning , store .GetFilename ()))
86+ storageFileLocation := credStore .FileStorageLocation (registryURL )
87+ if storageFileLocation != "" && credentials .Password != "" {
88+ _ , err = fmt .Fprintln (stdout , fmt .Sprintf (unencryptedPasswordWarning , storageFileLocation ))
10489 if err != nil {
10590 return err
10691 }
10792 }
10893
109- if err := creds .Store (dockercliconfigtypes .AuthConfig (* (authConfig ))); err != nil {
94+ err = credStore .Store (registryURL , credentials )
95+ if err != nil {
11096 return fmt .Errorf ("error saving credentials: %w" , err )
11197 }
11298
113- fmt .Fprintln (stdout , "Login Succeeded" )
114-
115- return nil
116- }
99+ _ , err = fmt .Fprintln (stdout , "Login Succeeded" )
117100
118- // GetDefaultAuthConfig gets the default auth config given a serverAddress.
119- // If credentials for given serverAddress exists in the credential store, the configuration will be populated with values in it.
120- // Code from github.com/docker/cli/cli/command (v20.10.3).
121- func GetDefaultAuthConfig (checkCredStore bool , serverAddress string , isDefaultRegistry bool ) (* registry.AuthConfig , error ) {
122- if ! isDefaultRegistry {
123- var err error
124- serverAddress , err = convertToHostname (serverAddress )
125- if err != nil {
126- return nil , err
127- }
128- }
129- authconfig := dockercliconfigtypes.AuthConfig {}
130- if checkCredStore {
131- dockerConfigFile , err := dockercliconfig .Load ("" )
132- if err != nil {
133- return nil , err
134- }
135- authconfig , err = dockerConfigFile .GetAuthConfig (serverAddress )
136- if err != nil {
137- return nil , err
138- }
139- }
140- authconfig .ServerAddress = serverAddress
141- authconfig .IdentityToken = ""
142- res := registry .AuthConfig (authconfig )
143- return & res , nil
101+ return err
144102}
145103
146- func loginClientSide (ctx context.Context , globalOptions types.GlobalCommandOptions , auth registry.AuthConfig ) (string , error ) {
147- host , err := convertToHostname (auth .ServerAddress )
148- if err != nil {
149- return "" , err
150- }
104+ func loginClientSide (ctx context.Context , globalOptions types.GlobalCommandOptions , registryURL * dockerconfigresolver.RegistryURL , credentials * dockerconfigresolver.Credentials ) (string , error ) {
105+ host := registryURL .Host
151106 var dOpts []dockerconfigresolver.Opt
152107 if globalOptions .InsecureRegistry {
153108 log .G (ctx ).Warnf ("skipping verifying HTTPS certs for %q" , host )
@@ -157,12 +112,12 @@ func loginClientSide(ctx context.Context, globalOptions types.GlobalCommandOptio
157112
158113 authCreds := func (acArg string ) (string , string , error ) {
159114 if acArg == host {
160- if auth .RegistryToken != "" {
115+ if credentials .RegistryToken != "" {
161116 // Even containerd/CRI does not support RegistryToken as of v1.4.3,
162117 // so, nobody is actually using RegistryToken?
163118 log .G (ctx ).Warnf ("RegistryToken (for %q) is not supported yet (FIXME)" , host )
164119 }
165- return auth .Username , auth .Password , nil
120+ return credentials .Username , credentials .Password , nil
166121 }
167122 return "" , "" , fmt .Errorf ("expected acArg to be %q, got %q" , host , acArg )
168123 }
@@ -251,10 +206,9 @@ func tryLoginWithRegHost(ctx context.Context, rh docker.RegistryHost) error {
251206 return errors .New ("too many 401 (probably)" )
252207}
253208
254- func ConfigureAuthentication (authConfig * registry.AuthConfig , username , password string ) error {
255- authConfig .Username = strings .TrimSpace (authConfig .Username )
209+ func configureAuthentication (credentials * dockerconfigresolver.Credentials , username , password string ) error {
256210 if username = strings .TrimSpace (username ); username == "" {
257- username = authConfig .Username
211+ username = credentials .Username
258212 }
259213 if username == "" {
260214 fmt .Print ("Enter Username: " )
@@ -281,8 +235,8 @@ func ConfigureAuthentication(authConfig *registry.AuthConfig, username, password
281235 return fmt .Errorf ("error: Password is Required" )
282236 }
283237
284- authConfig .Username = username
285- authConfig .Password = password
238+ credentials .Username = username
239+ credentials .Password = password
286240
287241 return nil
288242}
@@ -304,22 +258,3 @@ func readUsername() (string, error) {
304258
305259 return username , nil
306260}
307-
308- func convertToHostname (serverAddress string ) (string , error ) {
309- // Ensure that URL contains scheme for a good parsing process
310- if strings .Contains (serverAddress , "://" ) {
311- u , err := url .Parse (serverAddress )
312- if err != nil {
313- return "" , err
314- }
315- serverAddress = u .Host
316- } else {
317- u , err := url .Parse ("https://" + serverAddress )
318- if err != nil {
319- return "" , err
320- }
321- serverAddress = u .Host
322- }
323-
324- return serverAddress , nil
325- }
0 commit comments