167 lines
5.8 KiB
Rust
167 lines
5.8 KiB
Rust
//! Signed reconciliation is NOT an execution permit. Verify the signature over
|
|
//! exact stored bytes first, then all bindings/time/checkpoint and local hashes.
|
|
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct ReconciledOutcome {
|
|
pub state: String,
|
|
pub stdout: Vec<u8>,
|
|
pub stderr: Vec<u8>,
|
|
pub exit_code: Option<i32>,
|
|
pub output_truncated: bool,
|
|
pub reboot_required: bool,
|
|
pub evidence: serde_json::Value,
|
|
pub error_code: String,
|
|
}
|
|
/// Codec 1: struct field order, explicit nulls, raw output bytes unchanged;
|
|
/// recursively lexical evidence-object keys, independent of serde feature flags.
|
|
pub fn canonical_outcome_bytes(outcome: &ReconciledOutcome) -> Result<Vec<u8>, serde_json::Error> {
|
|
fn ordered(value: serde_json::Value) -> serde_json::Value {
|
|
match value {
|
|
serde_json::Value::Object(map) => {
|
|
let mut fields = map.into_iter().collect::<Vec<_>>();
|
|
fields.sort_by(|a, b| a.0.cmp(&b.0));
|
|
let mut sorted = serde_json::Map::new();
|
|
for (key, value) in fields {
|
|
sorted.insert(key, ordered(value));
|
|
}
|
|
serde_json::Value::Object(sorted)
|
|
}
|
|
serde_json::Value::Array(values) => {
|
|
serde_json::Value::Array(values.into_iter().map(ordered).collect())
|
|
}
|
|
other => other,
|
|
}
|
|
}
|
|
let mut value = outcome.clone();
|
|
value.evidence = ordered(value.evidence);
|
|
serde_json::to_vec(&value)
|
|
}
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct AttemptDecision {
|
|
pub job_id: Uuid,
|
|
pub attempt_id: Uuid,
|
|
pub job_payload_hash: String,
|
|
pub grant_id: Option<Uuid>,
|
|
pub authorization_id: Option<Uuid>,
|
|
pub plan_hash: Option<String>,
|
|
pub action: String,
|
|
pub execution_identity: String,
|
|
pub revision: u64,
|
|
pub outcome_state: String,
|
|
pub previous_outcome_sha256: Option<String>,
|
|
pub outcome_sha256: Option<String>,
|
|
pub outcome_bytes: Vec<u8>,
|
|
pub original_boot_id: Option<Uuid>,
|
|
pub unlock: bool,
|
|
pub reason: String,
|
|
}
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct ReconcileDecisionPayload {
|
|
pub domain: String,
|
|
pub schema_version: u32,
|
|
pub decision_id: Uuid,
|
|
pub request_id: Uuid,
|
|
pub request_hash: String,
|
|
pub tenant_id: Uuid,
|
|
pub asset_id: Uuid,
|
|
pub agent_id: Uuid,
|
|
pub enrollment_generation: u64,
|
|
pub assignment_version: u64,
|
|
pub connection_generation: u64,
|
|
pub boot_id: Uuid,
|
|
pub journal_id: Uuid,
|
|
pub checkpoint: u64,
|
|
pub helper_journal_id: Option<Uuid>,
|
|
pub helper_checkpoint: u64,
|
|
pub issued_at: DateTime<Utc>,
|
|
pub valid_until: DateTime<Utc>,
|
|
pub ready: bool,
|
|
pub helper_ready: bool,
|
|
pub reason: String,
|
|
pub attempts: Vec<AttemptDecision>,
|
|
pub acknowledged_message_ids: Vec<Uuid>,
|
|
pub key_id: String,
|
|
}
|
|
impl ReconcileDecisionPayload {
|
|
pub fn valid_profile(&self, now: DateTime<Utc>) -> bool {
|
|
self.domain == "rmm.reconciliation-decision"
|
|
&& self.schema_version == 1
|
|
&& !self.decision_id.is_nil()
|
|
&& !self.request_id.is_nil()
|
|
&& self.issued_at <= now
|
|
&& now < self.valid_until
|
|
&& self.valid_until > self.issued_at
|
|
&& (self.valid_until - self.issued_at).num_seconds() <= 60
|
|
&& self.attempts.len() <= 256
|
|
&& self.acknowledged_message_ids.len() <= 256
|
|
&& (!self.helper_ready || self.ready)
|
|
}
|
|
}
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn reconciliation_cannot_be_a_permit_or_unbounded_ticket() {
|
|
let id = Uuid::new_v4();
|
|
let now = Utc::now();
|
|
let mut d = ReconcileDecisionPayload {
|
|
domain: "rmm.reconciliation-decision".into(),
|
|
schema_version: 1,
|
|
decision_id: id,
|
|
request_id: id,
|
|
request_hash: "ab".repeat(32),
|
|
tenant_id: id,
|
|
asset_id: id,
|
|
agent_id: id,
|
|
enrollment_generation: 1,
|
|
assignment_version: 1,
|
|
connection_generation: 1,
|
|
boot_id: id,
|
|
journal_id: id,
|
|
checkpoint: 1,
|
|
helper_journal_id: None,
|
|
helper_checkpoint: 0,
|
|
issued_at: now,
|
|
valid_until: now + chrono::Duration::seconds(60),
|
|
ready: true,
|
|
helper_ready: false,
|
|
reason: "CONSISTENT".into(),
|
|
attempts: vec![],
|
|
acknowledged_message_ids: vec![],
|
|
key_id: "cd".repeat(32),
|
|
};
|
|
assert!(d.valid_profile(now));
|
|
d.domain = "rmm.execution-permit".into();
|
|
assert!(!d.valid_profile(now));
|
|
d.domain = "rmm.reconciliation-decision".into();
|
|
d.valid_until = now + chrono::Duration::seconds(61);
|
|
assert!(!d.valid_profile(now));
|
|
}
|
|
#[test]
|
|
fn outcome_codec_keeps_raw_output_and_sorts_evidence() {
|
|
let o = ReconciledOutcome {
|
|
state: "succeeded".into(),
|
|
stdout: vec![13, 10],
|
|
stderr: vec![],
|
|
exit_code: Some(0),
|
|
output_truncated: false,
|
|
reboot_required: false,
|
|
evidence: serde_json::from_str(r#"{"z":1,"a":{"y":2,"b":3}}"#).unwrap(),
|
|
error_code: String::new(),
|
|
};
|
|
let bytes = canonical_outcome_bytes(&o).unwrap();
|
|
let text = String::from_utf8(bytes.clone()).unwrap();
|
|
assert!(text.contains(r#""stdout":[13,10]"#));
|
|
assert!(text.contains(r#""evidence":{"a":{"b":3,"y":2},"z":1}"#));
|
|
assert_eq!(
|
|
canonical_outcome_bytes(&serde_json::from_slice(&bytes).unwrap()).unwrap(),
|
|
bytes
|
|
);
|
|
}
|
|
}
|