874 lines
35 KiB
Rust
874 lines
35 KiB
Rust
//! Tenant release publication and update-only bounded object transport.
|
|
//! Catalog metadata and passive checks never create/approve an execution job.
|
|
use crate::{
|
|
jobs::Error,
|
|
update_artifact_store::{MAX_CHUNK_BYTES, UpdateArtifacts},
|
|
};
|
|
use axum::{
|
|
Extension, Json, Router,
|
|
body::Body,
|
|
extract::{Path, Query, State},
|
|
http::{HeaderMap, HeaderValue, StatusCode},
|
|
response::{IntoResponse, Response},
|
|
routing::{get, post},
|
|
};
|
|
use futures_util::StreamExt;
|
|
use rmm_domain::{
|
|
agent_update::{ArtifactDescriptor, Platform, ReleaseEnvelope, ReleaseManifest},
|
|
jobs::{ArtifactReference, encode, hash},
|
|
};
|
|
use serde::Deserialize;
|
|
use serde_json::{Value, json};
|
|
use sqlx::{Postgres, Row, Transaction};
|
|
use std::{
|
|
fs,
|
|
io::Read,
|
|
os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt},
|
|
path::Path as FsPath,
|
|
time::{Duration, Instant},
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
fn fail(code: &'static str) -> Error {
|
|
Error(StatusCode::CONFLICT, code)
|
|
}
|
|
fn missing() -> Error {
|
|
Error(StatusCode::NOT_FOUND, "NOT_FOUND")
|
|
}
|
|
fn invalid() -> Error {
|
|
Error(StatusCode::BAD_REQUEST, "INVALID_AGENT_RELEASE")
|
|
}
|
|
pub fn platform_name(platform: Platform) -> &'static str {
|
|
match platform {
|
|
Platform::LinuxX64 => "x86_64-unknown-linux-gnu",
|
|
Platform::WindowsX64 => "x86_64-pc-windows-msvc",
|
|
}
|
|
}
|
|
pub fn protected_file(path: &FsPath, maximum: u64) -> Result<Vec<u8>, Error> {
|
|
let key_path =
|
|
std::env::var("RMM_JOB_KEY_FILE").map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
let key_meta =
|
|
fs::symlink_metadata(key_path).map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
if !key_meta.is_file() || key_meta.mode() & 0o077 != 0 {
|
|
return Err(fail("AGENT_UPDATE_NOT_CONFIGURED"));
|
|
}
|
|
let owner = key_meta.uid();
|
|
let mut file = fs::OpenOptions::new()
|
|
.read(true)
|
|
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
|
|
.open(path)
|
|
.map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
let m = file
|
|
.metadata()
|
|
.map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
let parent = fs::symlink_metadata(
|
|
path.parent()
|
|
.ok_or_else(|| fail("AGENT_UPDATE_NOT_CONFIGURED"))?,
|
|
)
|
|
.map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
if !m.is_file()
|
|
|| m.uid() != owner
|
|
|| m.mode() & 0o777 != 0o600
|
|
|| m.nlink() != 1
|
|
|| m.len() == 0
|
|
|| m.len() > maximum
|
|
|| !parent.is_dir()
|
|
|| parent.uid() != owner
|
|
|| parent.permissions().mode() & 0o022 != 0
|
|
{
|
|
return Err(fail("AGENT_UPDATE_NOT_CONFIGURED"));
|
|
}
|
|
let mut raw = Vec::new();
|
|
(&mut file)
|
|
.take(maximum + 1)
|
|
.read_to_end(&mut raw)
|
|
.map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
let after = file
|
|
.metadata()
|
|
.map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
if raw.len() as u64 != m.len()
|
|
|| (
|
|
m.dev(),
|
|
m.ino(),
|
|
m.len(),
|
|
m.uid(),
|
|
m.mode(),
|
|
m.ctime(),
|
|
m.ctime_nsec(),
|
|
m.mtime(),
|
|
m.mtime_nsec(),
|
|
) != (
|
|
after.dev(),
|
|
after.ino(),
|
|
after.len(),
|
|
after.uid(),
|
|
after.mode(),
|
|
after.ctime(),
|
|
after.ctime_nsec(),
|
|
after.mtime(),
|
|
after.mtime_nsec(),
|
|
)
|
|
{
|
|
return Err(fail("AGENT_UPDATE_NOT_CONFIGURED"));
|
|
}
|
|
Ok(raw)
|
|
}
|
|
pub fn store() -> Result<UpdateArtifacts, Error> {
|
|
let key = std::env::var("RMM_JOB_KEY_FILE").map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
let path = FsPath::new(&key);
|
|
let metadata = fs::symlink_metadata(path).map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
if !metadata.is_file() || metadata.permissions().mode() & 0o077 != 0 {
|
|
return Err(fail("AGENT_UPDATE_NOT_CONFIGURED"));
|
|
}
|
|
Ok(UpdateArtifacts::new(
|
|
&std::env::var("RMM_PROFILE").unwrap_or_default(),
|
|
path.parent()
|
|
.ok_or_else(|| fail("AGENT_UPDATE_NOT_CONFIGURED"))?
|
|
.join("agent-update-artifacts"),
|
|
metadata.uid(),
|
|
)?)
|
|
}
|
|
fn verifier_platform(platform: Platform) -> rmm_updater::Platform {
|
|
match platform {
|
|
Platform::LinuxX64 => rmm_updater::Platform::LinuxX64,
|
|
Platform::WindowsX64 => rmm_updater::Platform::WindowsX64,
|
|
}
|
|
}
|
|
pub fn verify_envelope(
|
|
envelope: &ReleaseEnvelope,
|
|
binding: Option<&rmm_domain::agent_update::UpdaterBinding>,
|
|
) -> Result<ReleaseManifest, Error> {
|
|
let decoded = envelope.decode().map_err(|_| invalid())?;
|
|
let path = std::env::var("RMM_AGENT_UPDATE_KEYRING_FILE")
|
|
.map_err(|_| fail("AGENT_UPDATE_NOT_CONFIGURED"))?;
|
|
let raw = protected_file(FsPath::new(&path), 16384)?;
|
|
let config: rmm_domain::agent_update::UpdateKeyringFile =
|
|
serde_json::from_slice(&raw).map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?;
|
|
if !config.validate()
|
|
|| binding.is_some_and(|b| {
|
|
b.keyring_generation != config.generation || b.keyring_sha256 != hash(&raw)
|
|
})
|
|
{
|
|
return Err(fail("AGENT_UPDATE_TRUST_INVALID"));
|
|
}
|
|
let mut keys = Vec::new();
|
|
for key in config.keys {
|
|
if !rmm_domain::agent_update::digest(&key.public_key_hex) {
|
|
return Err(fail("AGENT_UPDATE_TRUST_INVALID"));
|
|
}
|
|
let raw =
|
|
hex::decode(key.public_key_hex).map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?;
|
|
keys.push(
|
|
rmm_updater::TrustedUpdateKey::new(
|
|
raw.try_into()
|
|
.map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?,
|
|
verifier_platform(key.platform),
|
|
key.first_sequence,
|
|
key.last_sequence,
|
|
key.revoked,
|
|
)
|
|
.map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?,
|
|
);
|
|
}
|
|
let keyring = rmm_updater::UpdateKeyring::new(config.generation, keys)
|
|
.map_err(|_| fail("AGENT_UPDATE_TRUST_INVALID"))?;
|
|
let context = rmm_updater::TrustedContext {
|
|
platform: verifier_platform(decoded.manifest.platform),
|
|
now_utc_seconds: chrono::Utc::now()
|
|
.timestamp()
|
|
.try_into()
|
|
.map_err(|_| invalid())?,
|
|
keyring_generation: config.generation,
|
|
protocol_major: 1,
|
|
state_schema: 1,
|
|
updater_version: 1,
|
|
minimum_release_sequence: 0,
|
|
active_release: None,
|
|
};
|
|
let verified = rmm_updater::verify_release(
|
|
rmm_updater::SignedRelease {
|
|
schema_version: envelope.schema_version,
|
|
key_id: &envelope.key_id,
|
|
payload_bytes: &decoded.payload_bytes,
|
|
signature: &decoded.signature,
|
|
},
|
|
&keyring,
|
|
&context,
|
|
)
|
|
.map_err(|_| invalid())?;
|
|
if verified.manifest_sha256() != hash(&decoded.payload_bytes) {
|
|
return Err(invalid());
|
|
}
|
|
Ok(decoded.manifest)
|
|
}
|
|
pub fn routes(router: Router<crate::State>) -> Router<crate::State> {
|
|
router
|
|
.route(
|
|
"/api/v1/tenants/{tenant}/agent-releases",
|
|
get(list).post(import),
|
|
)
|
|
.route(
|
|
"/api/v1/tenants/{tenant}/agent-update-artifacts",
|
|
post(upload),
|
|
)
|
|
}
|
|
pub fn gateway_routes(router: Router<crate::State>) -> Router<crate::State> {
|
|
router
|
|
.route("/agent/v1/releases/current", get(current))
|
|
.route("/agent/v1/update-artifacts/{artifact}", get(download))
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct UploadDescriptor {
|
|
artifact_id: Uuid,
|
|
sha256: String,
|
|
size_bytes: u64,
|
|
}
|
|
pub async fn upload(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path(t): Path<Uuid>,
|
|
Query(input): Query<UploadDescriptor>,
|
|
body: Body,
|
|
) -> Result<Json<Value>, Error> {
|
|
let (p, tx) = crate::catalog::context(&s, &h, t, true).await?;
|
|
if input.artifact_id.is_nil()
|
|
|| !rmm_domain::agent_update::digest(&input.sha256)
|
|
|| !(1..=rmm_updater::MAX_ARTIFACT_BYTES).contains(&input.size_bytes)
|
|
|| h.get("content-type").and_then(|v| v.to_str().ok()) != Some("application/octet-stream")
|
|
|| h.get("content-length")
|
|
.and_then(|v| v.to_str().ok())
|
|
.and_then(|v| v.parse::<u64>().ok())
|
|
!= Some(input.size_bytes)
|
|
{
|
|
return Err(invalid());
|
|
}
|
|
tx.rollback().await?;
|
|
// Authentication/CSRF/tenant checks precede body reads. No unbounded body Vec.
|
|
let store = store()?;
|
|
let mut staged = store.begin(t, &input.sha256, input.size_bytes)?;
|
|
let deadline = Instant::now() + Duration::from_secs(180);
|
|
let mut stream = body.into_data_stream();
|
|
loop {
|
|
if Instant::now() >= deadline {
|
|
return Err(Error(
|
|
StatusCode::REQUEST_TIMEOUT,
|
|
"AGENT_UPDATE_IMPORT_TIMEOUT",
|
|
));
|
|
}
|
|
let frame = tokio::time::timeout(
|
|
deadline.saturating_duration_since(Instant::now()),
|
|
stream.next(),
|
|
)
|
|
.await
|
|
.map_err(|_| Error(StatusCode::REQUEST_TIMEOUT, "AGENT_UPDATE_IMPORT_TIMEOUT"))?;
|
|
if Instant::now() >= deadline {
|
|
return Err(Error(
|
|
StatusCode::REQUEST_TIMEOUT,
|
|
"AGENT_UPDATE_IMPORT_TIMEOUT",
|
|
));
|
|
}
|
|
let Some(frame) = frame else { break };
|
|
let bytes = frame.map_err(|_| invalid())?;
|
|
for chunk in bytes.chunks(32768) {
|
|
staged.append(chunk)?;
|
|
}
|
|
if Instant::now() >= deadline {
|
|
return Err(Error(
|
|
StatusCode::REQUEST_TIMEOUT,
|
|
"AGENT_UPDATE_IMPORT_TIMEOUT",
|
|
));
|
|
}
|
|
}
|
|
let (current_principal, mut tx) = crate::catalog::context(&s, &h, t, true).await?;
|
|
if current_principal.name != p.name {
|
|
return Err(Error(StatusCode::FORBIDDEN, "FORBIDDEN"));
|
|
}
|
|
if Instant::now() >= deadline {
|
|
return Err(Error(
|
|
StatusCode::REQUEST_TIMEOUT,
|
|
"AGENT_UPDATE_IMPORT_TIMEOUT",
|
|
));
|
|
}
|
|
staged.finish(&store, t)?;
|
|
sqlx::query("INSERT INTO agent_update_artifacts(tenant_id,artifact_id,sha256,size_bytes) VALUES($1,$2,$3,$4) ON CONFLICT DO NOTHING")
|
|
.bind(t).bind(input.artifact_id).bind(&input.sha256).bind(input.size_bytes as i64).execute(&mut *tx).await?;
|
|
let actual =
|
|
sqlx::query("SELECT sha256,size_bytes FROM agent_update_artifacts WHERE artifact_id=$1")
|
|
.bind(input.artifact_id)
|
|
.fetch_one(&mut *tx)
|
|
.await?;
|
|
if actual.get::<String, _>("sha256") != input.sha256
|
|
|| actual.get::<i64, _>("size_bytes") as u64 != input.size_bytes
|
|
{
|
|
return Err(fail("AGENT_UPDATE_ARTIFACT_CONFLICT"));
|
|
}
|
|
sqlx::query("INSERT INTO audit_events VALUES($1,$2,$3,'agent_update_artifact_imported',$4,now())")
|
|
.bind(t).bind(Uuid::new_v4()).bind(p.name).bind(json!({"artifact_id":input.artifact_id,"sha256":input.sha256,"size_bytes":input.size_bytes,"testsimulation":p.testsimulation})).execute(&mut *tx).await?;
|
|
tx.commit().await?;
|
|
Ok(Json(
|
|
json!({"artifact_id":input.artifact_id,"sha256":input.sha256,"size_bytes":input.size_bytes}),
|
|
))
|
|
}
|
|
pub async fn checked_release(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
id: Uuid,
|
|
) -> Result<(ReleaseEnvelope, ReleaseManifest), Error> {
|
|
let row = sqlx::query("SELECT envelope,envelope_sha256,manifest_sha256 FROM agent_update_releases WHERE release_id=$1")
|
|
.bind(id).fetch_optional(&mut **tx).await?.ok_or_else(missing)?;
|
|
let raw: Vec<u8> = row.get("envelope");
|
|
let envelope: ReleaseEnvelope = serde_json::from_slice(&raw)?;
|
|
let manifest = verify_envelope(&envelope, None)?;
|
|
if encode(&envelope)? != raw
|
|
|| hash(&raw) != row.get::<String, _>("envelope_sha256")
|
|
|| hash(&envelope.decode().map_err(|_| invalid())?.payload_bytes)
|
|
!= row.get::<String, _>("manifest_sha256")
|
|
|| manifest.release_id != id
|
|
{
|
|
return Err(fail("AGENT_UPDATE_CATALOG_INTEGRITY"));
|
|
}
|
|
Ok((envelope, manifest))
|
|
}
|
|
pub async fn resolve_artifacts(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
tenant: Uuid,
|
|
descriptors: &[ArtifactDescriptor; 2],
|
|
) -> Result<Vec<ArtifactReference>, Error> {
|
|
let store = store()?;
|
|
let mut refs = Vec::new();
|
|
for d in descriptors {
|
|
let actual = sqlx::query(
|
|
"SELECT sha256,size_bytes FROM agent_update_artifacts WHERE artifact_id=$1",
|
|
)
|
|
.bind(d.artifact_id)
|
|
.fetch_optional(&mut **tx)
|
|
.await?
|
|
.ok_or_else(missing)?;
|
|
if actual.get::<String, _>("sha256") != d.sha256
|
|
|| actual.get::<i64, _>("size_bytes") as u64 != d.bytes
|
|
{
|
|
return Err(fail("AGENT_UPDATE_ARTIFACT_BINDING"));
|
|
}
|
|
store.verify(tenant, &d.sha256, d.bytes)?;
|
|
refs.push(ArtifactReference {
|
|
artifact_id: d.artifact_id,
|
|
sha256: d.sha256.clone(),
|
|
size_bytes: d.bytes,
|
|
});
|
|
}
|
|
Ok(refs)
|
|
}
|
|
pub async fn import(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path(t): Path<Uuid>,
|
|
Json(envelope): Json<ReleaseEnvelope>,
|
|
) -> Result<Json<Value>, Error> {
|
|
let (p, mut tx) = crate::catalog::context(&s, &h, t, true).await?;
|
|
let raw = encode(&envelope)?;
|
|
if raw.len() > 32768 {
|
|
return Err(invalid());
|
|
}
|
|
let m = verify_envelope(&envelope, None)?;
|
|
let sequence = i64::try_from(m.release_sequence).map_err(|_| invalid())?;
|
|
resolve_artifacts(&mut tx, t, &m.artifacts).await?;
|
|
let manifest_sha = hash(&envelope.decode().map_err(|_| invalid())?.payload_bytes);
|
|
sqlx::query("INSERT INTO agent_update_releases(tenant_id,release_id,platform,release_sequence,manifest_sha256,envelope_sha256,envelope,agent_artifact_id,agent_sha256,agent_bytes,helper_artifact_id,helper_sha256,helper_bytes) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)")
|
|
.bind(t).bind(m.release_id).bind(platform_name(m.platform)).bind(sequence).bind(&manifest_sha).bind(hash(&raw)).bind(raw)
|
|
.bind(m.artifacts[0].artifact_id).bind(&m.artifacts[0].sha256).bind(m.artifacts[0].bytes as i64)
|
|
.bind(m.artifacts[1].artifact_id).bind(&m.artifacts[1].sha256).bind(m.artifacts[1].bytes as i64).execute(&mut *tx).await?;
|
|
sqlx::query("INSERT INTO audit_events VALUES($1,$2,$3,'agent_update_release_imported',$4,now())").bind(t).bind(Uuid::new_v4()).bind(p.name)
|
|
.bind(json!({"release_id":m.release_id,"manifest_sha256":manifest_sha,"testsimulation":p.testsimulation})).execute(&mut *tx).await?;
|
|
tx.commit().await?;
|
|
Ok(Json(
|
|
json!({"release_id":m.release_id,"manifest_sha256":manifest_sha,"release_envelope":envelope,"manifest":m}),
|
|
))
|
|
}
|
|
pub async fn list_for(
|
|
s: &crate::State,
|
|
p: &crate::auth::Principal,
|
|
tenant: Uuid,
|
|
query: crate::pagination::PageQuery,
|
|
) -> Result<Value, Error> {
|
|
crate::jobs::scope(p, tenant, false)?;
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
|
crate::permissions::require_principal(&mut tx, p, "operations:read").await?;
|
|
let mut page =
|
|
crate::pagination::Cursor::parse(s, p, "catalog:agent-releases:release_id:asc", query)
|
|
.map_err(|status| Error(status, "VALIDATION_ERROR"))?;
|
|
let rows = sqlx::query("SELECT release_id,envelope,envelope_sha256,manifest_sha256 FROM agent_update_releases WHERE release_id>$1 AND created_at<=$2 ORDER BY release_id LIMIT $3")
|
|
.bind(page.last).bind(page.as_of).bind(i64::from(page.limit)+1).fetch_all(&mut *tx).await?;
|
|
let truncated = rows.len() > usize::from(page.limit);
|
|
let next = if truncated {
|
|
Some(
|
|
page.next(s, rows[usize::from(page.limit) - 1].get("release_id"))
|
|
.map_err(|status| Error(status, "VALIDATION_ERROR"))?,
|
|
)
|
|
} else {
|
|
None
|
|
};
|
|
let mut items = Vec::new();
|
|
for row in rows.iter().take(usize::from(page.limit)) {
|
|
let raw: Vec<u8> = row.get("envelope");
|
|
let envelope: ReleaseEnvelope = serde_json::from_slice(&raw)?;
|
|
let decoded = envelope.decode().map_err(|_| invalid())?;
|
|
if encode(&envelope)? != raw
|
|
|| hash(&raw) != row.get::<String, _>("envelope_sha256")
|
|
|| hash(&decoded.payload_bytes) != row.get::<String, _>("manifest_sha256")
|
|
|| decoded.manifest.release_id != row.get::<Uuid, _>("release_id")
|
|
{
|
|
return Err(fail("AGENT_UPDATE_CATALOG_INTEGRITY"));
|
|
}
|
|
// Stored expired metadata remains readable and is explicitly not apply authority.
|
|
items.push(json!({"release_id":decoded.manifest.release_id,"manifest_sha256":hash(&decoded.payload_bytes),"release_envelope":envelope,"manifest":decoded.manifest,"signature_and_time_valid":verify_envelope(&envelope,None).is_ok()}));
|
|
}
|
|
tx.commit().await?;
|
|
Ok(
|
|
json!({"items":items,"next_cursor":next,"completeness":if truncated {"partial"}else{"complete"},"truncated":truncated,"as_of":page.as_of}),
|
|
)
|
|
}
|
|
async fn list(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path(t): Path<Uuid>,
|
|
Query(query): Query<crate::pagination::PageQuery>,
|
|
) -> Result<Json<Value>, Error> {
|
|
let p = s
|
|
.auth
|
|
.principal(&s, &h, false)
|
|
.await
|
|
.map_err(|status| Error(status, crate::auth::error_code(status)))?;
|
|
Ok(Json(list_for(&s, &p, t, query).await?))
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Current {
|
|
platform: Platform,
|
|
}
|
|
async fn device(
|
|
s: &crate::State,
|
|
peer: crate::Peer,
|
|
headers: &HeaderMap,
|
|
) -> Result<(Uuid, Uuid), Error> {
|
|
if headers.contains_key("authorization") {
|
|
return Err(Error(StatusCode::UNAUTHORIZED, "DEVICE_MTLS_REQUIRED"));
|
|
}
|
|
crate::agent_http::identity(s, peer)
|
|
.await
|
|
.map_err(|status| Error(status, "UNAUTHENTICATED"))
|
|
}
|
|
async fn current(
|
|
State(s): State<crate::State>,
|
|
Extension(peer): Extension<crate::Peer>,
|
|
headers: HeaderMap,
|
|
Query(q): Query<Current>,
|
|
) -> Result<Json<Value>, Error> {
|
|
let (tenant, agent) = device(&s, peer, &headers).await?;
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
|
let caps: Value = sqlx::query_scalar(
|
|
"SELECT capabilities FROM agent_installations WHERE agent_id=$1 AND revoked_at IS NULL",
|
|
)
|
|
.bind(agent)
|
|
.fetch_one(&mut *tx)
|
|
.await?;
|
|
let caps: Vec<String> = serde_json::from_value(caps)?;
|
|
let expected = match q.platform {
|
|
Platform::LinuxX64 => "linux.base/1",
|
|
Platform::WindowsX64 => "windows.base/1",
|
|
};
|
|
if !caps.iter().any(|c| c == expected) {
|
|
return Err(missing());
|
|
}
|
|
let ids:Vec<Uuid>=sqlx::query_scalar("SELECT release_id FROM agent_update_releases WHERE platform=$1 ORDER BY release_sequence DESC LIMIT 1").bind(platform_name(q.platform)).fetch_all(&mut *tx).await?;
|
|
let release = if let Some(id) = ids.first() {
|
|
let (envelope, manifest) = checked_release(&mut tx, *id).await?;
|
|
resolve_artifacts(&mut tx, tenant, &manifest.artifacts).await?;
|
|
Some(json!({"release_envelope":envelope,"manifest":manifest}))
|
|
} else {
|
|
None
|
|
};
|
|
tx.commit().await?;
|
|
Ok(Json(
|
|
json!({"status":if release.is_some(){"available"}else{"not_configured"},"release":release,"observed_at":chrono::Utc::now(),"server_time":chrono::Utc::now(),"job_created":false}),
|
|
))
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Chunk {
|
|
job_id: Uuid,
|
|
attempt_id: Uuid,
|
|
#[serde(default)]
|
|
offset: u64,
|
|
length: Option<u32>,
|
|
}
|
|
async fn download(
|
|
State(s): State<crate::State>,
|
|
Extension(peer): Extension<crate::Peer>,
|
|
headers: HeaderMap,
|
|
Path(id): Path<Uuid>,
|
|
Query(q): Query<Chunk>,
|
|
) -> Result<Response, Error> {
|
|
let (tenant, agent) = device(&s, peer, &headers).await?;
|
|
let length = q.length.unwrap_or(MAX_CHUNK_BYTES);
|
|
if !(1..=MAX_CHUNK_BYTES).contains(&length) {
|
|
return Err(invalid());
|
|
}
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
|
let row=sqlx::query("SELECT j.payload,p.payload AS plan_payload,p.plan_hash,z.principal AS approver,i.asset_id,i.enrollment_generation,i.assignment_version FROM jobs j JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations z ON z.tenant_id=o.tenant_id AND z.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=r.tenant_id AND p.plan_id=r.plan_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id WHERE j.agent_id=$1 AND j.job_id=$2 AND j.attempt_id=$3 AND j.state IN ('queued','accepted','running','verifying') AND r.approval_state='approved' AND z.revoked_at IS NULL AND p.expires_at>now() AND i.revoked_at IS NULL")
|
|
.bind(agent).bind(q.job_id).bind(q.attempt_id).fetch_optional(&mut *tx).await?.ok_or_else(missing)?;
|
|
let job: rmm_domain::jobs::JobPayload =
|
|
serde_json::from_slice(&row.get::<Vec<u8>, _>("payload"))?;
|
|
let raw: Vec<u8> = row.get("plan_payload");
|
|
if hash(&raw) != row.get::<String, _>("plan_hash") {
|
|
return Err(fail("PLAN_HASH_MISMATCH"));
|
|
}
|
|
let plan: rmm_domain::jobs::PlanPayload = serde_json::from_slice(&raw)?;
|
|
validate_job_binding(&plan, &job)?;
|
|
let rmm_domain::jobs::Action::UpdateAgent { update } = &job.action else {
|
|
return Err(missing());
|
|
};
|
|
crate::root_policy::authorize(&mut tx, &plan.principal, &job).await?;
|
|
crate::root_policy::authorize(&mut tx, &row.get::<String, _>("approver"), &job).await?;
|
|
check_update(&mut tx, update).await?;
|
|
if job.tenant_id != tenant
|
|
|| job.agent_id != agent
|
|
|| job.asset_id != row.get::<Uuid, _>("asset_id")
|
|
|| job.enrollment_generation != row.get::<i64, _>("enrollment_generation") as u64
|
|
|| job.assignment_version != row.get::<i64, _>("assignment_version") as u64
|
|
{
|
|
return Err(missing());
|
|
}
|
|
let manifest = update.manifest().map_err(|_| invalid())?;
|
|
let descriptor = manifest
|
|
.artifacts
|
|
.iter()
|
|
.find(|a| a.artifact_id == id)
|
|
.ok_or_else(missing)?;
|
|
if q.offset >= descriptor.bytes {
|
|
return Err(Error(
|
|
StatusCode::RANGE_NOT_SATISFIABLE,
|
|
"INVALID_ARTIFACT_RANGE",
|
|
));
|
|
}
|
|
tx.commit().await?;
|
|
let bytes = store()?.read_chunk(
|
|
tenant,
|
|
&descriptor.sha256,
|
|
descriptor.bytes,
|
|
q.offset,
|
|
length,
|
|
)?;
|
|
let end = q.offset + bytes.len() as u64;
|
|
let partial = q.offset > 0 || end < descriptor.bytes;
|
|
let mut response = (
|
|
if partial {
|
|
StatusCode::PARTIAL_CONTENT
|
|
} else {
|
|
StatusCode::OK
|
|
},
|
|
bytes,
|
|
)
|
|
.into_response();
|
|
let h = response.headers_mut();
|
|
for (name, value) in [
|
|
("content-type", "application/octet-stream"),
|
|
("content-disposition", "attachment"),
|
|
("x-content-type-options", "nosniff"),
|
|
("accept-ranges", "bytes"),
|
|
] {
|
|
h.insert(name, HeaderValue::from_static(value));
|
|
}
|
|
h.insert(
|
|
"x-artifact-sha256",
|
|
HeaderValue::from_str(&descriptor.sha256).map_err(|_| invalid())?,
|
|
);
|
|
if partial {
|
|
h.insert(
|
|
"content-range",
|
|
HeaderValue::from_str(&format!(
|
|
"bytes {}-{}/{}",
|
|
q.offset,
|
|
end - 1,
|
|
descriptor.bytes
|
|
))
|
|
.map_err(|_| invalid())?,
|
|
);
|
|
}
|
|
Ok(response)
|
|
}
|
|
|
|
pub async fn check_update(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
update: &rmm_domain::agent_update::ResolvedAgentUpdate,
|
|
) -> Result<(), Error> {
|
|
if !update.validate() {
|
|
return Err(invalid());
|
|
}
|
|
let manifest = verify_envelope(&update.release_envelope, Some(&update.updater))?;
|
|
let (envelope, stored) = checked_release(tx, manifest.release_id).await?;
|
|
if envelope != update.release_envelope || stored != manifest {
|
|
return Err(fail("AGENT_UPDATE_CATALOG_BINDING"));
|
|
}
|
|
Ok(())
|
|
}
|
|
pub fn validate_plan_bindings(plan: &rmm_domain::jobs::PlanPayload) -> Result<(), Error> {
|
|
use rmm_domain::jobs::Action;
|
|
let updates = plan
|
|
.jobs
|
|
.iter()
|
|
.filter(|j| matches!(j.payload.action, Action::UpdateAgent { .. }))
|
|
.count();
|
|
if updates == 0 {
|
|
return Ok(());
|
|
}
|
|
if updates != plan.jobs.len()
|
|
|| !plan.recipe_references.is_empty()
|
|
|| plan.artifact_references.len() != updates * 2
|
|
{
|
|
return Err(fail("AGENT_UPDATE_ARTIFACT_BINDING"));
|
|
}
|
|
for fixed in &plan.jobs {
|
|
let Action::UpdateAgent { update } = &fixed.payload.action else {
|
|
return Err(invalid());
|
|
};
|
|
let m = update.manifest().map_err(|_| invalid())?;
|
|
let refs = plan
|
|
.artifact_references
|
|
.iter()
|
|
.filter(|r| {
|
|
r.job_id == fixed.payload.job_id && r.attempt_id == fixed.payload.attempt_id
|
|
})
|
|
.collect::<Vec<_>>();
|
|
if !update.validate()
|
|
|| !fixed.payload.action.valid_job_limits(
|
|
fixed.payload.max_runtime_seconds,
|
|
fixed.payload.max_output_bytes,
|
|
)
|
|
|| plan.tenant_id != fixed.payload.tenant_id
|
|
|| update.execution_identity() != Some(fixed.payload.execution_identity.as_str())
|
|
|| refs.len() != 2
|
|
|| refs
|
|
.iter()
|
|
.zip(m.artifacts.iter())
|
|
.any(|(actual, expected)| {
|
|
actual.artifact
|
|
!= ArtifactReference {
|
|
artifact_id: expected.artifact_id,
|
|
sha256: expected.sha256.clone(),
|
|
size_bytes: expected.bytes,
|
|
}
|
|
})
|
|
|| hash(&encode(&fixed.payload)?) != fixed.job_payload_hash
|
|
|| fixed.script_sha256.is_some()
|
|
|| plan
|
|
.jobs
|
|
.iter()
|
|
.filter(|other| {
|
|
other.payload.job_id == fixed.payload.job_id
|
|
|| other.payload.attempt_id == fixed.payload.attempt_id
|
|
})
|
|
.count()
|
|
!= 1
|
|
{
|
|
return Err(fail("AGENT_UPDATE_ARTIFACT_BINDING"));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
pub fn validate_job_binding(
|
|
plan: &rmm_domain::jobs::PlanPayload,
|
|
job: &rmm_domain::jobs::JobPayload,
|
|
) -> Result<(), Error> {
|
|
if !matches!(job.action, rmm_domain::jobs::Action::UpdateAgent { .. }) {
|
|
return Err(missing());
|
|
}
|
|
validate_plan_bindings(plan)?;
|
|
let actual = encode(job)?;
|
|
let fixed = plan
|
|
.jobs
|
|
.iter()
|
|
.filter(|j| j.payload.job_id == job.job_id && j.payload.attempt_id == job.attempt_id)
|
|
.collect::<Vec<_>>();
|
|
if fixed.len() != 1
|
|
|| encode(&fixed[0].payload)? != actual
|
|
|| fixed[0].job_payload_hash != hash(&actual)
|
|
{
|
|
return Err(fail("AGENT_UPDATE_ARTIFACT_BINDING"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Only saved, authenticated passive observations are consulted. This never
|
|
/// asks a device for live data and never accepts caller-provided predecessor pins.
|
|
pub async fn resolve_target(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
asset: Uuid,
|
|
release_id: Uuid,
|
|
) -> Result<rmm_domain::agent_update::ResolvedAgentUpdate, Error> {
|
|
use rmm_domain::agent_update::{
|
|
AgentUpdateObservation, HealthProfile, ObservationQuality, Recovery, ResolvedAgentUpdate,
|
|
UpdateProfile,
|
|
};
|
|
let row=sqlx::query("SELECT i.tenant_id,i.asset_id,i.agent_id,i.enrollment_generation,i.assignment_version,i.connection_generation,i.connection_boot_id,i.last_contact_at,i.sync_ready,i.helper_sync_ready,i.restore_blocked,i.sync_recovery_required,i.sequence_regressed,o.boot_id,o.observed_at,o.received_at,o.quality,o.completeness,o.data,s.journal_id,s.helper_journal_id FROM agent_installations i JOIN agent_sync_state s ON s.tenant_id=i.tenant_id AND s.agent_id=i.agent_id JOIN LATERAL (SELECT boot_id,observed_at,received_at,quality,completeness,data FROM observations WHERE tenant_id=i.tenant_id AND agent_id=i.agent_id AND source_id IN ('linux.base','windows.base','agent.update') ORDER BY observed_at DESC,received_at DESC LIMIT 1) o ON true WHERE i.asset_id=$1 AND i.revoked_at IS NULL")
|
|
.bind(asset).fetch_all(&mut **tx).await?;
|
|
if row.len() != 1 {
|
|
return Err(fail("AGENT_UPDATE_STATE_NOT_CONFIGURED"));
|
|
}
|
|
let row = &row[0];
|
|
let now = chrono::Utc::now();
|
|
let observed: chrono::DateTime<chrono::Utc> = row.get("observed_at");
|
|
let received: chrono::DateTime<chrono::Utc> = row.get("received_at");
|
|
let contact: Option<chrono::DateTime<chrono::Utc>> = row.get("last_contact_at");
|
|
if row.get::<String, _>("quality") != "valid"
|
|
|| row.get::<String, _>("completeness") != "partial"
|
|
|| !row.get::<bool, _>("sync_ready")
|
|
|| !row.get::<bool, _>("helper_sync_ready")
|
|
|| row.get::<bool, _>("restore_blocked")
|
|
|| row.get::<bool, _>("sync_recovery_required")
|
|
|| row.get::<bool, _>("sequence_regressed")
|
|
|| row.get::<Option<Uuid>, _>("connection_boot_id") != Some(row.get("boot_id"))
|
|
|| contact.is_none_or(|t| t > now || t < now - chrono::Duration::seconds(90))
|
|
|| observed > now
|
|
|| received > now
|
|
|| observed < now - chrono::Duration::minutes(5)
|
|
|| received < now - chrono::Duration::minutes(5)
|
|
{
|
|
return Err(fail("AGENT_UPDATE_STATE_STALE"));
|
|
}
|
|
let data: Value = row.get("data");
|
|
let observation: AgentUpdateObservation = serde_json::from_value(
|
|
data.get("agent_update")
|
|
.cloned()
|
|
.ok_or_else(|| fail("AGENT_UPDATE_STATE_NOT_CONFIGURED"))?,
|
|
)?;
|
|
if !observation.validate()
|
|
|| observation.quality != ObservationQuality::Valid
|
|
|| observation.observed_at > observed
|
|
|| observation.observed_at < now - chrono::Duration::minutes(5)
|
|
{
|
|
return Err(fail("AGENT_UPDATE_STATE_STALE"));
|
|
}
|
|
let checkpoint=sqlx::query("SELECT payload,created_at,connection_generation,journal_id FROM sync_checkpoints WHERE agent_id=$1 AND decision_id=$2")
|
|
.bind(row.get::<Uuid,_>("agent_id")).bind(observation.signed_sync_decision_id).fetch_optional(&mut **tx).await?.ok_or_else(||fail("AGENT_UPDATE_SYNC_PROVENANCE"))?;
|
|
let original: rmm_domain::reconciliation::ReconcileDecisionPayload =
|
|
serde_json::from_slice(&checkpoint.get::<Vec<u8>, _>("payload"))?;
|
|
if !original.valid_profile(observation.observed_at)
|
|
|| !original.ready
|
|
|| !original.helper_ready
|
|
|| original.tenant_id != row.get::<Uuid, _>("tenant_id")
|
|
|| original.asset_id != asset
|
|
|| original.agent_id != row.get::<Uuid, _>("agent_id")
|
|
|| original.boot_id != row.get::<Uuid, _>("boot_id")
|
|
|| original.connection_generation != row.get::<i64, _>("connection_generation") as u64
|
|
|| original.enrollment_generation != row.get::<i64, _>("enrollment_generation") as u64
|
|
|| original.assignment_version != row.get::<i64, _>("assignment_version") as u64
|
|
|| original.journal_id != row.get::<Uuid, _>("journal_id")
|
|
|| original.helper_journal_id != row.get::<Option<Uuid>, _>("helper_journal_id")
|
|
|| checkpoint.get::<chrono::DateTime<chrono::Utc>, _>("created_at")
|
|
> observation.observed_at
|
|
{
|
|
return Err(fail("AGENT_UPDATE_SYNC_PROVENANCE"));
|
|
}
|
|
let (envelope, manifest) = checked_release(tx, release_id).await?;
|
|
let manifest_sha = hash(&envelope.decode().map_err(|_| invalid())?.payload_bytes);
|
|
let preview = observation
|
|
.candidate_previews
|
|
.iter()
|
|
.find(|c| c.release_id == release_id && c.manifest_sha256 == manifest_sha)
|
|
.ok_or_else(|| fail("AGENT_UPDATE_PREVIEW_NOT_CONFIGURED"))?;
|
|
if observation.platform != manifest.platform
|
|
|| observation
|
|
.activation_floor
|
|
.is_none_or(|floor| manifest.release_sequence <= floor)
|
|
{
|
|
return Err(fail("AGENT_UPDATE_PREDECESSOR_MISMATCH"));
|
|
}
|
|
let update = ResolvedAgentUpdate {
|
|
schema_version: 1,
|
|
profile: UpdateProfile::FixedCodePairV1,
|
|
release_envelope_sha256: envelope.sha256()?,
|
|
release_envelope: envelope,
|
|
manifest_sha256: manifest_sha,
|
|
predecessor: observation.predecessor.ok_or_else(invalid)?,
|
|
updater: observation.updater.ok_or_else(invalid)?,
|
|
peer_pin_transition: preview.peer_pin_transition.clone(),
|
|
recovery: Recovery::RestoreExactPredecessorOnce,
|
|
health_profile: HealthProfile::MtlsInventorySyncV1,
|
|
health_timeout_seconds: 180,
|
|
no_reboot: true,
|
|
};
|
|
if !update.validate() {
|
|
return Err(fail("AGENT_UPDATE_PREDECESSOR_MISMATCH"));
|
|
}
|
|
verify_envelope(&update.release_envelope, Some(&update.updater))?;
|
|
Ok(update)
|
|
}
|
|
pub async fn validate_current_target(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
job: &rmm_domain::jobs::JobPayload,
|
|
) -> Result<(), Error> {
|
|
let rmm_domain::jobs::Action::UpdateAgent { update } = &job.action else {
|
|
return Err(invalid());
|
|
};
|
|
let current = resolve_target(
|
|
tx,
|
|
job.asset_id,
|
|
update.manifest().map_err(|_| invalid())?.release_id,
|
|
)
|
|
.await?;
|
|
if ¤t != update.as_ref() {
|
|
return Err(fail("AGENT_UPDATE_PREDECESSOR_MISMATCH"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn actual_codec_pair_plan_requires_exact_per_attempt_artifact_authority() {
|
|
let fixtures: Value = serde_json::from_str(include_str!(
|
|
"../../../tests/agent_update/codec-golden.json"
|
|
))
|
|
.unwrap();
|
|
for name in ["linux_general", "linux_full", "windows_pair"] {
|
|
let fixture = &fixtures[name];
|
|
let raw: Vec<u8> =
|
|
serde_json::from_value(fixture["plan_payload_bytes"].clone()).unwrap();
|
|
let plan: rmm_domain::jobs::PlanPayload = serde_json::from_slice(&raw).unwrap();
|
|
assert_eq!(encode(&plan).unwrap(), raw);
|
|
let job = &plan.jobs[0].payload;
|
|
validate_job_binding(&plan, job).unwrap();
|
|
for variant in 0..5 {
|
|
let mut bad = plan.clone();
|
|
match variant {
|
|
0 => bad.artifact_references.clear(),
|
|
1 => bad
|
|
.artifact_references
|
|
.push(bad.artifact_references[0].clone()),
|
|
2 => bad.artifact_references[0].artifact.artifact_id = Uuid::new_v4(),
|
|
3 => bad.artifact_references[0].attempt_id = Uuid::new_v4(),
|
|
_ => bad.jobs[0].payload.max_runtime_seconds = 601,
|
|
}
|
|
assert!(validate_job_binding(&bad, job).is_err());
|
|
}
|
|
}
|
|
}
|
|
#[test]
|
|
fn plan_request_accepts_release_id_but_never_user_supplied_local_bindings() {
|
|
let mut value = json!({"tenant_id":"11111111-1111-4111-8111-111111111111","action":"update_agent","targets":{"asset_ids":["11111111-1111-4111-8111-111111111112"]},
|
|
"parameters":{"release_id":"11111111-1111-4111-8111-111111111113","execution_identity":"root","timeout_seconds":600,"max_output_bytes":65536},
|
|
"schedule":{"not_before":"2030-01-01T12:00:00Z","start_before":"2030-01-01T12:10:00Z","offline_policy":"wait_until_deadline"},"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"codec only"});
|
|
let parsed: crate::jobs::PlanRequest = serde_json::from_value(value.clone()).unwrap();
|
|
assert!(matches!(parsed, crate::jobs::PlanRequest::AgentUpdate(_)));
|
|
// The old synchronous resolver cannot manufacture local state.
|
|
assert!(
|
|
parsed
|
|
.resolve(Uuid::parse_str(value["tenant_id"].as_str().unwrap()).unwrap())
|
|
.is_err()
|
|
);
|
|
value["parameters"]["predecessor"] = json!({"installed_record_sha256":"00".repeat(32)});
|
|
assert!(serde_json::from_value::<crate::jobs::PlanRequest>(value).is_err());
|
|
}
|
|
}
|