Skip to content

Commit 88e30ff

Browse files
committed
[image-builder-mk3] handle host:port:token for auth
1 parent 9757b51 commit 88e30ff

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

components/image-builder-mk3/pkg/auth/auth.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,14 @@ func (a AllowedAuthFor) additionalAuth(domain string) *Authentication {
383383
dec, err := base64.StdEncoding.DecodeString(ath)
384384
if err == nil {
385385
segs := strings.Split(string(dec), ":")
386-
if len(segs) > 1 {
387-
res.Username = segs[0]
388-
res.Password = strings.Join(segs[1:], ":")
386+
numSegs := len(segs)
387+
388+
if numSegs > 1 {
389+
res.Username = strings.Join(segs[:numSegs-1], ":")
390+
res.Password = segs[numSegs-1]
389391
}
390392
} else {
391-
log.Errorf("failed getting additional auth")
393+
log.WithError(err).Warn("failed to decode base64 auth string in additionalAuth")
392394
}
393395
return res
394396
}

components/image-builder-mk3/pkg/auth/auth_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package auth
66

77
import (
8+
"encoding/base64"
89
"testing"
910

1011
"github.com/google/go-cmp/cmp"
@@ -30,3 +31,98 @@ func TestIsECRRegistry(t *testing.T) {
3031
})
3132
}
3233
}
34+
35+
func TestAdditionalAuth(t *testing.T) {
36+
tests := []struct {
37+
name string
38+
domain string
39+
additionalMap map[string]string
40+
expectedAuth *Authentication
41+
}{
42+
{
43+
name: "standard host:token",
44+
domain: "myregistry.com",
45+
additionalMap: map[string]string{
46+
"myregistry.com": base64.StdEncoding.EncodeToString([]byte("myregistry.com:mytoken")),
47+
},
48+
expectedAuth: &Authentication{
49+
Username: "myregistry.com",
50+
Password: "mytoken",
51+
Auth: base64.StdEncoding.EncodeToString([]byte("myregistry.com:mytoken")),
52+
},
53+
},
54+
{
55+
name: "buggy host:port:token",
56+
domain: "myregistry.com:5000",
57+
additionalMap: map[string]string{
58+
"myregistry.com:5000": base64.StdEncoding.EncodeToString([]byte("myregistry.com:5000:mytoken")),
59+
},
60+
expectedAuth: &Authentication{
61+
Username: "myregistry.com:5000",
62+
Password: "mytoken",
63+
Auth: base64.StdEncoding.EncodeToString([]byte("myregistry.com:5000:mytoken")),
64+
},
65+
},
66+
{
67+
name: "only username, no password/token (single segment)",
68+
domain: "useronly.com",
69+
additionalMap: map[string]string{
70+
"useronly.com": base64.StdEncoding.EncodeToString([]byte("justauser")),
71+
},
72+
expectedAuth: &Authentication{
73+
Auth: base64.StdEncoding.EncodeToString([]byte("justauser")),
74+
},
75+
},
76+
{
77+
name: "empty auth string",
78+
domain: "emptyauth.com",
79+
additionalMap: map[string]string{
80+
"emptyauth.com": base64.StdEncoding.EncodeToString([]byte("")),
81+
},
82+
expectedAuth: &Authentication{
83+
Auth: base64.StdEncoding.EncodeToString([]byte("")),
84+
},
85+
},
86+
{
87+
name: "domain not in map",
88+
domain: "notfound.com",
89+
additionalMap: map[string]string{"someother.com": base64.StdEncoding.EncodeToString([]byte("someauth"))},
90+
expectedAuth: nil,
91+
},
92+
{
93+
name: "invalid base64 string",
94+
domain: "invalidbase64.com",
95+
additionalMap: map[string]string{
96+
"invalidbase64.com": "!!!INVALID_BASE64!!!",
97+
},
98+
expectedAuth: &Authentication{
99+
Auth: "!!!INVALID_BASE64!!!",
100+
},
101+
},
102+
{
103+
name: "standard host:token where username in cred is different from domain key",
104+
domain: "docker.io",
105+
additionalMap: map[string]string{
106+
"docker.io": base64.StdEncoding.EncodeToString([]byte("user1:pass1")),
107+
},
108+
expectedAuth: &Authentication{
109+
Username: "user1",
110+
Password: "pass1",
111+
Auth: base64.StdEncoding.EncodeToString([]byte("user1:pass1")),
112+
},
113+
},
114+
}
115+
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
aaf := AllowedAuthFor{
119+
Additional: tt.additionalMap,
120+
}
121+
actualAuth := aaf.additionalAuth(tt.domain)
122+
123+
if diff := cmp.Diff(tt.expectedAuth, actualAuth); diff != "" {
124+
t.Errorf("additionalAuth() mismatch (-want +got):\n%s", diff)
125+
}
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)