Add gated agent update planning, preserved handoff and Linux worker ports
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -145,8 +145,10 @@ pub async fn ingest(
|
||||
}
|
||||
Some(Payload::InventorySnapshot(s)) => {
|
||||
ensure!(
|
||||
matches!(s.scope.as_str(), "linux.base" | "windows.base")
|
||||
&& s.schema_version == 1
|
||||
matches!(
|
||||
s.scope.as_str(),
|
||||
"linux.base" | "windows.base" | "agent.update"
|
||||
) && s.schema_version == 1
|
||||
&& s.chunk_count == 1
|
||||
&& s.chunk_index == 0
|
||||
&& s.completeness == "partial"
|
||||
|
||||
@@ -0,0 +1,595 @@
|
||||
//! Evidence for one original update, never admission, approval or readiness.
|
||||
//!
|
||||
//! The server checks its retained job, signed control history and inventory.
|
||||
//! Concrete local file/process/ledger observations still require the separately
|
||||
//! authenticated helper guard; device mTLS is not OS attestation.
|
||||
use crate::jobs::Error;
|
||||
use axum::{
|
||||
Extension, Json, Router,
|
||||
extract::{DefaultBodyLimit, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
routing::post,
|
||||
};
|
||||
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use chrono::{DateTime, Utc};
|
||||
use openssl::{
|
||||
pkey::{Id, PKey, Private},
|
||||
sign::{Signer, Verifier},
|
||||
};
|
||||
use prost::Message;
|
||||
use rmm_domain::{
|
||||
agent_update::{self as update, UpdateInflightProof},
|
||||
jobs::{Action, ExecutionPermitPayload, JobPayload, PlanPayload, encode, hash},
|
||||
reconciliation::ReconcileDecisionPayload,
|
||||
};
|
||||
use rmm_protocol::{Envelope, Payload, SyncRequest, SyncResponse};
|
||||
use rmm_updater_handoff::{Event, Evidence, HEALTH_DOMAIN, HealthReceipt, Phase, SignedHealth};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
use sqlx::{Postgres, Row, Transaction};
|
||||
use std::path::Path;
|
||||
use uuid::Uuid;
|
||||
|
||||
const RAW_LIMIT: usize = rmm_domain::MAX_MESSAGE;
|
||||
const ENCODED_LIMIT: usize = 4 * RAW_LIMIT.div_ceil(3);
|
||||
|
||||
fn denied() -> Error {
|
||||
Error(StatusCode::CONFLICT, "AGENT_UPDATE_HEALTH_UNPROVEN")
|
||||
}
|
||||
fn require(ok: bool) -> Result<(), Error> {
|
||||
if !ok {
|
||||
return Err(denied());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn gateway_routes(router: Router<crate::State>) -> Router<crate::State> {
|
||||
router.route(
|
||||
"/agent/v1/update-health",
|
||||
post(issue).layer(DefaultBodyLimit::max(5 * 1024 * 1024)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Canonical original bytes, not caller-selected health fields or timestamps.
|
||||
/// The confirming request must already be retained by the normal sync route;
|
||||
/// both its previous-decision links attest application of the earlier control.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct HealthRequest {
|
||||
proof: UpdateInflightProof,
|
||||
applied_sync_request_base64url: String,
|
||||
confirming_sync_request_base64url: String,
|
||||
inventory_envelope_base64url: String,
|
||||
}
|
||||
|
||||
fn message<M: Message + Default>(encoded: &str) -> Result<(M, Vec<u8>), Error> {
|
||||
require(!encoded.is_empty() && encoded.len() <= ENCODED_LIMIT)?;
|
||||
let raw = URL_SAFE_NO_PAD.decode(encoded).map_err(|_| denied())?;
|
||||
require(!raw.is_empty() && raw.len() <= RAW_LIMIT && URL_SAFE_NO_PAD.encode(&raw) == encoded)?;
|
||||
let value = M::decode(raw.as_slice()).map_err(|_| denied())?;
|
||||
// No duplicate fields, hidden unknown fields or alternate protobuf bytes.
|
||||
require(value.encode_to_vec() == raw)?;
|
||||
Ok((value, raw))
|
||||
}
|
||||
|
||||
fn health_signer() -> Result<PKey<Private>, Error> {
|
||||
let path = std::env::var("RMM_AGENT_UPDATE_HEALTH_KEY_FILE").map_err(|_| denied())?;
|
||||
require(Path::new(&path).is_absolute())?;
|
||||
let raw = crate::agent_updates::protected_file(Path::new(&path), 16384)?;
|
||||
let key = PKey::private_key_from_pem(&raw).map_err(|_| denied())?;
|
||||
require(key.id() == Id::ED25519)?;
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
/// Separately provisioned health key. Never generated or learned from a job,
|
||||
/// request, release, receipt or device. The original job's public identity is
|
||||
/// compared below; it never supplies the signing key itself.
|
||||
fn public_key(signer: &PKey<Private>) -> Result<[u8; 32], Error> {
|
||||
signer
|
||||
.raw_public_key()
|
||||
.map_err(|_| denied())?
|
||||
.try_into()
|
||||
.map_err(|_| denied())
|
||||
}
|
||||
|
||||
fn verify_job_signature(state: &crate::State, raw: &[u8], signature: &[u8]) -> Result<(), Error> {
|
||||
let public = PKey::public_key_from_raw_bytes(&state.job_signer.public_key()?, Id::ED25519)
|
||||
.map_err(|_| denied())?;
|
||||
require(
|
||||
signature.len() == 64
|
||||
&& Verifier::new_without_digest(&public)
|
||||
.map_err(|_| denied())?
|
||||
.verify_oneshot(signature, raw)
|
||||
.map_err(|_| denied())?,
|
||||
)
|
||||
}
|
||||
|
||||
struct StoredSync {
|
||||
decision: ReconcileDecisionPayload,
|
||||
response: Vec<u8>,
|
||||
created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
async fn stored_sync(
|
||||
state: &crate::State,
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
agent: Uuid,
|
||||
request: &SyncRequest,
|
||||
raw: &[u8],
|
||||
now: DateTime<Utc>,
|
||||
) -> Result<StoredSync, Error> {
|
||||
let id = Uuid::parse_str(&request.request_id).map_err(|_| denied())?;
|
||||
let row = sqlx::query("SELECT payload,signature,request_hash,created_at FROM sync_checkpoints WHERE agent_id=$1 AND request_id=$2")
|
||||
.bind(agent).bind(id).fetch_optional(&mut **tx).await?.ok_or_else(denied)?;
|
||||
let bytes: Vec<u8> = row.get("payload");
|
||||
let signature: Vec<u8> = row.get("signature");
|
||||
require(bytes.len() <= 512 * 1024 && row.get::<String, _>("request_hash") == hash(raw))?;
|
||||
verify_job_signature(state, &bytes, &signature)?;
|
||||
let decision: ReconcileDecisionPayload = serde_json::from_slice(&bytes)?;
|
||||
require(
|
||||
encode(&decision)? == bytes
|
||||
&& decision.valid_profile(now)
|
||||
&& decision.key_id == state.job_signer.key_id
|
||||
&& decision.request_id == id
|
||||
&& decision.request_hash == hash(raw)
|
||||
&& decision.journal_id.to_string() == request.journal_id
|
||||
&& decision.checkpoint == request.checkpoint
|
||||
&& decision
|
||||
.helper_journal_id
|
||||
.map(|id| id.to_string())
|
||||
.as_deref()
|
||||
== Some(request.helper_journal_id.as_str())
|
||||
&& decision.helper_checkpoint == request.helper_checkpoint
|
||||
&& request.journal_complete,
|
||||
)?;
|
||||
Ok(StoredSync {
|
||||
decision,
|
||||
response: SyncResponse {
|
||||
decision_bytes: bytes,
|
||||
signature,
|
||||
}
|
||||
.encode_to_vec(),
|
||||
created_at: row.get("created_at"),
|
||||
})
|
||||
}
|
||||
|
||||
fn confirming_application(request: &SyncRequest, prior: &ReconcileDecisionPayload) -> bool {
|
||||
request.previous_request_id.is_empty()
|
||||
&& request.previous_decision_id == prior.decision_id.to_string()
|
||||
&& request.helper_previous_decision_id == prior.decision_id.to_string()
|
||||
&& request.journal_id == prior.journal_id.to_string()
|
||||
&& request.helper_journal_id
|
||||
== prior
|
||||
.helper_journal_id
|
||||
.map(|id| id.to_string())
|
||||
.unwrap_or_default()
|
||||
&& request.checkpoint > prior.checkpoint
|
||||
&& request.helper_checkpoint > prior.helper_checkpoint
|
||||
}
|
||||
|
||||
fn exact_prefix(
|
||||
request: &SyncRequest,
|
||||
decision: &ReconcileDecisionPayload,
|
||||
proof: &UpdateInflightProof,
|
||||
header: &rmm_updater_handoff::Handoff,
|
||||
health_key: [u8; 32],
|
||||
) -> Result<(), Error> {
|
||||
let job = header.attempt.job_id;
|
||||
let attempt = header.attempt.attempt_id;
|
||||
let local = request
|
||||
.attempts
|
||||
.iter()
|
||||
.filter(|a| a.job_id == job.to_string() && a.attempt_id == attempt.to_string())
|
||||
.collect::<Vec<_>>();
|
||||
let signed = decision
|
||||
.attempts
|
||||
.iter()
|
||||
.filter(|a| a.job_id == job && a.attempt_id == attempt)
|
||||
.collect::<Vec<_>>();
|
||||
require(local.len() == 1 && signed.len() == 1 && !decision.ready && !decision.helper_ready)?;
|
||||
let local = local[0];
|
||||
let signed = signed[0];
|
||||
let expected = update::inflight_decision(proof, health_key).map_err(|_| denied())?;
|
||||
require(
|
||||
local.update_handoff_bytes == proof.bytes().map_err(|_| denied())?
|
||||
&& local.job_payload_hash == header.attempt.job_payload_sha256
|
||||
&& local.grant_id == header.attempt.grant_id.to_string()
|
||||
&& local.consumption == "consumed"
|
||||
&& local.state == "running"
|
||||
&& local.original_boot_id == header.boot_id.to_string()
|
||||
&& local.outcome_bytes.is_empty()
|
||||
&& local.outcome_sha256.is_empty()
|
||||
&& local.result.is_none()
|
||||
&& local.result_envelope.is_none()
|
||||
&& signed.update_inflight.as_ref() == Some(&expected)
|
||||
&& signed.grant_id == Some(header.attempt.grant_id)
|
||||
&& signed.authorization_id == Some(header.attempt.authorization_id)
|
||||
&& signed.plan_hash.as_deref() == Some(header.attempt.plan_sha256.as_str())
|
||||
&& signed.job_payload_hash == header.attempt.job_payload_sha256
|
||||
&& signed.action == "update_agent"
|
||||
&& signed.outcome_state == "running"
|
||||
&& !signed.unlock
|
||||
&& signed.outcome_bytes.is_empty()
|
||||
&& signed.outcome_sha256.is_none()
|
||||
&& request.attempts.iter().all(|a| {
|
||||
(a.job_id == job.to_string() && a.attempt_id == attempt.to_string())
|
||||
|| a.update_handoff_bytes.is_empty()
|
||||
})
|
||||
&& decision.attempts.iter().all(|a| {
|
||||
(a.job_id == job && a.attempt_id == attempt)
|
||||
|| (a.update_inflight.is_none()
|
||||
&& matches!(
|
||||
a.outcome_state.as_str(),
|
||||
"accepted" | "succeeded" | "failed" | "cancelled" | "expired"
|
||||
))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async fn issue(
|
||||
State(state): State<crate::State>,
|
||||
Extension(peer): Extension<crate::Peer>,
|
||||
headers: HeaderMap,
|
||||
Json(input): Json<HealthRequest>,
|
||||
) -> Result<Json<SignedHealth>, Error> {
|
||||
require(!headers.contains_key("authorization"))?;
|
||||
let (tenant, agent) = crate::agent_http::identity(&state, peer)
|
||||
.await
|
||||
.map_err(|status| Error(status, "DEVICE_MTLS_REQUIRED"))?;
|
||||
let signer = health_signer()?;
|
||||
let health_public = public_key(&signer)?;
|
||||
require(health_public.as_slice() != state.job_signer.public_key()?.as_slice())?;
|
||||
let health_key_id = hash(&health_public);
|
||||
let (header, journal) =
|
||||
update::verify_inflight_proof(&input.proof, health_public).map_err(|_| denied())?;
|
||||
require(journal.phase() == Phase::ServicesStarted)?;
|
||||
let decoded = input.proof.decode().map_err(|_| denied())?;
|
||||
let last: Event = serde_json::from_slice(decoded.events.last().ok_or_else(denied)?)?;
|
||||
let Evidence::ServicesStarted { services, .. } = &last.evidence else {
|
||||
return Err(denied());
|
||||
};
|
||||
let started_at = last.observed_at;
|
||||
let (applied_request, applied_raw) =
|
||||
message::<SyncRequest>(&input.applied_sync_request_base64url)?;
|
||||
let (confirming_request, confirming_raw) =
|
||||
message::<SyncRequest>(&input.confirming_sync_request_base64url)?;
|
||||
let (inventory, _) = message::<Envelope>(&input.inventory_envelope_base64url)?;
|
||||
let now = Utc::now();
|
||||
let seconds = u64::try_from(now.timestamp()).map_err(|_| denied())?;
|
||||
require(
|
||||
header.attempt.tenant_id == tenant
|
||||
&& header.attempt.agent_id == agent
|
||||
&& header.finish_before == header.consumed_at.checked_add(600).ok_or_else(denied)?
|
||||
&& started_at >= header.consumed_at
|
||||
&& started_at <= seconds
|
||||
&& seconds < header.finish_before
|
||||
&& seconds - started_at < 180,
|
||||
)?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
|
||||
crate::agent_service::control_lock(&mut tx, tenant, agent).await?;
|
||||
let row = sqlx::query("SELECT j.payload,j.payload_hash,j.state,j.result,j.cancel_requested,j.start_reserved,p.payload AS plan_payload,p.plan_hash,z.authorization_id,z.plan_id,z.principal AS approver,g.payload AS permit_payload,g.signature AS permit_signature,i.asset_id,i.enrollment_generation,i.assignment_version,i.connection_generation,i.connection_boot_id,i.sync_ready,i.helper_sync_ready,i.sequence_regressed,i.sync_recovery_required,s.journal_id,s.helper_journal_id,s.decision_id,s.checkpoint,s.helper_checkpoint FROM jobs j JOIN operations o USING(tenant_id,operation_id) JOIN authorizations z ON z.tenant_id=o.tenant_id AND z.authorization_id=o.authorization_id JOIN approval_requests a ON a.tenant_id=o.tenant_id AND a.operation_id=o.operation_id JOIN operation_plans p ON p.tenant_id=a.tenant_id AND p.plan_id=a.plan_id JOIN execution_permits g ON g.tenant_id=j.tenant_id AND g.job_id=j.job_id AND g.attempt_id=j.attempt_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id JOIN agent_sync_state s ON s.tenant_id=i.tenant_id AND s.agent_id=i.agent_id WHERE j.tenant_id=$1 AND j.agent_id=$2 AND j.job_id=$3 AND j.attempt_id=$4 AND g.grant_id=$5 AND a.approval_state='approved' AND z.revoked_at IS NULL AND i.revoked_at IS NULL FOR SHARE OF j,i,z")
|
||||
.bind(tenant).bind(agent).bind(header.attempt.job_id).bind(header.attempt.attempt_id).bind(header.attempt.grant_id)
|
||||
.fetch_optional(&mut *tx).await?.ok_or_else(denied)?;
|
||||
require(
|
||||
matches!(
|
||||
row.get::<String, _>("state").as_str(),
|
||||
"running" | "verifying"
|
||||
) && row.get::<Option<Value>, _>("result").is_none()
|
||||
&& !row.get::<bool, _>("cancel_requested")
|
||||
&& row.get::<bool, _>("start_reserved")
|
||||
&& !row.get::<bool, _>("sync_ready")
|
||||
&& !row.get::<bool, _>("helper_sync_ready")
|
||||
&& !row.get::<bool, _>("sequence_regressed")
|
||||
&& !row.get::<bool, _>("sync_recovery_required")
|
||||
&& row.get::<Uuid, _>("asset_id") == header.attempt.asset_id
|
||||
&& row.get::<i64, _>("enrollment_generation")
|
||||
== i64::try_from(header.attempt.enrollment_generation).map_err(|_| denied())?
|
||||
&& row.get::<i64, _>("assignment_version")
|
||||
== i64::try_from(header.attempt.assignment_version).map_err(|_| denied())?
|
||||
&& row.get::<Option<Uuid>, _>("connection_boot_id") == Some(header.boot_id)
|
||||
&& row.get::<Uuid, _>("journal_id") == header.agent_journal_id
|
||||
&& row.get::<Option<Uuid>, _>("helper_journal_id") == Some(header.helper_journal_id),
|
||||
)?;
|
||||
let generation =
|
||||
u64::try_from(row.get::<i64, _>("connection_generation")).map_err(|_| denied())?;
|
||||
require(generation > header.connection_generation)?;
|
||||
let job_bytes: Vec<u8> = row.get("payload");
|
||||
let job: JobPayload = serde_json::from_slice(&job_bytes)?;
|
||||
let Action::UpdateAgent { update: action } = &job.action else {
|
||||
return Err(denied());
|
||||
};
|
||||
require(
|
||||
action.validate()
|
||||
&& update::handoff_matches_job(&header, &job)
|
||||
&& action.updater.health_public_key_hex == hex::encode(health_public)
|
||||
&& hash(&job_bytes) == row.get::<String, _>("payload_hash")
|
||||
&& action.release_envelope.key_id != health_key_id,
|
||||
)?;
|
||||
let plan_bytes: Vec<u8> = row.get("plan_payload");
|
||||
let plan: PlanPayload = serde_json::from_slice(&plan_bytes)?;
|
||||
crate::agent_updates::validate_job_binding(&plan, &job)?;
|
||||
require(
|
||||
hash(&plan_bytes) == row.get::<String, _>("plan_hash")
|
||||
&& hash(&plan_bytes) == header.attempt.plan_sha256
|
||||
&& plan.plan_id == header.attempt.plan_id
|
||||
&& row.get::<Uuid, _>("plan_id") == plan.plan_id
|
||||
&& row.get::<Uuid, _>("authorization_id") == header.attempt.authorization_id,
|
||||
)?;
|
||||
let permission =
|
||||
crate::root_policy::execution_permission(&job.execution_identity).ok_or_else(denied)?;
|
||||
let approver: String = row.get("approver");
|
||||
for principal in [&plan.principal, &approver] {
|
||||
crate::permissions::require(&mut tx, principal, "updates:apply").await?;
|
||||
crate::permissions::require(&mut tx, principal, permission).await?;
|
||||
}
|
||||
let permit_bytes: Vec<u8> = row.get("permit_payload");
|
||||
let permit_signature: Vec<u8> = row.get("permit_signature");
|
||||
verify_job_signature(&state, &permit_bytes, &permit_signature)?;
|
||||
let permit: ExecutionPermitPayload = serde_json::from_slice(&permit_bytes)?;
|
||||
let original = update::OriginalHelperRequest {
|
||||
job_bytes: &job_bytes,
|
||||
permit_bytes: &permit_bytes,
|
||||
signature: &permit_signature,
|
||||
plan_hash: &permit.plan_hash,
|
||||
authorization_id: permit.authorization_id,
|
||||
};
|
||||
require(
|
||||
permit.domain == "rmm.execution-permit"
|
||||
&& permit.schema_version == 1
|
||||
&& permit.key_id == state.job_signer.key_id
|
||||
&& permit.agent_update_plan_id == Some(plan.plan_id)
|
||||
&& permit.tenant_id == tenant
|
||||
&& permit.asset_id == header.attempt.asset_id
|
||||
&& permit.agent_id == agent
|
||||
&& permit.enrollment_generation == header.attempt.enrollment_generation
|
||||
&& permit.assignment_version == header.attempt.assignment_version
|
||||
&& permit.job_id == header.attempt.job_id
|
||||
&& permit.attempt_id == header.attempt.attempt_id
|
||||
&& permit.execution_identity == job.execution_identity
|
||||
&& permit.grant_id == header.attempt.grant_id
|
||||
&& permit.authorization_id == header.attempt.authorization_id
|
||||
&& permit.plan_hash == header.attempt.plan_sha256
|
||||
&& permit.job_payload_hash == hash(&job_bytes)
|
||||
&& permit.action == "update_agent"
|
||||
&& permit.max_runtime_seconds == 600
|
||||
&& input.proof.delegation.prepared_at >= permit.not_before
|
||||
&& input.proof.delegation.prepared_at < permit.start_before
|
||||
&& hash(&encode(&original)?) == header.attempt.original_request_sha256,
|
||||
)?;
|
||||
let applied = stored_sync(&state, &mut tx, agent, &applied_request, &applied_raw, now).await?;
|
||||
let confirming = stored_sync(
|
||||
&state,
|
||||
&mut tx,
|
||||
agent,
|
||||
&confirming_request,
|
||||
&confirming_raw,
|
||||
now,
|
||||
)
|
||||
.await?;
|
||||
require(
|
||||
confirming_application(&confirming_request, &applied.decision)
|
||||
&& row.get::<Uuid, _>("decision_id") == confirming.decision.decision_id
|
||||
&& row.get::<i64, _>("checkpoint")
|
||||
== i64::try_from(confirming.decision.checkpoint).map_err(|_| denied())?
|
||||
&& row.get::<i64, _>("helper_checkpoint")
|
||||
== i64::try_from(confirming.decision.helper_checkpoint).map_err(|_| denied())?,
|
||||
)?;
|
||||
for (request, saved) in [
|
||||
(&applied_request, &applied),
|
||||
(&confirming_request, &confirming),
|
||||
] {
|
||||
let decision = &saved.decision;
|
||||
require(
|
||||
decision.tenant_id == tenant
|
||||
&& decision.asset_id == header.attempt.asset_id
|
||||
&& decision.agent_id == agent
|
||||
&& decision.enrollment_generation == header.attempt.enrollment_generation
|
||||
&& decision.assignment_version == header.attempt.assignment_version
|
||||
&& request.assignment_version == header.attempt.assignment_version
|
||||
&& decision.boot_id == header.boot_id
|
||||
&& decision.connection_generation == generation
|
||||
&& decision.journal_id == header.agent_journal_id
|
||||
&& decision.helper_journal_id == Some(header.helper_journal_id)
|
||||
&& u64::try_from(decision.issued_at.timestamp()).map_err(|_| denied())?
|
||||
>= started_at
|
||||
&& u64::try_from(saved.created_at.timestamp()).map_err(|_| denied())? >= started_at,
|
||||
)?;
|
||||
exact_prefix(request, decision, &input.proof, &header, health_public)?;
|
||||
}
|
||||
let Payload::InventorySnapshot(snapshot) = inventory.payload.as_ref().ok_or_else(denied)?
|
||||
else {
|
||||
return Err(denied());
|
||||
};
|
||||
require(
|
||||
inventory.protocol_major == 1
|
||||
&& inventory.agent_id == agent.to_string()
|
||||
&& inventory.enrollment_generation == header.attempt.enrollment_generation
|
||||
&& inventory.connection_generation == generation
|
||||
&& inventory.boot_id == header.boot_id.to_string()
|
||||
&& inventory.sequence > 0
|
||||
&& snapshot.schema_version == 1
|
||||
&& snapshot.chunk_count == 1
|
||||
&& snapshot.chunk_index == 0
|
||||
&& snapshot.completeness == "partial"
|
||||
&& snapshot.data.len() <= 512 * 1024
|
||||
&& snapshot.sha256 == hex::decode(hash(&snapshot.data)).map_err(|_| denied())?,
|
||||
)?;
|
||||
let scope = match action.manifest().map_err(|_| denied())?.platform {
|
||||
update::Platform::LinuxX64 => "linux.base",
|
||||
update::Platform::WindowsX64 => "windows.base",
|
||||
};
|
||||
// Health requires a fresh real base inventory of the restarted platform;
|
||||
// a not_configured update observation cannot stand in for that evidence.
|
||||
require(snapshot.scope == scope)?;
|
||||
let inventory_data =
|
||||
rmm_domain::inventory_document::validate_for_scope(&snapshot.data, now, &snapshot.scope)
|
||||
.map_err(|_| denied())?;
|
||||
let mut normalized = inventory.clone();
|
||||
normalized.connection_generation = 0;
|
||||
let inventory_hash = hash(&normalized.encode_to_vec());
|
||||
let inventory_row = sqlx::query("SELECT message_id,observed_at,received_at,source_id,quality,completeness,data FROM observations WHERE tenant_id=$1 AND agent_id=$2 AND boot_id=$3 AND message_id=ANY($4) AND payload_sha256=$5")
|
||||
.bind(tenant).bind(agent).bind(header.boot_id).bind(&applied.decision.acknowledged_message_ids).bind(&inventory_hash)
|
||||
.fetch_all(&mut *tx).await?;
|
||||
require(inventory_row.len() == 1)?;
|
||||
let inventory_row = &inventory_row[0];
|
||||
let inventory_id: Uuid = inventory_row.get("message_id");
|
||||
let observed: DateTime<Utc> = inventory_row.get("observed_at");
|
||||
let received: DateTime<Utc> = inventory_row.get("received_at");
|
||||
require(
|
||||
inventory.message_id == inventory_id.to_string()
|
||||
&& inventory.observed_at == observed.timestamp_millis()
|
||||
&& inventory_row.get::<String, _>("source_id") == snapshot.scope
|
||||
&& inventory_row.get::<String, _>("quality") == "valid"
|
||||
&& inventory_row.get::<String, _>("completeness") == "partial"
|
||||
&& inventory_row.get::<Value, _>("data") == inventory_data
|
||||
&& observed <= received
|
||||
&& received <= applied.created_at
|
||||
&& u64::try_from(observed.timestamp()).map_err(|_| denied())? >= started_at
|
||||
&& u64::try_from(received.timestamp()).map_err(|_| denied())? >= started_at,
|
||||
)?;
|
||||
// All IDs/timestamps below come from the verified retained records. The
|
||||
// confirming server round is an upper bound on actual local apply time,
|
||||
// not a claim to have remotely observed a local clock or OS process.
|
||||
let issued_at = u64::try_from(Utc::now().timestamp()).map_err(|_| denied())?;
|
||||
let valid_until = header
|
||||
.finish_before
|
||||
.min(started_at.checked_add(180).ok_or_else(denied)?)
|
||||
.min(u64::try_from(applied.decision.valid_until.timestamp()).map_err(|_| denied())?)
|
||||
.min(u64::try_from(confirming.decision.valid_until.timestamp()).map_err(|_| denied())?);
|
||||
require(issued_at < valid_until)?;
|
||||
let receipt = HealthReceipt {
|
||||
schema_version: 1,
|
||||
handoff_sha256: input.proof.delegation.handoff_sha256.clone(),
|
||||
owner_nonce: header.owner_nonce,
|
||||
boot_id: header.boot_id,
|
||||
helper_journal_id: header.helper_journal_id,
|
||||
agent_journal_id: header.agent_journal_id,
|
||||
connection_generation: generation,
|
||||
service_processes: services.clone(),
|
||||
ledger_identity_sha256: header.ledger_identity_sha256.clone(),
|
||||
signed_sync_decision_id: applied.decision.decision_id,
|
||||
signed_sync_request_sha256: hash(&applied_raw),
|
||||
signed_sync_response_sha256: hash(&applied.response),
|
||||
signed_sync_applied_at: u64::try_from(confirming.decision.issued_at.timestamp())
|
||||
.map_err(|_| denied())?,
|
||||
ready: false,
|
||||
inventory_message_id: inventory_id,
|
||||
inventory_received_at: u64::try_from(received.timestamp()).map_err(|_| denied())?,
|
||||
inventory_ack_sha256: hash(&applied.response),
|
||||
issued_at,
|
||||
valid_until,
|
||||
};
|
||||
let witness_hash = hash(&encode(&(
|
||||
input.proof.head_sha256.as_str(),
|
||||
hash(&applied_raw),
|
||||
hash(&confirming_raw),
|
||||
inventory_hash,
|
||||
))?);
|
||||
let cached: Option<Value> = sqlx::query_scalar("SELECT data FROM audit_events WHERE event_type='agent_update_health' AND data->>'grant_id'=$1 ORDER BY created_at LIMIT 1")
|
||||
.bind(header.attempt.grant_id.to_string()).fetch_optional(&mut *tx).await?;
|
||||
if let Some(cached) = cached {
|
||||
require(
|
||||
cached["witness_sha256"] == witness_hash && cached["health_key_id"] == health_key_id,
|
||||
)?;
|
||||
let original: SignedHealth = serde_json::from_value(cached["receipt"].clone())?;
|
||||
journal
|
||||
.prepare(
|
||||
issued_at,
|
||||
Evidence::HealthVerified {
|
||||
receipt: original.clone(),
|
||||
},
|
||||
)
|
||||
.map_err(|_| denied())?;
|
||||
tx.commit().await?;
|
||||
return Ok(Json(original));
|
||||
}
|
||||
let payload = encode(&receipt)?;
|
||||
require(payload.len() <= 4096)?;
|
||||
let mut message = HEALTH_DOMAIN.to_vec();
|
||||
message.extend_from_slice(&payload);
|
||||
let signature = Signer::new_without_digest(&signer)
|
||||
.map_err(|_| denied())?
|
||||
.sign_oneshot_to_vec(&message)
|
||||
.map_err(|_| denied())?;
|
||||
let signed = SignedHealth { payload, signature };
|
||||
// Reuse the exact pure kernel verification before publishing any receipt;
|
||||
// prepare is read-only and does not append a local event or mutate a job.
|
||||
journal
|
||||
.prepare(
|
||||
issued_at,
|
||||
Evidence::HealthVerified {
|
||||
receipt: signed.clone(),
|
||||
},
|
||||
)
|
||||
.map_err(|_| denied())?;
|
||||
sqlx::query("INSERT INTO audit_events(tenant_id,event_id,principal,event_type,data) VALUES($1,$2,$3,'agent_update_health',$4)")
|
||||
.bind(tenant).bind(Uuid::new_v4()).bind(format!("agent:{agent}"))
|
||||
.bind(json!({"grant_id":header.attempt.grant_id,"job_id":header.attempt.job_id,"attempt_id":header.attempt.attempt_id,
|
||||
"handoff_sha256":input.proof.delegation.handoff_sha256,"head_sha256":input.proof.head_sha256,"event_count":decoded.events.len(),
|
||||
"health_key_id":health_key_id,"witness_sha256":witness_hash,"receipt":signed,"evidence_only":true,"ready":false}))
|
||||
.execute(&mut *tx).await?;
|
||||
tx.commit().await?;
|
||||
Ok(Json(signed))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn original_protobuf_bytes_are_bounded_and_canonical() {
|
||||
let request = SyncRequest {
|
||||
request_id: Uuid::new_v4().to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
let raw = request.encode_to_vec();
|
||||
assert!(message::<SyncRequest>(&URL_SAFE_NO_PAD.encode(&raw)).is_ok());
|
||||
let mut duplicate = raw.clone();
|
||||
duplicate.extend_from_slice(&raw);
|
||||
assert!(message::<SyncRequest>(&URL_SAFE_NO_PAD.encode(duplicate)).is_err());
|
||||
assert!(message::<SyncRequest>("").is_err());
|
||||
assert!(message::<SyncRequest>(&"A".repeat(ENCODED_LIMIT + 1)).is_err());
|
||||
assert!(message::<SyncRequest>(&format!("{}=", URL_SAFE_NO_PAD.encode(raw))).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_request_or_agent_only_ack_never_proves_helper_application() {
|
||||
let decision_id = Uuid::new_v4();
|
||||
let agent_journal = Uuid::new_v4();
|
||||
let helper_journal = Uuid::new_v4();
|
||||
let prior: ReconcileDecisionPayload = serde_json::from_value(json!({
|
||||
"domain":"rmm.reconciliation-decision", "schema_version":1,
|
||||
"decision_id":decision_id, "request_id":Uuid::new_v4(), "request_hash":"ab".repeat(32),
|
||||
"tenant_id":Uuid::new_v4(), "asset_id":Uuid::new_v4(), "agent_id":Uuid::new_v4(),
|
||||
"enrollment_generation":1, "assignment_version":1, "connection_generation":2,
|
||||
"boot_id":Uuid::new_v4(), "journal_id":agent_journal, "checkpoint":10,
|
||||
"helper_journal_id":helper_journal, "helper_checkpoint":20,
|
||||
"issued_at":"2026-11-14T12:00:00Z", "valid_until":"2026-11-14T12:01:00Z",
|
||||
"ready":false, "helper_ready":false, "reason":"AGENT_UPDATE_INFLIGHT",
|
||||
"attempts":[], "acknowledged_message_ids":[], "key_id":"cd".repeat(32)
|
||||
}))
|
||||
.unwrap();
|
||||
let original = SyncRequest {
|
||||
previous_decision_id: decision_id.to_string(),
|
||||
helper_previous_decision_id: decision_id.to_string(),
|
||||
journal_id: agent_journal.to_string(),
|
||||
helper_journal_id: helper_journal.to_string(),
|
||||
checkpoint: 11,
|
||||
helper_checkpoint: 21,
|
||||
..Default::default()
|
||||
};
|
||||
assert!(confirming_application(&original, &prior));
|
||||
let mut only_agent = original.clone();
|
||||
only_agent.helper_previous_decision_id.clear();
|
||||
assert!(!confirming_application(&only_agent, &prior));
|
||||
let mut pending = original.clone();
|
||||
pending.previous_request_id = prior.request_id.to_string();
|
||||
assert!(!confirming_application(&pending, &prior));
|
||||
let mut stale_helper = original.clone();
|
||||
stale_helper.helper_checkpoint = prior.helper_checkpoint;
|
||||
assert!(!confirming_application(&stale_helper, &prior));
|
||||
let mut replaced_helper = original;
|
||||
replaced_helper.helper_journal_id = Uuid::new_v4().to_string();
|
||||
assert!(!confirming_application(&replaced_helper, &prior));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
//! Separate deny-only opt-in. Legacy Root/SCM/MSI/script policies grant no update.
|
||||
use crate::jobs::Error;
|
||||
use axum::http::StatusCode;
|
||||
use chrono::{DateTime, Utc};
|
||||
use rmm_domain::{
|
||||
agent_update::{ResolvedAgentUpdate, digest},
|
||||
jobs::{Action, JobPayload},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use sqlx::{Postgres, Transaction};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Policy {
|
||||
schema_version: u32,
|
||||
profile: String,
|
||||
expires_at: DateTime<Utc>,
|
||||
scopes: Vec<Scope>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Scope {
|
||||
tenant_id: Uuid,
|
||||
asset_id: Uuid,
|
||||
agent_id: Uuid,
|
||||
enrollment_generation: u64,
|
||||
assignment_version: u64,
|
||||
principals: Vec<String>,
|
||||
helper_policy_sha256: String,
|
||||
helper_binary_sha256: String,
|
||||
updater_binary_sha256: String,
|
||||
updater_policy_sha256: String,
|
||||
keyring_generation: u64,
|
||||
keyring_sha256: String,
|
||||
health_public_key_hex: String,
|
||||
health_key_pin_sha256: String,
|
||||
allowed_manifest_sha256: Vec<String>,
|
||||
allowed_predecessor_record_sha256: Vec<String>,
|
||||
acceptance_evidence_id: String,
|
||||
}
|
||||
impl Scope {
|
||||
fn valid(&self) -> bool {
|
||||
!self.tenant_id.is_nil()
|
||||
&& !self.asset_id.is_nil()
|
||||
&& !self.agent_id.is_nil()
|
||||
&& self.enrollment_generation > 0
|
||||
&& self.assignment_version > 0
|
||||
&& self.keyring_generation > 0
|
||||
&& !self.principals.is_empty()
|
||||
&& self.principals.len() <= 16
|
||||
&& self
|
||||
.principals
|
||||
.iter()
|
||||
.all(|p| !p.is_empty() && p.len() <= 256)
|
||||
&& [
|
||||
&self.helper_policy_sha256,
|
||||
&self.helper_binary_sha256,
|
||||
&self.updater_binary_sha256,
|
||||
&self.updater_policy_sha256,
|
||||
&self.keyring_sha256,
|
||||
&self.health_public_key_hex,
|
||||
&self.health_key_pin_sha256,
|
||||
]
|
||||
.iter()
|
||||
.all(|h| digest(h))
|
||||
&& [
|
||||
&self.allowed_manifest_sha256,
|
||||
&self.allowed_predecessor_record_sha256,
|
||||
]
|
||||
.iter()
|
||||
.all(|list| {
|
||||
!list.is_empty()
|
||||
&& list.len() <= 16
|
||||
&& list.iter().all(|h| digest(h))
|
||||
&& list.iter().collect::<std::collections::BTreeSet<_>>().len() == list.len()
|
||||
})
|
||||
&& !self.acceptance_evidence_id.trim().is_empty()
|
||||
&& self.acceptance_evidence_id.len() <= 512
|
||||
}
|
||||
fn permits(
|
||||
&self,
|
||||
principal: &str,
|
||||
job: &JobPayload,
|
||||
update: &ResolvedAgentUpdate,
|
||||
caps: &[String],
|
||||
) -> bool {
|
||||
if !self.valid()
|
||||
|| !update.validate()
|
||||
|| !job.action.available_for(&job.execution_identity, caps)
|
||||
|| !job
|
||||
.action
|
||||
.valid_job_limits(job.max_runtime_seconds, job.max_output_bytes)
|
||||
|| update.execution_identity() != Some(job.execution_identity.as_str())
|
||||
|| self.tenant_id != job.tenant_id
|
||||
|| self.asset_id != job.asset_id
|
||||
|| self.agent_id != job.agent_id
|
||||
|| self.enrollment_generation != job.enrollment_generation
|
||||
|| self.assignment_version != job.assignment_version
|
||||
|| !self.principals.iter().any(|p| p == principal)
|
||||
|| self.helper_binary_sha256 != update.predecessor.helper_sha256
|
||||
|| self.updater_binary_sha256 != update.updater.binary_sha256
|
||||
|| self.updater_policy_sha256 != update.updater.policy_sha256
|
||||
|| self.keyring_generation != update.updater.keyring_generation
|
||||
|| self.keyring_sha256 != update.updater.keyring_sha256
|
||||
|| self.health_public_key_hex != update.updater.health_public_key_hex
|
||||
|| self.health_key_pin_sha256 != update.updater.health_key_pin_sha256
|
||||
|| !self
|
||||
.allowed_manifest_sha256
|
||||
.contains(&update.manifest_sha256)
|
||||
|| !self
|
||||
.allowed_predecessor_record_sha256
|
||||
.contains(&update.predecessor.installed_record_sha256)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let helper = match job.execution_identity.as_str() {
|
||||
"root" => "helper.root.v1",
|
||||
"system" => "helper.system.v1",
|
||||
_ => return false,
|
||||
};
|
||||
[
|
||||
"update_agent".into(),
|
||||
"agent.update.apply/1".into(),
|
||||
"rmm.sync.v1".into(),
|
||||
helper.into(),
|
||||
format!("{}:update_agent", job.execution_identity),
|
||||
format!("helper.policy.sha256:{}", self.helper_policy_sha256),
|
||||
format!("helper.binary.sha256:{}", self.helper_binary_sha256),
|
||||
format!("updater.binary.sha256:{}", self.updater_binary_sha256),
|
||||
format!("updater.policy.sha256:{}", self.updater_policy_sha256),
|
||||
]
|
||||
.iter()
|
||||
.all(|needed| caps.contains(needed))
|
||||
}
|
||||
}
|
||||
pub async fn authorize(
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
principal: &str,
|
||||
job: &JobPayload,
|
||||
) -> Result<(), Error> {
|
||||
let Action::UpdateAgent { update } = &job.action else {
|
||||
return Err(Error(StatusCode::FORBIDDEN, "AGENT_UPDATE_POLICY_DENIED"));
|
||||
};
|
||||
let identity = crate::root_policy::execution_permission(&job.execution_identity)
|
||||
.ok_or(Error(StatusCode::FORBIDDEN, "EXECUTION_IDENTITY_DENIED"))?;
|
||||
crate::permissions::require(tx, principal, identity).await?;
|
||||
crate::permissions::require(tx, principal, "updates:apply").await?;
|
||||
if std::env::var("RMM_PROFILE").as_deref() != Ok("development") {
|
||||
return Err(Error(StatusCode::CONFLICT, "AGENT_UPDATE_NOT_CONFIGURED"));
|
||||
}
|
||||
let path = std::env::var("RMM_AGENT_UPDATE_POLICY_FILE")
|
||||
.map_err(|_| Error(StatusCode::CONFLICT, "AGENT_UPDATE_NOT_CONFIGURED"))?;
|
||||
let raw = crate::agent_updates::protected_file(std::path::Path::new(&path), 65536)?;
|
||||
let policy: Policy = serde_json::from_slice(&raw)
|
||||
.map_err(|_| Error(StatusCode::CONFLICT, "AGENT_UPDATE_POLICY_INVALID"))?;
|
||||
if policy.schema_version != 1
|
||||
|| policy.profile != "development"
|
||||
|| policy.expires_at <= Utc::now()
|
||||
|| policy.scopes.is_empty()
|
||||
|| policy.scopes.len() > 64
|
||||
|| policy.scopes.iter().any(|s| !s.valid())
|
||||
{
|
||||
return Err(Error(StatusCode::CONFLICT, "AGENT_UPDATE_POLICY_DENIED"));
|
||||
}
|
||||
let caps:serde_json::Value=sqlx::query_scalar("SELECT capabilities FROM agent_installations WHERE agent_id=$1 AND asset_id=$2 AND revoked_at IS NULL AND enrollment_generation=$3 AND assignment_version=$4")
|
||||
.bind(job.agent_id).bind(job.asset_id).bind(i64::try_from(job.enrollment_generation).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?)
|
||||
.bind(i64::try_from(job.assignment_version).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).fetch_optional(&mut **tx).await?.ok_or(Error(StatusCode::CONFLICT,"AUTHORIZATION_REVOKED"))?;
|
||||
let caps: Vec<String> = serde_json::from_value(caps)?;
|
||||
if !policy
|
||||
.scopes
|
||||
.iter()
|
||||
.any(|scope| scope.permits(principal, job, update, &caps))
|
||||
{
|
||||
return Err(Error(StatusCode::FORBIDDEN, "AGENT_UPDATE_POLICY_DENIED"));
|
||||
}
|
||||
crate::agent_updates::check_update(tx, update).await?;
|
||||
crate::agent_updates::validate_current_target(tx, job).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,873 @@
|
||||
//! 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());
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,10 @@ pub async fn references(
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
action: &rmm_domain::jobs::Action,
|
||||
) -> Result<Vec<rmm_domain::jobs::RecipeReference>, Error> {
|
||||
if let rmm_domain::jobs::Action::UpdateAgent { update } = action {
|
||||
crate::agent_updates::check_update(tx, update).await?;
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
if let Some(manifest) = action.windows_msi() {
|
||||
if !action.validate() {
|
||||
return Err(Error(
|
||||
|
||||
@@ -507,7 +507,10 @@ pub async fn permit(
|
||||
let payload: JobPayload = serde_json::from_slice(&row.get::<Vec<u8>, _>("payload"))?;
|
||||
let plan: PlanPayload = serde_json::from_slice(&row.get::<Vec<u8>, _>("plan_payload"))?;
|
||||
let capabilities: Vec<String> = serde_json::from_value(row.get("capabilities"))?;
|
||||
if crate::windows_msi_catalog::validate_job_binding(&plan, &payload).is_err() {
|
||||
if crate::windows_msi_catalog::validate_job_binding(&plan, &payload).is_err()
|
||||
|| (matches!(payload.action, Action::UpdateAgent { .. })
|
||||
&& crate::agent_updates::validate_job_binding(&plan, &payload).is_err())
|
||||
{
|
||||
return Ok(denied("WINDOWS_MSI_BINDING_MISMATCH"));
|
||||
}
|
||||
if row.get::<Uuid, _>("bound_asset") != payload.asset_id
|
||||
@@ -624,9 +627,20 @@ pub async fn permit(
|
||||
.filter(|r| r.job_id == payload.job_id && r.attempt_id == payload.attempt_id)
|
||||
.map(|r| r.artifact.artifact_id)
|
||||
.collect::<Vec<_>>();
|
||||
let artifacts = crate::catalog::resolve_artifacts(&mut tx, tenant, &artifact_ids)
|
||||
let artifacts = if let Action::UpdateAgent { update } = &payload.action {
|
||||
crate::agent_updates::resolve_artifacts(
|
||||
&mut tx,
|
||||
tenant,
|
||||
&update
|
||||
.manifest()
|
||||
.map_err(|_| anyhow::anyhow!("invalid agent release"))?
|
||||
.artifacts,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("artifact unavailable"))?;
|
||||
} else {
|
||||
crate::catalog::resolve_artifacts(&mut tx, tenant, &artifact_ids).await
|
||||
}
|
||||
.map_err(|_| anyhow::anyhow!("artifact unavailable"))?;
|
||||
if plan
|
||||
.artifact_references
|
||||
.iter()
|
||||
@@ -646,6 +660,9 @@ pub async fn permit(
|
||||
|| (matches!(payload.action, Action::RunScript { .. })
|
||||
&& (!crate::permissions::allowed(&mut tx, &plan.principal, "scripts:execute").await?
|
||||
|| !crate::permissions::allowed(&mut tx, &approver, "scripts:execute").await?))
|
||||
|| (matches!(payload.action, Action::UpdateAgent { .. })
|
||||
&& (!crate::permissions::allowed(&mut tx, &plan.principal, "updates:apply").await?
|
||||
|| !crate::permissions::allowed(&mut tx, &approver, "updates:apply").await?))
|
||||
{
|
||||
return Ok(denied("AUTHORIZATION_REVOKED"));
|
||||
}
|
||||
@@ -692,6 +709,8 @@ pub async fn permit(
|
||||
return Ok(denied("RESOURCE_LOCKED"));
|
||||
}
|
||||
let grant = ExecutionPermitPayload {
|
||||
agent_update_plan_id: matches!(payload.action, Action::UpdateAgent { .. })
|
||||
.then_some(plan.plan_id),
|
||||
domain: "rmm.execution-permit".into(),
|
||||
schema_version: 1,
|
||||
grant_id: Uuid::new_v4(),
|
||||
@@ -756,11 +775,15 @@ pub async fn next(
|
||||
};
|
||||
let payload: JobPayload = serde_json::from_slice(&row.get::<Vec<u8>, _>("payload"))?;
|
||||
let caps: Vec<String> = serde_json::from_value(row.get("capabilities"))?;
|
||||
if payload.action.windows_msi().is_some() {
|
||||
if payload.action.windows_msi().is_some()
|
||||
|| matches!(payload.action, Action::UpdateAgent { .. })
|
||||
{
|
||||
let raw: Vec<u8> = row.get("plan_payload");
|
||||
let plan: PlanPayload = serde_json::from_slice(&raw)?;
|
||||
if hash(&raw) != row.get::<String, _>("plan_hash")
|
||||
|| crate::windows_msi_catalog::validate_job_binding(&plan, &payload).is_err()
|
||||
|| (matches!(payload.action, Action::UpdateAgent { .. })
|
||||
&& crate::agent_updates::validate_job_binding(&plan, &payload).is_err())
|
||||
|| crate::catalog::check(&mut tx, &payload.action)
|
||||
.await
|
||||
.is_err()
|
||||
|
||||
+179
-13
@@ -121,11 +121,36 @@ pub struct PlanInput {
|
||||
#[derive(Clone, Deserialize, schemars::JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
pub enum PlanRequest {
|
||||
AgentUpdate(Box<AgentUpdatePlanInput>),
|
||||
Typed(Box<PlanInput>),
|
||||
Web(Box<WebPlanInput>),
|
||||
}
|
||||
#[derive(Clone, Deserialize, schemars::JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct AgentUpdatePlanInput {
|
||||
tenant_id: Uuid,
|
||||
action: AgentUpdateRequestAction,
|
||||
targets: Targets,
|
||||
parameters: AgentUpdateParameters,
|
||||
schedule: WebSchedule,
|
||||
rollout: Rollout,
|
||||
reason: String,
|
||||
}
|
||||
#[derive(Clone, Deserialize, schemars::JsonSchema)]
|
||||
pub enum AgentUpdateRequestAction {
|
||||
#[serde(rename = "update_agent")]
|
||||
UpdateAgent,
|
||||
}
|
||||
#[derive(Clone, Deserialize, schemars::JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct AgentUpdateParameters {
|
||||
release_id: Uuid,
|
||||
execution_identity: String,
|
||||
timeout_seconds: u32,
|
||||
max_output_bytes: u32,
|
||||
}
|
||||
#[derive(Clone, Deserialize, schemars::JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct WebPlanInput {
|
||||
tenant_id: Uuid,
|
||||
action: String,
|
||||
@@ -204,6 +229,12 @@ struct WebParameters {
|
||||
impl PlanRequest {
|
||||
pub fn resolve(self, tenant: Uuid) -> Result<PlanInput> {
|
||||
let w = match self {
|
||||
Self::AgentUpdate(_) => {
|
||||
return Err(Error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"AGENT_UPDATE_RELEASE_ID_REQUIRED",
|
||||
));
|
||||
}
|
||||
Self::Typed(t) => return Ok(*t),
|
||||
Self::Web(w) => *w,
|
||||
};
|
||||
@@ -325,6 +356,68 @@ impl PlanRequest {
|
||||
})
|
||||
}
|
||||
}
|
||||
/// Both HTTP and MCP resolve update jobs only from a fixed catalog release and
|
||||
/// saved authenticated per-device observations. Caller-supplied local pins are
|
||||
/// never an alternative route to the typed immutable execution payload.
|
||||
pub async fn plan_request(
|
||||
state: &crate::State,
|
||||
p: &Principal,
|
||||
tenant: Uuid,
|
||||
request: PlanRequest,
|
||||
) -> Result<Value> {
|
||||
let PlanRequest::AgentUpdate(input) = request else {
|
||||
return plan(state, p, tenant, request.resolve(tenant)?).await;
|
||||
};
|
||||
scope(p, tenant, true)?;
|
||||
if input.tenant_id != tenant {
|
||||
return Err(missing());
|
||||
}
|
||||
if input.schedule.offline_policy != "wait_until_deadline"
|
||||
|| input.parameters.release_id.is_nil()
|
||||
|| input.targets.asset_ids.is_empty()
|
||||
|| input.targets.asset_ids.len() > 100
|
||||
{
|
||||
return Err(fail("VALIDATION_ERROR"));
|
||||
}
|
||||
let _ = &input.action;
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
|
||||
crate::permissions::require_principal(&mut tx, p, "operations:plan").await?;
|
||||
crate::permissions::require_principal(&mut tx, p, "updates:apply").await?;
|
||||
let mut updates = std::collections::BTreeMap::new();
|
||||
for asset in &input.targets.asset_ids {
|
||||
let update =
|
||||
crate::agent_updates::resolve_target(&mut tx, *asset, input.parameters.release_id)
|
||||
.await?;
|
||||
if update.execution_identity() != Some(input.parameters.execution_identity.as_str()) {
|
||||
return Err(fail("EXECUTION_IDENTITY_DENIED"));
|
||||
}
|
||||
updates.insert(*asset, update);
|
||||
}
|
||||
tx.commit().await?;
|
||||
let first = updates
|
||||
.values()
|
||||
.next()
|
||||
.ok_or_else(|| fail("NOT_FOUND"))?
|
||||
.clone();
|
||||
let resolved = PlanInput {
|
||||
targets: input.targets,
|
||||
action: Action::UpdateAgent {
|
||||
update: Box::new(first),
|
||||
},
|
||||
script_variants: Vec::new(),
|
||||
execution_identity: input.parameters.execution_identity,
|
||||
schedule: Schedule {
|
||||
not_before: input.schedule.not_before,
|
||||
start_before: input.schedule.start_before,
|
||||
},
|
||||
max_runtime_seconds: input.parameters.timeout_seconds,
|
||||
max_output_bytes: input.parameters.max_output_bytes,
|
||||
rollout: input.rollout,
|
||||
reason: input.reason,
|
||||
artifact_ids: Vec::new(),
|
||||
};
|
||||
plan_resolved(state, p, tenant, resolved, Some(updates)).await
|
||||
}
|
||||
fn validate_variants(variants: &[Variant]) -> Result<()> {
|
||||
if variants.is_empty() || variants.len() > 2 {
|
||||
return Err(fail("AMBIGUOUS_TARGET"));
|
||||
@@ -427,6 +520,23 @@ pub async fn plan(
|
||||
p: &Principal,
|
||||
tenant: Uuid,
|
||||
input: PlanInput,
|
||||
) -> Result<Value> {
|
||||
if matches!(input.action, Action::UpdateAgent { .. }) {
|
||||
return Err(Error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"AGENT_UPDATE_RELEASE_ID_REQUIRED",
|
||||
));
|
||||
}
|
||||
plan_resolved(state, p, tenant, input, None).await
|
||||
}
|
||||
async fn plan_resolved(
|
||||
state: &crate::State,
|
||||
p: &Principal,
|
||||
tenant: Uuid,
|
||||
input: PlanInput,
|
||||
updates: Option<
|
||||
std::collections::BTreeMap<Uuid, rmm_domain::agent_update::ResolvedAgentUpdate>,
|
||||
>,
|
||||
) -> Result<Value> {
|
||||
scope(p, tenant, true)?;
|
||||
let now = Utc::now();
|
||||
@@ -495,11 +605,27 @@ pub async fn plan(
|
||||
if matches!(input.action, Action::RunScript { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "scripts:execute").await?;
|
||||
}
|
||||
if matches!(input.action, Action::UpdateAgent { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "updates:apply").await?;
|
||||
}
|
||||
if matches!(input.action, Action::QueryLogs { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "logs:read").await?;
|
||||
}
|
||||
let recipe_references = crate::catalog::references(&mut tx, &input.action).await?;
|
||||
let artifacts = if input.action.windows_msi().is_some() {
|
||||
let artifacts = if let Action::UpdateAgent { update } = &input.action {
|
||||
if !input.artifact_ids.is_empty() {
|
||||
return Err(fail("AGENT_UPDATE_EXACT_ARTIFACT_REQUIRED"));
|
||||
}
|
||||
crate::agent_updates::resolve_artifacts(
|
||||
&mut tx,
|
||||
tenant,
|
||||
&update
|
||||
.manifest()
|
||||
.map_err(|_| fail("INVALID_AGENT_RELEASE"))?
|
||||
.artifacts,
|
||||
)
|
||||
.await?
|
||||
} else if input.action.windows_msi().is_some() {
|
||||
crate::windows_msi_catalog::automatic_artifacts(&mut tx, tenant, &input.action).await?
|
||||
} else {
|
||||
crate::catalog::resolve_artifacts(&mut tx, tenant, &input.artifact_ids).await?
|
||||
@@ -516,7 +642,18 @@ pub async fn plan(
|
||||
}
|
||||
let row = &rows[0];
|
||||
let caps = capabilities(row.get("capabilities"))?;
|
||||
let action = target_action(&input, &caps)?;
|
||||
let action = if let Some(updates) = &updates {
|
||||
Action::UpdateAgent {
|
||||
update: Box::new(
|
||||
updates
|
||||
.get(&asset)
|
||||
.ok_or_else(|| fail("AGENT_UPDATE_STATE_NOT_CONFIGURED"))?
|
||||
.clone(),
|
||||
),
|
||||
}
|
||||
} else {
|
||||
target_action(&input, &caps)?
|
||||
};
|
||||
if !action.valid_job_limits(input.max_runtime_seconds, input.max_output_bytes) {
|
||||
return Err(fail("VALIDATION_ERROR"));
|
||||
}
|
||||
@@ -577,6 +714,7 @@ pub async fn plan(
|
||||
artifact_references,
|
||||
};
|
||||
crate::windows_msi_catalog::validate_plan_bindings(&payload)?;
|
||||
crate::agent_updates::validate_plan_bindings(&payload)?;
|
||||
let bytes = encode(&payload)?;
|
||||
let plan_hash = hash(&bytes);
|
||||
sqlx::query("INSERT INTO operation_plans(tenant_id,plan_id,principal,payload,plan_hash,expires_at) VALUES($1,$2,$3,$4,$5,$6)").bind(tenant).bind(payload.plan_id).bind(&p.name).bind(&bytes).bind(&plan_hash).bind(input.schedule.start_before).execute(&mut *tx).await?;
|
||||
@@ -612,17 +750,32 @@ async fn load_plan(
|
||||
}
|
||||
async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Result<()> {
|
||||
crate::windows_msi_catalog::validate_plan_bindings(plan)?;
|
||||
crate::agent_updates::validate_plan_bindings(plan)?;
|
||||
policy(tx).await?;
|
||||
let current_artifacts = crate::catalog::resolve_artifacts(
|
||||
tx,
|
||||
plan.tenant_id,
|
||||
&plan
|
||||
.artifact_references
|
||||
.iter()
|
||||
.map(|r| r.artifact.artifact_id)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?;
|
||||
let current_artifacts = if let Some(Action::UpdateAgent { update }) =
|
||||
plan.jobs.first().map(|j| &j.payload.action)
|
||||
{
|
||||
crate::agent_updates::resolve_artifacts(
|
||||
tx,
|
||||
plan.tenant_id,
|
||||
&update
|
||||
.manifest()
|
||||
.map_err(|_| fail("INVALID_AGENT_RELEASE"))?
|
||||
.artifacts,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
crate::catalog::resolve_artifacts(
|
||||
tx,
|
||||
plan.tenant_id,
|
||||
&plan
|
||||
.artifact_references
|
||||
.iter()
|
||||
.map(|r| r.artifact.artifact_id)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?
|
||||
};
|
||||
for reference in &plan.artifact_references {
|
||||
if !current_artifacts.contains(&reference.artifact)
|
||||
|| !plan.jobs.iter().any(|j| {
|
||||
@@ -640,6 +793,13 @@ async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Resu
|
||||
{
|
||||
crate::permissions::require(tx, &plan.principal, "scripts:execute").await?;
|
||||
}
|
||||
if plan
|
||||
.jobs
|
||||
.iter()
|
||||
.any(|j| matches!(j.payload.action, Action::UpdateAgent { .. }))
|
||||
{
|
||||
crate::permissions::require(tx, &plan.principal, "updates:apply").await?;
|
||||
}
|
||||
if plan
|
||||
.jobs
|
||||
.iter()
|
||||
@@ -736,6 +896,9 @@ pub async fn submit(
|
||||
if matches!(job.payload.action, Action::RunScript { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "scripts:execute").await?;
|
||||
}
|
||||
if matches!(job.payload.action, Action::UpdateAgent { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "updates:apply").await?;
|
||||
}
|
||||
if matches!(job.payload.action, Action::QueryLogs { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "logs:read").await?;
|
||||
}
|
||||
@@ -847,6 +1010,9 @@ async fn authorize_http(
|
||||
if matches!(job.payload.action, Action::RunScript { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, &p, "scripts:execute").await?;
|
||||
}
|
||||
if matches!(job.payload.action, Action::UpdateAgent { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, &p, "updates:apply").await?;
|
||||
}
|
||||
if let Some(permission) =
|
||||
crate::root_policy::execution_permission(&job.payload.execution_identity)
|
||||
{
|
||||
@@ -993,7 +1159,7 @@ async fn plan_http(
|
||||
Json(i): Json<PlanRequest>,
|
||||
) -> Result<Json<Value>> {
|
||||
let p = principal(&s, &h, true).await?;
|
||||
Ok(Json(plan(&s, &p, t, i.resolve(t)?).await?))
|
||||
Ok(Json(plan_request(&s, &p, t, i).await?))
|
||||
}
|
||||
async fn submit_http(
|
||||
State(s): State<crate::State>,
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
mod agent_artifacts;
|
||||
mod agent_http;
|
||||
mod agent_service;
|
||||
mod agent_update_health;
|
||||
mod agent_update_policy;
|
||||
mod agent_updates;
|
||||
mod api;
|
||||
mod artifact_store;
|
||||
mod auth;
|
||||
@@ -21,6 +24,7 @@ mod root_policy;
|
||||
mod scheduler;
|
||||
mod synchronization;
|
||||
mod tls;
|
||||
mod update_artifact_store;
|
||||
mod windows_msi_catalog;
|
||||
mod windows_policy;
|
||||
|
||||
@@ -157,6 +161,8 @@ async fn main() -> Result<()> {
|
||||
}
|
||||
let mut routes = Router::new().route("/healthz", get(|| async { "ok" }));
|
||||
if role.gateway() {
|
||||
routes = agent_updates::gateway_routes(routes);
|
||||
routes = agent_update_health::gateway_routes(routes);
|
||||
routes = routes
|
||||
.route("/agent/v1/enroll", post(gateway::enroll))
|
||||
.route("/agent/v1/messages", post(agent_http::messages))
|
||||
@@ -189,6 +195,7 @@ async fn main() -> Result<()> {
|
||||
routes = jobs::routes(routes);
|
||||
routes = controls::routes(routes);
|
||||
routes = catalog::routes(routes);
|
||||
routes = agent_updates::routes(routes);
|
||||
routes = routes
|
||||
.route("/api/v1/tenants/{tenant}/health", get(monitoring::health))
|
||||
.route(
|
||||
|
||||
@@ -91,6 +91,10 @@ fn tools() -> Vec<Tool> {
|
||||
"inventory.get_device",
|
||||
"Read stored inventory of one authorized device.",
|
||||
),
|
||||
tool::<Devices>(
|
||||
"updates.list_releases",
|
||||
"Read immutable signed agent release metadata for the authorized tenant; never approves or applies an update.",
|
||||
),
|
||||
tool::<Tenant>(
|
||||
"monitoring.get_health",
|
||||
"Stored presence and health; unknown is not healthy.",
|
||||
@@ -206,7 +210,20 @@ impl Adapter {
|
||||
}
|
||||
"operations.plan" => {
|
||||
let i: Plan = parse(args)?;
|
||||
crate::jobs::plan(&self.state, &p, i.tenant_id, i.input.resolve(i.tenant_id)?).await
|
||||
crate::jobs::plan_request(&self.state, &p, i.tenant_id, i.input).await
|
||||
}
|
||||
"updates.list_releases" => {
|
||||
let i: Devices = parse(args)?;
|
||||
crate::agent_updates::list_for(
|
||||
&self.state,
|
||||
&p,
|
||||
i.tenant_id,
|
||||
crate::pagination::PageQuery {
|
||||
cursor: i.cursor,
|
||||
limit: i.limit,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
"operations.submit" => {
|
||||
let i: Submit = parse(args)?;
|
||||
|
||||
@@ -326,6 +326,9 @@ pub async fn authorize(
|
||||
principal: &str,
|
||||
job: &JobPayload,
|
||||
) -> Result<(), Error> {
|
||||
if matches!(job.action, Action::UpdateAgent { .. }) {
|
||||
return crate::agent_update_policy::authorize(tx, principal, job).await;
|
||||
}
|
||||
match job.execution_identity.as_str() {
|
||||
"system" => return crate::windows_policy::authorize(tx, principal, job).await,
|
||||
"agent_service" => return Ok(()),
|
||||
|
||||
@@ -372,6 +372,23 @@ pub async fn reconcile(
|
||||
reason = "HELPER_CHECKPOINT_REQUIRED".into();
|
||||
chain_valid = false;
|
||||
}
|
||||
let previous_payload: Option<Vec<u8>> = if let Some(old) = &previous {
|
||||
sqlx::query_scalar(
|
||||
"SELECT payload FROM sync_checkpoints WHERE agent_id=$1 AND decision_id=$2",
|
||||
)
|
||||
.bind(agent)
|
||||
.bind(old.get::<Uuid, _>("decision_id"))
|
||||
.fetch_optional(&mut **tx)
|
||||
.await?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let previous_update_decisions: Vec<AttemptDecision> = previous_payload
|
||||
.as_deref()
|
||||
.map(serde_json::from_slice::<ReconcileDecisionPayload>)
|
||||
.transpose()?
|
||||
.map(|p| p.attempts)
|
||||
.unwrap_or_default();
|
||||
let mut locals = BTreeMap::new();
|
||||
for local in &r.attempts {
|
||||
let id = Uuid::parse_str(&local.job_id)?;
|
||||
@@ -389,6 +406,10 @@ pub async fn reconcile(
|
||||
"invalid attempt summary"
|
||||
);
|
||||
i64::try_from(local.revision)?;
|
||||
if !local.update_handoff_bytes.is_empty() {
|
||||
rmm_domain::agent_update::UpdateInflightProof::from_bytes(&local.update_handoff_bytes)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
}
|
||||
ensure!(
|
||||
matches!(
|
||||
local.state.as_str(),
|
||||
@@ -451,6 +472,7 @@ pub async fn reconcile(
|
||||
Some(local.outcome_sha256.clone())
|
||||
};
|
||||
let mut decision = AttemptDecision {
|
||||
update_inflight: None,
|
||||
job_id,
|
||||
attempt_id: attempt,
|
||||
job_payload_hash: local.job_payload_hash.clone(),
|
||||
@@ -510,7 +532,7 @@ pub async fn reconcile(
|
||||
ready = false;
|
||||
}
|
||||
let grant = sqlx::query(
|
||||
"SELECT grant_id,payload,created_at FROM execution_permits WHERE job_id=$1 AND attempt_id=$2",
|
||||
"SELECT grant_id,payload,signature,created_at FROM execution_permits WHERE job_id=$1 AND attempt_id=$2",
|
||||
)
|
||||
.bind(job_id)
|
||||
.bind(attempt)
|
||||
@@ -554,6 +576,138 @@ pub async fn reconcile(
|
||||
decisions.push(decision);
|
||||
continue;
|
||||
}
|
||||
if !local.update_handoff_bytes.is_empty() {
|
||||
use rmm_domain::agent_update as update;
|
||||
ensure!(
|
||||
chain_valid
|
||||
&& r.journal_complete
|
||||
&& reserved
|
||||
&& grant_matches
|
||||
&& helper_journal.is_some()
|
||||
&& matches!(
|
||||
server_state.as_str(),
|
||||
"queued" | "accepted" | "running" | "verifying"
|
||||
)
|
||||
&& row.get::<Option<Value>, _>("result").is_none()
|
||||
&& local.state == "running"
|
||||
&& local.consumption == "consumed"
|
||||
&& local.outcome_bytes.is_empty()
|
||||
&& local.outcome_sha256.is_empty()
|
||||
&& local.result.is_none()
|
||||
&& local.result_envelope.is_none(),
|
||||
"UPDATE_INFLIGHT_NOT_ORIGINAL_ACTIVE_ATTEMPT"
|
||||
);
|
||||
let proof = update::UpdateInflightProof::from_bytes(&local.update_handoff_bytes)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
let key = update::job_health_public_key(&payload).map_err(anyhow::Error::msg)?;
|
||||
let (header, _) =
|
||||
update::verify_inflight_proof(&proof, key).map_err(anyhow::Error::msg)?;
|
||||
let grant = grant.as_ref().context("UPDATE_ORIGINAL_GRANT_REQUIRED")?;
|
||||
let permit_bytes: Vec<u8> = grant.get("payload");
|
||||
let signature: Vec<u8> = grant.get("signature");
|
||||
let permit: ExecutionPermitPayload = serde_json::from_slice(&permit_bytes)?;
|
||||
let job_bytes: Vec<u8> = row.get("payload");
|
||||
let original = update::OriginalHelperRequest {
|
||||
job_bytes: &job_bytes,
|
||||
permit_bytes: &permit_bytes,
|
||||
signature: &signature,
|
||||
plan_hash: &permit.plan_hash,
|
||||
authorization_id: permit.authorization_id,
|
||||
};
|
||||
let authorized_plan:Option<Uuid>=sqlx::query_scalar("SELECT plan_id FROM authorizations WHERE tenant_id=$1 AND authorization_id=$2 AND plan_hash=$3")
|
||||
.bind(tenant).bind(permit.authorization_id).bind(&permit.plan_hash).fetch_optional(&mut **tx).await?;
|
||||
let original_sync: Vec<u8> = sqlx::query_scalar(
|
||||
"SELECT payload FROM sync_checkpoints WHERE agent_id=$1 AND decision_id=$2",
|
||||
)
|
||||
.bind(agent)
|
||||
.bind(header.previous_decision_id)
|
||||
.fetch_optional(&mut **tx)
|
||||
.await?
|
||||
.context("UPDATE_ORIGINAL_READY_SYNC_REQUIRED")?;
|
||||
let original_sync: ReconcileDecisionPayload = serde_json::from_slice(&original_sync)?;
|
||||
ensure!(
|
||||
original_sync.ready
|
||||
&& original_sync.helper_ready
|
||||
&& original_sync.agent_id == agent
|
||||
&& original_sync.tenant_id == tenant
|
||||
&& original_sync.journal_id == header.agent_journal_id
|
||||
&& original_sync.helper_journal_id == Some(header.helper_journal_id)
|
||||
&& original_sync.helper_checkpoint == header.helper_checkpoint
|
||||
&& original_sync.boot_id == header.boot_id
|
||||
&& original_sync.connection_generation == header.connection_generation
|
||||
&& original_sync.issued_at <= proof.delegation.prepared_at
|
||||
&& proof.delegation.prepared_at < original_sync.valid_until,
|
||||
"UPDATE_ORIGINAL_READY_SYNC_BINDING"
|
||||
);
|
||||
let seconds = u64::try_from(Utc::now().timestamp())?;
|
||||
ensure!(
|
||||
update::handoff_matches_job(&header, &payload)
|
||||
&& header.boot_id == boot
|
||||
&& header.attempt.plan_id
|
||||
== authorized_plan.context("UPDATE_ORIGINAL_PLAN_REQUIRED")?
|
||||
&& permit.agent_update_plan_id == Some(header.attempt.plan_id)
|
||||
&& header.agent_journal_id == journal_id
|
||||
&& Some(header.helper_journal_id) == helper_journal
|
||||
&& header.helper_checkpoint <= r.helper_checkpoint
|
||||
&& header.connection_generation <= e.connection_generation
|
||||
&& header.attempt.grant_id == permit.grant_id
|
||||
&& header.attempt.authorization_id == permit.authorization_id
|
||||
&& header.attempt.plan_sha256 == permit.plan_hash
|
||||
&& header.attempt.job_payload_sha256 == permit.job_payload_hash
|
||||
&& header.attempt.original_request_sha256
|
||||
== hash(&serde_json::to_vec(&original)?)
|
||||
&& permit.action == "update_agent"
|
||||
&& permit.max_runtime_seconds == 600
|
||||
&& permit.key_id == state.job_signer.key_id
|
||||
&& header.finish_before
|
||||
== header
|
||||
.consumed_at
|
||||
.checked_add(600)
|
||||
.context("UPDATE_DEADLINE_OVERFLOW")?
|
||||
&& header.consumed_at <= seconds
|
||||
&& seconds < header.finish_before
|
||||
&& proof.delegation.prepared_at >= permit.not_before
|
||||
&& proof.delegation.prepared_at < permit.start_before
|
||||
&& local.original_boot_id == boot.to_string(),
|
||||
"UPDATE_INFLIGHT_ORIGINAL_BINDING"
|
||||
);
|
||||
let ack = update::inflight_decision(&proof, key).map_err(anyhow::Error::msg)?;
|
||||
if let Some(old) = previous_update_decisions
|
||||
.iter()
|
||||
.find(|d| d.job_id == job_id && d.attempt_id == attempt)
|
||||
.and_then(|d| d.update_inflight.as_ref())
|
||||
{
|
||||
let decoded = proof.decode().map_err(anyhow::Error::msg)?;
|
||||
ensure!(
|
||||
old.handoff_sha256 == ack.handoff_sha256
|
||||
&& old.delegation_sha256 == ack.delegation_sha256
|
||||
&& old.owner_nonce == ack.owner_nonce
|
||||
&& old.finish_before == ack.finish_before
|
||||
&& (hash(&decoded.handoff_record) == old.head_sha256
|
||||
|| decoded
|
||||
.events
|
||||
.iter()
|
||||
.any(|bytes| hash(bytes) == old.head_sha256)),
|
||||
"UPDATE_INFLIGHT_PREFIX_REGRESSION"
|
||||
);
|
||||
}
|
||||
let revision = local.revision.max(row.get::<i64, _>("revision") as u64);
|
||||
sqlx::query(
|
||||
"UPDATE jobs SET state='running',revision=GREATEST(revision,$2) WHERE job_id=$1",
|
||||
)
|
||||
.bind(job_id)
|
||||
.bind(i64::try_from(revision)?)
|
||||
.execute(&mut **tx)
|
||||
.await?;
|
||||
decision.revision = revision;
|
||||
decision.outcome_state = "running".into();
|
||||
decision.update_inflight = Some(ack);
|
||||
decision.reason = "AGENT_UPDATE_INFLIGHT".into();
|
||||
decision.unlock = false;
|
||||
ready = false;
|
||||
decisions.push(decision);
|
||||
continue;
|
||||
}
|
||||
// A free script with no bound postcondition never becomes successful by
|
||||
// merely asserting a new state after unknown effect.
|
||||
let manual = server_state == "unknown"
|
||||
@@ -1286,6 +1440,7 @@ mod tests {
|
||||
#[test]
|
||||
fn accepted_is_not_a_new_attempt_or_grant() {
|
||||
let mut local = SyncAttempt {
|
||||
update_handoff_bytes: Vec::new(),
|
||||
state: "accepted".into(),
|
||||
consumption: "unconsumed".into(),
|
||||
..Default::default()
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
//! Separate bounded binary store. Existing 256KiB package artifacts are unchanged.
|
||||
//! A completed object is published only after complete length/hash verification.
|
||||
use anyhow::{Result, ensure};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
fs::{self, File, OpenOptions},
|
||||
io::{Read, Seek, SeekFrom, Write},
|
||||
os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub const MAX_CHUNK_BYTES: u32 = 512 * 1024;
|
||||
pub struct UpdateArtifacts {
|
||||
root: PathBuf,
|
||||
owner: u32,
|
||||
}
|
||||
pub struct Staging {
|
||||
file: File,
|
||||
temporary: PathBuf,
|
||||
final_path: PathBuf,
|
||||
parent: PathBuf,
|
||||
expected_hash: String,
|
||||
expected_size: u64,
|
||||
written: u64,
|
||||
hasher: Sha256,
|
||||
}
|
||||
fn descriptor(hash: &str, bytes: u64) -> bool {
|
||||
rmm_domain::agent_update::digest(hash) && (1..=rmm_updater::MAX_ARTIFACT_BYTES).contains(&bytes)
|
||||
}
|
||||
fn directory(path: &Path, owner: u32) -> Result<()> {
|
||||
let m = fs::symlink_metadata(path)?;
|
||||
ensure!(
|
||||
m.is_dir() && m.uid() == owner && m.mode() & 0o777 == 0o700,
|
||||
"private update store directory"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
fn regular(file: &File, owner: u32, expected_size: u64) -> Result<fs::Metadata> {
|
||||
let m = file.metadata()?;
|
||||
ensure!(
|
||||
m.is_file()
|
||||
&& m.uid() == owner
|
||||
&& m.mode() & 0o777 == 0o600
|
||||
&& m.nlink() == 1
|
||||
&& m.len() == expected_size,
|
||||
"immutable update object metadata"
|
||||
);
|
||||
Ok(m)
|
||||
}
|
||||
impl UpdateArtifacts {
|
||||
pub fn new(profile: &str, root: PathBuf, owner: u32) -> Result<Self> {
|
||||
ensure!(
|
||||
profile == "development",
|
||||
"update object adapter is development-only"
|
||||
);
|
||||
match fs::create_dir(&root) {
|
||||
Ok(()) => fs::set_permissions(&root, fs::Permissions::from_mode(0o700))?,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
directory(&root, owner)?;
|
||||
Ok(Self { root, owner })
|
||||
}
|
||||
fn parent(&self, tenant: Uuid) -> Result<PathBuf> {
|
||||
ensure!(!tenant.is_nil(), "update tenant identity");
|
||||
directory(&self.root, self.owner)?;
|
||||
let parent = self.root.join(tenant.to_string());
|
||||
match fs::create_dir(&parent) {
|
||||
Ok(()) => {
|
||||
fs::set_permissions(&parent, fs::Permissions::from_mode(0o700))?;
|
||||
File::open(&self.root)?.sync_all()?;
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
directory(&parent, self.owner)?;
|
||||
Ok(parent)
|
||||
}
|
||||
pub fn begin(&self, tenant: Uuid, hash: &str, bytes: u64) -> Result<Staging> {
|
||||
ensure!(descriptor(hash, bytes), "bounded update object descriptor");
|
||||
let parent = self.parent(tenant)?;
|
||||
let temporary = parent.join(format!(".staging-{}", Uuid::new_v4()));
|
||||
let file = OpenOptions::new()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.mode(0o600)
|
||||
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
|
||||
.open(&temporary)?;
|
||||
Ok(Staging {
|
||||
file,
|
||||
temporary,
|
||||
final_path: parent.join(hash),
|
||||
parent,
|
||||
expected_hash: hash.into(),
|
||||
expected_size: bytes,
|
||||
written: 0,
|
||||
hasher: Sha256::new(),
|
||||
})
|
||||
}
|
||||
fn verified_file(&self, tenant: Uuid, hash: &str, bytes: u64) -> Result<File> {
|
||||
ensure!(descriptor(hash, bytes), "bounded update object descriptor");
|
||||
let parent = self.parent(tenant)?;
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
|
||||
.open(parent.join(hash))?;
|
||||
let before = regular(&file, self.owner, bytes)?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0_u8; 32768];
|
||||
let mut seen = 0;
|
||||
loop {
|
||||
let n = file.read(&mut buffer)?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
seen += n as u64;
|
||||
ensure!(seen <= bytes, "update object additional bytes");
|
||||
hasher.update(&buffer[..n]);
|
||||
}
|
||||
let after = regular(&file, self.owner, bytes)?;
|
||||
ensure!(
|
||||
seen == bytes
|
||||
&& hex::encode(hasher.finalize()) == hash
|
||||
&& (
|
||||
before.dev(),
|
||||
before.ino(),
|
||||
before.ctime(),
|
||||
before.ctime_nsec(),
|
||||
before.mtime(),
|
||||
before.mtime_nsec()
|
||||
) == (
|
||||
after.dev(),
|
||||
after.ino(),
|
||||
after.ctime(),
|
||||
after.ctime_nsec(),
|
||||
after.mtime(),
|
||||
after.mtime_nsec()
|
||||
),
|
||||
"complete immutable update object integrity"
|
||||
);
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
Ok(file)
|
||||
}
|
||||
pub fn verify(&self, tenant: Uuid, hash: &str, bytes: u64) -> Result<()> {
|
||||
self.verified_file(tenant, hash, bytes).map(|_| ())
|
||||
}
|
||||
pub fn read_chunk(
|
||||
&self,
|
||||
tenant: Uuid,
|
||||
hash: &str,
|
||||
bytes: u64,
|
||||
offset: u64,
|
||||
length: u32,
|
||||
) -> Result<Vec<u8>> {
|
||||
ensure!(
|
||||
offset < bytes && (1..=MAX_CHUNK_BYTES).contains(&length),
|
||||
"bounded update range"
|
||||
);
|
||||
let mut file = self.verified_file(tenant, hash, bytes)?;
|
||||
let before = regular(&file, self.owner, bytes)?;
|
||||
file.seek(SeekFrom::Start(offset))?;
|
||||
let mut chunk = vec![0; (bytes - offset).min(length as u64) as usize];
|
||||
file.read_exact(&mut chunk)?;
|
||||
let after = regular(&file, self.owner, bytes)?;
|
||||
ensure!(
|
||||
(
|
||||
before.dev(),
|
||||
before.ino(),
|
||||
before.ctime(),
|
||||
before.ctime_nsec(),
|
||||
before.mtime(),
|
||||
before.mtime_nsec()
|
||||
) == (
|
||||
after.dev(),
|
||||
after.ino(),
|
||||
after.ctime(),
|
||||
after.ctime_nsec(),
|
||||
after.mtime(),
|
||||
after.mtime_nsec()
|
||||
),
|
||||
"update object changed during bounded range"
|
||||
);
|
||||
Ok(chunk)
|
||||
}
|
||||
}
|
||||
impl Staging {
|
||||
pub fn append(&mut self, chunk: &[u8]) -> Result<()> {
|
||||
ensure!(
|
||||
!chunk.is_empty()
|
||||
&& chunk.len() <= MAX_CHUNK_BYTES as usize
|
||||
&& self.written + chunk.len() as u64 <= self.expected_size,
|
||||
"bounded update import chunk"
|
||||
);
|
||||
self.file.write_all(chunk)?;
|
||||
self.hasher.update(chunk);
|
||||
self.written += chunk.len() as u64;
|
||||
Ok(())
|
||||
}
|
||||
pub fn finish(mut self, store: &UpdateArtifacts, tenant: Uuid) -> Result<()> {
|
||||
ensure!(
|
||||
self.written == self.expected_size
|
||||
&& hex::encode(self.hasher.clone().finalize()) == self.expected_hash,
|
||||
"complete update import hash/length"
|
||||
);
|
||||
self.file.flush()?;
|
||||
self.file.sync_all()?;
|
||||
if let Err(e) = fs::hard_link(&self.temporary, &self.final_path) {
|
||||
ensure!(
|
||||
e.kind() == std::io::ErrorKind::AlreadyExists,
|
||||
"update publication failed"
|
||||
);
|
||||
store.verify(tenant, &self.expected_hash, self.expected_size)?;
|
||||
}
|
||||
fs::remove_file(&self.temporary)?;
|
||||
File::open(&self.parent)?.sync_all()?;
|
||||
store.verify(tenant, &self.expected_hash, self.expected_size)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl Drop for Staging {
|
||||
fn drop(&mut self) {
|
||||
// Only this create-once temporary object, never a published object/job.
|
||||
let _ = fs::remove_file(&self.temporary);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn large_update_stream_preserves_legacy_bound_and_denies_corruption_or_foreign_tenant() {
|
||||
let root = std::env::temp_dir().join(format!("rmm-update-store-{}", Uuid::new_v4()));
|
||||
fs::create_dir(&root).unwrap();
|
||||
fs::set_permissions(&root, fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let owner = fs::metadata(&root).unwrap().uid();
|
||||
let store = UpdateArtifacts::new("development", root.clone(), owner).unwrap();
|
||||
assert!(UpdateArtifacts::new("production", root.clone(), owner).is_err());
|
||||
let tenant = Uuid::new_v4();
|
||||
let bytes = vec![37_u8; 300_007];
|
||||
let hash = rmm_domain::jobs::hash(&bytes);
|
||||
let mut staging = store.begin(tenant, &hash, bytes.len() as u64).unwrap();
|
||||
for chunk in bytes.chunks(32768) {
|
||||
staging.append(chunk).unwrap();
|
||||
}
|
||||
staging.finish(&store, tenant).unwrap();
|
||||
assert_eq!(
|
||||
store
|
||||
.read_chunk(tenant, &hash, bytes.len() as u64, 299_999, 16)
|
||||
.unwrap(),
|
||||
bytes[299_999..]
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.read_chunk(Uuid::new_v4(), &hash, bytes.len() as u64, 0, 32)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.read_chunk(tenant, &hash, bytes.len() as u64, bytes.len() as u64, 1)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.read_chunk(tenant, &hash, bytes.len() as u64, 0, MAX_CHUNK_BYTES + 1)
|
||||
.is_err()
|
||||
);
|
||||
assert!(store.begin(tenant, "../escape", 1).is_err());
|
||||
// An incomplete object is never published, including a clean early EOF.
|
||||
let truncated = rmm_domain::jobs::hash(b"truncated");
|
||||
let mut short = store.begin(tenant, &truncated, 9).unwrap();
|
||||
short.append(b"trunc").unwrap();
|
||||
assert!(short.finish(&store, tenant).is_err());
|
||||
assert!(!root.join(tenant.to_string()).join(truncated).exists());
|
||||
let object = root.join(tenant.to_string()).join(&hash);
|
||||
fs::write(&object, vec![38_u8; bytes.len()]).unwrap();
|
||||
assert!(store.verify(tenant, &hash, bytes.len() as u64).is_err());
|
||||
// Existing small package storage retains its own 256KiB boundary.
|
||||
use crate::artifact_store::ArtifactStore;
|
||||
let legacy =
|
||||
crate::artifact_store::LocalArtifacts::new("development", root.join("legacy")).unwrap();
|
||||
assert!(legacy.put(tenant, &bytes).is_err());
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user