@@ -3,12 +3,12 @@ package registry
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
6
7
7
8
"github.com/docker/cli/cli"
8
9
"github.com/docker/cli/cli/command"
9
10
"github.com/docker/cli/cli/command/formatter"
10
11
"github.com/docker/cli/opts"
11
- "github.com/docker/docker/registry"
12
12
registrytypes "github.com/moby/moby/api/types/registry"
13
13
"github.com/spf13/cobra"
14
14
)
@@ -52,13 +52,7 @@ func runSearch(ctx context.Context, dockerCli command.Cli, options searchOptions
52
52
if options .filter .Value ().Contains ("is-automated" ) {
53
53
_ , _ = fmt .Fprintln (dockerCli .Err (), `WARNING: the "is-automated" filter is deprecated, and searching for "is-automated=true" will not yield any results in future.` )
54
54
}
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 )
62
56
if err != nil {
63
57
return err
64
58
}
@@ -80,3 +74,37 @@ func runSearch(ctx context.Context, dockerCli command.Cli, options searchOptions
80
74
}
81
75
return SearchWrite (searchCtx , results )
82
76
}
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