Integrate strict Windows MSI manifests tenant catalog and SYSTEM package policy
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
+207
-1
@@ -181,6 +181,12 @@ struct WebParameters {
|
||||
recipe_version: Option<String>,
|
||||
recipe_id: Option<String>,
|
||||
apt_manifest: Option<rmm_domain::apt_transaction::AptExecutionManifest>,
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_windows_msi_manifest"
|
||||
)]
|
||||
#[schemars(with = "rmm_domain::windows_execution::ResolvedWindowsMsi")]
|
||||
windows_msi_manifest: Option<Box<rmm_domain::windows_execution::ResolvedWindowsMsi>>,
|
||||
installation_context: Option<String>,
|
||||
packages: Option<Vec<String>>,
|
||||
reboot_policy: Option<String>,
|
||||
@@ -205,6 +211,9 @@ impl PlanRequest {
|
||||
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
||||
}
|
||||
let p = w.parameters;
|
||||
if p.windows_msi_manifest.is_some() && w.action != "ensure_package" {
|
||||
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
||||
}
|
||||
let required =
|
||||
|value: Option<String>| value.ok_or(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
||||
let action = match w.action.as_str() {
|
||||
@@ -251,6 +260,7 @@ impl PlanRequest {
|
||||
installation_context: required(p.installation_context)?,
|
||||
recipe_id: p.recipe_id,
|
||||
apt_manifest: p.apt_manifest,
|
||||
windows_msi_manifest: p.windows_msi_manifest,
|
||||
},
|
||||
"apply_updates" => Action::ApplyUpdates {
|
||||
provider: required(p.provider)?,
|
||||
@@ -435,6 +445,14 @@ pub async fn plan(
|
||||
{
|
||||
return Err(Error(StatusCode::FORBIDDEN, "ROOT_IDENTITY_REQUIRED"));
|
||||
}
|
||||
if input.action.windows_msi().is_some()
|
||||
&& (input.execution_identity != "system" || !input.artifact_ids.is_empty())
|
||||
{
|
||||
return Err(Error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"WINDOWS_MSI_EXACT_ARTIFACT_REQUIRED",
|
||||
));
|
||||
}
|
||||
// Conservative policy: reboot is always serial, no provider-triggered reboot.
|
||||
if matches!(input.action, Action::Reboot { .. }) && input.rollout.max_parallel != 1 {
|
||||
return Err(fail("POLICY_DENIED"));
|
||||
@@ -458,7 +476,11 @@ pub async fn plan(
|
||||
crate::permissions::require_principal(&mut tx, p, "logs:read").await?;
|
||||
}
|
||||
let recipe_references = crate::catalog::references(&mut tx, &input.action).await?;
|
||||
let artifacts = crate::catalog::resolve_artifacts(&mut tx, tenant, &input.artifact_ids).await?;
|
||||
let artifacts = if input.action.windows_msi().is_some() {
|
||||
crate::windows_msi_catalog::automatic_artifacts(&mut tx, tenant, &input.action).await?
|
||||
} else {
|
||||
crate::catalog::resolve_artifacts(&mut tx, tenant, &input.artifact_ids).await?
|
||||
};
|
||||
let mut jobs = Vec::new();
|
||||
for asset in ids {
|
||||
let rows=sqlx::query("SELECT agent_id,enrollment_generation,assignment_version,capabilities FROM agent_installations WHERE asset_id=$1 AND revoked_at IS NULL ORDER BY agent_id FOR SHARE").bind(asset).fetch_all(&mut *tx).await?;
|
||||
@@ -531,6 +553,7 @@ pub async fn plan(
|
||||
recipe_references,
|
||||
artifact_references,
|
||||
};
|
||||
crate::windows_msi_catalog::validate_plan_bindings(&payload)?;
|
||||
let bytes = encode(&payload)?;
|
||||
let plan_hash = hash(&bytes);
|
||||
sqlx::query("INSERT INTO operation_plans(tenant_id,plan_id,principal,payload,plan_hash,expires_at) VALUES($1,$2,$3,$4,$5,$6)").bind(tenant).bind(payload.plan_id).bind(&p.name).bind(&bytes).bind(&plan_hash).bind(input.schedule.start_before).execute(&mut *tx).await?;
|
||||
@@ -565,6 +588,7 @@ async fn load_plan(
|
||||
Ok((serde_json::from_slice(&bytes)?, h, bytes))
|
||||
}
|
||||
async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Result<()> {
|
||||
crate::windows_msi_catalog::validate_plan_bindings(plan)?;
|
||||
policy(tx).await?;
|
||||
let current_artifacts = crate::catalog::resolve_artifacts(
|
||||
tx,
|
||||
@@ -1511,6 +1535,188 @@ mod windows_contract_tests {
|
||||
"FORBIDDEN"
|
||||
);
|
||||
tx.rollback().await.unwrap();
|
||||
// MSI metadata only: real migration/RLS/FKs and pending-only planning.
|
||||
// This ignored test runs alone in the dedicated isolated PostgreSQL process.
|
||||
// Opaque bytes deliberately are NOT an authored/installed MSI fixture.
|
||||
use crate::artifact_store::ArtifactStore;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let key_path = temp.join("key.pem");
|
||||
let policy_path = temp.join("system-msi.json");
|
||||
let settings = [
|
||||
("RMM_PROFILE", "development".to_string()),
|
||||
("RMM_JOB_KEY_FILE", key_path.display().to_string()),
|
||||
("RMM_DEV_SYSTEM_ENABLED", "true".to_string()),
|
||||
(
|
||||
"RMM_DEV_SYSTEM_POLICY_FILE",
|
||||
policy_path.display().to_string(),
|
||||
),
|
||||
];
|
||||
let previous = settings
|
||||
.iter()
|
||||
.map(|(key, _)| (*key, std::env::var_os(key)))
|
||||
.collect::<Vec<_>>();
|
||||
for (key, value) in &settings {
|
||||
unsafe {
|
||||
std::env::set_var(key, value);
|
||||
}
|
||||
}
|
||||
let artifact_id = Uuid::new_v4();
|
||||
let opaque = b"metadata-contract-not-an-executable-msi";
|
||||
let artifact_sha = crate::catalog::store()
|
||||
.unwrap()
|
||||
.put(tenant, opaque)
|
||||
.unwrap();
|
||||
let manifest = crate::windows_msi_catalog::test_manifest(
|
||||
artifact_id,
|
||||
&artifact_sha,
|
||||
opaque.len() as u64,
|
||||
);
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO artifacts(tenant_id,artifact_id,sha256,size_bytes,media_type) VALUES($1,$2,$3,$4,'application/octet-stream')")
|
||||
.bind(tenant).bind(artifact_id).bind(&artifact_sha).bind(opaque.len() as i64).execute(&mut *tx).await.unwrap();
|
||||
let catalog_id = crate::windows_msi_catalog::store_recipe(&mut tx, tenant, &manifest)
|
||||
.await
|
||||
.unwrap();
|
||||
tx.commit().await.unwrap();
|
||||
let other = Uuid::new_v4();
|
||||
sqlx::query("INSERT INTO tenants(tenant_id,name) VALUES($1,'other MSI tenant')")
|
||||
.bind(other)
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut foreign = rmm_persistence::tenant_tx(&state.pool, other)
|
||||
.await
|
||||
.unwrap();
|
||||
let count: i64 = sqlx::query_scalar("SELECT count(*) FROM windows_msi_recipes")
|
||||
.fetch_one(&mut *foreign)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(count, 0);
|
||||
assert!(
|
||||
crate::windows_msi_catalog::references(&mut foreign, &manifest)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
foreign.rollback().await.unwrap();
|
||||
let row=sqlx::query("INSERT INTO windows_msi_recipes(tenant_id,catalog_id,recipe_id,recipe_version,recipe_sha256,artifact_id,artifact_sha256,artifact_bytes,payload) VALUES($1,$2,$3,'1',$4,$5,$6,$7,$8)")
|
||||
.bind(other).bind(Uuid::new_v4()).bind(&manifest.recipe.recipe_id).bind(&manifest.recipe_sha256)
|
||||
.bind(artifact_id).bind(&artifact_sha).bind(opaque.len() as i64).bind(encode(&manifest.recipe).unwrap()).execute(&admin).await;
|
||||
assert!(row.is_err()); // database-level composite tenant/artifact FK
|
||||
assert!(
|
||||
sqlx::query("UPDATE windows_msi_recipes SET recipe_sha256=$2 WHERE catalog_id=$1")
|
||||
.bind(catalog_id)
|
||||
.bind("cd".repeat(32))
|
||||
.execute(&admin)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
sqlx::query("DELETE FROM windows_msi_recipes WHERE catalog_id=$1")
|
||||
.bind(catalog_id)
|
||||
.execute(&admin)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
sqlx::query("UPDATE memberships SET permissions=array_append(permissions,'system:execute') WHERE tenant_id=$1 AND principal=$2").bind(tenant).bind(&principal.name).execute(&admin).await.unwrap();
|
||||
let caps = json!([
|
||||
"windows.base/1",
|
||||
"rmm.sync.v1",
|
||||
"helper.system.v1",
|
||||
"ensure_package",
|
||||
"system:ensure_package",
|
||||
rmm_domain::windows_execution::MSI_CAPABILITY,
|
||||
format!("helper.policy.sha256:{}", "ab".repeat(32)),
|
||||
format!("helper.binary.sha256:{}", "cd".repeat(32))
|
||||
]);
|
||||
sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1")
|
||||
.bind(agents[1])
|
||||
.bind(&caps)
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let system = json!({"schema_version":2,"profile":"development","expires_at":Utc::now()+Duration::minutes(10),"scopes":[{
|
||||
"tenant_id":tenant,"asset_id":assets[1],"agent_id":agents[1],"enrollment_generation":1,"assignment_version":1,
|
||||
"principals":[principal.name],"helper_policy_sha256":"ab".repeat(32),"helper_binary_sha256":"cd".repeat(32),
|
||||
"acceptance_evidence_id":Uuid::new_v4(),"allowed_services":[],"max_runtime_seconds":120,"allowed_msi_recipe_sha256":[manifest.recipe_sha256]}]});
|
||||
std::fs::write(&policy_path, encode(&system).unwrap()).unwrap();
|
||||
std::fs::set_permissions(&policy_path, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
let mut package = input();
|
||||
package.targets.asset_ids = vec![assets[1]];
|
||||
package.script_variants.clear();
|
||||
package.execution_identity = "system".into();
|
||||
package.action = crate::windows_msi_catalog::test_action(manifest.clone());
|
||||
package.max_runtime_seconds = 30;
|
||||
package.max_output_bytes = 4096;
|
||||
let mut extra = package.clone();
|
||||
extra.artifact_ids = vec![artifact_id];
|
||||
assert_eq!(
|
||||
plan(&state, &principal, tenant, extra).await.unwrap_err().1,
|
||||
"WINDOWS_MSI_EXACT_ARTIFACT_REQUIRED"
|
||||
);
|
||||
let view = plan(&state, &principal, tenant, package).await.unwrap();
|
||||
let package_id = Uuid::parse_str(view["plan_id"].as_str().unwrap()).unwrap();
|
||||
let raw: Vec<u8> =
|
||||
sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1")
|
||||
.bind(package_id)
|
||||
.fetch_one(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let fixed: PlanPayload = serde_json::from_slice(&raw).unwrap();
|
||||
assert_eq!(fixed.artifact_references.len(), 1);
|
||||
assert_eq!(
|
||||
fixed.artifact_references[0].artifact.artifact_id,
|
||||
artifact_id
|
||||
);
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
|
||||
.await
|
||||
.unwrap();
|
||||
recheck(&mut tx, &fixed).await.unwrap();
|
||||
tx.rollback().await.unwrap();
|
||||
for case in 0..4 {
|
||||
let mut bad = fixed.clone();
|
||||
match case {
|
||||
0 => bad.artifact_references.clear(),
|
||||
1 => bad
|
||||
.artifact_references
|
||||
.push(bad.artifact_references[0].clone()),
|
||||
2 => bad.artifact_references[0].artifact.size_bytes += 1,
|
||||
_ => bad.artifact_references[0].artifact.artifact_id = Uuid::new_v4(),
|
||||
};
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(recheck(&mut tx, &bad).await.is_err());
|
||||
tx.rollback().await.unwrap();
|
||||
}
|
||||
let pending = submit(
|
||||
&state,
|
||||
&principal,
|
||||
tenant,
|
||||
package_id,
|
||||
Submit {
|
||||
idempotency_key: "fixed-msi-metadata".into(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(pending["approval_state"], "pending_approval");
|
||||
assert_eq!(pending["job_count"], 0);
|
||||
sqlx::query("UPDATE memberships SET permissions=array_remove(permissions,'system:execute') WHERE tenant_id=$1 AND principal=$2").bind(tenant).bind(&principal.name).execute(&admin).await.unwrap();
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(recheck(&mut tx, &fixed).await.unwrap_err().1, "FORBIDDEN");
|
||||
tx.rollback().await.unwrap();
|
||||
for (key, value) in previous {
|
||||
unsafe {
|
||||
match value {
|
||||
Some(value) => std::env::set_var(key, value),
|
||||
None => std::env::remove_var(key),
|
||||
}
|
||||
}
|
||||
}
|
||||
let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM jobs WHERE tenant_id=$1),(SELECT count(*) FROM outbox WHERE tenant_id=$1),(SELECT count(*) FROM execution_permits WHERE tenant_id=$1)").bind(tenant).fetch_one(&admin).await.unwrap();
|
||||
assert_eq!(counts, (0, 0, 0));
|
||||
let after: Vec<u8> =
|
||||
|
||||
Reference in New Issue
Block a user