Skip to content

Commit 7a69f5d

Browse files
committed
When the port is 443, also save credentials without port
Signed-off-by: apostasie <[email protected]>
1 parent 7e97f06 commit 7a69f5d

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

pkg/cmd/login/login.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ func Login(ctx context.Context, options types.LoginCommandOptions, stdout io.Wri
9292
return fmt.Errorf("error saving credentials: %w", err)
9393
}
9494

95+
// When the port is the https default (443), other clients cannot be expected to necessarily lookup the variants with port
96+
// so save it both with and without port.
97+
// This is the case for at least buildctl: https://github.com/containerd/nerdctl/issues/3748
98+
if registryURL.Port() == dockerconfigresolver.StandardHTTPSPort {
99+
registryURL.Host = registryURL.Hostname()
100+
err = credStore.Store(registryURL, credentials)
101+
if err != nil {
102+
return fmt.Errorf("error saving credentials: %w", err)
103+
}
104+
}
105+
95106
_, err = fmt.Fprintln(stdout, "Login Succeeded")
96107

97108
return err

pkg/imgutil/dockerconfigresolver/defaults.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import "errors"
2121
type scheme string
2222

2323
const (
24-
standardHTTPSPort = "443"
25-
schemeHTTP scheme = "http"
26-
schemeHTTPS scheme = "https"
24+
StandardHTTPSPort = "443"
25+
26+
schemeHTTPS scheme = "https"
27+
schemeHTTP scheme = "http"
2728
// schemeNerdctlExperimental is currently provisional, to unlock namespace based host authentication
2829
// This may change or break without notice, and you should have no expectations that credentials saved like that
2930
// will be supported in the future

pkg/imgutil/dockerconfigresolver/hostsstore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func hostDirsFromRoot(registryURL *RegistryURL, dirs []string) (string, error) {
5555
return found, err
5656
}
5757
// If not found, and the port is standard, try again without the port
58-
if registryURL.Port() == standardHTTPSPort {
58+
if registryURL.Port() == StandardHTTPSPort {
5959
found, err = config.HostDirFromRoot(hostsDir)(registryURL.Hostname())
6060
if (err != nil && !errors.Is(err, errdefs.ErrNotFound)) || (found != "") {
6161
return found, err

pkg/imgutil/dockerconfigresolver/registryurl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Parse(address string) (*RegistryURL, error) {
5050
}
5151
// If it has no port, add the standard port explicitly
5252
if u.Port() == "" {
53-
u.Host = u.Hostname() + ":" + standardHTTPSPort
53+
u.Host = u.Hostname() + ":" + StandardHTTPSPort
5454
}
5555
reg := &RegistryURL{URL: *u}
5656
queryParams := u.Query()
@@ -74,7 +74,7 @@ type RegistryURL struct {
7474
// CanonicalIdentifier returns the identifier expected to be used to save credentials to docker auth config
7575
func (rn *RegistryURL) CanonicalIdentifier() string {
7676
// If it is the docker index over https, port 443, on the /v1/ path, we use the docker fully qualified identifier
77-
if rn.Scheme == string(schemeHTTPS) && rn.Hostname() == "index.docker.io" && rn.Path == "/v1/" && rn.Port() == standardHTTPSPort ||
77+
if rn.Scheme == string(schemeHTTPS) && rn.Hostname() == "index.docker.io" && rn.Path == "/v1/" && rn.Port() == StandardHTTPSPort ||
7878
rn.URL.String() == dockerIndexServer {
7979
return dockerIndexServer
8080
}
@@ -102,7 +102,7 @@ func (rn *RegistryURL) AllIdentifiers() []string {
102102

103103
// Docker behavior: if the domain was index.docker.io over 443, we are allowed to additionally read the canonical
104104
// docker credentials
105-
if rn.Port() == standardHTTPSPort {
105+
if rn.Port() == StandardHTTPSPort {
106106
if rn.Hostname() == "index.docker.io" || rn.Hostname() == "registry-1.docker.io" {
107107
fullList = append(fullList, dockerIndexServer)
108108
}
@@ -116,7 +116,7 @@ func (rn *RegistryURL) AllIdentifiers() []string {
116116

117117
// Note that docker does not try to be smart wrt explicit port vs. implied port
118118
// If standard port, allow retrieving credentials from the variant without a port as well
119-
if rn.Port() == standardHTTPSPort {
119+
if rn.Port() == StandardHTTPSPort {
120120
fullList = append(
121121
fullList,
122122
rn.Hostname(),

0 commit comments

Comments
 (0)