1use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
70use std::fmt;
71use std::fs::{File, OpenOptions};
72use std::io::{Read, Write};
73#[cfg(unix)]
74use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
75use std::path::{Path, PathBuf};
76use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
77use std::sync::{Arc, Weak};
78use std::time::Duration;
79
80use parking_lot::{Mutex, RwLock};
81use rand::RngExt;
82use ring::aead::{AES_256_GCM, Aad, LessSafeKey, Nonce, UnboundKey};
83use ring::hkdf::{HKDF_SHA256, Prk, Salt};
84use serde::{Deserialize, Serialize};
85use subtle::ConstantTimeEq;
86use tokio::sync::{mpsc, oneshot, watch};
87use tokio_util::sync::CancellationToken;
88
89use pb_mapper_core::checksum::{
90 AesKeyType, Credential, MACHINE_MSG_HEADER_KEY_PATH, encode_temporary_credential,
91 env_safe_admin_key_error, get_process_credential, is_env_safe_admin_key, parse_credential,
92 process_credential_configured, set_process_msg_header_key,
93};
94use pb_mapper_core::time::unix_seconds;
95
96pub const ADMIN_NAMESPACE: u64 = ADMIN_KEY_ID.as_u64();
99pub const CAPABILITY_KEY_NAMESPACES: &str = "key_namespaces";
102pub const ADMIN_SCHEMA_VERSION: u16 = 1;
106pub const DEFAULT_AUTH_STATE_DIR: &str = "/var/lib/pb-mapper/auth";
107pub const DEFAULT_TEMP_KEY_CAPACITY: usize = 65_536;
108pub const MAX_TEMP_KEY_CAPACITY: usize = 1_048_576;
109pub const DEFAULT_MAX_TEMP_KEY_TTL: Duration = Duration::from_secs(30 * 24 * 60 * 60);
110pub const MIN_TEMP_KEY_TTL: Duration = Duration::from_secs(10);
111pub const MAX_TEMP_KEY_TTL: Duration = Duration::from_secs(365 * 24 * 60 * 60);
112const TOMBSTONE_RETENTION: Duration = Duration::from_secs(60);
113const MAX_SCHEDULABLE_DELAY: Duration =
116 Duration::from_secs(MAX_TEMP_KEY_TTL.as_secs() + TOMBSTONE_RETENTION.as_secs());
117const SNAPSHOT_COMPACTION_INTERVAL: Duration = Duration::from_secs(5 * 60);
118const SNAPSHOT_SCHEMA_VERSION: u16 = 1;
119const STATE_BLOB_MAGIC: &[u8; 5] = b"PBAS1";
120const STATE_AAD: &[u8] = b"pb-mapper-auth-state-v1";
121const INSTANCE_ID_LEN: usize = 16;
122const ADMIN_REPLAY_RETENTION: Duration = Duration::from_secs(10 * 60);
123const ADMIN_REPLAY_CAPACITY: usize = 65_536;
124const AUDIT_RECORD_CAPACITY: usize = 4096;
125
126const _: () = assert!(
127 MAX_TEMP_KEY_CAPACITY as u64 <= u32::MAX as u64 + 1,
128 "every slot index has to fit SlotIndex's 32 bits"
129);
130const _: () = assert!(
131 DEFAULT_TEMP_KEY_CAPACITY <= MAX_TEMP_KEY_CAPACITY,
132 "the default capacity has to be one the configuration accepts"
133);
134const _: () = assert!(
135 MIN_TEMP_KEY_TTL.as_secs() <= DEFAULT_MAX_TEMP_KEY_TTL.as_secs()
136 && DEFAULT_MAX_TEMP_KEY_TTL.as_secs() <= MAX_TEMP_KEY_TTL.as_secs(),
137 "the default maximum TTL has to be one the configuration accepts"
138);
139
140mod actor;
141pub mod codes;
142mod config;
143mod ids;
144mod keys;
145mod lease;
146mod leases;
147mod persistence;
148mod runtime;
149mod state;
150#[cfg(test)]
151mod tests;
152mod timing_wheel;
153mod types;
154
155pub use config::{AuthConfigOverrides, default_auth_state_dir};
157pub use ids::{ADMIN_KEY_ID, Generation, KeyId, MAX_ALLOCATED_NAMESPACE, SlotIndex};
158pub use keys::derive_temporary_key;
159pub use lease::{AuthContext, AuthLease};
160pub use persistence::{
161 ADMIN_KEY_FILE, acquire_state_dir_lock, admin_key_path, discard_staged_admin_key,
162 encrypted_auth_state_exists, generate_admin_key, initialize_admin_key,
163 stage_admin_key_candidate, staged_admin_key_path, write_admin_key_file,
164};
165pub use runtime::{AuthRuntime, LegacyConnectionGuard};
166pub use types::{
167 AuthConfig, AuthFailure, AuthStatus, IssuedTemporaryKey, KeyPage, LegacyProtocolPolicy,
168 TemporaryKeyMetadata,
169};
170
171use actor::{Actor, AdminReplays, AuthCommand};
173use keys::{
174 expand_temporary_key, load_isolated_server_admin_credential, load_server_admin_credential,
175 temporary_root,
176};
177use leases::Leases;
178use persistence::{
179 AdminReplayRecord, AuditRecord, PersistedEntry, PersistedSnapshot, StateMutation, WalRecord,
180 append_audit, append_mutation, append_record, atomic_write, audit_action, build_snapshot,
181 cancel_all_temporary_leases, empty_snapshot, hex, instance_id_path, load_or_create_instance_id,
182 load_persisted_state, normalize_tombstone_times, persist_recovery_admin_key,
183 prepare_state_dir_and_lock, push_audit_record, push_persisted_audit, random_instance_id,
184 read_admin_key, read_namespace_high_water, recover_admin_key_after_rotation,
185 recover_instance_id_after_reset, reset_already_installed, restore_namespace_high_water,
186 rotation_already_installed, rotation_key_path, split_high_slot_state, staged_instance_id_path,
187 validate_admin_key, write_admin_key, write_namespace_high_water,
188 write_snapshot_and_truncate_wal,
189};
190use runtime::off_runtime;
191use state::{
192 AdminState, AuthStateInner, ColdMetadata, LiveLeases, PreviousRoot, RestoredState, SlotHot,
193 SlotState,
194};
195use timing_wheel::{Timer, TimingWheel};