Skip to content

Commit ec16554

Browse files
committed
feat(auth): a gauge says whether the newest audit verification found the chain broken
After each run, the verifier sets piquel.auth.audit.broken from the newest recorded verification, whoever made it: 1 when broken, 0 when intact, so monitoring can alert on it beside the in-app alert. The purge job's way of reporting a count after each run, while it is fresh, is now JobCount, which both share.
1 parent aed0325 commit ec16554

3 files changed

Lines changed: 95 additions & 35 deletions

File tree

‎apps/auth-service/src/audit/verifier.rs‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
//! Verifies the chain on a schedule, so the dashboard's Audit tab shows a recent
22
//! verification without an operator asking for one.
33
4-
use super::{AuditChain, AuditError, AuditKeys, AuditVerification};
5-
use crate::jobs::{ExclusiveJob, Shutdown};
4+
use super::{AuditChain, AuditError, AuditKeys, AuditVerification, VerificationKeys};
5+
use crate::jobs::{ExclusiveJob, JobCount, Shutdown};
66
use chrono::{TimeDelta, Utc};
7-
use piquel_common::events::{EventContext, JobName};
8-
use piquel_repository::{ExclusiveConnection, LockKey};
7+
use piquel_common::events::{DomainEvent, EventContext, JobName};
8+
use piquel_repository::{ExclusiveConnection, LockKey, RecordedEvent};
9+
use piquel_telemetry::Gauge;
910
use std::time::Duration;
1011

12+
/// Whether the newest recorded verification found the chain broken, read after
13+
/// each run of the verifier.
14+
static BROKEN: JobCount = JobCount::new(
15+
Gauge::new(
16+
"piquel.auth.audit.broken",
17+
"1",
18+
"1 when the newest recorded verification of the audit chain found it broken, 0 when \
19+
intact.",
20+
),
21+
Verifier::EVERY,
22+
);
23+
1124
/// Verifies the whole chain, on one replica at a time, once the newest recorded
1225
/// verification, whoever made it, is [`Self::VERIFY_EVERY`] old, and records
1326
/// what it found as [`JobName::AuditVerification`].
@@ -25,19 +38,37 @@ impl Verifier {
2538
Self { keys }
2639
}
2740

41+
/// Registers the [`BROKEN`] gauge.
42+
pub(crate) fn observe() {
43+
BROKEN.observe();
44+
}
45+
2846
/// Verifies and records the chain, unless a verification was recorded in the
29-
/// last [`Self::VERIFY_EVERY`]. Returns what it found, if it verified.
47+
/// last [`Self::VERIFY_EVERY`], and keeps whether the newest one found it
48+
/// broken. Returns what it found, if it verified.
3049
pub(super) async fn pass(
3150
&self,
3251
connection: &mut ExclusiveConnection,
3352
) -> Result<Option<AuditVerification>, AuditError> {
3453
let newest = connection.events().newest_audit_verification().await?;
35-
if newest.is_some_and(|newest| Utc::now() - newest.occurred_at < Self::VERIFY_EVERY) {
54+
if let Some(newest) = newest
55+
&& Utc::now() - newest.occurred_at < Self::VERIFY_EVERY
56+
{
57+
// A verification a newer release recorded reads as unknown, and one
58+
// with keys given with `--key` says nothing of this environment's
59+
// checkpoints, as for its alert.
60+
if let RecordedEvent::Known(DomainEvent::AuditChainVerified(verification)) =
61+
newest.event
62+
&& verification.keys != VerificationKeys::Given
63+
{
64+
BROKEN.set(u64::from(verification.broken.is_some()));
65+
}
3666
return Ok(None);
3767
}
3868
let verification = AuditChain::verify_in(connection.begin().await?, &self.keys).await?;
3969
let context = EventContext::system(JobName::AuditVerification);
4070
AuditChain::record(connection.begin().await?, &context, &verification).await?;
71+
BROKEN.set(u64::from(verification.broken.is_some()));
4172
Ok(Some(verification))
4273
}
4374
}

‎apps/auth-service/src/jobs/mod.rs‎

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ impl Jobs {
9999
) -> Result<Self, StartupError> {
100100
let mut jobs = Self::new();
101101
jobs.exclusive(Cleanup::new(), database.clone());
102-
AccountPurge::observe();
102+
FAILING_PURGES.observe();
103103
jobs.exclusive(AccountPurge, database.clone());
104104
jobs.exclusive(Sealer::new(&config.audit), database.clone());
105105
let audit_keys = AuditKeys::for_server(&config.passkey.origin, config.audit.verifier())
106106
.map_err(StartupError::AuditKeys)?;
107+
Verifier::observe();
107108
jobs.exclusive(Verifier::new(audit_keys), database.clone());
108109
let (store, reloaded) = (store.clone(), keys.clone());
109110
jobs.spawn(|shutdown| store.run(reloaded, shutdown));
@@ -376,15 +377,57 @@ impl ExclusiveJob for Cleanup {
376377
}
377378
}
378379

379-
/// Accounts whose purge keeps failing, from [`FAILING_PURGES_COUNT`].
380-
static FAILING_PURGES: Gauge = Gauge::new(
381-
"piquel.auth.account_purge.failing",
382-
"{account}",
383-
"Accounts scheduled for deletion whose purge failed at least 3 times in a row.",
380+
/// A count the replica running a periodic job takes after each run, which its
381+
/// gauge reports. Every replica runs the job in turn, so each reports the count
382+
/// of its own latest run, at most one run older than the newest; one whose count
383+
/// is older than two runs, because it stopped running the job, reports none.
384+
/// Alerts read the minimum across replicas, or wait one run, before taking a
385+
/// count as current.
386+
pub(crate) struct JobCount {
387+
/// The gauge reporting it.
388+
gauge: Gauge,
389+
/// How often the job runs.
390+
every: Duration,
391+
/// The latest count on this replica, and when it was taken.
392+
counted: Mutex<Option<(u64, Instant)>>,
393+
}
394+
395+
impl JobCount {
396+
/// A count `gauge` reports, of a job that runs `every` so often.
397+
pub(crate) const fn new(gauge: Gauge, every: Duration) -> Self {
398+
Self {
399+
gauge,
400+
every,
401+
counted: Mutex::new(None),
402+
}
403+
}
404+
405+
/// Registers the gauge.
406+
pub(crate) fn observe(&'static self) {
407+
self.gauge.observe(|| {
408+
let counted = *self.counted.lock().unwrap_or_else(PoisonError::into_inner);
409+
counted
410+
.filter(|(_, at)| at.elapsed() < self.every * 2)
411+
.map(|(count, _)| (count, ()))
412+
});
413+
}
414+
415+
/// Keeps `count`, taken now.
416+
pub(crate) fn set(&self, count: u64) {
417+
*self.counted.lock().unwrap_or_else(PoisonError::into_inner) =
418+
Some((count, Instant::now()));
419+
}
420+
}
421+
422+
/// Accounts whose purge keeps failing, counted after each purge run.
423+
static FAILING_PURGES: JobCount = JobCount::new(
424+
Gauge::new(
425+
"piquel.auth.account_purge.failing",
426+
"{account}",
427+
"Accounts scheduled for deletion whose purge failed at least 3 times in a row.",
428+
),
429+
AccountPurge::EVERY,
384430
);
385-
/// The latest count of [`FAILING_PURGES`] on this replica, and when it was taken.
386-
/// The replica running the purge counts after each run.
387-
static FAILING_PURGES_COUNT: Mutex<Option<(u64, Instant)>> = Mutex::new(None);
388431

389432
/// Purges the accounts whose deletion grace period ended, each in a transaction of
390433
/// its own, attributed to [`JobName::AccountPurge`]. One run purges at most
@@ -403,22 +446,6 @@ impl AccountPurge {
403446
/// Failures in a row after which [`FAILING_PURGES`] counts an account.
404447
const FAILING_ATTEMPTS: u16 = 3;
405448

406-
/// Registers [`FAILING_PURGES`]. Every replica runs the job in turn, so each
407-
/// reports the count of its own latest run, at most one run older than the
408-
/// newest; one whose count is older than two runs, because it stopped running
409-
/// the job, reports none. Alerts read the minimum across replicas, or wait one
410-
/// run, before taking a count as current.
411-
fn observe() {
412-
FAILING_PURGES.observe(|| {
413-
let counted = *FAILING_PURGES_COUNT
414-
.lock()
415-
.unwrap_or_else(PoisonError::into_inner);
416-
counted
417-
.filter(|(_, at)| at.elapsed() < Self::EVERY * 2)
418-
.map(|(count, _)| (count, ()))
419-
});
420-
}
421-
422449
/// Purges `user_id` in `tx` if it is still scheduled for deletion and due,
423450
/// rechecked under the account lock, since it may have been restored meanwhile.
424451
pub(crate) async fn purge(
@@ -474,9 +501,7 @@ impl ExclusiveJob for AccountPurge {
474501
.deletions()
475502
.count_failing(Self::FAILING_ATTEMPTS)
476503
.await?;
477-
*FAILING_PURGES_COUNT
478-
.lock()
479-
.unwrap_or_else(PoisonError::into_inner) = Some((failing, Instant::now()));
504+
FAILING_PURGES.set(failing);
480505
let mut tx = connection.begin().await?;
481506
let context = EventContext::system(JobName::AccountPurge);
482507
let mut alerts = Alerts::new(&mut tx, &context);

‎docs/dev/security/audit-history.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ Every verification is recorded as an `audit_chain_verified` event, with who
205205
made it and what it found. Auth also verifies the chain on its own: every ten
206206
minutes one replica checks the newest recorded verification, and once it is six
207207
hours old, whoever made it, verifies the whole chain with the dashboard's keys
208-
(below) and records it as the `audit_verification` job. A verification that
208+
(below) and records it as the `audit_verification` job. After each check, that
209+
replica's `piquel.auth.audit.broken` gauge says whether the newest recorded
210+
verification found the chain broken (1) or intact (0), leaving out one made with
211+
`--key`, as the alert does. A replica that has not checked lately reports nothing,
212+
so an absent gauge does not mean intact. A verification that
209213
finds the chain broken raises the `audit_chain_broken` alert, which the
210214
dashboard's Alerts tab and `auth-service alerts list` show,
211215
and the next one that finds it intact clears it. One with keys given with

0 commit comments

Comments
 (0)