Skip to content

Commit a106161

Browse files
Merge pull request #6519 from thaJeztah/28.x_backport_authconfig_no_direct_cast
[28.x backport] cli/command: explicitly map AuthConfig fields instead of a direct cast
2 parents 5e42f82 + e491078 commit a106161

File tree

5 files changed

+95
-15
lines changed

5 files changed

+95
-15
lines changed

cli/command/image/build.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,17 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
340340
configFile := dockerCli.ConfigFile()
341341
creds, _ := configFile.GetAllCredentials()
342342
authConfigs := make(map[string]registrytypes.AuthConfig, len(creds))
343-
for k, auth := range creds {
344-
authConfigs[k] = registrytypes.AuthConfig(auth)
343+
for k, authConfig := range creds {
344+
authConfigs[k] = registrytypes.AuthConfig{
345+
Username: authConfig.Username,
346+
Password: authConfig.Password,
347+
ServerAddress: authConfig.ServerAddress,
348+
349+
// TODO(thaJeztah): Are these expected to be included?
350+
Auth: authConfig.Auth,
351+
IdentityToken: authConfig.IdentityToken,
352+
RegistryToken: authConfig.RegistryToken,
353+
}
345354
}
346355
buildOpts := imageBuildOptions(dockerCli, options)
347356
buildOpts.Version = buildtypes.BuilderV1

cli/command/registry.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,16 @@ func ResolveAuthConfig(cfg *configfile.ConfigFile, index *registrytypes.IndexInf
7777
}
7878

7979
a, _ := cfg.GetAuthConfig(configKey)
80-
return registrytypes.AuthConfig(a)
80+
return registrytypes.AuthConfig{
81+
Username: a.Username,
82+
Password: a.Password,
83+
ServerAddress: a.ServerAddress,
84+
85+
// TODO(thaJeztah): Are these expected to be included?
86+
Auth: a.Auth,
87+
IdentityToken: a.IdentityToken,
88+
RegistryToken: a.RegistryToken,
89+
}
8190
}
8291

8392
// GetDefaultAuthConfig gets the default auth config given a serverAddress
@@ -86,19 +95,27 @@ func GetDefaultAuthConfig(cfg *configfile.ConfigFile, checkCredStore bool, serve
8695
if !isDefaultRegistry {
8796
serverAddress = credentials.ConvertToHostname(serverAddress)
8897
}
89-
authconfig := configtypes.AuthConfig{}
98+
authCfg := configtypes.AuthConfig{}
9099
var err error
91100
if checkCredStore {
92-
authconfig, err = cfg.GetAuthConfig(serverAddress)
101+
authCfg, err = cfg.GetAuthConfig(serverAddress)
93102
if err != nil {
94103
return registrytypes.AuthConfig{
95104
ServerAddress: serverAddress,
96105
}, err
97106
}
98107
}
99-
authconfig.ServerAddress = serverAddress
100-
authconfig.IdentityToken = ""
101-
return registrytypes.AuthConfig(authconfig), nil
108+
109+
return registrytypes.AuthConfig{
110+
Username: authCfg.Username,
111+
Password: authCfg.Password,
112+
ServerAddress: serverAddress,
113+
114+
// TODO(thaJeztah): Are these expected to be included?
115+
Auth: authCfg.Auth,
116+
IdentityToken: "",
117+
RegistryToken: authCfg.RegistryToken,
118+
}, nil
102119
}
103120

104121
// PromptUserForCredentials handles the CLI prompt for the user to input
@@ -213,7 +230,16 @@ func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (strin
213230
return "", err
214231
}
215232

216-
encodedAuth, err := registrytypes.EncodeAuthConfig(registrytypes.AuthConfig(authConfig))
233+
encodedAuth, err := registrytypes.EncodeAuthConfig(registrytypes.AuthConfig{
234+
Username: authConfig.Username,
235+
Password: authConfig.Password,
236+
ServerAddress: authConfig.ServerAddress,
237+
238+
// TODO(thaJeztah): Are these expected to be included?
239+
Auth: authConfig.Auth,
240+
IdentityToken: authConfig.IdentityToken,
241+
RegistryToken: authConfig.RegistryToken,
242+
})
217243
if err != nil {
218244
return "", err
219245
}

cli/command/registry/login.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,30 @@ func loginWithDeviceCodeFlow(ctx context.Context, dockerCLI command.Cli) (msg st
259259
return "", err
260260
}
261261

262-
response, err := loginWithRegistry(ctx, dockerCLI.Client(), registrytypes.AuthConfig(*authConfig))
262+
response, err := loginWithRegistry(ctx, dockerCLI.Client(), registrytypes.AuthConfig{
263+
Username: authConfig.Username,
264+
Password: authConfig.Password,
265+
ServerAddress: authConfig.ServerAddress,
266+
267+
// TODO(thaJeztah): Are these expected to be included?
268+
Auth: authConfig.Auth,
269+
IdentityToken: authConfig.IdentityToken,
270+
RegistryToken: authConfig.RegistryToken,
271+
})
263272
if err != nil {
264273
return "", err
265274
}
266275

267-
if err = storeCredentials(dockerCLI.ConfigFile(), registrytypes.AuthConfig(*authConfig)); err != nil {
276+
if err = storeCredentials(dockerCLI.ConfigFile(), registrytypes.AuthConfig{
277+
Username: authConfig.Username,
278+
Password: authConfig.Password,
279+
ServerAddress: authConfig.ServerAddress,
280+
281+
// TODO(thaJeztah): Are these expected to be included?
282+
Auth: authConfig.Auth,
283+
IdentityToken: authConfig.IdentityToken,
284+
RegistryToken: authConfig.RegistryToken,
285+
}); err != nil {
268286
return "", err
269287
}
270288

@@ -273,7 +291,16 @@ func loginWithDeviceCodeFlow(ctx context.Context, dockerCLI command.Cli) (msg st
273291

274292
func storeCredentials(cfg *configfile.ConfigFile, authConfig registrytypes.AuthConfig) error {
275293
creds := cfg.GetCredentialsStore(authConfig.ServerAddress)
276-
if err := creds.Store(configtypes.AuthConfig(authConfig)); err != nil {
294+
if err := creds.Store(configtypes.AuthConfig{
295+
Username: authConfig.Username,
296+
Password: authConfig.Password,
297+
ServerAddress: authConfig.ServerAddress,
298+
299+
// TODO(thaJeztah): Are these expected to be included?
300+
Auth: authConfig.Auth,
301+
IdentityToken: authConfig.IdentityToken,
302+
RegistryToken: authConfig.RegistryToken,
303+
}); err != nil {
277304
return errors.Errorf("Error saving credentials: %v", err)
278305
}
279306

cli/command/registry/search.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ func getAuth(dockerCLI command.Cli, reposName string) (encodedAuth string, err e
102102
// "no credentials found"). We'll get an error when search failed,
103103
// so fine to ignore in most situations.
104104
authConfig, _ := dockerCLI.ConfigFile().GetAuthConfig(authCfgKey)
105-
return registrytypes.EncodeAuthConfig(registrytypes.AuthConfig(authConfig))
105+
return registrytypes.EncodeAuthConfig(registrytypes.AuthConfig{
106+
Username: authConfig.Username,
107+
Password: authConfig.Password,
108+
ServerAddress: authConfig.ServerAddress,
109+
110+
// TODO(thaJeztah): Are these expected to be included?
111+
Auth: authConfig.Auth,
112+
IdentityToken: authConfig.IdentityToken,
113+
RegistryToken: authConfig.RegistryToken,
114+
})
106115
}
107116

108117
// splitReposSearchTerm breaks a search term into an index name and remote name

cli/command/registry_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,17 @@ func TestGetDefaultAuthConfig(t *testing.T) {
5858
},
5959
}
6060
cfg := configfile.New("filename")
61-
for _, authconfig := range testAuthConfigs {
62-
assert.Check(t, cfg.GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig)))
61+
for _, authConfig := range testAuthConfigs {
62+
assert.Check(t, cfg.GetCredentialsStore(authConfig.ServerAddress).Store(configtypes.AuthConfig{
63+
Username: authConfig.Username,
64+
Password: authConfig.Password,
65+
ServerAddress: authConfig.ServerAddress,
66+
67+
// TODO(thaJeztah): Are these expected to be included?
68+
Auth: authConfig.Auth,
69+
IdentityToken: authConfig.IdentityToken,
70+
RegistryToken: authConfig.RegistryToken,
71+
}))
6372
}
6473
for _, tc := range testCases {
6574
serverAddress := tc.inputServerAddress

0 commit comments

Comments
 (0)