612 lines
29 KiB
Rust
612 lines
29 KiB
Rust
//! 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())?;
|
|
let rollback = journal.phase() == Phase::RollbackServicesStarted;
|
|
require(journal.phase() == Phase::ServicesStarted || rollback)?;
|
|
let decoded = input.proof.decode().map_err(|_| denied())?;
|
|
let last: Event = serde_json::from_slice(decoded.events.last().ok_or_else(denied)?)?;
|
|
let services = match &last.evidence {
|
|
Evidence::ServicesStarted { services, .. } if !rollback => services,
|
|
Evidence::RollbackServicesStarted { services, .. } if rollback => services,
|
|
_ => 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 | update::Platform::WindowsGnuX64 => "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 code_generation = if rollback { "predecessor" } else { "candidate" };
|
|
let cached: Option<Value> = sqlx::query_scalar("SELECT data FROM audit_events WHERE event_type='agent_update_health' AND data->>'grant_id'=$1 AND COALESCE(data->>'code_generation','candidate')=$2 ORDER BY created_at LIMIT 1")
|
|
.bind(header.attempt.grant_id.to_string()).bind(code_generation).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,
|
|
if rollback {
|
|
Evidence::RollbackHealthVerified {
|
|
receipt: original.clone(),
|
|
}
|
|
} else {
|
|
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,
|
|
if rollback {
|
|
Evidence::RollbackHealthVerified {
|
|
receipt: signed.clone(),
|
|
}
|
|
} else {
|
|
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,"code_generation":code_generation}))
|
|
.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));
|
|
}
|
|
}
|