Skip to content

Commit 1777636

Browse files
authored
Merge pull request #206 from nalind/pull-with-auth
Authenticate to registries when pulling images
2 parents ac0ec94 + 83be342 commit 1777636

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.

0 commit comments

Comments
 (0)