Skip to content

Commit 54dbe33

Browse files
committed
Harden image registry parsing
Allows handling of image registries given via https:// , docker:// , or any other scheme. Before this change the parsing of images given in these formats would fail unexpectedly and quietly.
1 parent 3f78a32 commit 54dbe33

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

pkg/registries/registries.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8+
"net/url"
89
"sort"
910
"strings"
1011

@@ -41,7 +42,7 @@ func newMirror(sourceImageLocation, mirrorImageLocation string, resolveTags bool
4142
mirrorHost = extractMirrorURL(sourceImageLocation, mirrorImageLocation)
4243
} else {
4344
// special case if source and mirror are the same. Do not drop the host repo name to avoid an empty host entry
44-
mirrorHost = extractHostname(mirrorImageLocation)
45+
mirrorHost = extractRegistryHostname(mirrorImageLocation)
4546
}
4647
return mirror{host: mirrorHost, resolveTags: resolveTags}
4748
}
@@ -82,14 +83,22 @@ func newMirrorSet(srcImage string, mirrorLocations []config.ImageMirror, resolve
8283
for _, m := range mirrorLocations {
8384
truncatedMirrors = append(truncatedMirrors, newMirror(srcImage, string(m), resolveTags))
8485
}
85-
return mirrorSet{source: extractHostname(srcImage), mirrors: truncatedMirrors, mirrorSourcePolicy: mirrorSourcePolicy}
86+
return mirrorSet{source: extractRegistryHostname(srcImage), mirrors: truncatedMirrors, mirrorSourcePolicy: mirrorSourcePolicy}
8687
}
8788

88-
// extractHostname extracts just the initial host repo from a full image location
89-
// e.g. mcr.microsoft.com would be extracted from mcr.microsoft.com/oss/kubernetes/pause:3.9
90-
func extractHostname(fullImage string) string {
91-
parts := strings.Split(fullImage, imagePathSeparator)
92-
return parts[0]
89+
// extractRegistryHostname extracts just the initial host repo from a full image location, as containerd does not allow
90+
// registries to exist on a subpath, given an input image `mcr.microsoft.com/oss/kubernetes/pause:3.9`,
91+
// mcr.microsoft.com would be the determined registry hostname.
92+
func extractRegistryHostname(fullImage string) string {
93+
// url.Parse will only work if URL has a scheme (https://)
94+
if parsedURL, err := url.Parse(fullImage); err == nil && parsedURL.Hostname() != "" {
95+
if parsedURL.Port() != "" {
96+
return parsedURL.Hostname() + ":" + parsedURL.Port()
97+
}
98+
return parsedURL.Hostname()
99+
}
100+
// For URLs without a scheme, just return everything before the first `/`
101+
return strings.Split(fullImage, imagePathSeparator)[0]
93102
}
94103

95104
// getMergedMirrorSets extracts and merges the contents of the given mirror sets.
@@ -230,7 +239,7 @@ func (ms *mirrorSet) generateConfig(secretsConfig credentialprovider.DockerConfi
230239
result += "\n"
231240

232241
// Extract the mirror repo's authorization credentials, if one exists
233-
if entry, ok := secretsConfig.Auths[extractHostname(m.host)]; ok {
242+
if entry, ok := secretsConfig.Auths[extractRegistryHostname(m.host)]; ok {
234243
credentials := entry.Username + ":" + entry.Password
235244
token := base64.StdEncoding.EncodeToString([]byte(credentials))
236245

pkg/registries/registries_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestGetMergedMirrorSets(t *testing.T) {
7979
Spec: config.ImageDigestMirrorSetSpec{
8080
ImageDigestMirrors: []config.ImageDigestMirrors{
8181
{
82-
Source: "source1",
82+
Source: "https://source1.local:5000",
8383
Mirrors: []config.ImageMirror{"mirror1"},
8484
MirrorSourcePolicy: config.AllowContactingSource,
8585
},
@@ -102,7 +102,7 @@ func TestGetMergedMirrorSets(t *testing.T) {
102102
},
103103
expectedOutput: []mirrorSet{
104104
{
105-
source: "source1",
105+
source: "source1.local:5000",
106106
mirrors: []mirror{{host: "mirror1", resolveTags: false}},
107107
mirrorSourcePolicy: config.AllowContactingSource,
108108
},
@@ -133,7 +133,7 @@ func TestGetMergedMirrorSets(t *testing.T) {
133133
Spec: config.ImageTagMirrorSetSpec{
134134
ImageTagMirrors: []config.ImageTagMirrors{
135135
{
136-
Source: "mcr.microsoft.com/oss/kubernetes/pause",
136+
Source: "docker://mcr.microsoft.com/oss/kubernetes/pause",
137137
Mirrors: []config.ImageMirror{"quay.io/testuser/oss/kubernetes/pause"},
138138
MirrorSourcePolicy: config.AllowContactingSource,
139139
},

0 commit comments

Comments
 (0)