#![deny(unsafe_code)]
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use magma_converge::{Outcome, Plan, PlanId};
use magma_drift::DriftReport;
use magma_fsm::LifecycleState;
use magma_stream::Event;
#[derive(Debug, Error)]
pub enum BundleError {
#[error("serialize: {0}")]
Serialize(#[from] serde_json::Error),
#[error("bundle id mismatch: have {have:?}, recomputed {recomputed:?}")]
IdMismatch { have: String, recomputed: String },
#[error("missing required field: {0}")]
Missing(&'static str),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bundle {
pub bundle_id: String,
pub built_at: DateTime<Utc>,
pub kind: String,
pub workspace: String,
pub plan: Plan,
pub outcome: Option<Outcome>,
pub drift: DriftReport,
pub lifecycle: LifecycleState,
pub audit: Vec<Event>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub gem_tree_attestation: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub observation: Option<magma_types::Observation>,
}
impl Bundle {
pub fn new(
kind: impl Into<String>,
workspace: impl Into<String>,
plan: Plan,
outcome: Option<Outcome>,
drift: DriftReport,
lifecycle: LifecycleState,
audit: Vec<Event>,
) -> Result<Self, BundleError> {
Self::new_with_gem_tree(
kind, workspace, plan, outcome, drift, lifecycle, audit, None,
)
}
pub fn new_with_gem_tree(
kind: impl Into<String>,
workspace: impl Into<String>,
plan: Plan,
outcome: Option<Outcome>,
drift: DriftReport,
lifecycle: LifecycleState,
audit: Vec<Event>,
gem_tree_attestation: Option<String>,
) -> Result<Self, BundleError> {
let kind = kind.into();
let workspace = workspace.into();
let bundle_id = Self::derive_id(
&kind,
&workspace,
&plan,
&outcome,
&drift,
&lifecycle,
&audit,
gem_tree_attestation.as_deref(),
None,
)?;
Ok(Self {
bundle_id,
built_at: Utc::now(),
kind,
workspace,
plan,
outcome,
drift,
lifecycle,
audit,
gem_tree_attestation,
observation: None,
})
}
pub fn with_observation(
mut self,
observation: magma_types::Observation,
) -> Result<Self, BundleError> {
self.observation = Some(observation);
self.bundle_id = Self::derive_id(
&self.kind,
&self.workspace,
&self.plan,
&self.outcome,
&self.drift,
&self.lifecycle,
&self.audit,
self.gem_tree_attestation.as_deref(),
self.observation.as_ref(),
)?;
Ok(self)
}
#[must_use]
pub fn observation(&self) -> Option<&magma_types::Observation> {
self.observation.as_ref()
}
pub fn derive_id(
kind: &str,
workspace: &str,
plan: &Plan,
outcome: &Option<Outcome>,
drift: &DriftReport,
lifecycle: &LifecycleState,
audit: &[Event],
gem_tree_attestation: Option<&str>,
observation: Option<&magma_types::Observation>,
) -> Result<String, BundleError> {
let mut canonical = serde_json::json!({
"kind": kind,
"workspace": workspace,
"plan_id": plan.id,
"plan_changes": plan.changes,
"outcome": outcome.as_ref().map(|o| serde_json::json!({
"plan_id": o.plan_id,
"applied": o.applied,
"failed": o.failed,
})),
"drift": serde_json::json!({
"plan_id": drift.plan_id,
"summary": drift.summary,
"events": drift.events.iter().map(|e| serde_json::json!({
"kind": e.kind,
"address": e.address,
"action": e.action,
"severity": e.severity,
"decision": e.decision,
"fingerprint": e.fingerprint,
})).collect::<Vec<_>>(),
}),
"lifecycle": serde_json::json!({
"current": lifecycle.current,
"history": lifecycle.history.iter().map(|t| serde_json::json!({
"from": t.from,
"to": t.to,
"plan_id": t.plan_id,
"reason": t.reason,
})).collect::<Vec<_>>(),
}),
"audit": audit.iter().map(|e| serde_json::json!({
"seq": e.seq,
"payload": e.payload,
"prev_hash": e.prev_hash,
"hash": e.hash,
})).collect::<Vec<_>>(),
"gem_tree_attestation": gem_tree_attestation,
});
if let Some(observation) = observation {
if let Some(map) = canonical.as_object_mut() {
map.insert(
"observation".to_string(),
serde_json::to_value(observation)?,
);
}
}
let bytes = serde_json::to_vec(&canonical)?;
Ok(hex::encode(blake3::hash(&bytes).as_bytes()))
}
pub fn verify(&self) -> Result<(), BundleError> {
let recomputed = Self::derive_id(
&self.kind,
&self.workspace,
&self.plan,
&self.outcome,
&self.drift,
&self.lifecycle,
&self.audit,
self.gem_tree_attestation.as_deref(),
self.observation.as_ref(),
)?;
if recomputed != self.bundle_id {
return Err(BundleError::IdMismatch {
have: self.bundle_id.clone(),
recomputed,
});
}
Ok(())
}
pub fn plan_id(&self) -> &PlanId {
&self.plan.id
}
pub fn change_count(&self) -> usize {
self.plan.change_count()
}
pub fn fully_succeeded(&self) -> bool {
match &self.outcome {
Some(o) => o.fully_succeeded() && self.lifecycle.current == magma_fsm::Phase::Stable,
None => false,
}
}
pub fn to_json(&self) -> Result<serde_json::Value, BundleError> {
Ok(serde_json::to_value(self)?)
}
pub fn from_json_verified(value: serde_json::Value) -> Result<Self, BundleError> {
let bundle: Bundle = serde_json::from_value(value)?;
bundle.verify()?;
Ok(bundle)
}
}
#[cfg(test)]
mod tests {
use super::*;
use magma_converge::{Action, ChangeSeverity, Outcome, change};
use magma_drift::{DriftPolicy, classify};
use magma_fsm::{LifecycleState, Phase};
use magma_stream::{Event, EventPayload};
use serde_json::json;
fn sample_plan() -> Plan {
Plan::new(
"terraform",
vec![change(
"aws_vpc.net",
Action::Create,
None,
Some(json!({"cidr": "10.0.0.0/16"})),
)],
)
}
fn sample_outcome(plan: &Plan) -> Outcome {
Outcome {
plan_id: plan.id.clone(),
kind: plan.kind.clone(),
applied: vec![magma_converge::AppliedChange {
address: "aws_vpc.net".into(),
action: Action::Create,
}],
failed: vec![],
started_at: Utc::now(),
finished_at: Utc::now(),
}
}
fn sample_lifecycle(plan_id: &PlanId) -> LifecycleState {
let mut l = LifecycleState::new();
l.transition(Phase::Planning, Some(plan_id.clone()), "trigger")
.unwrap();
l.transition(Phase::Applying, Some(plan_id.clone()), "auto")
.unwrap();
l.transition(Phase::Verifying, Some(plan_id.clone()), "applied")
.unwrap();
l.transition(Phase::Stable, Some(plan_id.clone()), "verified")
.unwrap();
l
}
fn sample_audit() -> Vec<Event> {
let e0 = Event::new(
0,
EventPayload::Custom {
category: "test".into(),
message: "a".into(),
},
"0".repeat(64),
);
let e1 = Event::new(
1,
EventPayload::Custom {
category: "test".into(),
message: "b".into(),
},
e0.hash.clone(),
);
vec![e0, e1]
}
#[test]
fn build_and_verify_round_trip() {
let plan = sample_plan();
let outcome = sample_outcome(&plan);
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let bundle = Bundle::new(
"terraform",
"ws-1",
plan,
Some(outcome),
drift,
lifecycle,
audit,
)
.unwrap();
assert_eq!(bundle.bundle_id.len(), 64);
bundle.verify().unwrap();
}
#[test]
fn bundle_id_deterministic_across_calls() {
let plan = sample_plan();
let outcome = sample_outcome(&plan);
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let id1 = Bundle::derive_id(
"terraform",
"ws-1",
&plan,
&Some(outcome.clone()),
&drift,
&lifecycle,
&audit,
None,
None,
)
.unwrap();
let id2 = Bundle::derive_id(
"terraform",
"ws-1",
&plan,
&Some(outcome),
&drift,
&lifecycle,
&audit,
None,
None,
)
.unwrap();
assert_eq!(id1, id2);
}
#[test]
fn bundle_id_changes_when_gem_tree_attestation_differs() {
let plan = sample_plan();
let outcome = sample_outcome(&plan);
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let id_a = Bundle::derive_id(
"terraform",
"ws-1",
&plan,
&Some(outcome.clone()),
&drift,
&lifecycle,
&audit,
Some("a".repeat(64).as_str()),
None,
)
.unwrap();
let id_b = Bundle::derive_id(
"terraform",
"ws-1",
&plan,
&Some(outcome),
&drift,
&lifecycle,
&audit,
Some("b".repeat(64).as_str()),
None,
)
.unwrap();
assert_ne!(id_a, id_b);
}
#[test]
fn bundle_id_distinguishes_a_blind_reconcile_from_a_clean_one() {
use magma_types::{Observation, RefreshCounts};
let plan = sample_plan();
let outcome = sample_outcome(&plan);
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let clean = Observation::of(RefreshCounts {
refreshed: 9,
..RefreshCounts::default()
});
let blind = Observation::of(RefreshCounts {
kept_on_error: 9,
..RefreshCounts::default()
});
assert_eq!(clean.coverage(), magma_types::Coverage::Complete);
assert_eq!(blind.coverage(), magma_types::Coverage::Blind);
let mk = |obs| {
Bundle::new(
"terraform",
"ws-1",
plan.clone(),
Some(outcome.clone()),
drift.clone(),
lifecycle.clone(),
audit.clone(),
)
.unwrap()
.with_observation(obs)
.unwrap()
};
let a = mk(clean);
let b = mk(blind);
assert_ne!(a.bundle_id, b.bundle_id);
a.verify().unwrap();
b.verify().unwrap();
}
#[test]
fn stripping_the_observation_breaks_verification() {
use magma_types::{Observation, RefreshCounts};
let plan = sample_plan();
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let mut bundle = Bundle::new(
"terraform",
"ws-1",
plan,
None,
drift,
lifecycle,
sample_audit(),
)
.unwrap()
.with_observation(Observation::of(RefreshCounts {
kept_on_error: 4,
..RefreshCounts::default()
}))
.unwrap();
bundle.verify().unwrap();
bundle.observation = None;
match bundle.verify() {
Err(BundleError::IdMismatch { .. }) => {}
other => panic!("expected IdMismatch, got {other:?}"),
}
}
#[test]
fn a_bundle_without_an_observation_hashes_exactly_as_it_did_before() {
let plan = sample_plan();
let outcome = sample_outcome(&plan);
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let historical = serde_json::json!({
"kind": "terraform",
"workspace": "ws-1",
"plan_id": plan.id,
"plan_changes": plan.changes,
"outcome": serde_json::json!({
"plan_id": outcome.plan_id,
"applied": outcome.applied,
"failed": outcome.failed,
}),
"drift": serde_json::json!({
"plan_id": drift.plan_id,
"summary": drift.summary,
"events": drift.events.iter().map(|e| serde_json::json!({
"kind": e.kind,
"address": e.address,
"action": e.action,
"severity": e.severity,
"decision": e.decision,
"fingerprint": e.fingerprint,
})).collect::<Vec<_>>(),
}),
"lifecycle": serde_json::json!({
"current": lifecycle.current,
"history": lifecycle.history.iter().map(|t| serde_json::json!({
"from": t.from,
"to": t.to,
"plan_id": t.plan_id,
"reason": t.reason,
})).collect::<Vec<_>>(),
}),
"audit": audit.iter().map(|e| serde_json::json!({
"seq": e.seq,
"payload": e.payload,
"prev_hash": e.prev_hash,
"hash": e.hash,
})).collect::<Vec<_>>(),
"gem_tree_attestation": None::<&str>,
});
let expected =
hex::encode(blake3::hash(&serde_json::to_vec(&historical).unwrap()).as_bytes());
let actual = Bundle::derive_id(
"terraform",
"ws-1",
&plan,
&Some(outcome),
&drift,
&lifecycle,
&audit,
None,
None,
)
.unwrap();
assert_eq!(
actual, expected,
"an observation-less bundle must project the pre-observation bytes",
);
}
#[test]
fn gem_tree_attestation_round_trips_through_serde() {
let plan = sample_plan();
let outcome = sample_outcome(&plan);
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let bundle = Bundle::new_with_gem_tree(
"terraform",
"ws-1",
plan,
Some(outcome),
drift,
lifecycle,
audit,
Some("c".repeat(64)),
)
.unwrap();
let v = bundle.to_json().unwrap();
let back = Bundle::from_json_verified(v).unwrap();
assert_eq!(
back.gem_tree_attestation.as_deref(),
Some("c".repeat(64).as_str())
);
assert_eq!(back.bundle_id, bundle.bundle_id);
}
#[test]
fn json_round_trip_verified() {
let plan = sample_plan();
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let outcome = sample_outcome(&plan);
let bundle = Bundle::new(
"terraform",
"ws-1",
plan,
Some(outcome),
drift,
lifecycle,
audit,
)
.unwrap();
let json_value = bundle.to_json().unwrap();
let restored = Bundle::from_json_verified(json_value).unwrap();
assert_eq!(restored.bundle_id, bundle.bundle_id);
assert_eq!(restored.workspace, "ws-1");
}
#[test]
fn tampered_workspace_fails_verify() {
let plan = sample_plan();
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let outcome = sample_outcome(&plan);
let mut bundle = Bundle::new(
"terraform",
"ws-1",
plan,
Some(outcome),
drift,
lifecycle,
audit,
)
.unwrap();
bundle.workspace = "ws-evil".into();
match bundle.verify() {
Err(BundleError::IdMismatch { .. }) => {}
other => panic!("expected IdMismatch, got {other:?}"),
}
}
#[test]
fn fully_succeeded_requires_stable_and_no_apply_failures() {
let plan = sample_plan();
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id); let outcome = sample_outcome(&plan);
let audit = sample_audit();
let bundle = Bundle::new(
"terraform",
"ws-1",
plan,
Some(outcome),
drift,
lifecycle,
audit,
)
.unwrap();
assert!(bundle.fully_succeeded());
}
#[test]
fn not_fully_succeeded_when_lifecycle_not_stable() {
let plan = sample_plan();
let drift = classify(&plan, &DriftPolicy::conservative_default());
let mut lifecycle = LifecycleState::new();
lifecycle
.transition(Phase::Planning, Some(plan.id.clone()), "x")
.unwrap();
let outcome = sample_outcome(&plan);
let audit = sample_audit();
let bundle = Bundle::new(
"terraform",
"ws-1",
plan,
Some(outcome),
drift,
lifecycle,
audit,
)
.unwrap();
assert!(!bundle.fully_succeeded());
}
#[test]
fn not_fully_succeeded_when_no_outcome() {
let plan = sample_plan();
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let audit = sample_audit();
let bundle = Bundle::new("terraform", "ws-1", plan, None, drift, lifecycle, audit).unwrap();
assert!(!bundle.fully_succeeded());
}
#[test]
fn empty_audit_still_bundles() {
let plan = sample_plan();
let drift = classify(&plan, &DriftPolicy::conservative_default());
let lifecycle = sample_lifecycle(&plan.id);
let outcome = sample_outcome(&plan);
let bundle = Bundle::new(
"terraform",
"ws-1",
plan,
Some(outcome),
drift,
lifecycle,
vec![],
)
.unwrap();
bundle.verify().unwrap();
}
}