//! Exact historical helper request/binding codecs and signed permit rules. use crate::crypto::{DefaultVerifier, Ed25519Verifier}; use anyhow::{Result, ensure}; use chrono::{DateTime, Utc}; use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload, hash}; use serde::{Deserialize, Serialize}; use uuid::Uuid; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Binding { pub tenant_id: Uuid, pub asset_id: Uuid, pub agent_id: Uuid, pub enrollment_generation: u64, pub assignment_version: u64, pub public_key_hex: String, } pub type AuthBinding = Binding; impl Binding { pub fn job(&self, bytes: &[u8]) -> Result { ensure!(bytes.len() <= 65536, "JOB_SIZE"); let j: JobPayload = serde_json::from_slice(bytes)?; ensure!( j.schema_version == 1 && j.tenant_id == self.tenant_id && j.asset_id == self.asset_id && j.agent_id == self.agent_id && j.enrollment_generation == self.enrollment_generation && j.assignment_version == self.assignment_version, "IDENTITY_BINDING" ); ensure!( j.action.validate() && (1..=3600).contains(&j.max_runtime_seconds) && (1..=262144).contains(&j.max_output_bytes) && j.not_before < j.start_before, "INVALID_JOB" ); Ok(j) } pub fn verify( &self, request: &Request, now: DateTime, ) -> Result<(JobPayload, ExecutionPermitPayload)> { self.verify_with(request, now, &DefaultVerifier::default()) } pub fn verify_with( &self, request: &Request, now: DateTime, verifier: &impl Ed25519Verifier, ) -> Result<(JobPayload, ExecutionPermitPayload)> { let job = self.job(&request.job_bytes)?; ensure!( request.permit_bytes.len() <= 8192 && request.signature.len() == 64, "PERMIT_SIZE" ); let raw = hex::decode(&self.public_key_hex)?; ensure!(raw.len() == 32, "TRUST_KEY_SIZE"); ensure!( verifier.verify(&raw, &request.permit_bytes, &request.signature)?, "INVALID_SIGNATURE" ); let p: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?; ensure!( p.domain == "rmm.execution-permit" && p.schema_version == 1 && p.key_id == hash(&raw) && !p.grant_id.is_nil() && !p.authorization_id.is_nil(), "PERMIT_DOMAIN" ); ensure!( p.authorization_id == request.authorization_id && p.plan_hash == request.plan_hash && p.plan_hash.len() == 64 && hex::decode(&p.plan_hash)?.len() == 32 && p.job_payload_hash == hash(&request.job_bytes) && p.tenant_id == job.tenant_id && p.asset_id == job.asset_id && p.agent_id == job.agent_id && p.enrollment_generation == job.enrollment_generation && p.assignment_version == job.assignment_version && p.job_id == job.job_id && p.attempt_id == job.attempt_id && p.action == job.action.name() && p.execution_identity == job.execution_identity && p.max_runtime_seconds == job.max_runtime_seconds, "PERMIT_BINDING" ); ensure!( p.not_before >= job.not_before && p.start_before <= job.start_before && p.not_before < p.start_before && (p.start_before - p.not_before).num_seconds() <= 60 && now >= p.not_before && now < p.start_before, "PERMIT_TIME" ); Ok((job, p)) } } #[derive(Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Request { pub job_bytes: Vec, pub permit_bytes: Vec, pub signature: Vec, pub plan_hash: String, pub authorization_id: Uuid, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Outcome { pub state: String, pub stdout: Vec, pub stderr: Vec, pub exit_code: Option, pub output_truncated: bool, pub reboot_required: bool, pub evidence: serde_json::Value, pub error_code: String, } impl Outcome { pub fn unknown(code: &str) -> Self { Self { state: "unknown".into(), stdout: vec![], stderr: vec![], exit_code: None, output_truncated: false, reboot_required: false, evidence: serde_json::json!({"postconditions_passed":false}), error_code: code.into(), } } }