Skip to content

Commit 83be342

Browse files
committedJan 10, 2022
Authenticate to registries when pulling images
When we attempt to pull images, use authentication secrets, if we can find some that match. We special-case docker.io and locations that end in .docker.io to also check for authentication information tagged for either docker.io, index.docker.io, or https://index.docker.io/v1/. Signed-off-by: Nalin Dahyabhai <[email protected]>
1 parent ac0ec94 commit 83be342

File tree

10 files changed

+1296
-1
lines changed

10 files changed

+1296
-1
lines changed
 

‎cmd/imagebuilder/imagebuilder.go

+52
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010

11+
"github.com/docker/distribution/reference"
1112
dockertypes "github.com/docker/docker/api/types"
1213
docker "github.com/fsouza/go-dockerclient"
1314
"k8s.io/klog"
@@ -87,7 +88,58 @@ func main() {
8788
options.TransientMounts = mounts
8889

8990
options.Out, options.ErrOut = os.Stdout, os.Stderr
91+
authConfigurations, err := docker.NewAuthConfigurationsFromDockerCfg()
92+
if err != nil {
93+
log.Fatalf("reading authentication configurations: %v", err)
94+
}
95+
if authConfigurations == nil {
96+
klog.V(4).Infof("No authentication secrets found")
97+
}
98+
9099
options.AuthFn = func(name string) ([]dockertypes.AuthConfig, bool) {
100+
if authConfigurations != nil {
101+
if authConfig, ok := authConfigurations.Configs[name]; ok {
102+
klog.V(4).Infof("Found authentication secret for registry %q", name)
103+
return []dockertypes.AuthConfig{{
104+
Username: authConfig.Username,
105+
Password: authConfig.Password,
106+
Email: authConfig.Email,
107+
ServerAddress: authConfig.ServerAddress,
108+
}}, true
109+
}
110+
if named, err := reference.ParseNormalizedNamed(name); err == nil {
111+
domain := reference.Domain(named)
112+
if authConfig, ok := authConfigurations.Configs[domain]; ok {
113+
klog.V(4).Infof("Found authentication secret for registry %q", domain)
114+
return []dockertypes.AuthConfig{{
115+
Username: authConfig.Username,
116+
Password: authConfig.Password,
117+
Email: authConfig.Email,
118+
ServerAddress: authConfig.ServerAddress,
119+
}}, true
120+
}
121+
if domain == "docker.io" || strings.HasSuffix(domain, ".docker.io") {
122+
var auths []dockertypes.AuthConfig
123+
for _, aka := range []string{"docker.io", "index.docker.io", "https://index.docker.io/v1/"} {
124+
if aka == domain {
125+
continue
126+
}
127+
if authConfig, ok := authConfigurations.Configs[aka]; ok {
128+
klog.V(4).Infof("Found authentication secret for registry %q", aka)
129+
auths = append(auths, dockertypes.AuthConfig{
130+
Username: authConfig.Username,
131+
Password: authConfig.Password,
132+
Email: authConfig.Email,
133+
ServerAddress: authConfig.ServerAddress,
134+
})
135+
}
136+
}
137+
if len(auths) > 0 {
138+
return auths, true
139+
}
140+
}
141+
}
142+
}
91143
return nil, false
92144
}
93145
options.LogFn = func(format string, args ...interface{}) {

‎go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
77
github.com/containerd/containerd v1.3.0
88
github.com/containers/storage v0.0.0-20181207174215-bf48aa83089d
9+
github.com/docker/distribution v2.7.1+incompatible
910
github.com/docker/docker v1.4.2-0.20170829193243-b68221c37ee5
1011
github.com/docker/go-connections v0.4.1-0.20180821093606-97c2040d34df // indirect
1112
github.com/docker/go-units v0.3.4-0.20181030082039-2fb04c6466a5 // indirect
@@ -20,7 +21,7 @@ require (
2021
github.com/opencontainers/runc v1.0.0-rc6.0.20190305074555-923a8f8a9a07 // indirect
2122
github.com/pkg/errors v0.8.2-0.20190227000051-27936f6d90f9
2223
github.com/pquerna/ffjson v0.0.0-20171002144729-d49c2bc1aa13 // indirect
23-
github.com/sirupsen/logrus v1.3.1-0.20190306131408-d7b6bf5e4d26 // indirect
24+
github.com/sirupsen/logrus v1.3.1-0.20190306131408-d7b6bf5e4d26
2425
github.com/stretchr/testify v1.6.1
2526
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
2627
golang.org/x/net v0.0.0-20190107210223-45ffb0cd1ba0 // indirect

‎go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/containers/storage v0.0.0-20181207174215-bf48aa83089d/go.mod h1:+RirK
1111
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1212
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1313
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
15+
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
1416
github.com/docker/docker v1.4.2-0.20170829193243-b68221c37ee5 h1:2iTeIddeUKUPxzR4/Gy35WsvRk79n5sUA+g8RHKU4tc=
1517
github.com/docker/docker v1.4.2-0.20170829193243-b68221c37ee5/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
1618
github.com/docker/go-connections v0.4.1-0.20180821093606-97c2040d34df h1:cGbd/ECh4QPOc6+Tbvdk5NjCcOYESiwc1RjXp0XciVg=

‎vendor/github.com/docker/distribution/LICENSE

+202
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/docker/distribution/digestset/set.go

+247
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/docker/distribution/reference/helpers.go

+42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/docker/distribution/reference/normalize.go

+170
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/docker/distribution/reference/reference.go

+433
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/docker/distribution/reference/regexp.go

+143
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/modules.txt

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ github.com/containers/storage/pkg/system
2121
github.com/containers/storage/pkg/mount
2222
# github.com/davecgh/go-spew v1.1.1
2323
github.com/davecgh/go-spew/spew
24+
# github.com/docker/distribution v2.7.1+incompatible
25+
github.com/docker/distribution/reference
26+
github.com/docker/distribution/digestset
2427
# github.com/docker/docker v1.4.2-0.20170829193243-b68221c37ee5
2528
github.com/docker/docker/api/types
2629
github.com/docker/docker/pkg/system

0 commit comments

Comments
 (0)
Please sign in to comment.