5
5
package auth
6
6
7
7
import (
8
+ "encoding/base64"
8
9
"testing"
9
10
10
11
"github.com/google/go-cmp/cmp"
@@ -30,3 +31,98 @@ func TestIsECRRegistry(t *testing.T) {
30
31
})
31
32
}
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
+ }
0 commit comments