Add gated agent update planning, preserved handoff and Linux worker ports
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-14 21:48:21 +01:00
parent dc9cb1b096
commit 1e89c4577f
82 changed files with 25052 additions and 69 deletions
+179 -13
View File
@@ -121,11 +121,36 @@ pub struct PlanInput {
#[derive(Clone, Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
pub enum PlanRequest {
AgentUpdate(Box<AgentUpdatePlanInput>),
Typed(Box<PlanInput>),
Web(Box<WebPlanInput>),
}
#[derive(Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct AgentUpdatePlanInput {
tenant_id: Uuid,
action: AgentUpdateRequestAction,
targets: Targets,
parameters: AgentUpdateParameters,
schedule: WebSchedule,
rollout: Rollout,
reason: String,
}
#[derive(Clone, Deserialize, schemars::JsonSchema)]
pub enum AgentUpdateRequestAction {
#[serde(rename = "update_agent")]
UpdateAgent,
}
#[derive(Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
struct AgentUpdateParameters {
release_id: Uuid,
execution_identity: String,
timeout_seconds: u32,
max_output_bytes: u32,
}
#[derive(Clone, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct WebPlanInput {
tenant_id: Uuid,
action: String,
@@ -204,6 +229,12 @@ struct WebParameters {
impl PlanRequest {
pub fn resolve(self, tenant: Uuid) -> Result<PlanInput> {
let w = match self {
Self::AgentUpdate(_) => {
return Err(Error(
StatusCode::BAD_REQUEST,
"AGENT_UPDATE_RELEASE_ID_REQUIRED",
));
}
Self::Typed(t) => return Ok(*t),
Self::Web(w) => *w,
};
@@ -325,6 +356,68 @@ impl PlanRequest {
})
}
}
/// Both HTTP and MCP resolve update jobs only from a fixed catalog release and
/// saved authenticated per-device observations. Caller-supplied local pins are
/// never an alternative route to the typed immutable execution payload.
pub async fn plan_request(
state: &crate::State,
p: &Principal,
tenant: Uuid,
request: PlanRequest,
) -> Result<Value> {
let PlanRequest::AgentUpdate(input) = request else {
return plan(state, p, tenant, request.resolve(tenant)?).await;
};
scope(p, tenant, true)?;
if input.tenant_id != tenant {
return Err(missing());
}
if input.schedule.offline_policy != "wait_until_deadline"
|| input.parameters.release_id.is_nil()
|| input.targets.asset_ids.is_empty()
|| input.targets.asset_ids.len() > 100
{
return Err(fail("VALIDATION_ERROR"));
}
let _ = &input.action;
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
crate::permissions::require_principal(&mut tx, p, "operations:plan").await?;
crate::permissions::require_principal(&mut tx, p, "updates:apply").await?;
let mut updates = std::collections::BTreeMap::new();
for asset in &input.targets.asset_ids {
let update =
crate::agent_updates::resolve_target(&mut tx, *asset, input.parameters.release_id)
.await?;
if update.execution_identity() != Some(input.parameters.execution_identity.as_str()) {
return Err(fail("EXECUTION_IDENTITY_DENIED"));
}
updates.insert(*asset, update);
}
tx.commit().await?;
let first = updates
.values()
.next()
.ok_or_else(|| fail("NOT_FOUND"))?
.clone();
let resolved = PlanInput {
targets: input.targets,
action: Action::UpdateAgent {
update: Box::new(first),
},
script_variants: Vec::new(),
execution_identity: input.parameters.execution_identity,
schedule: Schedule {
not_before: input.schedule.not_before,
start_before: input.schedule.start_before,
},
max_runtime_seconds: input.parameters.timeout_seconds,
max_output_bytes: input.parameters.max_output_bytes,
rollout: input.rollout,
reason: input.reason,
artifact_ids: Vec::new(),
};
plan_resolved(state, p, tenant, resolved, Some(updates)).await
}
fn validate_variants(variants: &[Variant]) -> Result<()> {
if variants.is_empty() || variants.len() > 2 {
return Err(fail("AMBIGUOUS_TARGET"));
@@ -427,6 +520,23 @@ pub async fn plan(
p: &Principal,
tenant: Uuid,
input: PlanInput,
) -> Result<Value> {
if matches!(input.action, Action::UpdateAgent { .. }) {
return Err(Error(
StatusCode::BAD_REQUEST,
"AGENT_UPDATE_RELEASE_ID_REQUIRED",
));
}
plan_resolved(state, p, tenant, input, None).await
}
async fn plan_resolved(
state: &crate::State,
p: &Principal,
tenant: Uuid,
input: PlanInput,
updates: Option<
std::collections::BTreeMap<Uuid, rmm_domain::agent_update::ResolvedAgentUpdate>,
>,
) -> Result<Value> {
scope(p, tenant, true)?;
let now = Utc::now();
@@ -495,11 +605,27 @@ pub async fn plan(
if matches!(input.action, Action::RunScript { .. }) {
crate::permissions::require_principal(&mut tx, p, "scripts:execute").await?;
}
if matches!(input.action, Action::UpdateAgent { .. }) {
crate::permissions::require_principal(&mut tx, p, "updates:apply").await?;
}
if matches!(input.action, Action::QueryLogs { .. }) {
crate::permissions::require_principal(&mut tx, p, "logs:read").await?;
}
let recipe_references = crate::catalog::references(&mut tx, &input.action).await?;
let artifacts = if input.action.windows_msi().is_some() {
let artifacts = if let Action::UpdateAgent { update } = &input.action {
if !input.artifact_ids.is_empty() {
return Err(fail("AGENT_UPDATE_EXACT_ARTIFACT_REQUIRED"));
}
crate::agent_updates::resolve_artifacts(
&mut tx,
tenant,
&update
.manifest()
.map_err(|_| fail("INVALID_AGENT_RELEASE"))?
.artifacts,
)
.await?
} else 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?
@@ -516,7 +642,18 @@ pub async fn plan(
}
let row = &rows[0];
let caps = capabilities(row.get("capabilities"))?;
let action = target_action(&input, &caps)?;
let action = if let Some(updates) = &updates {
Action::UpdateAgent {
update: Box::new(
updates
.get(&asset)
.ok_or_else(|| fail("AGENT_UPDATE_STATE_NOT_CONFIGURED"))?
.clone(),
),
}
} else {
target_action(&input, &caps)?
};
if !action.valid_job_limits(input.max_runtime_seconds, input.max_output_bytes) {
return Err(fail("VALIDATION_ERROR"));
}
@@ -577,6 +714,7 @@ pub async fn plan(
artifact_references,
};
crate::windows_msi_catalog::validate_plan_bindings(&payload)?;
crate::agent_updates::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?;
@@ -612,17 +750,32 @@ async fn load_plan(
}
async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Result<()> {
crate::windows_msi_catalog::validate_plan_bindings(plan)?;
crate::agent_updates::validate_plan_bindings(plan)?;
policy(tx).await?;
let current_artifacts = crate::catalog::resolve_artifacts(
tx,
plan.tenant_id,
&plan
.artifact_references
.iter()
.map(|r| r.artifact.artifact_id)
.collect::<Vec<_>>(),
)
.await?;
let current_artifacts = if let Some(Action::UpdateAgent { update }) =
plan.jobs.first().map(|j| &j.payload.action)
{
crate::agent_updates::resolve_artifacts(
tx,
plan.tenant_id,
&update
.manifest()
.map_err(|_| fail("INVALID_AGENT_RELEASE"))?
.artifacts,
)
.await?
} else {
crate::catalog::resolve_artifacts(
tx,
plan.tenant_id,
&plan
.artifact_references
.iter()
.map(|r| r.artifact.artifact_id)
.collect::<Vec<_>>(),
)
.await?
};
for reference in &plan.artifact_references {
if !current_artifacts.contains(&reference.artifact)
|| !plan.jobs.iter().any(|j| {
@@ -640,6 +793,13 @@ async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Resu
{
crate::permissions::require(tx, &plan.principal, "scripts:execute").await?;
}
if plan
.jobs
.iter()
.any(|j| matches!(j.payload.action, Action::UpdateAgent { .. }))
{
crate::permissions::require(tx, &plan.principal, "updates:apply").await?;
}
if plan
.jobs
.iter()
@@ -736,6 +896,9 @@ pub async fn submit(
if matches!(job.payload.action, Action::RunScript { .. }) {
crate::permissions::require_principal(&mut tx, p, "scripts:execute").await?;
}
if matches!(job.payload.action, Action::UpdateAgent { .. }) {
crate::permissions::require_principal(&mut tx, p, "updates:apply").await?;
}
if matches!(job.payload.action, Action::QueryLogs { .. }) {
crate::permissions::require_principal(&mut tx, p, "logs:read").await?;
}
@@ -847,6 +1010,9 @@ async fn authorize_http(
if matches!(job.payload.action, Action::RunScript { .. }) {
crate::permissions::require_principal(&mut tx, &p, "scripts:execute").await?;
}
if matches!(job.payload.action, Action::UpdateAgent { .. }) {
crate::permissions::require_principal(&mut tx, &p, "updates:apply").await?;
}
if let Some(permission) =
crate::root_policy::execution_permission(&job.payload.execution_identity)
{
@@ -993,7 +1159,7 @@ async fn plan_http(
Json(i): Json<PlanRequest>,
) -> Result<Json<Value>> {
let p = principal(&s, &h, true).await?;
Ok(Json(plan(&s, &p, t, i.resolve(t)?).await?))
Ok(Json(plan_request(&s, &p, t, i).await?))
}
async fn submit_http(
State(s): State<crate::State>,