forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creds_test.go
296 lines (263 loc) · 8.15 KB
/
creds_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package tcclient_test
import (
"fmt"
"net/http"
"net/url"
"os"
"reflect"
"testing"
"time"
tcclient "github.com/taskcluster/taskcluster/v40/clients/client-go"
"github.com/taskcluster/taskcluster/v40/clients/client-go/tcauth"
"github.com/taskcluster/taskcluster/v40/internal/testrooturl"
)
func ExampleCredentials_CreateTemporaryCredentials() {
permaCreds := tcclient.Credentials{
ClientID: os.Getenv("TASKCLUSTER_CLIENT_ID"),
AccessToken: os.Getenv("TASKCLUSTER_ACCESS_TOKEN"),
}
tempCreds, err := permaCreds.CreateTemporaryCredentials(24*time.Hour, "dummy:scope:1", "dummy:scope:2")
if err != nil {
// handle error
}
fmt.Printf("Temporary creds:\n%q\n", tempCreds)
}
func Test_CreateTemporaryCredentials_WellFormed(t *testing.T) {
// fake credentials
permaCreds := tcclient.Credentials{
ClientID: "permacred",
AccessToken: "eHMnHH7PTSqplJSC_qAJ2QKGt8egfvRaqxczIRgOScaw",
}
tempCreds, err := permaCreds.CreateTemporaryCredentials(24*time.Hour, "scope1")
if err != nil {
t.Error(err)
}
if tempCreds.AuthorizedScopes != nil {
t.Errorf("temp creds have AuthorizedScopes!?")
}
if tempCreds.ClientID != permaCreds.ClientID {
t.Errorf("%s != %s", tempCreds.ClientID, permaCreds.ClientID)
}
// Certificate and AccessToken are nondeterministic; we rely on other tests
// to verify them
}
// This clientId/accessToken pair is recognized as valid by the testAutheticate endpoint
var testCreds = &tcclient.Credentials{
ClientID: "tester",
AccessToken: "no-secret",
}
func checkAuthenticate(t *testing.T, response *tcauth.TestAuthenticateResponse, err error, expectedClientID string, expectedScopes []string) {
if err != nil {
t.Error(err)
return
}
if response.ClientID != expectedClientID {
t.Errorf("got unexpected clientId %s", response.ClientID)
}
if !reflect.DeepEqual(response.Scopes, expectedScopes) {
t.Errorf("got unexpected scopes %#v", response.Scopes)
}
}
func Test_PermaCred(t *testing.T) {
client := tcauth.New(testCreds, testrooturl.Get(t))
response, err := client.TestAuthenticate(&tcauth.TestAuthenticateRequest{
ClientScopes: []string{"scope:*"},
RequiredScopes: []string{"scope:this"},
})
checkAuthenticate(t, response, err,
"tester", []string{"assume:anonymous", "scope:*"})
}
func Test_TempCred(t *testing.T) {
tempCreds, err := testCreds.CreateTemporaryCredentials(1*time.Hour, "scope:1", "scope:2")
if err != nil {
t.Error(err)
return
}
client := tcauth.New(tempCreds, testrooturl.Get(t))
response, err := client.TestAuthenticate(&tcauth.TestAuthenticateRequest{
ClientScopes: []string{"scope:*"},
RequiredScopes: []string{"scope:1"},
})
checkAuthenticate(t, response, err,
"tester", []string{"assume:anonymous", "scope:1", "scope:2"})
}
func Test_NamedTempCred(t *testing.T) {
tempCreds, err := testCreds.CreateNamedTemporaryCredentials("jimmy", 1*time.Hour,
"scope:1", "scope:2")
if err != nil {
t.Error(err)
return
}
client := tcauth.New(tempCreds, testrooturl.Get(t))
response, err := client.TestAuthenticate(&tcauth.TestAuthenticateRequest{
ClientScopes: []string{"scope:*", "auth:create-client:jimmy"},
RequiredScopes: []string{"scope:1"},
})
checkAuthenticate(t, response, err,
"jimmy", []string{"assume:anonymous", "scope:1", "scope:2"})
}
func Test_PermaCred_Bewit(t *testing.T) {
client := tcauth.New(testCreds, testrooturl.Get(t))
url, err := client.TestAuthenticateGet_SignedURL(15 * time.Minute)
if err != nil {
t.Error(err)
return
}
resp, err := http.Get(url.String())
if err != nil {
t.Fatalf("Got error when fetching %v: %v", url, err)
}
if 200 != resp.StatusCode {
t.Fatalf("Got unexpected statusCode %d", resp.StatusCode)
return
}
}
func Test_PermaCred_Bewit_SignedURL(t *testing.T) {
client := tcclient.Client{Credentials: testCreds}
url, err := client.SignedURL(testrooturl.Get(t)+"/api/auth/v1/test-authenticate-get/", url.Values{}, 15*time.Minute)
if err != nil {
t.Error(err)
return
}
resp, err := http.Get(url.String())
if err != nil {
t.Fatalf("Got error when fetching %v: %v", url, err)
}
if 200 != resp.StatusCode {
t.Fatalf("Got unexpected statusCode %d", resp.StatusCode)
return
}
}
func Test_TempCred_Bewit(t *testing.T) {
tempCreds, err := testCreds.CreateTemporaryCredentials(1*time.Hour, "test:authenticate-get")
if err != nil {
t.Error(err)
return
}
client := tcauth.New(tempCreds, testrooturl.Get(t))
url, err := client.TestAuthenticateGet_SignedURL(15 * time.Minute)
if err != nil {
t.Error(err)
return
}
resp, err := http.Get(url.String())
if err != nil {
t.Fatalf("Got error when fetching %v: %v", url, err)
}
if 200 != resp.StatusCode {
t.Fatalf("Got unexpected statusCode %d", resp.StatusCode)
return
}
}
func Test_TempCred_Bewit_WrongScope(t *testing.T) {
tempCreds, err := testCreds.CreateTemporaryCredentials(1*time.Hour, "test:not-the-scope-you-need")
if err != nil {
t.Error(err)
return
}
client := tcauth.New(tempCreds, testrooturl.Get(t))
url, err := client.TestAuthenticateGet_SignedURL(15 * time.Minute)
if err != nil {
t.Error(err)
return
}
resp, err := http.Get(url.String())
if err != nil {
t.Fatalf("Got error when fetching %v: %v", url, err)
}
if 403 != resp.StatusCode {
t.Fatalf("Got unexpected statusCode %d", resp.StatusCode)
return
}
}
func Test_AuthScopes_Bewit(t *testing.T) {
authCreds := *testCreds
authCreds.AuthorizedScopes = []string{"test:authenticate-get"}
client := tcauth.New(&authCreds, testrooturl.Get(t))
url, err := client.TestAuthenticateGet_SignedURL(15 * time.Minute)
if err != nil {
t.Error(err)
return
}
resp, err := http.Get(url.String())
if err != nil {
t.Fatalf("Got error when fetching %v: %v", url, err)
}
if 200 != resp.StatusCode {
t.Fatalf("Got unexpected statusCode %d", resp.StatusCode)
return
}
}
func Test_TempCred_NoClientId(t *testing.T) {
baseCreds := tcclient.Credentials{AccessToken: "no-secret"}
_, err := baseCreds.CreateTemporaryCredentials(1*time.Hour, "s")
if err == nil {
t.Errorf("expected error")
}
}
func Test_TempCred_NoAccessToken(t *testing.T) {
baseCreds := tcclient.Credentials{ClientID: "tester"}
_, err := baseCreds.CreateTemporaryCredentials(1*time.Hour, "s")
if err == nil {
t.Errorf("expected error")
}
}
func Test_TempCred_TempBase(t *testing.T) {
baseCreds := tcclient.Credentials{
ClientID: "tester",
AccessToken: "no-secret",
Certificate: "{}",
}
_, err := baseCreds.CreateTemporaryCredentials(1*time.Hour, "s")
if err == nil {
t.Errorf("expected error")
}
}
func Test_TempCred_TooLong(t *testing.T) {
_, err := testCreds.CreateTemporaryCredentials(32*24*time.Hour, "s")
if err == nil {
t.Errorf("expected error")
}
}
func Test_AuthorizedScopes(t *testing.T) {
authCreds := *testCreds
authCreds.AuthorizedScopes = []string{"scope:1", "scope:3"}
client := tcauth.New(&authCreds, testrooturl.Get(t))
response, err := client.TestAuthenticate(&tcauth.TestAuthenticateRequest{
ClientScopes: []string{"scope:*"},
RequiredScopes: []string{"scope:1"},
})
checkAuthenticate(t, response, err,
"tester", []string{"assume:anonymous", "scope:1", "scope:3"})
}
func Test_TempCredWithAuthorizedScopes(t *testing.T) {
tempCreds, err := testCreds.CreateTemporaryCredentials(1*time.Hour, "scope:1", "scope:2")
if err != nil {
t.Error(err)
return
}
tempCreds.AuthorizedScopes = []string{"scope:1"}
client := tcauth.New(tempCreds, testrooturl.Get(t))
response, err := client.TestAuthenticate(&tcauth.TestAuthenticateRequest{
ClientScopes: []string{"scope:*"},
RequiredScopes: []string{"scope:1"},
})
checkAuthenticate(t, response, err,
"tester", []string{"assume:anonymous", "scope:1"})
}
func Test_NamedTempCredWithAuthorizedScopes(t *testing.T) {
tempCreds, err := testCreds.CreateNamedTemporaryCredentials("julie", 1*time.Hour,
"scope:1", "scope:2")
if err != nil {
t.Error(err)
return
}
tempCreds.AuthorizedScopes = []string{"scope:1"} // note: no create-client
client := tcauth.New(tempCreds, testrooturl.Get(t))
response, err := client.TestAuthenticate(&tcauth.TestAuthenticateRequest{
ClientScopes: []string{"scope:*", "auth:create-client:j*"},
RequiredScopes: []string{"scope:1"},
})
checkAuthenticate(t, response, err,
"julie", []string{"assume:anonymous", "scope:1"})
}