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
use std::collections::HashSet;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
pub type SymbolIndex = u64;
use crate::token::default_symbol_table;
use crate::{error, token::public_keys::PublicKeys};
use super::{Check, Fact, Predicate, Rule, Term, World};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SymbolTable {
symbols: Vec<String>,
pub(crate) public_keys: PublicKeys,
}
const DEFAULT_SYMBOLS: [&str; 28] = [
"read",
"write",
"resource",
"operation",
"right",
"time",
"role",
"owner",
"tenant",
"namespace",
"user",
"team",
"service",
"admin",
"email",
"group",
"member",
"ip_address",
"client",
"client_ip",
"domain",
"path",
"version",
"cluster",
"node",
"hostname",
"nonce",
"query",
];
const OFFSET: usize = 1024;
impl SymbolTable {
pub fn new() -> Self {
SymbolTable {
symbols: vec![],
public_keys: PublicKeys::new(),
}
}
pub fn from(symbols: Vec<String>) -> Result<Self, error::Format> {
let h1 = DEFAULT_SYMBOLS.iter().copied().collect::<HashSet<_>>();
let h2 = symbols.iter().map(|s| s.as_str()).collect::<HashSet<_>>();
if !h1.is_disjoint(&h2) {
return Err(error::Format::SymbolTableOverlap);
}
Ok(SymbolTable {
symbols,
public_keys: PublicKeys::new(),
})
}
pub fn extend(&mut self, other: &SymbolTable) -> Result<(), error::Format> {
if !self.is_disjoint(other) {
return Err(error::Format::SymbolTableOverlap);
}
self.symbols.extend(other.symbols.iter().cloned());
self.public_keys.extend(&other.public_keys)?;
Ok(())
}
pub fn insert(&mut self, s: &str) -> SymbolIndex {
if let Some(index) = DEFAULT_SYMBOLS.iter().position(|sym| *sym == s) {
return index as u64;
}
match self.symbols.iter().position(|sym| sym.as_str() == s) {
Some(index) => (OFFSET + index) as u64,
None => {
self.symbols.push(s.to_string());
(OFFSET + (self.symbols.len() - 1)) as u64
}
}
}
pub fn add(&mut self, s: &str) -> Term {
let term = self.insert(s);
Term::Str(term)
}
pub fn get(&self, s: &str) -> Option<SymbolIndex> {
if let Some(index) = DEFAULT_SYMBOLS.iter().position(|sym| *sym == s) {
return Some(index as u64);
}
self.symbols
.iter()
.position(|sym| sym.as_str() == s)
.map(|i| (OFFSET + i) as SymbolIndex)
}
pub fn strings(&self) -> Vec<String> {
self.symbols.clone()
}
pub fn current_offset(&self) -> usize {
self.symbols.len()
}
pub fn split_at(&mut self, offset: usize) -> SymbolTable {
let mut table = SymbolTable::new();
table.symbols = self.symbols.split_off(offset);
table
}
pub fn is_disjoint(&self, other: &SymbolTable) -> bool {
let h1 = self.symbols.iter().collect::<HashSet<_>>();
let h2 = other.symbols.iter().collect::<HashSet<_>>();
h1.is_disjoint(&h2)
}
pub fn get_symbol(&self, i: SymbolIndex) -> Option<&str> {
if i >= OFFSET as u64 {
self.symbols
.get((i - OFFSET as u64) as usize)
.map(|s| s.as_str())
} else {
DEFAULT_SYMBOLS.get(i as usize).copied()
}
}
pub fn print_symbol(&self, i: SymbolIndex) -> Result<String, error::Format> {
self.get_symbol(i)
.map(|s| s.to_string())
.ok_or(error::Format::UnknownSymbol(i))
}
pub fn print_symbol_default(&self, i: SymbolIndex) -> String {
self.get_symbol(i)
.map(|s| s.to_string())
.unwrap_or_else(|| format!("<{}?>", i))
}
pub fn print_world(&self, w: &World) -> String {
let facts = w
.facts
.inner
.iter()
.flat_map(|facts| facts.1.iter())
.map(|f| self.print_fact(f))
.collect::<Vec<_>>();
let rules = w
.rules
.inner
.iter()
.flat_map(|rules| rules.1.iter())
.map(|(_, r)| self.print_rule(r))
.collect::<Vec<_>>();
format!("World {{\n facts: {:#?}\n rules: {:#?}\n}}", facts, rules)
}
pub fn print_term(&self, term: &Term) -> String {
match term {
Term::Variable(i) => format!("${}", self.print_symbol_default(*i as u64)),
Term::Integer(i) => i.to_string(),
Term::Str(index) => format!("\"{}\"", self.print_symbol_default(*index as u64)),
Term::Date(d) => OffsetDateTime::from_unix_timestamp(*d as i64)
.ok()
.and_then(|t| t.format(&Rfc3339).ok())
.unwrap_or_else(|| "<invalid date>".to_string()),
Term::Bytes(s) => format!("hex:{}", hex::encode(s)),
Term::Bool(b) => {
if *b {
"true".to_string()
} else {
"false".to_string()
}
}
Term::Set(s) => {
let terms = s
.iter()
.map(|term| self.print_term(term))
.collect::<Vec<_>>();
format!("[{}]", terms.join(", "))
}
}
}
pub fn print_fact(&self, f: &Fact) -> String {
self.print_predicate(&f.predicate)
}
pub fn print_predicate(&self, p: &Predicate) -> String {
let strings = p
.terms
.iter()
.map(|term| self.print_term(term))
.collect::<Vec<_>>();
format!(
"{}({})",
self.get_symbol(p.name).unwrap_or("<?>"),
strings.join(", ")
)
}
pub fn print_expression(&self, e: &super::expression::Expression) -> String {
e.print(self)
.unwrap_or_else(|| format!("<invalid expression: {:?}>", e.ops))
}
pub fn print_rule_body(&self, r: &Rule) -> String {
let preds: Vec<_> = r.body.iter().map(|p| self.print_predicate(p)).collect();
let expressions: Vec<_> = r
.expressions
.iter()
.map(|c| self.print_expression(c))
.collect();
let e = if expressions.is_empty() {
String::new()
} else if preds.is_empty() {
expressions.join(", ")
} else {
format!(", {}", expressions.join(", "))
};
let scopes = if r.scopes.is_empty() {
String::new()
} else {
let s: Vec<_> = r
.scopes
.iter()
.map(|scope| match scope {
crate::token::Scope::Authority => "authority".to_string(),
crate::token::Scope::Previous => "previous".to_string(),
crate::token::Scope::PublicKey(key_id) => {
match self.public_keys.get_key(*key_id) {
Some(key) => format!("ed25519/{}", hex::encode(key.to_bytes())),
None => "<unknown public key id>".to_string(),
}
}
})
.collect();
format!(" trusting {}", s.join(", "))
};
format!("{}{}{}", preds.join(", "), e, scopes)
}
pub fn print_rule(&self, r: &Rule) -> String {
let res = self.print_predicate(&r.head);
format!("{} <- {}", res, self.print_rule_body(r))
}
pub fn print_check(&self, c: &Check) -> String {
let queries = c
.queries
.iter()
.map(|r| self.print_rule_body(r))
.collect::<Vec<_>>();
format!(
"check {} {}",
match c.kind {
crate::builder::CheckKind::One => "if",
crate::builder::CheckKind::All => "all",
},
queries.join(" or ")
)
}
}
impl Default for SymbolTable {
fn default() -> Self {
default_symbol_table()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TemporarySymbolTable<'a> {
base: &'a SymbolTable,
offset: usize,
symbols: Vec<String>,
}
impl<'a> TemporarySymbolTable<'a> {
pub fn new(base: &'a SymbolTable) -> Self {
let offset = OFFSET + base.current_offset();
TemporarySymbolTable {
base,
offset,
symbols: vec![],
}
}
pub fn get_symbol(&self, i: SymbolIndex) -> Option<&str> {
if i as usize >= self.offset {
self.symbols
.get(i as usize - self.offset)
.map(|s| s.as_str())
} else {
self.base.get_symbol(i)
}
}
pub fn insert(&mut self, s: &str) -> SymbolIndex {
if let Some(index) = self.base.get(s) {
return index as u64;
}
match self.symbols.iter().position(|sym| sym.as_str() == s) {
Some(index) => (self.offset + index) as u64,
None => {
self.symbols.push(s.to_string());
(self.offset + (self.symbols.len() - 1)) as u64
}
}
}
}