strig 0.6.0

Extensible authorization engine: permission catalog, RBAC, ABAC and ReBAC in one decision, with governance and auditing of changes.
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::net::IpAddr;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::attr::{Attributes, Value};
use crate::resource::Resource;
use crate::subject::Subject;

/// One side of a comparison: an attribute resolved at decision time or a literal.
///
/// Attributes use the path `<root>.<name>`, with root `subject`, `resource` or `env`.
/// `subject.id`, `subject.kind`, `resource.id` and `resource.kind` come from the identity; the
/// rest comes from the attribute map.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Operand {
    Attr { attr: String },
    Lit(Value),
}

impl Operand {
    pub fn attr(path: impl Into<String>) -> Self {
        Self::Attr { attr: path.into() }
    }

    pub fn lit(value: impl Into<Value>) -> Self {
        Self::Lit(value.into())
    }
}

/// Serializable ABAC condition, stored together with the grant.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Condition {
    Eq {
        left: Operand,
        right: Operand,
    },
    Ne {
        left: Operand,
        right: Operand,
    },
    Lt {
        left: Operand,
        right: Operand,
    },
    Le {
        left: Operand,
        right: Operand,
    },
    Gt {
        left: Operand,
        right: Operand,
    },
    Ge {
        left: Operand,
        right: Operand,
    },
    /// `item` belongs to the list `set`.
    In {
        item: Operand,
        set: Operand,
    },
    /// Text `left` starts with text `right`.
    StartsWith {
        left: Operand,
        right: Operand,
    },
    /// Text `left` ends with text `right`.
    EndsWith {
        left: Operand,
        right: Operand,
    },
    /// Text `left` contains text `right` as a substring, or list `left` has `right` as an element.
    Contains {
        left: Operand,
        right: Operand,
    },
    /// The list `left` has at least one element that belongs to the list `right`.
    AnyIn {
        left: Operand,
        right: Operand,
    },
    /// Every element of the list `left` belongs to the list `right`; an empty `left` holds.
    AllIn {
        left: Operand,
        right: Operand,
    },
    /// The IPv4 or IPv6 address `item` lies inside the CIDR `set`, or inside one of the CIDRs of
    /// the list `set`. An address without a prefix length stands for a single host.
    IpIn {
        item: Operand,
        set: Operand,
    },
    /// The UTC time of day of the decision instant is in `[from, until)`, both as `HH:MM`. A window
    /// with `from` after `until` crosses midnight; equal bounds make an empty window.
    TimeBetween {
        from: String,
        until: String,
    },
    /// The UTC weekday of the decision instant is one of `days` (`mon` to `sun`).
    WeekdayIn {
        days: Vec<String>,
    },
    /// The attribute exists.
    Exists {
        attr: String,
    },
    All {
        of: Vec<Condition>,
    },
    Any {
        of: Vec<Condition>,
    },
    Not {
        cond: Box<Condition>,
    },
    /// Function registered in the engine's [`ConditionRegistry`].
    Call {
        func: String,
        #[serde(default)]
        args: Vec<Operand>,
    },
}

impl Condition {
    pub fn eq(left: Operand, right: Operand) -> Self {
        Self::Eq { left, right }
    }

    pub fn ne(left: Operand, right: Operand) -> Self {
        Self::Ne { left, right }
    }

    pub fn lt(left: Operand, right: Operand) -> Self {
        Self::Lt { left, right }
    }

    pub fn le(left: Operand, right: Operand) -> Self {
        Self::Le { left, right }
    }

    pub fn gt(left: Operand, right: Operand) -> Self {
        Self::Gt { left, right }
    }

    pub fn ge(left: Operand, right: Operand) -> Self {
        Self::Ge { left, right }
    }

    pub fn is_in(item: Operand, set: Operand) -> Self {
        Self::In { item, set }
    }

    pub fn starts_with(left: Operand, right: Operand) -> Self {
        Self::StartsWith { left, right }
    }

    pub fn ends_with(left: Operand, right: Operand) -> Self {
        Self::EndsWith { left, right }
    }

    pub fn contains(left: Operand, right: Operand) -> Self {
        Self::Contains { left, right }
    }

    pub fn any_in(left: Operand, right: Operand) -> Self {
        Self::AnyIn { left, right }
    }

    pub fn all_in(left: Operand, right: Operand) -> Self {
        Self::AllIn { left, right }
    }

    pub fn ip_in(item: Operand, set: Operand) -> Self {
        Self::IpIn { item, set }
    }

    pub fn time_between(from: impl Into<String>, until: impl Into<String>) -> Self {
        Self::TimeBetween {
            from: from.into(),
            until: until.into(),
        }
    }

    pub fn weekday_in<S: Into<String>>(days: impl IntoIterator<Item = S>) -> Self {
        Self::WeekdayIn {
            days: days.into_iter().map(Into::into).collect(),
        }
    }

    pub fn exists(attr: impl Into<String>) -> Self {
        Self::Exists { attr: attr.into() }
    }

    pub fn all(of: impl IntoIterator<Item = Condition>) -> Self {
        Self::All {
            of: of.into_iter().collect(),
        }
    }

    pub fn any(of: impl IntoIterator<Item = Condition>) -> Self {
        Self::Any {
            of: of.into_iter().collect(),
        }
    }

    #[allow(clippy::should_implement_trait)]
    pub fn not(cond: Condition) -> Self {
        Self::Not { cond: Box::new(cond) }
    }

    pub fn call(func: impl Into<String>, args: impl IntoIterator<Item = Operand>) -> Self {
        Self::Call {
            func: func.into(),
            args: args.into_iter().collect(),
        }
    }

    /// Names of the functions used, to validate against the registry before writing the grant.
    pub fn functions(&self) -> Vec<&str> {
        let mut out = Vec::new();
        self.collect_functions(&mut out);
        out
    }

    fn collect_functions<'a>(&'a self, out: &mut Vec<&'a str>) {
        match self {
            Condition::Call { func, .. } => out.push(func),
            Condition::All { of } | Condition::Any { of } => of.iter().for_each(|c| c.collect_functions(out)),
            Condition::Not { cond } => cond.collect_functions(out),
            _ => {}
        }
    }

    /// Evaluates without a decision instant: the time conditions fail with a missing `now`.
    pub fn evaluate(&self, ctx: &EvalContext<'_>, registry: &ConditionRegistry) -> Result<bool, ConditionError> {
        self.evaluate_at(ctx, registry, None)
    }

    /// Evaluates at `now`, in milliseconds since the Unix epoch, the instant that `time_between` and
    /// `weekday_in` read, always in UTC. The core reads no clock; without an instant those two fail
    /// with [`ConditionError::MissingAttribute`], so an `allow` they guard does not apply.
    pub fn evaluate_at(
        &self,
        ctx: &EvalContext<'_>,
        registry: &ConditionRegistry,
        now: Option<u64>,
    ) -> Result<bool, ConditionError> {
        match self {
            Condition::Eq { left, right } => Ok(ctx.operand(left)?.loosely_eq(&ctx.operand(right)?)),
            Condition::Ne { left, right } => Ok(!ctx.operand(left)?.loosely_eq(&ctx.operand(right)?)),
            Condition::Lt { left, right } => ctx.order("lt", left, right, |o| o == Ordering::Less),
            Condition::Le { left, right } => ctx.order("le", left, right, |o| o != Ordering::Greater),
            Condition::Gt { left, right } => ctx.order("gt", left, right, |o| o == Ordering::Greater),
            Condition::Ge { left, right } => ctx.order("ge", left, right, |o| o != Ordering::Less),
            Condition::In { item, set } => match ctx.operand(set)? {
                Value::List(values) => {
                    let item = ctx.operand(item)?;
                    Ok(values.iter().any(|v| v.loosely_eq(&item)))
                }
                _ => Err(ConditionError::TypeMismatch(
                    "in: o conjunto precisa ser lista".to_string(),
                )),
            },
            Condition::StartsWith { left, right } => ctx.text("starts_with", left, right, |t, p| t.starts_with(p)),
            Condition::EndsWith { left, right } => ctx.text("ends_with", left, right, |t, p| t.ends_with(p)),
            Condition::Contains { left, right } => match ctx.operand(left)? {
                Value::List(values) => {
                    let item = ctx.operand(right)?;
                    Ok(values.iter().any(|v| v.loosely_eq(&item)))
                }
                Value::Str(text) => match ctx.operand(right)? {
                    Value::Str(part) => Ok(text.contains(part.as_str())),
                    other => Err(ConditionError::TypeMismatch(format!(
                        "contains: texto só contém texto, veio {other:?}"
                    ))),
                },
                other => Err(ConditionError::TypeMismatch(format!(
                    "contains: o lado esquerdo precisa ser texto ou lista, veio {other:?}"
                ))),
            },
            Condition::AnyIn { left, right } => {
                let (items, set) = ctx.lists("any_in", left, right)?;
                Ok(items.iter().any(|i| set.iter().any(|v| v.loosely_eq(i))))
            }
            Condition::AllIn { left, right } => {
                let (items, set) = ctx.lists("all_in", left, right)?;
                Ok(items.iter().all(|i| set.iter().any(|v| v.loosely_eq(i))))
            }
            Condition::IpIn { item, set } => {
                let addr = match ctx.operand(item)? {
                    Value::Str(text) => parse_addr(&text)?,
                    other => {
                        return Err(ConditionError::TypeMismatch(format!(
                            "ip_in: o endereço precisa ser texto, veio {other:?}"
                        )));
                    }
                };
                match ctx.operand(set)? {
                    Value::Str(cidr) => Ok(Cidr::parse(&cidr)?.contains(addr)),
                    Value::List(values) => {
                        for value in &values {
                            let Value::Str(cidr) = value else {
                                return Err(ConditionError::TypeMismatch(format!(
                                    "ip_in: CIDR precisa ser texto, veio {value:?}"
                                )));
                            };
                            if Cidr::parse(cidr)?.contains(addr) {
                                return Ok(true);
                            }
                        }
                        Ok(false)
                    }
                    other => Err(ConditionError::TypeMismatch(format!(
                        "ip_in: o conjunto precisa ser CIDR ou lista de CIDRs, veio {other:?}"
                    ))),
                }
            }
            Condition::TimeBetween { from, until } => {
                let (from, until) = (parse_clock(from)?, parse_clock(until)?);
                let at = minute_of_day(now)?;
                Ok(if from <= until {
                    from <= at && at < until
                } else {
                    at >= from || at < until
                })
            }
            Condition::WeekdayIn { days } => {
                let today = weekday_of(now)?;
                for day in days {
                    if parse_weekday(day)? == today {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
            Condition::Exists { attr } => Ok(ctx.resolve(attr)?.is_some()),
            Condition::All { of } => {
                for cond in of {
                    if !cond.evaluate_at(ctx, registry, now)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            Condition::Any { of } => {
                for cond in of {
                    if cond.evaluate_at(ctx, registry, now)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
            Condition::Not { cond } => Ok(!cond.evaluate_at(ctx, registry, now)?),
            Condition::Call { func, args } => {
                let f = registry
                    .get(func)
                    .ok_or_else(|| ConditionError::UnknownFunction(func.clone()))?;
                let values = args.iter().map(|a| ctx.operand(a)).collect::<Result<Vec<_>, _>>()?;
                f(&values)
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ConditionError {
    #[error("atributo ausente: {0}")]
    MissingAttribute(String),
    #[error("caminho de atributo inválido: {0}")]
    BadPath(String),
    #[error("tipos incompatíveis: {0}")]
    TypeMismatch(String),
    #[error("função não registrada: {0}")]
    UnknownFunction(String),
    #[error("{0}")]
    Function(String),
}

/// What a condition sees during the decision.
#[derive(Clone, Copy)]
pub struct EvalContext<'a> {
    pub subject: &'a Subject,
    pub resource: &'a Resource,
    pub env: &'a Attributes,
}

impl EvalContext<'_> {
    pub fn resolve(&self, path: &str) -> Result<Option<Value>, ConditionError> {
        let (root, name) = path
            .split_once('.')
            .ok_or_else(|| ConditionError::BadPath(path.to_string()))?;
        let value = match (root, name) {
            ("subject", "id") => self.subject.id.clone().map(Value::Str),
            ("subject", "kind") => Some(Value::Str(self.subject.kind.clone())),
            ("subject", _) => self.subject.attrs.get(name).cloned(),
            ("resource", "id") => self.resource.primary().and_then(|a| a.id.clone()).map(Value::Str),
            ("resource", "kind") => self.resource.primary().map(|a| Value::Str(a.kind.clone())),
            ("resource", _) => self.resource.attrs.get(name).cloned(),
            ("env", _) => self.env.get(name).cloned(),
            _ => return Err(ConditionError::BadPath(path.to_string())),
        };
        Ok(value)
    }

    fn operand(&self, operand: &Operand) -> Result<Value, ConditionError> {
        match operand {
            Operand::Lit(value) => Ok(value.clone()),
            Operand::Attr { attr } => self
                .resolve(attr)?
                .ok_or_else(|| ConditionError::MissingAttribute(attr.clone())),
        }
    }

    fn text(
        &self,
        op: &str,
        left: &Operand,
        right: &Operand,
        test: impl Fn(&str, &str) -> bool,
    ) -> Result<bool, ConditionError> {
        match (self.operand(left)?, self.operand(right)?) {
            (Value::Str(l), Value::Str(r)) => Ok(test(&l, &r)),
            (l, r) => Err(ConditionError::TypeMismatch(format!(
                "{op}: {l:?} e {r:?} precisam ser texto"
            ))),
        }
    }

    fn lists(&self, op: &str, left: &Operand, right: &Operand) -> Result<(Vec<Value>, Vec<Value>), ConditionError> {
        match (self.operand(left)?, self.operand(right)?) {
            (Value::List(l), Value::List(r)) => Ok((l, r)),
            (l, r) => Err(ConditionError::TypeMismatch(format!(
                "{op}: {l:?} e {r:?} precisam ser listas"
            ))),
        }
    }

    fn order(
        &self,
        op: &str,
        left: &Operand,
        right: &Operand,
        accept: impl Fn(Ordering) -> bool,
    ) -> Result<bool, ConditionError> {
        let (l, r) = (self.operand(left)?, self.operand(right)?);
        l.compare(&r)
            .map(accept)
            .ok_or_else(|| ConditionError::TypeMismatch(format!("{op}: {l:?} e {r:?} não se comparam")))
    }
}

fn bad_value(what: &str, text: &str) -> ConditionError {
    ConditionError::TypeMismatch(format!("{what} inválido: {text:?}"))
}

/// An address with an IPv4-mapped IPv6 form read as the IPv4 address it wraps.
fn parse_addr(text: &str) -> Result<IpAddr, ConditionError> {
    text.parse::<IpAddr>()
        .map(|a| a.to_canonical())
        .map_err(|_| bad_value("endereço IP", text))
}

/// A CIDR block, normalized so that IPv4 and its IPv4-mapped IPv6 spelling are the same block.
struct Cidr {
    network: IpAddr,
    prefix: u8,
}

impl Cidr {
    fn parse(text: &str) -> Result<Self, ConditionError> {
        let (addr, prefix) = match text.split_once('/') {
            Some((addr, prefix)) => (addr, Some(prefix)),
            None => (text, None),
        };
        let addr: IpAddr = addr.parse().map_err(|_| bad_value("CIDR", text))?;
        let max: u8 = if addr.is_ipv4() { 32 } else { 128 };
        let prefix = match prefix {
            None => max,
            Some(p) => p
                .parse::<u8>()
                .ok()
                .filter(|p| *p <= max)
                .ok_or_else(|| bad_value("CIDR", text))?,
        };
        // `::ffff:a.b.c.d/n` with n >= 96 is the IPv4 block `a.b.c.d/(n - 96)`.
        if let IpAddr::V6(v6) = addr
            && prefix >= 96
            && let Some(v4) = v6.to_ipv4_mapped()
        {
            return Ok(Self {
                network: IpAddr::V4(v4),
                prefix: prefix - 96,
            });
        }
        Ok(Self { network: addr, prefix })
    }

    fn contains(&self, addr: IpAddr) -> bool {
        match (self.network, addr) {
            (IpAddr::V4(n), IpAddr::V4(a)) => {
                let mask = u32::MAX.checked_shl(32 - u32::from(self.prefix)).unwrap_or(0);
                u32::from(n) & mask == u32::from(a) & mask
            }
            (IpAddr::V6(n), IpAddr::V6(a)) => {
                let mask = u128::MAX.checked_shl(128 - u32::from(self.prefix)).unwrap_or(0);
                u128::from(n) & mask == u128::from(a) & mask
            }
            _ => false,
        }
    }
}

/// Checks a CIDR block, for the language to refuse a literal one before any decision.
#[cfg(feature = "dsl")]
pub(crate) fn check_cidr(text: &str) -> Result<(), ConditionError> {
    Cidr::parse(text).map(|_| ())
}

/// Minutes since midnight of an `HH:MM` clock time.
pub(crate) fn parse_clock(text: &str) -> Result<u32, ConditionError> {
    let parsed = text.split_once(':').and_then(|(h, m)| {
        let digits = |s: &str| s.len() == 2 && s.bytes().all(|b| b.is_ascii_digit());
        if !digits(h) || !digits(m) {
            return None;
        }
        let (h, m) = (h.parse::<u32>().ok()?, m.parse::<u32>().ok()?);
        (h < 24 && m < 60).then_some(h * 60 + m)
    });
    parsed.ok_or_else(|| bad_value("horário (use HH:MM)", text))
}

/// Day of the week, 0 for Monday.
pub(crate) fn parse_weekday(text: &str) -> Result<u8, ConditionError> {
    const DAYS: [&str; 7] = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
    DAYS.iter()
        .position(|d| *d == text)
        .and_then(|i| u8::try_from(i).ok())
        .ok_or_else(|| bad_value("dia da semana (mon a sun)", text))
}

const MS_PER_MINUTE: u64 = 60_000;
const MS_PER_DAY: u64 = 86_400_000;

fn minute_of_day(now: Option<u64>) -> Result<u32, ConditionError> {
    let now = now.ok_or_else(|| ConditionError::MissingAttribute("now".to_string()))?;
    u32::try_from(now % MS_PER_DAY / MS_PER_MINUTE).map_err(|_| ConditionError::BadPath("now".to_string()))
}

fn weekday_of(now: Option<u64>) -> Result<u8, ConditionError> {
    let now = now.ok_or_else(|| ConditionError::MissingAttribute("now".to_string()))?;
    // Day 0 of the epoch, 1970-01-01, was a Thursday.
    u8::try_from((now / MS_PER_DAY + 3) % 7).map_err(|_| ConditionError::BadPath("now".to_string()))
}

pub type ConditionFn = Arc<dyn Fn(&[Value]) -> Result<bool, ConditionError> + Send + Sync>;

/// Functions that conditions can call by name. ABAC extension point.
#[derive(Clone, Default)]
pub struct ConditionRegistry {
    fns: HashMap<String, ConditionFn>,
}

impl ConditionRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn register(
        mut self,
        name: impl Into<String>,
        f: impl Fn(&[Value]) -> Result<bool, ConditionError> + Send + Sync + 'static,
    ) -> Self {
        self.fns.insert(name.into(), Arc::new(f));
        self
    }

    pub fn get(&self, name: &str) -> Option<&ConditionFn> {
        self.fns.get(name)
    }

    pub fn contains(&self, name: &str) -> bool {
        self.fns.contains_key(name)
    }
}

impl fmt::Debug for ConditionRegistry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut names: Vec<_> = self.fns.keys().collect();
        names.sort();
        f.debug_struct("ConditionRegistry").field("fns", &names).finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn eval(
        cond: &Condition,
        subject: &Subject,
        resource: &Resource,
        env: &Attributes,
    ) -> Result<bool, ConditionError> {
        let registry = ConditionRegistry::new().register("starts_with", |args| match args {
            [Value::Str(s), Value::Str(prefix)] => Ok(s.starts_with(prefix.as_str())),
            _ => Err(ConditionError::Function("starts_with(str, str)".to_string())),
        });
        cond.evaluate(&EvalContext { subject, resource, env }, &registry)
    }

    #[test]
    fn compares_subject_and_resource_attributes() {
        let s = Subject::user("u").with_attr("clearance", 3);
        let r = Resource::new("doc", "d").with_attr("level", 2);
        let cond = Condition::ge(Operand::attr("subject.clearance"), Operand::attr("resource.level"));
        assert!(eval(&cond, &s, &r, &Attributes::new()).unwrap());
    }

    #[test]
    fn missing_attribute_is_an_error_but_exists_is_false() {
        let s = Subject::user("u");
        let r = Resource::new("doc", "d");
        let env = Attributes::new();
        let cond = Condition::eq(Operand::attr("subject.dept"), Operand::lit("eng"));
        assert_eq!(
            eval(&cond, &s, &r, &env),
            Err(ConditionError::MissingAttribute("subject.dept".to_string()))
        );
        assert!(!eval(&Condition::exists("subject.dept"), &s, &r, &env).unwrap());
    }

    #[test]
    fn in_and_custom_function() {
        let s = Subject::user("u").with_attr("dept", "eng");
        let r = Resource::new("doc", "draft-1");
        let mut env = Attributes::new();
        env.insert("depts".to_string(), Value::from(vec!["eng", "ops"]));
        let cond = Condition::all([
            Condition::is_in(Operand::attr("subject.dept"), Operand::attr("env.depts")),
            Condition::call("starts_with", [Operand::attr("resource.id"), Operand::lit("draft")]),
        ]);
        assert!(eval(&cond, &s, &r, &env).unwrap());
        assert_eq!(cond.functions(), ["starts_with"]);
    }

    #[test]
    fn json_shape() {
        let cond: Condition = serde_json::from_str(
            r#"{"op":"any","of":[{"op":"eq","left":{"attr":"subject.id"},"right":"alice"},{"op":"not","cond":{"op":"exists","attr":"resource.locked"}}]}"#,
        )
        .unwrap();
        let s = Subject::user("bob");
        assert!(eval(&cond, &s, &Resource::new("doc", "d"), &Attributes::new()).unwrap());
    }

    // ---- string, set, network and time conditions ----

    fn lit(v: impl Into<Value>) -> Operand {
        Operand::lit(v)
    }

    fn holds(cond: &Condition) -> Result<bool, ConditionError> {
        let (s, r) = (Subject::user("u"), Resource::new("doc", "d"));
        cond.evaluate(
            &EvalContext {
                subject: &s,
                resource: &r,
                env: &Attributes::new(),
            },
            &ConditionRegistry::new(),
        )
    }

    fn holds_at(cond: &Condition, now: u64) -> Result<bool, ConditionError> {
        let (s, r) = (Subject::user("u"), Resource::new("doc", "d"));
        cond.evaluate_at(
            &EvalContext {
                subject: &s,
                resource: &r,
                env: &Attributes::new(),
            },
            &ConditionRegistry::new(),
            Some(now),
        )
    }

    fn is_mismatch(r: Result<bool, ConditionError>) -> bool {
        matches!(r, Err(ConditionError::TypeMismatch(_)))
    }

    #[test]
    fn text_conditions_match_prefix_suffix_and_substring() {
        assert_eq!(holds(&Condition::starts_with(lit("draft-1"), lit("draft"))), Ok(true));
        assert_eq!(holds(&Condition::starts_with(lit("draft-1"), lit("final"))), Ok(false));
        assert_eq!(holds(&Condition::ends_with(lit("a.pdf"), lit(".pdf"))), Ok(true));
        assert_eq!(holds(&Condition::ends_with(lit("a.pdf"), lit(".txt"))), Ok(false));
        assert_eq!(holds(&Condition::contains(lit("a-b-c"), lit("-b-"))), Ok(true));
        assert_eq!(holds(&Condition::contains(lit("abc"), lit("x"))), Ok(false));
    }

    #[test]
    fn text_conditions_treat_the_empty_text_as_prefix_suffix_and_substring_of_everything() {
        assert_eq!(holds(&Condition::starts_with(lit("abc"), lit(""))), Ok(true));
        assert_eq!(holds(&Condition::ends_with(lit("abc"), lit(""))), Ok(true));
        assert_eq!(holds(&Condition::contains(lit("abc"), lit(""))), Ok(true));
        assert_eq!(holds(&Condition::starts_with(lit(""), lit("a"))), Ok(false));
        assert_eq!(holds(&Condition::contains(lit(""), lit(""))), Ok(true));
    }

    #[test]
    fn text_conditions_reject_operands_that_are_not_text() {
        assert!(is_mismatch(holds(&Condition::starts_with(lit(1), lit("a")))));
        assert!(is_mismatch(holds(&Condition::ends_with(lit("a"), lit(true)))));
        assert!(is_mismatch(holds(&Condition::contains(lit("a"), lit(1)))));
        assert!(is_mismatch(holds(&Condition::contains(lit(1), lit(1)))));
    }

    #[test]
    fn contains_on_a_list_is_membership_with_loose_numbers() {
        let list = lit(Value::from(vec!["a", "b"]));
        assert_eq!(holds(&Condition::contains(list.clone(), lit("b"))), Ok(true));
        assert_eq!(holds(&Condition::contains(list, lit("c"))), Ok(false));
        let nums = lit(Value::List(vec![Value::Int(1), Value::Int(2)]));
        assert_eq!(holds(&Condition::contains(nums, lit(2.0))), Ok(true));
        assert_eq!(
            holds(&Condition::contains(lit(Value::List(vec![])), lit("a"))),
            Ok(false)
        );
    }

    #[test]
    fn missing_attribute_in_a_new_condition_is_an_error() {
        let cond = Condition::contains(Operand::attr("subject.tags"), lit("a"));
        assert_eq!(
            holds(&cond),
            Err(ConditionError::MissingAttribute("subject.tags".to_string()))
        );
    }

    #[test]
    fn any_in_needs_one_common_element() {
        let set = lit(Value::from(vec!["a", "b"]));
        assert_eq!(
            holds(&Condition::any_in(lit(Value::from(vec!["x", "b"])), set.clone())),
            Ok(true)
        );
        assert_eq!(
            holds(&Condition::any_in(lit(Value::from(vec!["x", "y"])), set.clone())),
            Ok(false)
        );
        assert_eq!(
            holds(&Condition::any_in(lit(Value::List(vec![])), set.clone())),
            Ok(false)
        );
        assert_eq!(
            holds(&Condition::any_in(
                lit(Value::from(vec!["a"])),
                lit(Value::List(vec![]))
            )),
            Ok(false)
        );
        assert!(is_mismatch(holds(&Condition::any_in(lit("a"), set))));
    }

    #[test]
    fn all_in_needs_every_element_and_an_empty_list_holds() {
        let set = lit(Value::from(vec!["a", "b"]));
        assert_eq!(
            holds(&Condition::all_in(lit(Value::from(vec!["a", "b"])), set.clone())),
            Ok(true)
        );
        assert_eq!(
            holds(&Condition::all_in(lit(Value::from(vec!["a", "x"])), set.clone())),
            Ok(false)
        );
        assert_eq!(
            holds(&Condition::all_in(lit(Value::List(vec![])), set.clone())),
            Ok(true)
        );
        assert_eq!(
            holds(&Condition::all_in(
                lit(Value::from(vec!["a"])),
                lit(Value::List(vec![]))
            )),
            Ok(false)
        );
        assert!(is_mismatch(holds(&Condition::all_in(set, lit("a")))));
    }

    #[test]
    fn ip_in_matches_ipv4_and_ipv6_blocks() {
        let v4 = |ip: &str, cidr: &str| holds(&Condition::ip_in(lit(ip), lit(cidr)));
        assert_eq!(v4("10.1.2.3", "10.0.0.0/8"), Ok(true));
        assert_eq!(v4("11.0.0.1", "10.0.0.0/8"), Ok(false));
        assert_eq!(v4("10.0.0.1", "10.0.0.1"), Ok(true), "no prefix is a single host");
        assert_eq!(v4("10.0.0.2", "10.0.0.1"), Ok(false));
        assert_eq!(v4("1.2.3.4", "0.0.0.0/0"), Ok(true));
        assert_eq!(v4("2001:db8::1", "2001:db8::/32"), Ok(true));
        assert_eq!(v4("2001:db9::1", "2001:db8::/32"), Ok(false));
        assert_eq!(v4("::1", "::1/128"), Ok(true));
        assert_eq!(v4("2001:db8::1", "::/0"), Ok(true));
        assert_eq!(v4("10.0.0.1", "2001:db8::/32"), Ok(false), "families do not mix");
    }

    #[test]
    fn ip_in_reads_an_ipv4_mapped_ipv6_address_as_ipv4() {
        let ip = |ip: &str, cidr: &str| holds(&Condition::ip_in(lit(ip), lit(cidr)));
        assert_eq!(ip("::ffff:10.1.2.3", "10.0.0.0/8"), Ok(true));
        assert_eq!(ip("::ffff:11.1.2.3", "10.0.0.0/8"), Ok(false));
        assert_eq!(ip("10.1.2.3", "::ffff:10.0.0.0/104"), Ok(true));
        assert_eq!(ip("10.1.2.3", "::ffff:11.0.0.0/104"), Ok(false));
        assert_eq!(ip("::ffff:10.1.2.3", "::ffff:10.0.0.0/104"), Ok(true));
    }

    #[test]
    fn ip_in_takes_a_list_of_blocks() {
        let blocks = lit(Value::from(vec!["10.0.0.0/8", "192.168.0.0/16"]));
        assert_eq!(holds(&Condition::ip_in(lit("192.168.1.1"), blocks.clone())), Ok(true));
        assert_eq!(holds(&Condition::ip_in(lit("172.16.0.1"), blocks)), Ok(false));
        assert_eq!(
            holds(&Condition::ip_in(lit("10.0.0.1"), lit(Value::List(vec![])))),
            Ok(false)
        );
    }

    #[test]
    fn ip_in_rejects_invalid_addresses_and_blocks() {
        for bad in [
            "10.0.0.0/33",
            "10.0.0.0/",
            "10.0.0.0/-1",
            "10.0.0/8",
            "nope",
            "",
            "::1/129",
            "10.0.0.0/8/8",
        ] {
            assert!(
                is_mismatch(holds(&Condition::ip_in(lit("10.0.0.1"), lit(bad)))),
                "{bad}"
            );
        }
        assert!(is_mismatch(holds(&Condition::ip_in(
            lit("10.0.0.256"),
            lit("10.0.0.0/8")
        ))));
        assert!(is_mismatch(holds(&Condition::ip_in(lit(5), lit("10.0.0.0/8")))));
        assert!(is_mismatch(holds(&Condition::ip_in(lit("10.0.0.1"), lit(5)))));
        assert!(is_mismatch(holds(&Condition::ip_in(
            lit("10.0.0.1"),
            lit(Value::List(vec![Value::Int(1)]))
        ))));
        // a bad block after a match is not reached, one before it fails
        let tail = lit(Value::from(vec!["10.0.0.0/8", "bad"]));
        assert_eq!(holds(&Condition::ip_in(lit("10.0.0.1"), tail)), Ok(true));
        let head = lit(Value::from(vec!["bad", "10.0.0.0/8"]));
        assert!(is_mismatch(holds(&Condition::ip_in(lit("10.0.0.1"), head))));
    }

    const MINUTE: u64 = 60_000;
    const HOUR: u64 = 60 * MINUTE;
    const DAY: u64 = 24 * HOUR;
    // 2024-01-01 was a Monday.
    const MONDAY: u64 = 1_704_067_200_000;

    #[test]
    fn time_between_is_half_open_in_utc() {
        let c = Condition::time_between("09:00", "17:30");
        assert_eq!(holds_at(&c, MONDAY + 8 * HOUR + 59 * MINUTE), Ok(false));
        assert_eq!(holds_at(&c, MONDAY + 9 * HOUR), Ok(true));
        assert_eq!(holds_at(&c, MONDAY + 17 * HOUR + 29 * MINUTE + 59_999), Ok(true));
        assert_eq!(holds_at(&c, MONDAY + 17 * HOUR + 30 * MINUTE), Ok(false));
        assert_eq!(holds_at(&c, 0), Ok(false), "the epoch is midnight UTC");
    }

    #[test]
    fn time_between_accepts_a_window_that_crosses_midnight() {
        let c = Condition::time_between("22:00", "06:00");
        assert_eq!(holds_at(&c, MONDAY + 21 * HOUR + 59 * MINUTE), Ok(false));
        assert_eq!(holds_at(&c, MONDAY + 22 * HOUR), Ok(true));
        assert_eq!(holds_at(&c, MONDAY + 23 * HOUR + 59 * MINUTE), Ok(true));
        assert_eq!(holds_at(&c, MONDAY), Ok(true), "midnight");
        assert_eq!(holds_at(&c, MONDAY + 5 * HOUR + 59 * MINUTE), Ok(true));
        assert_eq!(holds_at(&c, MONDAY + 6 * HOUR), Ok(false));
        assert_eq!(holds_at(&c, MONDAY + 12 * HOUR), Ok(false));
        let to_midnight = Condition::time_between("22:00", "00:00");
        assert_eq!(holds_at(&to_midnight, MONDAY + 23 * HOUR), Ok(true));
        assert_eq!(holds_at(&to_midnight, MONDAY), Ok(false));
    }

    #[test]
    fn time_between_with_equal_bounds_is_an_empty_window() {
        let c = Condition::time_between("09:00", "09:00");
        assert_eq!(holds_at(&c, MONDAY + 9 * HOUR), Ok(false));
        assert_eq!(holds_at(&c, MONDAY), Ok(false));
    }

    #[test]
    fn time_between_rejects_malformed_clock_times() {
        for bad in ["24:00", "9:00", "09:60", "09-00", "", "09:00:00", "ab:cd"] {
            let c = Condition::time_between(bad, "10:00");
            assert!(is_mismatch(holds_at(&c, MONDAY)), "{bad}");
        }
    }

    #[test]
    fn weekday_in_reads_the_utc_weekday() {
        let c = Condition::weekday_in(["mon", "sun"]);
        assert_eq!(holds_at(&c, MONDAY), Ok(true));
        assert_eq!(holds_at(&c, MONDAY + DAY - 1), Ok(true));
        assert_eq!(holds_at(&c, MONDAY + DAY), Ok(false), "tuesday");
        assert_eq!(holds_at(&c, MONDAY + 6 * DAY), Ok(true), "sunday");
        assert_eq!(holds_at(&c, 0), Ok(false), "the epoch was a thursday");
        assert_eq!(holds_at(&Condition::weekday_in(["thu"]), 0), Ok(true));
        assert_eq!(
            holds_at(&Condition::weekday_in(Vec::<String>::new()), MONDAY),
            Ok(false)
        );
        assert!(is_mismatch(holds_at(&Condition::weekday_in(["monday"]), MONDAY)));
    }

    #[test]
    fn time_conditions_without_an_instant_are_an_error_that_closes_access() {
        let missing = Err(ConditionError::MissingAttribute("now".to_string()));
        assert_eq!(holds(&Condition::time_between("00:00", "23:59")), missing);
        assert_eq!(holds(&Condition::weekday_in(["mon"])), missing);
        let nested = Condition::not(Condition::all([Condition::weekday_in(["mon"])]));
        assert_eq!(holds(&nested), missing);
        assert_eq!(
            holds_at(&nested, MONDAY),
            Ok(false),
            "the instant reaches nested conditions"
        );
    }

    #[test]
    fn new_conditions_have_their_own_json_shape() {
        let cases = [
            r#"{"op":"starts_with","left":{"attr":"resource.id"},"right":"draft"}"#,
            r#"{"op":"ends_with","left":"a.pdf","right":".pdf"}"#,
            r#"{"op":"contains","left":["a"],"right":"a"}"#,
            r#"{"op":"any_in","left":["a"],"right":["a","b"]}"#,
            r#"{"op":"all_in","left":["a"],"right":["a","b"]}"#,
            r#"{"op":"ip_in","item":{"attr":"env.ip"},"set":["10.0.0.0/8"]}"#,
            r#"{"op":"time_between","from":"22:00","until":"06:00"}"#,
            r#"{"op":"weekday_in","days":["mon","tue"]}"#,
        ];
        for json in cases {
            let cond: Condition = serde_json::from_str(json).unwrap();
            assert_eq!(serde_json::to_string(&cond).unwrap(), json);
        }
    }
}