Skip to content

Commit 79141ce

Browse files
committed
cli/command: remove uses of GetAuthConfigKey, ParseRepositoryInfo
Re-implement locally, based on the code in github.com/docker/docker/registry, but leaving out bits that are not used on the client-side, such as configuration of Mirrors, and configurable insecure-registry, which are not used on the client side. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 0442a73 commit 79141ce

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

cli/command/registry.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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,16 +27,22 @@ const (
2827
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
2928
)
3029

30+
// authConfigKey is the key used to store credentials for Docker Hub. It is
31+
// a copy of [registry.IndexServer].
32+
//
33+
// [registry.IndexServer]: https://pkg.go.dev/github.com/docker/docker/registry#IndexServer
34+
const authConfigKey = "https:/index.docker.io/v1/"
35+
3136
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
3237
// for the given command.
3338
func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInfo, cmdName string) registrytypes.RequestAuthConfig {
39+
configKey := getAuthConfigKey(index.Name)
40+
isDefaultRegistry := configKey == authConfigKey || index.Official
3441
return func(ctx context.Context) (string, error) {
3542
_, _ = fmt.Fprintf(cli.Out(), "\nLogin prior to %s:\n", cmdName)
36-
indexServer := registry.GetAuthConfigKey(index)
37-
isDefaultRegistry := indexServer == registry.IndexServer
38-
authConfig, err := GetDefaultAuthConfig(cli.ConfigFile(), true, indexServer, isDefaultRegistry)
43+
authConfig, err := GetDefaultAuthConfig(cli.ConfigFile(), true, configKey, isDefaultRegistry)
3944
if err != nil {
40-
_, _ = fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", indexServer, err)
45+
_, _ = fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", authConfigKey, err)
4146
}
4247

4348
select {
@@ -46,7 +51,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
4651
default:
4752
}
4853

49-
authConfig, err = PromptUserForCredentials(ctx, cli, "", "", authConfig.Username, indexServer)
54+
authConfig, err = PromptUserForCredentials(ctx, cli, "", "", authConfig.Username, authConfigKey)
5055
if err != nil {
5156
return "", err
5257
}
@@ -63,7 +68,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
6368
func ResolveAuthConfig(cfg *configfile.ConfigFile, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
6469
configKey := index.Name
6570
if index.Official {
66-
configKey = registry.IndexServer
71+
configKey = authConfigKey
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 == authConfigKey {
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.
@@ -225,9 +230,25 @@ func resolveAuthConfigFromImage(cfg *configfile.ConfigFile, image string) (regis
225230
if err != nil {
226231
return registrytypes.AuthConfig{}, err
227232
}
228-
repoInfo, err := registry.ParseRepositoryInfo(registryRef)
233+
configKey := getAuthConfigKey(reference.Domain(registryRef))
234+
a, err := cfg.GetAuthConfig(configKey)
229235
if err != nil {
230236
return registrytypes.AuthConfig{}, err
231237
}
232-
return ResolveAuthConfig(cfg, repoInfo.Index), nil
238+
return registrytypes.AuthConfig(a), nil
239+
}
240+
241+
// getAuthConfigKey special-cases using the full index address of the official
242+
// index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
243+
//
244+
// It is similar to [registry.GetAuthConfigKey], but does not require on
245+
// [registrytypes.IndexInfo] as intermediate.
246+
//
247+
// [registry.GetAuthConfigKey]: https://pkg.go.dev/github.com/docker/docker/registry#GetAuthConfigKey
248+
// [registrytypes.IndexInfo]:https://pkg.go.dev/github.com/docker/docker/api/types/registry#IndexInfo
249+
func getAuthConfigKey(domainName string) string {
250+
if domainName == "docker.io" || domainName == "index.docker.io" {
251+
return authConfigKey
252+
}
253+
return domainName
233254
}

0 commit comments

Comments
 (0)