strig 0.10.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
//! Approval flow for a [`Mutation`]: a change the [`ApprovalPolicy`] marks as needing approval does not
//! apply on the spot. It becomes a [`PendingRequest`] that another actor approves or refuses.
//!
//! - **Where the policy lives.** Per kind of change ([`MutationKind`]): a rule names the permission an
//!   approver must hold, and the resource they must hold it on. A kind with no rule applies at once, as
//!   [`Governance::apply`] always did.
//! - **Propose.** The invariants judge the change first, so a request that could never apply is refused
//!   up front; a change that passes is stored as pending, with an optional expiry.
//! - **Approve or refuse.** The approver needs the rule's permission and is never the author. An approval
//!   runs the invariants again over the grants as they are *now*, with the author as the actor, and only
//!   then applies; if they refuse, the request stays pending and the violations come back. A refusal
//!   carries a reason.
//! - **Impact.** With [`Approvals::with_impact`], a proposal stores what the change does to access
//!   ([`Impact`]) in the request, so the approver sees it. Approving recomputes it over the grants as
//!   they are now (they may have changed since the proposal); [`Approvals::review_impact`] shows both
//!   before deciding, and an approved request keeps the recomputed one.
//! - **Expiry.** A request is expired from the instant `expires_at` on. [`Approvals::expire_due`] closes
//!   the ones due; approving or refusing one that is due closes it too.
//! - **Audit.** With the `audit` feature and an auditor on the [`Governance`], every step is a record:
//!   `mutation_propose`, `mutation_approve`, `mutation_reject` and `mutation_expire`, under the fail mode
//!   of the call. The approval record is written before the change is applied, so a failed write never
//!   leaves an applied change the trail does not know about.
//!
//! The crate reads no clock: every step takes the instant. Requests live in a [`PendingStore`];
//! [`MemoryPendingStore`] is the in-memory one. The caller persists the grants an approval returns, as
//! with [`Governance::apply`]. Two approvers of the same request must not run at once: the store's
//! [`PendingStore::update`] refuses a request that is no longer pending, and a host with several
//! processes serialises approvals per request (a lock, a transaction) before applying the grants.

use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Mutex, MutexGuard};

use serde::{Deserialize, Serialize};

use crate::attr::{Attributes, Value};
#[cfg(feature = "audit")]
use crate::audit::{AuditAction, AuditOutcome, FailMode, PendingRef};
use crate::decision::Request;
use crate::engine::{Context, Engine};
use crate::governance::{Governance, Mutation, MutationKind, Violation, same_subject};
use crate::grant::Grant;
use crate::impact::{Impact, ImpactOptions};
use crate::rebac::TupleStore;
use crate::resource::Resource;
use crate::subject::Subject;

pub mod elevation;

use elevation::ElevationInfo;

/// Who may approve a kind of change: whoever holds `permission` on `resource`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ApprovalRule {
    pub kind: MutationKind,
    pub permission: String,
    pub resource: Resource,
}

/// The kinds of change that wait for approval.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ApprovalPolicy {
    rules: Vec<ApprovalRule>,
}

impl ApprovalPolicy {
    /// No kind needs approval.
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_rules(rules: impl IntoIterator<Item = ApprovalRule>) -> Self {
        Self {
            rules: rules.into_iter().collect(),
        }
    }

    /// `kind` waits for an approver holding `permission` on `resource`. A later rule for the same kind
    /// replaces the earlier one.
    #[must_use]
    pub fn require(mut self, kind: MutationKind, permission: impl Into<String>, resource: Resource) -> Self {
        self.rules.retain(|r| r.kind != kind);
        self.rules.push(ApprovalRule {
            kind,
            permission: permission.into(),
            resource,
        });
        self
    }

    /// Every kind of change waits for an approver holding `permission` on `resource`.
    #[must_use]
    pub fn require_all(self, permission: impl Into<String>, resource: Resource) -> Self {
        let permission = permission.into();
        MutationKind::ALL.into_iter().fold(self, |policy, kind| {
            policy.require(kind, permission.clone(), resource.clone())
        })
    }

    pub fn rule_for(&self, mutation: &Mutation) -> Option<&ApprovalRule> {
        let kind = mutation.kind();
        self.rules.iter().find(|r| r.kind == kind)
    }

    pub fn rules(&self) -> &[ApprovalRule] {
        &self.rules
    }
}

/// Where a request stands. Only `Pending` can still change.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "snake_case", rename_all_fields = "camelCase")]
pub enum PendingStatus {
    Pending,
    Approved { by: String, at: u64 },
    Rejected { by: String, at: u64, reason: String },
    Expired { at: u64 },
}

/// A change waiting for another actor.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PendingRequest {
    pub id: String,
    /// Whoever proposed the change. The invariants judge the change as this actor, at proposal and at approval.
    pub actor: Subject,
    pub mutation: Mutation,
    /// Milliseconds since the Unix epoch.
    pub requested_at: u64,
    /// From this instant on nobody can decide the request any more.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<u64>,
    pub status: PendingStatus,
    /// What the change does to access: computed when it was proposed, and again when it was approved.
    /// Absent when the flow ran without [`Approvals::with_impact`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub impact: Option<Impact>,
    /// Present when the request is a time-boxed elevation ([`elevation`]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub elevation: Option<ElevationInfo>,
}

impl PendingRequest {
    /// A pending request with no expiry and no impact.
    pub fn new(id: impl Into<String>, actor: Subject, mutation: Mutation, requested_at: u64) -> Self {
        Self {
            id: id.into(),
            actor,
            mutation,
            requested_at,
            expires_at: None,
            status: PendingStatus::Pending,
            impact: None,
            elevation: None,
        }
    }

    #[must_use]
    pub fn expiring_at(mut self, expires_at: Option<u64>) -> Self {
        self.expires_at = expires_at;
        self
    }

    #[must_use]
    pub fn with_status(mut self, status: PendingStatus) -> Self {
        self.status = status;
        self
    }

    #[must_use]
    pub fn with_impact(mut self, impact: Impact) -> Self {
        self.impact = Some(impact);
        self
    }

    #[must_use]
    pub fn with_elevation(mut self, elevation: ElevationInfo) -> Self {
        self.elevation = Some(elevation);
        self
    }

    pub fn is_pending(&self) -> bool {
        self.status == PendingStatus::Pending
    }

    pub fn is_due(&self, now: u64) -> bool {
        self.expires_at.is_some_and(|at| now >= at)
    }

    /// Grant id, or `role@subject` (`role#scope@subject` with a scope) for a role change.
    pub fn target(&self) -> String {
        match &self.mutation {
            Mutation::Add(grant) => grant.id.clone(),
            Mutation::Remove { id } => id.clone(),
            Mutation::AssignRole { subject, role } | Mutation::RevokeRole { subject, role } => {
                let who = subject
                    .id
                    .clone()
                    .unwrap_or_else(|| format!("{}:<anonymous>", subject.kind));
                match &role.scope {
                    Some(scope) => format!("{}#{scope}@{who}", role.role),
                    None => format!("{}@{who}", role.role),
                }
            }
        }
    }

    #[cfg(feature = "audit")]
    fn author(&self) -> String {
        self.actor.id.clone().unwrap_or_else(|| self.actor.kind.clone())
    }
}

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum PendingError {
    #[error("o pedido {0} já existe")]
    Duplicate(String),
    #[error("o pedido {0} não existe")]
    Unknown(String),
    #[error("o pedido {0} não está mais pendente")]
    NotPending(String),
    #[error("falha no armazenamento de pedidos: {0}")]
    Store(String),
}

/// Where the requests are kept. Implementations are shared between threads.
pub trait PendingStore: Send + Sync {
    /// Stores a new request; [`PendingError::Duplicate`] if the id is taken.
    fn insert(&self, request: PendingRequest) -> Result<(), PendingError>;
    fn get(&self, id: &str) -> Result<Option<PendingRequest>, PendingError>;
    /// Replaces the request with the same id. It must fail with [`PendingError::NotPending`] when the stored
    /// request is no longer pending, so a decided request is never decided again.
    fn update(&self, request: PendingRequest) -> Result<(), PendingError>;
    /// Every request, whatever its status, oldest first.
    fn list(&self) -> Result<Vec<PendingRequest>, PendingError>;
    /// Replaces the request with the same id whatever its status, to keep the bookkeeping of a decided
    /// elevation (its revocation, its break-glass review). Only [`elevation`] calls it, and it never
    /// changes `status`. A store that cannot do it keeps the default, which refuses, and the elevation
    /// operations that need it are refused with it.
    fn annotate(&self, request: PendingRequest) -> Result<(), PendingError> {
        Err(PendingError::Store(format!(
            "o armazenamento não anota o pedido {}",
            request.id
        )))
    }
}

/// [`PendingStore`] in memory.
#[derive(Debug, Default)]
pub struct MemoryPendingStore {
    requests: Mutex<BTreeMap<String, PendingRequest>>,
}

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

    fn lock(&self) -> MutexGuard<'_, BTreeMap<String, PendingRequest>> {
        self.requests.lock().unwrap_or_else(|p| p.into_inner())
    }
}

impl PendingStore for MemoryPendingStore {
    fn insert(&self, request: PendingRequest) -> Result<(), PendingError> {
        let mut requests = self.lock();
        if requests.contains_key(&request.id) {
            return Err(PendingError::Duplicate(request.id));
        }
        requests.insert(request.id.clone(), request);
        Ok(())
    }

    fn get(&self, id: &str) -> Result<Option<PendingRequest>, PendingError> {
        Ok(self.lock().get(id).cloned())
    }

    fn update(&self, request: PendingRequest) -> Result<(), PendingError> {
        let mut requests = self.lock();
        match requests.get_mut(&request.id) {
            None => Err(PendingError::Unknown(request.id)),
            Some(stored) if !stored.is_pending() => Err(PendingError::NotPending(request.id)),
            Some(stored) => {
                *stored = request;
                Ok(())
            }
        }
    }

    fn annotate(&self, request: PendingRequest) -> Result<(), PendingError> {
        match self.lock().get_mut(&request.id) {
            None => Err(PendingError::Unknown(request.id)),
            Some(stored) => {
                *stored = request;
                Ok(())
            }
        }
    }

    fn list(&self) -> Result<Vec<PendingRequest>, PendingError> {
        let mut all: Vec<PendingRequest> = self.lock().values().cloned().collect();
        all.sort_by(|a, b| a.requested_at.cmp(&b.requested_at).then_with(|| a.id.cmp(&b.id)));
        Ok(all)
    }
}

#[derive(Debug, thiserror::Error, PartialEq)]
pub enum ApprovalError {
    /// The invariants, the approval rule or the audit refused. The request, if any, stays as it was.
    #[error("recusado: {}", .0.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))]
    Refused(Vec<Violation>),
    /// The request ran out of time; it is now `expired`.
    #[error("o pedido {0} expirou")]
    Expired(String),
    #[error(transparent)]
    Store(#[from] PendingError),
}

impl ApprovalError {
    fn refused(invariant: &str, message: impl Into<String>) -> Self {
        Self::Refused(vec![Violation::new(invariant, message)])
    }
}

/// What a change is judged against: the grants as they are now, the tuples, the instant and the
/// metadata of the call (ip, request id, reason) that the audit keeps from the `detailed` level.
pub struct Review<'a> {
    pub engine: &'a Engine,
    pub current: &'a [Grant],
    pub tuples: Option<&'a dyn TupleStore>,
    pub now: u64,
    pub meta: &'a Attributes,
}

/// The outcome of [`Approvals::propose`].
#[derive(Debug, Clone, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum Proposed {
    /// The policy asks no approval for this kind of change: it went through the governance and these are
    /// the grants after it.
    Applied(Vec<Grant>),
    /// The change waits for an approver.
    Pending(PendingRequest),
}

/// A request approved and applied: the grants to persist (unchanged for a role change; the subject to
/// write comes from [`Mutation::subject_after`] of the request) and the request as it now stands.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Approved {
    pub grants: Vec<Grant>,
    /// The request as it now stands; its `impact` is the one recomputed at the approval.
    pub request: PendingRequest,
    /// The impact the request had when it was proposed, to compare with `request.impact`.
    pub proposed_impact: Option<Impact>,
}

/// The impact of a pending request as proposed and as it would be now, from [`Approvals::review_impact`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ImpactReview {
    pub proposed: Option<Impact>,
    pub current: Option<Impact>,
    /// The grants moved the answer since the proposal.
    pub changed: bool,
}

/// The approval flow over a [`Governance`], an [`ApprovalPolicy`] and a [`PendingStore`].
pub struct Approvals<'a> {
    governance: &'a Governance,
    policy: &'a ApprovalPolicy,
    store: &'a dyn PendingStore,
    impact: Option<&'a ImpactOptions<'a>>,
    distinct_roles: bool,
    #[cfg(feature = "audit")]
    call_mode: Option<FailMode>,
}

const DISTINCT_ROLES: &str = "distinct_approver_roles";
const SELF_APPROVAL: &str = "no_self_approval";
const APPROVAL_PERMISSION: &str = "require_approval_permission";
const SYSTEM_KIND: &str = "system";
const SYSTEM_ID: &str = "strig-expiry";

impl<'a> Approvals<'a> {
    pub fn new(governance: &'a Governance, policy: &'a ApprovalPolicy, store: &'a dyn PendingStore) -> Self {
        Self {
            governance,
            policy,
            store,
            impact: None,
            distinct_roles: false,
            #[cfg(feature = "audit")]
            call_mode: None,
        }
    }

    /// Also refuses an approver who holds a role (or a role that inherits or is inherited by one)
    /// that the author of the request holds: author and approver act with different roles, the
    /// dynamic separation of duties applied to the approval. Without it only the person is
    /// compared, as before.
    #[must_use]
    pub fn with_distinct_roles(mut self) -> Self {
        self.distinct_roles = true;
        self
    }

    /// Computes the [`Impact`] of every change proposed or approved through this flow, over `options`.
    /// The instant is the one of the [`Review`], and `options.tuples` should be the store the review
    /// reads. An impact that cannot be computed (the change is refused) is simply absent.
    #[must_use]
    pub fn with_impact(mut self, options: &'a ImpactOptions<'a>) -> Self {
        self.impact = Some(options);
        self
    }

    fn impact_now(&self, review: &Review<'_>, actor: &Subject, mutation: &Mutation) -> Option<Impact> {
        let options = ImpactOptions {
            now: Some(review.now),
            ..self.impact?.clone()
        };
        self.governance
            .impact(review.engine, actor, review.current, mutation, &options)
            .ok()
    }

    /// The impact of pending request `id` as proposed and as it would be over the grants now, for the
    /// approver to see before deciding. Neither is recorded.
    pub fn review_impact(&self, review: &Review<'_>, id: &str) -> Result<ImpactReview, ApprovalError> {
        let request = self
            .store
            .get(id)?
            .ok_or_else(|| PendingError::Unknown(id.to_string()))?;
        let current = self.impact_now(review, &request.actor, &request.mutation);
        let changed = request.impact != current;
        Ok(ImpactReview {
            proposed: request.impact,
            current,
            changed,
        })
    }

    /// The [`FailMode`] of every audit record of this flow, over the mode of the permission and the
    /// auditor's default.
    #[cfg(feature = "audit")]
    #[must_use]
    pub fn fail_mode(mut self, mode: FailMode) -> Self {
        self.call_mode = Some(mode);
        self
    }

    /// The requests still waiting at `now`: pending and not due.
    pub fn pending(&self, now: u64) -> Result<Vec<PendingRequest>, PendingError> {
        Ok(self
            .store
            .list()?
            .into_iter()
            .filter(|r| r.is_pending() && !r.is_due(now))
            .collect())
    }

    /// `actor` asks for `mutation`. Without a rule for its kind the change goes through the governance
    /// right away. With one, the invariants judge it first and, if they accept, it is stored as a pending
    /// request `id` that expires at `expires_at` (if given). Either way the audit records what happened,
    /// refusals included.
    pub fn propose(
        &self,
        review: &Review<'_>,
        actor: &Subject,
        mutation: &Mutation,
        id: impl Into<String>,
        expires_at: Option<u64>,
    ) -> Result<Proposed, ApprovalError> {
        crate::obs::approval("propose", || {
            self.propose_inner(review, actor, mutation, id.into(), expires_at, None)
        })
    }

    fn propose_inner(
        &self,
        review: &Review<'_>,
        actor: &Subject,
        mutation: &Mutation,
        id: String,
        expires_at: Option<u64>,
        elevation: Option<ElevationInfo>,
    ) -> Result<Proposed, ApprovalError> {
        let Some(rule) = self.policy.rule_for(mutation) else {
            return self.apply_directly(review, actor, mutation).map(Proposed::Applied);
        };
        let request = PendingRequest {
            id,
            actor: actor.clone(),
            mutation: mutation.clone(),
            requested_at: review.now,
            expires_at,
            status: PendingStatus::Pending,
            impact: None,
            elevation,
        };
        let refuse = |violations: Vec<Violation>| -> ApprovalError {
            match self.record(
                review,
                actor,
                Step::Propose,
                &request,
                Some(violations.as_slice()),
                None,
                rule,
            ) {
                Ok(()) => ApprovalError::Refused(violations),
                Err(audit) => audit,
            }
        };
        if request.id.is_empty() {
            return Err(refuse(vec![Violation::new("mutation", "o pedido precisa de um id")]));
        }
        if expires_at.is_some_and(|at| at <= review.now) {
            return Err(refuse(vec![Violation::new(
                "mutation",
                "a expiração do pedido precisa ser depois do instante do pedido",
            )]));
        }
        if self.store.get(&request.id)?.is_some() {
            return Err(PendingError::Duplicate(request.id.clone()).into());
        }
        if let Err(violations) = self.judge(review, actor, mutation) {
            return Err(refuse(violations));
        }
        self.record(review, actor, Step::Propose, &request, None, None, rule)?;
        let mut request = request;
        request.impact = self.impact_now(review, actor, mutation);
        self.store.insert(request.clone())?;
        Ok(Proposed::Pending(request))
    }

    /// `approver` approves request `id`. Refused, with the request left pending, when the approver is the
    /// author, lacks the rule's permission, or the invariants now refuse the change; the refusal is
    /// audited. On success the change has been applied through the governance and the request is
    /// `approved`.
    pub fn approve(&self, review: &Review<'_>, approver: &Subject, id: &str) -> Result<Approved, ApprovalError> {
        crate::obs::approval("approve", || self.approve_inner(review, approver, id))
    }

    fn approve_inner(&self, review: &Review<'_>, approver: &Subject, id: &str) -> Result<Approved, ApprovalError> {
        let (request, rule) = self.open_for_decision(review, id)?;
        let refuse = |violations: Vec<Violation>| -> ApprovalError {
            match self.record(
                review,
                approver,
                Step::Approve,
                &request,
                Some(violations.as_slice()),
                None,
                &rule,
            ) {
                Ok(()) => ApprovalError::Refused(violations),
                Err(audit) => audit,
            }
        };
        let violations = self.approver_violations(review, approver, &request, &rule);
        if !violations.is_empty() {
            return Err(refuse(violations));
        }
        if let Err(violations) = self.judge(review, &request.actor, &request.mutation) {
            return Err(refuse(violations));
        }
        self.record(review, approver, Step::Approve, &request, None, None, &rule)?;
        let current_impact = self.impact_now(review, &request.actor, &request.mutation);
        let mut meta = review.meta.clone();
        meta.insert("request_id".into(), Value::Str(request.id.clone()));
        meta.insert("approved_by".into(), Value::Str(subject_label(approver)));
        let grants = self.apply_directly(&Review { meta: &meta, ..*review }, &request.actor, &request.mutation)?;
        let mut request = request;
        let proposed_impact = request.impact.take();
        request.impact = current_impact;
        request.status = PendingStatus::Approved {
            by: subject_label(approver),
            at: review.now,
        };
        self.store.update(request.clone())?;
        Ok(Approved {
            grants,
            request,
            proposed_impact,
        })
    }

    /// `approver` refuses request `id`, with a non-empty `reason`. Same checks on the approver as in
    /// [`Approvals::approve`]; nothing is applied.
    pub fn reject(
        &self,
        review: &Review<'_>,
        approver: &Subject,
        id: &str,
        reason: &str,
    ) -> Result<PendingRequest, ApprovalError> {
        crate::obs::approval("reject", || self.reject_inner(review, approver, id, reason))
    }

    fn reject_inner(
        &self,
        review: &Review<'_>,
        approver: &Subject,
        id: &str,
        reason: &str,
    ) -> Result<PendingRequest, ApprovalError> {
        let (request, rule) = self.open_for_decision(review, id)?;
        let mut violations = self.approver_violations(review, approver, &request, &rule);
        if reason.trim().is_empty() {
            violations.push(Violation::new("mutation", "a recusa precisa de um motivo"));
        }
        if !violations.is_empty() {
            return Err(
                match self.record(
                    review,
                    approver,
                    Step::Reject,
                    &request,
                    Some(violations.as_slice()),
                    Some(reason),
                    &rule,
                ) {
                    Ok(()) => ApprovalError::Refused(violations),
                    Err(audit) => audit,
                },
            );
        }
        self.record(review, approver, Step::Reject, &request, None, Some(reason), &rule)?;
        let mut request = request;
        request.status = PendingStatus::Rejected {
            by: subject_label(approver),
            at: review.now,
            reason: reason.to_string(),
        };
        self.store.update(request.clone())?;
        Ok(request)
    }

    /// Closes every pending request due at `now` and returns them, each with its `mutation_expire` record.
    pub fn expire_due(&self, now: u64, meta: &Attributes) -> Result<Vec<PendingRequest>, ApprovalError> {
        crate::obs::approval("expire", || self.expire_due_inner(now, meta))
    }

    fn expire_due_inner(&self, now: u64, meta: &Attributes) -> Result<Vec<PendingRequest>, ApprovalError> {
        let mut expired = Vec::new();
        for request in self.store.list()? {
            if request.is_pending() && request.is_due(now) {
                expired.push(self.expire(request, now, meta)?);
            }
        }
        Ok(expired)
    }

    fn expire(
        &self,
        mut request: PendingRequest,
        now: u64,
        meta: &Attributes,
    ) -> Result<PendingRequest, ApprovalError> {
        let rule = self.policy.rule_for(&request.mutation);
        #[cfg(feature = "audit")]
        if let Some(auditor) = self.governance.auditor() {
            let system = Subject::of_kind(SYSTEM_KIND, SYSTEM_ID);
            auditor
                .pending_event(
                    &system,
                    AuditAction::MutationExpire,
                    pending_ref(&request, None),
                    AuditOutcome::Applied,
                    &[],
                    meta,
                    rule.map(|r| r.permission.as_str()),
                    self.call_mode,
                    now,
                )
                .map_err(|e| ApprovalError::refused("audit", e.to_string()))?;
        }
        #[cfg(not(feature = "audit"))]
        let _ = (meta, rule, SYSTEM_KIND, SYSTEM_ID);
        request.status = PendingStatus::Expired { at: now };
        self.store.update(request.clone())?;
        Ok(request)
    }

    /// The pending request `id` and the rule that governs it. A request that is due is closed first.
    fn open_for_decision(
        &self,
        review: &Review<'_>,
        id: &str,
    ) -> Result<(PendingRequest, ApprovalRule), ApprovalError> {
        let request = self
            .store
            .get(id)?
            .ok_or_else(|| PendingError::Unknown(id.to_string()))?;
        if !request.is_pending() {
            return Err(PendingError::NotPending(id.to_string()).into());
        }
        if request.is_due(review.now) {
            self.expire(request, review.now, review.meta)?;
            return Err(ApprovalError::Expired(id.to_string()));
        }
        let rule = self.policy.rule_for(&request.mutation).cloned().ok_or_else(|| {
            ApprovalError::refused(
                "approval_policy",
                "a política não pede mais aprovação para esta mudança",
            )
        })?;
        Ok((request, rule))
    }

    fn approver_violations(
        &self,
        review: &Review<'_>,
        approver: &Subject,
        request: &PendingRequest,
        rule: &ApprovalRule,
    ) -> Vec<Violation> {
        let mut violations = Vec::new();
        if approver.id.is_none() {
            violations.push(Violation::new(
                APPROVAL_PERMISSION,
                "quem decide o pedido precisa ser identificado",
            ));
        } else if same_subject(approver, &request.actor) {
            violations.push(Violation::new(
                SELF_APPROVAL,
                "quem propôs a mudança não decide o próprio pedido",
            ));
        }
        if self.distinct_roles {
            let roles = review.engine.roles();
            let held =
                |who: &Subject| -> BTreeSet<String> { roles.expand(who.roles_at(&rule.resource, Some(review.now))) };
            if let Some(shared) = held(approver).intersection(&held(&request.actor)).next() {
                violations.push(Violation::new(
                    DISTINCT_ROLES,
                    format!("quem propôs a mudança e quem a aprova não podem atuar com o mesmo papel ({shared})"),
                ));
            }
        }
        let ctx = Context::new(&review.current).at(review.now);
        let ctx = match review.tuples {
            Some(tuples) => ctx.with_tuples(tuples),
            None => ctx,
        };
        if !review
            .engine
            .check(&ctx, &Request::new(approver, &rule.permission, &rule.resource))
        {
            violations.push(Violation::new(
                APPROVAL_PERMISSION,
                format!("falta a permissão {}", rule.permission),
            ));
        }
        violations
    }

    /// The invariants over the grants now, with no record and no side effect.
    fn judge(&self, review: &Review<'_>, actor: &Subject, mutation: &Mutation) -> Result<Vec<Grant>, Vec<Violation>> {
        self.governance.evaluate_at(
            review.engine,
            actor,
            review.current,
            mutation,
            review.tuples,
            review.now,
        )
    }

    fn apply_directly(
        &self,
        review: &Review<'_>,
        actor: &Subject,
        mutation: &Mutation,
    ) -> Result<Vec<Grant>, ApprovalError> {
        let at = self.governance.at(review.now);
        #[cfg(feature = "audit")]
        let at = match self.call_mode {
            Some(mode) => at.fail_mode(mode),
            None => at,
        };
        if let Some(options) = self.impact {
            return at
                .apply_with_impact(
                    review.engine,
                    actor,
                    review.current,
                    mutation,
                    review.tuples,
                    review.meta,
                    options,
                )
                .map(|applied| applied.grants)
                .map_err(ApprovalError::Refused);
        }
        at.apply_with(
            review.engine,
            actor,
            review.current,
            mutation,
            review.tuples,
            review.meta,
        )
        .map_err(ApprovalError::Refused)
    }

    /// Writes the record of one step (`violations` present means the step was refused). Does nothing
    /// without an auditor; with one, a record that cannot be written refuses the step.
    #[allow(clippy::too_many_arguments)]
    fn record(
        &self,
        review: &Review<'_>,
        actor: &Subject,
        step: Step,
        request: &PendingRequest,
        violations: Option<&[Violation]>,
        reason: Option<&str>,
        rule: &ApprovalRule,
    ) -> Result<(), ApprovalError> {
        #[cfg(feature = "audit")]
        if let Some(auditor) = self.governance.auditor() {
            let (action, ok) = match step {
                Step::Propose => (AuditAction::MutationPropose, AuditOutcome::Pending),
                Step::Approve => (AuditAction::MutationApprove, AuditOutcome::Applied),
                Step::Reject => (AuditAction::MutationReject, AuditOutcome::Applied),
                Step::ReviewClose => (AuditAction::ElevationReview, AuditOutcome::Applied),
            };
            let outcome = if violations.is_some() {
                AuditOutcome::Rejected
            } else {
                ok
            };
            auditor
                .pending_event(
                    actor,
                    action,
                    pending_ref(request, reason),
                    outcome,
                    violations.unwrap_or(&[]),
                    review.meta,
                    Some(&rule.permission),
                    self.call_mode,
                    review.now,
                )
                .map_err(|e| ApprovalError::refused("audit", e.to_string()))?;
        }
        #[cfg(not(feature = "audit"))]
        let _ = (review, actor, step, request, violations, reason, rule);
        Ok(())
    }
}

#[derive(Clone, Copy)]
#[non_exhaustive]
enum Step {
    Propose,
    Approve,
    Reject,
    ReviewClose,
}

fn subject_label(subject: &Subject) -> String {
    subject.id.clone().unwrap_or_else(|| subject.kind.clone())
}

#[cfg(feature = "audit")]
fn pending_ref(request: &PendingRequest, reason: Option<&str>) -> PendingRef {
    PendingRef {
        id: request.id.clone(),
        author: request.author(),
        kind: request.mutation.kind(),
        target: request.target(),
        expires_at: request.expires_at,
        reason: reason.map(str::to_string),
    }
}