-
Notifications
You must be signed in to change notification settings - Fork 22
/
authorizer.go
470 lines (402 loc) · 11.6 KB
/
authorizer.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package biscuit
import (
"errors"
"fmt"
"strings"
"github.com/biscuit-auth/biscuit-go/v2/datalog"
"github.com/biscuit-auth/biscuit-go/v2/pb"
"google.golang.org/protobuf/proto"
)
var (
ErrMissingSymbols = errors.New("biscuit: missing symbols")
ErrPolicyDenied = errors.New("biscuit: denied by policy")
ErrNoMatchingPolicy = errors.New("biscuit: denied by no matching policies")
)
type Authorizer interface {
AddAuthorizer(a ParsedAuthorizer)
AddBlock(b ParsedBlock)
AddFact(fact Fact)
AddRule(rule Rule)
AddCheck(check Check)
AddPolicy(policy Policy)
Authorize() error
Query(rule Rule) (FactSet, error)
Biscuit() *Biscuit
Reset()
PrintWorld() string
LoadPolicies([]byte) error
SerializePolicies() ([]byte, error)
}
type authorizer struct {
biscuit *Biscuit
baseWorld *datalog.World
world *datalog.World
baseSymbols *datalog.SymbolTable
symbols *datalog.SymbolTable
block_worlds []*datalog.World
checks []Check
policies []Policy
dirty bool
}
var _ Authorizer = (*authorizer)(nil)
type AuthorizerOption func(w *authorizer)
func WithWorldOptions(opts ...datalog.WorldOption) AuthorizerOption {
return func(a *authorizer) {
a.baseWorld = datalog.NewWorld(opts...)
}
}
func NewVerifier(b *Biscuit, opts ...AuthorizerOption) (Authorizer, error) {
a := &authorizer{
biscuit: b,
baseWorld: datalog.NewWorld(),
baseSymbols: defaultSymbolTable.Clone(),
checks: []Check{},
policies: []Policy{},
block_worlds: []*datalog.World{},
}
for _, opt := range opts {
opt(a)
}
a.world = a.baseWorld.Clone()
a.symbols = a.baseSymbols.Clone()
return a, nil
}
func (v *authorizer) AddAuthorizer(a ParsedAuthorizer) {
v.AddBlock(a.Block)
for _, p := range a.Policies {
v.AddPolicy(p)
}
}
func (v *authorizer) AddBlock(block ParsedBlock) {
for _, f := range block.Facts {
v.AddFact(f)
}
for _, r := range block.Rules {
v.AddRule(r)
}
for _, c := range block.Checks {
v.AddCheck(c)
}
}
func (v *authorizer) AddFact(fact Fact) {
v.world.AddFact(fact.convert(v.symbols))
}
func (v *authorizer) AddRule(rule Rule) {
v.world.AddRule(rule.convert(v.symbols))
}
func (v *authorizer) AddCheck(check Check) {
v.checks = append(v.checks, check)
}
func (v *authorizer) AddPolicy(policy Policy) {
v.policies = append(v.policies, policy)
}
func (v *authorizer) Authorize() error {
// if we load facts from the verifier before
// the token's fact and rules, we might get inconsistent symbols
// token ements should first be converted to builder elements
// with the token's symbol table, then converted back
// with the verifier's symbol table
for _, fact := range *v.biscuit.authority.facts {
f, err := fromDatalogFact(v.biscuit.symbols, fact)
if err != nil {
return fmt.Errorf("biscuit: verification failed: %s", err)
}
v.world.AddFact(f.convert(v.symbols))
}
for _, rule := range v.biscuit.authority.rules {
r, err := fromDatalogRule(v.biscuit.symbols, rule)
if err != nil {
return fmt.Errorf("biscuit: verification failed: %s", err)
}
v.world.AddRule(r.convert(v.symbols))
}
if err := v.world.Run(v.symbols); err != nil {
return err
}
v.dirty = true
var errs []error
for i, check := range v.checks {
c := check.convert(v.symbols)
successful := false
for _, query := range c.Queries {
res := v.world.QueryRule(query, v.symbols)
if len(*res) != 0 {
successful = true
break
}
}
if !successful {
debug := datalog.SymbolDebugger{
SymbolTable: v.symbols,
}
errs = append(errs, fmt.Errorf("failed to verify check #%d: %s", i, debug.Check(c)))
}
}
for i, check := range v.biscuit.authority.checks {
ch, err := fromDatalogCheck(v.biscuit.symbols, check)
if err != nil {
return fmt.Errorf("biscuit: verification failed: %s", err)
}
c := ch.convert(v.symbols)
successful := false
for _, query := range c.Queries {
res := v.world.QueryRule(query, v.symbols)
if len(*res) != 0 {
successful = true
break
}
}
if !successful {
debug := datalog.SymbolDebugger{
SymbolTable: v.symbols,
}
errs = append(errs, fmt.Errorf("failed to verify block 0 check #%d: %s", i, debug.Check(c)))
}
}
policyMatched := false
policyResult := ErrPolicyDenied
for _, policy := range v.policies {
if policyMatched {
break
}
for _, query := range policy.Queries {
res := v.world.QueryRule(query.convert(v.symbols), v.symbols)
if len(*res) != 0 {
switch policy.Kind {
case PolicyKindAllow:
policyResult = nil
policyMatched = true
case PolicyKindDeny:
policyResult = ErrPolicyDenied
policyMatched = true
}
break
}
}
}
// remove the rules from the vrifier and authority blocks
// so they are not affected by facts created by later blocks
v.world.ResetRules()
for i, block := range v.biscuit.blocks {
block_world := v.world.Clone()
for _, fact := range *block.facts {
f, err := fromDatalogFact(v.biscuit.symbols, fact)
if err != nil {
return fmt.Errorf("biscuit: verification failed: %s", err)
}
block_world.AddFact(f.convert(v.symbols))
}
for _, rule := range block.rules {
r, err := fromDatalogRule(v.biscuit.symbols, rule)
if err != nil {
return fmt.Errorf("biscuit: verification failed: %s", err)
}
block_world.AddRule(r.convert(v.symbols))
}
if err := block_world.Run(v.symbols); err != nil {
return err
}
for j, check := range block.checks {
ch, err := fromDatalogCheck(v.biscuit.symbols, check)
if err != nil {
return fmt.Errorf("biscuit: verification failed: %s", err)
}
c := ch.convert(v.symbols)
successful := false
for _, query := range c.Queries {
res := block_world.QueryRule(query, v.symbols)
if len(*res) != 0 {
successful = true
break
}
}
if !successful {
debug := datalog.SymbolDebugger{
SymbolTable: v.symbols,
}
errs = append(errs, fmt.Errorf("failed to verify block #%d check #%d: %s", i+1, j, debug.Check(c)))
}
}
block_world.ResetRules()
v.block_worlds = append(v.block_worlds, block_world)
}
if len(errs) > 0 {
errMsg := make([]string, len(errs))
for i, e := range errs {
errMsg[i] = e.Error()
}
return fmt.Errorf("biscuit: verification failed: %s", strings.Join(errMsg, ", "))
}
v.baseWorld = v.world.Clone()
v.baseSymbols = v.symbols.Clone()
if policyMatched {
return policyResult
} else {
return ErrNoMatchingPolicy
}
}
func (v *authorizer) Query(rule Rule) (FactSet, error) {
if err := v.world.Run(v.symbols); err != nil {
return nil, err
}
v.dirty = true
facts := v.world.QueryRule(rule.convert(v.symbols), v.symbols)
result := make([]Fact, 0, len(*facts))
for _, fact := range *facts {
f, err := fromDatalogFact(v.symbols, fact)
if err != nil {
return nil, err
}
result = append(result, *f)
}
return result, nil
}
func (v *authorizer) Biscuit() *Biscuit {
return v.biscuit
}
// Returns the content of the Datalog environment
// This will be empty until the call to Authorize(), where
// facts, rules and checks will be evaluated
func (v *authorizer) PrintWorld() string {
debug := datalog.SymbolDebugger{
SymbolTable: v.symbols,
}
return debug.World(v.world)
}
func (v *authorizer) Reset() {
v.world = v.baseWorld.Clone()
v.symbols = v.baseSymbols.Clone()
v.checks = []Check{}
v.policies = []Policy{}
v.dirty = false
}
func (v *authorizer) LoadPolicies(authorizerPolicies []byte) error {
pbPolicies := &pb.AuthorizerPolicies{}
if err := proto.Unmarshal(authorizerPolicies, pbPolicies); err != nil {
return fmt.Errorf("verifier: failed to load policies: %w", err)
}
switch pbPolicies.GetVersion() {
case 3:
return v.loadPoliciesV2(pbPolicies)
default:
return fmt.Errorf("verifier: unsupported policies version %d", pbPolicies.GetVersion())
}
}
func (v *authorizer) loadPoliciesV2(pbPolicies *pb.AuthorizerPolicies) error {
policySymbolTable := datalog.SymbolTable(pbPolicies.Symbols)
v.symbols = v.baseSymbols.Clone()
v.symbols.Extend(&policySymbolTable)
for _, pbFact := range pbPolicies.Facts {
fact, err := protoFactToTokenFactV2(pbFact)
if err != nil {
return fmt.Errorf("verifier: load policies v1: failed to convert datalog fact: %w", err)
}
v.world.AddFact(*fact)
}
for _, pbRule := range pbPolicies.Rules {
rule, err := protoRuleToTokenRuleV2(pbRule)
if err != nil {
return fmt.Errorf("verifier: load policies v1: failed to convert datalog rule: %w", err)
}
v.world.AddRule(*rule)
}
v.checks = make([]Check, len(pbPolicies.Checks))
for i, pbCheck := range pbPolicies.Checks {
dlCheck, err := protoCheckToTokenCheckV2(pbCheck)
if err != nil {
return fmt.Errorf("verifier: load policies v1: failed to convert datalog check: %w", err)
}
check, err := fromDatalogCheck(v.symbols, *dlCheck)
if err != nil {
return fmt.Errorf("verifier: load policies v1: failed to convert check: %w", err)
}
v.checks[i] = *check
}
v.policies = make([]Policy, len(pbPolicies.Policies))
for i, pbPolicy := range pbPolicies.Policies {
policy := Policy{}
switch *pbPolicy.Kind {
case pb.Policy_Allow:
policy.Kind = PolicyKindAllow
case pb.Policy_Deny:
policy.Kind = PolicyKindDeny
default:
return fmt.Errorf("verifier: load policies v1: unsupported proto policy kind %v", pbPolicy.Kind)
}
policy.Queries = make([]Rule, len(pbPolicy.Queries))
for j, pbRule := range pbPolicy.Queries {
dlRule, err := protoRuleToTokenRuleV2(pbRule)
if err != nil {
return fmt.Errorf("verifier: load policies v1: failed to convert datalog policy rule: %w", err)
}
rule, err := fromDatalogRule(v.symbols, *dlRule)
if err != nil {
return fmt.Errorf("verifier: load policies v1: failed to convert policy rule: %w", err)
}
policy.Queries[j] = *rule
}
v.policies[i] = policy
}
return nil
}
func (v *authorizer) SerializePolicies() ([]byte, error) {
if v.dirty {
return nil, errors.New("verifier: can't serialize after world has been run")
}
protoFacts := make([]*pb.FactV2, len(*v.world.Facts()))
for i, fact := range *v.world.Facts() {
protoFact, err := tokenFactToProtoFactV2(fact)
if err != nil {
return nil, fmt.Errorf("verifier: failed to convert fact: %w", err)
}
protoFacts[i] = protoFact
}
protoRules := make([]*pb.RuleV2, len(v.world.Rules()))
for i, rule := range v.world.Rules() {
protoRule, err := tokenRuleToProtoRuleV2(rule)
if err != nil {
return nil, fmt.Errorf("verifier: failed to convert rule: %w", err)
}
protoRules[i] = protoRule
}
protoChecks := make([]*pb.CheckV2, len(v.checks))
for i, check := range v.checks {
protoCheck, err := tokenCheckToProtoCheckV2(check.convert(v.symbols))
if err != nil {
return nil, fmt.Errorf("verifier: failed to convert check: %w", err)
}
protoChecks[i] = protoCheck
}
protoPolicies := make([]*pb.Policy, len(v.policies))
for i, policy := range v.policies {
protoPolicy := &pb.Policy{}
switch policy.Kind {
case PolicyKindAllow:
kind := pb.Policy_Allow
protoPolicy.Kind = &kind
case PolicyKindDeny:
kind := pb.Policy_Deny
protoPolicy.Kind = &kind
default:
return nil, fmt.Errorf("verifier: unsupported policy kind %v", policy.Kind)
}
protoPolicy.Queries = make([]*pb.RuleV2, len(policy.Queries))
for j, rule := range policy.Queries {
protoRule, err := tokenRuleToProtoRuleV2(rule.convert(v.symbols))
if err != nil {
return nil, fmt.Errorf("verifier: failed to convert policy rule: %w", err)
}
protoPolicy.Queries[j] = protoRule
}
protoPolicies[i] = protoPolicy
}
version := MaxSchemaVersion
return proto.Marshal(&pb.AuthorizerPolicies{
Symbols: *v.symbols.Clone(),
Version: proto.Uint32(version),
Facts: protoFacts,
Rules: protoRules,
Checks: protoChecks,
Policies: protoPolicies,
})
}