feat: snapshot Linux-first RMM MVP and pause handover
Rust development slice / linux (push) Failing after 1m10s
Rust development slice / linux (push) Failing after 1m10s
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
use axum::{
|
||||
Extension, Json,
|
||||
body::Bytes,
|
||||
extract::State,
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use prost::Message;
|
||||
use rmm_protocol::*;
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
pub(crate) async fn identity(
|
||||
s: &crate::State,
|
||||
peer: crate::Peer,
|
||||
) -> Result<(Uuid, Uuid), StatusCode> {
|
||||
if !peer.1.is_some_and(|expires| expires > chrono::Utc::now()) {
|
||||
return Err(StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
let f = peer.0.ok_or(StatusCode::UNAUTHORIZED)?;
|
||||
crate::agent_service::binding(s, &f)
|
||||
.await
|
||||
.map_err(|_| StatusCode::UNAUTHORIZED)
|
||||
}
|
||||
fn content_type(h: &HeaderMap) -> Result<(), StatusCode> {
|
||||
if h.get("content-type").and_then(|v| v.to_str().ok()) != Some("application/x-protobuf") {
|
||||
return Err(StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn response(messages: Vec<Envelope>) -> Response {
|
||||
(
|
||||
[("content-type", "application/x-protobuf")],
|
||||
MessageBatch {
|
||||
protocol_major: 1,
|
||||
messages,
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
pub async fn messages(
|
||||
State(s): State<crate::State>,
|
||||
Extension(peer): Extension<crate::Peer>,
|
||||
h: HeaderMap,
|
||||
bytes: Bytes,
|
||||
) -> Result<Response, StatusCode> {
|
||||
let (t, a) = identity(&s, peer).await?;
|
||||
content_type(&h)?;
|
||||
let b = MessageBatch::decode(bytes).map_err(|_| StatusCode::BAD_REQUEST)?;
|
||||
if b.protocol_major != 1 || b.messages.is_empty() || b.messages.len() > 16 {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
if b.messages
|
||||
.iter()
|
||||
.any(|e| matches!(e.payload, Some(Payload::ClientHello(_))))
|
||||
&& b.messages.len() != 1
|
||||
{
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
let mut replies = Vec::new();
|
||||
if b.messages.len() > 1
|
||||
&& b.messages.iter().any(|e| {
|
||||
!matches!(
|
||||
e.payload,
|
||||
Some(
|
||||
Payload::InventorySnapshot(_)
|
||||
| Payload::JobAccepted(_)
|
||||
| Payload::JobStateChanged(_)
|
||||
| Payload::JobResult(_)
|
||||
)
|
||||
)
|
||||
})
|
||||
{
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
let last = b.messages.last().cloned().ok_or(StatusCode::BAD_REQUEST)?;
|
||||
let mut generation = 0;
|
||||
for e in b.messages {
|
||||
let reply = if matches!(e.payload, Some(Payload::ClientHello(_))) {
|
||||
crate::agent_service::open(&s, t, a, &e, "https").await
|
||||
} else {
|
||||
crate::agent_service::ingest(&s, t, a, &e, "https").await
|
||||
}
|
||||
.map_err(|_| StatusCode::CONFLICT)?;
|
||||
generation = reply.connection_generation;
|
||||
replies.push(reply);
|
||||
}
|
||||
if let Some(work) = crate::job_gateway::next(&s, t, a, generation)
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?
|
||||
{
|
||||
replies.push(crate::agent_service::reply(&last, work, generation));
|
||||
}
|
||||
Ok(response(replies))
|
||||
}
|
||||
pub async fn poll(
|
||||
State(s): State<crate::State>,
|
||||
Extension(peer): Extension<crate::Peer>,
|
||||
h: HeaderMap,
|
||||
bytes: Bytes,
|
||||
) -> Result<Response, StatusCode> {
|
||||
let (t, a) = identity(&s, peer).await?;
|
||||
content_type(&h)?;
|
||||
let p = PollRequest::decode(bytes).map_err(|_| StatusCode::BAD_REQUEST)?;
|
||||
if p.wait_seconds > 25 {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
let e = p.envelope.ok_or(StatusCode::BAD_REQUEST)?;
|
||||
if !matches!(e.payload, Some(Payload::Heartbeat(_))) {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
let ack = crate::agent_service::ingest(&s, t, a, &e, "https")
|
||||
.await
|
||||
.map_err(|_| StatusCode::CONFLICT)?;
|
||||
let deadline =
|
||||
tokio::time::Instant::now() + std::time::Duration::from_secs(u64::from(p.wait_seconds));
|
||||
loop {
|
||||
if let Some(work) = crate::job_gateway::next(&s, t, a, e.connection_generation)
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?
|
||||
{
|
||||
return Ok(response(vec![
|
||||
ack,
|
||||
crate::agent_service::reply(&e, work, e.connection_generation),
|
||||
]));
|
||||
}
|
||||
if tokio::time::Instant::now() >= deadline {
|
||||
return Ok(response(vec![ack]));
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
|
||||
}
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Renewal {
|
||||
csr_pem: String,
|
||||
}
|
||||
pub async fn renew(
|
||||
State(s): State<crate::State>,
|
||||
Extension(peer): Extension<crate::Peer>,
|
||||
Json(input): Json<Renewal>,
|
||||
) -> Result<Json<serde_json::Value>, StatusCode> {
|
||||
if input.csr_pem.len() > 8192 {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
let fingerprint = peer.0.clone().ok_or(StatusCode::UNAUTHORIZED)?;
|
||||
let (t, a) = identity(&s, peer).await?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, t)
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
|
||||
let row=sqlx::query("SELECT certificate_sha256,renewal_csr_sha256,certificate_pem FROM agent_installations WHERE agent_id=$1 AND revoked_at IS NULL FOR UPDATE").bind(a).fetch_optional(&mut *tx).await.map_err(|_|StatusCode::SERVICE_UNAVAILABLE)?.ok_or(StatusCode::UNAUTHORIZED)?;
|
||||
use sqlx::Row;
|
||||
let csr_hash = rmm_domain::jobs::hash(input.csr_pem.as_bytes());
|
||||
if row
|
||||
.get::<Option<String>, _>("renewal_csr_sha256")
|
||||
.as_deref()
|
||||
== Some(&csr_hash)
|
||||
{
|
||||
return Ok(Json(
|
||||
serde_json::json!({"agent_id":a,"certificate_pem":row.get::<Option<String>,_>("certificate_pem"),"overlap_seconds":300}),
|
||||
));
|
||||
}
|
||||
if row.get::<String, _>("certificate_sha256") != fingerprint {
|
||||
return Err(StatusCode::CONFLICT);
|
||||
}
|
||||
let certificate =
|
||||
crate::tls::issue(&s, &input.csr_pem, a).map_err(|_| StatusCode::BAD_REQUEST)?;
|
||||
let hash = rmm_domain::jobs::hash(
|
||||
&certificate
|
||||
.to_der()
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
|
||||
);
|
||||
let pem = String::from_utf8(
|
||||
certificate
|
||||
.to_pem()
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
|
||||
)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
sqlx::query("UPDATE agent_installations SET previous_certificate_sha256=certificate_sha256,previous_certificate_until=now()+interval '5 minutes',certificate_sha256=$2,certificate_pem=$3,renewal_csr_sha256=$4,connection_generation=connection_generation+1 WHERE agent_id=$1").bind(a).bind(hash).bind(&pem).bind(csr_hash).execute(&mut *tx).await.map_err(|_|StatusCode::SERVICE_UNAVAILABLE)?;
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
|
||||
Ok(Json(
|
||||
serde_json::json!({"agent_id":a,"certificate_pem":pem,"overlap_seconds":300}),
|
||||
))
|
||||
}
|
||||
Reference in New Issue
Block a user