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
12 changes: 11 additions & 1 deletion apps/auth-service/src/audit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,6 +112,9 @@ trait Chain: Sized {
/// [`EventContent::NEWEST_FORMAT`], or [`AuditError::Canonical`].
fn extend(&self, content: &EventContent) -> Result<Self, AuditError>;

/// 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;
}
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions apps/auth-service/src/audit/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ impl From<Checkpoint> 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,
}
Expand Down
5 changes: 1 addition & 4 deletions apps/auth-service/src/audit/sealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 3 additions & 15 deletions apps/auth-service/src/audit/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Checked, AuditError> {
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,
Expand Down Expand Up @@ -202,13 +199,7 @@ impl AuditChain {
pub(crate) async fn page(&self, after: Option<u64>) -> Result<AuditPage, AuditError> {
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),
Expand Down Expand Up @@ -263,10 +254,7 @@ impl AuditChain {
keys: &AuditKeys,
) -> Result<AuditVerification, AuditError> {
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.
Expand Down