feat: snapshot Linux-first RMM MVP and pause handover
Rust development slice / linux (push) Failing after 1m10s
Rust development slice / linux (push) Failing after 1m10s
This commit is contained in:
@@ -0,0 +1,317 @@
|
||||
//! Dedicated APT route: no fallback to generic helper, shell or SSH. Readiness
|
||||
//! is nonce/root-peer/identity/profile/stack pinned before advertising or use.
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use rmm_domain::jobs::JobPayload;
|
||||
use rmm_privileged_helper::{Binding, Outcome, Request, apt as provider, readiness};
|
||||
use std::{
|
||||
ffi::OsString,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
pub fn socket_for(
|
||||
job: &JobPayload,
|
||||
general: Option<OsString>,
|
||||
apt: Option<OsString>,
|
||||
) -> Result<PathBuf> {
|
||||
select_socket(provider::is_apt_action(job), general, apt)
|
||||
}
|
||||
fn select_socket(
|
||||
is_apt: bool,
|
||||
general: Option<OsString>,
|
||||
apt: Option<OsString>,
|
||||
) -> Result<PathBuf> {
|
||||
if is_apt {
|
||||
let socket = PathBuf::from(apt.context("APT_HELPER_SOCKET_NOT_CONFIGURED")?);
|
||||
ensure!(
|
||||
socket == Path::new(readiness::SOCKET),
|
||||
"APT_HELPER_SOCKET_PROFILE"
|
||||
);
|
||||
Ok(socket)
|
||||
} else {
|
||||
Ok(PathBuf::from(
|
||||
general.context("GENERAL_HELPER_SOCKET_NOT_CONFIGURED")?,
|
||||
))
|
||||
}
|
||||
}
|
||||
fn status(binding: &Binding) -> Result<readiness::Response> {
|
||||
let path =
|
||||
std::env::var_os("RMM_APT_HELPER_PROOF_FILE").context("APT_HELPER_PIN_NOT_CONFIGURED")?;
|
||||
let pin = readiness::Pin::read(Path::new(&path), binding)?;
|
||||
let socket =
|
||||
std::env::var_os("RMM_APT_HELPER_SOCKET").context("APT_HELPER_SOCKET_NOT_CONFIGURED")?;
|
||||
readiness::probe(Path::new(&socket), binding, &pin)
|
||||
}
|
||||
pub fn capabilities_from(response: &readiness::Response) -> Vec<String> {
|
||||
if response.profile != "apt_only" {
|
||||
return vec![];
|
||||
}
|
||||
let mut result = vec![];
|
||||
if response.fixture_acceptance_ready && !response.bootstrap_recipes.is_empty() {
|
||||
result.push("apt_native_acceptance".into());
|
||||
}
|
||||
if !response.accepted_recipes.is_empty() {
|
||||
result.push("ensure_package".into());
|
||||
}
|
||||
if response
|
||||
.accepted_recipes
|
||||
.iter()
|
||||
.any(|r| r == provider::UPDATE_RECIPE)
|
||||
{
|
||||
result.push("apply_updates".into());
|
||||
}
|
||||
result
|
||||
}
|
||||
pub fn capabilities(binding: &Binding) -> Vec<String> {
|
||||
status(binding)
|
||||
.map(|s| {
|
||||
let mut caps = capabilities_from(&s);
|
||||
if !caps.is_empty() {
|
||||
let roots: Vec<String> = caps.iter().map(|c| format!("root:{c}")).collect();
|
||||
caps.extend(roots);
|
||||
caps.extend([
|
||||
"helper.root.v1".into(),
|
||||
format!("helper.policy.sha256:{}", s.provider_stack_sha256),
|
||||
"apt.manifest.v2".into(),
|
||||
]);
|
||||
if s.fixture_acceptance_ready && !s.bootstrap_recipes.is_empty() {
|
||||
caps.push("apt.native-acceptance.v1".into());
|
||||
}
|
||||
if !s.accepted_recipes.is_empty() {
|
||||
caps.push("apt.native-accepted.v1".into());
|
||||
}
|
||||
}
|
||||
caps
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Called only by the periodic blocking inventory worker, never heartbeat or a
|
||||
/// caller-selected live query. Failure retains honest unknown/not_configured.
|
||||
pub fn planning_observation(binding: Option<&Binding>) -> serde_json::Value {
|
||||
let result = (|| -> Result<readiness::PlanningResult> {
|
||||
let binding = binding.context("APT_PLANNING_NOT_CONFIGURED")?;
|
||||
let path =
|
||||
std::env::var_os("RMM_APT_HELPER_PROOF_FILE").context("APT_PLANNING_NOT_CONFIGURED")?;
|
||||
let pin = readiness::Pin::read(Path::new(&path), binding)?;
|
||||
let socket =
|
||||
std::env::var_os("RMM_APT_HELPER_SOCKET").context("APT_PLANNING_NOT_CONFIGURED")?;
|
||||
readiness::planning(Path::new(&socket), binding, &pin)?
|
||||
.planning
|
||||
.context("APT_PLANNING_UNAVAILABLE")
|
||||
})();
|
||||
match result {
|
||||
Ok(planning) => {
|
||||
serde_json::json!({"scope":"linux.apt.planning", "source_id":"root-helper:apt_planning_v1",
|
||||
"observed_at":planning.observed_at, "quality":"valid", "completeness":"complete", "truncated":false, "data":planning})
|
||||
}
|
||||
Err(_) => {
|
||||
serde_json::json!({"scope":"linux.apt.planning", "source_id":"root-helper:apt_planning_v1",
|
||||
"observed_at":chrono::Utc::now(), "quality":"unknown", "completeness":"unknown", "truncated":false,
|
||||
"error_code":if binding.is_none() || std::env::var_os("RMM_APT_HELPER_SOCKET").is_none(){"APT_PLANNING_NOT_CONFIGURED"}else{"APT_PLANNING_UNAVAILABLE"}, "data":null})
|
||||
}
|
||||
}
|
||||
}
|
||||
fn recipe_ready(job: &JobPayload, response: &readiness::Response) -> Result<()> {
|
||||
ensure!(
|
||||
job.execution_identity == "root" && provider::is_apt_action(job),
|
||||
"APT_TYPED_ROOT_ACTION_REQUIRED"
|
||||
);
|
||||
let action = serde_json::to_value(&job.action)?;
|
||||
let manifest: provider::AptExecutionManifest = serde_json::from_value(
|
||||
action
|
||||
.get("apt_manifest")
|
||||
.context("APT_MANIFEST_NOT_CONFIGURED")?
|
||||
.clone(),
|
||||
)?;
|
||||
manifest.resolved_apt_transaction.validate_fixture()?;
|
||||
let recipe = &manifest.resolved_apt_transaction.recipe_id;
|
||||
if job.action.name() == "apt_native_acceptance" {
|
||||
ensure!(
|
||||
manifest.action == "apt-native-acceptance"
|
||||
&& response.fixture_acceptance_ready
|
||||
&& response.bootstrap_recipes.contains(recipe),
|
||||
"APT_FIXTURE_READINESS_REQUIRED"
|
||||
);
|
||||
} else {
|
||||
ensure!(
|
||||
manifest.action == "apt-execute" && response.accepted_recipes.contains(recipe),
|
||||
"APT_NATIVE_ACCEPTANCE_REQUIRED"
|
||||
);
|
||||
ensure!(
|
||||
job.action.name() != "apply_updates" || recipe == provider::UPDATE_RECIPE,
|
||||
"APT_UPDATE_RECIPE_SCOPE"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn supported(binding: &Binding, job: &JobPayload) -> bool {
|
||||
status(binding).and_then(|s| recipe_ready(job, &s)).is_ok()
|
||||
}
|
||||
pub fn invoke(binding: &Binding, job: &JobPayload, request: &Request) -> Result<Outcome> {
|
||||
let socket = socket_for(
|
||||
job,
|
||||
std::env::var_os("RMM_HELPER_SOCKET"),
|
||||
std::env::var_os("RMM_APT_HELPER_SOCKET"),
|
||||
)?;
|
||||
if provider::is_apt_action(job) {
|
||||
recipe_ready(job, &status(binding)?)?;
|
||||
}
|
||||
rmm_privileged_helper::invoke(&socket, request, job.max_runtime_seconds)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn passive_planning_without_binding_is_honest_unknown_without_data() {
|
||||
let observation = planning_observation(None);
|
||||
assert_eq!(observation["scope"], "linux.apt.planning");
|
||||
assert_eq!(observation["quality"], "unknown");
|
||||
assert_eq!(observation["error_code"], "APT_PLANNING_NOT_CONFIGURED");
|
||||
assert!(observation["data"].is_null());
|
||||
assert!(observation.get("received_at").is_none());
|
||||
}
|
||||
#[test]
|
||||
fn typed_actions_require_their_own_fixture_readiness_or_accepted_recipe() {
|
||||
let id = uuid::Uuid::new_v4();
|
||||
let now = chrono::Utc::now();
|
||||
let tx = serde_json::json!({"schema_version":1,"recipe_id":provider::INSTALL_RECIPE,"recipe_version":"1","platform":"ubuntu-24.04-amd64",
|
||||
"resolver_config_version":"rmm-apt-local-v1","no_reboot":true,"status_fingerprint":"a".repeat(64),"baseline_sha256":"b".repeat(64),
|
||||
"packages":[{"name":"rmm-mvp-test","architecture":"all","old_version":null,"new_version":"1.0","origin":"rmm-test",
|
||||
"sha256":rmm_domain::apt_transaction::INSTALL_SHA,"size":20672,
|
||||
"archive_path":format!("/var/lib/rmm-apt-transaction/archives/{}.deb",rmm_domain::apt_transaction::INSTALL_SHA)}]});
|
||||
let bytes = serde_json::to_vec(&tx).unwrap();
|
||||
let manifest = serde_json::json!({"type":"rmm.apt.helper-action","schema_version":2,"action":"apt-native-acceptance","execution_profile":"dev-test",
|
||||
"transaction_sha256":rmm_domain::jobs::hash(&bytes),"resolved_apt_transaction":tx,"transaction_bytes":bytes,
|
||||
"provider_sha256":"a".repeat(64),"hook_sha256":"a".repeat(64),"apt_config_sha256":"a".repeat(64),"runtime_sha256":"a".repeat(64),"test_result_id":"fixture-test"});
|
||||
let mut job:JobPayload=serde_json::from_value(serde_json::json!({"schema_version":1,"tenant_id":id,"asset_id":id,"agent_id":id,
|
||||
"enrollment_generation":1,"assignment_version":1,"job_id":id,"attempt_id":id,"execution_identity":"root",
|
||||
"not_before":now,"start_before":now+chrono::Duration::minutes(1),"max_runtime_seconds":300,"max_output_bytes":4096,
|
||||
"action":{"action":"apt_native_acceptance","recipe_id":provider::INSTALL_RECIPE,"recipe_version":"1","apt_manifest":manifest}})).unwrap();
|
||||
assert!(job.action.validate());
|
||||
let identity = readiness::Identity {
|
||||
tenant_id: id,
|
||||
asset_id: id,
|
||||
agent_id: id,
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
signing_key_id: "a".repeat(64),
|
||||
};
|
||||
let mut response = readiness::Response {
|
||||
schema_version: 1,
|
||||
challenge: id,
|
||||
identity,
|
||||
profile: "apt_only".into(),
|
||||
provider_stack_sha256: "a".repeat(64),
|
||||
fixture_acceptance_ready: true,
|
||||
bootstrap_recipes: vec![provider::INSTALL_RECIPE.into()],
|
||||
accepted_recipes: vec![],
|
||||
ledger_device: 1,
|
||||
ledger_inode: 2,
|
||||
lock_device: 1,
|
||||
lock_inode: 3,
|
||||
planning: None,
|
||||
};
|
||||
recipe_ready(&job, &response).unwrap();
|
||||
let mut normal = manifest;
|
||||
normal["action"] = serde_json::json!("apt-execute");
|
||||
job.action=serde_json::from_value(serde_json::json!({"action":"ensure_package","provider":"apt","package":"rmm-mvp-test","version":"1.0",
|
||||
"architecture":"all","recipe_version":"1","installation_context":"system","recipe_id":provider::INSTALL_RECIPE,"apt_manifest":normal})).unwrap();
|
||||
assert!(job.action.validate());
|
||||
assert!(recipe_ready(&job, &response).is_err());
|
||||
response.accepted_recipes = vec![provider::UPDATE_RECIPE.into()];
|
||||
assert!(recipe_ready(&job, &response).is_err());
|
||||
response.accepted_recipes = vec![provider::INSTALL_RECIPE.into()];
|
||||
recipe_ready(&job, &response).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn apt_route_cannot_fall_back_to_general_or_a_caller_path() {
|
||||
assert!(select_socket(true, Some("/run/general.sock".into()), None).is_err());
|
||||
assert!(
|
||||
select_socket(
|
||||
true,
|
||||
Some("/run/general.sock".into()),
|
||||
Some("/tmp/fake.sock".into())
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
select_socket(
|
||||
true,
|
||||
Some("/run/general.sock".into()),
|
||||
Some(readiness::SOCKET.into())
|
||||
)
|
||||
.unwrap(),
|
||||
Path::new(readiness::SOCKET)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn bootstrap_readiness_never_claims_package_acceptance() {
|
||||
let id = readiness::Identity {
|
||||
tenant_id: uuid::Uuid::new_v4(),
|
||||
asset_id: uuid::Uuid::new_v4(),
|
||||
agent_id: uuid::Uuid::new_v4(),
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
signing_key_id: "a".repeat(64),
|
||||
};
|
||||
let mut response = readiness::Response {
|
||||
schema_version: 1,
|
||||
challenge: uuid::Uuid::new_v4(),
|
||||
identity: id,
|
||||
profile: "apt_only".into(),
|
||||
provider_stack_sha256: "a".repeat(64),
|
||||
fixture_acceptance_ready: true,
|
||||
bootstrap_recipes: vec![provider::INSTALL_RECIPE.into()],
|
||||
accepted_recipes: vec![],
|
||||
ledger_device: 1,
|
||||
ledger_inode: 2,
|
||||
lock_device: 1,
|
||||
lock_inode: 3,
|
||||
planning: None,
|
||||
};
|
||||
assert_eq!(capabilities_from(&response), vec!["apt_native_acceptance"]);
|
||||
response.accepted_recipes = vec![provider::INSTALL_RECIPE.into()];
|
||||
assert!(!capabilities_from(&response).contains(&"apply_updates".into()));
|
||||
response
|
||||
.accepted_recipes
|
||||
.push(provider::UPDATE_RECIPE.into());
|
||||
assert!(capabilities_from(&response).contains(&"apply_updates".into()));
|
||||
}
|
||||
#[test]
|
||||
fn generic_script_never_uses_apt_socket() {
|
||||
let now = chrono::Utc::now();
|
||||
let id = uuid::Uuid::new_v4();
|
||||
let job = JobPayload {
|
||||
schema_version: 1,
|
||||
tenant_id: id,
|
||||
asset_id: id,
|
||||
agent_id: id,
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
job_id: id,
|
||||
attempt_id: id,
|
||||
action: rmm_domain::jobs::Action::RunScript {
|
||||
interpreter: "posix_sh".into(),
|
||||
script_utf8: "true".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
},
|
||||
execution_identity: "root".into(),
|
||||
not_before: now,
|
||||
start_before: now + chrono::Duration::minutes(1),
|
||||
max_runtime_seconds: 300,
|
||||
max_output_bytes: 4096,
|
||||
};
|
||||
assert!(socket_for(&job, None, Some(readiness::SOCKET.into())).is_err());
|
||||
assert_eq!(
|
||||
socket_for(
|
||||
&job,
|
||||
Some("/run/general.sock".into()),
|
||||
Some(readiness::SOCKET.into())
|
||||
)
|
||||
.unwrap(),
|
||||
Path::new("/run/general.sock")
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user