Skip to main content

hyperlane_plugin_audit/
struct.rs

1use super::*;
2
3/// Single tamper-evident hash-chained HTTP audit event.
4#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
5pub struct AuditRecord {
6    /// Monotonically increasing sequence number (`1`, `2`, ...).
7    #[get(type(copy))]
8    pub(crate) sequence: u64,
9    /// Wall-clock UTC timestamp in milliseconds since Unix epoch.
10    #[get(type(copy))]
11    pub(crate) timestamp_ms: u64,
12    /// HTTP method string.
13    pub(crate) method: String,
14    /// Request path.
15    pub(crate) path: String,
16    /// HTTP response status code.
17    #[get(type(copy))]
18    pub(crate) status_code: usize,
19    /// Request processing latency in microseconds.
20    #[get(type(copy))]
21    pub(crate) latency_us: u64,
22    /// Actor identity (from [`set_audit_actor`] or `"anonymous"`).
23    pub(crate) actor: String,
24    /// Semantic action label (from [`set_audit_action`] or empty).
25    pub(crate) action: String,
26    /// Correlation ID extracted from request headers, if present.
27    pub(crate) request_id: String,
28    /// Captured safe/redacted request headers.
29    pub(crate) headers: Vec<(String, String)>,
30    /// Previous record's `entry_hash` (or [`AUDIT_GENESIS_HASH`]).
31    pub(crate) prev_hash: String,
32    /// SHA-256 chain hash binding all fields and `prev_hash`.
33    pub(crate) entry_hash: String,
34}
35
36/// Bounded in-memory ring-buffer [`AuditSink`] suitable for testing and recent audit queries.
37#[derive(Debug)]
38pub struct InMemoryAuditSink {
39    /// Maximum retained records before oldest eviction.
40    pub(crate) capacity: usize,
41    /// Guarded ring buffer of recent records.
42    pub(crate) records: Mutex<VecDeque<AuditRecord>>,
43}
44
45/// Configuration for [`AuditPlugin`].
46#[derive(Clone, Data, Debug, Eq, PartialEq)]
47pub struct AuditConfig {
48    /// When `true`, only mutating methods (`POST`, `PUT`, `PATCH`, `DELETE`) are audited.
49    #[get(type(copy))]
50    pub(crate) mutations_only: bool,
51    /// Optional prefix allowlist; empty audits all non-excluded paths.
52    pub(crate) include_prefixes: Vec<String>,
53    /// Path prefixes excluded from audit recording (for example `"/healthz"`, `"/metrics"`).
54    pub(crate) exclude_prefixes: Vec<String>,
55    /// Request header names copied into each [`AuditRecord`].
56    pub(crate) capture_headers: Vec<String>,
57    /// Sensitive header names whose values are always replaced with `[REDACTED]`.
58    pub(crate) redact_headers: Vec<String>,
59    /// Header name used to extract correlation request ID.
60    pub(crate) request_id_header: String,
61    /// Default ring-buffer capacity when initialized with [`AuditPlugin::init`].
62    #[get(type(copy))]
63    pub(crate) in_memory_capacity: usize,
64}
65
66/// Compiled tamper-evident audit engine backing [`AuditRequestHook`] and [`AuditResponseHook`].
67pub struct AuditPlugin {
68    /// Retained configuration.
69    pub(crate) config: AuditConfig,
70    /// Normalized include prefixes.
71    pub(crate) include_prefixes: Box<[String]>,
72    /// Normalized exclude prefixes.
73    pub(crate) exclude_prefixes: Box<[String]>,
74    /// Lowercased capture headers.
75    pub(crate) capture_headers_lower: Box<[String]>,
76    /// Lowercased redacted header names.
77    pub(crate) redact_headers_lower: Box<[String]>,
78    /// Lowercased request ID header name.
79    pub(crate) request_id_header_lower: String,
80    /// Next sequence number counter.
81    pub(crate) seq: AtomicU64,
82    /// Previous record hash protected by a short critical section.
83    pub(crate) last_hash: Mutex<[u8; 64]>,
84    /// Active sink receiving finalized audit events.
85    pub(crate) sink: Arc<dyn AuditSink>,
86    /// Optional reference to the built-in in-memory sink for direct snapshots.
87    pub(crate) memory_sink: Option<Arc<InMemoryAuditSink>>,
88}
89
90/// Incremental stack-allocated FIPS 180-4 SHA-256 hasher avoiding intermediate string formatting.
91pub(crate) struct AuditSha256 {
92    /// Current 256-bit SHA-256 state words.
93    pub(crate) state: [u32; 8],
94    /// Partial 64-byte input block buffer.
95    pub(crate) block: [u8; 64],
96    /// Number of valid buffered bytes in `block`.
97    pub(crate) block_len: usize,
98    /// Total number of bytes absorbed so far.
99    pub(crate) total_bytes: u64,
100}
101
102/// Opt-in request middleware capturing entry timestamps for audited requests.
103#[derive(Clone, Copy, Debug, Default)]
104#[hyperlane::request_middleware(-118)]
105pub struct AuditRequestHook;
106
107/// Opt-in response middleware emitting hash-chained [`AuditRecord`] events to the configured sink.
108#[derive(Clone, Copy, Debug, Default)]
109#[hyperlane::response_middleware(92)]
110pub struct AuditResponseHook;