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
|
use super::ClientEndpointId;
pub(crate) struct DecodedAgentViewProjection {
pub(crate) boot_id: String,
pub(crate) revision: u64,
pub(crate) view: Result<Option<crate::api::schema::AgentViewSetParams>, ()>,
}
pub(crate) enum EndpointControlMessage {
HealthPong,
AgentViewProjection(DecodedAgentViewProjection),
Snapshot(Box<crate::protocol::ClientShellSnapshot>),
Ignored,
}
pub(crate) fn decode_endpoint_control(
kind: &str,
data: &str,
) -> Result<EndpointControlMessage, String> {
if kind == crate::protocol::endpoint::HEALTH_PONG_KIND {
return Ok(EndpointControlMessage::HealthPong);
}
if kind == crate::protocol::endpoint::AGENT_VIEW_PROJECTION_KIND {
let Ok(projection): Result<crate::protocol::endpoint::EndpointAgentViewProjection, _> =
serde_json::from_str(data)
else {
return Ok(EndpointControlMessage::Ignored);
};
let view = match projection.view {
Some(value) => serde_json::from_value(value)
.map(Some)
.map_err(|_| ())
.and_then(|mut view| {
view.as_mut()
.map(crate::app::agent_view::validate_agent_view)
.transpose()
.map(|_| view)
.map_err(|_| ())
}),
None => Ok(None),
};
return Ok(EndpointControlMessage::AgentViewProjection(
DecodedAgentViewProjection {
boot_id: projection.boot_id,
revision: projection.revision,
view,
},
));
}
if kind == crate::protocol::endpoint::ENDPOINT_SNAPSHOT_KIND {
let snapshot = serde_json::from_str(data)
.map_err(|error| format!("invalid endpoint snapshot: {error}"))?;
return Ok(EndpointControlMessage::Snapshot(Box::new(snapshot)));
}
if kind.starts_with("shell.snapshot.") {
return Err(format!(
"unsupported mandatory endpoint snapshot codec {kind:?}"
));
}
Ok(EndpointControlMessage::Ignored)
}
pub(crate) fn protocol_failure_is_fatal(endpoint_id: &ClientEndpointId) -> bool {
endpoint_id.is_local()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::endpoint::ProfileId;
#[test]
fn unknown_optional_controls_are_ignored() {
assert!(matches!(
decode_endpoint_control("future.optional", "not json").unwrap(),
EndpointControlMessage::Ignored
));
}
#[test]
fn agent_view_projection_decodes_and_validates_the_view() {
let view = crate::api::schema::AgentViewSetParams {
source: "example.views".into(),
label: Some("focus".into()),
filter: None,
sort: Vec::new(),
};
let crate::protocol::ServerMessage::EndpointControl { kind, data } =
crate::protocol::endpoint::agent_view_projection_message("boot", 4, Some(&view))
.unwrap()
else {
panic!("projection control");
};
let EndpointControlMessage::AgentViewProjection(decoded) =
decode_endpoint_control(&kind, &data).unwrap()
else {
panic!("decoded projection");
};
assert_eq!(decoded.boot_id, "boot");
assert_eq!(decoded.revision, 4);
assert_eq!(decoded.view, Ok(Some(view)));
}
#[test]
fn unsupported_agent_view_payload_falls_back_without_rejecting_endpoint() {
let projection = crate::protocol::endpoint::EndpointAgentViewProjection {
boot_id: "boot".into(),
revision: 5,
view: Some(serde_json::json!({
"source": "example.views",
"filter": {"op": "future_filter"}
})),
};
let decoded = decode_endpoint_control(
crate::protocol::endpoint::AGENT_VIEW_PROJECTION_KIND,
&serde_json::to_string(&projection).unwrap(),
)
.unwrap();
let EndpointControlMessage::AgentViewProjection(decoded) = decoded else {
panic!("decoded projection");
};
assert_eq!(decoded.view, Err(()));
}
#[test]
fn malformed_agent_view_envelope_is_ignored() {
assert!(matches!(
decode_endpoint_control(
crate::protocol::endpoint::AGENT_VIEW_PROJECTION_KIND,
"not json"
)
.unwrap(),
EndpointControlMessage::Ignored
));
}
#[test]
fn unknown_snapshot_codecs_are_rejected() {
assert_eq!(
decode_endpoint_control("shell.snapshot.v2", "{}")
.err()
.as_deref(),
Some("unsupported mandatory endpoint snapshot codec \"shell.snapshot.v2\"")
);
}
#[test]
fn only_local_protocol_failures_end_the_client() {
let remote =
ClientEndpointId::Ssh(ProfileId::parse("0123456789abcdef0123456789abcdef").unwrap());
assert!(protocol_failure_is_fatal(&ClientEndpointId::Local));
assert!(!protocol_failure_is_fatal(&remote));
}
}
|