Files
rust-discovery/crates/protocol/src/lib.rs
T
BuTzZ 6c3203d725
Rust development slice / linux (push) Failing after 1m10s
feat: snapshot Linux-first RMM MVP and pause handover
2026-11-12 22:06:25 +01:00

182 lines
5.3 KiB
Rust

//! Wire types mirror proto/agent.proto. Golden-wire tests protect stable tags.
use prost::Message;
use serde::{Deserialize, Serialize};
mod jobs;
pub use jobs::*;
#[derive(Clone, PartialEq, Message)]
pub struct Envelope {
#[prost(uint32, tag = "1")]
pub protocol_major: u32,
#[prost(uint32, tag = "2")]
pub protocol_minor: u32,
#[prost(string, tag = "3")]
pub message_id: String,
#[prost(string, tag = "4")]
pub agent_id: String,
#[prost(uint64, tag = "5")]
pub enrollment_generation: u64,
#[prost(uint64, tag = "6")]
pub connection_generation: u64,
#[prost(string, tag = "7")]
pub boot_id: String,
#[prost(uint64, tag = "8")]
pub sequence: u64,
#[prost(int64, tag = "9")]
pub observed_at: i64,
#[prost(
oneof = "Payload",
tags = "10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 25, 26, 27, 28"
)]
pub payload: Option<Payload>,
}
#[derive(Clone, PartialEq, prost::Oneof)]
pub enum Payload {
#[prost(message, tag = "10")]
ClientHello(ClientHello),
#[prost(message, tag = "11")]
ServerHello(ServerHello),
#[prost(message, tag = "12")]
Heartbeat(Heartbeat),
#[prost(message, tag = "13")]
InventorySnapshot(InventorySnapshot),
#[prost(message, tag = "14")]
Ack(Ack),
#[prost(message, tag = "20")]
JobOffer(JobOffer),
#[prost(message, tag = "21")]
JobAccepted(JobAccepted),
#[prost(message, tag = "22")]
ExecutionPermitRequest(ExecutionPermitRequest),
#[prost(message, tag = "23")]
ExecutionPermit(ExecutionPermit),
#[prost(message, tag = "24")]
JobStateChanged(JobStateChanged),
#[prost(message, tag = "25")]
JobResult(JobResult),
#[prost(message, tag = "26")]
CancelRequest(CancelRequest),
#[prost(message, tag = "27")]
SyncRequest(SyncRequest),
#[prost(message, tag = "28")]
SyncResponse(SyncResponse),
}
#[derive(Clone, PartialEq, Message)]
pub struct ClientHello {
#[prost(string, tag = "1")]
pub agent_version: String,
#[prost(string, repeated, tag = "2")]
pub capabilities: Vec<String>,
}
#[derive(Clone, PartialEq, Message)]
pub struct ServerHello {
#[prost(uint64, tag = "1")]
pub connection_generation: u64,
#[prost(uint32, tag = "2")]
pub max_message_bytes: u32,
}
#[derive(Clone, PartialEq, Message)]
pub struct Heartbeat {
#[prost(string, tag = "1")]
pub instance_id: String,
#[prost(uint64, tag = "2")]
pub uptime_seconds: u64,
}
#[derive(Clone, PartialEq, Message)]
pub struct InventorySnapshot {
#[prost(string, tag = "1")]
pub snapshot_id: String,
#[prost(string, tag = "2")]
pub scope: String,
#[prost(uint32, tag = "3")]
pub schema_version: u32,
#[prost(uint32, tag = "4")]
pub chunk_index: u32,
#[prost(uint32, tag = "5")]
pub chunk_count: u32,
#[prost(bytes = "vec", tag = "6")]
pub data: Vec<u8>,
#[prost(bytes = "vec", tag = "7")]
pub sha256: Vec<u8>,
#[prost(string, tag = "8")]
pub completeness: String,
}
#[derive(Clone, PartialEq, Message)]
pub struct Ack {
#[prost(string, tag = "1")]
pub message_id: String,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EnrollmentRequest {
pub token: String,
pub csr_pem: String,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EnrollmentResponse {
pub agent_id: String,
pub asset_id: String,
pub certificate_pem: String,
}
/// HTTP batch profile 1. Maximum 16 envelopes and 1 MiB encoded total.
#[derive(Clone, PartialEq, Message)]
pub struct MessageBatch {
#[prost(uint32, tag = "1")]
pub protocol_major: u32,
#[prost(message, repeated, tag = "2")]
pub messages: Vec<Envelope>,
}
/// Submit one fresh heartbeat/control envelope and wait up to 25 seconds.
#[derive(Clone, PartialEq, Message)]
pub struct PollRequest {
#[prost(message, optional, tag = "1")]
pub envelope: Option<Envelope>,
#[prost(uint32, tag = "2")]
pub wait_seconds: u32,
}
pub fn valid_envelope(e: &Envelope, agent: &str, generation: u64) -> bool {
e.protocol_major == 1
&& e.protocol_minor == 0
&& e.agent_id == agent
&& e.enrollment_generation == 1
&& e.connection_generation == generation
&& uuid::Uuid::parse_str(&e.message_id).is_ok()
&& uuid::Uuid::parse_str(&e.boot_id).is_ok()
&& e.sequence > 0
&& e.payload.is_some()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn golden_heartbeat_and_unknown_major() {
let e = Envelope {
protocol_major: 1,
payload: Some(Payload::Heartbeat(Heartbeat {
instance_id: "x".into(),
uptime_seconds: 1,
})),
..Default::default()
};
assert_eq!(e.encode_to_vec(), vec![8, 1, 98, 5, 10, 1, 120, 16, 1]);
assert_eq!(Envelope::decode(e.encode_to_vec().as_slice()).unwrap(), e);
assert!(!valid_envelope(&e, "", 0));
}
#[test]
fn http_batch_wire_golden() {
let batch = MessageBatch {
protocol_major: 1,
messages: vec![Envelope {
protocol_major: 1,
..Default::default()
}],
};
assert_eq!(batch.encode_to_vec(), vec![8, 1, 18, 2, 8, 1]);
assert_eq!(
MessageBatch::decode(batch.encode_to_vec().as_slice()).unwrap(),
batch
);
}
}