Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/image-builder-mk3/pkg/auth/auth_test.go
2500 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package auth
6
7
import (
8
"encoding/base64"
9
"testing"
10
11
"github.com/google/go-cmp/cmp"
12
)
13
14
func TestIsECRRegistry(t *testing.T) {
15
tests := []struct {
16
Registry string
17
Expectation bool
18
}{
19
{Registry: "422899872803.dkr.ecr.eu-central-1.amazonaws.com/private-repo-demo:latest", Expectation: true},
20
{Registry: "422899872803.dkr.ecr.eu-central-1.amazonaws.com", Expectation: true},
21
{Registry: "index.docker.io/foo:bar", Expectation: false},
22
}
23
24
for _, test := range tests {
25
t.Run(test.Registry, func(t *testing.T) {
26
act := isECRRegistry(test.Registry)
27
28
if diff := cmp.Diff(test.Expectation, act); diff != "" {
29
t.Errorf("isECRRegistry() mismatch (-want +got):\n%s", diff)
30
}
31
})
32
}
33
}
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
}
129
130