-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfallback_test.go
409 lines (370 loc) · 10.4 KB
/
fallback_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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package oso
import (
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"testing"
"github.com/dhuan/mock/pkg/mock"
)
const (
MOCK_SERVER_HOST = "localhost"
MOCK_SERVER_PORT = "4000"
)
type TestServerType int
const (
USE_MOCK_SERVER TestServerType = iota
USE_REAL_SERVER TestServerType = iota
)
func resetMockServer() {
url := url.URL{
Scheme: "http",
Host: strings.Join([]string{MOCK_SERVER_HOST, MOCK_SERVER_PORT}, ":"),
Path: "__mock__/reset",
}
r, err := http.NewRequest("POST", url.String(), nil)
if err != nil {
panic(err)
}
r.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
panic(err)
}
defer res.Body.Close()
}
func assertCalled(method mock.ConditionValue, path string, count uint, t *testing.T) {
mockConfig := &mock.MockConfig{Url: strings.Join([]string{MOCK_SERVER_HOST, MOCK_SERVER_PORT}, ":")}
validationErrors, err := mock.Assert(mockConfig, &mock.AssertOptions{
Route: path,
Condition: &mock.Condition{
Type: mock.ConditionType_MethodMatch,
Value: method,
},
})
if err != nil {
t.Error(err)
}
if count == 0 {
for _, ve := range validationErrors {
if ve.Code == mock.ValidationErrorCode_NoCall {
return
}
}
t.Error("expected endpoint not to be called")
} else {
if len(validationErrors) > 0 {
t.Error(mock.ToReadableError(validationErrors))
}
}
}
type OsoTestClients struct {
valid OsoClientImpl
unreachable OsoClientImpl
httpError OsoClientImpl
http300 OsoClientImpl
http400 OsoClientImpl
http404 OsoClientImpl
}
// Get some test clients.
//
// Supports two cases.
//
// In one case we just want to test that the fallback URL is called, to test
// that all endpoints that we expect to fall back actually do. In this case we
// use the mock test server.
//
// In the other case, we want to use a real functional Oso test server, so
// that we can check that the request actually succeeds when the fallback
// service is running.
func getOsoTestClients(serverType TestServerType) OsoTestClients {
const realTestServer = "http://localhost:8081"
const mockTestServer = "http://localhost:4000"
var testServer string
if serverType == USE_MOCK_SERVER {
testServer = mockTestServer
} else {
testServer = realTestServer
}
const testServer300 = "http://localhost:4000/return-300"
const testServer404 = "http://localhost:4000/return-404"
const testServer400 = "http://localhost:4000/return-400"
const testServer500 = "http://localhost:4000/return-500"
const testServerNonexistent = "http://localhost:6000"
const apiKey = "e_0123456789_12345_osotesttoken01xiIn"
return OsoTestClients{
valid: NewClient(testServer, apiKey).(OsoClientImpl),
unreachable: NewClientWithFallbackUrl(testServerNonexistent, apiKey, testServer).(OsoClientImpl),
httpError: NewClientWithFallbackUrl(testServer500, apiKey, testServer).(OsoClientImpl),
http300: NewClientWithFallbackUrl(testServer300, apiKey, testServer).(OsoClientImpl),
http400: NewClientWithFallbackUrl(testServer400, apiKey, testServer).(OsoClientImpl),
http404: NewClientWithFallbackUrl(testServer404, apiKey, testServer).(OsoClientImpl),
}
}
type FallbackTestCase struct {
client OsoClient
expected_count uint
}
func getFallbackEligibilityTestCases() []FallbackTestCase {
testClients := getOsoTestClients(USE_MOCK_SERVER)
const apiKey = "e_0123456789_12345_osotesttoken01xiIn"
return []FallbackTestCase{
// NOTE: Fallback will be called after one 400 error because it does not
// retry.
{testClients.http404, 0},
{testClients.http400, 1},
{testClients.httpError, 1},
{testClients.unreachable, 1},
{testClients.http300, 0},
}
}
func user() Value {
return Value{Type: "User", ID: "bob"}
}
func repo() Value {
return Value{Type: "Repo", ID: "acme"}
}
func Test_AuthorizeFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.Authorize(
user(), "read", repo(),
)
assertCalled("post", "api/authorize", tc.expected_count, t)
resetMockServer()
}
}
func Test_ListFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.List(user(), "read", "Repo", []Fact{})
assertCalled("post", "api/list", tc.expected_count, t)
resetMockServer()
}
}
func Test_ActionsFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.Actions(
user(), repo(),
)
assertCalled("post", "api/actions", tc.expected_count, t)
resetMockServer()
}
}
func Test_BuildQueryFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
var mapping map[string][]string
repoVar := TypedVar("Repo")
action := TypedVar("String")
tc.client.BuildQuery(NewQueryFact(
"allow", user(), String("read"), repo(),
),
).Evaluate(&mapping, map[Variable]Variable{repoVar: action})
assertCalled("post", "api/evaluate_query", tc.expected_count, t)
resetMockServer()
}
}
func Test_AuthorizeLocalFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.AuthorizeLocal(
user(), "read", repo(),
)
assertCalled("post", "api/authorize_query", tc.expected_count, t)
resetMockServer()
}
}
func Test_ListLocalFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.ListLocal(user(), "read", "repo()", "id")
assertCalled("post", "api/list_query", tc.expected_count, t)
resetMockServer()
}
}
func Test_ActionsLocalFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.ActionsLocal(
user(), repo(),
)
assertCalled("post", "api/actions_query", tc.expected_count, t)
resetMockServer()
}
}
func Test_GetFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.Get(NewFactPattern("has_role", user(), nil, repo()))
assertCalled("get", "api/facts", tc.expected_count, t)
resetMockServer()
}
}
func Test_PolicyMetadataFallback(t *testing.T) {
testCases := getFallbackEligibilityTestCases()
for _, tc := range testCases {
tc.client.GetPolicyMetadata()
assertCalled("get", "api/policy_metadata", tc.expected_count, t)
resetMockServer()
}
}
func getTestFacts() []Fact {
return []Fact{
{
"has_permission",
[]Value{
{Type: "User", ID: "bob"},
String("read"),
{Type: "Repo", ID: "acme"},
},
},
{
"has_permission",
[]Value{
{Type: "User", ID: "alice"},
String("read"),
{Type: "Repo", ID: "acme"},
},
},
}
}
var initializeServer sync.Once
func getRealServerClients(t *testing.T) OsoTestClients {
testClients := getOsoTestClients(USE_REAL_SERVER)
initializeServer.Do(func() {
testClients.valid.clearData()
err := testClients.valid.Batch(func(tx BatchTransaction) {
for _, fact := range getTestFacts() {
tx.Insert(fact)
}
})
if err != nil {
t.Fatal("Failed to do test server setup: ", err)
}
})
return testClients
}
func Test_BatchFails(t *testing.T) {
testClients := getRealServerClients(t)
err := testClients.unreachable.Batch(func(tx BatchTransaction) {
tx.Insert(Fact{
"has_permission",
[]Value{
{Type: "User", ID: "alice"},
String("read"),
{Type: "Repo", ID: "acme"},
},
})
})
if err == nil {
t.Fatal("Expected insert to fail")
}
assertCalled("post", "api/batch", 0, t)
resetMockServer()
}
func Test_AuthorizeIfOsoIsUnreachable(t *testing.T) {
testClients := getRealServerClients(t)
res, err := testClients.unreachable.Authorize(
Value{Type: "User", ID: "bob"},
"read",
Value{Type: "Repo", ID: "acme"},
)
if err != nil {
t.Fatal("Expected authorize to succeed: ", err)
}
if res != true {
t.Fatal("Expected authorize to be true")
}
}
func Test_GetIfOsoIsUnreachable(t *testing.T) {
testClients := getRealServerClients(t)
perms, err := testClients.unreachable.Get(
NewFactPattern("has_permission", nil, String("read"), nil),
)
if err != nil {
t.Fatal("Expected get to succeed")
}
if len(perms) != len(getTestFacts()) {
t.Fatal("Expected permissions length to equal length of all our facts")
}
}
func Test_AuthorizeIfOsoReturnsHttpError(t *testing.T) {
testClients := getRealServerClients(t)
res, err := testClients.httpError.Authorize(
Value{Type: "User", ID: "bob"},
"read",
Value{Type: "Repo", ID: "acme"},
)
if err != nil {
t.Fatal("Expected authorize to succeed: ", err)
}
if res != true {
t.Fatal("Expected authorize to be true")
}
}
func Test_GetIfOsoReturnsHttpError(t *testing.T) {
testClients := getRealServerClients(t)
perms, err := testClients.unreachable.Get(
NewFactPattern("has_permission", nil, String("read"), nil),
)
if err != nil {
t.Fatal("Expected get to succeed")
}
if len(perms) != len(getTestFacts()) {
t.Fatal("Expected permissions length to equal length of all our facts")
}
}
func Test_AuthorizeIfOsoReturnsHttp400(t *testing.T) {
testClients := getRealServerClients(t)
res, err := testClients.http400.Authorize(
Value{Type: "User", ID: "bob"},
"read",
Value{Type: "Repo", ID: "acme"},
)
if err != nil {
t.Fatal("Expected authorize to succeed: ", err)
}
if res != true {
t.Fatal("Expected authorize to be true")
}
}
func Test_GetIfOsoReturnsHttp400(t *testing.T) {
testClients := getRealServerClients(t)
perms, err := testClients.http400.Get(
NewFactPattern("has_permission", nil, String("read"), nil),
)
if err != nil {
t.Fatal("Expected get to succeed")
}
if len(perms) != len(getTestFacts()) {
t.Fatal("Expected permissions length to equal length of all our facts")
}
}
func Test_FallbackEndToEnd(t *testing.T) {
oso := NewClientWithFallbackUrl("http://localhost:6000", "e_0123456789_12345_osotesttoken01xiIn", "http://localhost:8081")
user := Value{Type: "User", ID: fmt.Sprintf("%v", idCounter)}
idCounter++
acme := Value{Type: "Repo", ID: fmt.Sprintf("%v", idCounter)}
idCounter++
t.Run("tell", func(t *testing.T) {
e := oso.Insert(NewFact("has_permission", user, String("read"), acme))
if e == nil {
t.Fatalf("Insert should fail because it is not supported by fallback")
}
})
t.Run("authorize", func(t *testing.T) {
result, e := oso.AuthorizeWithContext(user, "read", acme, []Fact{
{
Predicate: "has_permission",
Args: []Value{user, String("read"), acme},
},
})
if e != nil || result != true {
t.Fatalf("Expect authorize to succeed")
}
})
}