Skip to main content

qualia_vision/biosense/policy/
policy_permit_check.rs

1//! Local federated policy permit-check (D4.04).
2//!
3//! The honest LOCAL answer to a federated question — "is this camera's biometric
4//! use permitted?" — by composing the existing fail-closed policy engine
5//! [`evaluate_processing_act()`]. It does NOT reimplement the consent/purpose matrix.
6//!
7//! FEDERATION NOTE: the transport that carries such a question between peers (a
8//! signed FED ask/answer over the mixnet) is future work. This function is the
9//! purely local determination a node makes for a camera it controls; a federated
10//! wrapper would attach identity/provenance around this same answer. Until that
11//! exists, the answer is honest but local-only — it speaks for THIS node's policy,
12//! not a network-wide grant.
13
14use crate::biosense::consent::BiosenseConsent;
15use crate::biosense::policy::{evaluate_processing_act, PolicyDecision, ProcessingAct};
16
17/// Answer to a "may this camera perform `act`?" check: the decision, a static
18/// human-readable reason, and the camera it pertains to.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct PermitAnswer {
21    pub decision: PolicyDecision,
22    pub reason: &'static str,
23    pub camera_id: u64,
24}
25
26/// Fail-closed permit check for a camera + processing act under a consent record.
27///
28/// Composes [`evaluate_processing_act()`] and attaches a clear static reason per
29/// outcome. No consent → `Forbid`. Never panics; no `unwrap`.
30pub fn policy_permit_check(
31    camera_id: u64,
32    act: ProcessingAct,
33    consent: BiosenseConsent,
34) -> PermitAnswer {
35    let decision = evaluate_processing_act(consent, act);
36    let reason = reason_for(act, decision, consent.allow_process);
37    PermitAnswer {
38        decision,
39        reason,
40        camera_id,
41    }
42}
43
44/// Map (act, decision) to an honest, non-empty static reason string.
45fn reason_for(act: ProcessingAct, decision: PolicyDecision, allow_process: bool) -> &'static str {
46    match decision {
47        PolicyDecision::Permit => match act {
48            ProcessingAct::MotionOnly => "motion-only processing permitted",
49            ProcessingAct::FaceDetect => {
50                "face detection permitted under surveillance-policy purpose"
51            }
52            ProcessingAct::FaceEmbed | ProcessingAct::Identify1N => {
53                "identification permitted: purpose-bound security grant with template consent"
54            }
55            ProcessingAct::Rppg => "rPPG permitted for self-monitoring / research purpose",
56            ProcessingAct::Affect => {
57                "affect estimation permitted for self-monitoring / research purpose"
58            }
59            ProcessingAct::RecordStore => {
60                "record storage permitted by template or graph-observation consent"
61            }
62        },
63        PolicyDecision::Forbid => {
64            if !allow_process {
65                return "fail-closed: no processing consent for this camera";
66            }
67            match act {
68                ProcessingAct::FaceEmbed | ProcessingAct::Identify1N => {
69                    "identification requires a purpose-bound security grant"
70                }
71                ProcessingAct::FaceDetect => {
72                    "face detection requires a surveillance-policy purpose"
73                }
74                ProcessingAct::Rppg => "rPPG requires a self-monitoring or research purpose",
75                ProcessingAct::Affect => {
76                    "affect estimation requires a self-monitoring or research purpose"
77                }
78                ProcessingAct::RecordStore => {
79                    "record storage requires template or graph-observation consent"
80                }
81                // MotionOnly is always permitted; unreachable in the Forbid arm.
82                ProcessingAct::MotionOnly => {
83                    "motion-only processing not permitted for this consent"
84                }
85            }
86        }
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93    use crate::biosense::consent::BiosensePurpose;
94
95    #[test]
96    fn denied_consent_forbids_with_reason() {
97        let consent = BiosenseConsent::denied(BiosensePurpose::Security);
98        let a = policy_permit_check(0xCA51, ProcessingAct::FaceEmbed, consent);
99        assert_eq!(a.decision, PolicyDecision::Forbid);
100        assert_eq!(a.camera_id, 0xCA51);
101        assert_eq!(
102            a.reason,
103            "fail-closed: no processing consent for this camera"
104        );
105        assert!(!a.reason.is_empty());
106    }
107
108    #[test]
109    fn security_template_grant_permits_face_embed() {
110        let consent = BiosenseConsent::grant_security_template(0xABC);
111        let a = policy_permit_check(0xCA52, ProcessingAct::FaceEmbed, consent);
112        assert_eq!(a.decision, PolicyDecision::Permit);
113        assert_eq!(a.camera_id, 0xCA52);
114        assert!(!a.reason.is_empty());
115    }
116
117    #[test]
118    fn surveillance_face_embed_forbidden_needs_security_grant() {
119        let consent = BiosenseConsent::grant_process(BiosensePurpose::SurveillancePolicy, 1);
120        let a = policy_permit_check(0xCA53, ProcessingAct::FaceEmbed, consent);
121        assert_eq!(a.decision, PolicyDecision::Forbid);
122        assert_eq!(
123            a.reason,
124            "identification requires a purpose-bound security grant"
125        );
126    }
127
128    #[test]
129    fn motion_only_always_permitted() {
130        // Even a surveillance-policy (non-security) consent permits motion-only.
131        let consent = BiosenseConsent::grant_process(BiosensePurpose::SurveillancePolicy, 1);
132        let a = policy_permit_check(0xCA54, ProcessingAct::MotionOnly, consent);
133        assert_eq!(a.decision, PolicyDecision::Permit);
134        assert_eq!(a.reason, "motion-only processing permitted");
135        assert_eq!(a.camera_id, 0xCA54);
136    }
137
138    #[test]
139    fn every_answer_carries_camera_id_and_nonempty_reason() {
140        let grants = [
141            BiosenseConsent::denied(BiosensePurpose::Research),
142            BiosenseConsent::grant_process(BiosensePurpose::WellfairSelfMonitor, 2),
143            BiosenseConsent::grant_security_template(3),
144        ];
145        let acts = [
146            ProcessingAct::MotionOnly,
147            ProcessingAct::FaceDetect,
148            ProcessingAct::FaceEmbed,
149            ProcessingAct::Identify1N,
150            ProcessingAct::Rppg,
151            ProcessingAct::Affect,
152            ProcessingAct::RecordStore,
153        ];
154        for (i, &c) in grants.iter().enumerate() {
155            for &act in &acts {
156                let cam = 0x1000 + i as u64;
157                let a = policy_permit_check(cam, act, c);
158                assert_eq!(a.camera_id, cam);
159                assert!(!a.reason.is_empty(), "reason must be non-empty for {act:?}");
160            }
161        }
162    }
163}