feat: qualify bound configure-only recovery runtime on cloud VM
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -70,6 +70,8 @@ pub struct PackageArchive {
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ResolvedAptTransaction {
|
||||
pub schema_version: u32,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub operation: Option<rmm_domain::apt_transaction::AptTransactionOperation>,
|
||||
pub recipe_id: String,
|
||||
pub recipe_version: String,
|
||||
pub platform: String,
|
||||
@@ -82,8 +84,7 @@ pub struct ResolvedAptTransaction {
|
||||
impl ResolvedAptTransaction {
|
||||
pub fn validate_fixture(&self) -> Result<()> {
|
||||
ensure!(
|
||||
self.schema_version == 1
|
||||
&& self.recipe_version == "1"
|
||||
self.recipe_version == "1"
|
||||
&& self.platform == "ubuntu-24.04-amd64"
|
||||
&& self.resolver_config_version == "rmm-apt-local-v1"
|
||||
&& self.no_reboot
|
||||
@@ -93,9 +94,18 @@ impl ResolvedAptTransaction {
|
||||
sha(&self.status_fingerprint)?;
|
||||
sha(&self.baseline_sha256)?;
|
||||
let p = &self.packages[0];
|
||||
let (old, new, expected_hash) = match self.recipe_id.as_str() {
|
||||
INSTALL_RECIPE => (None, "1.0", INSTALL_SHA),
|
||||
UPDATE_RECIPE => (Some("1.0"), "1.1", UPDATE_SHA),
|
||||
let (old, new, expected_hash) = match (
|
||||
self.schema_version,
|
||||
&self.operation,
|
||||
self.recipe_id.as_str(),
|
||||
) {
|
||||
(1, None, INSTALL_RECIPE) => (None, "1.0", INSTALL_SHA),
|
||||
(1, None, UPDATE_RECIPE) => (Some("1.0"), "1.1", UPDATE_SHA),
|
||||
(
|
||||
2,
|
||||
Some(rmm_domain::apt_transaction::AptTransactionOperation::ConfigureOnly),
|
||||
INSTALL_RECIPE,
|
||||
) => (Some("1.0"), "1.0", INSTALL_SHA),
|
||||
_ => bail!("APT_RECIPE_NOT_CONFIGURED"),
|
||||
};
|
||||
ensure!(
|
||||
@@ -133,6 +143,8 @@ pub struct AptExecutionManifest {
|
||||
pub test_result_id: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub native_retry: Option<rmm_domain::apt_transaction::AptNativeRetry>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub native_recovery: Option<rmm_domain::apt_transaction::AptNativeRecovery>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -299,11 +311,20 @@ fn validate_manifest(
|
||||
config.validate()?;
|
||||
ensure!(
|
||||
m.kind == "rmm.apt.helper-action"
|
||||
&& match (&m.native_retry, m.schema_version) {
|
||||
(None, 2) => true,
|
||||
(Some(retry), 3) => {
|
||||
action == "apt_native_acceptance" && retry.validate()
|
||||
&& match (&m.native_retry, &m.native_recovery, m.schema_version) {
|
||||
(None, None, 2) => m.resolved_apt_transaction.operation.is_none(),
|
||||
(Some(retry), None, 3) => {
|
||||
action == "apt_native_acceptance"
|
||||
&& retry.validate()
|
||||
&& m.resolved_apt_transaction.operation.is_none()
|
||||
}
|
||||
(None, Some(recovery), 4) =>
|
||||
action == "apt_native_acceptance"
|
||||
&& recovery.validate()
|
||||
&& m.resolved_apt_transaction.operation
|
||||
== Some(
|
||||
rmm_domain::apt_transaction::AptTransactionOperation::ConfigureOnly
|
||||
),
|
||||
_ => false,
|
||||
}
|
||||
&& m.execution_profile == config.execution_profile
|
||||
@@ -600,6 +621,10 @@ impl PreparedApt {
|
||||
/// Read-only prerequisite under the caller's device ExecutorGuard. A new
|
||||
/// signed retry does not clear an unknown effect or change an old record.
|
||||
pub fn verify_retry_history(&self, db: &rusqlite::Connection) -> Result<()> {
|
||||
if self.manifest.native_recovery.is_some() {
|
||||
self.recovery_partial(db)?;
|
||||
return Ok(());
|
||||
}
|
||||
let Some(retry) = &self.manifest.native_retry else {
|
||||
return Ok(());
|
||||
};
|
||||
@@ -647,6 +672,44 @@ impl PreparedApt {
|
||||
self.verify_retry_evidence(db, &evidence)
|
||||
}
|
||||
|
||||
fn recovery_partial(
|
||||
&self,
|
||||
db: &rusqlite::Connection,
|
||||
) -> Result<crate::apt_reconcile::ClosedPartial> {
|
||||
let reference = self
|
||||
.manifest
|
||||
.native_recovery
|
||||
.as_ref()
|
||||
.context("APT_RECOVERY_REFERENCE_REQUIRED")?;
|
||||
let prior = crate::apt_reconcile::closed_partial(db, &self.binding, Some(reference))?;
|
||||
ensure!(
|
||||
self.manifest.schema_version == 4
|
||||
&& self.manifest.action == "apt-native-acceptance"
|
||||
&& self.manifest.execution_profile == "dev-test"
|
||||
&& reference.previous_grant_id != self.permit.grant_id
|
||||
&& reference.previous_attempt_id != self.job.attempt_id
|
||||
&& prior.job_id != self.job.job_id
|
||||
&& prior.authorization_id != self.permit.authorization_id
|
||||
&& prior.plan_sha256 != self.permit.plan_hash
|
||||
&& prior.previous_start_before <= self.permit.not_before
|
||||
&& self.manifest.resolved_apt_transaction.baseline_sha256 == prior.baseline_sha256
|
||||
&& self.manifest.resolved_apt_transaction.status_fingerprint
|
||||
== prior.status_fingerprint,
|
||||
"APT_RECOVERY_NEW_APPROVED_BINDING"
|
||||
);
|
||||
for name in [
|
||||
format!("recovery-attempt-{}.json", reference.previous_grant_id),
|
||||
format!("recovery-{}.json", reference.previous_grant_id),
|
||||
format!("pending-recovery-{}.json", reference.previous_grant_id),
|
||||
] {
|
||||
ensure!(
|
||||
absent_nofollow(&Path::new(BASE).join(name))?,
|
||||
"APT_RECOVERY_ALREADY_ATTEMPTED"
|
||||
);
|
||||
}
|
||||
Ok(prior)
|
||||
}
|
||||
|
||||
fn verify_retry_evidence(
|
||||
&self,
|
||||
db: &rusqlite::Connection,
|
||||
@@ -901,6 +964,11 @@ impl PreparedApt {
|
||||
fn stage(&self, db: &rusqlite::Connection, ledger_id: &str) -> Result<PathBuf> {
|
||||
verify_installation()?;
|
||||
verify_prefetched(&self.manifest, &self.permit)?;
|
||||
let partial = if self.manifest.native_recovery.is_some() {
|
||||
Some(self.recovery_partial(db)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let remaining = (self.permit.start_before - Utc::now())
|
||||
.num_nanoseconds()
|
||||
.context("APT_DEADLINE_RANGE")?;
|
||||
@@ -959,6 +1027,9 @@ impl PreparedApt {
|
||||
1024 * 1024,
|
||||
)?;
|
||||
write_once(&active.join("baseline.json"), &baseline, 0o600)?;
|
||||
if let Some(partial) = partial {
|
||||
write_once(&active.join("partial-proof.json"), &partial.proof, 0o600)?;
|
||||
}
|
||||
let config = Command::new("/usr/bin/python3")
|
||||
.args(["-I", CODE, "print-config"])
|
||||
.env_clear()
|
||||
@@ -1098,11 +1169,31 @@ fn map_driver_result(
|
||||
"APT_FIXTURE_PAYLOAD_EVIDENCE"
|
||||
);
|
||||
if bootstrap {
|
||||
sha(result
|
||||
.evidence
|
||||
.get("native_record_sha256")
|
||||
.and_then(|v| v.as_str())
|
||||
.context("APT_NATIVE_RECORD")?)?;
|
||||
if let Some(recovery) = &m.native_recovery {
|
||||
ensure!(
|
||||
result.evidence.get("native_recovery") == Some(&serde_json::json!(true))
|
||||
&& result.evidence.get("partial_proof_sha256")
|
||||
== Some(&serde_json::json!(recovery.partial_proof_sha256))
|
||||
&& result.evidence.get("native_record_sha256").is_none(),
|
||||
"APT_RECOVERY_SEPARATE_RECORD_REQUIRED"
|
||||
);
|
||||
sha(result
|
||||
.evidence
|
||||
.get("recovery_record_sha256")
|
||||
.and_then(|v| v.as_str())
|
||||
.context("APT_RECOVERY_RECORD")?)?;
|
||||
} else {
|
||||
ensure!(
|
||||
result.evidence.get("native_recovery").is_none()
|
||||
&& result.evidence.get("recovery_record_sha256").is_none(),
|
||||
"APT_RECOVERY_WRONG_ACTION"
|
||||
);
|
||||
sha(result
|
||||
.evidence
|
||||
.get("native_record_sha256")
|
||||
.and_then(|v| v.as_str())
|
||||
.context("APT_NATIVE_RECORD")?)?;
|
||||
}
|
||||
ensure!(
|
||||
result.evidence.get("test_result_id")
|
||||
== Some(&serde_json::json!(m.test_result_id)),
|
||||
|
||||
@@ -3,6 +3,46 @@ use openssl::{pkey::PKey, sign::Signer};
|
||||
use rmm_domain::jobs::{Action, encode};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[test]
|
||||
fn configure_only_is_explicit_native_recovery_and_never_install_acceptance() {
|
||||
let (config, mut m) = manifest(true);
|
||||
m.schema_version = 4;
|
||||
m.native_recovery = Some(rmm_domain::apt_transaction::AptNativeRecovery {
|
||||
schema_version: 1,
|
||||
previous_grant_id: Uuid::new_v4(),
|
||||
previous_attempt_id: Uuid::new_v4(),
|
||||
partial_proof_sha256: hash(b"partial"),
|
||||
});
|
||||
m.resolved_apt_transaction.schema_version = 2;
|
||||
m.resolved_apt_transaction.operation =
|
||||
Some(rmm_domain::apt_transaction::AptTransactionOperation::ConfigureOnly);
|
||||
m.resolved_apt_transaction.packages[0].old_version = Some("1.0".into());
|
||||
m.transaction_bytes = serde_json::to_vec(&m.resolved_apt_transaction).unwrap();
|
||||
m.transaction_sha256 = hash(&m.transaction_bytes);
|
||||
validate_manifest(&m, "apt_native_acceptance", &config).unwrap();
|
||||
assert!(validate_manifest(&m, "ensure_package", &config).is_err());
|
||||
let mut evidence = serde_json::json!({"test_acceptance":true,"noProductionEvidence":true,"native_recovery":true,"recovery_record_sha256":hash(b"recovery record"),"partial_proof_sha256":hash(b"partial"),"test_result_id":m.test_result_id,"effect_armed_ledger_id":"ledger","packages":[{"name":"rmm-mvp-test","architecture":"all","version":"1.0"}],"snapshot_sha256":hash(b"snapshot"),"fixture_payload_sha256":hash(b"1.0\n"),"reboot_required":false});
|
||||
let encode_result = |e: &serde_json::Value| {
|
||||
serde_json::to_vec(&serde_json::json!({"effect_state":"verified","code":"POSTCONDITIONS_VERIFIED","evidence":e})).unwrap()
|
||||
};
|
||||
assert_eq!(
|
||||
map_driver_result(&encode_result(&evidence), Some(0), true, &m, "ledger")
|
||||
.unwrap()
|
||||
.state,
|
||||
"succeeded"
|
||||
);
|
||||
evidence["native_record_sha256"] = serde_json::json!(hash(b"fake install acceptance"));
|
||||
assert!(map_driver_result(&encode_result(&evidence), Some(0), true, &m, "ledger").is_err());
|
||||
evidence
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.remove("native_record_sha256");
|
||||
evidence["partial_proof_sha256"] = serde_json::json!(hash(b"other"));
|
||||
assert!(map_driver_result(&encode_result(&evidence), Some(0), true, &m, "ledger").is_err());
|
||||
m.schema_version = 2;
|
||||
assert!(validate_manifest(&m, "apt_native_acceptance", &config).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn actual_domain_native_action_roundtrips_exact_signed_manifest_to_helper() {
|
||||
let (binding, mut request, key, now) = signed_request();
|
||||
@@ -352,6 +392,7 @@ fn consumed_parent_row_and_original_start_request_are_mandatory() {
|
||||
fn manifest(bootstrap: bool) -> (ProviderConfig, AptExecutionManifest) {
|
||||
let transaction = ResolvedAptTransaction {
|
||||
schema_version: 1,
|
||||
operation: None,
|
||||
recipe_id: INSTALL_RECIPE.into(),
|
||||
recipe_version: "1".into(),
|
||||
platform: "ubuntu-24.04-amd64".into(),
|
||||
@@ -398,6 +439,7 @@ fn manifest(bootstrap: bool) -> (ProviderConfig, AptExecutionManifest) {
|
||||
runtime_sha256: config.runtime_sha256.clone(),
|
||||
test_result_id: "fixture-test-1".into(),
|
||||
native_retry: None,
|
||||
native_recovery: None,
|
||||
};
|
||||
(config, manifest)
|
||||
}
|
||||
|
||||
@@ -132,6 +132,120 @@ struct LedgerRow {
|
||||
start: Vec<u8>,
|
||||
outcome: Vec<u8>,
|
||||
}
|
||||
pub struct ClosedPartial {
|
||||
pub reference: rmm_domain::apt_transaction::AptNativeRecovery,
|
||||
pub proof: Vec<u8>,
|
||||
pub job_id: Uuid,
|
||||
pub authorization_id: Uuid,
|
||||
pub plan_sha256: String,
|
||||
pub previous_start_before: DateTime<Utc>,
|
||||
pub baseline_sha256: String,
|
||||
pub status_fingerprint: String,
|
||||
}
|
||||
/// Only a previously classified partial whose exact outcome was closed by
|
||||
/// signed RootSync can seed a new, separately approved configure-only job.
|
||||
pub fn closed_partial(
|
||||
db: &Connection,
|
||||
binding: &Binding,
|
||||
wanted: Option<&rmm_domain::apt_transaction::AptNativeRecovery>,
|
||||
) -> Result<ClosedPartial> {
|
||||
closed_partial_from(db, binding, wanted, read_evidence)
|
||||
}
|
||||
fn closed_partial_from(
|
||||
db: &Connection,
|
||||
binding: &Binding,
|
||||
wanted: Option<&rmm_domain::apt_transaction::AptNativeRecovery>,
|
||||
read: impl Fn() -> Result<BTreeMap<String, Vec<u8>>>,
|
||||
) -> Result<ClosedPartial> {
|
||||
let mut q=db.prepare("SELECT c.rowid,c.grant_id,c.agent,c.generation,c.job,c.attempt,e.start,c.result,e.outcome,p.original_request_sha256,p.previous_outcome_sha256,p.previous_outcome,p.proof_sha256,p.proof,p.classified_outcome,p.checked_at_ms FROM apt_partial_reconciliations p JOIN consumed c USING(grant_id) JOIN execution_evidence e USING(grant_id) ORDER BY c.rowid")?;
|
||||
let rows = q
|
||||
.query_map([], |r| {
|
||||
Ok((
|
||||
LedgerRow {
|
||||
rowid: r.get(0)?,
|
||||
grant: r.get(1)?,
|
||||
agent: r.get(2)?,
|
||||
generation: r.get(3)?,
|
||||
job: r.get(4)?,
|
||||
attempt: r.get(5)?,
|
||||
start: r.get(6)?,
|
||||
outcome: r.get(11)?,
|
||||
},
|
||||
r.get::<_, Option<Vec<u8>>>(7)?,
|
||||
r.get::<_, Option<Vec<u8>>>(8)?,
|
||||
r.get::<_, String>(9)?,
|
||||
r.get::<_, String>(10)?,
|
||||
r.get::<_, String>(12)?,
|
||||
r.get::<_, Vec<u8>>(13)?,
|
||||
r.get::<_, Vec<u8>>(14)?,
|
||||
r.get::<_, i64>(15)?,
|
||||
))
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
ensure!(rows.len() <= 256, "APT_RECOVERY_PROOF_LIMIT");
|
||||
let mut found = None;
|
||||
for (row, result, outcome, request_sha, old_sha, proof_sha, proof, classified, checked) in rows
|
||||
{
|
||||
if wanted.is_some_and(|w| w.previous_grant_id.to_string() != row.grant) {
|
||||
continue;
|
||||
}
|
||||
let Some(c) = eligible(binding, row)? else {
|
||||
continue;
|
||||
};
|
||||
let reference = rmm_domain::apt_transaction::AptNativeRecovery {
|
||||
schema_version: 1,
|
||||
previous_grant_id: c.probe.grant_id,
|
||||
previous_attempt_id: c.probe.attempt_id,
|
||||
partial_proof_sha256: proof_sha.clone(),
|
||||
};
|
||||
ensure!(
|
||||
wanted.is_none_or(|w| w == &reference)
|
||||
&& reference.validate()
|
||||
&& hash(&proof) == proof_sha
|
||||
&& hash(&c.original_outcome) == old_sha
|
||||
&& hash(&serde_json::to_vec(&c.request)?) == request_sha,
|
||||
"APT_RECOVERY_IMMUTABLE_PROOF"
|
||||
);
|
||||
let result: Outcome =
|
||||
serde_json::from_slice(&result.context("APT_RECOVERY_PRIOR_UNRESOLVED")?)?;
|
||||
let classified_value: Value = serde_json::from_slice(&classified)?;
|
||||
ensure!(
|
||||
result.state == "failed"
|
||||
&& result.error_code == CODE
|
||||
&& !result.reboot_required
|
||||
&& serde_json::to_value(&result)? == classified_value
|
||||
&& serde_json::from_slice::<Value>(
|
||||
&outcome.context("APT_RECOVERY_PRIOR_OUTCOME")?
|
||||
)? == classified_value,
|
||||
"APT_RECOVERY_PRIOR_NOT_CLOSED_PARTIAL"
|
||||
);
|
||||
let p: PartialProof = serde_json::from_slice(&proof)?;
|
||||
ensure!(p.checked_at_ms == checked, "APT_RECOVERY_PROOF_TIME");
|
||||
let files = read()?;
|
||||
verify_proof(
|
||||
&c,
|
||||
&files,
|
||||
&ProbeOutput {
|
||||
effect_state: "known_failed".into(),
|
||||
code: CODE.into(),
|
||||
proof: p.clone(),
|
||||
},
|
||||
DateTime::from_timestamp_millis(checked).context("APT_RECOVERY_PROOF_TIME")?,
|
||||
)?;
|
||||
ensure!(found.is_none(), "APT_RECOVERY_AMBIGUOUS_PROOFS");
|
||||
found = Some(ClosedPartial {
|
||||
reference,
|
||||
proof,
|
||||
job_id: c.probe.job_id,
|
||||
authorization_id: c.probe.authorization_id,
|
||||
plan_sha256: c.probe.plan_sha256,
|
||||
previous_start_before: c.permit.start_before,
|
||||
baseline_sha256: p.observed_snapshot_sha256,
|
||||
status_fingerprint: p.status_fingerprint,
|
||||
});
|
||||
}
|
||||
found.context("APT_RECOVERY_CLOSED_PROOF_REQUIRED")
|
||||
}
|
||||
fn digest(s: &str) -> bool {
|
||||
s.len() == 64
|
||||
&& s.bytes()
|
||||
@@ -176,6 +290,9 @@ fn eligible(binding: &Binding, row: LedgerRow) -> Result<Option<Candidate>> {
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
if apt_manifest.schema_version != 3 || apt_manifest.native_retry.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
ensure!(
|
||||
recipe_id == apt::INSTALL_RECIPE
|
||||
&& recipe_version == "1"
|
||||
@@ -847,4 +964,38 @@ mod tests {
|
||||
classified
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn new_recovery_requires_exact_closed_partial_signed_history_and_immutable_proof() {
|
||||
let (binding, row, files, now) = fixture();
|
||||
let c = eligible(&binding, row.clone()).unwrap().unwrap();
|
||||
let proof = output(&c, &files, now).proof;
|
||||
let mut db = Connection::open_in_memory().unwrap();
|
||||
db.execute_batch("CREATE TABLE consumed(grant_id TEXT PRIMARY KEY,agent TEXT,generation INTEGER,job TEXT,attempt TEXT,result BLOB);CREATE TABLE execution_evidence(grant_id TEXT PRIMARY KEY,start BLOB,outcome BLOB);").unwrap();
|
||||
db.execute("INSERT INTO consumed(rowid,grant_id,agent,generation,job,attempt,result) VALUES(?1,?2,?3,?4,?5,?6,NULL)",params![row.rowid,row.grant,row.agent,row.generation,row.job,row.attempt]).unwrap();
|
||||
db.execute(
|
||||
"INSERT INTO execution_evidence VALUES(?1,?2,?3)",
|
||||
params![row.grant, row.start, row.outcome],
|
||||
)
|
||||
.unwrap();
|
||||
persist(&mut db, &c, &proof).unwrap();
|
||||
assert!(closed_partial_from(&db, &binding, None, || Ok(files.clone())).is_err());
|
||||
db.execute(
|
||||
"UPDATE consumed SET result=(SELECT outcome FROM execution_evidence)",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
let closed = closed_partial_from(&db, &binding, None, || Ok(files.clone())).unwrap();
|
||||
assert_eq!(closed.reference.previous_grant_id, c.probe.grant_id);
|
||||
assert_eq!(closed.reference.previous_attempt_id, c.probe.attempt_id);
|
||||
assert_eq!(closed.baseline_sha256, proof.observed_snapshot_sha256);
|
||||
let mut changed = closed.reference.clone();
|
||||
changed.previous_attempt_id = Uuid::new_v4();
|
||||
assert!(closed_partial_from(&db, &binding, Some(&changed), || Ok(files.clone())).is_err());
|
||||
let mut changed_files = files.clone();
|
||||
changed_files.insert("signed-action.json".into(), b"{}".to_vec());
|
||||
assert!(closed_partial_from(&db, &binding, None, || Ok(changed_files.clone())).is_err());
|
||||
db.execute("UPDATE apt_partial_reconciliations SET proof=x'7b7d'", [])
|
||||
.unwrap();
|
||||
assert!(closed_partial_from(&db, &binding, None, || Ok(files.clone())).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,8 @@ pub struct Response {
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct PlanningResult {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub native_recovery: Option<rmm_domain::apt_transaction::AptNativeRecovery>,
|
||||
pub observed_at: chrono::DateTime<chrono::Utc>,
|
||||
pub fixture_package: Option<FixturePackage>,
|
||||
pub baseline_sha256: String,
|
||||
@@ -106,6 +108,16 @@ pub struct PlanningReceipt {
|
||||
}
|
||||
impl PlanningResult {
|
||||
pub fn validate(&self, now: chrono::DateTime<chrono::Utc>) -> Result<()> {
|
||||
if let Some(reference) = &self.native_recovery {
|
||||
ensure!(
|
||||
reference.validate()
|
||||
&& self
|
||||
.fixture_package
|
||||
.as_ref()
|
||||
.is_some_and(|p| p.version == "1.0" && p.status == "install ok unpacked"),
|
||||
"APT_RECOVERY_PLANNING_SCOPE"
|
||||
);
|
||||
}
|
||||
ensure!(
|
||||
self.observed_at <= now
|
||||
&& now - self.observed_at < chrono::Duration::seconds(30)
|
||||
@@ -235,18 +247,28 @@ pub fn validate_response(request: &Request, response: &Response) -> Result<()> {
|
||||
ensure!(
|
||||
matches!(
|
||||
request.operation.as_str(),
|
||||
"apt_readiness_v1" | "apt_planning_v1"
|
||||
"apt_readiness_v1" | "apt_planning_v1" | "apt_recovery_planning_v1"
|
||||
),
|
||||
"APT_PROBE_OPERATION"
|
||||
);
|
||||
if let Some(planning) = &response.planning {
|
||||
ensure!(
|
||||
request.operation == "apt_planning_v1",
|
||||
matches!(
|
||||
request.operation.as_str(),
|
||||
"apt_planning_v1" | "apt_recovery_planning_v1"
|
||||
),
|
||||
"APT_PLANNING_OPERATION"
|
||||
);
|
||||
planning.validate(chrono::Utc::now())?;
|
||||
ensure!(
|
||||
(request.operation == "apt_recovery_planning_v1") == planning.native_recovery.is_some(),
|
||||
"APT_RECOVERY_PLANNING_OPERATION"
|
||||
);
|
||||
}
|
||||
if request.operation == "apt_planning_v1" {
|
||||
if matches!(
|
||||
request.operation.as_str(),
|
||||
"apt_planning_v1" | "apt_recovery_planning_v1"
|
||||
) {
|
||||
ensure!(
|
||||
response.planning.is_some()
|
||||
&& !response.fixture_acceptance_ready
|
||||
@@ -292,6 +314,9 @@ pub fn probe(socket: &Path, binding: &Binding, pin: &Pin) -> Result<Response> {
|
||||
pub fn planning(socket: &Path, binding: &Binding, pin: &Pin) -> Result<Response> {
|
||||
probe_operation(socket, binding, pin, "apt_planning_v1")
|
||||
}
|
||||
pub fn planning_for_recovery(socket: &Path, binding: &Binding, pin: &Pin) -> Result<Response> {
|
||||
probe_operation(socket, binding, pin, "apt_recovery_planning_v1")
|
||||
}
|
||||
fn probe_operation(
|
||||
socket: &Path,
|
||||
binding: &Binding,
|
||||
@@ -368,7 +393,7 @@ pub fn answer(
|
||||
ensure!(
|
||||
matches!(
|
||||
request.operation.as_str(),
|
||||
"apt_readiness_v1" | "apt_planning_v1"
|
||||
"apt_readiness_v1" | "apt_planning_v1" | "apt_recovery_planning_v1"
|
||||
) && !request.challenge.is_nil()
|
||||
&& request.identity == Identity::from_binding(binding)?,
|
||||
"APT_PROBE_REQUEST_BINDING"
|
||||
@@ -378,11 +403,21 @@ pub fn answer(
|
||||
stack == request.expected_stack_sha256,
|
||||
"APT_PROBE_STACK_MISMATCH"
|
||||
);
|
||||
if request.operation == "apt_planning_v1" {
|
||||
if matches!(
|
||||
request.operation.as_str(),
|
||||
"apt_planning_v1" | "apt_recovery_planning_v1"
|
||||
) {
|
||||
// A metadata-only process is bounded separately from package execution.
|
||||
// Missing/unsafe/dirty/unprefetched state closes only this observation.
|
||||
let planning: PlanningResult =
|
||||
serde_json::from_slice(&rmm_agent_linux::apt_planning_metadata()?)?;
|
||||
let planning: PlanningResult = if request.operation == "apt_recovery_planning_v1" {
|
||||
recovery_planning(ledger, binding)?
|
||||
} else {
|
||||
serde_json::from_slice(&rmm_agent_linux::apt_planning_metadata()?)?
|
||||
};
|
||||
ensure!(
|
||||
(request.operation == "apt_recovery_planning_v1") == planning.native_recovery.is_some(),
|
||||
"APT_RECOVERY_PLANNING_OPERATION"
|
||||
);
|
||||
planning.validate(chrono::Utc::now())?;
|
||||
ensure!(
|
||||
planning.runtime_sha256 == config.runtime_sha256
|
||||
@@ -475,14 +510,154 @@ pub fn answer(
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
fn recovery_planning(ledger: &rusqlite::Connection, binding: &Binding) -> Result<PlanningResult> {
|
||||
use anyhow::Context;
|
||||
use std::{
|
||||
os::unix::process::CommandExt,
|
||||
process::{Command, Stdio},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
ensure!(
|
||||
ledger.query_row(
|
||||
"SELECT count(*) FROM consumed WHERE result IS NULL",
|
||||
[],
|
||||
|r| r.get::<_, u64>(0)
|
||||
)? == 0,
|
||||
"APT_RECOVERY_PLANNING_UNRESOLVED"
|
||||
);
|
||||
apt::verify_installation()?;
|
||||
let partial = crate::apt_reconcile::closed_partial(ledger, binding, None)?;
|
||||
let proof: serde_json::Value = serde_json::from_slice(&partial.proof)?;
|
||||
let input = serde_json::to_vec(
|
||||
&serde_json::json!({"native_recovery":partial.reference,"proof":proof}),
|
||||
)?;
|
||||
ensure!(
|
||||
input.len() <= 1024 * 1024,
|
||||
"APT_RECOVERY_PLANNING_INPUT_LIMIT"
|
||||
);
|
||||
let mut child = Command::new("/usr/bin/python3")
|
||||
.args(["-I", apt::CODE, "recovery-planning"])
|
||||
.env_clear()
|
||||
.env("PATH", "/usr/sbin:/usr/bin:/sbin:/bin")
|
||||
.env("LC_ALL", "C")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.process_group(0)
|
||||
.spawn()?;
|
||||
let result = (|| -> Result<Vec<u8>> {
|
||||
let mut stdin = Some(child.stdin.take().context("APT_RECOVERY_PLANNING_STDIN")?);
|
||||
let mut stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.context("APT_RECOVERY_PLANNING_STDOUT")?;
|
||||
for fd in [
|
||||
stdin
|
||||
.as_ref()
|
||||
.context("APT_RECOVERY_PLANNING_STDIN")?
|
||||
.as_raw_fd(),
|
||||
stdout.as_raw_fd(),
|
||||
] {
|
||||
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
|
||||
ensure!(
|
||||
flags >= 0
|
||||
&& unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } >= 0,
|
||||
"APT_RECOVERY_PLANNING_PIPE"
|
||||
);
|
||||
}
|
||||
let deadline = Instant::now() + Duration::from_secs(4);
|
||||
let mut written = 0;
|
||||
let mut output = vec![];
|
||||
let mut exited = None;
|
||||
let mut eof = false;
|
||||
loop {
|
||||
ensure!(Instant::now() < deadline, "APT_RECOVERY_PLANNING_TIMEOUT");
|
||||
if let Some(pipe) = stdin.as_mut() {
|
||||
match pipe.write(&input[written..]) {
|
||||
Ok(n) => written += n,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
if written == input.len() {
|
||||
stdin = None;
|
||||
}
|
||||
}
|
||||
let mut buf = [0; 4096];
|
||||
for _ in 0..16 {
|
||||
match stdout.read(&mut buf) {
|
||||
Ok(0) => {
|
||||
eof = true;
|
||||
break;
|
||||
}
|
||||
Ok(n) => {
|
||||
ensure!(
|
||||
output.len() + n <= 32768,
|
||||
"APT_RECOVERY_PLANNING_OUTPUT_LIMIT"
|
||||
);
|
||||
output.extend_from_slice(&buf[..n]);
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
if exited.is_none() {
|
||||
exited = child.try_wait()?;
|
||||
}
|
||||
if eof && let Some(status) = exited {
|
||||
ensure!(
|
||||
status.success() && written == input.len(),
|
||||
"APT_RECOVERY_PLANNING_FAILED"
|
||||
);
|
||||
return Ok(output);
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
})();
|
||||
unsafe {
|
||||
libc::kill(-(child.id() as i32), libc::SIGKILL);
|
||||
}
|
||||
let _ = child.wait();
|
||||
let planning: PlanningResult = serde_json::from_slice(&result?)?;
|
||||
ensure!(
|
||||
planning.native_recovery.as_ref() == Some(&partial.reference)
|
||||
&& planning.baseline_sha256 == partial.baseline_sha256
|
||||
&& planning.status_fingerprint == partial.status_fingerprint,
|
||||
"APT_RECOVERY_PLANNING_PROOF_BINDING"
|
||||
);
|
||||
Ok(planning)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn recovery_planning_is_a_distinct_bound_partial_observation() {
|
||||
let mut p = planning_fixture();
|
||||
let r = rmm_domain::apt_transaction::AptNativeRecovery {
|
||||
schema_version: 1,
|
||||
previous_grant_id: Uuid::new_v4(),
|
||||
previous_attempt_id: Uuid::new_v4(),
|
||||
partial_proof_sha256: "a".repeat(64),
|
||||
};
|
||||
p.native_recovery = Some(r);
|
||||
assert!(p.validate(chrono::Utc::now()).is_err());
|
||||
p.fixture_package = Some(FixturePackage {
|
||||
version: "1.0".into(),
|
||||
status: "install ok unpacked".into(),
|
||||
});
|
||||
p.validate(chrono::Utc::now()).unwrap();
|
||||
p.fixture_package.as_mut().unwrap().status = "install ok installed".into();
|
||||
assert!(p.validate(chrono::Utc::now()).is_err());
|
||||
p.fixture_package.as_mut().unwrap().status = "install ok unpacked".into();
|
||||
p.native_recovery.as_mut().unwrap().previous_grant_id = Uuid::nil();
|
||||
assert!(p.validate(chrono::Utc::now()).is_err());
|
||||
}
|
||||
fn planning_fixture() -> PlanningResult {
|
||||
let raw =
|
||||
include_str!("../../../tests/apt_transaction/fixtures/planning-canonical.json").trim();
|
||||
let runtime = serde_json::from_str(raw).unwrap();
|
||||
PlanningResult {
|
||||
native_recovery: None,
|
||||
observed_at: chrono::Utc::now(),
|
||||
fixture_package: None,
|
||||
baseline_sha256: "b".repeat(64),
|
||||
|
||||
Reference in New Issue
Block a user