use local ConvertToHostname() implementation

Commit 27b2797f7d added a local implementation
of this function, so let's use the local variant to (slightly) reduce the
dependency on moby's registry package.

Also made some minor cleanups.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-26 20:10:38 +01:00
parent 925e7d6870
commit 8376b3e428
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
6 changed files with 59 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/distribution/reference" "github.com/distribution/reference"
"github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/credentials"
configtypes "github.com/docker/cli/cli/config/types" configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/cli/hints" "github.com/docker/cli/cli/hints"
"github.com/docker/cli/cli/streams" "github.com/docker/cli/cli/streams"
@ -71,7 +72,7 @@ func ResolveAuthConfig(cfg *configfile.ConfigFile, index *registrytypes.IndexInf
// If credentials for given serverAddress exists in the credential store, the configuration will be populated with values in it // If credentials for given serverAddress exists in the credential store, the configuration will be populated with values in it
func GetDefaultAuthConfig(cfg *configfile.ConfigFile, checkCredStore bool, serverAddress string, isDefaultRegistry bool) (registrytypes.AuthConfig, error) { func GetDefaultAuthConfig(cfg *configfile.ConfigFile, checkCredStore bool, serverAddress string, isDefaultRegistry bool) (registrytypes.AuthConfig, error) {
if !isDefaultRegistry { if !isDefaultRegistry {
serverAddress = registry.ConvertToHostname(serverAddress) serverAddress = credentials.ConvertToHostname(serverAddress)
} }
authconfig := configtypes.AuthConfig{} authconfig := configtypes.AuthConfig{}
var err error var err error

View File

@ -6,6 +6,7 @@ import (
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config/credentials"
"github.com/docker/docker/registry" "github.com/docker/docker/registry"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -46,7 +47,7 @@ func runLogout(_ context.Context, dockerCli command.Cli, serverAddress string) e
hostnameAddress = serverAddress hostnameAddress = serverAddress
) )
if !isDefaultRegistry { if !isDefaultRegistry {
hostnameAddress = registry.ConvertToHostname(serverAddress) hostnameAddress = credentials.ConvertToHostname(serverAddress)
// the tries below are kept for backward compatibility where a user could have // the tries below are kept for backward compatibility where a user could have
// saved the registry in one of the following format. // saved the registry in one of the following format.
regsToLogout = append(regsToLogout, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress) regsToLogout = append(regsToLogout, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress)

View File

@ -1,6 +1,7 @@
package credentials package credentials
import ( import (
"net/url"
"strings" "strings"
"github.com/docker/cli/cli/config/types" "github.com/docker/cli/cli/config/types"
@ -68,14 +69,14 @@ func (c *fileStore) IsFileStore() bool {
// ConvertToHostname converts a registry url which has http|https prepended // ConvertToHostname converts a registry url which has http|https prepended
// to just an hostname. // to just an hostname.
// Copied from github.com/docker/docker/registry.ConvertToHostname to reduce dependencies. // Copied from github.com/docker/docker/registry.ConvertToHostname to reduce dependencies.
func ConvertToHostname(url string) string { func ConvertToHostname(maybeURL string) string {
stripped := url stripped := maybeURL
if strings.HasPrefix(url, "http://") { if strings.Contains(stripped, "://") {
stripped = strings.TrimPrefix(url, "http://") u, err := url.Parse(stripped)
} else if strings.HasPrefix(url, "https://") { if err == nil && u.Hostname() != "" {
stripped = strings.TrimPrefix(url, "https://") return u.Hostname()
}
} }
hostName, _, _ := strings.Cut(stripped, "/") hostName, _, _ := strings.Cut(stripped, "/")
return hostName return hostName
} }

View File

@ -134,3 +134,45 @@ func TestFileStoreErase(t *testing.T) {
t.Fatalf("expected empty email, got %s", a.Email) t.Fatalf("expected empty email, got %s", a.Email)
} }
} }
func TestConvertToHostname(t *testing.T) {
tests := []struct{ input, expected string }{
{
input: "example.com",
expected: "example.com",
},
{
input: "http://example.com",
expected: "example.com",
},
{
input: "https://example.com",
expected: "example.com",
},
{
input: "https://example.com/",
expected: "example.com",
},
{
input: "https://example.com/v2/",
expected: "example.com",
},
{
// FIXME(thaJeztah): should ConvertToHostname correctly handle this / fail on this?
input: "unix:///var/run/docker.sock",
expected: "unix:",
},
{
// FIXME(thaJeztah): should ConvertToHostname correctly handle this?
input: "ftp://example.com",
expected: "example.com",
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.input, func(t *testing.T) {
actual := ConvertToHostname(tc.input)
assert.Equal(t, actual, tc.expected)
})
}
}

View File

@ -101,23 +101,23 @@ func (c *client) MountBlob(ctx context.Context, sourceRef reference.Canonical, t
func (c *client) PutManifest(ctx context.Context, ref reference.Named, manifest distribution.Manifest) (digest.Digest, error) { func (c *client) PutManifest(ctx context.Context, ref reference.Named, manifest distribution.Manifest) (digest.Digest, error) {
repoEndpoint, err := newDefaultRepositoryEndpoint(ref, c.insecureRegistry) repoEndpoint, err := newDefaultRepositoryEndpoint(ref, c.insecureRegistry)
if err != nil { if err != nil {
return digest.Digest(""), err return "", err
} }
repoEndpoint.actions = trust.ActionsPushAndPull repoEndpoint.actions = trust.ActionsPushAndPull
repo, err := c.getRepositoryForReference(ctx, ref, repoEndpoint) repo, err := c.getRepositoryForReference(ctx, ref, repoEndpoint)
if err != nil { if err != nil {
return digest.Digest(""), err return "", err
} }
manifestService, err := repo.Manifests(ctx) manifestService, err := repo.Manifests(ctx)
if err != nil { if err != nil {
return digest.Digest(""), err return "", err
} }
_, opts, err := getManifestOptionsFromReference(ref) _, opts, err := getManifestOptionsFromReference(ref)
if err != nil { if err != nil {
return digest.Digest(""), err return "", err
} }
dgst, err := manifestService.Put(ctx, manifest, opts...) dgst, err := manifestService.Put(ctx, manifest, opts...)

View File

@ -270,7 +270,7 @@ func (c *client) iterateEndpoints(ctx context.Context, namedRef reference.Named,
return newNotFoundError(namedRef.String()) return newNotFoundError(namedRef.String())
} }
// allEndpoints returns a list of endpoints ordered by priority (v2, https, v1). // allEndpoints returns a list of endpoints ordered by priority (v2, http).
func allEndpoints(namedRef reference.Named, insecure bool) ([]registry.APIEndpoint, error) { func allEndpoints(namedRef reference.Named, insecure bool) ([]registry.APIEndpoint, error) {
repoInfo, err := registry.ParseRepositoryInfo(namedRef) repoInfo, err := registry.ParseRepositoryInfo(namedRef)
if err != nil { if err != nil {