@@ -27,9 +27,6 @@ import (
27
27
"os"
28
28
"strings"
29
29
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"
33
30
"golang.org/x/net/context/ctxhttp"
34
31
"golang.org/x/term"
35
32
@@ -48,106 +45,64 @@ Configure a credential helper to remove this warning. See
48
45
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
49
46
`
50
47
51
- type isFileStore interface {
52
- IsFileStore () bool
53
- GetFilename () string
54
- }
55
-
56
48
func 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
62
57
}
63
58
64
59
var responseIdentityToken string
65
- isDefaultRegistry := serverAddress == dockerconfigresolver .IndexServer
66
60
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 )
74
66
}
75
67
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 )
78
70
if err != nil {
79
71
return err
80
72
}
81
73
82
- responseIdentityToken , err = loginClientSide (ctx , options .GOptions , * authConfig )
74
+ responseIdentityToken , err = loginClientSide (ctx , options .GOptions , registryURL , credentials )
83
75
if err != nil {
84
76
return err
85
77
}
86
78
}
87
79
88
80
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
96
83
}
97
84
98
- creds := dockerConfigFile .GetCredentialsStore (serverAddress )
99
-
100
- store , isFile := creds .(isFileStore )
101
85
// 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 ))
104
89
if err != nil {
105
90
return err
106
91
}
107
92
}
108
93
109
- if err := creds .Store (dockercliconfigtypes .AuthConfig (* (authConfig ))); err != nil {
94
+ err = credStore .Store (registryURL , credentials )
95
+ if err != nil {
110
96
return fmt .Errorf ("error saving credentials: %w" , err )
111
97
}
112
98
113
- fmt .Fprintln (stdout , "Login Succeeded" )
114
-
115
- return nil
116
- }
99
+ _ , err = fmt .Fprintln (stdout , "Login Succeeded" )
117
100
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
144
102
}
145
103
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
151
106
var dOpts []dockerconfigresolver.Opt
152
107
if globalOptions .InsecureRegistry {
153
108
log .G (ctx ).Warnf ("skipping verifying HTTPS certs for %q" , host )
@@ -157,12 +112,12 @@ func loginClientSide(ctx context.Context, globalOptions types.GlobalCommandOptio
157
112
158
113
authCreds := func (acArg string ) (string , string , error ) {
159
114
if acArg == host {
160
- if auth .RegistryToken != "" {
115
+ if credentials .RegistryToken != "" {
161
116
// Even containerd/CRI does not support RegistryToken as of v1.4.3,
162
117
// so, nobody is actually using RegistryToken?
163
118
log .G (ctx ).Warnf ("RegistryToken (for %q) is not supported yet (FIXME)" , host )
164
119
}
165
- return auth .Username , auth .Password , nil
120
+ return credentials .Username , credentials .Password , nil
166
121
}
167
122
return "" , "" , fmt .Errorf ("expected acArg to be %q, got %q" , host , acArg )
168
123
}
@@ -251,10 +206,9 @@ func tryLoginWithRegHost(ctx context.Context, rh docker.RegistryHost) error {
251
206
return errors .New ("too many 401 (probably)" )
252
207
}
253
208
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 {
256
210
if username = strings .TrimSpace (username ); username == "" {
257
- username = authConfig .Username
211
+ username = credentials .Username
258
212
}
259
213
if username == "" {
260
214
fmt .Print ("Enter Username: " )
@@ -281,8 +235,8 @@ func ConfigureAuthentication(authConfig *registry.AuthConfig, username, password
281
235
return fmt .Errorf ("error: Password is Required" )
282
236
}
283
237
284
- authConfig .Username = username
285
- authConfig .Password = password
238
+ credentials .Username = username
239
+ credentials .Password = password
286
240
287
241
return nil
288
242
}
@@ -304,22 +258,3 @@ func readUsername() (string, error) {
304
258
305
259
return username , nil
306
260
}
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