Rust development slice / linux (push) Failing after 7s
Bind the administrative connection-205 review to original signed history and preserve closed readiness. Include the qualified server dependency closure, UI review, native ARM64 build provenance, and guarded deployment evidence. Migration, API/gateway replacement and TLS checks passed with unchanged job authority, protected mounts and time/database services; native Windows receipt application remains separate.
513 lines
22 KiB
Rust
513 lines
22 KiB
Rust
//! Exact connection-205 interactive administrative closure. No job/readiness writes.
|
|
use crate::{auth::Principal, jobs::Error, windows_checkpoint_review::access};
|
|
use axum::{
|
|
Json, Router,
|
|
extract::{Path, State},
|
|
http::{HeaderMap, StatusCode},
|
|
routing::{get, post},
|
|
};
|
|
use chrono::{Duration, SubsecRound, Utc};
|
|
use openssl::{
|
|
pkey::{Id, PKey},
|
|
sign::Verifier,
|
|
};
|
|
use prost::Message;
|
|
use rmm_domain::{
|
|
checkpoint_repair::{SignedReceipt, inventory_sha256},
|
|
jobs::{ExecutionPermitPayload, JobPayload, PlanPayload, encode, hash},
|
|
reconciliation::ReconcileDecisionPayload,
|
|
windows_echo_completion::{Receipt as Completion, successful_outcome},
|
|
windows_echo_probe::{AGENT, ASSET, TENANT, TEST_PRINCIPAL},
|
|
windows_sync_alignment::{self as profile, Receipt, Witness},
|
|
};
|
|
use rmm_protocol::{JobOffer, SyncRequest};
|
|
use serde::Deserialize;
|
|
use serde_json::{Value, json};
|
|
use sqlx::{Postgres, Row, Transaction};
|
|
use uuid::Uuid;
|
|
type Result<T> = std::result::Result<T, Error>;
|
|
fn mismatch() -> Error {
|
|
Error(StatusCode::CONFLICT, "WINDOWS_SYNC_ALIGNMENT_MISMATCH")
|
|
}
|
|
fn browser(p: &Principal) -> Result<&str> {
|
|
if p.name != TEST_PRINCIPAL
|
|
|| p.tenant != TENANT
|
|
|| !p.testsimulation
|
|
|| !p.operator
|
|
|| p.identity.is_some()
|
|
|| p.scopes.is_some()
|
|
|| p.csrf.is_none()
|
|
{
|
|
return Err(Error(StatusCode::FORBIDDEN, "DEV_TEST_WEBUI_REQUIRED"));
|
|
}
|
|
p.session_hash
|
|
.as_deref()
|
|
.ok_or(Error(StatusCode::FORBIDDEN, "DEV_TEST_WEBUI_REQUIRED"))
|
|
}
|
|
pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
|
|
r.route(
|
|
"/api/v1/tenants/{tenant}/windows-sync-alignment",
|
|
get(read).post(submit),
|
|
)
|
|
.route(
|
|
"/api/v1/tenants/{tenant}/windows-sync-alignment/{request}/confirm",
|
|
post(confirm),
|
|
)
|
|
.route(
|
|
"/api/v1/tenants/{tenant}/windows-sync-alignment/{request}/receipt",
|
|
get(download),
|
|
)
|
|
}
|
|
fn verify(s: &crate::State, bytes: &[u8], signature: &[u8]) -> Result<()> {
|
|
let key = PKey::public_key_from_raw_bytes(&s.job_signer.public_key()?, Id::ED25519)
|
|
.map_err(|_| mismatch())?;
|
|
if bytes.len() > profile::MAX_WITNESS_BYTES + 8192
|
|
|| signature.len() != 64
|
|
|| !Verifier::new_without_digest(&key)
|
|
.map_err(|_| mismatch())?
|
|
.verify_oneshot(signature, bytes)
|
|
.map_err(|_| mismatch())?
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
Ok(())
|
|
}
|
|
fn original(w: &Witness, i: usize) -> Result<(SyncRequest, ReconcileDecisionPayload)> {
|
|
let raw = &w.original_requests[i];
|
|
let r = SyncRequest::decode(raw.as_slice()).map_err(|_| mismatch())?;
|
|
let d: ReconcileDecisionPayload =
|
|
serde_json::from_slice(&w.original_responses[i].decision_bytes)?;
|
|
let (id, rid, cp, hcp) = if i == 0 {
|
|
(profile::FIRST, profile::FIRST_REQUEST, 10135, 134)
|
|
} else {
|
|
(profile::SECOND, profile::SECOND_REQUEST, 10137, 135)
|
|
};
|
|
if r.encode_to_vec() != *raw
|
|
|| r.request_id != rid.to_string()
|
|
|| r.journal_id != w.agent_journal_id.to_string()
|
|
|| r.helper_journal_id != w.helper_journal_id.to_string()
|
|
|| r.assignment_version != 1
|
|
|| r.checkpoint != cp
|
|
|| r.helper_checkpoint != hcp
|
|
|| !r.journal_complete
|
|
|| (i == 1 && r.previous_decision_id != profile::FIRST.to_string())
|
|
|| !d.valid_profile(d.issued_at)
|
|
|| d.decision_id != id
|
|
|| d.request_id != rid
|
|
|| d.request_hash != hash(raw)
|
|
|| (d.tenant_id, d.asset_id, d.agent_id) != (TENANT, ASSET, AGENT)
|
|
|| d.enrollment_generation != 1
|
|
|| d.assignment_version != 1
|
|
|| d.boot_id != profile::BOOT
|
|
|| d.connection_generation != 205
|
|
|| d.journal_id != w.agent_journal_id
|
|
|| d.helper_journal_id != Some(w.helper_journal_id)
|
|
|| d.checkpoint != cp
|
|
|| d.helper_checkpoint != hcp
|
|
|| d.key_id != w.verification_key_id
|
|
|| d.attempts.iter().any(|a| {
|
|
a.update_inflight.is_some()
|
|
|| ![
|
|
(profile::ECHO_JOB, profile::ECHO_ATTEMPT),
|
|
(profile::EXPIRED_JOB, profile::EXPIRED_ATTEMPT),
|
|
]
|
|
.contains(&(a.job_id, a.attempt_id))
|
|
})
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
Ok((r, d))
|
|
}
|
|
async fn validate(tx: &mut Transaction<'_, Postgres>, s: &crate::State, w: &Witness) -> Result<()> {
|
|
if !w.valid()
|
|
|| w.verification_key_id != s.job_signer.key_id
|
|
|| hex::encode(s.job_signer.public_key()?) != w.verification_public_key_hex
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
crate::agent_service::control_lock(tx, TENANT, AGENT).await?;
|
|
let dev=sqlx::query("SELECT asset_id,enrollment_generation,assignment_version,connection_generation,connection_boot_id,revoked_at,sequence_regressed,sync_recovery_required FROM agent_installations WHERE tenant_id=$1 AND agent_id=$2 FOR UPDATE").bind(TENANT).bind(AGENT).fetch_one(&mut **tx).await?;
|
|
if dev.get::<Uuid, _>("asset_id") != ASSET
|
|
|| dev.get::<i64, _>("enrollment_generation") != 1
|
|
|| dev.get::<i64, _>("assignment_version") != 1
|
|
|| dev.get::<i64, _>("connection_generation") != 205
|
|
|| dev.get::<Option<Uuid>, _>("connection_boot_id") != Some(profile::BOOT)
|
|
|| dev
|
|
.get::<Option<chrono::DateTime<Utc>>, _>("revoked_at")
|
|
.is_some()
|
|
|| dev.get::<bool, _>("sequence_regressed")
|
|
|| dev.get::<bool, _>("sync_recovery_required")
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
let state=sqlx::query("SELECT journal_id,checkpoint,decision_id,helper_journal_id,helper_checkpoint FROM agent_sync_state WHERE tenant_id=$1 AND agent_id=$2 FOR SHARE").bind(TENANT).bind(AGENT).fetch_one(&mut **tx).await?;
|
|
if state.get::<Uuid, _>("journal_id") != w.agent_journal_id
|
|
|| state.get::<i64, _>("checkpoint") != 10137
|
|
|| state.get::<Uuid, _>("decision_id") != profile::SECOND
|
|
|| state.get::<Option<Uuid>, _>("helper_journal_id") != Some(w.helper_journal_id)
|
|
|| state.get::<i64, _>("helper_checkpoint") != 135
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
for i in 0..2 {
|
|
let (r, d) = original(w, i)?;
|
|
verify(
|
|
s,
|
|
&w.original_responses[i].decision_bytes,
|
|
&w.original_responses[i].signature,
|
|
)?;
|
|
let row=sqlx::query("SELECT request_hash,journal_id,checkpoint,connection_generation,payload,signature FROM sync_checkpoints WHERE tenant_id=$1 AND agent_id=$2 AND decision_id=$3 AND request_id=$4").bind(TENANT).bind(AGENT).bind(d.decision_id).bind(d.request_id).fetch_one(&mut **tx).await?;
|
|
if row.get::<String, _>("request_hash") != hash(&r.encode_to_vec())
|
|
|| row.get::<Uuid, _>("journal_id") != w.agent_journal_id
|
|
|| row.get::<i64, _>("checkpoint")
|
|
!= i64::try_from(d.checkpoint).map_err(|_| mismatch())?
|
|
|| row.get::<i64, _>("connection_generation") != 205
|
|
|| row.get::<Vec<u8>, _>("payload") != w.original_responses[i].decision_bytes
|
|
|| row.get::<Vec<u8>, _>("signature") != w.original_responses[i].signature
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
}
|
|
validate_jobs(tx, s, w).await
|
|
}
|
|
async fn validate_jobs(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
s: &crate::State,
|
|
w: &Witness,
|
|
) -> Result<()> {
|
|
let jobs=sqlx::query("SELECT j.job_id,j.attempt_id,j.operation_id,j.asset_id,j.state,j.payload,j.payload_hash,j.result,a.authorization_id FROM jobs j JOIN authorizations a ON a.tenant_id=j.tenant_id AND a.operation_id=j.operation_id WHERE j.tenant_id=$1 AND j.agent_id=$2 ORDER BY j.job_id FOR SHARE OF j,a").bind(TENANT).bind(AGENT).fetch_all(&mut **tx).await?;
|
|
let total: i64 =
|
|
sqlx::query_scalar("SELECT count(*) FROM jobs WHERE tenant_id=$1 AND agent_id=$2")
|
|
.bind(TENANT)
|
|
.bind(AGENT)
|
|
.fetch_one(&mut **tx)
|
|
.await?;
|
|
if jobs.len() != 2 || total != 2 {
|
|
return Err(mismatch());
|
|
}
|
|
let echo = jobs
|
|
.iter()
|
|
.find(|r| r.get::<Uuid, _>("job_id") == profile::ECHO_JOB)
|
|
.ok_or_else(mismatch)?;
|
|
let expired = jobs
|
|
.iter()
|
|
.find(|r| r.get::<Uuid, _>("job_id") == profile::EXPIRED_JOB)
|
|
.ok_or_else(mismatch)?;
|
|
for (row, raw, id, attempt, state) in [
|
|
(
|
|
echo,
|
|
&w.echo_offer,
|
|
profile::ECHO_JOB,
|
|
profile::ECHO_ATTEMPT,
|
|
"succeeded",
|
|
),
|
|
(
|
|
expired,
|
|
&w.expired_offer,
|
|
profile::EXPIRED_JOB,
|
|
profile::EXPIRED_ATTEMPT,
|
|
"expired",
|
|
),
|
|
] {
|
|
let offer = JobOffer::decode(raw.as_slice()).map_err(|_| mismatch())?;
|
|
let j: JobPayload = serde_json::from_slice(&offer.payload_bytes)?;
|
|
if offer.encode_to_vec() != *raw
|
|
|| offer.job_id != id.to_string()
|
|
|| offer.attempt_id != attempt.to_string()
|
|
|| row.get::<Uuid, _>("attempt_id") != attempt
|
|
|| row.get::<Uuid, _>("asset_id") != ASSET
|
|
|| row.get::<String, _>("state") != state
|
|
|| row.get::<Vec<u8>, _>("payload") != offer.payload_bytes
|
|
|| row.get::<String, _>("payload_hash") != hash(&offer.payload_bytes)
|
|
|| offer.job_payload_hash != hash(&offer.payload_bytes)
|
|
|| offer.authorization_id != row.get::<Uuid, _>("authorization_id").to_string()
|
|
|| (j.tenant_id, j.asset_id, j.agent_id, j.job_id, j.attempt_id)
|
|
!= (TENANT, ASSET, AGENT, id, attempt)
|
|
|| j.enrollment_generation != 1
|
|
|| j.assignment_version != 1
|
|
|| j.execution_identity != "agent_service"
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
let plan_raw: Option<Vec<u8>> = sqlx::query_scalar("SELECT p.payload FROM authorizations a JOIN operation_plans p ON p.tenant_id=a.tenant_id AND p.plan_id=a.plan_id AND p.plan_hash=a.plan_hash WHERE a.tenant_id=$1 AND a.authorization_id=$2 AND a.operation_id=$3 AND a.plan_hash=$4 AND a.revoked_at IS NULL AND a.principal=$5 AND a.approval_origin='dev_test_browser_simulation' AND a.testsimulation")
|
|
.bind(TENANT).bind(row.get::<Uuid,_>("authorization_id")).bind(row.get::<Uuid,_>("operation_id")).bind(&offer.plan_hash).bind(TEST_PRINCIPAL).fetch_optional(&mut **tx).await?;
|
|
let plan_raw = plan_raw.ok_or_else(mismatch)?;
|
|
let plan: PlanPayload = serde_json::from_slice(&plan_raw)?;
|
|
if hash(&plan_raw) != offer.plan_hash
|
|
|| encode(&plan)? != plan_raw
|
|
|| plan.tenant_id != TENANT
|
|
|| plan.jobs.len() != 1
|
|
|| plan.jobs[0].payload != j
|
|
|| plan.jobs[0].job_payload_hash != offer.job_payload_hash
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
}
|
|
let grant_count:i64=sqlx::query_scalar("SELECT count(*) FROM execution_permits p JOIN jobs j ON j.tenant_id=p.tenant_id AND j.job_id=p.job_id WHERE j.tenant_id=$1 AND j.agent_id=$2").bind(TENANT).bind(AGENT).fetch_one(&mut **tx).await?;
|
|
if grant_count != 1 {
|
|
return Err(mismatch());
|
|
}
|
|
let grant=sqlx::query("SELECT grant_id,payload,signature FROM execution_permits WHERE tenant_id=$1 AND job_id=$2 AND attempt_id=$3").bind(TENANT).bind(profile::ECHO_JOB).bind(profile::ECHO_ATTEMPT).fetch_one(&mut **tx).await?;
|
|
let graw: Vec<u8> = grant.get("payload");
|
|
let gsig: Vec<u8> = grant.get("signature");
|
|
verify(s, &graw, &gsig)?;
|
|
let g: ExecutionPermitPayload = serde_json::from_slice(&graw)?;
|
|
let start: Value = serde_json::from_slice(&w.echo_start)?;
|
|
let gbytes: Vec<u8> = serde_json::from_value(start["request"]["permit_bytes"].clone())?;
|
|
let gsignature: Vec<u8> = serde_json::from_value(start["request"]["signature"].clone())?;
|
|
let jbytes: Vec<u8> = serde_json::from_value(start["request"]["job_bytes"].clone())?;
|
|
let mut outcome = echo
|
|
.get::<Option<Value>, _>("result")
|
|
.ok_or_else(mismatch)?;
|
|
if outcome["grant_id"] != g.grant_id.to_string() {
|
|
return Err(mismatch());
|
|
}
|
|
outcome
|
|
.as_object_mut()
|
|
.ok_or_else(mismatch)?
|
|
.remove("grant_id");
|
|
outcome["state"] = json!("succeeded");
|
|
let native: Value = serde_json::from_slice(&w.echo_outcome)?;
|
|
let result: Value = serde_json::from_slice(&w.echo_result)?;
|
|
if gbytes != graw
|
|
|| gsignature != gsig
|
|
|| jbytes != echo.get::<Vec<u8>, _>("payload")
|
|
|| native != result
|
|
|| native != outcome
|
|
|| !successful_outcome(&outcome)
|
|
|| g.grant_id != grant.get::<Uuid, _>("grant_id")
|
|
|| g.key_id != w.verification_key_id
|
|
|| (g.tenant_id, g.asset_id, g.agent_id, g.job_id, g.attempt_id)
|
|
!= (
|
|
TENANT,
|
|
ASSET,
|
|
AGENT,
|
|
profile::ECHO_JOB,
|
|
profile::ECHO_ATTEMPT,
|
|
)
|
|
|| g.enrollment_generation != 1
|
|
|| g.assignment_version != 1
|
|
|| g.execution_identity != "agent_service"
|
|
|| g.action != "run_script"
|
|
|| g.agent_update_plan_id.is_some()
|
|
|| g.job_payload_hash != echo.get::<String, _>("payload_hash")
|
|
|| g.authorization_id != echo.get::<Uuid, _>("authorization_id")
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
let completion: SignedReceipt = serde_json::from_slice(&w.echo_completion_receipt)?;
|
|
verify(s, &completion.payload, &completion.signature)?;
|
|
let c: Completion = serde_json::from_slice(&completion.payload)?;
|
|
if !c.valid(c.issued_at)
|
|
|| c.key_id != w.verification_key_id
|
|
|| c.witness.scope.job_id != profile::ECHO_JOB
|
|
|| c.witness.scope.attempt_id != profile::ECHO_ATTEMPT
|
|
|| c.witness.boot_id != profile::BOOT
|
|
|| c.witness.grant_id != g.grant_id
|
|
|| c.witness.grant_payload_sha256 != hash(&graw)
|
|
|| c.witness.grant_signature_sha256 != hash(&gsig)
|
|
|| c.witness.outcome_sha256 != inventory_sha256(&outcome)?
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
let retained =
|
|
sqlx::query("SELECT payload,signature FROM windows_echo_completions WHERE tenant_id=$1")
|
|
.bind(TENANT)
|
|
.fetch_one(&mut **tx)
|
|
.await?;
|
|
if retained.get::<Vec<u8>, _>("payload") != completion.payload
|
|
|| retained.get::<Vec<u8>, _>("signature") != completion.signature
|
|
{
|
|
return Err(mismatch());
|
|
}
|
|
Ok(())
|
|
}
|
|
async fn stored(tx: &mut Transaction<'_, Postgres>) -> Result<Option<(Uuid, Witness)>> {
|
|
let row = sqlx::query(
|
|
"SELECT request_id,witness FROM windows_sync_alignment_reviews WHERE tenant_id=$1",
|
|
)
|
|
.bind(TENANT)
|
|
.fetch_optional(&mut **tx)
|
|
.await?;
|
|
row.map(|r| {
|
|
Ok((
|
|
r.get("request_id"),
|
|
serde_json::from_value(r.get("witness"))?,
|
|
))
|
|
})
|
|
.transpose()
|
|
}
|
|
async fn view(tx: &mut Transaction<'_, Postgres>, id: Uuid, w: &Witness) -> Result<Value> {
|
|
let row=sqlx::query("SELECT payload FROM windows_sync_alignment_receipts WHERE tenant_id=$1 AND request_id=$2 ORDER BY issued_at DESC,receipt_id DESC LIMIT 1").bind(TENANT).bind(id).fetch_optional(&mut **tx).await?;
|
|
let receipt = row
|
|
.map(|r| serde_json::from_slice::<Value>(&r.get::<Vec<u8>, _>("payload")))
|
|
.transpose()?;
|
|
Ok(json!({"request_id":id,"witness":w,"witness_sha256":w.digest(),"receipt":receipt}))
|
|
}
|
|
async fn read(
|
|
State(s): State<crate::State>,
|
|
Path(t): Path<Uuid>,
|
|
h: HeaderMap,
|
|
) -> Result<Json<Value>> {
|
|
let p = access(&s, &h, t, false).await?;
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
|
crate::permissions::require_principal(&mut tx, &p, "inventory:read").await?;
|
|
let result = if let Some((id, w)) = stored(&mut tx).await? {
|
|
validate(&mut tx, &s, &w).await?;
|
|
view(&mut tx, id, &w).await?
|
|
} else {
|
|
json!({"request_id":null})
|
|
};
|
|
tx.commit().await?;
|
|
Ok(Json(result))
|
|
}
|
|
async fn submit(
|
|
State(s): State<crate::State>,
|
|
Path(t): Path<Uuid>,
|
|
h: HeaderMap,
|
|
Json(w): Json<Witness>,
|
|
) -> Result<Json<Value>> {
|
|
let p = access(&s, &h, t, true).await?;
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
|
crate::permissions::require_principal(&mut tx, &p, "inventory:read").await?;
|
|
validate(&mut tx, &s, &w).await?;
|
|
let id = if let Some((id, old)) = stored(&mut tx).await? {
|
|
if old != w {
|
|
return Err(mismatch());
|
|
}
|
|
id
|
|
} else {
|
|
let id = Uuid::new_v4();
|
|
let digest = w.digest().ok_or_else(mismatch)?;
|
|
sqlx::query("INSERT INTO windows_sync_alignment_reviews(tenant_id,request_id,agent_id,witness,witness_sha256,submitted_by) VALUES($1,$2,$3,$4,$5,$6)").bind(t).bind(id).bind(AGENT).bind(serde_json::to_value(&w)?).bind(&digest).bind(&p.name).execute(&mut *tx).await?;
|
|
sqlx::query("INSERT INTO audit_events(tenant_id,event_id,principal,event_type,data) VALUES($1,$2,$3,'windows_sync_alignment_submitted',$4)").bind(t).bind(id).bind(&p.name).bind(json!({"witness_sha256":digest,"connection_generation":205,"ready":false,"helper_ready":false,"unlock":false})).execute(&mut *tx).await?;
|
|
id
|
|
};
|
|
let result = view(&mut tx, id, &w).await?;
|
|
tx.commit().await?;
|
|
Ok(Json(result))
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Confirmation {
|
|
reviewed: bool,
|
|
witness_sha256: String,
|
|
}
|
|
async fn confirm(
|
|
State(s): State<crate::State>,
|
|
Path((t, id)): Path<(Uuid, Uuid)>,
|
|
h: HeaderMap,
|
|
Json(input): Json<Confirmation>,
|
|
) -> Result<Json<Value>> {
|
|
let p = access(&s, &h, t, true).await?;
|
|
let session = browser(&p)?;
|
|
if !input.reviewed {
|
|
return Err(Error(StatusCode::BAD_REQUEST, "REVIEW_REQUIRED"));
|
|
}
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
|
let allowed: bool = sqlx::query_scalar("SELECT lock_windows_echo_probe_reviewer()")
|
|
.fetch_one(&mut *tx)
|
|
.await?;
|
|
if !allowed {
|
|
return Err(Error(StatusCode::FORBIDDEN, "DEV_TEST_WEBUI_REQUIRED"));
|
|
}
|
|
let (actual, w) = stored(&mut tx)
|
|
.await?
|
|
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
|
if actual != id || w.digest().as_deref() != Some(input.witness_sha256.as_str()) {
|
|
return Err(mismatch());
|
|
}
|
|
validate(&mut tx, &s, &w).await?;
|
|
let now = Utc::now().trunc_subsecs(6);
|
|
let r = Receipt {
|
|
domain: profile::DOMAIN.into(),
|
|
schema_version: 1,
|
|
receipt_id: Uuid::new_v4(),
|
|
request_id: id,
|
|
witness: w.clone(),
|
|
witness_sha256: input.witness_sha256,
|
|
key_id: s.job_signer.key_id.clone(),
|
|
issued_at: now,
|
|
valid_until: now + Duration::minutes(5),
|
|
confirmed_by: p.name.clone(),
|
|
approval_origin: p.approval_origin().into(),
|
|
testsimulation: true,
|
|
ready: false,
|
|
helper_ready: false,
|
|
unlock: false,
|
|
};
|
|
if !r.valid(now) {
|
|
return Err(mismatch());
|
|
}
|
|
let payload = serde_json::to_vec(&r)?;
|
|
let signature = s.job_signer.sign(&payload)?;
|
|
sqlx::query("INSERT INTO windows_sync_alignment_receipts(tenant_id,request_id,receipt_id,witness_sha256,payload,signature,confirmed_by,session_hash,issued_at,valid_until) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)").bind(t).bind(id).bind(r.receipt_id).bind(&r.witness_sha256).bind(&payload).bind(signature).bind(&p.name).bind(session).bind(now).bind(r.valid_until).execute(&mut *tx).await?;
|
|
sqlx::query("INSERT INTO audit_events(tenant_id,event_id,principal,event_type,data) VALUES($1,$2,$3,'windows_sync_admin_closed_alignment_confirmed',$4)").bind(t).bind(r.receipt_id).bind(&p.name).bind(json!({"request_id":id,"witness_sha256":r.witness_sha256,"receipt_sha256":hash(&payload),"approval_origin":r.approval_origin,"testsimulation":true,"ready":false,"helper_ready":false,"unlock":false,"connection_generation":205})).execute(&mut *tx).await?;
|
|
let result = view(&mut tx, id, &w).await?;
|
|
tx.commit().await?;
|
|
Ok(Json(result))
|
|
}
|
|
async fn download(
|
|
State(s): State<crate::State>,
|
|
Path((t, id)): Path<(Uuid, Uuid)>,
|
|
h: HeaderMap,
|
|
) -> Result<Json<SignedReceipt>> {
|
|
let p = access(&s, &h, t, false).await?;
|
|
browser(&p)?;
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
|
crate::permissions::require_principal(&mut tx, &p, "plans:approve").await?;
|
|
let (actual, w) = stored(&mut tx)
|
|
.await?
|
|
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
|
if actual != id {
|
|
return Err(mismatch());
|
|
}
|
|
validate(&mut tx, &s, &w).await?;
|
|
let row=sqlx::query("SELECT payload,signature FROM windows_sync_alignment_receipts WHERE tenant_id=$1 AND request_id=$2 ORDER BY issued_at DESC,receipt_id DESC LIMIT 1").bind(t).bind(id).fetch_optional(&mut *tx).await?.ok_or(Error(StatusCode::NOT_FOUND,"NOT_FOUND"))?;
|
|
let signed = SignedReceipt {
|
|
payload: row.get("payload"),
|
|
signature: row.get("signature"),
|
|
};
|
|
verify(&s, &signed.payload, &signed.signature)?;
|
|
let r: Receipt = serde_json::from_slice(&signed.payload)?;
|
|
if !r.valid(Utc::now()) || r.witness != w {
|
|
return Err(Error(
|
|
StatusCode::CONFLICT,
|
|
"WINDOWS_SYNC_ALIGNMENT_EXPIRED",
|
|
));
|
|
}
|
|
tx.commit().await?;
|
|
Ok(Json(signed))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn administrative_confirmation_requires_actual_scoped_browser_session() {
|
|
let p = Principal {
|
|
name: TEST_PRINCIPAL.into(),
|
|
tenant: TENANT,
|
|
operator: true,
|
|
csrf: Some("csrf".into()),
|
|
session_hash: Some("ab".repeat(32)),
|
|
scopes: None,
|
|
testsimulation: true,
|
|
identity: None,
|
|
tenants: vec![TENANT],
|
|
};
|
|
assert!(browser(&p).is_ok());
|
|
for mutation in 0..5 {
|
|
let mut bad = p.clone();
|
|
match mutation {
|
|
0 => bad.session_hash = None,
|
|
1 => bad.csrf = None,
|
|
2 => bad.scopes = Some(Default::default()),
|
|
3 => bad.testsimulation = false,
|
|
_ => bad.operator = false,
|
|
};
|
|
assert_eq!(browser(&bad).unwrap_err().0, StatusCode::FORBIDDEN);
|
|
}
|
|
}
|
|
}
|