From dfe10e30a72208417eedf5ec0117861126ec904c Mon Sep 17 00:00:00 2001 From: piquel Date: Fri, 20 Nov 2026 09:02:09 +0100 Subject: [PATCH] refactor(auth): audit reads the link a checkpoint signs in one place Five places built a ChainLink from a stored checkpoint's position and head by hand. Chain::signed builds it. --- apps/auth-service/src/audit/mod.rs | 12 +++++++++++- apps/auth-service/src/audit/routes.rs | 6 +----- apps/auth-service/src/audit/sealer.rs | 5 +---- apps/auth-service/src/audit/verify.rs | 18 +++--------------- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/apps/auth-service/src/audit/mod.rs b/apps/auth-service/src/audit/mod.rs index 8d44c2777..54d067faf 100644 --- a/apps/auth-service/src/audit/mod.rs +++ b/apps/auth-service/src/audit/mod.rs @@ -33,7 +33,7 @@ use chrono::{DateTime, SecondsFormat, Utc}; use serde::Serialize; use sourceorigin_common::config::Origin; use sourceorigin_crypto::{CryptoError, Digest, NoteVerifier, NoteVerifiers}; -use sourceorigin_repository::{ChainLink, Database, DatabaseError, EventContent}; +use sourceorigin_repository::{ChainLink, Checkpoint, Database, DatabaseError, EventContent}; use thiserror::Error; /// The verifier keys committed to the repository, one per line: each environment's @@ -112,6 +112,9 @@ trait Chain: Sized { /// [`EventContent::NEWEST_FORMAT`], or [`AuditError::Canonical`]. fn extend(&self, content: &EventContent) -> Result; + /// The link `checkpoint` signs: its position and the head stored beside it. + fn signed(checkpoint: &Checkpoint) -> Self; + /// This link as verification reports it. fn point(&self) -> ChainPoint; } @@ -151,6 +154,13 @@ impl Chain for ChainLink { }) } + fn signed(checkpoint: &Checkpoint) -> Self { + Self { + position: checkpoint.position, + hash: checkpoint.head, + } + } + fn point(&self) -> ChainPoint { ChainPoint { position: self.position, diff --git a/apps/auth-service/src/audit/routes.rs b/apps/auth-service/src/audit/routes.rs index 02cb6344a..6cca2b2b5 100644 --- a/apps/auth-service/src/audit/routes.rs +++ b/apps/auth-service/src/audit/routes.rs @@ -72,11 +72,7 @@ impl From for PublishedCheckpoint { /// Keeps the stored values; the note is what readers verify. fn from(checkpoint: Checkpoint) -> Self { Self { - head: ChainLink { - position: checkpoint.position, - hash: checkpoint.head, - } - .point(), + head: ChainLink::signed(&checkpoint).point(), time: checkpoint.created_at, note: checkpoint.note, } diff --git a/apps/auth-service/src/audit/sealer.rs b/apps/auth-service/src/audit/sealer.rs index 86957632c..10f5f9162 100644 --- a/apps/auth-service/src/audit/sealer.rs +++ b/apps/auth-service/src/audit/sealer.rs @@ -192,10 +192,7 @@ impl Sealer { .into_iter() .flatten() .max_by_key(|checkpoint| checkpoint.position) - .map_or(ChainLink::GENESIS, |checkpoint| ChainLink { - position: checkpoint.position, - hash: checkpoint.head, - }); + .map_or(ChainLink::GENESIS, ChainLink::signed); // Nothing new to sign, or an untrusted checkpoint holds the head's position // until the next event is sealed: return before recomputing anything. let newest = untrusted diff --git a/apps/auth-service/src/audit/verify.rs b/apps/auth-service/src/audit/verify.rs index 4d24463d4..98afb64e7 100644 --- a/apps/auth-service/src/audit/verify.rs +++ b/apps/auth-service/src/audit/verify.rs @@ -62,10 +62,7 @@ impl AuditKeys { /// Returns [`AuditError::Verifying`] if the cryptographic provider fails, which /// says nothing about the checkpoint. fn check(&self, checkpoint: &Checkpoint) -> Result { - let stored = ChainLink { - position: checkpoint.position, - hash: checkpoint.head, - }; + let stored = ChainLink::signed(checkpoint); let problem = match self.verifiers.open(&checkpoint.note) { Ok(text) => match CheckpointText::parse(text) { Some(text) if text.head == stored && text.time == checkpoint.created_at => None, @@ -202,13 +199,7 @@ impl AuditChain { pub(crate) async fn page(&self, after: Option) -> Result { let mut tx = self.database.begin().await?; tx.audit().read_snapshot().await?; - let point = |checkpoint: Checkpoint| { - ChainLink { - position: checkpoint.position, - hash: checkpoint.head, - } - .point() - }; + let point = |checkpoint: Checkpoint| ChainLink::signed(&checkpoint).point(); let chain = StoredChain { anchor: tx.audit().anchor().await?.map(point), newest: tx.audit().head().await?.as_ref().map(Chain::point), @@ -263,10 +254,7 @@ impl AuditChain { keys: &AuditKeys, ) -> Result { let checkpoints = Self::checkpoints(tx, keys).await?; - let anchor = tx.audit().anchor().await?.map(|anchor| ChainLink { - position: anchor.position, - hash: anchor.head, - }); + let anchor = tx.audit().anchor().await?.as_ref().map(ChainLink::signed); let start = anchor.unwrap_or(ChainLink::GENESIS); // Pruning deletes every event through the anchor, so one left there means // the anchor was moved to hide it.