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}
42
43impl ConnectionDescribeReply {
44    pub fn new(local: bool, operator_app: Option<OperatorApp>) -> Self {
45        Self {
46            op: ConnectionDescribeOp::Describe,
47            local,
48            operator_app,
49        }
50    }
51}
52
53// A private, required tag prevents accepting a reply for a different operation.
54#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
55enum ConnectionDescribeOp {
56    #[serde(rename = "connection.describe")]
57    Describe,
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use serde_json::json;
64
65    #[test]
66    fn connection_describe_bodies_require_the_op() {
67        for op in [None, Some("app.open")] {
68            let mut request = json!({});
69            let mut reply = json!({"local": true});
70            if let Some(op) = op {
71                request["op"] = json!(op);
72                reply["op"] = json!(op);
73            }
74            assert!(serde_json::from_value::<ConnectionDescribeRequest>(request).is_err());
75            assert!(serde_json::from_value::<ConnectionDescribeReply>(reply).is_err());
76        }
77    }
78
79    #[test]
80    fn connection_describe_wire_shapes() {
81        assert_eq!(
82            serde_json::to_value(ConnectionDescribeRequest::new()).unwrap(),
83            json!({"op": "connection.describe"})
84        );
85        assert_eq!(
86            serde_json::to_value(ConnectionDescribeReply::new(false, None)).unwrap(),
87            json!({"op": "connection.describe", "local": false})
88        );
89        let reply = ConnectionDescribeReply::new(true, Some(OperatorApp::new("fixture", 42)));
90        let wire = json!({"op": "connection.describe", "local": true, "operator_app": {"app": "fixture", "pid": 42}});
91        assert_eq!(serde_json::to_value(&reply).unwrap(), wire);
92        assert_eq!(
93            serde_json::from_value::<ConnectionDescribeReply>(wire).unwrap(),
94            reply
95        );
96    }
97}