/
caveats_test.go
313 lines (212 loc) · 6.45 KB
/
caveats_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
package caveats
// https://pkg.go.dev/github.com/superfly/macaroon
// https://fly.io/blog/macaroons-escalated-quickly/
import (
"context"
"fmt"
"path/filepath"
"testing"
"time"
sfom_macaroon "github.com/sfomuseum/go-macaroon"
flyio_macaroon "github.com/superfly/macaroon"
)
func TestSFOMuseumCaveats(t *testing.T) {
ctx := context.Background()
abs_path, err := filepath.Abs(".")
if err != nil {
t.Fatalf("Failed to derive absolute path, %v", err)
}
path_key := filepath.Join(abs_path, "../fixtures/signing.key")
key_uri := fmt.Sprintf("file://%s", path_key)
loc := "sfomuseum.org"
duration := "PT10S"
m_uri, err := sfom_macaroon.NewMacaroonURI(loc, key_uri, duration)
if err != nil {
t.Fatalf("Failed to create URI, %v", err)
}
m, err := sfom_macaroon.NewMacaroon(ctx, m_uri)
if err != nil {
t.Fatalf("Failed to create new macaroon, %v", err)
}
// Allowed users
c1 := &isUserCaveat{
Users: []string{"bob", "alice"},
}
// Required role
c2 := &hasRoleCaveat{
Role: "staff",
}
err = m.Add(c1, c2)
if err != nil {
t.Fatalf("Failed to add caveats, %v", err)
}
enc, err := sfom_macaroon.EncodeMacaroonAsBase64(m, false)
if err != nil {
t.Fatalf("Failed to encode, %v", err)
}
fmt.Printf("Macaroon expires %v\n", m.Expiration())
fmt.Println("Sleeping 9 seconds...")
time.Sleep(9 * time.Second)
m2, err := sfom_macaroon.DecodeMacaroonFromBase64(enc, false)
if err != nil {
t.Fatalf("Failed to decode, %v", err)
}
key, err := sfom_macaroon.NewSigningKey(ctx, key_uri)
if err != nil {
t.Fatalf("Failed to retrieve signing key for %s, %v", key_uri, err)
}
cs, err := m2.Verify(key, [][]byte{}, nil)
if err != nil {
t.Fatalf("Failed to verify, %v", err)
}
err = cs.Validate(
&isUserAccess{User: "alice"},
&hasRoleAccess{Role: "staff"},
)
if err != nil {
t.Fatalf("Failed to validate alice, %v", err)
}
fmt.Println("OK alice")
err = cs.Validate(
&isUserAccess{User: "bob"},
&hasRoleAccess{Role: "staff"},
)
if err != nil {
t.Fatalf("Failed to validate bob, %v", err)
}
fmt.Println("OK bob")
err = cs.Validate(
&isUserAccess{User: "doug"},
&hasRoleAccess{Role: "staff"},
)
if err == nil {
t.Fatalf("NO DOUG FOR YOU")
}
fmt.Println("DENY doug (this is good)")
fmt.Println("Sleep again 2 seconds...")
time.Sleep(2 * time.Second)
err = cs.Validate(
&isUserAccess{User: "bob"},
&hasRoleAccess{Role: "staff"},
)
if err == nil {
t.Fatalf("Macaroon should have expired")
}
fmt.Printf("Failed to validate bob, %v (this is good)\n", err)
if !sfom_macaroon.IsExpired(m2) {
t.Fatalf("Macaroon should be expired")
}
}
func TestSFOMuseumThirdPartyCaveats(t *testing.T) {
ctx := context.Background()
abs_path, err := filepath.Abs(".")
if err != nil {
t.Fatalf("Failed to derive absolute path, %v", err)
}
path_key := filepath.Join(abs_path, "../fixtures/signing.key")
key_uri := fmt.Sprintf("file://%s", path_key)
path_encryption_key := filepath.Join(abs_path, "../fixtures/3p-encryption.key")
encryption_key_uri := fmt.Sprintf("file://%s", path_encryption_key)
key_ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
fmt.Printf("Load sigining key, %s\n", key_uri)
key, err := sfom_macaroon.NewSigningKey(key_ctx, key_uri)
if err != nil {
t.Fatalf("Failed to retrieve signing key for %s, %v", key_uri, err)
}
fmt.Printf("Load encryption key, %s\n", encryption_key_uri)
shared_key, err := sfom_macaroon.NewEncryptionKey(key_ctx, encryption_key_uri)
if err != nil {
t.Fatalf("Failed to load encyption key from %s, %v", encryption_key_uri, err)
}
loc := "sfomuseum.org"
duration := "PT10S"
m_uri, err := sfom_macaroon.NewMacaroonURI(loc, key_uri, duration)
if err != nil {
t.Fatalf("Failed to create URI, %v", err)
}
m, err := sfom_macaroon.NewMacaroon(ctx, m_uri)
if err != nil {
t.Fatalf("Failed to create new macaroon, %v", err)
}
tp_c := &dayOfWeekCaveat{
Days: []string{"Friday"},
}
tp_loc := "example.com"
err = m.Add3P(shared_key, tp_loc, tp_c)
if err != nil {
t.Fatalf("Failed to add 3P, %v", err)
}
// END OF add third-party caveat here
enc, err := sfom_macaroon.EncodeMacaroonAsBase64(m, false)
if err != nil {
t.Fatalf("Failed to encode macaroon, %v", err)
}
// Pretend we are sending enc somewhere...
fmt.Println("Sleeping 2 seconds to simulate exchanging macaroon...")
time.Sleep(2 * time.Second)
// Pretend enc is being received somewhere...
m2, err := sfom_macaroon.DecodeMacaroonFromBase64(enc, false)
if err != nil {
t.Fatalf("Failed to decode macaroon, %v", err)
}
// START OF third-party discharges
discharges := make([][]byte, 0)
discharge_keys := make(map[string]flyio_macaroon.EncryptionKey)
tp, err := m2.ThirdPartyTicket(tp_loc)
if err != nil {
t.Fatalf("Failed to get third party tickets, %v", err)
}
// Pretend we are sending tp to loc here...
fmt.Println("Sleeping 2 seconds to simulate sending discharge ticket...")
time.Sleep(2 * time.Second)
tp_caveats, tp_discharge, err := flyio_macaroon.DischargeTicket(shared_key, tp_loc, tp)
if err != nil {
t.Fatalf("Failed to parse discharge ticket, %v", err)
}
tp_cs := flyio_macaroon.NewCaveatSet(tp_caveats...)
err = tp_cs.Validate(
&dayOfWeekAccess{Day: "Friday"},
)
if err != nil {
// Pretend tp_loc is sending back an error here
t.Fatalf("TP dispatch failed to validate CS, %v", err)
}
encd, err := tp_discharge.Encode()
if err != nil {
t.Fatalf("Failed to create discharge token, %v", err)
}
// Pretend tp_loc is sending back 'encd' here...
fmt.Println("Sleeping 2 seconds to simulate receiving discharge token...")
time.Sleep(2 * time.Second)
discharges = append(discharges, encd)
discharge_keys[tp_loc] = shared_key
// END OF third-party discharges
// Verify with all the other caveats
cs, err := m2.Verify(key, discharges, discharge_keys)
if err != nil {
t.Fatalf("Failed to verify macaroon, %v", err)
}
// Test caveats (defined in sfomuseum.TestMacaroon)
err = cs.Validate(
&isUserAccess{User: "alice"},
&hasRoleAccess{Role: "staff"},
)
if err != nil {
t.Fatalf("Failed to validate alice, %v", err)
}
fmt.Println("All caveats validate")
fmt.Println("Sleeping 4 seconds to trigger expiration")
time.Sleep(4 * time.Second)
err = cs.Validate(
&isUserAccess{User: "alice"},
&hasRoleAccess{Role: "staff"},
)
if err == nil {
t.Fatalf("Expected macaroon to be expired")
}
if !sfom_macaroon.IsExpired(m2) {
t.Fatalf("Expected macaroon to be expired")
}
fmt.Printf("Macaroon expired, %v", err)
}