Skip to content

Commit e504faf

Browse files
committed
cli/command/registry: remove uses of registry.ParseSearchIndexInfo
This utility was only used in the CLI, but the implementation was based on it being used on the daemon side, so included resolving the host's IP-address, mirrors, etc. The only reason it's used in the CLI is to provide credentials for the registry that's being searched, so reduce it to just that. There's more cleaning up to do in this area, so to make our lives easier, it's implemented locally as non-exported functions; likely to be replaced with something else. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 03ff54b commit e504faf

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

cli/command/registry/search.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package registry
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/docker/cli/cli"
89
"github.com/docker/cli/cli/command"
910
"github.com/docker/cli/cli/command/formatter"
1011
"github.com/docker/cli/opts"
11-
"github.com/docker/docker/registry"
1212
registrytypes "github.com/moby/moby/api/types/registry"
1313
"github.com/spf13/cobra"
1414
)
@@ -52,13 +52,7 @@ func runSearch(ctx context.Context, dockerCli command.Cli, options searchOptions
5252
if options.filter.Value().Contains("is-automated") {
5353
_, _ = fmt.Fprintln(dockerCli.Err(), `WARNING: the "is-automated" filter is deprecated, and searching for "is-automated=true" will not yield any results in future.`)
5454
}
55-
indexInfo, err := registry.ParseSearchIndexInfo(options.term)
56-
if err != nil {
57-
return err
58-
}
59-
60-
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), indexInfo)
61-
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
55+
encodedAuth, err := getAuth(dockerCli, options.term)
6256
if err != nil {
6357
return err
6458
}
@@ -80,3 +74,37 @@ func runSearch(ctx context.Context, dockerCli command.Cli, options searchOptions
8074
}
8175
return SearchWrite(searchCtx, results)
8276
}
77+
78+
// authConfigKey is the key used to store credentials for Docker Hub. It is
79+
// a copy of [registry.IndexServer].
80+
//
81+
// [registry.IndexServer]: https://pkg.go.dev/github.com/docker/docker/registry#IndexServer
82+
const authConfigKey = "https://index.docker.io/v1/"
83+
84+
// getAuth will use fetch auth based on the given search-term. If the search
85+
// does not contain a hostname for the registry, it assumes Docker Hub is used,
86+
// and resolves authentication for Docker Hub, otherwise it resolves authentication
87+
// for the given registry.
88+
func getAuth(dockerCLI command.Cli, reposName string) (encodedAuth string, err error) {
89+
authCfgKey := splitReposSearchTerm(reposName)
90+
if authCfgKey == "docker.io" || authCfgKey == "index.docker.io" {
91+
authCfgKey = authConfigKey
92+
}
93+
94+
// Ignoring errors here, which was the existing behavior (likely
95+
// "no credentials found"). We'll get an error when search failed,
96+
// so fine to ignore in most situations.
97+
authConfig, _ := dockerCLI.ConfigFile().GetAuthConfig(authCfgKey)
98+
return registrytypes.EncodeAuthConfig(registrytypes.AuthConfig(authConfig))
99+
}
100+
101+
// splitReposSearchTerm breaks a search term into an index name and remote name
102+
func splitReposSearchTerm(reposName string) string {
103+
nameParts := strings.SplitN(reposName, "/", 2)
104+
if len(nameParts) == 1 || (!strings.Contains(nameParts[0], ".") && !strings.Contains(nameParts[0], ":") && nameParts[0] != "localhost") {
105+
// This is a Docker Hub repository (ex: samalba/hipache or ubuntu),
106+
// use the default Docker Hub registry (docker.io)
107+
return "docker.io"
108+
}
109+
return nameParts[0]
110+
}

0 commit comments

Comments
 (0)