Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions backend/crates/bi-server/src/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ pub(crate) struct HttpRecord<'a> {
pub action: bi_audit::Action,
pub result: bi_audit::ResultCode,
pub resource_type: &'static str,
pub resource_id: &'static str,
pub resource_id: &'a str,
/// The decision's fingerprint; `None` when no decision was taken.
pub policy_decision_id: Option<String>,
/// The redacted shape of a query, when the route answers one.
pub query_shape: Option<bi_audit::QueryShape>,
}

/// Appends `record` within `budget`, all of it: connection, transaction,
/// the append (which may wait for the audit head) and the commit.
/// Appends `record` within `budget`, all of it: the event's identifier
/// (entropy runs on the blocking pool, which may be busy), connection,
/// transaction, the append (which may wait for the audit head) and the
/// commit.
/// PostgreSQL also ends its own wait within what is left, so an abandoned
/// statement never outlives the request; an unconfirmed transaction rolls
/// back and frees its connection. No retry. The error is the problem code
Expand All @@ -68,28 +72,30 @@ pub(crate) async fn record_within(
) -> Result<(), &'static str> {
const UNAVAILABLE: &str = "audit_unavailable";
let started = Instant::now();
let event_id = crate::auth::random_token()
.await
.map_err(|()| UNAVAILABLE)?;
let (request_id, trace_id) = correlation(record.request_id);
let event = Event {
event_id,
occurred_at: std::time::SystemTime::now().into(),
actor_id: record.actor.to_owned(),
actor_type: bi_audit::ActorType::User,
session_id: None,
action: record.action,
resource_type: record.resource_type.to_owned(),
resource_id: record.resource_id.to_owned(),
result: record.result,
request_id,
trace_id,
policy_decision_id: record.policy_decision_id,
source: bi_audit::Source::Http,
metadata_redacted: bi_audit::MetadataRedacted { query_shape: None },
schema_version: bi_audit::SCHEMA_VERSION,
};
let write = async {
let event_id = crate::auth::random_token()
.await
.map_err(|()| UNAVAILABLE)?;
let event = Event {
event_id,
occurred_at: std::time::SystemTime::now().into(),
actor_id: record.actor.to_owned(),
actor_type: bi_audit::ActorType::User,
session_id: None,
action: record.action,
resource_type: record.resource_type.to_owned(),
resource_id: record.resource_id.to_owned(),
result: record.result,
request_id,
trace_id,
policy_decision_id: record.policy_decision_id,
source: bi_audit::Source::Http,
metadata_redacted: bi_audit::MetadataRedacted {
query_shape: record.query_shape,
},
schema_version: bi_audit::SCHEMA_VERSION,
};
let mut transaction = db
.begin()
.await
Expand Down Expand Up @@ -433,3 +439,6 @@ pub async fn report(pool: &PgPool, trusted: Option<Checkpoint>) -> Result<Report
checkpoint: checkpoint_out,
})
}

#[cfg(test)]
mod tests;
66 changes: 66 additions & 0 deletions backend/crates/bi-server/src/audit/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! The audit writer's budget covers every wait, the event's identifier
//! included: entropy runs on the blocking pool, which another task may be
//! holding (#138).

use std::time::{Duration, Instant};

use super::{HttpRecord, record_within};
use crate::db::Database;
use crate::metadata::MetadataMode;

fn record() -> HttpRecord<'static> {
HttpRecord {
actor: "ana",
request_id: "req-audit-budget",
action: bi_audit::Action::Query,
result: bi_audit::ResultCode::Failure,
resource_type: "semantic_request",
resource_id: "semantic:request",
policy_decision_id: None,
query_shape: None,
}
}

#[test]
fn a_busy_blocking_pool_never_outlives_the_audit_budget() {
// One blocking thread, held: the identifier's entropy has to queue.
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.max_blocking_threads(1)
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
// No metadata database is reachable: the write could only fail
// after the identifier, never wait for it unbounded.
let db = Database::start(
&"postgres://bi:bi@127.0.0.1:1/bi".parse().unwrap(),
MetadataMode::Development,
);
let (release, held) = std::sync::mpsc::channel::<()>();
let (started, running) = tokio::sync::oneshot::channel();
let holder = tokio::task::spawn_blocking(move || {
let _ = started.send(());
let _ = held.recv();
});
running.await.unwrap();
let budget = Duration::from_millis(200);
let begun = Instant::now();
let refused =
tokio::time::timeout(Duration::from_secs(5), record_within(&db, record(), budget))
.await
.expect("the budget bounds the wait for entropy");
let elapsed = begun.elapsed();
assert_eq!(refused, Err("audit_unavailable"));
assert!(elapsed >= budget, "{elapsed:?}");
assert!(elapsed < budget * 3, "{elapsed:?} for a {budget:?} budget");
// Released, the identifier is generated again: what fails now is the
// unreachable database, promptly and within the same budget.
release.send(()).unwrap();
holder.await.unwrap();
let begun = Instant::now();
let answered = record_within(&db, record(), Duration::from_secs(5)).await;
assert!(answered.is_err());
assert!(begun.elapsed() < Duration::from_secs(5));
});
}
4 changes: 4 additions & 0 deletions backend/crates/bi-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub fn router_with_static(state: AppState, static_dir: Option<PathBuf>) -> Route
"/api/v1/semantic/explain",
post(semantic::explain).layer(DefaultBodyLimit::max(QUERY_BODY_LIMIT)),
)
.route(
"/api/v1/semantic/query",
post(semantic::query).layer(DefaultBodyLimit::max(QUERY_BODY_LIMIT)),
)
.with_state(state.clone());
let app = if let Some(directory) = static_dir {
app.fallback(move |request| static_files::serve(directory.clone(), request))
Expand Down
1 change: 1 addition & 0 deletions backend/crates/bi-server/src/models/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl Event {
resource_type: "project_catalog",
resource_id: RESOURCE_ID,
policy_decision_id: self.fingerprint,
query_shape: None,
};
crate::audit::record_within(db, record, budget).await
}
Expand Down
Loading