engenho-control-server 0.53.119

engenho-control-server — serves engenho's control API (spec/engenho-control.openapi.yaml): the local Unix socket with kernel peer credentials, routing every request through the spec's operation catalog to one EngenhoControl implementation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! The audit chain.
//!
//! Every call at mutate authority or above, every call whose response is
//! sensitive, and every refusal is recorded — a call that changes something
//! twice: its intent before it runs and its result after, so a crash between
//! the two leaves the intent standing alone rather than nothing.
//!
//! Records are the spec's own [`AuditRecord`], one JSON line each, appended
//! to `audit.jsonl` and fsync'd before the call proceeds. Each carries the
//! BLAKE3 of the line before it (`prev`), so an edited, removed or reordered
//! line breaks the chain at that record ([`verify_chain`]). Parameters are
//! never written, only their BLAKE3 — a secret passed to a call does not
//! land in the log.

use std::collections::VecDeque;
use std::io::{BufRead, Write};
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, PoisonError};

use engenho_control_types::types::{AuditPhase, AuditRecord, Blake3Hex, PrincipalView, Replay};
use engenho_control_types::wire::HttpParts;
use engenho_control_types::{AuthorityTier, OperationId, Principal};

/// Where calls are recorded.
pub trait Audit: Send + Sync {
    /// Whether a call is recorded before and after it runs: mutate authority
    /// or above, or a sensitive response. Refusals are always recorded.
    fn wants(&self, by: &Principal, id: OperationId) -> bool {
        let _ = by;
        let row = id.spec();
        row.tier >= AuthorityTier::Mutate || row.sensitive
    }

    /// Record one entry.
    fn record(&self, entry: AuditEntry);
}

/// Records nothing (tests, and callers that audit elsewhere).
#[derive(Debug, Default, Clone, Copy)]
pub struct NoAudit;

impl Audit for NoAudit {
    fn wants(&self, _: &Principal, _: OperationId) -> bool {
        false
    }

    fn record(&self, _: AuditEntry) {}
}

/// One entry, before the chain gives it a sequence number and a link.
#[derive(Debug, Clone, PartialEq)]
pub struct AuditEntry {
    principal: PrincipalView,
    operation: OperationId,
    phase: AuditPhase,
    params_blake3: String,
    http_status: Option<u16>,
    confirmation: Option<String>,
}

impl AuditEntry {
    /// A call about to run.
    #[must_use]
    pub fn intent(by: &Principal, id: OperationId, parts: &HttpParts) -> Self {
        let confirmation = parts.header("engenho-confirmation").map(str::to_owned);
        Self {
            principal: by.view(),
            operation: id,
            phase: AuditPhase::Intent,
            params_blake3: params_digest(parts),
            http_status: None,
            confirmation,
        }
    }

    /// The same call, finished with `status`.
    #[must_use]
    pub fn result(self, status: u16) -> Self {
        Self {
            phase: AuditPhase::Result,
            http_status: Some(status),
            ..self
        }
    }

    /// A call refused before it ran.
    #[must_use]
    pub fn refused(by: &Principal, id: OperationId, status: u16) -> Self {
        Self {
            principal: by.view(),
            operation: id,
            phase: AuditPhase::Refused,
            params_blake3: blake3::hash(b"").to_hex().to_string(),
            http_status: Some(status),
            confirmation: None,
        }
    }
}

/// The BLAKE3 of a call's parameters: its path parameters, query and body,
/// canonically serialized. Headers are left out (they carry the caller's
/// declarations, recorded in the principal).
fn params_digest(parts: &HttpParts) -> String {
    let canonical = serde_json::json!({
        "path": parts.path_params,
        "query": parts.query,
        "body": parts.body,
    });
    let bytes = serde_json::to_vec(&canonical).unwrap_or_default();
    blake3::hash(&bytes).to_hex().to_string()
}

/// How many recent records are kept in memory for paging.
pub const RECENT: usize = 1024;

/// The file-backed chain.
#[derive(Debug)]
pub struct AuditLog {
    path: PathBuf,
    state: Mutex<ChainState>,
}

#[derive(Debug)]
struct ChainState {
    file: std::fs::File,
    next_seq: NonZeroU64,
    prev: String,
    recent: VecDeque<AuditRecord>,
}

/// Why the audit log could not be opened.
#[derive(Debug, thiserror::Error)]
#[error("audit log {}: {source}", path.display())]
pub struct AuditOpenError {
    path: PathBuf,
    source: std::io::Error,
}

/// The `prev` of the first record.
const GENESIS: &str = "0000000000000000000000000000000000000000000000000000000000000000";

impl AuditLog {
    /// The file under `dir`.
    pub const FILE: &'static str = "audit.jsonl";

    /// Open (or start) the chain in `dir`, continuing from its last record.
    ///
    /// # Errors
    ///
    /// The directory or the file cannot be created or read.
    pub fn open(dir: &Path) -> Result<Self, AuditOpenError> {
        use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
        let path = dir.join(Self::FILE);
        let err = |source| AuditOpenError {
            path: path.clone(),
            source,
        };
        std::fs::create_dir_all(dir).map_err(err)?;
        std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700)).map_err(err)?;
        let mut next_seq = NonZeroU64::MIN;
        let mut prev = GENESIS.to_string();
        let mut recent = VecDeque::new();
        if let Ok(existing) = std::fs::File::open(&path) {
            for line in std::io::BufReader::new(existing).lines() {
                let line = line.map_err(err)?;
                match serde_json::from_str::<AuditRecord>(&line) {
                    Ok(record) => {
                        next_seq = record.seq.saturating_add(1);
                        prev = blake3::hash(line.as_bytes()).to_hex().to_string();
                        if recent.len() == RECENT {
                            recent.pop_front();
                        }
                        recent.push_back(record);
                    }
                    Err(e) => {
                        tracing::warn!(error = %e, "unreadable audit line; the chain continues after it");
                    }
                }
            }
        }
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .mode(0o600)
            .open(&path)
            .map_err(err)?;
        Ok(Self {
            path,
            state: Mutex::new(ChainState {
                file,
                next_seq,
                prev,
                recent,
            }),
        })
    }

    /// The file.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Records after `after`, at most `limit`, with the cursor to continue
    /// from and whether older records fell out of memory.
    #[must_use]
    pub fn page(&self, after: u64, limit: usize) -> (Vec<AuditRecord>, u64, Replay) {
        let state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        let oldest = state
            .recent
            .front()
            .map_or(state.next_seq.get(), |r| r.seq.get());
        let replay = if after.saturating_add(1) < oldest && after < state.next_seq.get() - 1 {
            Replay::Truncated(oldest.saturating_sub(1))
        } else {
            Replay::Complete
        };
        let records: Vec<AuditRecord> = state
            .recent
            .iter()
            .filter(|r| r.seq.get() > after)
            .take(limit)
            .cloned()
            .collect();
        let next = records.last().map_or(after, |r| r.seq.get());
        (records, next, replay)
    }
}

impl Audit for AuditLog {
    fn record(&self, entry: AuditEntry) {
        let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        let (Ok(prev), Ok(params)) = (
            Blake3Hex::try_from(state.prev.as_str()),
            Blake3Hex::try_from(entry.params_blake3.as_str()),
        ) else {
            tracing::error!("an audit digest is not BLAKE3 hex; record dropped");
            return;
        };
        let record = AuditRecord {
            seq: state.next_seq,
            at: chrono::Utc::now(),
            prev,
            principal: entry.principal,
            operation: entry.operation.as_str().to_owned(),
            tier: entry.operation.spec().tier,
            phase: entry.phase,
            params_blake3: params,
            http_status: entry.http_status,
            confirmation: entry
                .confirmation
                .and_then(|c| engenho_control_types::types::ConfirmationId::try_from(c).ok()),
        };
        let Ok(mut line) = serde_json::to_vec(&record) else {
            return;
        };
        let digest = blake3::hash(&line).to_hex().to_string();
        line.push(b'\n');
        if let Err(err) = state
            .file
            .write_all(&line)
            .and_then(|()| state.file.sync_data())
        {
            tracing::error!(error = %err, "cannot append to the audit log");
            return;
        }
        state.prev = digest;
        state.next_seq = state.next_seq.saturating_add(1);
        if state.recent.len() == RECENT {
            state.recent.pop_front();
        }
        state.recent.push_back(record);
    }
}

/// Where a chain is broken.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ChainBreak {
    /// A line is not a record.
    #[error("line {line} is not an audit record")]
    Unreadable {
        /// The 1-based line.
        line: usize,
    },
    /// A record's `prev` is not the BLAKE3 of the line before it.
    #[error("record {seq} does not link to the record before it")]
    Unlinked {
        /// The record's sequence number.
        seq: u64,
    },
    /// A record's sequence number is not one more than the one before.
    #[error("record {seq} is out of sequence")]
    OutOfSequence {
        /// The record's sequence number.
        seq: u64,
    },
}

/// Walk the chain in `path`; the number of records when it is whole.
///
/// # Errors
///
/// The first [`ChainBreak`]; an unreadable file is treated as empty.
pub fn verify_chain(path: &Path) -> Result<u64, ChainBreak> {
    let text = std::fs::read_to_string(path).unwrap_or_default();
    let mut prev = GENESIS.to_string();
    let mut expected = 1u64;
    for (i, line) in text.lines().enumerate() {
        let record: AuditRecord =
            serde_json::from_str(line).map_err(|_| ChainBreak::Unreadable { line: i + 1 })?;
        if record.seq.get() != expected {
            return Err(ChainBreak::OutOfSequence {
                seq: record.seq.get(),
            });
        }
        if record.prev.as_str() != prev {
            return Err(ChainBreak::Unlinked {
                seq: record.seq.get(),
            });
        }
        prev = blake3::hash(line.as_bytes()).to_hex().to_string();
        expected += 1;
    }
    Ok(expected - 1)
}

#[cfg(test)]
mod tests {
    use engenho_control_types::types::{AttestedView, DeclaredView, GrantView};

    use super::*;

    fn by() -> Principal {
        Principal::mint(
            AttestedView::LocalUid {
                uid: 501,
                gid: 20,
                pid: 9,
            },
            DeclaredView::Human,
            GrantView {
                granted: AuthorityTier::Destructive,
                effective: AuthorityTier::Destructive,
            },
        )
    }

    fn write_three(dir: &Path) -> AuditLog {
        let log = AuditLog::open(dir).expect("open");
        let parts = HttpParts::default();
        let intent = AuditEntry::intent(&by(), OperationId::StopRuntime, &parts);
        log.record(intent.clone());
        log.record(intent.result(200));
        log.record(AuditEntry::refused(&by(), OperationId::WipeStore, 403));
        log
    }

    #[test]
    fn the_chain_links_every_record_and_survives_a_reopen() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let log = write_three(tmp.path());
        assert_eq!(verify_chain(log.path()), Ok(3));
        let (records, next, replay) = log.page(0, 10);
        assert_eq!(records.len(), 3);
        assert_eq!(next, 3);
        assert_eq!(replay, Replay::Complete);
        assert_eq!(records[1].phase, AuditPhase::Result);
        assert_eq!(records[1].http_status, Some(200));
        drop(log);

        let reopened = AuditLog::open(tmp.path()).expect("reopen");
        reopened.record(AuditEntry::refused(&by(), OperationId::ReseedPki, 403));
        assert_eq!(verify_chain(reopened.path()), Ok(4));
        let mode = std::os::unix::fs::PermissionsExt::mode(
            &std::fs::metadata(reopened.path())
                .expect("stat")
                .permissions(),
        );
        assert_eq!(mode & 0o777, 0o600);
    }

    #[test]
    fn an_edited_record_breaks_the_chain_where_it_was_edited() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let log = write_three(tmp.path());
        let path = log.path().to_path_buf();
        drop(log);
        let text = std::fs::read_to_string(&path).expect("read");
        let tampered = text.replacen("\"result\"", "\"intent\"", 1);
        assert_ne!(text, tampered);
        std::fs::write(&path, tampered).expect("write");
        // Record 2 was edited, so record 3's link no longer matches it.
        assert_eq!(verify_chain(&path), Err(ChainBreak::Unlinked { seq: 3 }));

        let lines: Vec<&str> = text.lines().collect();
        std::fs::write(&path, format!("{}\n{}\n", lines[0], lines[2])).expect("write");
        assert_eq!(
            verify_chain(&path),
            Err(ChainBreak::OutOfSequence { seq: 3 })
        );
    }

    #[test]
    fn only_mutations_and_sensitive_reads_are_audited() {
        let log = NoAudit;
        assert!(!log.wants(&by(), OperationId::GetRuntime));
        let tmp = tempfile::tempdir().expect("tempdir");
        let chain = AuditLog::open(tmp.path()).expect("open");
        assert!(!chain.wants(&by(), OperationId::GetRuntime));
        assert!(chain.wants(&by(), OperationId::StopRuntime));
        assert!(chain.wants(&by(), OperationId::WipeStore));
    }

    #[test]
    fn parameters_are_hashed_never_written() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let log = AuditLog::open(tmp.path()).expect("open");
        let parts = HttpParts {
            body: Some(serde_json::json!({"value": "s3cr3t-token"})),
            ..HttpParts::default()
        };
        log.record(AuditEntry::intent(
            &by(),
            OperationId::SetConfigLeaf,
            &parts,
        ));
        let text = std::fs::read_to_string(log.path()).expect("read");
        assert!(!text.contains("s3cr3t"), "{text}");
    }
}