Skip to content

Commit

Permalink
registry: don't call "/info" API endpoint to get default registry
Browse files Browse the repository at this point in the history
The CLI currenly calls the `/info` endpoint to get the address
of the default registry to use.

This functionality was added as part of the initial Windows implementation
of the engine. For legal reasons, Microsoft Windows (and thus Docker images
based on Windows) were not allowed to be distributed through non-Microsoft
infrastructure. As a temporary solution, a dedicated "registry-win-tp3.docker.io"
registry was created to serve Windows images.

As a result, the default registry was no longer "fixed", so a helper function
(`ElectAuthServer`) was added to allow the CLI to get the correct registry
address from the daemon. (docker/docker PR's/issues 18019, 19891, 19973)

Using separate registries was not an ideal solution, and a more permanent
solution was created by introducing "foreign image layers" in the distribution
spec, after which the "registry-win-tp3.docker.io" ceased to exist, and
removed from the engine through docker/docker PR 21100.

However, the `ElectAuthServer` was left in place, quoting from that PR;

> make the client check which default registry the daemon uses is still
> more correct than leaving it up to the client, even if it won't technically
> matter after this PR. There may be some backward compatibility scenarios
> where `ElectAuthServer` [sic] is still helpful.

That comment was 5 years ago, and given that the engine and cli are
released in tandem, and the default registry is not configurable, we
can save the extra roundtrip to the daemon by using a fixed value.

This patch deprecates the `ElectAuthServer` function, and makes it
return the default registry without calling (potentially expensie)
`/info` API endpoint.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Apr 7, 2021
1 parent 59fd6f0 commit 8eeba85
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 89 deletions.
34 changes: 7 additions & 27 deletions cli/command/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/cli/debug"
"github.com/docker/cli/cli/streams"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
Expand All @@ -22,28 +21,10 @@ import (
"github.com/pkg/errors"
)

// ElectAuthServer returns the default registry to use (by asking the daemon)
func ElectAuthServer(ctx context.Context, cli Cli) string {
// The daemon `/info` endpoint informs us of the default registry being
// used. This is essential in cross-platforms environment, where for
// example a Linux client might be interacting with a Windows daemon, hence
// the default registry URL might be Windows specific.
info, err := cli.Client().Info(ctx)
if err != nil {
// Daemon is not responding so use system default.
if debug.IsEnabled() {
// Only report the warning if we're in debug mode to prevent nagging during engine initialization workflows
fmt.Fprintf(cli.Err(), "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, registry.IndexServer)
}
return registry.IndexServer
}
if info.IndexServerAddress == "" {
if debug.IsEnabled() {
fmt.Fprintf(cli.Err(), "Warning: Empty registry endpoint from daemon. Using system default: %s\n", registry.IndexServer)
}
return registry.IndexServer
}
return info.IndexServerAddress
// ElectAuthServer returns the default registry to use
// Deprecated: use registry.IndexServer instead
func ElectAuthServer(_ context.Context, _ Cli) string {
return registry.IndexServer
}

// EncodeAuthToBase64 serializes the auth configuration as JSON base64 payload
Expand All @@ -61,7 +42,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
return func() (string, error) {
fmt.Fprintf(cli.Out(), "\nPlease login prior to %s:\n", cmdName)
indexServer := registry.GetAuthConfigKey(index)
isDefaultRegistry := indexServer == ElectAuthServer(context.Background(), cli)
isDefaultRegistry := indexServer == registry.IndexServer
authConfig, err := GetDefaultAuthConfig(cli, true, indexServer, isDefaultRegistry)
if authConfig == nil {
authConfig = &types.AuthConfig{}
Expand All @@ -80,12 +61,11 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
// ResolveAuthConfig is like registry.ResolveAuthConfig, but if using the
// default index, it uses the default index name for the daemon's platform,
// not the client's platform.
func ResolveAuthConfig(ctx context.Context, cli Cli, index *registrytypes.IndexInfo) types.AuthConfig {
func ResolveAuthConfig(_ context.Context, cli Cli, index *registrytypes.IndexInfo) types.AuthConfig {
configKey := index.Name
if index.Official {
configKey = ElectAuthServer(ctx, cli)
configKey = registry.IndexServer
}

a, _ := cli.ConfigFile().GetAuthConfig(configKey)
return types.AuthConfig(a)
}
Expand Down
13 changes: 6 additions & 7 deletions cli/command/registry/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,17 @@ func runLogin(dockerCli command.Cli, opts loginOptions) error { //nolint: gocycl
return err
}
var (
serverAddress string
authServer = command.ElectAuthServer(ctx, dockerCli)
serverAddress = registry.IndexServer
err error
authConfig *types.AuthConfig
response registrytypes.AuthenticateOKBody
)
if opts.serverAddress != "" && opts.serverAddress != registry.DefaultNamespace {
serverAddress = opts.serverAddress
} else {
serverAddress = authServer
}

var response registrytypes.AuthenticateOKBody
isDefaultRegistry := serverAddress == authServer
authConfig, err := command.GetDefaultAuthConfig(dockerCli, opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry)
isDefaultRegistry := serverAddress == registry.IndexServer
authConfig, err = command.GetDefaultAuthConfig(dockerCli, opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry)
if authConfig == nil {
authConfig = &types.AuthConfig{}
}
Expand Down
4 changes: 1 addition & 3 deletions cli/command/registry/logout.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package registry

import (
"context"
"fmt"

"github.com/docker/cli/cli"
Expand Down Expand Up @@ -30,11 +29,10 @@ func NewLogoutCommand(dockerCli command.Cli) *cobra.Command {
}

func runLogout(dockerCli command.Cli, serverAddress string) error {
ctx := context.Background()
var isDefaultRegistry bool

if serverAddress == "" {
serverAddress = command.ElectAuthServer(ctx, dockerCli)
serverAddress = registry.IndexServer
isDefaultRegistry = true
}

Expand Down
52 changes: 0 additions & 52 deletions cli/command/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (
"fmt"
"testing"

"github.com/pkg/errors"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"

// Prevents a circular import with "github.com/docker/cli/internal/test"

. "github.com/docker/cli/cli/command"
configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/cli/debug"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -45,56 +43,6 @@ func (cli *fakeClient) Info(_ context.Context) (types.Info, error) {
return types.Info{}, nil
}

func TestElectAuthServer(t *testing.T) {
testCases := []struct {
expectedAuthServer string
expectedWarning string
infoFunc func() (types.Info, error)
}{
{
expectedAuthServer: "https://index.docker.io/v1/",
expectedWarning: "",
infoFunc: func() (types.Info, error) {
return types.Info{IndexServerAddress: "https://index.docker.io/v1/"}, nil
},
},
{
expectedAuthServer: "https://index.docker.io/v1/",
expectedWarning: "Empty registry endpoint from daemon",
infoFunc: func() (types.Info, error) {
return types.Info{IndexServerAddress: ""}, nil
},
},
{
expectedAuthServer: "https://foo.bar",
expectedWarning: "",
infoFunc: func() (types.Info, error) {
return types.Info{IndexServerAddress: "https://foo.bar"}, nil
},
},
{
expectedAuthServer: "https://index.docker.io/v1/",
expectedWarning: "failed to get default registry endpoint from daemon",
infoFunc: func() (types.Info, error) {
return types.Info{}, errors.Errorf("error getting info")
},
},
}
// Enable debug to see warnings we're checking for
debug.Enable()
for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc})
server := ElectAuthServer(context.Background(), cli)
assert.Check(t, is.Equal(tc.expectedAuthServer, server))
actual := cli.ErrBuffer().String()
if tc.expectedWarning == "" {
assert.Check(t, is.Len(actual, 0))
} else {
assert.Check(t, is.Contains(actual, tc.expectedWarning))
}
}
}

func TestGetDefaultAuthConfig(t *testing.T) {
testCases := []struct {
checkCredStore bool
Expand Down

0 comments on commit 8eeba85

Please sign in to comment.