authly_common/policy/
engine.rs

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
//! Policy evaluation engine that implements a Policy Decision Point (PDP).

use std::collections::BTreeSet;

use fnv::{FnvHashMap, FnvHashSet};
use tracing::error;

use crate::id::{AttrId, Eid, Id128, PolicyId, PropId};

use super::code::{Bytecode, PolicyValue};

/// Evaluation error.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum EvalError {
    /// Error in the program encoding
    Program,

    /// Type error
    Type,
}

/// The parameters to an policy-based access control evaluation.
///
/// The access control paramaters generall consists of attributes related to a `subject` and a `resource`.
///
/// The `subject` represents the entity or entities requesting access.
/// The `resource` is a representation of the abstract object being requested.
#[derive(Default, Debug)]
pub struct AccessControlParams {
    /// Entity IDs related to the `subject`.
    pub subject_eids: FnvHashMap<PropId, Eid>,

    /// Attributes related to the `subject`.
    pub subject_attrs: FnvHashSet<AttrId>,

    /// Entity IDs related to the `resource`.
    pub resource_eids: FnvHashMap<PropId, Eid>,

    /// Attributes related to the `resource`.
    pub resource_attrs: FnvHashSet<AttrId>,
}

/// The state of the policy engine.
///
/// Contains compiled policies and their triggers.
#[derive(Default, Debug)]
pub struct PolicyEngine {
    policies: FnvHashMap<PolicyId, Policy>,

    /// The triggers in this map are keyed by the one of the
    /// attributes that has to match the trigger.
    trigger_groups: FnvHashMap<AttrId, Vec<PolicyTrigger>>,
}

/// The policy trigger maps a set of attributes to a set of policies.
#[derive(Debug)]
struct PolicyTrigger {
    /// The set of attributes that has to match for this policy to trigger
    pub attr_matcher: BTreeSet<AttrId>,

    /// The policy which gets triggered by this attribute matcher
    pub policy_ids: BTreeSet<PolicyId>,
}

/// A tracer used to collect debugging information from the policy engine
#[allow(unused)]
pub trait PolicyTracer {
    /// Reports applicable policies of a specific class
    fn report_applicable(&mut self, class: PolicyValue, policies: impl Iterator<Item = PolicyId>) {}

    /// Report start of a policy evaluation
    fn report_policy_eval_start(&mut self, policy_id: PolicyId) {}

    /// Reports the value of policy after it has been evaluated
    fn report_policy_eval_end(&mut self, value: bool) {}
}

/// A [PolicyTracer] that does nothing.
pub struct NoOpPolicyTracer;

impl PolicyTracer for NoOpPolicyTracer {}

#[derive(Debug)]
struct Policy {
    class: PolicyValue,
    bytecode: Vec<u8>,
}

#[derive(PartialEq, Eq, Debug)]
enum StackItem<'a> {
    Uint(u64),
    AttrIdSet(&'a FnvHashSet<AttrId>),
    EntityId(Eid),
    AttrId(AttrId),
}

#[derive(Debug)]
struct EvalCtx<'e> {
    applicable_allow: FnvHashMap<PolicyId, &'e Policy>,
    applicable_deny: FnvHashMap<PolicyId, &'e Policy>,
}

impl PolicyEngine {
    /// Adds a new policy to the engine.
    pub fn add_policy(&mut self, id: PolicyId, class: PolicyValue, bytecode: Vec<u8>) {
        self.policies.insert(id, Policy { class, bytecode });
    }

    /// Adds a new policy trigger to the engine.
    pub fn add_trigger(
        &mut self,
        attr_matcher: impl Into<BTreeSet<AttrId>>,
        policy_ids: impl Into<BTreeSet<PolicyId>>,
    ) {
        let attr_matcher = attr_matcher.into();
        let policy_ids = policy_ids.into();

        if let Some(first_attr) = attr_matcher.iter().next() {
            self.trigger_groups
                .entry(*first_attr)
                .or_default()
                .push(PolicyTrigger {
                    attr_matcher,
                    policy_ids,
                });
        }
    }

    /// Get the number of policies currently in the engine.
    pub fn get_policy_count(&self) -> usize {
        self.policies.len()
    }

    /// Get the number of policy triggers currently in the engine.
    pub fn get_trigger_count(&self) -> usize {
        self.trigger_groups.values().map(Vec::len).sum()
    }

    /// Perform an access control evalution of the given parameters within this engine.
    pub fn eval(
        &self,
        params: &AccessControlParams,
        tracer: &mut impl PolicyTracer,
    ) -> Result<PolicyValue, EvalError> {
        let mut eval_ctx = EvalCtx {
            applicable_allow: Default::default(),
            applicable_deny: Default::default(),
        };

        for attr in &params.subject_attrs {
            self.collect_applicable(*attr, params, &mut eval_ctx)?;
        }

        for attr in &params.resource_attrs {
            self.collect_applicable(*attr, params, &mut eval_ctx)?;
        }

        {
            tracer.report_applicable(PolicyValue::Deny, eval_ctx.applicable_deny.keys().copied());
            tracer.report_applicable(
                PolicyValue::Allow,
                eval_ctx.applicable_allow.keys().copied(),
            );
        }

        let has_allow = !eval_ctx.applicable_allow.is_empty();
        let has_deny = !eval_ctx.applicable_deny.is_empty();

        match (has_allow, has_deny) {
            (false, false) => {
                // idea: Fallback mode, no policies matched
                for subj_attr in &params.subject_attrs {
                    if params.resource_attrs.contains(subj_attr) {
                        return Ok(PolicyValue::Allow);
                    }
                }

                Ok(PolicyValue::Deny)
            }
            (true, false) => {
                // starts in Deny state, try to prove Allow
                let is_allow =
                    eval_policies_disjunctive(eval_ctx.applicable_allow, params, tracer)?;
                Ok(PolicyValue::from(is_allow))
            }
            (false, true) => {
                // starts in Allow state, try to prove Deny
                let is_deny = eval_policies_disjunctive(eval_ctx.applicable_deny, params, tracer)?;
                Ok(PolicyValue::from(!is_deny))
            }
            (true, true) => {
                // starts in Deny state, try to prove Allow
                let is_allow =
                    eval_policies_disjunctive(eval_ctx.applicable_allow, params, tracer)?;
                if !is_allow {
                    return Ok(PolicyValue::Deny);
                }

                // moved into in Allow state, try to prove Deny
                let is_deny = eval_policies_disjunctive(eval_ctx.applicable_deny, params, tracer)?;
                Ok(PolicyValue::from(!is_deny))
            }
        }
    }

    fn collect_applicable<'e>(
        &'e self,
        attr: AttrId,
        params: &AccessControlParams,
        eval_ctx: &mut EvalCtx<'e>,
    ) -> Result<(), EvalError> {
        // Find all potential triggers to investigate for this attribute
        let Some(policy_triggers) = self.trigger_groups.get(&attr) else {
            return Ok(());
        };

        for policy_trigger in policy_triggers {
            if policy_trigger.attr_matcher.len() > 1 {
                // a multi-attribute trigger: needs some post-processing
                // to figure out if it applies
                let mut matches: BTreeSet<AttrId> = Default::default();

                for attrs in [&params.subject_attrs, &params.resource_attrs] {
                    for attr in attrs {
                        if policy_trigger.attr_matcher.contains(attr) {
                            matches.insert(*attr);
                        }
                    }
                }

                if matches != policy_trigger.attr_matcher {
                    // not applicable
                    continue;
                }
            }

            // The trigger applies; register all its policies as applicable
            for policy_id in policy_trigger.policy_ids.iter().copied() {
                let Some(policy) = self.policies.get(&policy_id) else {
                    error!(?policy_id, "policy is missing");

                    // internal error, which is not exposed
                    continue;
                };

                match policy.class {
                    PolicyValue::Deny => {
                        eval_ctx.applicable_deny.insert(policy_id, policy);
                    }
                    PolicyValue::Allow => {
                        eval_ctx.applicable_allow.insert(policy_id, policy);
                    }
                }
            }
        }

        Ok(())
    }
}

/// Evaluate set of policies, map their outputs to a boolean value and return the OR function applied to those values.
fn eval_policies_disjunctive(
    map: FnvHashMap<PolicyId, &Policy>,
    params: &AccessControlParams,
    tracer: &mut impl PolicyTracer,
) -> Result<bool, EvalError> {
    for (policy_id, policy) in &map {
        tracer.report_policy_eval_start(*policy_id);

        let value = eval_policy(&policy.bytecode, params)?;

        tracer.report_policy_eval_end(value);

        if value {
            return Ok(true);
        }
    }

    Ok(false)
}

/// Evaluate one standalone policy on the given access control parameters
fn eval_policy(mut pc: &[u8], params: &AccessControlParams) -> Result<bool, EvalError> {
    let mut stack: Vec<StackItem> = Vec::with_capacity(16);

    while let Some(code) = pc.first() {
        pc = &pc[1..];

        let Ok(code) = Bytecode::try_from(*code) else {
            return Err(EvalError::Program);
        };

        match code {
            Bytecode::LoadSubjectId => {
                let (key, next) = decode_id(pc)?;
                let Some(id) = params.subject_eids.get(&key) else {
                    return Err(EvalError::Type);
                };
                stack.push(StackItem::EntityId(*id));
                pc = next;
            }
            Bytecode::LoadSubjectAttrs => {
                stack.push(StackItem::AttrIdSet(&params.subject_attrs));
            }
            Bytecode::LoadResourceId => {
                let (key, next) = decode_id(pc)?;
                let Some(id) = params.resource_eids.get(&key) else {
                    return Err(EvalError::Type);
                };
                stack.push(StackItem::EntityId(*id));
                pc = next;
            }
            Bytecode::LoadResourceAttrs => {
                stack.push(StackItem::AttrIdSet(&params.resource_attrs));
            }
            Bytecode::LoadConstEntityId => {
                let (id, next) = decode_id(pc)?;
                stack.push(StackItem::EntityId(id));
                pc = next;
            }
            Bytecode::LoadConstAttrId => {
                let (id, next) = decode_id(pc)?;
                stack.push(StackItem::AttrId(id));
                pc = next;
            }
            Bytecode::IsEq => {
                let Some(a) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                let Some(b) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                let is_eq = match (a, b) {
                    (StackItem::AttrId(a), StackItem::AttrId(b)) => a == b,
                    (StackItem::EntityId(a), StackItem::EntityId(b)) => a == b,
                    (StackItem::AttrIdSet(set), StackItem::AttrId(id)) => set.contains(&id),
                    (StackItem::AttrId(id), StackItem::AttrIdSet(set)) => set.contains(&id),
                    _ => false,
                };
                stack.push(StackItem::Uint(if is_eq { 1 } else { 0 }));
            }
            Bytecode::SupersetOf => {
                let Some(StackItem::AttrIdSet(a)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                let Some(StackItem::AttrIdSet(b)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                stack.push(StackItem::Uint(if a.is_superset(b) { 1 } else { 0 }));
            }
            Bytecode::IdSetContains => {
                let Some(a) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                let Some(b) = stack.pop() else {
                    return Err(EvalError::Type);
                };

                match (a, b) {
                    (StackItem::AttrIdSet(a), StackItem::AttrId(b)) => {
                        // BUG: Does not support u128?
                        stack.push(StackItem::Uint(if a.contains(&b) { 1 } else { 0 }));
                    }
                    _ => {
                        return Err(EvalError::Type);
                    }
                }
            }
            Bytecode::And => {
                let Some(StackItem::Uint(rhs)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                let Some(StackItem::Uint(lhs)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                stack.push(StackItem::Uint(if rhs > 0 && lhs > 0 { 1 } else { 0 }));
            }
            Bytecode::Or => {
                let Some(StackItem::Uint(rhs)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                let Some(StackItem::Uint(lhs)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                stack.push(StackItem::Uint(if rhs > 0 || lhs > 0 { 1 } else { 0 }));
            }
            Bytecode::Not => {
                let Some(StackItem::Uint(val)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                stack.push(StackItem::Uint(if val > 0 { 0 } else { 1 }));
            }
            Bytecode::Return => {
                let Some(StackItem::Uint(u)) = stack.pop() else {
                    return Err(EvalError::Type);
                };
                return Ok(u > 0);
            }
        }
    }

    Err(EvalError::Program)
}

#[inline]
fn decode_id<K>(buf: &[u8]) -> Result<(Id128<K>, &[u8]), EvalError> {
    let (uint, next) = unsigned_varint::decode::u128(buf).map_err(|_| EvalError::Program)?;

    Ok((Id128::from_uint(uint), next))
}