Skip to main content

subc_control/
app_trust.rs

1//! Owner-local operations for trusting a specific signed desktop development build.
2
3use serde::{Deserialize, Serialize};
4
5/// A build the operator approved, pinned by its signing identifier and cdhash.
6#[non_exhaustive]
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8pub struct TrustedAppBuild {
9    pub identifier: String,
10    pub cdhash: String,
11    pub version: String,
12    pub build_time: Option<String>,
13    pub added_ms: u64,
14}
15
16impl TrustedAppBuild {
17    pub fn new(
18        identifier: String,
19        cdhash: String,
20        version: String,
21        build_time: Option<String>,
22        added_ms: u64,
23    ) -> Self {
24        Self {
25            identifier,
26            cdhash,
27            version,
28            build_time,
29            added_ms,
30        }
31    }
32}
33
34macro_rules! app_op {
35    ($tag:ident, $op:literal, $request:ident { $($r:ident: $rt:ty),* },
36     $reply:ident { $($s:ident: $st:ty),* }) => {
37        #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
38        enum $tag {
39            #[serde(rename = $op)]
40            Op,
41        }
42
43        #[non_exhaustive]
44        #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
45        pub struct $request {
46            op: $tag,
47            $(pub $r: $rt,)*
48        }
49
50        impl $request {
51            pub fn new($($r: $rt),*) -> Self {
52                Self { op: $tag::Op, $($r,)* }
53            }
54        }
55
56        #[non_exhaustive]
57        #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
58        pub struct $reply {
59            op: $tag,
60            $(pub $s: $st,)*
61        }
62
63        impl $reply {
64            pub fn new($($s: $st),*) -> Self {
65                Self { op: $tag::Op, $($s,)* }
66            }
67        }
68    };
69}
70
71app_op!(
72    TrustOp,
73    "app.trust",
74    AppTrustRequest { path: String },
75    AppTrustReply {
76        entry: TrustedAppBuild
77    }
78);
79app_op!(TrustedOp, "app.trusted", AppTrustedRequest {}, AppTrustedReply { entries: Vec<TrustedAppBuild> });
80app_op!(
81    UntrustOp,
82    "app.untrust",
83    AppUntrustRequest { cdhash: String },
84    AppUntrustReply { removed: bool }
85);
86
87impl Default for AppTrustedRequest {
88    fn default() -> Self {
89        Self::new()
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use serde_json::{json, to_value};
97
98    #[test]
99    fn app_trust_wire_shapes() {
100        assert_eq!(
101            to_value(AppTrustRequest::new("/Test.app".into())).unwrap(),
102            json!({"op":"app.trust", "path":"/Test.app"})
103        );
104        assert_eq!(
105            to_value(AppTrustedRequest::new()).unwrap(),
106            json!({"op":"app.trusted"})
107        );
108        assert_eq!(
109            to_value(AppUntrustRequest::new("abc".into())).unwrap(),
110            json!({"op":"app.untrust", "cdhash":"abc"})
111        );
112        let entry = TrustedAppBuild::new(
113            "dev.cortexkit.alfonso".into(),
114            "abc".into(),
115            "1.0".into(),
116            None,
117            12,
118        );
119        let body = json!({"identifier":"dev.cortexkit.alfonso", "cdhash":"abc", "version":"1.0", "build_time":null, "added_ms":12});
120        assert_eq!(
121            to_value(AppTrustReply::new(entry.clone())).unwrap(),
122            json!({"op":"app.trust", "entry":body})
123        );
124        assert_eq!(
125            to_value(AppTrustedReply::new(vec![entry])).unwrap(),
126            json!({"op":"app.trusted", "entries":[body]})
127        );
128        assert_eq!(
129            to_value(AppUntrustReply::new(true)).unwrap(),
130            json!({"op":"app.untrust", "removed":true})
131        );
132        assert!(serde_json::from_value::<AppTrustRequest>(
133            json!({"op":"app.untrust", "path":"/Test.app"})
134        )
135        .is_err());
136        assert!(serde_json::from_value::<AppTrustedRequest>(json!({})).is_err());
137    }
138}