Keep participant checks consistent across submission and dispatch
This commit is contained in:
@@ -528,7 +528,25 @@ pub async fn permit(
|
|||||||
denial_code: code.into(),
|
denial_code: code.into(),
|
||||||
unreserved_request: None,
|
unreserved_request: None,
|
||||||
};
|
};
|
||||||
let row=sqlx::query("SELECT j.*,a.authorization_id,a.revoked_at,a.principal AS approver,r.principal AS submitter,p.plan_hash,p.payload AS plan_payload,r.approval_state,i.enrollment_generation,i.assignment_version,i.asset_id AS bound_asset,i.capabilities,i.revoked_at AS device_revoked,i.connection_generation,t.changes_stopped FROM jobs j JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=j.tenant_id AND p.plan_id=r.plan_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id JOIN tenants t ON t.tenant_id=j.tenant_id WHERE j.job_id=$1 AND j.attempt_id=$2 AND j.agent_id=$3 FOR UPDATE OF j").bind(Uuid::parse_str(&r.job_id)?).bind(Uuid::parse_str(&r.attempt_id)?).bind(agent).fetch_optional(&mut *tx).await?;
|
let row = sqlx::query(
|
||||||
|
"SELECT j.*, a.authorization_id, a.revoked_at, a.principal AS approver, r.principal AS submitter,
|
||||||
|
p.plan_hash, p.payload AS plan_payload, r.approval_state,
|
||||||
|
i.enrollment_generation, i.assignment_version, i.asset_id AS bound_asset,
|
||||||
|
i.capabilities, i.revoked_at AS device_revoked, i.connection_generation, t.changes_stopped
|
||||||
|
FROM jobs j
|
||||||
|
JOIN operations o USING (tenant_id,operation_id)
|
||||||
|
JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id
|
||||||
|
JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id
|
||||||
|
JOIN operation_plans p ON p.tenant_id=r.tenant_id AND p.plan_id=r.plan_id
|
||||||
|
JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id
|
||||||
|
JOIN tenants t ON t.tenant_id=j.tenant_id
|
||||||
|
WHERE j.job_id=$1 AND j.attempt_id=$2 AND j.agent_id=$3 FOR UPDATE OF j",
|
||||||
|
)
|
||||||
|
.bind(Uuid::parse_str(&r.job_id)?)
|
||||||
|
.bind(Uuid::parse_str(&r.attempt_id)?)
|
||||||
|
.bind(agent)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?;
|
||||||
let Some(row) = row else {
|
let Some(row) = row else {
|
||||||
return Ok(denied("NOT_FOUND"));
|
return Ok(denied("NOT_FOUND"));
|
||||||
};
|
};
|
||||||
@@ -798,7 +816,28 @@ pub async fn next(
|
|||||||
// without leasing it so later eligible work on the same device can proceed.
|
// without leasing it so later eligible work on the same device can proceed.
|
||||||
let mut after = None;
|
let mut after = None;
|
||||||
let row = loop {
|
let row = loop {
|
||||||
let row=sqlx::query("SELECT j.job_id,j.attempt_id,j.payload,j.payload_hash,a.authorization_id,a.principal AS approver,r.principal AS submitter,p.plan_hash,p.payload AS plan_payload,i.capabilities FROM outbox b JOIN jobs j USING(tenant_id,job_id,attempt_id) JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=j.tenant_id AND p.plan_id=r.plan_id JOIN tenants t ON t.tenant_id=j.tenant_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id WHERE j.agent_id=$1 AND ($2::uuid IS NULL OR j.job_id>$2) AND j.state='queued' AND NOT j.cancel_requested AND NOT t.changes_stopped AND a.revoked_at IS NULL AND r.approval_state='approved' AND p.expires_at>now() AND b.acknowledged_at IS NULL AND (b.lease_until IS NULL OR b.lease_until<now()) ORDER BY j.job_id FOR UPDATE OF b SKIP LOCKED LIMIT 1").bind(agent).bind(after).fetch_optional(&mut *tx).await?;
|
let row = sqlx::query(
|
||||||
|
"SELECT j.job_id, j.attempt_id, j.payload, j.payload_hash, a.authorization_id,
|
||||||
|
a.principal AS approver, r.principal AS submitter, p.plan_hash,
|
||||||
|
p.payload AS plan_payload, i.capabilities
|
||||||
|
FROM outbox b
|
||||||
|
JOIN jobs j USING (tenant_id,job_id,attempt_id)
|
||||||
|
JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id
|
||||||
|
JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id
|
||||||
|
JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id
|
||||||
|
JOIN operation_plans p ON p.tenant_id=r.tenant_id AND p.plan_id=r.plan_id
|
||||||
|
JOIN tenants t ON t.tenant_id=j.tenant_id
|
||||||
|
JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id
|
||||||
|
WHERE j.agent_id=$1 AND ($2::uuid IS NULL OR j.job_id>$2)
|
||||||
|
AND j.state='queued' AND NOT j.cancel_requested AND NOT t.changes_stopped
|
||||||
|
AND a.revoked_at IS NULL AND r.approval_state='approved' AND p.expires_at>now()
|
||||||
|
AND b.acknowledged_at IS NULL AND (b.lease_until IS NULL OR b.lease_until<now())
|
||||||
|
ORDER BY j.job_id FOR UPDATE OF b SKIP LOCKED LIMIT 1",
|
||||||
|
)
|
||||||
|
.bind(agent)
|
||||||
|
.bind(after)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?;
|
||||||
let Some(row) = row else {
|
let Some(row) = row else {
|
||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
|||||||
@@ -834,6 +834,7 @@ async fn authorize_execution(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Result<()> {
|
async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Result<()> {
|
||||||
|
crate::permissions::require(tx, &plan.principal, "operations:plan").await?;
|
||||||
recheck_for(tx, plan, &[plan.principal.as_str()].into()).await
|
recheck_for(tx, plan, &[plan.principal.as_str()].into()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -879,7 +880,6 @@ async fn recheck_for(
|
|||||||
return Err(fail("ARTIFACT_BINDING_MISMATCH"));
|
return Err(fail("ARTIFACT_BINDING_MISMATCH"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
crate::permissions::require(tx, &plan.principal, "operations:plan").await?;
|
|
||||||
let mut references = Vec::new();
|
let mut references = Vec::new();
|
||||||
for job in &plan.jobs {
|
for job in &plan.jobs {
|
||||||
if !job.payload.action.valid_job_limits(
|
if !job.payload.action.valid_job_limits(
|
||||||
@@ -966,7 +966,8 @@ pub async fn submit(
|
|||||||
let request_hash = hash(id.as_bytes());
|
let request_hash = hash(id.as_bytes());
|
||||||
let (plan, _, _) = load_plan(&mut tx, id).await?;
|
let (plan, _, _) = load_plan(&mut tx, id).await?;
|
||||||
for job in &plan.jobs {
|
for job in &plan.jobs {
|
||||||
for permission in execution_permissions(&job.payload.action, &job.payload.execution_identity) {
|
for permission in execution_permissions(&job.payload.action, &job.payload.execution_identity)
|
||||||
|
{
|
||||||
crate::permissions::require_principal(&mut tx, p, permission).await?;
|
crate::permissions::require_principal(&mut tx, p, permission).await?;
|
||||||
}
|
}
|
||||||
crate::root_policy::authorize(&mut tx, &p.name, &job.payload).await?;
|
crate::root_policy::authorize(&mut tx, &p.name, &job.payload).await?;
|
||||||
|
|||||||
Reference in New Issue
Block a user