-
Notifications
You must be signed in to change notification settings - Fork 22
/
verifier.go
158 lines (130 loc) · 3.05 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
package biscuit
import (
"errors"
"fmt"
"strings"
"github.com/biscuit-auth/biscuit-go/datalog"
)
var (
ErrMissingSymbols = errors.New("biscuit: missing symbols")
)
type Verifier interface {
AddFact(fact Fact)
AddRule(rule Rule)
AddCheck(check Check)
Verify() error
Query(rule Rule) (FactSet, error)
Biscuit() *Biscuit
Reset()
PrintWorld() string
}
type verifier struct {
biscuit *Biscuit
baseWorld *datalog.World
baseSymbols *datalog.SymbolTable
world *datalog.World
symbols *datalog.SymbolTable
checks []Check
}
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) 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
}
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, ", "))
}
return nil
}
func (v *verifier) Query(rule Rule) (FactSet, error) {
if err := v.world.Run(); err != nil {
return nil, err
}
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.checks = []Check{}
v.world = v.baseWorld.Clone()
v.symbols = v.baseSymbols.Clone()
}