|
| 1 | +//! Masked denial responses, person requests, and the audit detail behind them. |
| 2 | +
|
| 3 | +use reqwest::header; |
| 4 | +use serde_json::{Value, json}; |
| 5 | +use uuid::Uuid; |
| 6 | + |
| 7 | +use crate::store::main::{begin::begin, commit::commit, query::query}; |
| 8 | +use crate::store::models::{ControlStore, TransactionMode}; |
| 9 | + |
| 10 | +/// Status, the headers that could tell cases apart, and the body with its decision ID masked. |
| 11 | +pub(crate) async fn masked(response: reqwest::Response) -> (Value, Uuid) { |
| 12 | + let status = response.status().as_u16(); |
| 13 | + let headers: Vec<(String, String)> = response |
| 14 | + .headers() |
| 15 | + .iter() |
| 16 | + .filter(|(name, _)| *name != header::DATE) |
| 17 | + .map(|(name, value)| (name.to_string(), value.to_str().unwrap_or("").to_owned())) |
| 18 | + .collect(); |
| 19 | + let text = response.text().await.expect("denial body"); |
| 20 | + let body: Value = serde_json::from_str(&text).expect("JSON denial"); |
| 21 | + let decision: Uuid = serde_json::from_value(body["decision_id"].clone()).expect("decision ID"); |
| 22 | + ( |
| 23 | + json!({"status": status, "headers": headers, |
| 24 | + "body": text.replace(&decision.to_string(), "<decision>")}), |
| 25 | + decision, |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +pub(crate) async fn audited_scope(store: &ControlStore, decision: Uuid) -> Value { |
| 30 | + let transaction = begin(store, TransactionMode::Read).await.expect("audit"); |
| 31 | + let (transaction, rows) = query( |
| 32 | + transaction, |
| 33 | + "SELECT scope_json, after_json FROM sqb_security_audit WHERE action='authorization.denied'", |
| 34 | + vec![], |
| 35 | + ) |
| 36 | + .await |
| 37 | + .expect("denials"); |
| 38 | + commit(transaction).await.expect("close audit"); |
| 39 | + rows.into_iter() |
| 40 | + .find(|row| row["after_json"]["decision_id"] == json!(decision)) |
| 41 | + .map(|row| json!([row["scope_json"]["nodes"], row["after_json"]["reason"]])) |
| 42 | + .unwrap_or(Value::Null) |
| 43 | +} |
| 44 | + |
| 45 | +/// A person's browser request with session cookie, origin and CSRF proof. |
| 46 | +pub(crate) async fn person_send( |
| 47 | + client: &reqwest::Client, |
| 48 | + (url, session, csrf): (&str, &str, &str), |
| 49 | + body: Option<Value>, |
| 50 | +) -> reqwest::Response { |
| 51 | + let request = body.map_or_else(|| client.get(url), |body| client.post(url).json(&body)); |
| 52 | + request |
| 53 | + .header(header::HOST, "control.example.com") |
| 54 | + .header(header::ORIGIN, "https://control.example.com") |
| 55 | + .header(header::COOKIE, format!("__Host-sqb-session={session}")) |
| 56 | + .header("x-sqb-csrf", csrf) |
| 57 | + .send() |
| 58 | + .await |
| 59 | + .expect("person request") |
| 60 | +} |
| 61 | + |
| 62 | +/// A job request body for a location and job key. |
| 63 | +pub(crate) fn job(location_id: Uuid, job_key: &str) -> Value { |
| 64 | + json!({"run_id": Uuid::now_v7(), "location_id": location_id, "target": "development", |
| 65 | + "job_key": job_key, "reason": "Refresh orders", "priority": 0}) |
| 66 | +} |
| 67 | + |
| 68 | +/// A trigger change body of the given kind. |
| 69 | +pub(crate) fn trigger(trigger_id: Uuid, change: &str) -> Value { |
| 70 | + json!({"event_id": Uuid::now_v7(), "trigger_id": trigger_id, "actor": "", |
| 71 | + "reason": "Hold orders", "change": {"kind": change}}) |
| 72 | +} |
0 commit comments