Files
rust-discovery/crates/domain/src/windows_echo_probe.rs
T
2026-11-15 23:50:29 +01:00

168 lines
6.9 KiB
Rust

//! One LocalService echo on the named Windows development guest.
use crate::{
checkpoint_repair::inventory_sha256,
jobs::{Action, JobPayload, PlanPayload, WindowsExecutionProfile, encode, hash},
};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;
pub const DOMAIN: &str = "rmm.dev.windows-echo-probe.v1";
pub const SCRIPT: &str = "Write-Output 'Zeitprofil-Test'\r\n";
pub const OUTPUT: &str = "Zeitprofil-Test\r\n";
pub const AGENT: Uuid = Uuid::from_u128(0x63ea15b20643403db5b4cba1288dbea3);
pub const ASSET: Uuid = Uuid::from_u128(0xa176520e8a914e93a78b8e7620f430e0);
pub const TENANT: Uuid = Uuid::from_u128(0x11111111111141118111111111111111);
pub const TEST_PRINCIPAL: &str = "simulated-testprincipal-a";
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ProbeScope {
pub tenant_id: Uuid,
pub asset_id: Uuid,
pub agent_id: Uuid,
pub operation_id: Uuid,
pub plan_id: Uuid,
pub plan_hash: String,
pub job_id: Uuid,
pub attempt_id: Uuid,
pub job_payload_hash: String,
pub script_sha256: String,
pub enrollment_generation: u64,
pub assignment_version: u64,
pub execution_identity: String,
pub max_runtime_seconds: u32,
pub max_output_bytes: u32,
pub not_before: DateTime<Utc>,
pub start_before: DateTime<Utc>,
}
fn fixed_job(p: &JobPayload) -> bool {
p.schema_version == 1
&& (p.tenant_id, p.asset_id, p.agent_id) == (TENANT, ASSET, AGENT)
&& (p.enrollment_generation, p.assignment_version) == (1, 1)
&& p.execution_identity == "agent_service"
&& !p.job_id.is_nil()
&& !p.attempt_id.is_nil()
&& p.job_id != p.attempt_id
&& (p.max_runtime_seconds, p.max_output_bytes) == (60, 65536)
&& p.not_before < p.start_before
&& p.start_before - p.not_before <= Duration::minutes(15)
&& matches!(&p.action, Action::RunScript {
interpreter, script_utf8, arguments, expected_stdout: Some(output),
windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned),
privileged_script_profile: None,
} if interpreter=="powershell" && script_utf8==SCRIPT && arguments.is_empty() && output==OUTPUT)
}
impl ProbeScope {
pub fn from_plan(plan: &PlanPayload, operation_id: Uuid) -> Option<Self> {
let [job] = plan.jobs.as_slice() else {
return None;
};
let p = &job.payload;
if plan.schema_version != 1
|| plan.tenant_id != TENANT
|| plan.plan_id.is_nil()
|| operation_id.is_nil()
|| plan.principal != format!("automation:{TENANT}")
|| (plan.max_parallel, plan.halt_after_failures) != (1, 1)
|| !plan.recipe_references.is_empty()
|| !plan.artifact_references.is_empty()
|| !fixed_job(p)
|| hash(&encode(p).ok()?) != job.job_payload_hash
|| job.script_sha256.as_deref() != Some(hash(SCRIPT.as_bytes()).as_str())
{
return None;
}
Some(Self {
tenant_id: p.tenant_id,
asset_id: p.asset_id,
agent_id: p.agent_id,
operation_id,
plan_id: plan.plan_id,
plan_hash: hash(&encode(plan).ok()?),
job_id: p.job_id,
attempt_id: p.attempt_id,
job_payload_hash: job.job_payload_hash.clone(),
script_sha256: hash(SCRIPT.as_bytes()),
enrollment_generation: p.enrollment_generation,
assignment_version: p.assignment_version,
execution_identity: p.execution_identity.clone(),
max_runtime_seconds: p.max_runtime_seconds,
max_output_bytes: p.max_output_bytes,
not_before: p.not_before,
start_before: p.start_before,
})
}
pub fn matches_job(&self, p: &JobPayload) -> bool {
fixed_job(p)
&& !self.operation_id.is_nil()
&& !self.plan_id.is_nil()
&& self.plan_hash.len() == 64
&& self
.plan_hash
.bytes()
.all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c))
&& (self.tenant_id, self.asset_id, self.agent_id)
== (p.tenant_id, p.asset_id, p.agent_id)
&& (self.enrollment_generation, self.assignment_version)
== (p.enrollment_generation, p.assignment_version)
&& (self.job_id, self.attempt_id) == (p.job_id, p.attempt_id)
&& self.execution_identity == p.execution_identity
&& (self.max_runtime_seconds, self.max_output_bytes)
== (p.max_runtime_seconds, p.max_output_bytes)
&& (self.not_before, self.start_before) == (p.not_before, p.start_before)
&& self.script_sha256 == hash(SCRIPT.as_bytes())
&& encode(p).is_ok_and(|b| hash(&b) == self.job_payload_hash)
}
pub fn digest(&self) -> Option<String> {
inventory_sha256(&serde_json::to_value(self).ok()?).ok()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProbeReceipt {
pub domain: String,
pub schema_version: u32,
pub request_id: Uuid,
pub receipt_id: Uuid,
pub witness: Value,
pub witness_sha256: String,
pub scope: ProbeScope,
pub scope_sha256: String,
pub job_payload_bytes: Vec<u8>,
pub key_id: String,
pub issued_at: DateTime<Utc>,
pub valid_until: DateTime<Utc>,
pub confirmed_by: String,
pub approval_origin: String,
pub testsimulation: bool,
}
impl ProbeReceipt {
/// The caller separately verifies the signature and installed, immutable scope.
pub fn matches_reviewed_witness(&self, expected: &Value, now: DateTime<Utc>) -> bool {
self.domain == DOMAIN
&& self.schema_version == 1
&& !self.request_id.is_nil()
&& !self.receipt_id.is_nil()
&& self.request_id != self.receipt_id
&& &self.witness == expected
&& self.key_id == expected["verification_key_id"]
&& inventory_sha256(expected).is_ok_and(|v| v == self.witness_sha256)
&& self.testsimulation
&& self.approval_origin == "dev_test_browser_simulation"
&& self.confirmed_by == TEST_PRINCIPAL
&& self.issued_at <= now
&& now < self.valid_until
&& self.valid_until <= self.scope.start_before
&& self.valid_until - self.issued_at <= Duration::minutes(10)
&& self.scope.digest().as_deref() == Some(&self.scope_sha256)
&& hash(&self.job_payload_bytes) == self.scope.job_payload_hash
&& serde_json::from_slice::<JobPayload>(&self.job_payload_bytes).is_ok_and(|j| {
self.scope.matches_job(&j) && encode(&j).is_ok_and(|b| b == self.job_payload_bytes)
})
}
}