keysink 0.1.4

Local gateway that keeps real AI API keys out of agents (Claude Code, OpenAI, MCP): agents hold revocable virtual keys, real keys stay in an encrypted vault.
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use crate::util;
use serde::Serialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::{Receiver, RecvTimeoutError, SyncSender, TrySendError, sync_channel};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

const MAX_AUDIT_BYTES: u64 = 10 * 1024 * 1024;
/// Rotated generations kept on disk: `.1` (newest) through `.{N}` (oldest).
/// Rotation is the log's only retention mechanism, and rejected requests are
/// audited too — without generations, a flood of unauthenticated requests
/// evicts the forensic trail of real traffic (60MB of history at N=5, plus
/// the live file) in a few minutes at loopback speeds.
const AUDIT_GENERATIONS: usize = 5;
/// Bounded queue depth: under disk-write jitter, prefer dropping audit lines (with a counted warning) over blocking the request path.
const QUEUE_DEPTH: usize = 1024;
/// How long the writer idles before flushing buffered lines: under load,
/// lines batch into the BufWriter (flushed at its 8KB capacity), so one
/// syscall carries many lines; when the queue goes quiet, this bounds how
/// long a `tail -f` style consumer waits for the tail.
const FLUSH_IDLE: Duration = Duration::from_millis(50);
/// Per-fingerprint admission interval for rejection rows: a single probing
/// key leaves a steady, complete trail (one row per second) without being
/// able to rotate the log on its own.
const REJECT_FP_INTERVAL: Duration = Duration::from_secs(1);
/// Global rejection admission per window: bounds floods that randomize the
/// fingerprint (each fake token is a fresh bucket, so the per-fingerprint
/// gate alone cannot cap them).
const REJECT_GLOBAL_WINDOW: Duration = Duration::from_secs(1);
const REJECT_GLOBAL_MAX: usize = 100;
/// Per-fingerprint table bound: randomized-token floods would otherwise grow
/// it without limit; when full it resets (buckets re-arm, the global cap
/// still holds).
const REJECT_FP_TABLE_MAX: usize = 4096;

/// Audit disk writes run on a dedicated thread + bounded channel:
/// - the request path only does try_send, never a synchronous open/write/chmod on a tokio worker;
/// - a single writer naturally eliminates the race where two concurrent requests trigger the rotation rename at the same time.
pub struct Audit {
    writer: Mutex<Option<Writer>>,
    /// Rejection-side admission state (see record_reject): bounded, checked
    /// on the calling thread before the queue.
    rejects: Mutex<RejectGate>,
}

struct RejectGate {
    per_fp: HashMap<String, Instant>,
    window_start: Instant,
    window_count: usize,
    suppressed: u64,
}

impl Default for RejectGate {
    fn default() -> Self {
        Self {
            per_fp: HashMap::new(),
            window_start: Instant::now(),
            window_count: 0,
            suppressed: 0,
        }
    }
}

struct Writer {
    tx: SyncSender<AuditEntry>,
    handle: JoinHandle<()>,
}

#[derive(Serialize)]
pub struct AuditEntry {
    pub ts: String,
    pub rid: String,
    pub route: String,
    pub key_fp: String,
    pub method: String,
    pub path: String,
    pub status: u16,
    pub ms: u64,
    pub bytes_in: usize,
    pub stream: bool,
    pub attempts: u8,
}

impl Audit {
    pub fn disabled() -> Self {
        Self {
            writer: Mutex::new(None),
            rejects: Mutex::new(RejectGate::default()),
        }
    }

    /// Whether rows are being written. The writer is chosen once at startup
    /// (--audit-log or not), so the answer cannot change mid-request; callers
    /// skip `AuditEntry` construction entirely when this is false — its
    /// RFC3339 local-time format plus several String allocations are pure
    /// waste on the default audit-off path. Same lock (and same early-out)
    /// that `record` would take anyway.
    #[inline]
    pub fn is_enabled(&self) -> bool {
        self.writer
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .is_some()
    }

    pub fn new(path: PathBuf) -> Self {
        // mode(0o600) only takes effect at creation; if an existing file has overly broad permissions, warn once at startup.
        if path.exists()
            && let Err(e) = util::ensure_private_file(&path, "audit log")
        {
            tracing::warn!(error = %e, "audit log permissions too wide; consider tightening to 0600");
        }
        let (tx, rx) = sync_channel(QUEUE_DEPTH);
        match std::thread::Builder::new()
            .name("keysink-audit".into())
            .spawn(move || writer_loop(path, rx))
        {
            Ok(handle) => Self {
                writer: Mutex::new(Some(Writer { tx, handle })),
                rejects: Mutex::new(RejectGate::default()),
            },
            Err(e) => {
                tracing::warn!(error = %e, "failed to start audit writer thread; audit disabled for this run");
                Self::disabled()
            }
        }
    }

    /// Records only route/fingerprint/status/latency/byte counts; never headers, bodies, or query.
    pub fn record(&self, entry: AuditEntry) {
        let tx = {
            let guard = self.writer.lock().unwrap_or_else(|e| e.into_inner());
            guard.as_ref().map(|w| w.tx.clone())
        };
        let Some(tx) = tx else {
            return;
        };
        if let Err(TrySendError::Full(_)) = tx.try_send(entry) {
            static DROPPED: AtomicU64 = AtomicU64::new(0);
            let n = DROPPED.fetch_add(1, Ordering::Relaxed) + 1;
            if n % 100 == 1 {
                tracing::warn!(dropped = n, "audit queue full; dropping audit lines");
            }
        }
        // Disconnected (after close) is silent: audit is no longer needed during shutdown.
    }

    /// Audits a rejected request (auth failure, disabled route, body limits,
    /// route configuration errors). Rejection rows are the one audit input an
    /// unauthenticated peer controls, so they pass an admission gate before
    /// the queue: per-fingerprint (one row per `REJECT_FP_INTERVAL`) plus a
    /// global cap per window. Suppressed rows are counted, never silent.
    pub fn record_reject(&self, entry: AuditEntry) {
        let admit = {
            let mut gate = self.rejects.lock().unwrap_or_else(|e| e.into_inner());
            let now = Instant::now();
            if now.duration_since(gate.window_start) >= REJECT_GLOBAL_WINDOW {
                gate.window_start = now;
                gate.window_count = 0;
            }
            let admitted = if gate.window_count >= REJECT_GLOBAL_MAX {
                false
            } else {
                match gate.per_fp.get_mut(&entry.key_fp) {
                    Some(last) => {
                        if now.duration_since(*last) >= REJECT_FP_INTERVAL {
                            *last = now;
                            true
                        } else {
                            false
                        }
                    }
                    None => {
                        if gate.per_fp.len() >= REJECT_FP_TABLE_MAX {
                            gate.per_fp.clear();
                        }
                        gate.per_fp.insert(entry.key_fp.clone(), now);
                        true
                    }
                }
            };
            if admitted {
                gate.window_count += 1;
            } else {
                gate.suppressed += 1;
                let n = gate.suppressed;
                if n % 100 == 1 {
                    tracing::warn!(
                        suppressed = n,
                        global_per_sec = REJECT_GLOBAL_MAX,
                        "rejection audit rows throttled (1/s per fingerprint); a flood can no longer rotate the log away"
                    );
                }
            }
            admitted
        };
        if admit {
            self.record(entry);
        }
    }

    /// On shutdown, drain the queue and wait for the writer thread to exit so tail audit lines are not lost.
    pub fn close(&self) {
        let mut guard = self.writer.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(w) = guard.take() {
            drop(w.tx);
            let _ = w.handle.join();
        }
    }
}

fn writer_loop(path: PathBuf, rx: Receiver<AuditEntry>) {
    // The handle stays open, buffered: lines batch in a BufWriter instead of
    // costing one write syscall each; the buffer flushes at capacity, when
    // the queue goes idle (FLUSH_IDLE), and before rotation or shutdown. On
    // rotation or write failure the handle becomes None and the next record
    // reopens.
    let mut log = OpenLog {
        file: None,
        written: 0,
    };
    loop {
        match rx.recv_timeout(FLUSH_IDLE) {
            Ok(entry) => {
                if let Err(e) = append(&path, &mut log, &entry) {
                    tracing::warn!(error = %e, "failed to write audit log");
                    // Dropping the BufWriter attempts a best-effort flush of
                    // whatever is still buffered; the next record reopens.
                    log.file = None;
                }
            }
            Err(RecvTimeoutError::Timeout) => flush_dirty(&mut log),
            // Disconnected (after close): drain, flush, exit so tail audit lines are not lost.
            Err(RecvTimeoutError::Disconnected) => break,
        }
    }
    flush_dirty(&mut log);
}

fn flush_dirty(log: &mut OpenLog) {
    let Some(writer) = log.file.as_mut() else {
        return;
    };
    if writer.buffer().is_empty() {
        return;
    }
    if let Err(e) = writer.flush() {
        tracing::warn!(error = %e, "failed to flush audit log");
        log.file = None;
    }
}

struct OpenLog {
    file: Option<BufWriter<File>>,
    written: u64,
}

fn append(path: &Path, log: &mut OpenLog, entry: &AuditEntry) -> std::io::Result<()> {
    let mut line = serde_json::to_vec(entry).map_err(std::io::Error::other)?;
    line.push(b'\n');
    if log.file.is_none() {
        rotate_if_needed(path, log)?;
        let file = util::private_open_options(0o600)
            .create(true)
            .append(true)
            .open(path)?;
        log.written = file.metadata()?.len();
        log.file = Some(BufWriter::new(file));
    }
    // Unreachable in practice (the block above just opened it), but the
    // invariant is enforced here instead of asserted with an unwrap.
    let Some(writer) = log.file.as_mut() else {
        return Ok(());
    };
    writer.write_all(&line)?;
    log.written += line.len() as u64;
    if log.written >= MAX_AUDIT_BYTES {
        // Flush before the handle is dropped: the rotation rename happens on
        // the next reopen, and buffered-but-unwritten tail lines must not be
        // left behind in a file that is about to be renamed. (Windows also
        // refuses to rename an open file.)
        writer.flush()?;
        log.file = None;
    }
    Ok(())
}

fn rotate_if_needed(path: &Path, log: &mut OpenLog) -> std::io::Result<()> {
    if log.written < MAX_AUDIT_BYTES {
        return Ok(());
    }
    // Shift the chain one generation older (.N-1 → .N … current → .1),
    // dropping the previous .N. remove-before-rename keeps Windows happy
    // (rename refuses to replace an existing file there); the live file's
    // replacement target (.1) was just vacated by the shift.
    for generation in (2..=AUDIT_GENERATIONS).rev() {
        let from = PathBuf::from(format!("{}.{}", path.display(), generation - 1));
        let to = PathBuf::from(format!("{}.{}", path.display(), generation));
        if from.exists() {
            let _ = std::fs::remove_file(&to);
            std::fs::rename(&from, &to)?;
        }
    }
    let rotated = PathBuf::from(format!("{}.1", path.display()));
    std::fs::rename(path, rotated)?;
    log.written = 0;
    Ok(())
}

impl AuditEntry {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        rid: impl Into<String>,
        route: impl Into<String>,
        key_fp: impl Into<String>,
        method: impl Into<String>,
        path: impl Into<String>,
        bytes_in: usize,
        stream: bool,
    ) -> Self {
        Self {
            ts: util::now_ts(),
            rid: rid.into(),
            route: route.into(),
            key_fp: key_fp.into(),
            method: method.into(),
            path: path.into(),
            status: 0,
            ms: 0,
            bytes_in,
            stream,
            attempts: 1,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_enabled_reflects_the_writer() {
        assert!(!Audit::disabled().is_enabled());
        let dir = tempfile::tempdir().unwrap();
        let audit = Audit::new(dir.path().join("audit.jsonl"));
        assert!(audit.is_enabled());
        audit.close();
        assert!(!audit.is_enabled(), "close() takes the writer out");
    }

    #[test]
    fn writes_one_json_line() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("audit.jsonl");
        let audit = Audit::new(path.clone());
        let mut entry =
            AuditEntry::new("R1", "claude", "abcd1234", "POST", "/v1/messages", 12, true);
        entry.status = 200;
        entry.ms = 42;
        audit.record(entry);
        audit.close(); // assert only after the queue is drained
        let text = std::fs::read_to_string(path).unwrap();
        assert_eq!(text.lines().count(), 1);
        let v: serde_json::Value = serde_json::from_str(text.lines().next().unwrap()).unwrap();
        assert_eq!(v["route"], "claude");
        assert_eq!(v["status"], 200);
        assert!(v.get("query").is_none());
    }

    #[test]
    fn rotates_after_size_limit() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("audit.jsonl");
        std::fs::write(&path, vec![b'x'; MAX_AUDIT_BYTES as usize + 1]).unwrap();
        let audit = Audit::new(path.clone());
        // The first record pushes written past the limit (forcing the next close); the second opens a new file.
        audit.record(AuditEntry::new("R3", "r", "fp", "GET", "/x", 0, false));
        audit.record(AuditEntry::new("R4", "r", "fp", "GET", "/x", 0, false));
        audit.close();

        let rotated = dir.path().join("audit.jsonl.1");
        assert!(rotated.exists());
        assert!(std::fs::metadata(&rotated).unwrap().len() > MAX_AUDIT_BYTES);
        let text = std::fs::read_to_string(&path).unwrap();
        assert_eq!(text.lines().count(), 1);
        assert!(
            text.contains("R4"),
            "new file after rotation should contain only the last entry: {text}"
        );
    }

    #[test]
    fn record_after_close_is_silent_noop() {
        let dir = tempfile::tempdir().unwrap();
        let audit = Audit::new(dir.path().join("audit.jsonl"));
        audit.close();
        audit.record(AuditEntry::new("R2", "r", "fp", "GET", "/x", 0, false));
        assert!(!dir.path().join("audit.jsonl").exists());
    }

    #[test]
    fn rotation_shifts_generations_in_order() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("audit.jsonl");
        std::fs::write(&path, "current").unwrap();
        std::fs::write(dir.path().join("audit.jsonl.1"), "gen1").unwrap();
        std::fs::write(dir.path().join("audit.jsonl.2"), "gen2").unwrap();
        let mut log = OpenLog {
            file: None,
            written: u64::MAX, // force rotation
        };
        rotate_if_needed(&path, &mut log).unwrap();
        assert_eq!(log.written, 0);
        assert_eq!(
            std::fs::read_to_string(dir.path().join("audit.jsonl.1")).unwrap(),
            "current"
        );
        assert_eq!(
            std::fs::read_to_string(dir.path().join("audit.jsonl.2")).unwrap(),
            "gen1"
        );
        assert_eq!(
            std::fs::read_to_string(dir.path().join("audit.jsonl.3")).unwrap(),
            "gen2"
        );
        assert!(!dir.path().join("audit.jsonl.4").exists());
        assert!(!path.exists(), "the live file moves into .1 on rotation");
    }

    #[test]
    fn rotation_drops_the_oldest_generation() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("audit.jsonl");
        std::fs::write(&path, "current").unwrap();
        for generation in 1..=AUDIT_GENERATIONS {
            std::fs::write(
                dir.path().join(format!("audit.jsonl.{generation}")),
                format!("gen{generation}"),
            )
            .unwrap();
        }
        let mut log = OpenLog {
            file: None,
            written: u64::MAX,
        };
        rotate_if_needed(&path, &mut log).unwrap();
        assert_eq!(
            std::fs::read_to_string(dir.path().join("audit.jsonl.5")).unwrap(),
            format!("gen{}", AUDIT_GENERATIONS - 1)
        );
        assert!(
            !dir.path()
                .join(format!("audit.jsonl.{}", AUDIT_GENERATIONS + 1))
                .exists()
        );
    }

    #[test]
    fn rejection_rows_throttle_per_fingerprint_and_globally() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("audit.jsonl");
        let audit = Audit::new(path.clone());
        audit.record_reject(AuditEntry::new("A1", "-", "fpX", "GET", "/x", 0, false));
        audit.record_reject(AuditEntry::new("A2", "-", "fpX", "GET", "/x", 0, false));
        // Unique fingerprints (randomized-token flood): admitted only up to
        // the global window cap, which fpX's first row already counted against.
        for i in 0..150 {
            audit.record_reject(AuditEntry::new(
                format!("U{i}"),
                "-",
                format!("fp{i}"),
                "GET",
                "/x",
                0,
                false,
            ));
        }
        audit.record(AuditEntry::new(
            "F1",
            "claude",
            "fpOK",
            "POST",
            "/v1/messages",
            12,
            true,
        ));
        audit.close();

        let text = std::fs::read_to_string(path).unwrap();
        let count = text.lines().count();
        assert!(text.contains("\"A1\""));
        assert!(
            !text.contains("\"A2\""),
            "same-fingerprint repeat within the interval must be suppressed"
        );
        assert_eq!(
            count,
            1 + REJECT_GLOBAL_MAX,
            "gate admits the window cap plus the forward"
        );
    }
}