Skip to content

Commit 2db8052

Browse files
authored
Merge pull request containerd#3410 from apostasie/dev-login-hosts-file-default-port
Login refactor (small breakout): hostsstore resolution cleanup
2 parents 21c81cd + 1a4a5a9 commit 2db8052

File tree

2 files changed

+79
-24
lines changed

2 files changed

+79
-24
lines changed

pkg/imgutil/dockerconfigresolver/dockerconfigresolver.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"errors"
23-
"os"
2423

2524
"github.com/containerd/containerd/v2/core/remotes"
2625
"github.com/containerd/containerd/v2/core/remotes/docker"
@@ -57,24 +56,9 @@ func WithSkipVerifyCerts(b bool) Opt {
5756

5857
// WithHostsDirs specifies directories like /etc/containerd/certs.d and /etc/docker/certs.d
5958
func WithHostsDirs(orig []string) Opt {
60-
var ss []string
61-
if len(orig) == 0 {
62-
log.L.Debug("no hosts dir was specified")
63-
}
64-
for _, v := range orig {
65-
if _, err := os.Stat(v); err == nil {
66-
log.L.Debugf("Found hosts dir %q", v)
67-
ss = append(ss, v)
68-
} else {
69-
if errors.Is(err, os.ErrNotExist) {
70-
log.L.WithError(err).Debugf("Ignoring hosts dir %q", v)
71-
} else {
72-
log.L.WithError(err).Warnf("Ignoring hosts dir %q", v)
73-
}
74-
}
75-
}
59+
validDirs := validateDirectories(orig)
7660
return func(o *opts) {
77-
o.hostsDirs = ss
61+
o.hostsDirs = validDirs
7862
}
7963
}
8064

@@ -96,14 +80,19 @@ func NewHostOptions(ctx context.Context, refHostname string, optFuncs ...Opt) (*
9680
}
9781
var ho dockerconfig.HostOptions
9882

99-
ho.HostDir = func(s string) (string, error) {
100-
for _, hostsDir := range o.hostsDirs {
101-
found, err := dockerconfig.HostDirFromRoot(hostsDir)(s)
102-
if (err != nil && !errdefs.IsNotFound(err)) || (found != "") {
103-
return found, err
83+
ho.HostDir = func(hostURL string) (string, error) {
84+
regURL, err := Parse(hostURL)
85+
if err != nil {
86+
return "", err
87+
}
88+
dir, err := hostDirsFromRoot(regURL, o.hostsDirs)
89+
if err != nil {
90+
if errors.Is(err, errdefs.ErrNotFound) {
91+
err = nil
10492
}
93+
return "", err
10594
}
106-
return "", nil
95+
return dir, nil
10796
}
10897

10998
if o.authCreds != nil {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package dockerconfigresolver
18+
19+
import (
20+
"errors"
21+
"os"
22+
23+
"github.com/containerd/containerd/v2/core/remotes/docker/config"
24+
"github.com/containerd/errdefs"
25+
"github.com/containerd/log"
26+
)
27+
28+
// validateDirectories inspect a slice of strings and returns the ones that are valid readable directories
29+
func validateDirectories(orig []string) []string {
30+
ss := []string{}
31+
for _, v := range orig {
32+
fi, err := os.Stat(v)
33+
if err != nil || !fi.IsDir() {
34+
if !errors.Is(err, os.ErrNotExist) {
35+
log.L.WithError(err).Warnf("Ignoring hosts location %q", v)
36+
}
37+
continue
38+
}
39+
ss = append(ss, v)
40+
}
41+
return ss
42+
}
43+
44+
// hostDirsFromRoot will retrieve a host.toml file for the namespace host, possibly trying without port
45+
// if the requested port is standard.
46+
// https://github.com/containerd/nerdctl/issues/3047
47+
func hostDirsFromRoot(registryURL *RegistryURL, dirs []string) (string, error) {
48+
hostsDirs := validateDirectories(dirs)
49+
50+
// Go through the configured system location to consider for hosts.toml files
51+
for _, hostsDir := range hostsDirs {
52+
found, err := config.HostDirFromRoot(hostsDir)(registryURL.Host)
53+
// If we errored with anything but NotFound, or if we found one, return now
54+
if (err != nil && !errdefs.IsNotFound(err)) || (found != "") {
55+
return found, err
56+
}
57+
// If not found, and the port is standard, try again without the port
58+
if registryURL.Port() == standardHTTPSPort {
59+
found, err = config.HostDirFromRoot(hostsDir)(registryURL.Hostname())
60+
if (err != nil && !errors.Is(err, errdefs.ErrNotFound)) || (found != "") {
61+
return found, err
62+
}
63+
}
64+
}
65+
return "", nil
66+
}

0 commit comments

Comments
 (0)