Recheck the submitting principal and protect stored log output
This commit is contained in:
+138
-79
@@ -13,6 +13,7 @@ use rmm_domain::jobs::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use sqlx::{Postgres, Row, Transaction};
|
||||
use std::collections::BTreeSet;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -606,21 +607,9 @@ pub(crate) async fn plan_resolved(
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
|
||||
policy(&mut tx).await?;
|
||||
crate::permissions::require_principal(&mut tx, p, "operations:plan").await?;
|
||||
if let Some(permission) = crate::root_policy::execution_permission(&input.execution_identity) {
|
||||
for permission in execution_permissions(&input.action, &input.execution_identity) {
|
||||
crate::permissions::require_principal(&mut tx, p, permission).await?;
|
||||
}
|
||||
if matches!(input.action, Action::AptNativeAcceptance { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "apt:native_acceptance").await?;
|
||||
}
|
||||
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 let Action::UpdateAgent { update } = &input.action {
|
||||
if !input.artifact_ids.is_empty() {
|
||||
@@ -760,7 +749,99 @@ async fn load_plan(
|
||||
}
|
||||
Ok((serde_json::from_slice(&bytes)?, h, bytes))
|
||||
}
|
||||
fn execution_permissions(action: &Action, identity: &str) -> impl Iterator<Item = &'static str> {
|
||||
let action_permission = match action {
|
||||
Action::RunScript { .. } => Some("scripts:execute"),
|
||||
Action::UpdateAgent { .. } => Some("updates:apply"),
|
||||
Action::QueryLogs { .. } => Some("logs:read"),
|
||||
Action::AptNativeAcceptance { .. } => Some("apt:native_acceptance"),
|
||||
_ => None,
|
||||
};
|
||||
[
|
||||
action_permission,
|
||||
crate::root_policy::execution_permission(identity),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
}
|
||||
|
||||
struct JobParticipants<'a> {
|
||||
planner: &'a str,
|
||||
submitter: &'a str,
|
||||
approver: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> JobParticipants<'a> {
|
||||
fn distinct(&self) -> BTreeSet<&'a str> {
|
||||
[self.planner, self.submitter, self.approver].into()
|
||||
}
|
||||
|
||||
async fn require_roles(
|
||||
&self,
|
||||
state: &crate::State,
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
tenant: Uuid,
|
||||
) -> Result<()> {
|
||||
for name in self.distinct() {
|
||||
if !requester_enabled(state, tx, name, tenant).await? {
|
||||
return Err(fail("AUTHORIZATION_REVOKED"));
|
||||
}
|
||||
}
|
||||
for (name, permission) in [
|
||||
(self.planner, "operations:plan"),
|
||||
(self.submitter, "operations:submit"),
|
||||
(self.submitter, "operations:read"),
|
||||
(self.approver, "plans:approve"),
|
||||
] {
|
||||
crate::permissions::require(tx, name, permission).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn authorize_approved_job(
|
||||
state: &crate::State,
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
tenant: Uuid,
|
||||
plan: &PlanPayload,
|
||||
job: &JobPayload,
|
||||
row: &sqlx::postgres::PgRow,
|
||||
) -> Result<()> {
|
||||
let participants = JobParticipants {
|
||||
planner: &plan.principal,
|
||||
submitter: row.get("submitter"),
|
||||
approver: row.get("approver"),
|
||||
};
|
||||
if job.tenant_id != tenant || plan.tenant_id != tenant {
|
||||
return Err(fail("BINDING_MISMATCH"));
|
||||
}
|
||||
participants.require_roles(state, tx, tenant).await?;
|
||||
authorize_execution(tx, &participants.distinct(), job).await
|
||||
}
|
||||
|
||||
async fn authorize_execution(
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
actors: &BTreeSet<&str>,
|
||||
job: &JobPayload,
|
||||
) -> Result<()> {
|
||||
for &name in actors {
|
||||
for permission in execution_permissions(&job.action, &job.execution_identity) {
|
||||
crate::permissions::require(tx, name, permission).await?;
|
||||
}
|
||||
crate::root_policy::authorize(tx, name, job).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Result<()> {
|
||||
recheck_for(tx, plan, &[plan.principal.as_str()].into()).await
|
||||
}
|
||||
|
||||
async fn recheck_for(
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
plan: &PlanPayload,
|
||||
actors: &BTreeSet<&str>,
|
||||
) -> Result<()> {
|
||||
crate::script_library::validate_plan_bindings(plan)?;
|
||||
crate::windows_msi_catalog::validate_plan_bindings(plan)?;
|
||||
crate::agent_updates::validate_plan_bindings(plan)?;
|
||||
@@ -799,27 +880,6 @@ async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Resu
|
||||
}
|
||||
}
|
||||
crate::permissions::require(tx, &plan.principal, "operations:plan").await?;
|
||||
if plan
|
||||
.jobs
|
||||
.iter()
|
||||
.any(|j| matches!(j.payload.action, Action::RunScript { .. }))
|
||||
{
|
||||
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()
|
||||
.any(|j| matches!(j.payload.action, Action::QueryLogs { .. }))
|
||||
{
|
||||
crate::permissions::require(tx, &plan.principal, "logs:read").await?;
|
||||
}
|
||||
let mut references = Vec::new();
|
||||
for job in &plan.jobs {
|
||||
if !job.payload.action.valid_job_limits(
|
||||
@@ -831,7 +891,7 @@ async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Resu
|
||||
if !job.payload.action.validate() {
|
||||
return Err(Error(StatusCode::CONFLICT, "APT_MANIFEST_REQUIRED"));
|
||||
}
|
||||
crate::root_policy::authorize(tx, &plan.principal, &job.payload).await?;
|
||||
authorize_execution(tx, actors, &job.payload).await?;
|
||||
crate::catalog::check(tx, &job.payload.action).await?;
|
||||
references.extend(crate::catalog::references(tx, &job.payload.action).await?);
|
||||
if job.payload.start_before <= Utc::now() {
|
||||
@@ -906,24 +966,10 @@ pub async fn submit(
|
||||
let request_hash = hash(id.as_bytes());
|
||||
let (plan, _, _) = load_plan(&mut tx, id).await?;
|
||||
for job in &plan.jobs {
|
||||
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?;
|
||||
}
|
||||
if let Some(permission) =
|
||||
crate::root_policy::execution_permission(&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::root_policy::authorize(&mut tx, &p.name, &job.payload).await?;
|
||||
}
|
||||
if matches!(job.payload.action, Action::AptNativeAcceptance { .. }) {
|
||||
crate::permissions::require_principal(&mut tx, p, "apt:native_acceptance").await?;
|
||||
}
|
||||
crate::root_policy::authorize(&mut tx, &p.name, &job.payload).await?;
|
||||
}
|
||||
if let Some(row)=sqlx::query("SELECT request_hash,operation_id FROM operation_submissions WHERE principal=$1 AND idempotency_key=$2").bind(&p.name).bind(&input.idempotency_key).fetch_optional(&mut *tx).await? {
|
||||
if row.get::<String,_>("request_hash")!=request_hash{return Err(fail("IDEMPOTENCY_CONFLICT"))}
|
||||
@@ -970,6 +1016,28 @@ struct Authorize {
|
||||
fn approve_decision() -> String {
|
||||
"approve".into()
|
||||
}
|
||||
pub(crate) async fn recheck_approval(
|
||||
state: &crate::State,
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
plan: &PlanPayload,
|
||||
submitter: &str,
|
||||
approver: &Principal,
|
||||
) -> Result<()> {
|
||||
let participants = JobParticipants {
|
||||
planner: &plan.principal,
|
||||
submitter,
|
||||
approver: &approver.name,
|
||||
};
|
||||
participants.require_roles(state, tx, plan.tenant_id).await?;
|
||||
for job in &plan.jobs {
|
||||
if execution_permissions(&job.payload.action, &job.payload.execution_identity)
|
||||
.any(|permission| !approver.permits_scope(permission))
|
||||
{
|
||||
return Err(Error(StatusCode::FORBIDDEN, "FORBIDDEN"));
|
||||
}
|
||||
}
|
||||
recheck_for(tx, plan, &participants.distinct()).await
|
||||
}
|
||||
async fn authorize_http(
|
||||
State(state): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
@@ -987,7 +1055,7 @@ async fn authorize_http(
|
||||
}
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
|
||||
let row = sqlx::query(
|
||||
"SELECT operation_id,approval_state FROM approval_requests WHERE plan_id=$1 FOR UPDATE",
|
||||
"SELECT operation_id,approval_state,principal FROM approval_requests WHERE plan_id=$1 FOR UPDATE",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_optional(&mut *tx)
|
||||
@@ -1013,26 +1081,7 @@ async fn authorize_http(
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
} else {
|
||||
recheck(&mut tx, &plan).await?;
|
||||
// Both requester and approver retain explicit operator rights: the only
|
||||
// shipped dev principals are fixed-scope and cannot grant memberships.
|
||||
if !requester_enabled(&state, &mut tx, &plan.principal, tenant).await? {
|
||||
return Err(fail("AUTHORIZATION_REVOKED"));
|
||||
}
|
||||
for job in &plan.jobs {
|
||||
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)
|
||||
{
|
||||
crate::permissions::require_principal(&mut tx, &p, permission).await?;
|
||||
}
|
||||
crate::root_policy::authorize(&mut tx, &p.name, &job.payload).await?;
|
||||
}
|
||||
recheck_approval(&state, &mut tx, &plan, row.get("principal"), &p).await?;
|
||||
let authorization = Uuid::new_v4();
|
||||
sqlx::query("INSERT INTO authorizations(tenant_id,authorization_id,operation_id,plan_id,plan_hash,principal,session_hash,approval_origin,testsimulation,policy_version) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,1)").bind(tenant).bind(authorization).bind(op).bind(id).bind(&ph).bind(&p.name).bind(session).bind(p.approval_origin()).bind(p.testsimulation).execute(&mut *tx).await?;
|
||||
sqlx::query("INSERT INTO operations VALUES($1,$2,$3)")
|
||||
@@ -1137,11 +1186,6 @@ pub async fn operation(
|
||||
tx.commit().await?;
|
||||
Ok(result)
|
||||
}
|
||||
fn job_view(r: &sqlx::postgres::PgRow) -> Value {
|
||||
let mut result = job_summary(r);
|
||||
result["result"] = r.get::<Option<Value>, _>("result").unwrap_or(Value::Null);
|
||||
result
|
||||
}
|
||||
fn job_summary(r: &sqlx::postgres::PgRow) -> Value {
|
||||
json!({"asset_id":r.get::<Uuid,_>("asset_id"),"agent_id":r.get::<Uuid,_>("agent_id"),"job_id":r.get::<Uuid,_>("job_id"),"attempt_id":r.get::<Uuid,_>("attempt_id"),"state":r.get::<String,_>("state"),"cancel_requested":r.get::<bool,_>("cancel_requested"),"revision":r.get::<i64,_>("revision"),"pending_reason":r.get::<Option<String>,_>("pending_reason")})
|
||||
}
|
||||
@@ -1150,14 +1194,29 @@ pub async fn job(state: &crate::State, p: &Principal, tenant: Uuid, id: Uuid) ->
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
|
||||
crate::permissions::require_principal(&mut tx, p, "operations:read").await?;
|
||||
let r = sqlx::query(
|
||||
"SELECT asset_id,agent_id,job_id,attempt_id,state,cancel_requested,revision,result,pending_reason FROM jobs WHERE job_id=$1",
|
||||
"SELECT asset_id,agent_id,job_id,attempt_id,state,cancel_requested,revision,result,pending_reason,
|
||||
convert_from(payload,'UTF8')::jsonb->'action'->>'action' AS action, log_sensitive
|
||||
FROM jobs WHERE job_id=$1",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
.ok_or_else(missing)?;
|
||||
let mut view = job_summary(&r);
|
||||
let stored = r.get::<Option<Value>, _>("result");
|
||||
if let Some(result) = stored {
|
||||
if r.try_get::<String, _>("action")? == "query_logs"
|
||||
&& !crate::monitoring::LogReadAccess::for_principal(&mut tx, p).await?.permits(r.get("log_sensitive"))
|
||||
{
|
||||
view["result"] = json!({"output_restricted":true,"exit_code":result["exit_code"].as_i64()});
|
||||
} else {
|
||||
view["result"] = result;
|
||||
}
|
||||
} else {
|
||||
view["result"] = Value::Null;
|
||||
}
|
||||
tx.commit().await?;
|
||||
Ok(job_view(&r))
|
||||
Ok(view)
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
|
||||
Reference in New Issue
Block a user