239 lines
7.8 KiB
Rust
239 lines
7.8 KiB
Rust
#[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);
|
|
}
|
|
}
|