Skip to content

Commit 7a692f3

Browse files
committed
cli/config/configfile: normalize hostname when resolving auth
Previously, normalization was done before calling these functions, which required implementations to normalize before using. Move the normalization into the GetAuthConfig, GetCredentialsStore, so that non-normalized hostnames will be able to resolve the correct auth. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 5927d80 commit 7a692f3

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

cli/command/registry.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (strin
194194
if err != nil {
195195
return "", err
196196
}
197-
configKey := getAuthConfigKey(reference.Domain(registryRef))
198-
authConfig, err := cfg.GetAuthConfig(configKey)
197+
authConfig, err := cfg.GetAuthConfig(reference.Domain(registryRef))
199198
if err != nil {
200199
return "", err
201200
}
@@ -211,18 +210,3 @@ func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (strin
211210
RegistryToken: authConfig.RegistryToken,
212211
})
213212
}
214-
215-
// getAuthConfigKey special-cases using the full index address of the official
216-
// index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
217-
//
218-
// It is similar to [registry.GetAuthConfigKey], but does not require on
219-
// [registrytypes.IndexInfo] as intermediate.
220-
//
221-
// [registry.GetAuthConfigKey]: https://pkg.go.dev/github.com/docker/docker@v28.3.3+incompatible/registry#GetAuthConfigKey
222-
// [registrytypes.IndexInfo]: https://pkg.go.dev/github.com/docker/docker@v28.3.3+incompatible/api/types/registry#IndexInfo
223-
func getAuthConfigKey(domainName string) string {
224-
if domainName == "docker.io" || domainName == "index.docker.io" {
225-
return authConfigKey
226-
}
227-
return domainName
228-
}

cli/config/configfile/file.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,34 @@ import (
2020
"github.com/sirupsen/logrus"
2121
)
2222

23+
// authConfigKey is the key used to store credentials for Docker Hub. It is
24+
// a copy of [registry.IndexServer].
25+
//
26+
// [registry.IndexServer]: https://pkg.go.dev/github.com/docker/docker@v28.5.1+incompatible/registry#IndexServer
27+
const authConfigKey = "https://index.docker.io/v1/"
28+
29+
// getAuthConfigKey returns the canonical key used to look up stored
30+
// registry credentials for the given registry domain.
31+
//
32+
// For the official Docker Hub registry ("docker.io"), credentials are stored
33+
// under the historical full index address ("https://index.docker.io/v1/").
34+
//
35+
// For all other registries, the input is domainName to already be a normalized
36+
// hostname (optionally including ":port") and is returned unchanged.
37+
//
38+
// This function performs key normalization only; it does not validate or parse
39+
// the input.
40+
//
41+
// It is similar to [registry.GetAuthConfigKey] in the daemon.
42+
//
43+
// [registry.GetAuthConfigKey]: https://pkg.go.dev/github.com/docker/docker@v28.5.1+incompatible/registry#GetAuthConfigKey
44+
func getAuthConfigKey(domainName string) string {
45+
if domainName == "docker.io" || domainName == "index.docker.io" {
46+
return authConfigKey
47+
}
48+
return domainName
49+
}
50+
2351
// ConfigFile ~/.docker/config.json file info
2452
type ConfigFile struct {
2553
AuthConfigs map[string]types.AuthConfig `json:"auths"`
@@ -293,7 +321,7 @@ func decodeAuth(authStr string) (string, string, error) {
293321
func (configFile *ConfigFile) GetCredentialsStore(registryHostname string) credentials.Store {
294322
store := credentials.NewFileStore(configFile)
295323

296-
if helper := getConfiguredCredentialStore(configFile, registryHostname); helper != "" {
324+
if helper := getConfiguredCredentialStore(configFile, getAuthConfigKey(registryHostname)); helper != "" {
297325
store = newNativeStore(configFile, helper)
298326
}
299327

@@ -358,7 +386,8 @@ var newNativeStore = func(configFile *ConfigFile, helperSuffix string) credentia
358386

359387
// GetAuthConfig for a repository from the credential store
360388
func (configFile *ConfigFile) GetAuthConfig(registryHostname string) (types.AuthConfig, error) {
361-
return configFile.GetCredentialsStore(registryHostname).Get(registryHostname)
389+
acKey := getAuthConfigKey(registryHostname)
390+
return configFile.GetCredentialsStore(acKey).Get(acKey)
362391
}
363392

364393
// getConfiguredCredentialStore returns the credential helper configured for the

0 commit comments

Comments
 (0)