Skip to content

Commit 80be02c

Browse files
authored
Merge pull request docker#6200 from thaJeztah/search_no_registrypkg
cli/command/registry: remove uses of registry.ParseSearchIndexInfo
2 parents 03ff54b + e504faf commit 80be02c

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

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)