Skip to content

Commit b910a42

Browse files
Björn BrauerMarkus Wolf
andauthored
Docker auth (nektos#891)
* feat: read docker credentials from local docker config * fix: url.Parse requires protocol Co-authored-by: Björn Brauer <zaubernerd@zaubernerd.de> * fix: docker decides by the existence of . or : if... ... the image is in a custom registry or not. Co-authored-by: Björn Brauer <zaubernerd@zaubernerd.de> * fix: make docker hostname detection more robust * test: mock docker config for getImagePullOptions test By default github actions have a docker config set with a token to pull images from docker hub. Co-authored-by: Markus Wolf <markus.wolf@new-work.se> Co-authored-by: Markus Wolf <markus.wolf@new-work.se>
1 parent 5bdb9ed commit b910a42

5 files changed

Lines changed: 74 additions & 2 deletions

File tree

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ github.com/docker/docker v20.10.6+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05b
408408
github.com/docker/docker v20.10.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
409409
github.com/docker/docker v20.10.10+incompatible h1:GKkP0T7U4ks6X3lmmHKC2QDprnpRJor2Z5a8m62R9ZM=
410410
github.com/docker/docker v20.10.10+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
411+
github.com/docker/docker-credential-helpers v0.6.3 h1:zI2p9+1NQYdnG6sMU26EX4aVGlqbInSQxQXLvzJ4RPQ=
411412
github.com/docker/docker-credential-helpers v0.6.3/go.mod h1:WRaJzqw3CTB9bk10avuGsjVBZsD05qeibJ1/TYlvc0Y=
412413
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
413414
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=

pkg/container/docker_auth.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package container
2+
3+
import (
4+
"strings"
5+
6+
"github.com/docker/cli/cli/config"
7+
"github.com/docker/cli/cli/config/credentials"
8+
"github.com/docker/docker/api/types"
9+
log "github.com/sirupsen/logrus"
10+
)
11+
12+
func LoadDockerAuthConfig(image string) (types.AuthConfig, error) {
13+
config, err := config.Load(config.Dir())
14+
if err != nil {
15+
log.Warnf("Could not load docker config: %v", err)
16+
return types.AuthConfig{}, err
17+
}
18+
19+
if !config.ContainsAuth() {
20+
config.CredentialsStore = credentials.DetectDefaultStore(config.CredentialsStore)
21+
}
22+
23+
hostName := "index.docker.io"
24+
index := strings.IndexRune(image, '/')
25+
if index > -1 && (strings.ContainsAny(image[:index], ".:") || image[:index] == "localhost") {
26+
hostName = image[:index]
27+
}
28+
29+
authConfig, err := config.GetAuthConfig(hostName)
30+
if err != nil {
31+
log.Warnf("Could not get auth config from docker config: %v", err)
32+
return types.AuthConfig{}, err
33+
}
34+
35+
return types.AuthConfig(authConfig), nil
36+
}

pkg/container/docker_pull.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func getImagePullOptions(ctx context.Context, input NewDockerPullExecutorInput)
7777
imagePullOptions := types.ImagePullOptions{
7878
Platform: input.Platform,
7979
}
80+
8081
if input.Username != "" && input.Password != "" {
8182
logger := common.Logger(ctx)
8283
logger.Debugf("using authentication for docker pull")
@@ -91,6 +92,21 @@ func getImagePullOptions(ctx context.Context, input NewDockerPullExecutorInput)
9192
return imagePullOptions, err
9293
}
9394

95+
imagePullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(encodedJSON)
96+
} else {
97+
authConfig, err := LoadDockerAuthConfig(input.Image)
98+
if err != nil {
99+
return imagePullOptions, err
100+
}
101+
if authConfig.Username == "" && authConfig.Password == "" {
102+
return imagePullOptions, nil
103+
}
104+
105+
encodedJSON, err := json.Marshal(authConfig)
106+
if err != nil {
107+
return imagePullOptions, err
108+
}
109+
94110
imagePullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(encodedJSON)
95111
}
96112

pkg/container/docker_pull_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/docker/cli/cli/config"
8+
79
log "github.com/sirupsen/logrus"
810
assert "github.com/stretchr/testify/assert"
911
)
@@ -35,15 +37,25 @@ func TestCleanImage(t *testing.T) {
3537
func TestGetImagePullOptions(t *testing.T) {
3638
ctx := context.Background()
3739

40+
config.SetDir("/non-existent/docker")
41+
3842
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{})
3943
assert.Nil(t, err, "Failed to create ImagePullOptions")
40-
assert.Equal(t, options.RegistryAuth, "", "RegistryAuth should be empty if no username or password is set")
44+
assert.Equal(t, "", options.RegistryAuth, "RegistryAuth should be empty if no username or password is set")
4145

4246
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
4347
Image: "",
4448
Username: "username",
4549
Password: "password",
4650
})
4751
assert.Nil(t, err, "Failed to create ImagePullOptions")
48-
assert.Equal(t, options.RegistryAuth, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9", "Username and Password should be provided")
52+
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9", options.RegistryAuth, "Username and Password should be provided")
53+
54+
config.SetDir("testdata/docker-pull-options")
55+
56+
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
57+
Image: "nektos/act",
58+
})
59+
assert.Nil(t, err, "Failed to create ImagePullOptions")
60+
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZFxuIiwic2VydmVyYWRkcmVzcyI6Imh0dHBzOi8vaW5kZXguZG9ja2VyLmlvL3YxLyJ9", options.RegistryAuth, "RegistryAuth should be taken from local docker config")
4961
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"auths": {
3+
"https://index.docker.io/v1/": {
4+
"auth": "dXNlcm5hbWU6cGFzc3dvcmQK"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)