723 lines
30 KiB
Rust
723 lines
30 KiB
Rust
//! Individually reviewed Windows control-history repair. No job or ordinary
|
|
//! reconciliation decision can be issued through this development-only route.
|
|
use crate::{auth::Principal, jobs::Error};
|
|
use axum::{
|
|
Json, Router,
|
|
extract::{Path, State},
|
|
http::{HeaderMap, StatusCode},
|
|
routing::{get, post},
|
|
};
|
|
use chrono::{DateTime, Duration, Utc};
|
|
use openssl::{
|
|
pkey::{Id, PKey},
|
|
sign::Verifier,
|
|
};
|
|
use rmm_domain::{
|
|
checkpoint_repair::{SignedReceipt, inventory_sha256},
|
|
jobs::hash,
|
|
reconciliation::ReconcileDecisionPayload,
|
|
windows_sync_history::{RECEIPT_DOMAIN, Receipt, case_witness},
|
|
};
|
|
use serde::Deserialize;
|
|
use serde_json::{Value, json};
|
|
use sqlx::{Postgres, Row, Transaction};
|
|
use uuid::Uuid;
|
|
|
|
type Result<T> = std::result::Result<T, Error>;
|
|
const CASE_AGENT: Uuid = Uuid::from_u128(0x63ea15b20643403db5b4cba1288dbea3);
|
|
const CASE_ASSET: Uuid = Uuid::from_u128(0xa176520e8a914e93a78b8e7620f430e0);
|
|
const CASE_TENANT: Uuid = Uuid::from_u128(0x11111111111141118111111111111111);
|
|
pub(super) const FIRST: Uuid = Uuid::from_u128(0xb6a3101f68ad41c8be4e4c7c2bc963d6);
|
|
pub(super) const DENIED: Uuid = Uuid::from_u128(0x0633a4de7e524e5da2a159fb134817fe);
|
|
const CERTIFICATE: &str = "c55aa8214cb8d60382fe453bddd56c6e46fa79b61ccefda502e001be56bb5075";
|
|
|
|
fn changed() -> Error {
|
|
Error(StatusCode::CONFLICT, "WINDOWS_RECOVERY_WITNESS_CHANGED")
|
|
}
|
|
fn missing() -> Error {
|
|
Error(StatusCode::NOT_FOUND, "NOT_FOUND")
|
|
}
|
|
fn forbidden() -> Error {
|
|
Error(StatusCode::FORBIDDEN, "REAL_HUMAN_WEBUI_REQUIRED")
|
|
}
|
|
pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
|
|
r.route(
|
|
"/api/v1/tenants/{tenant}/windows-checkpoint-recovery",
|
|
get(read).post(submit),
|
|
)
|
|
.route(
|
|
"/api/v1/tenants/{tenant}/windows-checkpoint-recovery/{request}/confirm",
|
|
post(confirm),
|
|
)
|
|
.route(
|
|
"/api/v1/tenants/{tenant}/windows-checkpoint-recovery/{request}/receipt",
|
|
get(download),
|
|
)
|
|
}
|
|
pub(super) async fn access(
|
|
s: &crate::State,
|
|
h: &HeaderMap,
|
|
t: Uuid,
|
|
write: bool,
|
|
) -> Result<Principal> {
|
|
let p = s
|
|
.auth
|
|
.principal(s, h, write)
|
|
.await
|
|
.map_err(|status| Error(status, crate::auth::error_code(status)))?;
|
|
crate::jobs::scope(&p, t, write)?;
|
|
if t != CASE_TENANT {
|
|
return Err(missing());
|
|
}
|
|
Ok(p)
|
|
}
|
|
fn real_browser(p: &Principal) -> Result<&str> {
|
|
if p.testsimulation
|
|
|| p.scopes.is_some()
|
|
|| p.identity.is_none()
|
|
|| p.csrf.is_none()
|
|
|| !p.operator
|
|
{
|
|
return Err(forbidden());
|
|
}
|
|
p.session_hash.as_deref().ok_or_else(forbidden)
|
|
}
|
|
async fn reviewer(tx: &mut Transaction<'_, Postgres>, p: &Principal) -> Result<()> {
|
|
real_browser(p)?;
|
|
// Hold the current human membership through the commit.
|
|
let identity = p.identity.as_ref().ok_or_else(forbidden)?;
|
|
let allowed: bool = sqlx::query_scalar("SELECT lock_windows_checkpoint_reviewer($1,$2,$3)")
|
|
.bind(&p.name)
|
|
.bind(&identity.issuer)
|
|
.bind(&identity.subject)
|
|
.fetch_one(&mut **tx)
|
|
.await?;
|
|
if !allowed {
|
|
return Err(forbidden());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn view(tx: &mut Transaction<'_, Postgres>) -> Result<Value> {
|
|
let row=sqlx::query_scalar::<_,Value>(
|
|
"SELECT jsonb_build_object('request_id',r.request_id,'tenant_id',r.tenant_id,'asset_id',r.asset_id,
|
|
'agent_id',r.agent_id,'witness_sha256',r.witness_sha256,'witness',r.witness,
|
|
'status',CASE WHEN c.receipt_id IS NULL THEN 'pending_review' ELSE 'confirmed' END,
|
|
'confirmed_by',c.confirmed_by,'confirmed_at',c.issued_at,'receipt_id',c.receipt_id,'receipt_valid_until',c.valid_until)
|
|
FROM windows_checkpoint_reviews r LEFT JOIN LATERAL
|
|
(SELECT * FROM windows_checkpoint_review_receipts WHERE tenant_id=r.tenant_id AND request_id=r.request_id
|
|
ORDER BY issued_at DESC,receipt_id DESC LIMIT 1) c ON true
|
|
WHERE r.tenant_id=$1 AND r.agent_id=$2").bind(CASE_TENANT).bind(CASE_AGENT).fetch_optional(&mut **tx).await?;
|
|
Ok(json!({"request":row}))
|
|
}
|
|
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 value = view(&mut tx).await?;
|
|
tx.commit().await?;
|
|
Ok(Json(value))
|
|
}
|
|
fn verify_original(
|
|
s: &crate::State,
|
|
row: &sqlx::postgres::PgRow,
|
|
expected: &Value,
|
|
w: &Value,
|
|
) -> Result<()> {
|
|
let raw: Vec<u8> = row.try_get("payload")?;
|
|
let signature: Vec<u8> = row.try_get("signature")?;
|
|
if raw.len() > 32768
|
|
|| signature.len() != 64
|
|
|| hash(&raw) != expected["payload_sha256"]
|
|
|| hash(&signature) != expected["signature_sha256"]
|
|
{
|
|
return Err(changed());
|
|
}
|
|
let key = PKey::public_key_from_raw_bytes(&s.job_signer.public_key()?, Id::ED25519)
|
|
.map_err(|_| changed())?;
|
|
let mut verifier = Verifier::new_without_digest(&key).map_err(|_| changed())?;
|
|
if !verifier
|
|
.verify_oneshot(&signature, &raw)
|
|
.map_err(|_| changed())?
|
|
{
|
|
return Err(changed());
|
|
}
|
|
let d: ReconcileDecisionPayload = serde_json::from_slice(&raw)?;
|
|
let ready = d.checkpoint == 6755;
|
|
if !d.valid_profile(d.issued_at)
|
|
|| d.key_id != s.job_signer.key_id
|
|
|| w["verification_key_id"] != d.key_id
|
|
|| d.tenant_id != CASE_TENANT
|
|
|| d.asset_id != CASE_ASSET
|
|
|| d.agent_id != CASE_AGENT
|
|
|| d.enrollment_generation != 1
|
|
|| d.assignment_version != 1
|
|
|| w["boot_id"] != d.boot_id.to_string()
|
|
|| d.connection_generation != 150
|
|
|| row.get::<i64, _>("connection_generation") != 150
|
|
|| expected["request_id"] != d.request_id.to_string()
|
|
|| row.get::<Uuid, _>("request_id") != d.request_id
|
|
|| expected["decision_id"] != d.decision_id.to_string()
|
|
|| row.get::<Uuid, _>("decision_id") != d.decision_id
|
|
|| expected["request_sha256"] != d.request_hash
|
|
|| row.get::<String, _>("request_hash") != d.request_hash
|
|
|| expected["checkpoint"] != d.checkpoint
|
|
|| row.get::<i64, _>("checkpoint") != d.checkpoint as i64
|
|
|| w["agent_journal_id"] != d.journal_id.to_string()
|
|
|| row.get::<Uuid, _>("journal_id") != d.journal_id
|
|
|| d.helper_journal_id.map(|id| Value::String(id.to_string()))
|
|
!= Some(w["helper_journal_id"].clone())
|
|
|| d.helper_checkpoint != 1
|
|
|| d.ready != ready
|
|
|| d.helper_ready != ready
|
|
|| expected["reason"] != d.reason
|
|
|| !d.attempts.is_empty()
|
|
{
|
|
return Err(changed());
|
|
}
|
|
Ok(())
|
|
}
|
|
async fn verify_case(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
s: &crate::State,
|
|
w: &Value,
|
|
promoted: bool,
|
|
) -> Result<()> {
|
|
verify_case_state(tx, s, w, promoted, false).await
|
|
}
|
|
pub(super) async fn verify_probe_case(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
s: &crate::State,
|
|
w: &Value,
|
|
promoted: bool,
|
|
) -> Result<()> {
|
|
verify_case_state(tx, s, w, promoted, true).await
|
|
}
|
|
async fn verify_case_state(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
s: &crate::State,
|
|
w: &Value,
|
|
promoted: bool,
|
|
probe: bool,
|
|
) -> Result<()> {
|
|
crate::agent_service::control_lock(tx, CASE_TENANT, CASE_AGENT).await?;
|
|
let dev=sqlx::query("SELECT asset_id,certificate_sha256,enrollment_generation,assignment_version,connection_generation,connection_boot_id,capabilities,revoked_at IS NULL AS active,restore_blocked,sequence_regressed,sync_recovery_required,sync_required
|
|
FROM agent_installations WHERE tenant_id=$1 AND agent_id=$2 FOR UPDATE")
|
|
.bind(CASE_TENANT).bind(CASE_AGENT).fetch_optional(&mut **tx).await?.ok_or_else(changed)?;
|
|
let mut caps: Vec<String> = serde_json::from_value(dev.get("capabilities"))?;
|
|
caps.sort();
|
|
if dev.get::<Uuid, _>("asset_id") != CASE_ASSET
|
|
|| dev.get::<String, _>("certificate_sha256") != CERTIFICATE
|
|
|| dev.get::<i64, _>("enrollment_generation") != 1
|
|
|| dev.get::<i64, _>("assignment_version") != 1
|
|
|| dev.get::<i64, _>("connection_generation") <= 150
|
|
|| dev
|
|
.get::<Option<Uuid>, _>("connection_boot_id")
|
|
.map(|v| Value::String(v.to_string()))
|
|
!= Some(w["boot_id"].clone())
|
|
|| !dev.get::<bool, _>("active")
|
|
|| !dev.get::<bool, _>("restore_blocked")
|
|
|| dev.get::<bool, _>("sequence_regressed")
|
|
|| dev.get::<bool, _>("sync_recovery_required")
|
|
|| if probe {
|
|
!dev.get::<bool, _>("sync_required")
|
|
|| ![
|
|
"windows.base/1",
|
|
"windows.echo-probe/1",
|
|
"run_script",
|
|
"rmm.sync.v1",
|
|
"script.powershell/1",
|
|
"script.powershell.process_remote_signed/1",
|
|
]
|
|
.iter()
|
|
.all(|k| caps.iter().any(|c| c == k))
|
|
|| caps.iter().any(|k| {
|
|
![
|
|
"windows.base/1",
|
|
"windows.echo-probe/1",
|
|
"run_script",
|
|
"rmm.sync.v1",
|
|
"script.powershell/1",
|
|
"script.powershell.process_remote_signed/1",
|
|
"heartbeat/1",
|
|
"agent.update/1",
|
|
"refresh_inventory",
|
|
]
|
|
.contains(&k.as_str())
|
|
})
|
|
} else {
|
|
dev.get::<bool, _>("sync_required")
|
|
|| caps != ["agent.update/1", "heartbeat/1", "windows.base/1"]
|
|
}
|
|
{
|
|
return Err(changed());
|
|
}
|
|
// The device row lock also excludes a concurrent plan authorization targeting it.
|
|
let jobs: bool =
|
|
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM jobs WHERE tenant_id=$1 AND agent_id=$2)")
|
|
.bind(CASE_TENANT)
|
|
.bind(CASE_AGENT)
|
|
.fetch_one(&mut **tx)
|
|
.await?;
|
|
// Outbox and permit rows have non-null foreign keys to the scoped jobs.
|
|
if jobs {
|
|
return Err(changed());
|
|
}
|
|
let history=sqlx::query("SELECT request_id,decision_id,request_hash,journal_id,checkpoint,connection_generation,payload,signature
|
|
FROM sync_checkpoints WHERE tenant_id=$1 AND agent_id=$2 ORDER BY checkpoint LIMIT 3")
|
|
.bind(CASE_TENANT).bind(CASE_AGENT).fetch_all(&mut **tx).await?;
|
|
if history.len() != 2 {
|
|
return Err(changed());
|
|
}
|
|
for (index, row) in history.iter().enumerate() {
|
|
verify_original(s, row, &w["decisions"][index], w)?;
|
|
}
|
|
let head=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 UPDATE")
|
|
.bind(CASE_TENANT).bind(CASE_AGENT).fetch_optional(&mut **tx).await?.ok_or_else(changed)?;
|
|
let (cp, id) = if promoted {
|
|
(6756, DENIED)
|
|
} else {
|
|
(6755, FIRST)
|
|
};
|
|
if head.get::<i64, _>("checkpoint") != cp
|
|
|| head.get::<Uuid, _>("decision_id") != id
|
|
|| w["agent_journal_id"] != head.get::<Uuid, _>("journal_id").to_string()
|
|
|| head.get::<i64, _>("helper_checkpoint") != 1
|
|
|| head
|
|
.get::<Option<Uuid>, _>("helper_journal_id")
|
|
.map(|id| Value::String(id.to_string()))
|
|
!= Some(w["helper_journal_id"].clone())
|
|
{
|
|
return Err(changed());
|
|
}
|
|
Ok(())
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Empty {}
|
|
async fn submit(
|
|
State(s): State<crate::State>,
|
|
Path(t): Path<Uuid>,
|
|
h: HeaderMap,
|
|
Json(_): Json<Empty>,
|
|
) -> 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, "operations:submit").await?;
|
|
crate::agent_service::control_lock(&mut tx, t, CASE_AGENT).await?;
|
|
let mut value = view(&mut tx).await?;
|
|
if value["request"].is_null() {
|
|
let w = case_witness();
|
|
verify_case(&mut tx, &s, &w, false).await?;
|
|
let digest = inventory_sha256(&w)?;
|
|
let request_id = Uuid::new_v4();
|
|
sqlx::query("INSERT INTO windows_checkpoint_reviews(tenant_id,request_id,agent_id,asset_id,witness,witness_sha256,requested_by) VALUES($1,$2,$3,$4,$5,$6,$7)")
|
|
.bind(t).bind(request_id).bind(CASE_AGENT).bind(CASE_ASSET).bind(&w).bind(&digest).bind(&p.name).execute(&mut *tx).await?;
|
|
value = view(&mut tx).await?;
|
|
}
|
|
tx.commit().await?;
|
|
Ok(Json(value))
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Confirmation {
|
|
witness_sha256: String,
|
|
reviewed: bool,
|
|
previous_receipt_id: Option<Uuid>,
|
|
}
|
|
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?;
|
|
confirm_review(&s, &p, id, input, &case_witness()).await
|
|
}
|
|
async fn confirm_review(
|
|
s: &crate::State,
|
|
p: &Principal,
|
|
id: Uuid,
|
|
input: Confirmation,
|
|
expected: &Value,
|
|
) -> Result<Json<Value>> {
|
|
let t = p.tenant;
|
|
let session = real_browser(p)?;
|
|
if !input.reviewed {
|
|
return Err(Error(StatusCode::BAD_REQUEST, "REVIEW_REQUIRED"));
|
|
}
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
|
reviewer(&mut tx, p).await?;
|
|
crate::agent_service::control_lock(&mut tx, t, CASE_AGENT).await?;
|
|
let row=sqlx::query("SELECT witness,witness_sha256 FROM windows_checkpoint_reviews WHERE tenant_id=$1 AND request_id=$2 AND agent_id=$3")
|
|
.bind(t).bind(id).bind(CASE_AGENT).fetch_optional(&mut *tx).await?.ok_or_else(missing)?;
|
|
let w: Value = row.get("witness");
|
|
let digest = inventory_sha256(&w)?;
|
|
if &w != expected
|
|
|| input.witness_sha256 != digest
|
|
|| row.get::<String, _>("witness_sha256") != digest
|
|
{
|
|
return Err(changed());
|
|
}
|
|
let last=sqlx::query("SELECT receipt_id,valid_until FROM windows_checkpoint_review_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?;
|
|
verify_case(&mut tx, s, &w, last.is_some()).await?;
|
|
if let Some(ref r) = last {
|
|
if r.get::<DateTime<Utc>, _>("valid_until") > Utc::now() {
|
|
let value = view(&mut tx).await?;
|
|
tx.commit().await?;
|
|
return Ok(Json(value));
|
|
}
|
|
if input.previous_receipt_id != Some(r.get("receipt_id")) {
|
|
return Err(Error(
|
|
StatusCode::CONFLICT,
|
|
"EXPIRED_RECEIPT_REVIEW_REQUIRED",
|
|
));
|
|
}
|
|
} else if input.previous_receipt_id.is_some() {
|
|
return Err(changed());
|
|
}
|
|
|
|
let now = Utc::now();
|
|
let receipt = Receipt {
|
|
domain: RECEIPT_DOMAIN.into(),
|
|
schema_version: 1,
|
|
request_id: id,
|
|
receipt_id: Uuid::new_v4(),
|
|
witness_sha256: digest,
|
|
witness: w,
|
|
key_id: s.job_signer.key_id.clone(),
|
|
issued_at: now,
|
|
valid_until: now + Duration::minutes(10),
|
|
confirmed_by: p.name.clone(),
|
|
approval_origin: "human_webui".into(),
|
|
testsimulation: false,
|
|
ready: false,
|
|
helper_ready: false,
|
|
unlock: false,
|
|
};
|
|
let payload = serde_json::to_vec(&receipt)?;
|
|
let signature = s.job_signer.sign(&payload)?;
|
|
if last.is_none() && sqlx::query("UPDATE agent_sync_state SET checkpoint=6756, decision_id=$4 WHERE tenant_id=$1 AND agent_id=$2 AND checkpoint=6755 AND decision_id=$3 AND helper_checkpoint=1")
|
|
.bind(t).bind(CASE_AGENT).bind(FIRST).bind(DENIED).execute(&mut *tx).await?.rows_affected()!=1 {return Err(changed());}
|
|
sqlx::query("INSERT INTO windows_checkpoint_review_receipts(tenant_id,request_id,receipt_id,confirmed_by,session_hash,issued_at,valid_until,payload,signature) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)")
|
|
.bind(t).bind(id).bind(receipt.receipt_id).bind(&p.name).bind(session).bind(now).bind(receipt.valid_until).bind(&payload).bind(signature).execute(&mut *tx).await?;
|
|
sqlx::query("INSERT INTO audit_events(tenant_id,event_id,principal,event_type,data) VALUES($1,$2,$3,'windows_checkpoint_human_repair',$4)")
|
|
.bind(t).bind(receipt.receipt_id).bind(&p.name)
|
|
.bind(json!({"request_id":id,"agent_id":CASE_AGENT,"witness_sha256":receipt.witness_sha256,"receipt_sha256":hash(&payload),"approval_origin":"human_webui","testsimulation":false,"ready":false,"helper_ready":false,"unlock":false}))
|
|
.execute(&mut *tx).await?;
|
|
let value = view(&mut tx).await?;
|
|
tx.commit().await?;
|
|
Ok(Json(value))
|
|
}
|
|
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?;
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
|
reviewer(&mut tx, &p).await?;
|
|
verify_case(&mut tx, &s, &case_witness(), true).await?;
|
|
let row=sqlx::query("SELECT receipt_id,confirmed_by,payload,signature FROM windows_checkpoint_review_receipts WHERE tenant_id=$1 AND request_id=$2 AND valid_until>now() ORDER BY issued_at DESC,receipt_id DESC LIMIT 1")
|
|
.bind(t).bind(id).fetch_optional(&mut *tx).await?.ok_or_else(missing)?;
|
|
let signed = SignedReceipt {
|
|
payload: row.get("payload"),
|
|
signature: row.get("signature"),
|
|
};
|
|
let receipt: Receipt = serde_json::from_slice(&signed.payload)?;
|
|
let key = PKey::public_key_from_raw_bytes(&s.job_signer.public_key()?, Id::ED25519)
|
|
.map_err(|_| changed())?;
|
|
if !Verifier::new_without_digest(&key)
|
|
.map_err(|_| changed())?
|
|
.verify_oneshot(&signed.signature, &signed.payload)
|
|
.map_err(|_| changed())?
|
|
|| !receipt.matches_human_review(Utc::now())
|
|
|| receipt.request_id != id
|
|
|| receipt.receipt_id != row.get::<Uuid, _>("receipt_id")
|
|
|| receipt.confirmed_by != row.get::<String, _>("confirmed_by")
|
|
{
|
|
return Err(changed());
|
|
}
|
|
tx.commit().await?;
|
|
Ok(Json(signed))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::job_signer::JobSigner;
|
|
use std::{collections::BTreeSet, sync::Arc};
|
|
|
|
fn operator() -> Principal {
|
|
Principal {
|
|
tenant: CASE_TENANT,
|
|
name: format!("oidc:{}", "e".repeat(64)),
|
|
operator: true,
|
|
session_hash: Some("a".repeat(64)),
|
|
csrf: Some("test-csrf".into()),
|
|
scopes: None,
|
|
testsimulation: false,
|
|
identity: Some(rmm_oidc_auth::Identity {
|
|
issuer: "https://human-review.example.test".into(),
|
|
subject: "synthetic-operator".into(),
|
|
}),
|
|
tenants: vec![CASE_TENANT],
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn simulated_sessions_and_bearers_have_no_confirmation_authority() {
|
|
let p = operator();
|
|
assert!(real_browser(&p).is_ok());
|
|
let mut simulated = p.clone();
|
|
simulated.testsimulation = true;
|
|
assert!(real_browser(&simulated).is_err());
|
|
let mut bearer = p.clone();
|
|
bearer.scopes = Some(BTreeSet::from(["recovery:confirm".into()]));
|
|
assert!(real_browser(&bearer).is_err());
|
|
for field in ["identity", "session", "csrf"] {
|
|
let mut p = p.clone();
|
|
match field {
|
|
"identity" => p.identity = None,
|
|
"session" => p.session_hash = None,
|
|
_ => p.csrf = None,
|
|
}
|
|
assert!(real_browser(&p).is_err());
|
|
}
|
|
}
|
|
|
|
async fn fixture(
|
|
admin_var: &str,
|
|
app_var: &str,
|
|
) -> (
|
|
sqlx::PgPool,
|
|
crate::State,
|
|
Principal,
|
|
Value,
|
|
Uuid,
|
|
std::path::PathBuf,
|
|
) {
|
|
let admin = sqlx::PgPool::connect(&std::env::var(admin_var).unwrap())
|
|
.await
|
|
.unwrap();
|
|
let pool = sqlx::PgPool::connect(&std::env::var(app_var).unwrap())
|
|
.await
|
|
.unwrap();
|
|
rmm_persistence::verify_application_role(&pool)
|
|
.await
|
|
.unwrap();
|
|
let tmp = std::env::temp_dir().join(format!("windows-review-{}", Uuid::new_v4()));
|
|
let s = crate::State {
|
|
pool,
|
|
signer: None,
|
|
viewer_a: [0; 32],
|
|
viewer_b: [1; 32],
|
|
auth: Arc::new(crate::auth::Auth::from_env("development").await.unwrap()),
|
|
job_signer: Arc::new(JobSigner::load_or_create(&tmp.join("fixture-key.pem")).unwrap()),
|
|
};
|
|
let p = operator();
|
|
let identity = p.identity.as_ref().unwrap();
|
|
let mut w = case_witness();
|
|
w["verification_key_id"] = json!(s.job_signer.key_id);
|
|
let boot = Uuid::parse_str(w["boot_id"].as_str().unwrap()).unwrap();
|
|
let journal = Uuid::parse_str(w["agent_journal_id"].as_str().unwrap()).unwrap();
|
|
let helper = Uuid::parse_str(w["helper_journal_id"].as_str().unwrap()).unwrap();
|
|
sqlx::query("INSERT INTO assets VALUES($1,$2)")
|
|
.bind(CASE_TENANT)
|
|
.bind(CASE_ASSET)
|
|
.execute(&admin)
|
|
.await
|
|
.unwrap();
|
|
sqlx::query("INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256,assignment_version,connection_generation,connection_boot_id,restore_blocked,capabilities) VALUES($1,$2,$3,$4,1,151,$5,true,$6)")
|
|
.bind(CASE_TENANT).bind(CASE_AGENT).bind(CASE_ASSET).bind(CERTIFICATE).bind(boot)
|
|
.bind(json!(["heartbeat/1","windows.base/1","agent.update/1"])).execute(&admin).await.unwrap();
|
|
sqlx::query("INSERT INTO agent_sync_state(tenant_id,agent_id,journal_id,checkpoint,decision_id,helper_journal_id,helper_checkpoint) VALUES($1,$2,$3,6755,$4,$5,1)")
|
|
.bind(CASE_TENANT).bind(CASE_AGENT).bind(journal).bind(FIRST).bind(helper).execute(&admin).await.unwrap();
|
|
for (i, c) in w["decisions"]
|
|
.as_array_mut()
|
|
.unwrap()
|
|
.iter_mut()
|
|
.enumerate()
|
|
{
|
|
let now = Utc::now() - Duration::hours(1);
|
|
let d = ReconcileDecisionPayload {
|
|
domain: "rmm.reconciliation-decision".into(),
|
|
schema_version: 1,
|
|
decision_id: Uuid::parse_str(c["decision_id"].as_str().unwrap()).unwrap(),
|
|
request_id: Uuid::parse_str(c["request_id"].as_str().unwrap()).unwrap(),
|
|
request_hash: c["request_sha256"].as_str().unwrap().into(),
|
|
tenant_id: CASE_TENANT,
|
|
agent_id: CASE_AGENT,
|
|
asset_id: CASE_ASSET,
|
|
enrollment_generation: 1,
|
|
assignment_version: 1,
|
|
connection_generation: 150,
|
|
boot_id: boot,
|
|
journal_id: journal,
|
|
checkpoint: 6755 + i as u64,
|
|
helper_journal_id: Some(helper),
|
|
helper_checkpoint: 1,
|
|
issued_at: now,
|
|
valid_until: now + Duration::seconds(60),
|
|
ready: i == 0,
|
|
helper_ready: i == 0,
|
|
reason: c["reason"].as_str().unwrap().into(),
|
|
attempts: vec![],
|
|
acknowledged_message_ids: vec![],
|
|
key_id: s.job_signer.key_id.clone(),
|
|
};
|
|
let raw = serde_json::to_vec(&d).unwrap();
|
|
let signature = s.job_signer.sign(&raw).unwrap();
|
|
c["payload_sha256"] = json!(hash(&raw));
|
|
c["signature_sha256"] = json!(hash(&signature));
|
|
sqlx::query("INSERT INTO sync_checkpoints(tenant_id,agent_id,request_id,decision_id,request_hash,journal_id,checkpoint,connection_generation,payload,signature) VALUES($1,$2,$3,$4,$5,$6,$7,150,$8,$9)")
|
|
.bind(CASE_TENANT).bind(CASE_AGENT).bind(d.request_id).bind(d.decision_id).bind(&d.request_hash)
|
|
.bind(journal).bind(d.checkpoint as i64).bind(raw).bind(signature).execute(&admin).await.unwrap();
|
|
}
|
|
let request = Uuid::new_v4();
|
|
let digest = inventory_sha256(&w).unwrap();
|
|
sqlx::query("INSERT INTO oidc_principals(principal,issuer,subject,tenant_ids) VALUES($1,$2,$3,ARRAY[$4]::uuid[])")
|
|
.bind(&p.name).bind(&identity.issuer).bind(&identity.subject).bind(CASE_TENANT).execute(&admin).await.unwrap();
|
|
sqlx::query("INSERT INTO memberships(tenant_id,principal,kind,permissions) VALUES($1,$2,'human',ARRAY['inventory:read'])")
|
|
.bind(CASE_TENANT).bind(&p.name).execute(&admin).await.unwrap();
|
|
sqlx::query("INSERT INTO windows_checkpoint_reviews(tenant_id,request_id,asset_id,agent_id,witness,witness_sha256,requested_by) VALUES($1,$2,$3,$4,$5,$6,'fixture-submitter')")
|
|
.bind(CASE_TENANT).bind(request).bind(CASE_ASSET).bind(CASE_AGENT).bind(&w).bind(&digest).execute(&admin).await.unwrap();
|
|
(admin, s, p, w, request, tmp)
|
|
}
|
|
#[tokio::test]
|
|
#[ignore = "requires a fresh isolated PostgreSQL database"]
|
|
async fn isolated_windows_echo_scope() {
|
|
let (admin, s, p, w, _, tmp) =
|
|
fixture("RMM_TEST_ECHO_ADMIN_URL", "RMM_TEST_ECHO_APP_URL").await;
|
|
crate::windows_echo_probe::test_fixed_echo(&admin, &s, p, w).await;
|
|
std::fs::remove_dir_all(tmp).unwrap();
|
|
}
|
|
#[tokio::test]
|
|
#[ignore = "requires a fresh isolated PostgreSQL database"]
|
|
async fn isolated_windows_checkpoint_human_review() {
|
|
let (admin, s, p, w, request, tmp) =
|
|
fixture("RMM_TEST_REPAIR_ADMIN_URL", "RMM_TEST_REPAIR_APP_URL").await;
|
|
let digest = inventory_sha256(&w).unwrap();
|
|
let confirmation = || Confirmation {
|
|
reviewed: true,
|
|
witness_sha256: digest.clone(),
|
|
previous_receipt_id: None,
|
|
};
|
|
assert_eq!(
|
|
confirm_review(&s, &p, request, confirmation(), &w)
|
|
.await
|
|
.unwrap_err()
|
|
.0,
|
|
StatusCode::FORBIDDEN
|
|
);
|
|
sqlx::query("UPDATE memberships SET permissions=array_append(permissions,'recovery:confirm') WHERE tenant_id=$1 AND principal=$2").bind(CASE_TENANT).bind(&p.name).execute(&admin).await.unwrap();
|
|
sqlx::query("UPDATE oidc_principals SET disabled=true WHERE principal=$1")
|
|
.bind(&p.name)
|
|
.execute(&admin)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
confirm_review(&s, &p, request, confirmation(), &w)
|
|
.await
|
|
.unwrap_err()
|
|
.0,
|
|
StatusCode::FORBIDDEN
|
|
);
|
|
sqlx::query("UPDATE oidc_principals SET disabled=false WHERE principal=$1")
|
|
.bind(&p.name)
|
|
.execute(&admin)
|
|
.await
|
|
.unwrap();
|
|
let mut sim = p.clone();
|
|
sim.testsimulation = true;
|
|
assert!(
|
|
confirm_review(&s, &sim, request, confirmation(), &w)
|
|
.await
|
|
.is_err()
|
|
);
|
|
let mut wrong = confirmation();
|
|
wrong.witness_sha256 = "00".repeat(32);
|
|
assert!(confirm_review(&s, &p, request, wrong, &w).await.is_err());
|
|
let mut altered = w.clone();
|
|
altered["decisions"][0]["signature_sha256"] = json!("00".repeat(32));
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, CASE_TENANT)
|
|
.await
|
|
.unwrap();
|
|
assert!(verify_case(&mut tx, &s, &altered, false).await.is_err());
|
|
tx.rollback().await.unwrap();
|
|
let mut other =
|
|
rmm_persistence::tenant_tx(&s.pool, Uuid::parse_str(rmm_domain::TENANT_B).unwrap())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
sqlx::query_scalar::<_, i64>("SELECT count(*) FROM windows_checkpoint_reviews")
|
|
.fetch_one(&mut *other)
|
|
.await
|
|
.unwrap(),
|
|
0
|
|
);
|
|
other.rollback().await.unwrap();
|
|
let first = confirm_review(&s, &p, request, confirmation(), &w)
|
|
.await
|
|
.unwrap()
|
|
.0;
|
|
assert_eq!(first["request"]["status"], "confirmed");
|
|
assert_eq!(
|
|
confirm_review(&s, &p, request, confirmation(), &w)
|
|
.await
|
|
.unwrap()
|
|
.0,
|
|
first
|
|
);
|
|
let row = sqlx::query(
|
|
"SELECT payload,signature FROM windows_checkpoint_review_receipts WHERE request_id=$1",
|
|
)
|
|
.bind(request)
|
|
.fetch_one(&admin)
|
|
.await
|
|
.unwrap();
|
|
let bytes: Vec<u8> = row.get("payload");
|
|
let signature: Vec<u8> = row.get("signature");
|
|
let key = PKey::public_key_from_raw_bytes(&s.job_signer.public_key().unwrap(), Id::ED25519)
|
|
.unwrap();
|
|
assert!(
|
|
Verifier::new_without_digest(&key)
|
|
.unwrap()
|
|
.verify_oneshot(&signature, &bytes)
|
|
.unwrap()
|
|
);
|
|
let receipt: Receipt = serde_json::from_slice(&bytes).unwrap();
|
|
assert!(
|
|
!receipt.ready && !receipt.helper_ready && !receipt.unlock && !receipt.testsimulation
|
|
);
|
|
assert_eq!(
|
|
(receipt.domain, receipt.witness, receipt.confirmed_by),
|
|
(RECEIPT_DOMAIN.to_owned(), w, p.name)
|
|
);
|
|
let flags=sqlx::query("SELECT restore_blocked,sync_required,helper_sync_ready FROM agent_installations WHERE agent_id=$1").bind(CASE_AGENT).fetch_one(&admin).await.unwrap();
|
|
assert!(
|
|
flags.get::<bool, _>("restore_blocked")
|
|
&& !flags.get::<bool, _>("sync_required")
|
|
&& !flags.get::<bool, _>("helper_sync_ready")
|
|
);
|
|
assert_eq!(
|
|
sqlx::query_scalar::<_, Uuid>(
|
|
"SELECT decision_id FROM agent_sync_state WHERE agent_id=$1"
|
|
)
|
|
.bind(CASE_AGENT)
|
|
.fetch_one(&admin)
|
|
.await
|
|
.unwrap(),
|
|
DENIED
|
|
);
|
|
assert_eq!(sqlx::query_scalar::<_,i64>("SELECT count(*) FROM audit_events WHERE event_type='windows_checkpoint_human_repair'").fetch_one(&admin).await.unwrap(),1);
|
|
assert_eq!(sqlx::query_scalar::<_,i64>("SELECT (SELECT count(*) FROM jobs)+(SELECT count(*) FROM outbox)+(SELECT count(*) FROM execution_permits)").fetch_one(&admin).await.unwrap(),0);
|
|
std::fs::remove_dir_all(tmp).unwrap();
|
|
}
|
|
}
|