Skip to content

Commit e017b03

Browse files
committed
WIP: remove registry dep
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 5973fd3 commit e017b03

File tree

6 files changed

+62
-181
lines changed

6 files changed

+62
-181
lines changed

cli/cobra.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ package cli
33
import (
44
"fmt"
55
"os"
6-
"path/filepath"
76
"sort"
87
"strings"
98

109
pluginmanager "github.com/docker/cli/cli-plugins/manager"
1110
"github.com/docker/cli/cli/command"
1211
cliflags "github.com/docker/cli/cli/flags"
13-
"github.com/docker/docker/pkg/homedir"
14-
"github.com/docker/docker/registry"
1512
"github.com/fvbommel/sortorder"
1613
"github.com/moby/term"
1714
"github.com/morikuni/aec"
@@ -63,11 +60,13 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *c
6360
}
6461

6562
// Configure registry.CertsDir() when running in rootless-mode
66-
if os.Getenv("ROOTLESSKIT_STATE_DIR") != "" {
67-
if configHome, err := homedir.GetConfigHome(); err == nil {
68-
registry.SetCertsDir(filepath.Join(configHome, "docker/certs.d"))
69-
}
70-
}
63+
//
64+
// FIXME(thaJeztah): this causes docker/distribution to be a dependency for cli-plugins
65+
// if os.Getenv("ROOTLESSKIT_STATE_DIR") != "" {
66+
// if configHome, err := homedir.GetConfigHome(); err == nil {
67+
// registry.SetCertsDir(filepath.Join(configHome, "docker/certs.d"))
68+
// }
69+
// }
7170

7271
return opts, helpCommand
7372
}

cli/command/registry.go

+55-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/docker/cli/cli/streams"
1616
"github.com/docker/cli/internal/tui"
1717
registrytypes "github.com/docker/docker/api/types/registry"
18-
"github.com/docker/docker/registry"
1918
"github.com/morikuni/aec"
2019
"github.com/pkg/errors"
2120
)
@@ -28,13 +27,19 @@ const (
2827
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
2928
)
3029

30+
const (
31+
// IndexHostname is the index hostname, used for authentication and image search.
32+
indexHostname = "index.docker.io"
33+
// IndexServer is used for user auth and image search
34+
indexServer = "https://" + indexHostname + "/v1/"
35+
)
36+
3137
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
3238
// for the given command.
3339
func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInfo, cmdName string) registrytypes.RequestAuthConfig {
3440
return func(ctx context.Context) (string, error) {
3541
_, _ = fmt.Fprintf(cli.Out(), "\nLogin prior to %s:\n", cmdName)
36-
indexServer := registry.GetAuthConfigKey(index)
37-
isDefaultRegistry := indexServer == registry.IndexServer
42+
isDefaultRegistry := index.Official || index.Name == indexServer
3843
authConfig, err := GetDefaultAuthConfig(cli.ConfigFile(), true, indexServer, isDefaultRegistry)
3944
if err != nil {
4045
_, _ = fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", indexServer, err)
@@ -63,7 +68,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
6368
func ResolveAuthConfig(cfg *configfile.ConfigFile, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
6469
configKey := index.Name
6570
if index.Official {
66-
configKey = registry.IndexServer
71+
configKey = indexServer
6772
}
6873

6974
a, _ := cfg.GetAuthConfig(configKey)
@@ -132,7 +137,7 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword
132137

133138
argUser = strings.TrimSpace(argUser)
134139
if argUser == "" {
135-
if serverAddress == registry.IndexServer {
140+
if serverAddress == indexServer {
136141
// When signing in to the default (Docker Hub) registry, we display
137142
// hints for creating an account, and (if hints are enabled), using
138143
// a token instead of a password.
@@ -219,15 +224,58 @@ func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (strin
219224
return encodedAuth, nil
220225
}
221226

227+
var IndexConfigs = map[string]*registrytypes.IndexInfo{
228+
"docker.io": {
229+
Name: "docker.io",
230+
Mirrors: nil,
231+
Secure: true,
232+
Official: true,
233+
},
234+
}
235+
236+
// newIndexInfo returns IndexInfo configuration from indexName
237+
func newIndexInfo(indexName string) (*registrytypes.IndexInfo, error) {
238+
var err error
239+
indexName, err = validateIndexName(indexName)
240+
if err != nil {
241+
return nil, err
242+
}
243+
244+
// Return any configured index info, first.
245+
if index, ok := IndexConfigs[indexName]; ok {
246+
return index, nil
247+
}
248+
249+
// Construct a non-configured index info.
250+
return &registrytypes.IndexInfo{
251+
Name: indexName,
252+
}, nil
253+
}
254+
255+
// validateIndexName validates an index name. It is used by the daemon to
256+
// validate the daemon configuration.
257+
func validateIndexName(val string) (string, error) {
258+
// TODO: upstream this to check to reference package
259+
if val == "index.docker.io" {
260+
val = "docker.io"
261+
}
262+
if strings.HasPrefix(val, "-") || strings.HasSuffix(val, "-") {
263+
// return "", errdefs.InvalidParameter(fmt.Errorf("invalid index name (%s). Cannot begin or end with a hyphen", val))
264+
return "", fmt.Errorf("invalid index name (%s). Cannot begin or end with a hyphen", val)
265+
}
266+
return val, nil
267+
}
268+
222269
// resolveAuthConfigFromImage retrieves that AuthConfig using the image string
223270
func resolveAuthConfigFromImage(cfg *configfile.ConfigFile, image string) (registrytypes.AuthConfig, error) {
224271
registryRef, err := reference.ParseNormalizedNamed(image)
225272
if err != nil {
226273
return registrytypes.AuthConfig{}, err
227274
}
228-
repoInfo, err := registry.ParseRepositoryInfo(registryRef)
275+
domainName := reference.Domain(registryRef)
276+
idxInfo, err := newIndexInfo(domainName)
229277
if err != nil {
230278
return registrytypes.AuthConfig{}, err
231279
}
232-
return ResolveAuthConfig(cfg, repoInfo.Index), nil
280+
return ResolveAuthConfig(cfg, idxInfo), nil
233281
}

vendor/github.com/docker/docker/pkg/homedir/homedir.go

-28
This file was deleted.

vendor/github.com/docker/docker/pkg/homedir/homedir_linux.go

-105
This file was deleted.

vendor/github.com/docker/docker/pkg/homedir/homedir_others.go

-32
This file was deleted.

vendor/modules.txt

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ github.com/docker/docker/internal/lazyregexp
8686
github.com/docker/docker/internal/multierror
8787
github.com/docker/docker/pkg/archive
8888
github.com/docker/docker/pkg/atomicwriter
89-
github.com/docker/docker/pkg/homedir
9089
github.com/docker/docker/pkg/idtools
9190
github.com/docker/docker/pkg/ioutils
9291
github.com/docker/docker/pkg/jsonmessage

0 commit comments

Comments
 (0)