Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (

"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
configtypes "github.com/docker/cli/cli/config/types"
dcontext "github.com/docker/cli/cli/context"
"github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/store"
"github.com/docker/cli/cli/debug"
cliflags "github.com/docker/cli/cli/flags"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/cli/version"
"github.com/docker/cli/internal/secretsengine"
dopts "github.com/docker/cli/opts"
"github.com/moby/moby/api/types/build"
"github.com/moby/moby/client"
Expand Down Expand Up @@ -75,6 +77,8 @@ type DockerCli struct {
contextStoreConfig *store.Config
initTimeout time.Duration
res telemetryResource
registryAuthOnce sync.Once
registryAuth secretsengine.AuthProvider

// baseCtx is the base context used for internal operations. In the future
// this may be replaced by explicitly passing a context to functions that
Expand Down Expand Up @@ -141,6 +145,21 @@ func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
return cli.configFile
}

func (cli *DockerCli) RegistryAuthProvider() secretsengine.AuthProvider {
cli.registryAuthOnce.Do(func() {
cli.registryAuth = secretsengine.NewDesktop(configFileAuth{cli})
})
return cli.registryAuth
}

type configFileAuth struct {
cli *DockerCli
}

func (c configFileAuth) GetAuthConfig(registryHostname string) (configtypes.AuthConfig, error) {
return c.cli.ConfigFile().GetAuthConfig(registryHostname)
}

// ServerInfo returns the server version details for the host this client is
// connected to
func (cli *DockerCli) ServerInfo() ServerInfo {
Expand Down
15 changes: 15 additions & 0 deletions cli/command/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/cli/context/store"
"github.com/docker/cli/cli/flags"
"github.com/moby/moby/client"
Expand Down Expand Up @@ -501,3 +502,17 @@ func TestNewDockerCliWithAPIClientOptions(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, received, "fake-agent/0.0.1")
}

func TestRegistryAuthProvider(t *testing.T) {
t.Parallel()
cli := &DockerCli{configFile: &configfile.ConfigFile{}}
p := cli.RegistryAuthProvider()
assert.Assert(t, p == cli.RegistryAuthProvider(), "the provider must be created once per DockerCli")

cli.configFile = &configfile.ConfigFile{AuthConfigs: map[string]configtypes.AuthConfig{
"ghcr.io": {Username: "user", Password: "pass"},
}}
got, err := p.GetAuthConfig("ghcr.io")
assert.NilError(t, err)
assert.DeepEqual(t, got, configtypes.AuthConfig{Username: "user", Password: "pass"})
}
3 changes: 2 additions & 1 deletion cli/command/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/docker/cli/cli/config/types"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/internal/jsonstream"
"github.com/docker/cli/internal/registryauth"
"github.com/docker/cli/opts"
"github.com/moby/moby/api/types/mount"
"github.com/moby/moby/client"
Expand Down Expand Up @@ -133,7 +134,7 @@ func runCreate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet,
}

func pullImage(ctx context.Context, dockerCLI command.Cli, img string, options *createOptions) error {
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), img)
encodedAuth, err := registryauth.EncodedAuth(dockerCLI, img)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/image/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/internal/jsonstream"
"github.com/docker/cli/internal/registryauth"
"github.com/moby/moby/client"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -90,7 +91,7 @@ func runPull(ctx context.Context, dockerCLI command.Cli, opts pullOptions) error
ociPlatforms = append(ociPlatforms, p)
}

encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), distributionRef.String())
encodedAuth, err := registryauth.EncodedAuth(dockerCLI, distributionRef.String())
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/image/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/internal/jsonstream"
"github.com/docker/cli/internal/registryauth"
"github.com/docker/cli/internal/tui"
"github.com/moby/moby/api/types/auxprogress"
"github.com/moby/moby/client"
Expand Down Expand Up @@ -108,7 +109,7 @@ To push the complete multi-platform image, remove the --platform flag.
}

// Resolve the Auth config relevant for this server
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), ref.String())
encodedAuth, err := registryauth.EncodedAuth(dockerCli, ref.String())
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/plugin/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/jsonstream"
"github.com/docker/cli/internal/prompt"
"github.com/docker/cli/internal/registryauth"
"github.com/moby/moby/api/types/plugin"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,7 +61,7 @@ func buildPullConfig(dockerCLI command.Cli, opts pluginOptions) (client.PluginIn
return client.PluginInstallOptions{}, err
}

encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), ref.String())
encodedAuth, err := registryauth.EncodedAuth(dockerCLI, ref.String())
if err != nil {
return client.PluginInstallOptions{}, err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/plugin/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/jsonstream"
"github.com/docker/cli/internal/registryauth"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func runPush(ctx context.Context, dockerCli command.Cli, name string) error {
}

named = reference.TagNameOnly(named)
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), named.String())
encodedAuth, err := registryauth.EncodedAuth(dockerCli, named.String())
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/internal/registryauth"
cliopts "github.com/docker/cli/opts"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
Expand Down Expand Up @@ -120,7 +121,7 @@ func runCreate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet,
if opts.registryAuth {
// Retrieve encoded auth token from the image reference
var err error
encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), opts.image)
encodedAuth, err = registryauth.EncodedAuth(dockerCLI, opts.image)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/internal/registryauth"
"github.com/docker/cli/opts"
"github.com/docker/cli/opts/swarmopts"
"github.com/moby/moby/api/types/container"
Expand Down Expand Up @@ -223,7 +224,7 @@ func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet,
// Retrieve encoded auth token from the image reference
// This would be the old image if it didn't change in this update
var err error
encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), spec.TaskTemplate.ContainerSpec.Image)
encodedAuth, err = registryauth.EncodedAuth(dockerCLI, spec.TaskTemplate.ContainerSpec.Image)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/stack/deploy_composefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/cli/cli/command/service"
"github.com/docker/cli/cli/compose/convert"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/docker/cli/internal/registryauth"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
"github.com/moby/moby/api/types/swarm"
Expand Down Expand Up @@ -224,7 +225,7 @@ func deployServices(ctx context.Context, dockerCLI command.Cli, services map[str

if sendAuth {
// Retrieve encoded auth token from the image reference
encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), image)
encodedAuth, err = registryauth.EncodedAuth(dockerCLI, image)
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/commandline/login.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ With Docker Desktop, the credential store is already installed and configured
for you. Unless you want to change the credential store used by Docker Desktop,
you can skip the following steps.

#### Docker Desktop sign-in

When Docker Desktop is running and you are signed in, commands that pull or
push images through the Docker Engine, such as `docker pull`, `docker push`,
`docker run`, and `docker service create`, use the Docker Desktop session for
Docker Hub (`docker.io`) and Docker Hardened Images (`dhi.io`). The session is
used before the credential store, credential helpers, and the configuration
file. Credentials set in the `DOCKER_AUTH_CONFIG` environment variable take
precedence over the Docker Desktop session. If Docker Desktop is not installed
or not running, or you are not signed in, the Docker CLI uses its own
credentials.

#### Configure the credential store

You need to specify the credential store in `$HOME/.docker/config.json`
Expand Down
5 changes: 3 additions & 2 deletions internal/registry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const (
// IndexServer is used for user auth and image search
IndexServer = "https://index.docker.io/v1/"
// IndexName is the name of the index
IndexName = "docker.io"
IndexName = "docker.io"
DefaultRegistryHost = "registry-1.docker.io"
)

var (
Expand All @@ -61,7 +62,7 @@ var (
// or "ubuntu:latest"), as well as references using the "docker.io" domain
// name, which is used as canonical reference for images on Docker Hub, but
// does not match the domain-name of Docker Hub's registry.
DefaultV2Registry = &url.URL{Scheme: "https", Host: "registry-1.docker.io"}
DefaultV2Registry = &url.URL{Scheme: "https", Host: DefaultRegistryHost}

validHostPortRegex = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)
Expand Down
62 changes: 62 additions & 0 deletions internal/registryauth/registryauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package registryauth

import (
"os"

"github.com/distribution/reference"
"github.com/docker/cli/cli/config/configfile"
configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/internal/secretsengine"
"github.com/moby/moby/api/pkg/authconfig"
registrytypes "github.com/moby/moby/api/types/registry"
)

type configProvider interface {
ConfigFile() *configfile.ConfigFile
}

type authProvider interface {
RegistryAuthProvider() secretsengine.AuthProvider
}

type envAuthFunc func(registryHostname string) (configtypes.AuthConfig, bool)

func EncodedAuth(dockerCLI configProvider, image string) (string, error) {
var auth secretsengine.AuthProvider = dockerCLI.ConfigFile()
if p, ok := dockerCLI.(authProvider); ok {
auth = p.RegistryAuthProvider()
}
return encodedAuth(envAuthConfig, auth, image)
}

func encodedAuth(envAuth envAuthFunc, auth secretsengine.AuthProvider, image string) (string, error) {
registryRef, err := reference.ParseNormalizedNamed(image)
if err != nil {
return "", err
}
domain := reference.Domain(registryRef)
authConfig, ok := envAuth(domain)
if !ok {
authConfig, err = auth.GetAuthConfig(domain)
if err != nil {
return "", err
}
}

return authconfig.Encode(registrytypes.AuthConfig{
Username: authConfig.Username,
Password: authConfig.Password,
ServerAddress: authConfig.ServerAddress,
Auth: authConfig.Auth,
IdentityToken: authConfig.IdentityToken,
RegistryToken: authConfig.RegistryToken,
})
}

func envAuthConfig(registryHostname string) (configtypes.AuthConfig, bool) {
if os.Getenv(configfile.DockerEnvConfigKey) == "" {
return configtypes.AuthConfig{}, false
}
authConfig, err := configfile.New("").GetAuthConfig(registryHostname)
return authConfig, err == nil && authConfig.Username != ""
}
Loading
Loading