-
Notifications
You must be signed in to change notification settings - Fork 22
/
parser.go
239 lines (193 loc) · 5.42 KB
/
parser.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
package parser
import (
"errors"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer/stateful"
"github.com/biscuit-auth/biscuit-go"
)
var (
ErrVariableInFact = errors.New("parser: a fact cannot contain any variables")
ErrVariableInSet = errors.New("parser: a set cannot contain any variables")
)
var BiscuitLexerRules = []stateful.Rule{
{Name: "Keyword", Pattern: `check if|allow if|deny if`, Action: nil},
{Name: "Function", Pattern: `prefix|suffix|matches|length|contains`, Action: nil},
{Name: "Hex", Pattern: `hex:`, Action: nil},
{Name: "Dot", Pattern: `\.`, Action: nil},
{Name: "Arrow", Pattern: `<-`, Action: nil},
{Name: "Or", Pattern: `\|\|`, Action: nil},
{Name: "Operator", Pattern: `==|>=|<=|>|<|not|in`, Action: nil},
{Name: "Comment", Pattern: `//[^\n]*`, Action: nil},
{Name: "String", Pattern: `\"[^\"]*\"`, Action: nil},
{Name: "Variable", Pattern: `\$[a-zA-Z0-9_]+`, Action: nil},
{Name: "DateTime", Pattern: `\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|([-+]\d\d:\d\d))?`},
{Name: "Int", Pattern: `[0-9]+`, Action: nil},
{Name: "Bool", Pattern: `true|false`, Action: nil},
{Name: "Ident", Pattern: `[a-zA-Z0-9_]+`, Action: nil},
{Name: "Whitespace", Pattern: `[ \t]+`, Action: nil},
{Name: "EOL", Pattern: `[\n\r]+`, Action: nil},
{Name: "Punct", Pattern: `[-[!@%^&#$*()+_={}\|:;"'<,>.?/]|]`, Action: nil},
}
var DefaultParserOptions = []participle.Option{
participle.Lexer(stateful.MustSimple(BiscuitLexerRules)),
participle.UseLookahead(1),
participle.Elide("Whitespace", "EOL"),
participle.Unquote("String"),
}
type Parser interface {
Fact(fact string) (biscuit.Fact, error)
Rule(rule string) (biscuit.Rule, error)
Check(check string) (biscuit.Check, error)
Policy(policy string) (biscuit.Policy, error)
Must() MustParser
}
type MustParser interface {
Fact(fact string) biscuit.Fact
Rule(rule string) biscuit.Rule
Check(check string) biscuit.Check
Policy(policy string) biscuit.Policy
}
type parser struct {
factParser *participle.Parser
ruleParser *participle.Parser
checkParser *participle.Parser
policyParser *participle.Parser
}
var _ Parser = (*parser)(nil)
type mustParser struct {
parser Parser
}
var _ MustParser = (*mustParser)(nil)
func New() Parser {
return &parser{
factParser: participle.MustBuild(&Predicate{}, DefaultParserOptions...),
ruleParser: participle.MustBuild(&Rule{}, DefaultParserOptions...),
checkParser: participle.MustBuild(&Check{}, DefaultParserOptions...),
policyParser: participle.MustBuild(&Policy{}, DefaultParserOptions...),
}
}
func (p *parser) Fact(fact string) (biscuit.Fact, error) {
parsed := &Predicate{}
if err := p.factParser.ParseString("fact", fact, parsed); err != nil {
return biscuit.Fact{}, err
}
pred, err := parsed.ToBiscuit()
if err != nil {
return biscuit.Fact{}, err
}
for _, a := range pred.IDs {
if a.Type() == biscuit.TermTypeVariable {
return biscuit.Fact{}, ErrVariableInFact
}
}
return biscuit.Fact{Predicate: *pred}, nil
}
func (p *parser) Rule(rule string) (biscuit.Rule, error) {
parsed := &Rule{}
if err := p.ruleParser.ParseString("rule", rule, parsed); err != nil {
return biscuit.Rule{}, err
}
r, err := parsed.ToBiscuit()
if err != nil {
return biscuit.Rule{}, err
}
return *r, nil
}
func (p *parser) Check(check string) (biscuit.Check, error) {
parsed := &Check{}
if err := p.checkParser.ParseString("check", check, parsed); err != nil {
return biscuit.Check{}, err
}
queries := make([]biscuit.Rule, len(parsed.Queries))
for i, q := range parsed.Queries {
query, err := q.ToBiscuit()
if err != nil {
return biscuit.Check{}, err
}
queries[i] = *query
}
return biscuit.Check{
Queries: queries,
}, nil
}
func (p *parser) Policy(policy string) (biscuit.Policy, error) {
parsed := &Policy{}
if err := p.policyParser.ParseString("policy", policy, parsed); err != nil {
return biscuit.Policy{}, err
}
var parsedQueries []*CheckQuery
var kind biscuit.PolicyKind
switch {
case parsed.Allow != nil:
{
parsedQueries = parsed.Allow.Queries
kind = biscuit.PolicyKindAllow
break
}
case parsed.Deny != nil:
{
parsedQueries = parsed.Allow.Queries
kind = biscuit.PolicyKindDeny
break
}
}
queries := make([]biscuit.Rule, len(parsedQueries))
for i, q := range parsedQueries {
query, err := q.ToBiscuit()
if err != nil {
return biscuit.Policy{}, err
}
queries[i] = *query
}
return biscuit.Policy{
Kind: kind,
Queries: queries,
}, nil
}
func (p *parser) Must() MustParser {
return &mustParser{parser: p}
}
func (m *mustParser) Fact(fact string) biscuit.Fact {
f, err := m.parser.Fact(fact)
if err != nil {
panic(err)
}
return f
}
func (m *mustParser) Rule(rule string) biscuit.Rule {
r, err := m.parser.Rule(rule)
if err != nil {
panic(err)
}
return r
}
func (m *mustParser) Check(check string) biscuit.Check {
c, err := m.parser.Check(check)
if err != nil {
panic(err)
}
return c
}
func (m *mustParser) Policy(policy string) biscuit.Policy {
c, err := m.parser.Policy(policy)
if err != nil {
panic(err)
}
return c
}
func FromStringFact(input string) (biscuit.Fact, error) {
p := New()
return p.Fact(input)
}
func FromStringRule(input string) (biscuit.Rule, error) {
p := New()
return p.Rule(input)
}
func FromStringCheck(input string) (biscuit.Check, error) {
p := New()
return p.Check(input)
}
func FromStringPolicy(input string) (biscuit.Policy, error) {
p := New()
return p.Policy(input)
}