feat: snapshot Linux-first RMM MVP and pause handover
Rust development slice / linux (push) Failing after 1m10s

This commit is contained in:
BuTzZ
2026-11-12 22:06:25 +01:00
commit 6c3203d725
194 changed files with 49234 additions and 0 deletions
+199
View File
@@ -0,0 +1,199 @@
#[path = "../src/certificate_rotation.rs"]
mod certificate_rotation;
// Rotation uses the HTTPS implementation too. Run its transport contract in this
// harness rather than compiling an untested/unused copy of the channel module.
#[path = "https_channel.rs"]
mod transport_tests;
use certificate_rotation::*;
use common::*;
use std::{
fs,
os::unix::fs::DirBuilderExt,
os::unix::fs::PermissionsExt,
path::PathBuf,
sync::{Arc, Mutex},
};
use transport_tests::{common, https_channel};
use uuid::Uuid;
struct Temp(PathBuf);
impl Temp {
fn new() -> Self {
let path =
PathBuf::from("/tmp/opencode").join(format!("agent-transport-{}", Uuid::new_v4()));
fs::DirBuilder::new().mode(0o700).create(&path).unwrap();
Self(path)
}
}
impl Drop for Temp {
fn drop(&mut self) {
fs::remove_dir_all(&self.0).unwrap();
}
}
fn csr_cert(pki: &Pki, csr: &str, cn: &str) -> Vec<u8> {
let csr = openssl::x509::X509Req::from_pem(csr.as_bytes()).unwrap();
let now = chrono::Utc::now().timestamp();
pki.issue(
&csr.public_key().unwrap(),
cn,
false,
now - 60,
now + 30 * 86400,
)
.to_pem()
.unwrap()
}
fn initialized(pki: &Pki, temp: &Temp, agent: Uuid, now: i64) -> RotationStore {
let store = RotationStore::open(&temp.0, &pki.ca.to_pem().unwrap(), agent, 1).unwrap();
let key = key();
let cert = pki.issue(
&key,
&agent.to_string(),
false,
now - 21 * 86400,
now + 9 * 86400,
);
store
.initialize(
&cert.to_pem().unwrap(),
&key.private_key_to_pem_pkcs8().unwrap(),
now,
)
.unwrap();
store
}
#[tokio::test]
async fn lost_response_reuses_csr_and_atomically_installs_matching_key() {
let pki = Pki::new();
let temp = Temp::new();
let agent = Uuid::new_v4();
let now = chrono::Utc::now().timestamp();
let store = initialized(&pki, &temp, agent, now);
assert!(store.due(RotationPolicy::default(), now).unwrap());
let original = store.current(now).unwrap();
let cached = Arc::new(Mutex::new(None::<(String, Vec<u8>)>));
let cache = cached.clone();
let issuer = Pki {
ca: pki.ca.clone(),
ca_key: pki.ca_key.clone(),
};
let server = Server::start(&pki, "rmm-dev.internal", move |r| {
assert_eq!(r.path, "/agent/v1/certificates/renew");
assert_eq!(r.content_type, "application/json");
let json: serde_json::Value = serde_json::from_slice(&r.body).unwrap();
let csr = json["csr_pem"].as_str().unwrap();
let mut cache = cache.lock().unwrap();
if let Some((old, cert)) = cache.as_ref() {
assert_eq!(old, csr);
return Some(Response { status: 200, media: "application/json", body: serde_json::to_vec(&serde_json::json!({"agent_id":agent,"certificate_pem":String::from_utf8(cert.clone()).unwrap(),"overlap_seconds":300})).unwrap() });
}
*cache = Some((csr.into(), csr_cert(&issuer, csr, &agent.to_string())));
None
});
assert!(store.renew(server.address, now).await.is_err());
assert_eq!(
store.current(now).unwrap().selector.version,
original.selector.version
);
// Reopen simulates process loss after pending key/CSR and lost response.
let reopened = RotationStore::open(&temp.0, &pki.ca.to_pem().unwrap(), agent, 1).unwrap();
let result = reopened.renew(server.address, now).await.unwrap();
assert!(result.needs_reconnect);
assert_eq!(result.enrollment_generation, 1);
let installed = reopened.current(now).unwrap();
assert_ne!(installed.selector.version, original.selector.version);
assert_eq!(
installed.selector.previous_version,
Some(original.selector.version)
);
assert!(installed.selector.previous_until.unwrap() >= now + 300);
assert_ne!(installed.private_key_pem, original.private_key_pem);
assert!(
temp.0
.join("versions")
.join(original.selector.version.to_string())
.join("device.key")
.exists()
);
assert!(!reopened.due(RotationPolicy::default(), now).unwrap());
for entry in fs::read_dir(temp.0.join("versions")).unwrap() {
let dir = entry.unwrap().path();
assert_eq!(
fs::metadata(&dir).unwrap().permissions().mode() & 0o777,
0o700
);
for file in fs::read_dir(dir).unwrap() {
assert_eq!(
file.unwrap().metadata().unwrap().permissions().mode() & 0o777,
0o600
);
}
}
}
#[test]
fn expired_unknown_issuer_changed_identity_key_mismatch_and_server_eku_block() {
let pki = Pki::new();
let agent = Uuid::new_v4();
let now = chrono::Utc::now().timestamp();
for mode in 0..5 {
let temp = Temp::new();
let store = RotationStore::open(&temp.0, &pki.ca.to_pem().unwrap(), agent, 1).unwrap();
let key = key();
let wrong_key = common::key();
let other = Pki::new();
let cert = match mode {
0 => pki.issue(&key, &agent.to_string(), false, now - 86400, now - 1),
1 => other.issue(&key, &agent.to_string(), false, now - 60, now + 86400),
2 => pki.issue(
&key,
&Uuid::new_v4().to_string(),
false,
now - 60,
now + 86400,
),
3 => pki.issue(&wrong_key, &agent.to_string(), false, now - 60, now + 86400),
_ => pki.issue(&key, &agent.to_string(), true, now - 60, now + 86400),
};
assert!(
store
.initialize(
&cert.to_pem().unwrap(),
&key.private_key_to_pem_pkcs8().unwrap(),
now
)
.is_err()
);
assert!(!temp.0.join("current.json").exists());
}
}
#[tokio::test]
async fn orphan_stages_do_not_change_selector_and_expiry_never_reenrolls() {
let pki = Pki::new();
let temp = Temp::new();
let agent = Uuid::new_v4();
let now = chrono::Utc::now().timestamp();
let store = initialized(&pki, &temp, agent, now);
let original = store.current(now).unwrap().selector.version;
let orphan = temp.0.join("versions").join(Uuid::new_v4().to_string());
fs::create_dir(&orphan).unwrap();
fs::set_permissions(&orphan, fs::Permissions::from_mode(0o700)).unwrap();
fs::write(orphan.join("device.key"), b"incomplete crash stage").unwrap();
assert_eq!(store.current(now).unwrap().selector.version, original);
assert!(
store
.renew("127.0.0.1:1".parse().unwrap(), now + 10 * 86400)
.await
.is_err()
);
assert!(!temp.0.join("pending.json").exists());
let changed =
RotationStore::open(&temp.0, &pki.ca.to_pem().unwrap(), Uuid::new_v4(), 1).unwrap();
assert!(changed.current(now).is_err());
let generation = RotationStore::open(&temp.0, &pki.ca.to_pem().unwrap(), agent, 2).unwrap();
assert!(generation.current(now).is_err());
}
+238
View File
@@ -0,0 +1,238 @@
#[path = "../../../tests/agent_transport/common.rs"]
pub(crate) mod common;
#[path = "../src/https_channel.rs"]
pub(crate) mod https_channel;
use common::*;
use https_channel::*;
use prost::Message;
use rmm_protocol::*;
use std::sync::{Arc, Mutex};
fn envelope(payload: Payload, generation: u64) -> Envelope {
Envelope {
protocol_major: 1,
protocol_minor: 0,
agent_id: "agent".into(),
enrollment_generation: 1,
connection_generation: generation,
boot_id: uuid::Uuid::new_v4().to_string(),
message_id: uuid::Uuid::new_v4().to_string(),
sequence: 1,
observed_at: chrono::Utc::now().timestamp_millis(),
payload: Some(payload),
}
}
fn hello() -> Envelope {
envelope(Payload::ClientHello(ClientHello::default()), 0)
}
fn heartbeat(generation: u64) -> Envelope {
envelope(Payload::Heartbeat(Heartbeat::default()), generation)
}
fn reply(e: &Envelope, payload: Payload) -> Envelope {
Envelope {
connection_generation: 7,
payload: Some(payload),
..e.clone()
}
}
fn batch(messages: Vec<Envelope>) -> Response {
Response::protobuf(
MessageBatch {
protocol_major: 1,
messages,
}
.encode_to_vec(),
)
}
fn client(pki: &Pki, address: std::net::SocketAddr) -> reqwest::Client {
let key = key();
let now = chrono::Utc::now().timestamp();
let cert = pki.issue(&key, "agent", false, now - 60, now + 86400);
mtls_client(
address,
&pki.ca.to_pem().unwrap(),
&cert.to_pem().unwrap(),
&key.private_key_to_pem_pkcs8().unwrap(),
)
.unwrap()
}
#[tokio::test]
async fn real_mtls_hello_poll_durable_replay_and_fencing() {
let pki = Pki::new();
let results = Arc::new(Mutex::new(Vec::new()));
let seen = results.clone();
let stored = Arc::new(Mutex::new(std::collections::HashMap::new()));
let durable = stored.clone();
let server = Server::start(&pki, "rmm-dev.internal", move |r| {
assert_eq!(r.content_type, "application/x-protobuf");
if r.path.ends_with("/poll") {
let poll = PollRequest::decode(r.body.as_slice()).unwrap();
assert_eq!(poll.wait_seconds, 25);
let e = poll.envelope.unwrap();
return Some(batch(vec![reply(
&e,
Payload::Ack(Ack {
message_id: e.message_id.clone(),
}),
)]));
}
let b = MessageBatch::decode(r.body.as_slice()).unwrap();
let e = &b.messages[0];
if matches!(e.payload, Some(Payload::ClientHello(_))) {
return Some(batch(vec![reply(
e,
Payload::ServerHello(ServerHello {
connection_generation: 7,
max_message_bytes: MAX_BODY as u32,
}),
)]));
}
if matches!(e.payload, Some(Payload::JobResult(_))) {
let mut durable = durable.lock().unwrap();
let bytes = e.encode_to_vec();
if let Some(previous) = durable.get(&e.message_id) {
assert_eq!(previous, &bytes);
} else {
durable.insert(e.message_id.clone(), bytes);
}
seen.lock().unwrap().push(r.body.clone());
if seen.lock().unwrap().len() == 1 {
return None;
} // committed response lost
}
Some(batch(vec![reply(
e,
Payload::Ack(Ack {
message_id: e.message_id.clone(),
}),
)]))
});
let mut channel = HttpsChannel::connect(client(&pki, server.address), server.address, hello())
.await
.unwrap();
assert_eq!(channel.generation(), 7);
assert!(matches!(
channel.receive().unwrap().payload,
Some(Payload::ServerHello(_))
));
let result = envelope(Payload::JobResult(JobResult::default()), 7);
assert!(channel.send(&result).await.is_err());
assert!(channel.receive().is_none()); // no fabricated ACK on HTTP/IO failure
channel.send(&result).await.unwrap();
assert_eq!(
channel.receive().unwrap().payload,
Some(Payload::Ack(Ack {
message_id: result.message_id.clone()
}))
);
assert_eq!(
results.lock().unwrap().as_slice(),
&[
MessageBatch {
protocol_major: 1,
messages: vec![result.clone()]
}
.encode_to_vec(),
MessageBatch {
protocol_major: 1,
messages: vec![result.clone()]
}
.encode_to_vec()
]
);
channel.poll(heartbeat(7), 25).await.unwrap();
channel.receive().unwrap();
assert!(channel.poll(heartbeat(7), 26).await.is_err());
assert!(channel.send(&heartbeat(6)).await.is_err());
channel.fence();
assert!(channel.poll(heartbeat(7), 0).await.is_err());
channel.send(&result).await.unwrap(); // late durable evidence still reliable
assert_eq!(stored.lock().unwrap().len(), 1); // one durable application record
}
#[tokio::test]
async fn bad_dns_bad_ca_and_missing_client_identity_are_rejected() {
let pki = Pki::new();
let wrong_dns = Server::start(&pki, "wrong.internal", |_| {
panic!("bad TLS reached application")
});
assert!(
HttpsChannel::connect(client(&pki, wrong_dns.address), wrong_dns.address, hello())
.await
.is_err()
);
let good = Server::start(&pki, "rmm-dev.internal", |_| {
panic!("bad TLS reached application")
});
assert!(
HttpsChannel::connect(client(&Pki::new(), good.address), good.address, hello())
.await
.is_err()
);
let without_identity = reqwest::Client::builder()
.https_only(true)
.no_proxy()
.tls_built_in_root_certs(false)
.add_root_certificate(reqwest::Certificate::from_pem(&pki.ca.to_pem().unwrap()).unwrap())
.resolve("rmm-dev.internal", good.address)
.build()
.unwrap();
assert!(
HttpsChannel::connect(without_identity, good.address, hello())
.await
.is_err()
);
}
#[tokio::test]
async fn malformed_content_limits_generation_and_redirect_fail_closed() {
let pki = Pki::new();
for mode in 0..5 {
let server = Server::start(&pki, "rmm-dev.internal", move |r| {
let b = MessageBatch::decode(r.body.as_slice()).unwrap();
let e = &b.messages[0];
let mut out = batch(vec![reply(
e,
Payload::ServerHello(ServerHello {
connection_generation: 7,
max_message_bytes: MAX_BODY as u32,
}),
)]);
match mode {
0 => out.media = "text/html",
1 => out.body = vec![0; MAX_BODY + 1],
2 => {
let mut e = reply(e, Payload::Ack(Ack::default()));
e.connection_generation = 6;
out = batch(vec![e]);
}
3 => out.status = 302,
_ => out.status = 409,
}
Some(out)
});
assert!(
HttpsChannel::connect(client(&pki, server.address), server.address, hello())
.await
.is_err()
);
}
}
#[test]
fn fallback_is_explicit_bounded_and_never_tls_bypass() {
assert!(allow_https_fallback(WssFailure::UpgradeBlocked, 2));
assert!(!allow_https_fallback(WssFailure::UpgradeBlocked, 4));
for reason in [
WssFailure::TlsOrIdentity,
WssFailure::Authentication,
WssFailure::Other,
] {
assert!(!allow_https_fallback(reason, 2));
}
for attempt in 0..100 {
assert!(retry_delay(attempt).as_secs() <= 60);
}
}
File diff suppressed because it is too large Load Diff