|
@@ -20,15 +20,14 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
// ElectAuthServer returns the default registry to use (by asking the daemon)
|
|
// ElectAuthServer returns the default registry to use (by asking the daemon)
|
|
-// TODO: only used in registry package and from ResolveAuthConfig
|
|
|
|
-func (cli *DockerCli) ElectAuthServer(ctx context.Context) string {
|
|
|
|
|
|
+func ElectAuthServer(ctx context.Context, cli *DockerCli) string {
|
|
// The daemon `/info` endpoint informs us of the default registry being
|
|
// The daemon `/info` endpoint informs us of the default registry being
|
|
// used. This is essential in cross-platforms environment, where for
|
|
// used. This is essential in cross-platforms environment, where for
|
|
// example a Linux client might be interacting with a Windows daemon, hence
|
|
// example a Linux client might be interacting with a Windows daemon, hence
|
|
// the default registry URL might be Windows specific.
|
|
// the default registry URL might be Windows specific.
|
|
serverAddress := registry.IndexServer
|
|
serverAddress := registry.IndexServer
|
|
- if info, err := cli.client.Info(ctx); err != nil {
|
|
|
|
- fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
|
|
|
|
|
|
+ if info, err := cli.Client().Info(ctx); err != nil {
|
|
|
|
+ fmt.Fprintf(cli.Out(), "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
|
|
} else {
|
|
} else {
|
|
serverAddress = info.IndexServerAddress
|
|
serverAddress = info.IndexServerAddress
|
|
}
|
|
}
|
|
@@ -46,13 +45,12 @@ func EncodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
|
|
|
|
|
|
// 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.
|
|
-// TODO: image/plugin
|
|
|
|
-func (cli *DockerCli) RegistryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) types.RequestPrivilegeFunc {
|
|
|
|
|
|
+func RegistryAuthenticationPrivilegedFunc(cli *DockerCli, index *registrytypes.IndexInfo, cmdName string) types.RequestPrivilegeFunc {
|
|
return func() (string, error) {
|
|
return func() (string, error) {
|
|
- fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
|
|
|
|
|
|
+ fmt.Fprintf(cli.Out(), "\nPlease login prior to %s:\n", cmdName)
|
|
indexServer := registry.GetAuthConfigKey(index)
|
|
indexServer := registry.GetAuthConfigKey(index)
|
|
- isDefaultRegistry := indexServer == cli.ElectAuthServer(context.Background())
|
|
|
|
- authConfig, err := cli.ConfigureAuth("", "", indexServer, isDefaultRegistry)
|
|
|
|
|
|
+ isDefaultRegistry := indexServer == ElectAuthServer(context.Background(), cli)
|
|
|
|
+ authConfig, err := ConfigureAuth(cli, "", "", indexServer, isDefaultRegistry)
|
|
if err != nil {
|
|
if err != nil {
|
|
return "", err
|
|
return "", err
|
|
}
|
|
}
|
|
@@ -63,11 +61,10 @@ func (cli *DockerCli) RegistryAuthenticationPrivilegedFunc(index *registrytypes.
|
|
// ResolveAuthConfig is like registry.ResolveAuthConfig, but if using the
|
|
// ResolveAuthConfig is like registry.ResolveAuthConfig, but if using the
|
|
// default index, it uses the default index name for the daemon's platform,
|
|
// default index, it uses the default index name for the daemon's platform,
|
|
// not the client's platform.
|
|
// not the client's platform.
|
|
-// TODO: plugin/image/container and from RetrieveAuthTokenFromImage
|
|
|
|
-func (cli *DockerCli) ResolveAuthConfig(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
|
|
|
|
|
|
+func ResolveAuthConfig(ctx context.Context, cli *DockerCli, index *registrytypes.IndexInfo) types.AuthConfig {
|
|
configKey := index.Name
|
|
configKey := index.Name
|
|
if index.Official {
|
|
if index.Official {
|
|
- configKey = cli.ElectAuthServer(ctx)
|
|
|
|
|
|
+ configKey = ElectAuthServer(ctx, cli)
|
|
}
|
|
}
|
|
|
|
|
|
a, _ := cli.CredentialsStore().Get(configKey)
|
|
a, _ := cli.CredentialsStore().Get(configKey)
|
|
@@ -75,8 +72,7 @@ func (cli *DockerCli) ResolveAuthConfig(ctx context.Context, index *registrytype
|
|
}
|
|
}
|
|
|
|
|
|
// ConfigureAuth returns an AuthConfig from the specified user, password and server.
|
|
// ConfigureAuth returns an AuthConfig from the specified user, password and server.
|
|
-// TODO: only used in registry package
|
|
|
|
-func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, isDefaultRegistry bool) (types.AuthConfig, error) {
|
|
|
|
|
|
+func ConfigureAuth(cli *DockerCli, flUser, flPassword, serverAddress string, isDefaultRegistry bool) (types.AuthConfig, error) {
|
|
// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
|
|
// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
|
|
if runtime.GOOS == "windows" {
|
|
if runtime.GOOS == "windows" {
|
|
cli.in = NewInStream(os.Stdin)
|
|
cli.in = NewInStream(os.Stdin)
|
|
@@ -107,10 +103,10 @@ func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, is
|
|
if flUser = strings.TrimSpace(flUser); flUser == "" {
|
|
if flUser = strings.TrimSpace(flUser); flUser == "" {
|
|
if isDefaultRegistry {
|
|
if isDefaultRegistry {
|
|
// if this is a default registry (docker hub), then display the following message.
|
|
// if this is a default registry (docker hub), then display the following message.
|
|
- fmt.Fprintln(cli.out, "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.")
|
|
|
|
|
|
+ fmt.Fprintln(cli.Out(), "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.")
|
|
}
|
|
}
|
|
- cli.promptWithDefault("Username", authconfig.Username)
|
|
|
|
- flUser = readInput(cli.in, cli.out)
|
|
|
|
|
|
+ promptWithDefault(cli.Out(), "Username", authconfig.Username)
|
|
|
|
+ flUser = readInput(cli.In(), cli.Out())
|
|
flUser = strings.TrimSpace(flUser)
|
|
flUser = strings.TrimSpace(flUser)
|
|
if flUser == "" {
|
|
if flUser == "" {
|
|
flUser = authconfig.Username
|
|
flUser = authconfig.Username
|
|
@@ -124,11 +120,11 @@ func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, is
|
|
if err != nil {
|
|
if err != nil {
|
|
return authconfig, err
|
|
return authconfig, err
|
|
}
|
|
}
|
|
- fmt.Fprintf(cli.out, "Password: ")
|
|
|
|
|
|
+ fmt.Fprintf(cli.Out(), "Password: ")
|
|
term.DisableEcho(cli.In().FD(), oldState)
|
|
term.DisableEcho(cli.In().FD(), oldState)
|
|
|
|
|
|
- flPassword = readInput(cli.in, cli.out)
|
|
|
|
- fmt.Fprint(cli.out, "\n")
|
|
|
|
|
|
+ flPassword = readInput(cli.In(), cli.Out())
|
|
|
|
+ fmt.Fprint(cli.Out(), "\n")
|
|
|
|
|
|
term.RestoreTerminal(cli.In().FD(), oldState)
|
|
term.RestoreTerminal(cli.In().FD(), oldState)
|
|
if flPassword == "" {
|
|
if flPassword == "" {
|
|
@@ -154,19 +150,18 @@ func readInput(in io.Reader, out io.Writer) string {
|
|
return string(line)
|
|
return string(line)
|
|
}
|
|
}
|
|
|
|
|
|
-func (cli *DockerCli) promptWithDefault(prompt string, configDefault string) {
|
|
|
|
|
|
+func promptWithDefault(out io.Writer, prompt string, configDefault string) {
|
|
if configDefault == "" {
|
|
if configDefault == "" {
|
|
- fmt.Fprintf(cli.out, "%s: ", prompt)
|
|
|
|
|
|
+ fmt.Fprintf(out, "%s: ", prompt)
|
|
} else {
|
|
} else {
|
|
- fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
|
|
|
|
|
|
+ fmt.Fprintf(out, "%s (%s): ", prompt, configDefault)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete image
|
|
// RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete image
|
|
-// TODO: used in service/stack packages
|
|
|
|
-func (cli *DockerCli) RetrieveAuthTokenFromImage(ctx context.Context, image string) (string, error) {
|
|
|
|
|
|
+func RetrieveAuthTokenFromImage(ctx context.Context, cli *DockerCli, image string) (string, error) {
|
|
// Retrieve encoded auth token from the image reference
|
|
// Retrieve encoded auth token from the image reference
|
|
- authConfig, err := cli.resolveAuthConfigFromImage(ctx, image)
|
|
|
|
|
|
+ authConfig, err := resolveAuthConfigFromImage(ctx, cli, image)
|
|
if err != nil {
|
|
if err != nil {
|
|
return "", err
|
|
return "", err
|
|
}
|
|
}
|
|
@@ -178,7 +173,7 @@ func (cli *DockerCli) RetrieveAuthTokenFromImage(ctx context.Context, image stri
|
|
}
|
|
}
|
|
|
|
|
|
// resolveAuthConfigFromImage retrieves that AuthConfig using the image string
|
|
// resolveAuthConfigFromImage retrieves that AuthConfig using the image string
|
|
-func (cli *DockerCli) resolveAuthConfigFromImage(ctx context.Context, image string) (types.AuthConfig, error) {
|
|
|
|
|
|
+func resolveAuthConfigFromImage(ctx context.Context, cli *DockerCli, image string) (types.AuthConfig, error) {
|
|
registryRef, err := reference.ParseNamed(image)
|
|
registryRef, err := reference.ParseNamed(image)
|
|
if err != nil {
|
|
if err != nil {
|
|
return types.AuthConfig{}, err
|
|
return types.AuthConfig{}, err
|
|
@@ -187,6 +182,5 @@ func (cli *DockerCli) resolveAuthConfigFromImage(ctx context.Context, image stri
|
|
if err != nil {
|
|
if err != nil {
|
|
return types.AuthConfig{}, err
|
|
return types.AuthConfig{}, err
|
|
}
|
|
}
|
|
- authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
|
|
|
|
- return authConfig, nil
|
|
|
|
|
|
+ return ResolveAuthConfig(ctx, cli, repoInfo.Index), nil
|
|
}
|
|
}
|