-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathverifier.go
336 lines (286 loc) · 8.09 KB
/
verifier.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
package biscuit
import (
"errors"
"fmt"
"strings"
"github.com/biscuit-auth/biscuit-go/datalog"
"github.com/biscuit-auth/biscuit-go/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 Verifier interface {
AddFact(fact Fact)
AddRule(rule Rule)
AddCheck(check Check)
AddPolicy(policy Policy)
Verify() error
Query(rule Rule) (FactSet, error)
Biscuit() *Biscuit
Reset()
PrintWorld() string
LoadPolicies([]byte) error
SerializePolicies() ([]byte, error)
}
type verifier struct {
biscuit *Biscuit
baseWorld *datalog.World
baseSymbols *datalog.SymbolTable
world *datalog.World
symbols *datalog.SymbolTable
checks []Check
policies []Policy
dirty bool
}
var _ Verifier = (*verifier)(nil)
func NewVerifier(b *Biscuit) (Verifier, error) {
baseWorld, err := b.generateWorld(b.symbols)
if err != nil {
return nil, err
}
return &verifier{
biscuit: b,
baseWorld: baseWorld,
baseSymbols: b.symbols.Clone(),
world: baseWorld.Clone(),
symbols: b.symbols.Clone(),
checks: []Check{},
}, nil
}
func (v *verifier) AddFact(fact Fact) {
v.world.AddFact(fact.convert(v.symbols))
}
func (v *verifier) AddRule(rule Rule) {
v.world.AddRule(rule.convert(v.symbols))
}
func (v *verifier) AddCheck(check Check) {
v.checks = append(v.checks, check)
}
func (v *verifier) AddPolicy(policy Policy) {
v.policies = append(v.policies, policy)
}
func (v *verifier) Verify() error {
debug := datalog.SymbolDebugger{
SymbolTable: v.symbols,
}
if v.symbols.Sym("authority") == nil || v.symbols.Sym("ambient") == nil {
return ErrMissingSymbols
}
if err := v.world.Run(); 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)
if len(*res) != 0 {
successful = true
break
}
}
if !successful {
errs = append(errs, fmt.Errorf("failed to verify check #%d: %s", i, debug.Check(c)))
}
}
for bi, blockChecks := range v.biscuit.Checks() {
for ci, check := range blockChecks {
successful := false
for _, query := range check.Queries {
res := v.world.QueryRule(query)
if len(*res) != 0 {
successful = true
break
}
}
if !successful {
errs = append(errs, fmt.Errorf("failed to verify block #%d check #%d: %s", bi, ci, debug.Check(check)))
}
}
}
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, ", "))
}
for _, policy := range v.policies {
for _, query := range policy.Queries {
res := v.world.QueryRule(query.convert(v.symbols))
if len(*res) != 0 {
switch policy.Kind {
case PolicyKindAllow:
return nil
case PolicyKindDeny:
return ErrPolicyDenied
}
}
}
}
return ErrNoMatchingPolicy
}
func (v *verifier) Query(rule Rule) (FactSet, error) {
if err := v.world.Run(); err != nil {
return nil, err
}
v.dirty = true
facts := v.world.QueryRule(rule.convert(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 *verifier) Biscuit() *Biscuit {
return v.biscuit
}
func (v *verifier) PrintWorld() string {
debug := datalog.SymbolDebugger{
SymbolTable: v.symbols,
}
return debug.World(v.world)
}
func (v *verifier) Reset() {
v.world = v.baseWorld.Clone()
v.symbols = v.baseSymbols.Clone()
v.checks = []Check{}
v.policies = []Policy{}
v.dirty = false
}
func (v *verifier) LoadPolicies(verifierPolicies []byte) error {
pbPolicies := &pb.VerifierPolicies{}
if err := proto.Unmarshal(verifierPolicies, pbPolicies); err != nil {
return fmt.Errorf("verifier: failed to load policies: %w", err)
}
switch pbPolicies.Version {
case 1:
return v.loadPoliciesV1(pbPolicies)
default:
return fmt.Errorf("verifier: unsupported policies version %d", pbPolicies.Version)
}
}
func (v *verifier) loadPoliciesV1(pbPolicies *pb.VerifierPolicies) error {
policySymbolTable := datalog.SymbolTable(pbPolicies.Symbols)
v.symbols = v.baseSymbols.Clone()
v.symbols.Extend(&policySymbolTable)
for _, pbFact := range pbPolicies.Facts {
fact, err := protoFactToTokenFactV1(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 := protoRuleToTokenRuleV1(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 := protoCheckToTokenCheckV1(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 := protoRuleToTokenRuleV1(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 *verifier) SerializePolicies() ([]byte, error) {
if v.dirty {
return nil, errors.New("verifier: can't serialize after world has been run")
}
protoFacts := make([]*pb.FactV1, len(*v.world.Facts()))
for i, fact := range *v.world.Facts() {
protoFact, err := tokenFactToProtoFactV1(fact)
if err != nil {
return nil, fmt.Errorf("verifier: failed to convert fact: %w", err)
}
protoFacts[i] = protoFact
}
protoRules := make([]*pb.RuleV1, len(v.world.Rules()))
for i, rule := range v.world.Rules() {
protoRule, err := tokenRuleToProtoRuleV1(rule)
if err != nil {
return nil, fmt.Errorf("verifier: failed to convert rule: %w", err)
}
protoRules[i] = protoRule
}
protoChecks := make([]*pb.CheckV1, len(v.checks))
for i, check := range v.checks {
protoCheck, err := tokenCheckToProtoCheckV1(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:
protoPolicy.Kind = pb.Policy_Allow
case PolicyKindDeny:
protoPolicy.Kind = pb.Policy_Deny
default:
return nil, fmt.Errorf("verifier: unsupported policy kind %v", policy.Kind)
}
protoPolicy.Queries = make([]*pb.RuleV1, len(policy.Queries))
for j, rule := range policy.Queries {
protoRule, err := tokenRuleToProtoRuleV1(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
}
return proto.Marshal(&pb.VerifierPolicies{
Symbols: *v.symbols.Clone(),
Version: MaxSchemaVersion,
Facts: protoFacts,
Rules: protoRules,
Checks: protoChecks,
Policies: protoPolicies,
})
}