From fd7225600b2351b7b4a1f764008b5436951d8b14 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Fri, 13 Nov 2026 15:50:34 +0100 Subject: [PATCH] feat: preserve failed APT history for one signed UI-approved recovery --- README.md | 2 +- Rust_RMM_Implementierungsspezifikation.md | 18 +- crates/domain/src/apt_transaction.rs | 55 ++++- crates/privileged-helper/src/apt.rs | 249 ++++++++++++++++++++- crates/privileged-helper/src/apt/tests.rs | 249 +++++++++++++++++++++ crates/privileged-helper/src/main.rs | 3 + deploy/browser/cloud_apt_acceptance.cjs | 97 ++++++++ deploy/cloud_apt_retry.py | 210 +++++++++++++++++ deploy/cloud_apt_sandbox_upgrade.py | 21 +- deploy/cloud_server_upgrade.py | 125 +++++++++++ docs/development.md | 2 +- docs/handover.md | 10 + docs/implementation-status.md | 40 ++++ docs/project-decisions.md | 13 +- docs/server-deployment.md | 2 +- tests/apt_transaction/test_native_retry.py | 123 ++++++++++ tools/apt-transaction/HELPER-CONTRACT.md | 32 +++ tools/apt-transaction/apt_transaction.py | 128 +++++++++-- 18 files changed, 1345 insertions(+), 34 deletions(-) create mode 100644 deploy/browser/cloud_apt_acceptance.cjs create mode 100644 deploy/cloud_apt_retry.py create mode 100644 deploy/cloud_server_upgrade.py create mode 100644 tests/apt_transaction/test_native_retry.py diff --git a/README.md b/README.md index cdf34cb..0163090 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ > Deploymentstand: [docs/server-deployment.md](docs/server-deployment.md). > Die lokalen Serverbefehle und Slice-Beschreibungen unten sind historisch. -**Aktuell (Spezifikation 1.8):** Windows darf auf ausdrücklichen Userwunsch bereits +**Aktuell (Spezifikation 1.9):** Windows darf auf ausdrücklichen Userwunsch bereits parallel entwickelt werden. Windows-11-VM per abgesichertem Reverse-SSH mit Administratortoken erreichbar; noch kein RMM-Windows-Client installiert. Linux-Cloud-Agent, getrennte API/Gateway-Rollen, echte WebUI-Genehmigung und diff --git a/Rust_RMM_Implementierungsspezifikation.md b/Rust_RMM_Implementierungsspezifikation.md index c1a49e8..cab7adf 100644 --- a/Rust_RMM_Implementierungsspezifikation.md +++ b/Rust_RMM_Implementierungsspezifikation.md @@ -1,6 +1,6 @@ # Rust RMM: Architektur und Implementierungsspezifikation -Version 1.8 · 2026-11-13 · Sprache: Deutsch +Version 1.9 · 2026-11-13 · Sprache: Deutsch **Zweck:** Vollständige, eigenständig nutzbare Grundlage für die Implementierung einer mandantenfähigen Plattform zur Inventarisierung, Überwachung und Fernadministration von Kundengeräten durch eine Coding-KI und ein Entwicklungsteam. @@ -66,6 +66,22 @@ Deploymentdaten, offene Konfiguration und reale Nachweise: ### 0.1 Auftrag an die implementierende KI +**Präzisierung Version 1.9, 2026-11-13 — expliziter neuer APT-Testversuch:** +Ein bekannter Fehler vor der vertrauenswürdigen Hookübergabe darf nach belegtem +unverändertem vollständigem Paketbestand durch genau einen neuen festen, +separat WebUI-genehmigten Versuch des unveränderten Fixture-Rezepts geprüft +werden. Manifestschema 3 bindet hierfür den ursprünglichen Grant/Versuch und +Hash des unveränderten Create-once-Markers. Der Helper prüft unabhängig den +ursprünglichen signierten Auftrag und dessen eindeutiges `HOOK_NOT_HANDED_OFF` +im selben dauerhaften Ledger. Die alte Signaturprüfung ist ausschließlich +historisch und berechtigt keinen neuen Start. Alter Marker und vollständige +Historie bleiben erhalten; der neue Versuch erhält einen eigenen Create-once- +Retrymarker. Vorhandener Hook, unbekannte/partielle Wirkung, geänderte Baseline, +Identität, Generation oder Transaktion verweigern die Wiederaufnahme. Keine +automatische Wiederholung und keine allgemeine Freigabe unbekannter Wirkungen. +Schema 2 bleibt ohne Retryfeld bytekompatibel. Diese Vertragspräzisierung ist +kein Nachweis einer ausgeführten oder bestandenen Paketabnahme. + **Useroverride Version 1.8, 2026-11-13:** Der User erlaubt ausdrücklich, den Windows-11-Client bereits parallel zur offenen Linux-MVP-Abnahme zu entwickeln und auf seiner Windows-Test-VM nativ zu prüfen. Die bisherige diff --git a/crates/domain/src/apt_transaction.rs b/crates/domain/src/apt_transaction.rs index 5092f69..4e0b42b 100644 --- a/crates/domain/src/apt_transaction.rs +++ b/crates/domain/src/apt_transaction.rs @@ -33,6 +33,24 @@ pub struct PackageArchive { } pub type AptExpectation = ResolvedAptTransaction; pub type AptExpectedPackage = PackageArchive; +/// One explicitly approved recovery of a fixture acceptance that failed before +/// the trusted pre-dpkg hook. The original create-once marker remains intact. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct AptNativeRetry { + pub schema_version: u32, + pub previous_grant_id: uuid::Uuid, + pub previous_attempt_id: uuid::Uuid, + pub bootstrap_marker_sha256: String, +} +impl AptNativeRetry { + pub fn validate(&self) -> bool { + self.schema_version == 1 + && !self.previous_grant_id.is_nil() + && !self.previous_attempt_id.is_nil() + && digest(&self.bootstrap_marker_sha256) + } +} #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)] #[serde(deny_unknown_fields)] pub struct AptExecutionManifest { @@ -49,6 +67,8 @@ pub struct AptExecutionManifest { pub apt_config_sha256: String, pub runtime_sha256: String, pub test_result_id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub native_retry: Option, } impl ResolvedAptTransaction { pub fn validate_fixture(&self) -> bool { @@ -86,7 +106,11 @@ impl ResolvedAptTransaction { impl AptExecutionManifest { pub fn validate(&self) -> bool { self.kind == "rmm.apt.helper-action" - && self.schema_version == 2 + && match (&self.native_retry, self.schema_version) { + (None, 2) => true, + (Some(retry), 3) => self.action == "apt-native-acceptance" && retry.validate(), + _ => false, + } && matches!( self.action.as_str(), "apt-native-acceptance" | "apt-execute" @@ -320,8 +344,37 @@ mod tests { apt_config_sha256: hash(b"config"), runtime_sha256: hash(b"runtime"), test_result_id: "fixture-test-1".into(), + native_retry: None, }; assert!(m.validate()); + assert!( + !serde_json::to_value(&m) + .unwrap() + .as_object() + .unwrap() + .contains_key("native_retry") + ); + let mut retry = m.clone(); + retry.schema_version = 3; + assert!(!retry.validate()); + retry.native_retry = Some(AptNativeRetry { + schema_version: 1, + previous_grant_id: Uuid::new_v4(), + previous_attempt_id: Uuid::new_v4(), + bootstrap_marker_sha256: hash(b"immutable original marker"), + }); + assert!(retry.validate()); + let encoded_retry = crate::jobs::encode(&retry).unwrap(); + let parsed_retry: AptExecutionManifest = serde_json::from_slice(&encoded_retry).unwrap(); + assert_eq!(crate::jobs::encode(&parsed_retry).unwrap(), encoded_retry); + retry.schema_version = 2; + assert!(!retry.validate()); + retry.schema_version = 3; + retry.action = "apt-execute".into(); + assert!(!retry.validate()); + retry.action = "apt-native-acceptance".into(); + retry.native_retry.as_mut().unwrap().previous_attempt_id = Uuid::nil(); + assert!(!retry.validate()); let a = Action::AptNativeAcceptance { recipe_id: INSTALL_RECIPE.into(), recipe_version: "1".into(), diff --git a/crates/privileged-helper/src/apt.rs b/crates/privileged-helper/src/apt.rs index 58a88ef..2e1e77e 100644 --- a/crates/privileged-helper/src/apt.rs +++ b/crates/privileged-helper/src/apt.rs @@ -131,6 +131,8 @@ pub struct AptExecutionManifest { pub apt_config_sha256: String, pub runtime_sha256: String, pub test_result_id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub native_retry: Option, } #[derive(Deserialize)] @@ -180,6 +182,7 @@ pub fn policy_header(job: &JobPayload, config: &ProviderConfig) -> Result<()> { /// Opaque verified capability: callers cannot manufacture one from a manifest. pub struct PreparedApt { + binding: Binding, job: JobPayload, permit: ExecutionPermitPayload, request: Request, @@ -197,6 +200,7 @@ pub fn prepare( verify_installation()?; verify_prefetched(&manifest, &permit)?; Ok(PreparedApt { + binding: binding.clone(), job, permit, request: request.clone(), @@ -295,7 +299,13 @@ fn validate_manifest( config.validate()?; ensure!( m.kind == "rmm.apt.helper-action" - && m.schema_version == 2 + && match (&m.native_retry, m.schema_version) { + (None, 2) => true, + (Some(retry), 3) => { + action == "apt_native_acceptance" && retry.validate() + } + _ => false, + } && m.execution_profile == config.execution_profile && !m.test_result_id.is_empty() && m.test_result_id.len() <= 128 @@ -549,7 +559,243 @@ fn verify_prefetched(m: &AptExecutionManifest, permit: &ExecutionPermitPayload) Ok(()) } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct BootstrapAttempt { + transaction_sha256: String, + job_payload_sha256: String, + grant_id: uuid::Uuid, + attempt_id: uuid::Uuid, +} + +struct RetryEvidence { + marker: Vec, + context: Vec, + signed_action: Vec, + expectation: Vec, + baseline: Vec, + launch: Vec, + sandbox_result: Vec, +} + +struct RetryLedgerRecord { + agent: String, + generation: u64, + job: String, + attempt: String, + result: Option>, + start: Vec, + outcome: Option>, +} + +fn absent_nofollow(path: &Path) -> Result { + match fs::symlink_metadata(path) { + Ok(_) => Ok(false), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(true), + Err(error) => Err(error.into()), + } +} + 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<()> { + let Some(retry) = &self.manifest.native_retry else { + return Ok(()); + }; + let recipe = &self.manifest.resolved_apt_transaction.recipe_id; + self.manifest.resolved_apt_transaction.validate_fixture()?; + let base = Path::new(BASE); + for name in [ + format!("bootstrap-retry-{recipe}.json"), + format!("acceptance-{recipe}.json"), + format!("pending-acceptance-{recipe}.json"), + ] { + ensure!(absent_nofollow(&base.join(name))?, "APT_RETRY_ALREADY_USED"); + } + let active = base.join("active"); + let history = base + .join("history") + .join(retry.previous_grant_id.to_string()); + let active_is_prior = if absent_nofollow(&active)? { + false + } else { + secure(&active, 0, true, true)?; + let context: serde_json::Value = + serde_json::from_slice(&read_sealed(&active.join("context.json"), 8192)?)?; + context.get("grant_id") == Some(&serde_json::json!(retry.previous_grant_id)) + }; + ensure!( + !(active_is_prior && !absent_nofollow(&history)?), + "APT_RETRY_AMBIGUOUS_HISTORY" + ); + let prior = if active_is_prior { active } else { history }; + secure(&prior, 0, true, true)?; + ensure!( + absent_nofollow(&prior.join("hook-armed.json"))?, + "APT_RETRY_PRIOR_HOOK_ARMED" + ); + let evidence = RetryEvidence { + marker: read_sealed(&base.join(format!("bootstrap-attempt-{recipe}.json")), 8192)?, + context: read_sealed(&prior.join("context.json"), 8192)?, + signed_action: read_sealed(&prior.join("signed-action.json"), 65536)?, + expectation: read_sealed(&prior.join("expectation.json"), 65536)?, + baseline: read_sealed(&prior.join("baseline.json"), 1024 * 1024)?, + launch: read_sealed(&prior.join("launch.json"), 8192)?, + sandbox_result: read_sealed(&prior.join("sandbox-result.json"), 8192)?, + }; + self.verify_retry_evidence(db, &evidence) + } + + fn verify_retry_evidence( + &self, + db: &rusqlite::Connection, + evidence: &RetryEvidence, + ) -> Result<()> { + let retry = self + .manifest + .native_retry + .as_ref() + .context("APT_RETRY_REQUIRED")?; + ensure!( + self.manifest.schema_version == 3 + && self.manifest.action == "apt-native-acceptance" + && self.manifest.execution_profile == "dev-test" + && retry.validate() + && retry.previous_grant_id != self.permit.grant_id + && retry.previous_attempt_id != self.job.attempt_id + && hash(&evidence.marker) == retry.bootstrap_marker_sha256, + "APT_RETRY_BINDING" + ); + let marker: BootstrapAttempt = serde_json::from_slice(&evidence.marker)?; + ensure!( + marker.grant_id == retry.previous_grant_id + && marker.attempt_id == retry.previous_attempt_id + && marker.job_payload_sha256 == hash(&evidence.signed_action) + && marker.transaction_sha256 == hash(&evidence.expectation) + && marker.transaction_sha256 == self.manifest.transaction_sha256 + && evidence.expectation == self.manifest.transaction_bytes, + "APT_RETRY_ORIGINAL_MARKER" + ); + let prior: RetryLedgerRecord = db.query_row( + "SELECT c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c JOIN execution_evidence e ON e.grant_id=c.grant_id WHERE c.grant_id=?1", + [retry.previous_grant_id.to_string()], + |row| Ok(RetryLedgerRecord {agent: row.get(0)?, generation: row.get(1)?, + job: row.get(2)?, attempt: row.get(3)?, result: row.get(4)?, + start: row.get(5)?, outcome: row.get(6)?}), + )?; + let result_bytes = prior.result.context("APT_RETRY_PREVIOUS_UNRESOLVED")?; + let result: Outcome = serde_json::from_slice(&result_bytes)?; + let recorded_outcome: serde_json::Value = + serde_json::from_slice(&prior.outcome.context("APT_RETRY_OUTCOME_REQUIRED")?)?; + ensure!( + prior.agent == self.job.agent_id.to_string() + && prior.generation == self.job.enrollment_generation + && prior.attempt == retry.previous_attempt_id.to_string() + && prior.job != self.job.job_id.to_string() + && result.state == "failed" + && result.error_code == "HOOK_NOT_HANDED_OFF" + && !result.reboot_required + && result.evidence.get("test_acceptance") == Some(&serde_json::json!(true)) + && result.evidence.get("noProductionEvidence") == Some(&serde_json::json!(true)) + && recorded_outcome == serde_json::from_slice::(&result_bytes)?, + "APT_RETRY_PREVIOUS_NOT_KNOWN_FAILED" + ); + let start: serde_json::Value = serde_json::from_slice(&prior.start)?; + let request: Request = serde_json::from_value( + start + .get("request") + .context("APT_RETRY_START_REQUEST")? + .clone(), + )?; + let prior_permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?; + // Historical verification validates the original signature/bindings at + // its original not-before time; it grants no present execution right. + let (prior_job, prior_permit) = self.binding.verify(&request, prior_permit.not_before)?; + ensure!( + prior_permit.grant_id == retry.previous_grant_id + && prior_permit.start_before <= self.permit.not_before + && prior_permit.authorization_id != self.permit.authorization_id + && prior_permit.plan_hash != self.permit.plan_hash + && prior_job.job_id.to_string() == prior.job + && prior_job.attempt_id == retry.previous_attempt_id + && prior_job.execution_identity == "root" + && request.job_bytes == evidence.signed_action, + "APT_RETRY_PRIOR_SIGNED_REQUEST" + ); + let rmm_domain::jobs::Action::AptNativeAcceptance { + recipe_id, + recipe_version, + apt_manifest, + } = &prior_job.action + else { + bail!("APT_RETRY_PRIOR_NATIVE_ACTION_REQUIRED"); + }; + ensure!( + apt_manifest.schema_version == 2 + && apt_manifest.native_retry.is_none() + && recipe_id == &self.manifest.resolved_apt_transaction.recipe_id + && recipe_version == &self.manifest.resolved_apt_transaction.recipe_version + && apt_manifest.transaction_bytes == evidence.expectation + && apt_manifest.transaction_sha256 == marker.transaction_sha256 + && hash(&evidence.baseline) + == self.manifest.resolved_apt_transaction.baseline_sha256, + "APT_RETRY_PRIOR_RECIPE_OR_BASELINE" + ); + let context: serde_json::Value = serde_json::from_slice(&evidence.context)?; + for (name, expected) in [ + ("schema_version", serde_json::json!(2)), + ( + "transaction_sha256", + serde_json::json!(marker.transaction_sha256), + ), + ( + "job_payload_sha256", + serde_json::json!(marker.job_payload_sha256), + ), + ("grant_id", serde_json::json!(prior_permit.grant_id)), + ("job_id", serde_json::json!(prior_job.job_id)), + ("attempt_id", serde_json::json!(prior_job.attempt_id)), + ( + "authorization_id", + serde_json::json!(prior_permit.authorization_id), + ), + ("plan_sha256", serde_json::json!(prior_permit.plan_hash)), + ("action", serde_json::json!("apt-native-acceptance")), + ("execution_profile", serde_json::json!("dev-test")), + ( + "not_before_ms", + serde_json::json!(prior_permit.not_before.timestamp_millis()), + ), + ( + "start_before_ms", + serde_json::json!(prior_permit.start_before.timestamp_millis()), + ), + ] { + ensure!( + context.get(name) == Some(&expected), + "APT_RETRY_PRIOR_CONTEXT" + ); + } + let ledger_id = context + .get("effect_armed_ledger_id") + .context("APT_RETRY_PRIOR_LEDGER")?; + ensure!( + result.evidence.get("effect_armed_ledger_id") == Some(ledger_id), + "APT_RETRY_PRIOR_LEDGER" + ); + ensure!( + serde_json::from_slice::(&evidence.launch)? + == serde_json::json!({"transaction_sha256":marker.transaction_sha256, + "effect_armed_ledger_id":ledger_id}) + && serde_json::from_slice::(&evidence.sandbox_result)? + == serde_json::json!({"transaction_sha256":marker.transaction_sha256, + "apt_exit_code":100,"reboot_required":false}), + "APT_RETRY_PRIOR_NO_EFFECT_PROOF" + ); + Ok(()) + } + fn verify_parent_consumption(&self, db: &rusqlite::Connection) -> Result { let row: (i64, String, u64, String, String, Option>) = db.query_row( "SELECT rowid,agent,generation,job,attempt,result FROM consumed WHERE grant_id=?1", @@ -596,6 +842,7 @@ impl PreparedApt { pub fn execute(self, db: &rusqlite::Connection) -> Result { ensure_root()?; let ledger_id = self.verify_parent_consumption(db)?; + self.verify_retry_history(db)?; let active = self.stage(db, &ledger_id)?; // No timeout killing the native package manager. Client timeout means // unknown; this helper continues and journals the eventual real outcome. diff --git a/crates/privileged-helper/src/apt/tests.rs b/crates/privileged-helper/src/apt/tests.rs index a6d4da4..e30c0e8 100644 --- a/crates/privileged-helper/src/apt/tests.rs +++ b/crates/privileged-helper/src/apt/tests.rs @@ -38,6 +38,253 @@ fn actual_domain_native_action_roundtrips_exact_signed_manifest_to_helper() { assert!(binding.verify(&request, now).is_err()); } +#[test] +fn retry_schema_requires_an_explicit_native_reference_and_preserves_legacy_bytes() { + let (config, mut m) = manifest(true); + assert!( + serde_json::to_value(&m) + .unwrap() + .get("native_retry") + .is_none() + ); + m.schema_version = 3; + assert!(validate_manifest(&m, "apt_native_acceptance", &config).is_err()); + m.native_retry = Some(rmm_domain::apt_transaction::AptNativeRetry { + schema_version: 1, + previous_grant_id: Uuid::new_v4(), + previous_attempt_id: Uuid::new_v4(), + bootstrap_marker_sha256: hash(b"original"), + }); + validate_manifest(&m, "apt_native_acceptance", &config).unwrap(); + m.schema_version = 2; + assert!(validate_manifest(&m, "apt_native_acceptance", &config).is_err()); + m.schema_version = 3; + m.action = "apt-execute".into(); + assert!(validate_manifest(&m, "ensure_package", &config).is_err()); + m.action = "apt-native-acceptance".into(); + m.native_retry.as_mut().unwrap().bootstrap_marker_sha256 = "invalid".into(); + assert!(validate_manifest(&m, "apt_native_acceptance", &config).is_err()); +} + +fn resign( + request: &mut Request, + job: &JobPayload, + permit: &mut ExecutionPermitPayload, + key: &PKey, +) { + request.job_bytes = encode(job).unwrap(); + permit.job_payload_hash = hash(&request.job_bytes); + request.permit_bytes = encode(permit).unwrap(); + request.signature = Signer::new_without_digest(key) + .unwrap() + .sign_oneshot_to_vec(&request.permit_bytes) + .unwrap(); + request.authorization_id = permit.authorization_id; + request.plan_hash = permit.plan_hash.clone(); +} + +fn retry_fixture() -> ( + PreparedApt, + RetryEvidence, + rusqlite::Connection, + PathBuf, + ExecutionPermitPayload, +) { + let (binding, mut request, key, now) = signed_request(); + let (mut job, mut permit) = binding.verify(&request, now).unwrap(); + let (_, manifest) = manifest(true); + job.action = Action::AptNativeAcceptance { + recipe_id: INSTALL_RECIPE.into(), + recipe_version: "1".into(), + apt_manifest: serde_json::from_value(serde_json::to_value(&manifest).unwrap()).unwrap(), + }; + permit.action = "apt_native_acceptance".into(); + resign(&mut request, &job, &mut permit, &key); + binding.verify(&request, now).unwrap(); + let dir = temporary(); + // SAFETY: geteuid has no pointers or preconditions. + let mut db = crate::open_ledger(&dir, unsafe { libc::geteuid() }).unwrap(); + crate::consume(&mut db, &permit).unwrap(); + crate::save_start(&db, &permit, &request).unwrap(); + let ledger = format!("apt-{}-1", permit.grant_id); + let outcome = Outcome { + state: "failed".into(), + evidence: serde_json::json!({"postconditions_passed":false,"apt":{ + "test_acceptance":true,"noProductionEvidence":true}, + "effect_armed_ledger_id":ledger,"test_acceptance":true,"noProductionEvidence":true}), + ..Outcome::unknown("HOOK_NOT_HANDED_OFF") + }; + crate::finish(&db, &permit, &outcome).unwrap(); + let marker = serde_json::to_vec(&serde_json::json!({ + "transaction_sha256":manifest.transaction_sha256, + "job_payload_sha256":hash(&request.job_bytes), + "grant_id":permit.grant_id,"attempt_id":job.attempt_id, + })) + .unwrap(); + let evidence = RetryEvidence { + marker: marker.clone(), + context: serde_json::to_vec(&serde_json::json!({ + "schema_version":2,"transaction_sha256":manifest.transaction_sha256, + "job_payload_sha256":hash(&request.job_bytes),"action":"apt-native-acceptance", + "execution_profile":"dev-test","effect_armed_ledger_id":ledger, + "grant_id":permit.grant_id,"job_id":job.job_id,"attempt_id":job.attempt_id, + "authorization_id":permit.authorization_id,"plan_sha256":permit.plan_hash, + "not_before_ms":permit.not_before.timestamp_millis(), + "start_before_ms":permit.start_before.timestamp_millis(), + "boot_id":"prior-boot","monotonic_start_before_ns":123, + })).unwrap(), + signed_action: request.job_bytes.clone(), expectation: manifest.transaction_bytes.clone(), + baseline: b"{}".to_vec(), + launch: serde_json::to_vec(&serde_json::json!({ + "transaction_sha256":manifest.transaction_sha256,"effect_armed_ledger_id":ledger, + })).unwrap(), + sandbox_result: serde_json::to_vec(&serde_json::json!({ + "transaction_sha256":manifest.transaction_sha256,"apt_exit_code":100,"reboot_required":false, + })).unwrap(), + }; + let previous_permit = permit.clone(); + let mut new_manifest = manifest; + new_manifest.schema_version = 3; + new_manifest.native_retry = Some(rmm_domain::apt_transaction::AptNativeRetry { + schema_version: 1, + previous_grant_id: permit.grant_id, + previous_attempt_id: job.attempt_id, + bootstrap_marker_sha256: hash(&marker), + }); + job.job_id = Uuid::new_v4(); + job.attempt_id = Uuid::new_v4(); + job.not_before = permit.start_before + chrono::Duration::seconds(1); + job.start_before = job.not_before + chrono::Duration::seconds(59); + job.action = Action::AptNativeAcceptance { + recipe_id: INSTALL_RECIPE.into(), + recipe_version: "1".into(), + apt_manifest: serde_json::from_value(serde_json::to_value(&new_manifest).unwrap()).unwrap(), + }; + permit.grant_id = Uuid::new_v4(); + permit.authorization_id = Uuid::new_v4(); + permit.plan_hash = hash(b"new fixed UI approved retry plan"); + permit.job_id = job.job_id; + permit.attempt_id = job.attempt_id; + permit.not_before = job.not_before; + permit.start_before = job.start_before; + resign(&mut request, &job, &mut permit, &key); + binding.verify(&request, permit.not_before).unwrap(); + ( + PreparedApt { + binding, + job, + permit, + request, + manifest: new_manifest, + }, + evidence, + db, + dir, + previous_permit, + ) +} + +#[test] +fn native_retry_requires_original_signed_known_failure_and_unchanged_baseline() { + let (mut prepared, mut evidence, db, dir, previous) = retry_fixture(); + prepared.verify_retry_evidence(&db, &evidence).unwrap(); + let baseline = evidence.baseline.clone(); + evidence.baseline.push(b' '); + assert!(prepared.verify_retry_evidence(&db, &evidence).is_err()); + evidence.baseline = baseline; + let marker = evidence.marker.clone(); + evidence.marker.push(b' '); + assert!(prepared.verify_retry_evidence(&db, &evidence).is_err()); + evidence.marker = marker; + let old_attempt = prepared.job.attempt_id; + prepared.job.attempt_id = previous.attempt_id; + assert!(prepared.verify_retry_evidence(&db, &evidence).is_err()); + prepared.job.attempt_id = old_attempt; + let old_signature: Vec = db + .query_row( + "SELECT start FROM execution_evidence WHERE grant_id=?1", + [previous.grant_id.to_string()], + |r| r.get(0), + ) + .unwrap(); + let mut start: serde_json::Value = serde_json::from_slice(&old_signature).unwrap(); + let signature = start["request"]["signature"][0].as_u64().unwrap(); + start["request"]["signature"][0] = serde_json::json!(signature ^ 1); + db.execute( + "UPDATE execution_evidence SET start=?2 WHERE grant_id=?1", + rusqlite::params![ + previous.grant_id.to_string(), + serde_json::to_vec(&start).unwrap() + ], + ) + .unwrap(); + assert!(prepared.verify_retry_evidence(&db, &evidence).is_err()); + db.execute( + "UPDATE execution_evidence SET start=?2 WHERE grant_id=?1", + rusqlite::params![previous.grant_id.to_string(), old_signature], + ) + .unwrap(); + prepared.verify_retry_evidence(&db, &evidence).unwrap(); + db.execute( + "UPDATE consumed SET result=NULL WHERE grant_id=?1", + [previous.grant_id.to_string()], + ) + .unwrap(); + assert!(prepared.verify_retry_evidence(&db, &evidence).is_err()); + drop(db); + fs::remove_dir_all(dir).unwrap(); +} + +#[test] +fn native_retry_rejects_wrong_result_context_and_any_possible_effect() { + let (prepared, evidence, db, dir, previous) = retry_fixture(); + let mut value: serde_json::Value = serde_json::from_slice(&evidence.sandbox_result).unwrap(); + value["apt_exit_code"] = serde_json::json!(0); + let mut bad = RetryEvidence { + sandbox_result: serde_json::to_vec(&value).unwrap(), + marker: evidence.marker.clone(), + context: evidence.context.clone(), + signed_action: evidence.signed_action.clone(), + expectation: evidence.expectation.clone(), + baseline: evidence.baseline.clone(), + launch: evidence.launch.clone(), + }; + assert!(prepared.verify_retry_evidence(&db, &bad).is_err()); + bad.sandbox_result = evidence.sandbox_result.clone(); + let mut context: serde_json::Value = serde_json::from_slice(&bad.context).unwrap(); + context["authorization_id"] = serde_json::json!(Uuid::new_v4()); + bad.context = serde_json::to_vec(&context).unwrap(); + assert!(prepared.verify_retry_evidence(&db, &bad).is_err()); + let result: Vec = db + .query_row( + "SELECT result FROM consumed WHERE grant_id=?1", + [previous.grant_id.to_string()], + |r| r.get(0), + ) + .unwrap(); + for state in ["unknown", "succeeded"] { + let mut changed: Outcome = serde_json::from_slice(&result).unwrap(); + changed.state = state.into(); + let changed = serde_json::to_vec(&changed).unwrap(); + db.execute( + "UPDATE consumed SET result=?2 WHERE grant_id=?1", + rusqlite::params![previous.grant_id.to_string(), &changed], + ) + .unwrap(); + db.execute( + "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", + rusqlite::params![previous.grant_id.to_string(), &changed], + ) + .unwrap(); + assert!(prepared.verify_retry_evidence(&db, &evidence).is_err()); + } + let path = dir.join("hook-armed.json"); + std::os::unix::fs::symlink(dir.join("missing-target"), &path).unwrap(); + assert!(!absent_nofollow(&path).unwrap()); + drop(db); + fs::remove_dir_all(dir).unwrap(); +} + #[test] fn prefetch_receipt_must_precede_grant_and_match_exact_archive() { let (_, m) = manifest(true); @@ -70,6 +317,7 @@ fn consumed_parent_row_and_original_start_request_are_mandatory() { // Test-only construction exercises the ledger validator without granting // access to the root process path or weakening the public constructor. let prepared = PreparedApt { + binding, job, permit: permit.clone(), request: request.clone(), @@ -149,6 +397,7 @@ fn manifest(bootstrap: bool) -> (ProviderConfig, AptExecutionManifest) { apt_config_sha256: config.apt_config_sha256.clone(), runtime_sha256: config.runtime_sha256.clone(), test_result_id: "fixture-test-1".into(), + native_retry: None, }; (config, manifest) } diff --git a/crates/privileged-helper/src/main.rs b/crates/privileged-helper/src/main.rs index c83d6bc..cbe8562 100644 --- a/crates/privileged-helper/src/main.rs +++ b/crates/privileged-helper/src/main.rs @@ -433,6 +433,9 @@ fn handle( } else { None }; + if let Some(apt) = &apt { + apt.verify_retry_history(db)?; + } consume(db, &permit)?; rmm_privileged_helper::save_start(db, &permit, &request)?; let mut outcome = diff --git a/deploy/browser/cloud_apt_acceptance.cjs b/deploy/browser/cloud_apt_acceptance.cjs new file mode 100644 index 0000000..0c73bc9 --- /dev/null +++ b/deploy/browser/cloud_apt_acceptance.cjs @@ -0,0 +1,97 @@ +// Actual UI only. No injected fetch, request context or direct approval API. +const fs = require('node:fs'); +const path = require('node:path'); +const crypto = require('node:crypto'); +const { isDeepStrictEqual: equal } = require('node:util'); +const { chromium } = require('../../web/node_modules/playwright'); +const root = path.resolve(__dirname, '../..'); +const assert = v => { if (!v) throw new Error('APT UI contract mismatch'); }; +function readPrivate(file) { + const s = fs.lstatSync(file); + assert(s.isFile() && !s.isSymbolicLink() && s.uid === process.getuid() && (s.mode & 0o777) === 0o600 && s.size < 1048576); + return JSON.parse(fs.readFileSync(file, 'utf8')); +} +function checkAction(p, expected) { + const native = ['apt_install_retry', 'apt_native_update'].includes(expected.case); + const version = expected.case === 'apt_install_retry' ? '1.0' : '1.1'; + const recipe = version === '1.0' ? 'rmm-apt-fixture-install-1.0' : 'rmm-apt-fixture-update-1.1'; + const action = p.action; const m = action.apt_manifest; const tx = m.resolved_apt_transaction; + assert(action.action === (native ? 'apt_native_acceptance' : expected.case)); + assert(p.execution_identity === 'root' && p.max_runtime_seconds === 300 && p.max_output_bytes === 65536); + assert(m.type === 'rmm.apt.helper-action' && m.execution_profile === 'dev-test' && m.action === (native ? 'apt-native-acceptance' : 'apt-execute')); + assert(m.schema_version === (expected.case === 'apt_install_retry' ? 3 : 2)); + assert(tx.recipe_id === recipe && tx.recipe_version === '1' && tx.no_reboot === true && tx.packages.length === 1); + const pkg = tx.packages[0]; + const sha = version === '1.0' ? '74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867' : 'ef67c9fe0ba1443fcfe465768e1204763063c369a2a1da04af6027d8a9c00491'; + assert(pkg.name === 'rmm-mvp-test' && pkg.architecture === 'all' && pkg.origin === 'rmm-test' && pkg.new_version === version && pkg.sha256 === sha && pkg.size === 20672); + assert(crypto.createHash('sha256').update(Buffer.from(m.transaction_bytes)).digest('hex') === m.transaction_sha256); + assert(equal(JSON.parse(Buffer.from(m.transaction_bytes)), tx)); + assert(equal(m, expected.request.parameters.apt_manifest)); + if (expected.case === 'apt_install_retry') { + assert(m.native_retry.schema_version === 1 && m.native_retry.bootstrap_marker_sha256 === 'ec84aa4df8065f0b54b282bd273a6f342f07a6d47312289608389d05c7fb6053'); + assert(m.native_retry.previous_attempt_id !== p.attempt_id); + } +} +(async () => { + const file = path.resolve(process.argv[2]); const expected = readPrivate(file); + assert(expected.profile === 'dev/test' && ['apt_install_retry', 'apt_native_update', 'ensure_package', 'apply_updates'].includes(expected.case) && expected.expected_principal === 'simulated-testprincipal-a'); + const login = readPrivate(path.join(root, 'credentials/server-browser-login.json')); + const browsers = path.join(root, 'web/node_modules/.cache/playwright-browsers'); + const version = fs.readdirSync(browsers).find(x => /^chromium-/.test(x)); + const executablePath = ['chrome-linux/chrome', 'chrome-linux64/chrome'].map(x => path.join(browsers, version, x)).find(x => fs.existsSync(x)); + const browser = await chromium.launch({ headless: true, executablePath, env: { ...process.env, HOME: path.join(root, 'web/node_modules/.cache/cloud-acceptance/home') } }); + try { + const page = await browser.newPage({ ignoreHTTPSErrors: false, timezoneId: 'UTC' }); page.setDefaultTimeout(15000); + let approvals = 0; + page.on('request', r => { if (new URL(r.url()).pathname.endsWith('/authorizations')) approvals++; }); + await page.goto('https://localhost:18443/'); + await page.getByLabel('Benutzername').fill(login.username); await page.getByLabel('Passwort', { exact: true }).fill(login.password); + const response = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/session' && r.status() === 200); + await page.getByRole('button', { name: 'Anmelden', exact: true }).click(); + const session = await (await response).json(); + assert(session.testsimulation === true && session.approval_origin === 'dev_test_browser_simulation' && session.principal === expected.expected_principal); + await page.getByRole('status').filter({ hasText: 'SIMULIERT' }).waitFor(); + const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) }); + if (expected.phase === 'prepare') { + await page.getByRole('checkbox', { name: 'Ziel ' + expected.asset_id, exact: true }).check(); + await page.getByLabel('Aktion', { exact: true }).selectOption(expected.request.action); + await page.getByLabel('Aktionsparameter', { exact: true }).fill(JSON.stringify(expected.request.parameters)); + await page.getByLabel('Ausführungsidentität', { exact: true }).selectOption('root'); + await page.getByLabel('Laufzeitlimit (s)', { exact: true }).fill('300'); await page.getByLabel('Ausgabelimit (Bytes)', { exact: true }).fill('65536'); + const start = new Date(Math.ceil(Date.now() / 60000) * 60000); + await page.getByLabel('Frühester Start (lokale Zeit)', { exact: true }).fill(start.toISOString().slice(0, 16)); + await page.getByLabel('Startfrist / Ablauf (lokale Zeit)', { exact: true }).fill(new Date(start.getTime() + 15 * 60000).toISOString().slice(0, 16)); + await page.getByLabel('Maximal parallel', { exact: true }).fill('1'); await page.getByLabel('Stopp nach Fehlern', { exact: true }).fill('1'); + await page.getByLabel('Begründung', { exact: true }).fill(expected.request.reason); + await page.getByRole('button', { name: 'Plan erstellen · noch keine Ausführung' }).click(); + await detail.locator('pre').waitFor(); const plan = JSON.parse(await detail.locator('pre').innerText()); + assert(plan.jobs.length === 1); checkAction(plan.jobs[0].payload, expected); + assert(crypto.createHash('sha256').update(Buffer.from(plan.plan_payload_bytes)).digest('hex') === plan.plan_hash); + await detail.getByRole('button', { name: 'Antrag absenden · pending_approval' }).click(); + const op = page.locator('section').filter({ has: page.getByRole('heading', { name: /^Antrag \/ Ausführung / }) }); + await op.locator('pre').first().filter({ hasText: '"job_count": 0' }).waitFor(); const operation = JSON.parse(await op.locator('pre').first().innerText()); + assert(operation.approval_state === 'pending_approval' && operation.execution_state === null && approvals === 0); + assert(path.dirname(path.resolve(expected.output_file)) === path.dirname(file)); + fs.writeFileSync(expected.output_file, JSON.stringify({ plan, operation }), { flag: 'wx', mode: 0o600 }); + } else { + assert(expected.phase === 'approve'); await page.getByRole('button', { name: expected.operation_id, exact: true }).click(); + const op = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Antrag / Ausführung ' + expected.operation_id, exact: true }) }); + const pending = JSON.parse(await op.locator('pre').first().innerText()); + assert(pending.approval_state === 'pending_approval' && pending.job_count === 0 && pending.execution_state === null); + await op.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen' }).click(); + const plan = JSON.parse(await detail.locator('pre').innerText()); + assert(equal(plan, expected.plan) && plan.jobs.length === 1 && plan.resolved_targets.length === 1); + assert(crypto.createHash('sha256').update(Buffer.from(plan.plan_payload_bytes)).digest('hex') === plan.plan_hash); + const p = plan.jobs[0].payload; checkAction(p, expected); + assert(p.agent_id === expected.agent_id && p.asset_id === expected.asset_id && p.tenant_id === expected.tenant_id && Date.parse(p.start_before) > Date.now()); + const button = detail.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen', exact: true }); assert(await button.isDisabled()); + await detail.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check(); await button.click(); + await op.locator('p.warning').filter({ hasText: 'Antrag: approved' }).waitFor(); + assert(approvals === 1); + } + await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); + } finally { await browser.close(); } +})().catch(error => { + const line = error.stack?.match(/cloud_apt_acceptance\.cjs:(\d+):\d+/)?.[1] || 'unknown'; + console.log('CLOUD_APT_UI_INCOMPLETE:' + error.constructor.name + ':source_line=' + line); process.exitCode = 1; +}); diff --git a/deploy/cloud_apt_retry.py b/deploy/cloud_apt_retry.py new file mode 100644 index 0000000..4bf2cfd --- /dev/null +++ b/deploy/cloud_apt_retry.py @@ -0,0 +1,210 @@ +#!/usr/bin/env python3 +"""Single explicitly signed native recovery, then separate fixed fixture jobs. + +No direct approval, package-manager SSH execution, history reset or implicit +retry. Existing checkpoints may only be read to finish/audit that SAME job. +""" +import argparse +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import subprocess +import time +import uuid + +import cloud_apt_followup as prior +import cloud_root_evidence +import cloud_root_policy +import vm_mvp_adapters as adapters + +cloud = prior.cloud +require = cloud.require +STATE = cloud.ROOT / 'devstate/cloud-apt-retry' +CASES = ('apt_install_retry', 'apt_native_update', 'ensure_package', 'apply_updates') + + +def switch_endpoint(payload): + import sqlite3 + os.umask(0o077) + binary = Path('/usr/local/libexec/rmm-privileged-helper') + require(hashlib.sha256(binary.read_bytes()).hexdigest() == payload['binary_sha256'], 'reviewed helper required') + state = Path('/var/lib/rmm-agent-remote') + require(json.loads((state/'identity.json').read_text())['agent_id'] == payload['agent_id'], 'fixed cloud identity') + for database, query in ((state/'journal.sqlite', "SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')"), + (Path('/var/lib/rmm-helper/consumption.sqlite'), 'SELECT count(*) FROM consumed WHERE result IS NULL')): + with sqlite3.connect(database.as_uri()+'?mode=ro', uri=True) as db: + require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok' and db.execute(query).fetchone()[0] == 0, 'unresolved native attempt') + path = Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf') + full = ('[Service]\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper-cloud/control.sock\n' + 'Environment=RMM_HELPER_POLICY_FILE=/etc/rmm-remote/general-helper-proof.json\n' + 'Environment=RMM_APT_HELPER_SOCKET=/run/rmm-apt-helper/helper.sock\n' + 'Environment=RMM_APT_HELPER_PROOF_FILE=/etc/rmm-remote/apt-helper-proof.json\n' + 'Environment=RMM_LOG_UNITS_FILE=/etc/rmm-remote/log-units.json\n') + reduced = ''.join(line+'\n' for line in full.splitlines() if not line.startswith('Environment=RMM_APT_HELPER_')) + previous, updated = (reduced, full) if payload['enabled'] else (full, reduced) + require(not path.is_symlink() and path.stat().st_uid == 0 and path.read_text() == previous, 'known drop-in required') + backup = Path('/var/lib/rmm-bootstrap-history') / ('cloud-apt-endpoint-'+payload['id']) + backup.mkdir(mode=0o700) + with (backup/'previous.conf').open('xb') as f: + f.write(previous.encode()); f.flush(); os.fsync(f.fileno()) + if payload['enabled']: + subprocess.run(['systemctl', 'start', 'rmm-apt-helper.service'], capture_output=True, check=True, timeout=30) + next_path = path.with_suffix('.retry-next') + fd = os.open(next_path, os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, 'w') as f: + f.write(updated); f.flush(); os.fsync(f.fileno()); os.fchmod(f.fileno(), 0o644) + os.replace(next_path, path) + subprocess.run(['systemctl', 'daemon-reload'], capture_output=True, check=True, timeout=30) + subprocess.run(['systemctl', 'restart', 'rmm-agent-remote.service'], capture_output=True, check=True, timeout=30) + if not payload['enabled']: + subprocess.run(['systemctl', 'stop', 'rmm-apt-helper.service'], capture_output=True, check=True, timeout=30) + return dict(enabled=payload['enabled'], package_effects=False, backup=str(backup)) + + +def replace_policy(payload): + import datetime as dt + p = Path('/opt/rust-discovery/devstate/root-policy/policy.json') + require(not p.is_symlink() and p.stat().st_uid == 10001 and p.stat().st_mode&0o777 == 0o600, 'safe policy required') + old = p.read_bytes() + require(hashlib.sha256(old).hexdigest() == payload['previous_sha256'] and + dt.datetime.fromisoformat(json.loads(old)['expires_at']) < dt.datetime.now(dt.timezone.utc), 'previous policy must be closed') + policy = payload['policy']; require(len(policy['scopes']) == 1, 'one APT target only') + scope = policy['scopes'][0] + require(not scope['allow_root_scripts'] and not scope['allow_reboot'] and not scope['allowed_services'] and + scope['principals'] == ['simulated-testprincipal-a'] and scope['apt']['allow_native_acceptance'] and + scope['apt']['allow_execute'], 'APT-only simulation limits') + backup = p.parents[1] / ('root-policy-bootstrap-'+payload['id']); backup.mkdir(mode=0o700) + with (backup/'previous-policy.json').open('xb') as f: + f.write(old); f.flush(); os.fsync(f.fileno()) + next_path = p.with_suffix('.retry-next') + fd = os.open(next_path, os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'w') as f: + json.dump(policy, f); f.flush(); os.fsync(f.fileno()); os.fchown(f.fileno(), 10001, 10001) + os.replace(next_path, p) + return dict(policy_sha256=hashlib.sha256(p.read_bytes()).hexdigest(), backup=str(backup), approvals_created=False) + + +def enable(): + m = cloud.contract; require(prior.evidence('gate')['unresolved'] == 0, 'unresolved central job') + reports = [m.protected_json(p) for p in prior.STATE.glob('sandbox-*.json')] + code_sha = hashlib.sha256((cloud.ROOT/'tools/apt-transaction/apt_transaction.py').read_bytes()).hexdigest() + # Exactly the reviewed current compiled provider, never an old readiness. + ready = next(r for r in reports if r['completed'] and r.get('provider_sha256') == code_sha) + target = prior.identity(); binding = ready['apt_pin']['identity'] + require(all(binding[k] == target[k] for k in target), 'native helper target binding') + helper = (prior.STATE/'helper-native-retry').read_bytes() + endpoint = cloud.bootstrap.rpc('linux-vm.conf', switch_endpoint, dict(id=str(uuid.uuid4()), enabled=True, + agent_id=target['agent_id'], binary_sha256=hashlib.sha256(helper).hexdigest())) + m.write_private(STATE/'endpoint-enabled.json', endpoint) + peers = cloud.bootstrap.rpc('linux-vm.conf', cloud_root_evidence.inspect_helpers, target) + require(peers['apt']['fixture_acceptance_ready'] and peers['ledger']['faults'] == 0, 'actual shared-ledger native readiness') + scope = {k:binding[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')} + scope.update(principals=['simulated-testprincipal-a'], helper_policy_sha256=ready['apt_pin']['provider_stack_sha256'], + acceptance_evidence_id='cloud-signed-single-retry:'+ready['runtime_sha256'], allowed_services=[], allow_root_scripts=False, allow_reboot=False, + apt=dict(allow_native_acceptance=True, allow_execute=True, runtime_sha256=ready['runtime_sha256'], + apt_config_sha256=ready['apt_config_sha256'], provider_sha256=code_sha, + hook_sha256=hashlib.sha256((cloud.ROOT/'tools/apt-transaction/hook').read_bytes()).hexdigest())) + policy = dict(schema_version=1, profile='development', expires_at=(dt.datetime.now(dt.timezone.utc)+dt.timedelta(hours=2)).isoformat(), scopes=[scope]) + old = m.protected_json(prior.STATE/'diagnostic-policy-closed.json') + result = cloud.bootstrap.rpc('server.conf', replace_policy, dict(id=str(uuid.uuid4()), previous_sha256=old['policy_sha256'], policy=policy)) + m.write_private(STATE/'policy.json', dict(result, policy=policy, peers=peers)) + print('APT-only expiring limits installed; no job or approval created') + + +def accept(case): + m = cloud.contract; target = prior.identity(); backend = cloud.Backend() + require(prior.evidence('gate')['unresolved'] == 0, 'unresolved central job; no new attempt') + device = backend.call('inventory.get_device', {k:target[k] for k in ('tenant_id','asset_id')}) + require(device['presence'] == 'online', 'online cloud VM required') + planning = adapters.planning_from_device(device) + source_case = 'apt_native_install' if case == 'apt_install_retry' else case + request = m.case_input(source_case, target, planning=planning) + if case == 'apt_install_retry': + old = m.protected_json(cloud.ROOT/'devstate/cloud-root/cases/apt_native_install/checkpoint.json') + require(old['result']['state'] == 'failed' and old['result']['result']['error_code'] == 'HOOK_NOT_HANDED_OFF', 'original definitive pre-hook failure') + manifest = request['parameters']['apt_manifest'] + manifest.update(schema_version=3, native_retry=dict(schema_version=1, + previous_grant_id=old['result']['result']['grant_id'], previous_attempt_id=old['job']['attempt_id'], + bootstrap_marker_sha256='ec84aa4df8065f0b54b282bd273a6f342f07a6d47312289608389d05c7fb6053')) + request['reason'] = 'One new UI-approved fixture attempt: preserve original failed attempt, verify known no-handoff history, separate retry tombstone; never reboot' + require({'rmm.sync.v1','helper.root.v1','root:'+request['action'],request['action']} <= set(device['capabilities']), 'native root capability required') + folder = STATE/case; folder.mkdir(mode=0o700, exist_ok=False) + common = dict(profile='dev/test', case=case, expected_principal='simulated-testprincipal-a', **target) + prepare = folder/'prepare.json'; output = folder/'prepared.json' + m.write_private(prepare, dict(common, phase='prepare', request=request, output_file=str(output))) + cloud.prepare_browser() + def browser(file): + result = subprocess.run(['node', str(cloud.ROOT/'deploy/browser/cloud_apt_acceptance.cjs'), str(file)], capture_output=True, timeout=120) + require(result.returncode == 0, 'actual UI incomplete; preserve checkpoint, never repeat automatically') + print(case+': actual UI preparation', flush=True); browser(prepare) + prepared = m.protected_json(output); plan = prepared['plan']; operation = prepared['operation']; job = plan['jobs'][0] + require(len(plan['jobs']) == 1 and hashlib.sha256(bytes(plan['plan_payload_bytes'])).hexdigest() == plan['plan_hash'], 'immutable single job') + before = prior.evidence('gate', job_id=job['job_id']) + pending = backend.call('jobs.get', dict(tenant_id=target['tenant_id'], operation_id=operation['operation_id'])) + require(pending['approval_state'] == 'pending_approval' and pending['job_count'] == 0 and pending['execution_state'] is None and + pending['plan_hash'] == plan['plan_hash'] and all(before[k] == 0 for k in ('jobs','outbox','permits')), 'pending zero execution artifacts') + checkpoint = dict(common, plan=plan, request=request, operation_id=operation['operation_id'], preapproval=before, stage='pending_approval') + m.write_private(folder/'checkpoint.json', checkpoint) + approval = folder/'approve.json'; m.write_private(approval, dict(checkpoint, phase='approve')) + print(case+': pending 0/0/0 verified; separate actual UI approval', flush=True); browser(approval) + finish(case) + + +def finish(case): + m = cloud.contract; folder = STATE/case; checkpoint = m.protected_json(folder/'checkpoint.json') + job = checkpoint['plan']['jobs'][0]; backend = cloud.Backend(); end = time.monotonic()+420 + while True: + result = backend.call('jobs.get', dict(tenant_id=checkpoint['tenant_id'], job_id=job['job_id'])) + if result['state'] in m.TERMINAL or result['state'] in ('unknown','awaiting_reboot'): break + require(time.monotonic() < end, 'unconfirmed effect; no retry'); time.sleep(3) + checkpoint.update(stage=result['state'], result=result) + def save(): + path = folder/'checkpoint.json'; path.write_text(json.dumps(checkpoint, indent=2)); path.chmod(0o600) + save() + after = prior.evidence('gate', job_id=job['job_id']); audit = prior.evidence('audit', operation_id=checkpoint['operation_id']) + require([after[k] for k in ('jobs','outbox','permits')] == [1,1,1] and audit == dict(origin='dev_test_browser_simulation', simulation=True, + principal='simulated-testprincipal-a', plan_hash=checkpoint['plan']['plan_hash']), 'single attempt and simulated UI audit') + checkpoint.update(postapproval=after, audit=audit); save() + require(result['state'] == 'succeeded' and result['result']['evidence']['postconditions_passed'], 'native job failed; retain outcome, never repeat') + apt = result['result']['evidence']['apt']; manifest = checkpoint['request']['parameters']['apt_manifest'] + package = manifest['resolved_apt_transaction']['packages'][0] + require(apt['packages'] == [dict(name='rmm-mvp-test', architecture='all', version=package['new_version'])] and + apt['fixture_payload_sha256'] == hashlib.sha256((package['new_version']+'\n').encode()).hexdigest(), 'native version/payload proof') + if case in ('apt_install_retry','apt_native_update'): + require(apt['test_acceptance'] is True and apt['noProductionEvidence'] is True and apt['test_result_id'] == manifest['test_result_id'] and + len(apt['native_record_sha256']) == 64, 'real scoped native acceptance required') + else: + require(apt['changed'] is False and apt['already_desired'] is True and apt['transaction_executed'] is False and + apt['postcondition_already_met'] is True, 'separate exact desired-state no-change result required') + checkpoint['verified'] = True; save() + print('PASS '+case+': real cloud VM, actual simulated UI, 0/0/0 -> 1/1/1') + + +def close(): + require(prior.evidence('gate')['unresolved'] == 0, 'unresolved central job') + previous = cloud.contract.protected_json(STATE/'policy.json') + result = cloud.bootstrap.rpc('server.conf', cloud_root_policy.close_policy, dict(expected_sha256=previous['policy_sha256'], backup=previous['backup'])) + cloud.contract.write_private(STATE/'policy-closed.json', result) + endpoint = cloud.bootstrap.rpc('linux-vm.conf', switch_endpoint, dict(id=str(uuid.uuid4()), enabled=False, + agent_id=prior.identity()['agent_id'], binary_sha256=hashlib.sha256((prior.STATE/'helper-native-retry').read_bytes()).hexdigest())) + cloud.contract.write_private(STATE/'endpoint-disabled.json', endpoint) + print('APT scope closed and endpoint stopped; all attempt history preserved') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('action', choices=('enable','accept','finish','close')); parser.add_argument('--case', choices=CASES) + args = parser.parse_args(); os.umask(0o077); STATE.mkdir(mode=0o700, exist_ok=True) + if args.action in ('accept','finish'): + require(args.case is not None, 'fixed case required') + (accept if args.action == 'accept' else finish)(args.case) + else: (enable if args.action == 'enable' else close)() + + +if __name__ == '__main__': + try: main() + except Exception as error: + print('CLOUD_APT_RETRY_INCOMPLETE:'+type(error).__name__) + raise SystemExit(2) from None diff --git a/deploy/cloud_apt_sandbox_upgrade.py b/deploy/cloud_apt_sandbox_upgrade.py index 869c2c5..0b650a0 100644 --- a/deploy/cloud_apt_sandbox_upgrade.py +++ b/deploy/cloud_apt_sandbox_upgrade.py @@ -98,7 +98,8 @@ def upgrade(payload): require(general==old_general,'general policy changed') require(all(manifest(Path(p))==v for p,v in preserved.items()) and markers[0].read_bytes()==marker and (ledger.stat().st_dev,ledger.stat().st_ino)==inode,'preserved identity/attempt/ledger changed') - report.update(stage='installed_preserved',preserved_state=True,runtime_sha256=runtime_sha,apt_config_sha256=config_sha,apt_pin=pin,general_pin=general);save() + report.update(stage='installed_preserved',preserved_state=True,runtime_sha256=runtime_sha,apt_config_sha256=config_sha,apt_pin=pin,general_pin=general, + provider_sha256=sha(code),helper_sha256=sha(raw));save() fcntl.flock(lockfd,fcntl.LOCK_UN);os.close(lockfd);lockfd=None run(['systemd-analyze','verify','/etc/systemd/system/rmm-apt-helper.service']) run(['systemctl','daemon-reload']) @@ -135,17 +136,23 @@ def installed_metadata(payload): def main(): - parser=argparse.ArgumentParser();parser.add_argument('--local-file-fix',action='store_true');args=parser.parse_args() + parser=argparse.ArgumentParser();group=parser.add_mutually_exclusive_group() + group.add_argument('--local-file-fix',action='store_true') + group.add_argument('--native-retry',action='store_true',help='One versioned signed-retry upgrade; never a package retry itself') + args=parser.parse_args() os.umask(0o077);m=cloud.contract require(followup.evidence('gate')['unresolved']==0,'unresolved central job') diagnosis=m.protected_json(followup.STATE/'diagnosis/checkpoint.json');require(diagnosis.get('verified') is True,'diagnostic evidence required') reports=list(followup.STATE.glob('sandbox-*.json')) - if args.local_file_fix: - require(len(reports)==1,'exact prior successful sandbox upgrade required; never repeat migration') - prior=m.protected_json(reports[0]);require(prior['completed'] is True,'previous migration incomplete') - pins=m.protected_json(followup.STATE/'provider-before-local-file-fix.json') + if args.local_file_fix or args.native_retry: + require(len(reports)==(2 if args.native_retry else 1),'exact prior successful sandbox upgrades required; never repeat migration') + previous=[m.protected_json(p) for p in reports] + require(all(p['completed'] is True for p in previous),'previous migration incomplete') + pins=m.protected_json(followup.STATE/('provider-before-native-retry.json' if args.native_retry else 'provider-before-local-file-fix.json')) + candidates=[p for p in previous if p['runtime_sha256']==pins.get('runtime_sha256')] if args.native_retry else previous + require(len(candidates)==1,'reviewed prior runtime required');prior=candidates[0] old=cloud.bootstrap.rpc('linux-vm.conf',installed_metadata,dict(pins,runtime_sha256=prior['runtime_sha256'],apt_pin=prior['apt_pin'])) - artifact=(followup.STATE/'helper-local-file-fixed').read_bytes() + artifact=(followup.STATE/('helper-native-retry' if args.native_retry else 'helper-local-file-fixed')).read_bytes() folders=[cloud.ROOT/'devstate/cloud-root/cases'/name for name in ('service_setup','service_start','service_stop','apt_native_install')] folders += [followup.STATE/name for name in ('diagnosis','diagnosis-acquisition','diagnosis-hook','diagnosis-local-hook')] expected=[str(uuid.UUID(m.protected_json(p/'checkpoint.json')['result']['result']['grant_id'])) for p in folders] diff --git a/deploy/cloud_server_upgrade.py b/deploy/cloud_server_upgrade.py new file mode 100644 index 0000000..d4872d5 --- /dev/null +++ b/deploy/cloud_server_upgrade.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python3 +"""Versioned cloud source/build/deploy, no DB/PKI reset or job approval.""" +import argparse +import base64 +import hashlib +import io +import json +import os +from pathlib import Path +import tarfile +import uuid + +import cloud_mvp_acceptance as cloud + +STATE = cloud.ROOT/'devstate/server-upgrades' +require = cloud.require + + +def stage(payload): + import io + import tarfile + root = Path('/opt/rust-discovery') + backup = root/'devstate'/('source-upgrade-'+payload['id']); backup.mkdir(mode=0o700) + raw = base64.b64decode(payload['tar'], validate=True) + require(hashlib.sha256(raw).hexdigest() == payload['sha256'], 'source archive hash') + def allowed(name): + return name in ('Cargo.toml','Cargo.lock','rust-toolchain.toml','deploy/Dockerfile', + 'deploy/linux/rmm-apt-helper.service','tools/apt-transaction/apt_transaction.py', + 'tools/apt-transaction/hook') or name.startswith(('crates/','migrations/')) + old_image = subprocess.run(['docker','image','inspect','rust-discovery-server:local','--format','{{.Id}}'], capture_output=True, check=True).stdout.decode().strip() + manifest = {}; previous = {} + with tarfile.open(fileobj=io.BytesIO(raw), mode='r:') as archive: + for member in archive.getmembers(): + name = member.name + require(member.isfile() and allowed(name) and not Path(name).is_absolute() and '..' not in Path(name).parts and name not in manifest, 'source path allowlist') + dest = root/name; data = archive.extractfile(member).read() + require(len(data) <= 2*1024*1024 and not dest.is_symlink(), 'source size/path') + for parent in dest.parents: + require(not parent.is_symlink(), 'source parent symlink') + if parent == root: break + if dest.exists(): + old = dest.read_bytes(); previous[name] = hashlib.sha256(old).hexdigest() + retained = backup/'source'/name; retained.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + with retained.open('xb') as f: f.write(old); f.flush(); os.fsync(f.fileno()) + else: previous[name] = None + manifest[name] = hashlib.sha256(data).hexdigest() + dest.parent.mkdir(mode=0o755, parents=True, exist_ok=True) + next_path = dest.with_name(dest.name+'.upgrade-next') + fd = os.open(next_path, os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW, 0o644) + with os.fdopen(fd, 'wb') as f: f.write(data); f.flush(); os.fsync(f.fileno()); os.fchmod(f.fileno(), 0o644) + os.replace(next_path, dest) + # Build asynchronously. Captured compiler output remains root-private. + worker = backup/'build.py' + worker.write_text('''import json,os,subprocess +from pathlib import Path +os.umask(0o077) +root=Path('/opt/rust-discovery'); here=Path(__file__).parent +with (here/'build.log').open('xb') as log: + result=subprocess.run(['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'build','migrate'],stdout=log,stderr=subprocess.STDOUT) +(here/'build-result.json').write_text(json.dumps({'exit_code':result.returncode})) +'''); worker.chmod(0o600) + record = dict(id=payload['id'], source_sha256=payload['sha256'], files=manifest, previous=previous, old_image=old_image, backup=str(backup)) + (backup/'source.json').write_text(json.dumps(record)); (backup/'source.json').chmod(0o600) + unit = 'rmm-source-build-'+payload['id'] + result = subprocess.run(['systemd-run','--unit='+unit,'--property=UMask=0077','--property=WorkingDirectory='+str(root), + '/usr/bin/python3',str(worker)], capture_output=True, timeout=15) + require(result.returncode == 0, 'source build launch incomplete') + return dict(record, unit=unit, launched=True, deployed=False) + + +def status(payload): + root = Path('/opt/rust-discovery'); backup = root/'devstate'/('source-upgrade-'+payload['id']) + record = json.loads((backup/'source.json').read_text()) + result = backup/'build-result.json' + return dict(id=payload['id'], finished=result.exists(), exit_code=json.loads(result.read_text())['exit_code'] if result.exists() else None, + source_unchanged=all(hashlib.sha256((root/name).read_bytes()).hexdigest() == sha for name,sha in record['files'].items())) + + +def deploy(payload): + root = Path('/opt/rust-discovery'); backup = root/'devstate'/('source-upgrade-'+payload['id']) + record = json.loads((backup/'source.json').read_text()) + require(json.loads((backup/'build-result.json').read_text())['exit_code'] == 0, 'build must pass') + require(all(hashlib.sha256((root/name).read_bytes()).hexdigest() == sha for name,sha in record['files'].items()), 'source drift') + require(not (backup/'deploy-result.json').exists(), 'deployment already recorded') + cmd = ['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml')] + # Existing migration image runs without rebuild; preserves PG volume/PKI. + migrate = subprocess.run(cmd+['run','--rm','--no-deps','migrate'], capture_output=True, timeout=25) + require(migrate.returncode == 0, 'migration failed') + result = subprocess.run(cmd+['up','-d','--no-deps','api','gateway'], capture_output=True, timeout=25) + outcome = dict(deployed=result.returncode == 0, existing_state_preserved=True, approvals_created=False) + (backup/'deploy-result.json').write_text(json.dumps(outcome)); (backup/'deploy-result.json').chmod(0o600) + return outcome + + +def main(): + parser = argparse.ArgumentParser(description=__doc__); parser.add_argument('action', choices=('stage','status','deploy')); parser.add_argument('--id') + args = parser.parse_args(); os.umask(0o077); STATE.mkdir(mode=0o700, parents=True, exist_ok=True) + if args.action == 'stage': + names = ['Cargo.toml','Cargo.lock','rust-toolchain.toml','deploy/Dockerfile','deploy/linux/rmm-apt-helper.service', + 'tools/apt-transaction/apt_transaction.py','tools/apt-transaction/hook'] + names += [str(p.relative_to(cloud.ROOT)) for base in ('crates','migrations') for p in (cloud.ROOT/base).rglob('*') + if p.is_file() and not p.is_symlink() and 'target' not in p.parts and '__pycache__' not in p.parts and p.suffix in ('.rs','.toml','.sql')] + stream = io.BytesIO() + with tarfile.open(fileobj=stream, mode='w:') as archive: + for name in sorted(set(names)): + path = cloud.ROOT/name; require(not path.is_symlink(), 'source symlink') + archive.add(path, arcname=name, recursive=False) + data = stream.getvalue(); ident = str(uuid.uuid4()) + result = cloud.bootstrap.rpc('server.conf', stage, dict(id=ident, tar=base64.b64encode(data).decode(), sha256=hashlib.sha256(data).hexdigest())) + cloud.contract.write_private(STATE/(ident+'.json'), result) + print(dict(id=ident, launched=result['launched'], deployed=False)) + else: + ident = str(uuid.UUID(args.id)); cloud.contract.protected_json(STATE/(ident+'.json')) + if args.action == 'deploy': + target = cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json')['identity'] + gate = cloud.bootstrap.rpc('server.conf', cloud.server, dict(kind='gate', tenant_id=cloud.TENANT, agent_id=target['agent_id'])) + require(gate['unresolved'] == 0, 'unresolved cloud job; no server switch') + result = cloud.bootstrap.rpc('server.conf', status if args.action == 'status' else deploy, dict(id=ident)) + print(result) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print('CLOUD_SERVER_UPGRADE_INCOMPLETE:'+type(error).__name__); raise SystemExit(2) from None diff --git a/docs/development.md b/docs/development.md index 645b1df..bfdd1e9 100644 --- a/docs/development.md +++ b/docs/development.md @@ -1,6 +1,6 @@ # Linux-first-Entwicklung und reale VM-Abnahme -Stand: 2026-11-13 · Spezifikation 1.8. **Windows-Override:** Entwicklung und native +Stand: 2026-11-13 · Spezifikation 1.9. **Windows-Override:** Entwicklung und native Tests auf der neuen Windows-11-VM sind ausdrücklich parallel erlaubt, unabhängig von der noch offenen Linux-MVP-Abnahme. Die nachstehenden älteren Wartevorgaben sind hierfür überstimmt; keine Windows-Pflicht zur Linux-Abnahme. diff --git a/docs/handover.md b/docs/handover.md index 3716cfe..3618907 100644 --- a/docs/handover.md +++ b/docs/handover.md @@ -1,5 +1,15 @@ # Übergabe / Entwicklungspause — 2026-11-12 +**Aktueller Arbeitsstand 2026-11-13:** Neuer signierter einmaliger APT- +Wiederaufnahmevertrag implementiert und lokal geprüft; aktualisierter Helper/ +Provider auf Cloud-VM installiert, echter Readonly-Sandboxprobe bestanden. +Originalmarker/Fehlversuch und Sharedledger erhalten, kein Paketjob gestartet. +APT wieder gestoppt/Policy geschlossen; Agent und allgemeiner Helper aktiv. +Serverbuild läuft getrennt; Windows-Client parallel in Arbeit. Neueste Details +stehen oben in `implementation-status.md`. Die folgenden Zwischenstände sind +historische Snapshots. + + **Aktueller Folgestand 2026-11-13:** APT-100-Ursache durch separat WebUI-genehmigte Cloud-Diagnosen belegt: `--no-download` lieferte im echten v3-Hook `**ERROR**` statt Archivpfad. Lokale Dateiacquisition korrigiert; wirkliche IPv4-/IPv6-Sperre diff --git a/docs/implementation-status.md b/docs/implementation-status.md index eea695f..a75d86b 100644 --- a/docs/implementation-status.md +++ b/docs/implementation-status.md @@ -1,5 +1,45 @@ # Tatsächlicher Implementierungsstand +## Signierte APT-Wiederaufnahme: neue Runtime nativ bereit — 2026-11-13 + +Für den explizit beauftragten neuen Abnahmeschritt ist ein eigener sicherer +Wiederaufnahmevertrag implementiert (Spezifikation 1.9). Schema 3 trägt eine +typisierte Referenz auf ursprünglichen Grant/Versuch und unveränderten +Bootstrapmarker. Schema 2 bleibt unverändert serialisierbar. Vor Grantverbrauch +und Staging prüft der Helper im **gleichen** Ledger den eindeutigen alten Fehler, +gespeicherten ursprünglichen Ed25519-Auftrag, Ziel/Generationen, neue feste +Job-/Versuchs-/Genehmigungs-/Planbindung und identische vollständige Transaktion. +Python prüft historische Baseline, Kontext, Launch und APT-100-Ergebnis ohne +Hook nochmals. Alter Marker bleibt erhalten; genau ein eigener Retrymarker +wird exklusiv geschrieben. Unknown, vorhandener Hook/Acceptance, geänderte +Baseline oder Wiederverwendung sperren. Keine Marker-/Ledger-/Identitätslöschung. + +Tatsächlich ausgeführt: **65 Python-APT-Tests**, **12 Domain / 28 Helper-Library / +3 Helper-Binary / 20 Sync-Tests**, Clippy mit `-D warnings`, Formatprüfung und +Releasebuild bestanden. Die Rustprüfung lief in isoliertem Buildcontainer mit +passendem vorherigem Workspace-/Locksnapshot, da Windows-Abhängigkeiten parallel +ergänzt wurden; kein vollständiger Workspace-/Servernachweis dieses Snapshots. + +`python3 deploy/cloud_apt_sandbox_upgrade.py --native-retry` auf der echten +Cloud-Linux-VM erfolgreich: versionierte Backups, unveränderte beide +Agentidentitäten/Journale, Sharedledger-Inode/Bytes, Archive/Baselines/History, +gesamter alter `active/` und originaler Marker nachgewiesen. Echter neuer +Readonly-Sandboxprobe unter unverändertem APT-Profil bestanden; zehn stabile +Daemonprüfungen. **Keine Pakettransaktion und keine neue native Acceptance.** + +- Helper: `91ffd13a4a0a6713bf6d1aa07ccc72e1bea737ce5533fbec87b40a9f274cf35c`. +- Provider: `465ac5ed61185dd72f4600c49e0da5b9da16f75aa3686f7c278f8667a6564945`. +- Runtime: `711d81290ab3262cab409eb00e2d35ac0ef430de45bbbbe8b988973e47a632f6`. +- Stack: `a89219bb230dff1412da78174cddc493972ae5972143f371d08191a49331110d`. + +APT nach Probe wieder gestoppt, allgemeiner Helper und Cloud-Agent gestartet. +Acht bekannte abgeschlossene Rootversuche; kein neuer Job. Rootpolicy weiter +geschlossen. Signierte neue Fixtureinstallation, Upgrade und normale +Ensure-/Update-Noops **geplant, noch nicht ausgeführt**. Serverbuild mit neuen +APT-/Windows-Verträgen separat gestartet, noch kein bestätigtes Deployment. +Windows-Client wird parallel implementiert; native Clientabnahme noch offen. +Geschützt: `devstate/cloud-apt-followup/sandbox-*.json`, Build-/VM-Backups. + ## Providerkorrektur und erneute native Readiness bestanden — 2026-11-13 Korrektur in `tools/apt-transaction/apt_transaction.py`: feste lokale diff --git a/docs/project-decisions.md b/docs/project-decisions.md index 7800c5e..2a788d4 100644 --- a/docs/project-decisions.md +++ b/docs/project-decisions.md @@ -1,6 +1,17 @@ # Verbindliche Projektentscheidungen -Stand: **2026-11-13**, Spezifikation **1.8** (`Rust_RMM_Implementierungsspezifikation.md`). Die ausdrücklichen normativen Entscheidungen in Kapitel 0 haben Vorrang vor früheren Architekturannahmen. Version 1.6 ersetzt die lokale Servertopologie durch den dedizierten Internetserver; Version 1.7 präzisiert das Notebook als reinen Entwicklungsrechner; Version 1.8 erlaubt vorgezogene parallele Windows-Entwicklung. +Stand: **2026-11-13**, Spezifikation **1.9** (`Rust_RMM_Implementierungsspezifikation.md`). Die ausdrücklichen normativen Entscheidungen in Kapitel 0 haben Vorrang vor früheren Architekturannahmen. Version 1.6 ersetzt die lokale Servertopologie durch den dedizierten Internetserver; Version 1.7 präzisiert das Notebook als reinen Entwicklungsrechner; Version 1.8 erlaubt vorgezogene parallele Windows-Entwicklung. + +## Einzelner signierter APT-Wiederaufnahmeversuch (2026-11-13) + +Die ausdrücklich beauftragte Fortsetzung des bekannten Pre-Hook-Fehlers erhält +keinen Markerreset. Ein neuer UI-genehmigter Plan bindet im Manifestschema 3 +ursprünglichen Grant/Versuch und Markerhash. Helper prüft die ursprüngliche +Signatur und eindeutigen Fehlschlag im gleichen Ledger, unveränderte Transaktion +und Baseline. Genau ein eigener dauerhafter Retrymarker; bei Unknown, Hook, +geändertem Zustand oder bestehendem Retry keine Wirkung. Schema 2 bleibt +bytekompatibel. Vertrag und reale Nachweise separat in Spezifikation 0.1 und +`implementation-status.md`; keine implizite Produktions-/MVP-Freigabe. ## Windows parallel auf ausdrücklichen Userwunsch (2026-11-13) diff --git a/docs/server-deployment.md b/docs/server-deployment.md index 452d5ab..27a0488 100644 --- a/docs/server-deployment.md +++ b/docs/server-deployment.md @@ -1,6 +1,6 @@ # Dedizierter RMM-Server -Stand: 2026-11-13 · Spezifikation 1.8 · Linux-MVP weiterhin nicht abgenommen. +Stand: 2026-11-13 · Spezifikation 1.9 · Linux-MVP weiterhin nicht abgenommen. ## Neuester APT-/Bootstrapstand diff --git a/tests/apt_transaction/test_native_retry.py b/tests/apt_transaction/test_native_retry.py new file mode 100644 index 0000000..9635ee4 --- /dev/null +++ b/tests/apt_transaction/test_native_retry.py @@ -0,0 +1,123 @@ +"""Signed retry checks on temporary files; no native package execution.""" +import copy +from dataclasses import replace +from pathlib import Path +import tempfile +import uuid +from unittest import TestCase +from unittest.mock import patch + +from test_guard import apt +from test_native_acceptance import context, fixture_transaction, action_manifest, signed_job + + +class NativeRetryTests(TestCase): + def setup_history(self, base): + active = base / 'active' + active.mkdir() + old = replace(context(), grant_id=str(uuid.uuid4()), attempt_id=str(uuid.uuid4())) + previous = base / 'history' / old.grant_id + previous.mkdir(parents=True) + obj = fixture_transaction() + obj['baseline_sha256'] = apt.digest(apt.canonical({})) + tx = apt.ResolvedAptTransaction.parse(obj) + txbytes = apt.canonical(obj) + old = replace(old, transaction_sha256=apt.digest(txbytes)) + old_action = action_manifest(old, obj, {}) + old_job = apt.canonical(signed_job(old, old_action)) + old = replace(old, job_payload_sha256=apt.digest(old_job)) + files = {'context.json': apt.canonical(old.__dict__), 'signed-action.json': old_job, + 'expectation.json': txbytes, 'baseline.json': apt.canonical({}), + 'launch.json': apt.canonical({'transaction_sha256': old.transaction_sha256, + 'effect_armed_ledger_id': old.effect_armed_ledger_id}), + 'sandbox-result.json': apt.canonical({'transaction_sha256': old.transaction_sha256, + 'apt_exit_code': 100, 'reboot_required': False})} + for name, raw in files.items(): + (previous / name).write_bytes(raw) + original = base / ('bootstrap-attempt-' + tx.recipe_id + '.json') + original.write_bytes(apt.canonical(apt.attempt_binding(old))) + new = replace(context(), grant_id=str(uuid.uuid4()), attempt_id=str(uuid.uuid4()), + job_id='new-job', authorization_id='new-approval', plan_sha256='d' * 64, + transaction_sha256=old.transaction_sha256) + action = action_manifest(new, obj, {}) + action.update(schema_version=3, native_retry={ + 'schema_version': 1, 'previous_grant_id': old.grant_id, + 'previous_attempt_id': old.attempt_id, + 'bootstrap_marker_sha256': apt.digest(original.read_bytes())}) + job = apt.canonical(signed_job(new, action)) + new = replace(new, job_payload_sha256=apt.digest(job)) + (active / 'baseline.json').write_bytes(apt.canonical({})) + (active / 'signed-action.json').write_bytes(job) + (active / 'expectation.json').write_bytes(txbytes) + return tx, new, action, old, previous, original + + def scenario(self, variation=None): + with tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp: + base = Path(temp) + with patch.object(apt, 'BASE', base), patch.object(apt, 'ACTIVE', base / 'active'), \ + patch.object(apt, 'secure_path', side_effect=lambda path, **kwargs: path), \ + patch.object(apt, 'sealed_read', side_effect=lambda path, *args: + b'code' if path == apt.CODE else apt.hook_script() + if path == Path(apt.HOOK) else path.read_bytes()): + tx, new, action, old, previous, original = self.setup_history(base) + original_bytes = original.read_bytes() + if variation == 'armed': + (previous / 'hook-armed.json').write_bytes(b'{}') + elif variation == 'dangling-hook': + (previous / 'hook-armed.json').symlink_to(previous / 'missing') + elif variation == 'marker-hash': + action['native_retry']['bootstrap_marker_sha256'] = '0' * 64 + elif variation == 'same-attempt': + new = replace(new, attempt_id=old.attempt_id) + elif variation == 'same-approval': + new = replace(new, authorization_id=old.authorization_id) + elif variation == 'baseline': + (apt.ACTIVE / 'baseline.json').write_bytes(b'{"changed":true}') + elif variation == 'partial-result': + (previous / 'sandbox-result.json').write_bytes(b'{') + elif variation == 'other-exit': + (previous / 'sandbox-result.json').write_bytes(apt.canonical({ + 'transaction_sha256': old.transaction_sha256, + 'apt_exit_code': 0, 'reboot_required': False})) + elif variation == 'old-payload': + (previous / 'signed-action.json').write_bytes(b'{}') + elif variation == 'accepted': + apt.record_path(tx).write_bytes(b'{}') + if variation is not None: + with self.assertRaises((apt.Veto, OSError, ValueError)): + apt.bootstrap_binding(tx, new, action) + self.assertEqual(original.read_bytes(), original_bytes) + return + # Full signed-loader path accepts schema3 only with bound data. + self.assertEqual(apt.load_signed_action(tx, new), action) + path, binding = apt.bootstrap_binding(tx, new, action) + self.assertEqual(path.name, 'bootstrap-retry-' + tx.recipe_id + '.json') + self.assertEqual(binding['native_retry'], action['native_retry']) + apt.durable_once(path, binding) + apt.check_bootstrap_attempt(tx, new, action) + with self.assertRaisesRegex(apt.Veto, 'ALREADY_HANDED_OFF'): + apt.durable_once(path, binding) + with self.assertRaisesRegex(apt.Veto, 'BOOTSTRAP_ATTEMPT_BINDING'): + apt.check_bootstrap_attempt(tx, replace(new, grant_id=str(uuid.uuid4())), action) + self.assertEqual(original.read_bytes(), original_bytes) + + def test_single_new_approved_attempt_preserves_original_tombstone(self): + self.scenario() + + def test_missing_or_changed_known_no_effect_evidence_vetoes_retry(self): + for variation in ('armed', 'dangling-hook', 'marker-hash', 'same-attempt', 'same-approval', + 'baseline', 'partial-result', 'other-exit', 'old-payload', 'accepted'): + with self.subTest(variation=variation): + self.scenario(variation) + + def test_retry_reference_has_strict_schema_and_canonical_uuid_paths(self): + valid = {'schema_version': 1, 'previous_grant_id': str(uuid.uuid4()), + 'previous_attempt_id': str(uuid.uuid4()), 'bootstrap_marker_sha256': 'a' * 64} + apt.validate_retry_reference(valid) + for key, value in (('schema_version', True), ('previous_grant_id', '../active'), + ('previous_attempt_id', 'not-a-uuid'), ('bootstrap_marker_sha256', 'x'), + ('unknown', 'field')): + altered = copy.deepcopy(valid) + altered[key] = value + with self.subTest(key=key), self.assertRaises((apt.Veto, ValueError)): + apt.validate_retry_reference(altered) diff --git a/tools/apt-transaction/HELPER-CONTRACT.md b/tools/apt-transaction/HELPER-CONTRACT.md index 351850f..5ab7a34 100644 --- a/tools/apt-transaction/HELPER-CONTRACT.md +++ b/tools/apt-transaction/HELPER-CONTRACT.md @@ -1,5 +1,37 @@ # Typed RootHelper bridge contract (implementation, not native acceptance) +## Explicit one-attempt recovery (schema 3, 2026-11-13) + +Schema 2 keeps its exact old encoding (the optional field is omitted). Schema 3 +requires `native_retry` and is valid only for dev-test `apt-native-acceptance`: + +``` +native_retry: { + schema_version: 1, + previous_grant_id: UUID, + previous_attempt_id: UUID, + bootstrap_marker_sha256: SHA256(original exact marker bytes) +} +``` + +This reference belongs to a **new separately UI-approved signed job**, never +an unsigned IPC override. Under the existing device lock, before consuming the +new grant and again before staging, the helper verifies the original saved +Ed25519 request historically, identical target/generation/assignment/transaction, +distinct job/attempt/authorization/plan, same-ledger terminal `failed` result +`HOOK_NOT_HANDED_OFF`, matching recorded outcome, original context/launch/ +sandbox-100 evidence and absent hook/acceptance/retry markers. Historical +signature verification never grants a current start. Unknown or partial history +fails closed. Original active state may only move through the existing terminal +history rule; original tombstone never changes. + +Provider independently checks original marker hash and immutable history, +identical full baseline and current normal baseline gates. It exclusively writes +`bootstrap-retry-.json` binding the new attempt, signed reference and +hashes of historical evidence. Worker, hook and completion recheck it. This is +one recovery attempt per fixed fixture recipe, not general automatic retries. +Test/native outcomes are recorded in `docs/implementation-status.md`. + **Dedicated-runner update:** the current deployment and readiness protocol is defined in [`../../deploy/linux/apt-helper-README.md`](../../deploy/linux/apt-helper-README.md). The actual additive domain schema-2 types and AptNativeAcceptance variant have diff --git a/tools/apt-transaction/apt_transaction.py b/tools/apt-transaction/apt_transaction.py index 69fc51c..e03079d 100644 --- a/tools/apt-transaction/apt_transaction.py +++ b/tools/apt-transaction/apt_transaction.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 """Root-helper-only, fail-closed local-archive APT transaction provider. -No network API, resolver approval, grant verifier, or retry mechanism lives here. +No network API, resolver approval, grant verifier, or automatic retry lives here. See README.md before integrating. Python 3.9+, Linux amd64 only. """ import ctypes @@ -366,12 +366,18 @@ def load_signed_action(transaction, context): context.action == ('apt-native-acceptance' if outer_action == 'apt_native_acceptance' else 'apt-execute'), 'DEDICATED_HELPER_ACTION_REQUIRED') action = job['action'].get('apt_manifest') - exact(action, 'type schema_version action execution_profile transaction_sha256 ' - 'resolved_apt_transaction provider_sha256 hook_sha256 apt_config_sha256 ' - 'runtime_sha256 test_result_id transaction_bytes') + manifest_keys = 'type schema_version action execution_profile transaction_sha256 ' + manifest_keys += ('resolved_apt_transaction provider_sha256 hook_sha256 apt_config_sha256 ' + 'runtime_sha256 test_result_id transaction_bytes') + version = action.get('schema_version') if type(action) is dict else None + exact(action, manifest_keys + (' native_retry' if version == 3 else '')) require(action['type'] == 'rmm.apt.helper-action' and - type(action['schema_version']) is int and action['schema_version'] == 2, + type(version) is int and version in (2, 3), 'SIGNED_ACTION_SCHEMA') + if version == 3: + validate_retry_reference(action['native_retry']) + require(context.action == 'apt-native-acceptance' and context.execution_profile == 'dev-test', + 'RETRY_NATIVE_TEST_ONLY') expected_bytes = sealed_read(ACTIVE / 'expectation.json') require(type(action['transaction_bytes']) is list and len(action['transaction_bytes']) <= 65536 and all(type(value) is int and 0 <= value <= 255 for value in action['transaction_bytes']) and @@ -465,12 +471,95 @@ def runtime_identity(): 'unit_profile_sha256': digest(sealed_read(APT_UNIT))} -def check_bootstrap_attempt(transaction, context): - attempt = json_bytes(sealed_read(BASE / ('bootstrap-attempt-' + transaction.recipe_id + '.json'))) - require(attempt == {'transaction_sha256': context.transaction_sha256, - 'job_payload_sha256': context.job_payload_sha256, - 'grant_id': context.grant_id, 'attempt_id': context.attempt_id}, - 'BOOTSTRAP_ATTEMPT_BINDING') +def attempt_binding(context): + return {'transaction_sha256': context.transaction_sha256, + 'job_payload_sha256': context.job_payload_sha256, + 'grant_id': context.grant_id, 'attempt_id': context.attempt_id} + + +def validate_retry_reference(ref): + exact(ref, 'schema_version previous_grant_id previous_attempt_id bootstrap_marker_sha256') + require(type(ref['schema_version']) is int and ref['schema_version'] == 1, 'RETRY_SCHEMA') + for key in ('previous_grant_id', 'previous_attempt_id'): + value = ref[key] + require(type(value) is str and str(uuid.UUID(value)) == value, 'RETRY_ID') + token(ref['bootstrap_marker_sha256'], HASH, 'RETRY_MARKER_HASH') + + +def historical_retry_evidence(transaction, context, action): + """Recheck immutable no-handoff evidence, never turn unknown into failed. + + The parent separately verifies its signed saved request and definitive + HOOK_NOT_HANDED_OFF result in the SAME consumed ledger before this call. + This evidence check cannot itself authorize a retry: the reference must be + inside the NEW exact signed/UI-approved job payload and consumed attempt. + """ + ref = action['native_retry'] + validate_retry_reference(ref) + require(context.action == 'apt-native-acceptance' and context.execution_profile == 'dev-test' and + context.grant_id != ref['previous_grant_id'] and + context.attempt_id != ref['previous_attempt_id'], 'RETRY_NEW_APPROVED_ATTEMPT_REQUIRED') + original_path = BASE / ('bootstrap-attempt-' + transaction.recipe_id + '.json') + original_bytes = sealed_read(original_path) + require(digest(original_bytes) == ref['bootstrap_marker_sha256'], 'RETRY_ORIGINAL_MARKER_HASH') + previous = BASE / 'history' / ref['previous_grant_id'] + secure_path(previous, directory=True, private=True) + require(not os.path.lexists(previous / 'hook-armed.json') and + not os.path.lexists(record_path(transaction)) and + not os.path.lexists(BASE / ('pending-acceptance-' + transaction.recipe_id + '.json')), + 'RETRY_PRIOR_EFFECT_OR_ACCEPTANCE') + names = ('context.json', 'signed-action.json', 'expectation.json', 'baseline.json', + 'launch.json', 'sandbox-result.json') + raw = {name: sealed_read(previous / name) for name in names} + old = VerifiedHelperContext.parse(json_bytes(raw['context.json'])) + require(old.action == 'apt-native-acceptance' and old.execution_profile == 'dev-test' and + old.grant_id == ref['previous_grant_id'] and old.attempt_id == ref['previous_attempt_id'] and + json_bytes(original_bytes) == attempt_binding(old), 'RETRY_ORIGINAL_ATTEMPT_BINDING') + require(digest(raw['signed-action.json']) == old.job_payload_sha256 and + digest(raw['expectation.json']) == old.transaction_sha256, 'RETRY_PRIOR_PAYLOAD_HASH') + old_tx = ResolvedAptTransaction.parse(json_bytes(raw['expectation.json'])) + require(fixture_scope(old_tx) == fixture_scope(transaction) and + old_tx.status_fingerprint == transaction.status_fingerprint and + old_tx.baseline_sha256 == transaction.baseline_sha256, 'RETRY_BASELINE_OR_RECIPE_CHANGED') + require(digest(canonical(json_bytes(raw['baseline.json']))) == old_tx.baseline_sha256 and + raw['baseline.json'] == sealed_read(ACTIVE / 'baseline.json'), 'RETRY_COMPLETE_BASELINE') + previous_job = json_bytes(raw['signed-action.json']) + current_job = json_bytes(sealed_read(ACTIVE / 'signed-action.json')) + require(all(previous_job[key] == current_job[key] for key in + ('tenant_id', 'asset_id', 'agent_id', 'enrollment_generation', 'assignment_version')) and + previous_job['job_id'] == old.job_id and previous_job['attempt_id'] == old.attempt_id and + previous_job['execution_identity'] == 'root' and + previous_job['action']['action'] == 'apt_native_acceptance', 'RETRY_DEVICE_OR_JOB_BINDING') + old_manifest = previous_job['action']['apt_manifest'] + require(old_manifest['schema_version'] == 2 and 'native_retry' not in old_manifest and + old_manifest['transaction_sha256'] == old.transaction_sha256 and + bytes(old_manifest['transaction_bytes']) == raw['expectation.json'] and + old_manifest['resolved_apt_transaction'] == json_bytes(raw['expectation.json']), + 'RETRY_ORIGINAL_SIGNED_TRANSACTION') + require(json_bytes(raw['launch.json']) == { + 'transaction_sha256': old.transaction_sha256, + 'effect_armed_ledger_id': old.effect_armed_ledger_id} and + json_bytes(raw['sandbox-result.json']) == { + 'transaction_sha256': old.transaction_sha256, + 'apt_exit_code': 100, 'reboot_required': False}, 'RETRY_KNOWN_PRE_HOOK_FAILURE_REQUIRED') + require(context.job_id != old.job_id and context.authorization_id != old.authorization_id and + context.plan_sha256 != old.plan_sha256, 'RETRY_NEW_APPROVAL_REQUIRED') + return digest(canonical({name: digest(data) for name, data in raw.items()})) + + +def bootstrap_binding(transaction, context, action): + binding = attempt_binding(context) + if action.get('native_retry') is not None: + binding.update(native_retry=action['native_retry'], previous_evidence_sha256= + historical_retry_evidence(transaction, context, action)) + return BASE / ('bootstrap-retry-' + transaction.recipe_id + '.json'), binding + return BASE / ('bootstrap-attempt-' + transaction.recipe_id + '.json'), binding + + +def check_bootstrap_attempt(transaction, context, action=None): + action = load_signed_action(transaction, context) if action is None else action + path, binding = bootstrap_binding(transaction, context, action) + require(json_bytes(sealed_read(path)) == binding, 'BOOTSTRAP_ATTEMPT_BINDING') def invalid_control_syscall_probes(): @@ -549,7 +638,7 @@ def complete_acceptance(transaction, context, action, runtime, evidence): """Called only after real child success, armed hook AND independent verify.""" require(context.action == 'apt-native-acceptance' and context.execution_profile == 'dev-test', 'DEDICATED_HELPER_ACTION_REQUIRED') - check_bootstrap_attempt(transaction, context) + check_bootstrap_attempt(transaction, context, action) prechecks = json_bytes(sealed_read(ACTIVE / 'native-prechecks.json')) expected_checks = {'namespaces_caps_nnp_seccomp': True, 'readonly_controlpaths': True, 'private_tmpfs_sockets': True, @@ -881,7 +970,7 @@ def hook(): native_acceptance = context.action == 'apt-native-acceptance' check_native_gate(transaction, context, action, runtime_identity(), native_acceptance) if native_acceptance: - check_bootstrap_attempt(transaction, context) + check_bootstrap_attempt(transaction, context, action) ready = json_bytes(sealed_read(ACTIVE / 'sandbox-ready.json')) require(ready == {'transaction_sha256': context.transaction_sha256, 'sandbox_version': 1}, 'SANDBOX_HANDSHAKE') @@ -1087,7 +1176,7 @@ def sandbox_worker(): check_native_gate(transaction, context, action, runtime_identity(), context.action == 'apt-native-acceptance') if context.action == 'apt-native-acceptance': - check_bootstrap_attempt(transaction, context) + check_bootstrap_attempt(transaction, context, action) setup_sandbox() # Executed natively in the actual mutation sandbox, never synthesized by # the parent or by a bootstrap flag. Also required for record-backed runs. @@ -1206,12 +1295,11 @@ def execute(request, native_acceptance=False): # No reinstall/downgrade/remove is introduced by the no-change path. check_preconditions(transaction, baseline) if native_acceptance: - # Persist once per code-defined recipe, independently of active/ - # and of grant IDs. Failures are not silently retryable. - durable_once(BASE / ('bootstrap-attempt-' + transaction.recipe_id + '.json'), { - 'transaction_sha256': context.transaction_sha256, - 'job_payload_sha256': context.job_payload_sha256, - 'grant_id': context.grant_id, 'attempt_id': context.attempt_id}) + # Original marker never changes. One separately signed retry + # may reference proven no-handoff history; it has its OWN + # create-once tombstone, never a reset of the original attempt. + path, binding = bootstrap_binding(transaction, context, action) + durable_once(path, binding) durable_once(ACTIVE / 'host-namespaces.json', { name: os.readlink('/proc/self/ns/' + name) for name in ('pid', 'mnt', 'net')}) durable_once(ACTIVE / 'launch.json', {'transaction_sha256': context.transaction_sha256,