Skip to main content

pb_mapper_auth/persistence/
mod.rs

1//! Durable, encrypted authentication state and audit/replay retention.
2//!
3//! ```text
4//! startup:   lock -> admin.key -> recover instance id -> decrypt snapshot -> replay WAL
5//! mutation:  command   -> fsync encrypted WAL -> publish hot-state change
6//! compact:   hot state + audit + replay set -> snapshot -> truncate WAL, when changed
7//! ```
8//!
9//! Snapshot replacement and administrator-key files use atomic rename. Bounded audit
10//! and replay collections are carried through compaction so security history does not
11//! disappear when the WAL is truncated.
12
13use super::*;
14
15mod admin_key;
16mod blob;
17mod format;
18mod fs;
19mod instance_id;
20mod namespaces;
21mod snapshot;
22mod wal;
23
24pub use admin_key::{
25    discard_staged_admin_key, generate_admin_key, initialize_admin_key, stage_admin_key_candidate,
26    staged_admin_key_path, write_admin_key_file,
27};
28pub(crate) use admin_key::{
29    persist_recovery_admin_key, read_admin_key, recover_admin_key_after_rotation,
30    rotation_already_installed, validate_admin_key, write_admin_key,
31};
32pub(crate) use blob::{open_blob, seal_blob};
33pub(crate) use format::{
34    AdminReplayRecord, AuditRecord, PersistedEntry, PersistedSnapshot, StateMutation, WalRecord,
35    audit_action,
36};
37pub use fs::acquire_state_dir_lock;
38#[cfg(test)]
39pub(crate) use fs::prepare_state_dir;
40pub(crate) use fs::sync_parent_directory;
41pub(crate) use fs::{atomic_write, create_new_write, prepare_state_dir_and_lock};
42#[cfg(test)]
43pub(crate) use instance_id::read_instance_id_file;
44pub(crate) use instance_id::{
45    load_or_create_instance_id, random_instance_id, recover_instance_id_after_reset,
46    reset_already_installed,
47};
48pub(crate) use namespaces::{
49    read_namespace_high_water, restore_namespace_high_water, write_namespace_high_water,
50};
51#[cfg(test)]
52pub(crate) use snapshot::try_load_persisted_state;
53pub(crate) use snapshot::{
54    build_snapshot, cancel_all_temporary_leases, empty_snapshot, load_persisted_state,
55    normalize_tombstone_times, push_audit_record, push_persisted_audit, split_high_slot_state,
56};
57#[cfg(test)]
58pub(crate) use wal::append_wal;
59pub(crate) use wal::{
60    append_audit, append_mutation, append_record, read_wal, truncate_auth_wal,
61    write_snapshot_and_truncate_wal,
62};
63
64// The files of a state directory. Renaming one orphans the state an older
65// relay left there.
66const AUTH_SNAPSHOT_FILE: &str = "auth.snapshot";
67const AUTH_WAL_FILE: &str = "auth.wal";
68const LOCK_FILE: &str = "auth.lock";
69/// The live administrator key's file name in a state directory.
70pub const ADMIN_KEY_FILE: &str = "admin.key";
71/// The new key a root rotation stages while it installs it. Not the candidate
72/// [`staged_admin_key_path`] names for the CLI and SDK (`.admin.key.next`):
73/// those are two different files.
74const ROTATION_KEY_FILE: &str = "admin.key.next";
75const INSTANCE_ID_FILE: &str = "server-instance-id";
76/// The new instance id a reset stages until its snapshot is in place.
77const STAGED_INSTANCE_ID_FILE: &str = "server-instance-id.next";
78
79pub(crate) fn auth_snapshot_path(state_dir: &Path) -> PathBuf {
80    state_dir.join(AUTH_SNAPSHOT_FILE)
81}
82
83pub(crate) fn auth_wal_path(state_dir: &Path) -> PathBuf {
84    state_dir.join(AUTH_WAL_FILE)
85}
86
87fn lock_path(state_dir: &Path) -> PathBuf {
88    state_dir.join(LOCK_FILE)
89}
90
91/// Where the live administrator key of `state_dir` is kept.
92pub fn admin_key_path(state_dir: &Path) -> PathBuf {
93    state_dir.join(ADMIN_KEY_FILE)
94}
95
96pub(crate) fn rotation_key_path(state_dir: &Path) -> PathBuf {
97    state_dir.join(ROTATION_KEY_FILE)
98}
99
100pub(crate) fn instance_id_path(state_dir: &Path) -> PathBuf {
101    state_dir.join(INSTANCE_ID_FILE)
102}
103
104pub(crate) fn staged_instance_id_path(state_dir: &Path) -> PathBuf {
105    state_dir.join(STAGED_INSTANCE_ID_FILE)
106}
107
108pub fn encrypted_auth_state_exists(state_dir: &Path) -> bool {
109    auth_snapshot_path(state_dir).exists() || auth_wal_path(state_dir).exists()
110}
111
112/// `N` bytes from the thread's cryptographically secure generator.
113pub(crate) fn random_bytes<const N: usize>() -> [u8; N] {
114    let mut bytes = [0; N];
115    rand::fill(&mut bytes);
116    bytes
117}
118
119pub(crate) fn hex(bytes: &[u8]) -> String {
120    const DIGITS: &[u8; 16] = b"0123456789abcdef";
121    let mut output = String::with_capacity(bytes.len() * 2);
122    for byte in bytes {
123        output.push(DIGITS[(byte >> 4) as usize] as char);
124        output.push(DIGITS[(byte & 0x0f) as usize] as char);
125    }
126    output
127}