Merge pull request #5942 from thaJeztah/simplify_auth_step1
cli/command: remove uses of GetAuthConfigKey, ParseRepositoryInfo
This commit is contained in:
commit
e002576821
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/docker/cli/cli/streams"
|
"github.com/docker/cli/cli/streams"
|
||||||
"github.com/docker/cli/internal/tui"
|
"github.com/docker/cli/internal/tui"
|
||||||
registrytypes "github.com/docker/docker/api/types/registry"
|
registrytypes "github.com/docker/docker/api/types/registry"
|
||||||
"github.com/docker/docker/registry"
|
|
||||||
"github.com/morikuni/aec"
|
"github.com/morikuni/aec"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
@ -28,16 +27,22 @@ const (
|
|||||||
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
|
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// authConfigKey is the key used to store credentials for Docker Hub. It is
|
||||||
|
// a copy of [registry.IndexServer].
|
||||||
|
//
|
||||||
|
// [registry.IndexServer]: https://pkg.go.dev/github.com/docker/docker/registry#IndexServer
|
||||||
|
const authConfigKey = "https:/index.docker.io/v1/"
|
||||||
|
|
||||||
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
|
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
|
||||||
// for the given command.
|
// for the given command.
|
||||||
func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInfo, cmdName string) registrytypes.RequestAuthConfig {
|
func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInfo, cmdName string) registrytypes.RequestAuthConfig {
|
||||||
|
configKey := getAuthConfigKey(index.Name)
|
||||||
|
isDefaultRegistry := configKey == authConfigKey || index.Official
|
||||||
return func(ctx context.Context) (string, error) {
|
return func(ctx context.Context) (string, error) {
|
||||||
_, _ = fmt.Fprintf(cli.Out(), "\nLogin prior to %s:\n", cmdName)
|
_, _ = fmt.Fprintf(cli.Out(), "\nLogin prior to %s:\n", cmdName)
|
||||||
indexServer := registry.GetAuthConfigKey(index)
|
authConfig, err := GetDefaultAuthConfig(cli.ConfigFile(), true, configKey, isDefaultRegistry)
|
||||||
isDefaultRegistry := indexServer == registry.IndexServer
|
|
||||||
authConfig, err := GetDefaultAuthConfig(cli.ConfigFile(), true, indexServer, isDefaultRegistry)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _ = fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", indexServer, err)
|
_, _ = fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", authConfigKey, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -46,7 +51,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
authConfig, err = PromptUserForCredentials(ctx, cli, "", "", authConfig.Username, indexServer)
|
authConfig, err = PromptUserForCredentials(ctx, cli, "", "", authConfig.Username, authConfigKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -63,7 +68,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
|
|||||||
func ResolveAuthConfig(cfg *configfile.ConfigFile, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
|
func ResolveAuthConfig(cfg *configfile.ConfigFile, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
|
||||||
configKey := index.Name
|
configKey := index.Name
|
||||||
if index.Official {
|
if index.Official {
|
||||||
configKey = registry.IndexServer
|
configKey = authConfigKey
|
||||||
}
|
}
|
||||||
|
|
||||||
a, _ := cfg.GetAuthConfig(configKey)
|
a, _ := cfg.GetAuthConfig(configKey)
|
||||||
@ -132,7 +137,7 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword
|
|||||||
|
|
||||||
argUser = strings.TrimSpace(argUser)
|
argUser = strings.TrimSpace(argUser)
|
||||||
if argUser == "" {
|
if argUser == "" {
|
||||||
if serverAddress == registry.IndexServer {
|
if serverAddress == authConfigKey {
|
||||||
// When signing in to the default (Docker Hub) registry, we display
|
// When signing in to the default (Docker Hub) registry, we display
|
||||||
// hints for creating an account, and (if hints are enabled), using
|
// hints for creating an account, and (if hints are enabled), using
|
||||||
// a token instead of a password.
|
// a token instead of a password.
|
||||||
@ -225,9 +230,25 @@ func resolveAuthConfigFromImage(cfg *configfile.ConfigFile, image string) (regis
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return registrytypes.AuthConfig{}, err
|
return registrytypes.AuthConfig{}, err
|
||||||
}
|
}
|
||||||
repoInfo, err := registry.ParseRepositoryInfo(registryRef)
|
configKey := getAuthConfigKey(reference.Domain(registryRef))
|
||||||
|
a, err := cfg.GetAuthConfig(configKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return registrytypes.AuthConfig{}, err
|
return registrytypes.AuthConfig{}, err
|
||||||
}
|
}
|
||||||
return ResolveAuthConfig(cfg, repoInfo.Index), nil
|
return registrytypes.AuthConfig(a), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getAuthConfigKey special-cases using the full index address of the official
|
||||||
|
// index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
|
||||||
|
//
|
||||||
|
// It is similar to [registry.GetAuthConfigKey], but does not require on
|
||||||
|
// [registrytypes.IndexInfo] as intermediate.
|
||||||
|
//
|
||||||
|
// [registry.GetAuthConfigKey]: https://pkg.go.dev/github.com/docker/docker/registry#GetAuthConfigKey
|
||||||
|
// [registrytypes.IndexInfo]:https://pkg.go.dev/github.com/docker/docker/api/types/registry#IndexInfo
|
||||||
|
func getAuthConfigKey(domainName string) string {
|
||||||
|
if domainName == "docker.io" || domainName == "index.docker.io" {
|
||||||
|
return authConfigKey
|
||||||
|
}
|
||||||
|
return domainName
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user