Skip to main content

subc_control/
connection_describe.rs

1//! Facts about the caller's current connection, not a claim supplied by the client.
2//!
3//! Recognition is a snapshot: revalidation can change whether a later call or
4//! route bind carries an operator app. This query grants nothing and never prompts.
5
6use serde::{Deserialize, Serialize};
7use subc_protocol::peer::OperatorApp;
8
9/// Ask the daemon what it knows about this connection.
10#[non_exhaustive]
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
12pub struct ConnectionDescribeRequest {
13    op: ConnectionDescribeOp,
14}
15
16impl ConnectionDescribeRequest {
17    pub fn new() -> Self {
18        Self {
19            op: ConnectionDescribeOp::Describe,
20        }
21    }
22}
23
24impl Default for ConnectionDescribeRequest {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30/// The daemon's current view of the caller's connection.
31#[non_exhaustive]
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
33pub struct ConnectionDescribeReply {
34    op: ConnectionDescribeOp,
35    /// True for a Unix socket or Windows named pipe, false for loopback TCP.
36    pub local: bool,
37    /// The app currently recognised for this connection, omitted when none is.
38    /// `route.bind` sends it only to targets that advertise `operator-app/v1`.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub operator_app: Option<OperatorApp>,
41    /// A development build that has not received an operator-app stamp.
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    pub dev_build: Option<ConnectionDevBuild>,
44}
45
46/// Diagnostic only: neither variant establishes operator-app identity.
47#[non_exhaustive]
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
49#[serde(tag = "status", rename_all = "snake_case")]
50pub enum ConnectionDevBuild {
51    Untrusted {
52        cdhash: String,
53    },
54    Ineligible {
55        reason: String,
56    },
57    Unavailable {
58        reason: String,
59    },
60    #[serde(other)]
61    Unknown,
62}
63
64impl ConnectionDescribeReply {
65    pub fn new(local: bool, operator_app: Option<OperatorApp>) -> Self {
66        Self {
67            op: ConnectionDescribeOp::Describe,
68            local,
69            operator_app,
70            dev_build: None,
71        }
72    }
73}
74
75// A private, required tag prevents accepting a reply for a different operation.
76#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
77enum ConnectionDescribeOp {
78    #[serde(rename = "connection.describe")]
79    Describe,
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use serde_json::json;
86
87    #[test]
88    fn connection_describe_bodies_require_the_op() {
89        for op in [None, Some("app.open")] {
90            let mut request = json!({});
91            let mut reply = json!({"local": true});
92            if let Some(op) = op {
93                request["op"] = json!(op);
94                reply["op"] = json!(op);
95            }
96            assert!(serde_json::from_value::<ConnectionDescribeRequest>(request).is_err());
97            assert!(serde_json::from_value::<ConnectionDescribeReply>(reply).is_err());
98        }
99    }
100
101    #[test]
102    fn connection_describe_wire_shapes() {
103        assert_eq!(
104            serde_json::to_value(ConnectionDescribeRequest::new()).unwrap(),
105            json!({"op": "connection.describe"})
106        );
107        assert_eq!(
108            serde_json::to_value(ConnectionDescribeReply::new(false, None)).unwrap(),
109            json!({"op": "connection.describe", "local": false})
110        );
111        let reply = ConnectionDescribeReply::new(true, Some(OperatorApp::new("fixture", 42)));
112        let wire = json!({"op": "connection.describe", "local": true, "operator_app": {"app": "fixture", "pid": 42}});
113        assert_eq!(serde_json::to_value(&reply).unwrap(), wire);
114        assert_eq!(
115            serde_json::from_value::<ConnectionDescribeReply>(wire).unwrap(),
116            reply
117        );
118    }
119
120    #[test]
121    fn connection_describe_reports_dev_build_without_changing_stamp_shape() {
122        let wire = json!({"op":"connection.describe", "local":true, "dev_build":{"status":"untrusted", "cdhash":"abc"}, "future_field":42});
123        let reply: ConnectionDescribeReply = serde_json::from_value(wire).unwrap();
124        assert_eq!(
125            reply.dev_build,
126            Some(ConnectionDevBuild::Untrusted {
127                cdhash: "abc".into()
128            })
129        );
130        assert_eq!(reply.operator_app, None);
131        assert_eq!(
132            serde_json::to_value(reply).unwrap(),
133            json!({"op":"connection.describe", "local":true, "dev_build":{"status":"untrusted", "cdhash":"abc"}})
134        );
135        let ineligible = ConnectionDevBuild::Ineligible {
136            reason: "Hardened runtime is required.".into(),
137        };
138        assert_eq!(
139            serde_json::to_value(ineligible).unwrap(),
140            json!({"status":"ineligible", "reason":"Hardened runtime is required."})
141        );
142        let unavailable = ConnectionDevBuild::Unavailable {
143            reason: "The keychain could not be read.".into(),
144        };
145        assert_eq!(
146            serde_json::to_value(unavailable).unwrap(),
147            json!({"status":"unavailable", "reason":"The keychain could not be read."})
148        );
149    }
150}