diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 122d15e2e149..92e14ca444b4 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -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" @@ -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.Encoded(ctx, dockerCLI.ConfigFile(), img) if err != nil { return err } diff --git a/cli/command/image/pull.go b/cli/command/image/pull.go index 7485d21a6880..d45434aa12ba 100644 --- a/cli/command/image/pull.go +++ b/cli/command/image/pull.go @@ -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" @@ -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.Encoded(ctx, dockerCLI.ConfigFile(), distributionRef.String()) if err != nil { return err } diff --git a/cli/command/image/push.go b/cli/command/image/push.go index 87e2f7dea4c4..4fe434d83908 100644 --- a/cli/command/image/push.go +++ b/cli/command/image/push.go @@ -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" @@ -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.Encoded(ctx, dockerCli.ConfigFile(), ref.String()) if err != nil { return err } diff --git a/cli/command/plugin/install.go b/cli/command/plugin/install.go index 7d2d9aafaeb0..a44195b76f61 100644 --- a/cli/command/plugin/install.go +++ b/cli/command/plugin/install.go @@ -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" @@ -51,7 +52,7 @@ func newInstallCommand(dockerCLI command.Cli) *cobra.Command { return cmd } -func buildPullConfig(dockerCLI command.Cli, opts pluginOptions) (client.PluginInstallOptions, error) { +func buildPullConfig(ctx context.Context, dockerCLI command.Cli, opts pluginOptions) (client.PluginInstallOptions, error) { // Names with both tag and digest will be treated by the daemon // as a pull by digest with a local name for the tag // (if no local name is provided). @@ -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.Encoded(ctx, dockerCLI.ConfigFile(), ref.String()) if err != nil { return client.PluginInstallOptions{}, err } @@ -89,7 +90,7 @@ func runInstall(ctx context.Context, dockerCLI command.Cli, opts pluginOptions) localName = reference.FamiliarString(reference.TagNameOnly(aref)) } - options, err := buildPullConfig(dockerCLI, opts) + options, err := buildPullConfig(ctx, dockerCLI, opts) if err != nil { return err } diff --git a/cli/command/plugin/push.go b/cli/command/plugin/push.go index af76db49ebef..ce471a0e73b8 100644 --- a/cli/command/plugin/push.go +++ b/cli/command/plugin/push.go @@ -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" ) @@ -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.Encoded(ctx, dockerCli.ConfigFile(), named.String()) if err != nil { return err } diff --git a/cli/command/plugin/upgrade.go b/cli/command/plugin/upgrade.go index 9053ce6e8980..322aed6f854d 100644 --- a/cli/command/plugin/upgrade.go +++ b/cli/command/plugin/upgrade.go @@ -78,7 +78,7 @@ func runUpgrade(ctx context.Context, dockerCLI command.Cli, opts pluginOptions) } } - options, err := buildPullConfig(dockerCLI, opts) + options, err := buildPullConfig(ctx, dockerCLI, opts) if err != nil { return err } diff --git a/cli/command/registry.go b/cli/command/registry.go index a949f8afed91..4690b3cdd523 100644 --- a/cli/command/registry.go +++ b/cli/command/registry.go @@ -8,15 +8,14 @@ import ( "runtime" "strings" - "github.com/distribution/reference" "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/config/credentials" configtypes "github.com/docker/cli/cli/config/types" "github.com/docker/cli/cli/hints" "github.com/docker/cli/cli/streams" "github.com/docker/cli/internal/prompt" + "github.com/docker/cli/internal/registryauth" "github.com/docker/cli/internal/tui" - "github.com/moby/moby/api/pkg/authconfig" registrytypes "github.com/moby/moby/api/types/registry" "github.com/morikuni/aec" ) @@ -196,23 +195,5 @@ func PromptUserForCredentials(ctx context.Context, cli Streams, argUser, argPass // // [RFC 4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5 func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (string, error) { - registryRef, err := reference.ParseNormalizedNamed(image) - if err != nil { - return "", err - } - authConfig, err := cfg.GetAuthConfig(reference.Domain(registryRef)) - if err != nil { - return "", err - } - - return authconfig.Encode(registrytypes.AuthConfig{ - Username: authConfig.Username, - Password: authConfig.Password, - ServerAddress: authConfig.ServerAddress, - - // TODO(thaJeztah): Are these expected to be included? - Auth: authConfig.Auth, - IdentityToken: authConfig.IdentityToken, - RegistryToken: authConfig.RegistryToken, - }) + return registryauth.Encoded(context.Background(), cfg, image) } diff --git a/cli/command/registry_test.go b/cli/command/registry_test.go index 751711f36391..8b94d2948830 100644 --- a/cli/command/registry_test.go +++ b/cli/command/registry_test.go @@ -1,15 +1,12 @@ package command_test import ( - "bytes" - "path" "path/filepath" "testing" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/config/configfile" configtypes "github.com/docker/cli/cli/config/types" - "github.com/moby/moby/api/pkg/authconfig" "github.com/moby/moby/api/types/registry" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -95,113 +92,3 @@ func TestGetDefaultAuthConfig_HelperError(t *testing.T) { assert.Check(t, is.DeepEqual(expectedAuthConfig, authCfg)) assert.Check(t, is.ErrorContains(err, "docker-credential-fake-does-not-exist")) } - -func TestRetrieveAuthTokenFromImage(t *testing.T) { - // configFileContent contains a plain-text "username:password", as stored by - // the plain-text store; - // https://github.com/docker/cli/blob/v28.0.4/cli/config/configfile/file.go#L218-L229 - const configFileContent = `{"auths": { - "https://index.docker.io/v1/": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "[::1]": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "[::1]:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "127.0.0.1": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "127.0.0.1:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "localhost": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "localhost:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "registry-1.docker.io": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, - "registry.hub.docker.com": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="} - } -}` - cfg := configfile.ConfigFile{} - err := cfg.LoadFromReader(bytes.NewReader([]byte(configFileContent))) - assert.NilError(t, err) - - remoteRefs := []string{ - "ubuntu", - "ubuntu:latest", - "ubuntu:latest@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", - "ubuntu@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", - "library/ubuntu", - "library/ubuntu:latest", - "library/ubuntu:latest@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", - "library/ubuntu@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", - } - - tests := []struct { - prefix string - expectedAddress string - expectedAuthCfg registry.AuthConfig - }{ - { - prefix: "", - expectedAddress: "https://index.docker.io/v1/", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, - }, - { - prefix: "docker.io", - expectedAddress: "https://index.docker.io/v1/", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, - }, - { - prefix: "index.docker.io", - expectedAddress: "https://index.docker.io/v1/", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, - }, - { - // FIXME(thaJeztah): registry-1.docker.io (the actual registry) is the odd one out, and is stored separate from other URLs used for docker hub's registry - prefix: "registry-1.docker.io", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry-1.docker.io"}, - }, - { - // FIXME(thaJeztah): registry.hub.docker.com is stored separate from other URLs used for docker hub's registry - prefix: "registry.hub.docker.com", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry.hub.docker.com"}, - }, - { - prefix: "[::1]", - expectedAddress: "[::1]", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "[::1]"}, - }, - { - prefix: "[::1]:5000", - expectedAddress: "[::1]:5000", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "[::1]:5000"}, - }, - { - prefix: "127.0.0.1", - expectedAddress: "127.0.0.1", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "127.0.0.1"}, - }, - { - prefix: "localhost", - expectedAddress: "localhost", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "localhost"}, - }, - { - prefix: "localhost:5000", - expectedAddress: "localhost:5000", - expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "localhost:5000"}, - }, - { - prefix: "no-auth.example.com", - expectedAuthCfg: registry.AuthConfig{}, - }, - } - - for _, tc := range tests { - tcName := tc.prefix - if tc.prefix == "" { - tcName = "no-prefix" - } - t.Run(tcName, func(t *testing.T) { - for _, remoteRef := range remoteRefs { - imageRef := path.Join(tc.prefix, remoteRef) - actual, err := command.RetrieveAuthTokenFromImage(&cfg, imageRef) - assert.NilError(t, err) - expectedAuthCfg, err := authconfig.Encode(tc.expectedAuthCfg) - assert.NilError(t, err) - assert.Equal(t, actual, expectedAuthCfg) - } - }) - } -} diff --git a/cli/command/service/create.go b/cli/command/service/create.go index 45406d7b2633..b44e8097ae69 100644 --- a/cli/command/service/create.go +++ b/cli/command/service/create.go @@ -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" @@ -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.Encoded(ctx, dockerCLI.ConfigFile(), opts.image) if err != nil { return err } diff --git a/cli/command/service/update.go b/cli/command/service/update.go index 2e7a0ee25fce..416ef083caa9 100644 --- a/cli/command/service/update.go +++ b/cli/command/service/update.go @@ -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" @@ -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.Encoded(ctx, dockerCLI.ConfigFile(), spec.TaskTemplate.ContainerSpec.Image) if err != nil { return err } diff --git a/cli/command/stack/deploy_composefile.go b/cli/command/stack/deploy_composefile.go index ad85765133cb..1ab2705cef9a 100644 --- a/cli/command/stack/deploy_composefile.go +++ b/cli/command/stack/deploy_composefile.go @@ -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" @@ -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.Encoded(ctx, dockerCLI.ConfigFile(), image) if err != nil { return nil, err } diff --git a/cli/config/types/authconfig.go b/cli/config/types/authconfig.go index 9fe90003b138..e4e07c039bd4 100644 --- a/cli/config/types/authconfig.go +++ b/cli/config/types/authconfig.go @@ -3,15 +3,22 @@ package types // AuthConfig contains authorization information for connecting to a Registry type AuthConfig struct { Username string `json:"username,omitempty"` + + // Password is the password of Username, or a token used in its place, + // such as a personal or organization access token. Password string `json:"password,omitempty"` - Auth string `json:"auth,omitempty"` + + // Auth is the base64-encoded "username:password" from config.json. It is + // decoded into Username and Password when the file is loaded. + Auth string `json:"auth,omitempty"` ServerAddress string `json:"serveraddress,omitempty"` - // IdentityToken is used to authenticate the user and get - // an access token for the registry. + // IdentityToken is an OAuth2 refresh token, which the daemon exchanges + // for an access token. IdentityToken string `json:"identitytoken,omitempty"` - // RegistryToken is a bearer token to be sent to a registry + // RegistryToken is a bearer token, which the daemon sends to the + // registry as is. RegistryToken string `json:"registrytoken,omitempty"` } diff --git a/internal/registryauth/registryauth.go b/internal/registryauth/registryauth.go new file mode 100644 index 000000000000..ae8de6624f64 --- /dev/null +++ b/internal/registryauth/registryauth.go @@ -0,0 +1,41 @@ +package registryauth + +import ( + "context" + + "github.com/distribution/reference" + "github.com/docker/cli/cli/config/types" + "github.com/moby/moby/api/pkg/authconfig" + registrytypes "github.com/moby/moby/api/types/registry" +) + +type AuthConfigProvider interface { + // GetAuthConfig for a repository from the credential store. + GetAuthConfig(registryHostname string) (types.AuthConfig, error) +} + +// Encoded retrieves an encoded auth token given a +// complete image reference. The auth configuration is serialized as a +// base64url encoded ([RFC 4648, Section 5]) JSON string for sending through +// the "X-Registry-Auth" header. +// +// [RFC 4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5 +func Encoded(ctx context.Context, cfg AuthConfigProvider, image string) (string, error) { + registryRef, err := reference.ParseNormalizedNamed(image) + if err != nil { + return "", err + } + authConfig, err := cfg.GetAuthConfig(reference.Domain(registryRef)) + 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, + }) +} diff --git a/internal/registryauth/registryauth_test.go b/internal/registryauth/registryauth_test.go new file mode 100644 index 000000000000..c9172bc18dd6 --- /dev/null +++ b/internal/registryauth/registryauth_test.go @@ -0,0 +1,162 @@ +package registryauth + +import ( + "bytes" + "path" + "testing" + + "github.com/docker/cli/cli/config/configfile" + "github.com/moby/moby/api/pkg/authconfig" + registrytypes "github.com/moby/moby/api/types/registry" + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" +) + +func loadConfig(t *testing.T) *configfile.ConfigFile { + t.Helper() + const configFileContent = `{"auths": { + "https://index.docker.io/v1/": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "localhost:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="} + } +}` + cfg := &configfile.ConfigFile{} + assert.NilError(t, cfg.LoadFromReader(bytes.NewReader([]byte(configFileContent)))) + return cfg +} + +func encode(t *testing.T, authConfig registrytypes.AuthConfig) string { + t.Helper() + encoded, err := authconfig.Encode(authConfig) + assert.NilError(t, err) + return encoded +} + +func TestEncoded(t *testing.T) { + // configFileContent contains a plain-text "username:password", as stored by + // the plain-text store; + // https://github.com/docker/cli/blob/v28.0.4/cli/config/configfile/file.go#L218-L229 + const configFileContent = `{"auths": { + "https://index.docker.io/v1/": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "[::1]": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "[::1]:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "127.0.0.1": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "127.0.0.1:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "localhost": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "localhost:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "registry-1.docker.io": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}, + "registry.hub.docker.com": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="} + } +}` + cfg := configfile.ConfigFile{} + err := cfg.LoadFromReader(bytes.NewReader([]byte(configFileContent))) + assert.NilError(t, err) + + remoteRefs := []string{ + "ubuntu", + "ubuntu:latest", + "ubuntu:latest@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", + "ubuntu@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", + "library/ubuntu", + "library/ubuntu:latest", + "library/ubuntu:latest@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", + "library/ubuntu@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782", + } + + tests := []struct { + prefix string + expectedAddress string + expectedAuthCfg registrytypes.AuthConfig + }{ + { + prefix: "", + expectedAddress: "https://index.docker.io/v1/", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, + }, + { + prefix: "docker.io", + expectedAddress: "https://index.docker.io/v1/", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, + }, + { + prefix: "index.docker.io", + expectedAddress: "https://index.docker.io/v1/", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "https://index.docker.io/v1/"}, + }, + { + // FIXME(thaJeztah): registry-1.docker.io (the actual registry) is the odd one out, and is stored separate from other URLs used for docker hub's registry + prefix: "registry-1.docker.io", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry-1.docker.io"}, + }, + { + // FIXME(thaJeztah): registry.hub.docker.com is stored separate from other URLs used for docker hub's registry + prefix: "registry.hub.docker.com", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry.hub.docker.com"}, + }, + { + prefix: "[::1]", + expectedAddress: "[::1]", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "[::1]"}, + }, + { + prefix: "[::1]:5000", + expectedAddress: "[::1]:5000", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "[::1]:5000"}, + }, + { + prefix: "127.0.0.1", + expectedAddress: "127.0.0.1", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "127.0.0.1"}, + }, + { + prefix: "localhost", + expectedAddress: "localhost", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "localhost"}, + }, + { + prefix: "localhost:5000", + expectedAddress: "localhost:5000", + expectedAuthCfg: registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "localhost:5000"}, + }, + { + prefix: "no-auth.example.com", + expectedAuthCfg: registrytypes.AuthConfig{}, + }, + } + + for _, tc := range tests { + tcName := tc.prefix + if tc.prefix == "" { + tcName = "no-prefix" + } + t.Run(tcName, func(t *testing.T) { + for _, remoteRef := range remoteRefs { + imageRef := path.Join(tc.prefix, remoteRef) + actual, err := Encoded(t.Context(), &cfg, imageRef) + assert.NilError(t, err) + expectedAuthCfg, err := authconfig.Encode(tc.expectedAuthCfg) + assert.NilError(t, err) + assert.Equal(t, actual, expectedAuthCfg) + } + }) + } +} + +func TestEncodedEnvTakesPrecedence(t *testing.T) { + t.Setenv(configfile.DockerEnvConfigKey, `{"auths":{"https://index.docker.io/v1/":{"auth":"ZW52dXNlcjplbnZwYXNz"}}}`) + cfg := loadConfig(t) + + actual, err := Encoded(t.Context(), cfg, "ubuntu") + assert.NilError(t, err) + assert.Check(t, is.Equal(actual, encode(t, registrytypes.AuthConfig{Username: "envuser", Password: "envpass", ServerAddress: "https://index.docker.io/v1/"}))) + + actual, err = Encoded(t.Context(), cfg, "localhost:5000/ubuntu") + assert.NilError(t, err) + assert.Check(t, is.Equal(actual, encode(t, registrytypes.AuthConfig{Username: "username", Password: "password", ServerAddress: "localhost:5000"})), + "registries missing from DOCKER_AUTH_CONFIG must use the configuration file") +} + +func TestEncodedRejectsInvalidReference(t *testing.T) { + t.Setenv(configfile.DockerEnvConfigKey, "") + _, err := Encoded(t.Context(), loadConfig(t), "Invalid Reference") + assert.ErrorContains(t, err, "invalid reference format") +}