sysknife-daemon 0.10.0

Policy-enforced, audit-logged Linux action daemon for SysKnife
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
//! Storage backend abstraction for the audit log.
//!
//! Two backends ship in the box:
//!
//! - [`SqliteStore`] — `rusqlite`-backed, single-file local database. The
//!   default for dev/test; explicitly **not recommended** for production
//!   because the audit log dies with the host (no off-box durability,
//!   nothing to forward to a SOC).
//! - [`PostgresStore`] — `sqlx`-backed, native async, TLS via rustls +
//!   webpki-roots. Wire-compatible with AWS RDS / Aurora, GCP Cloud SQL +
//!   AlloyDB, Azure Database for PostgreSQL Flexible Server, Supabase,
//!   Neon, and self-hosted PostgreSQL.
//!
//! The dispatcher does not know which backend is active. It calls into
//! [`AuditStore`] (an async trait) and the runtime resolves to the configured
//! impl through `state.audit: Arc<dyn AuditStore + Send + Sync>`.
//!
//! ## Cloud provider quick reference
//!
//! See `docs/storage-cloud.md` for the full table. Summary:
//!
//! These three rows said `require` until they were checked against the code.
//! `pg_tls::require_tls_for_remote` REFUSES `sslmode=require` on any connection
//! that crosses the network, because `require` completes TLS against any
//! certificate and so authenticates nothing. An operator following the old table
//! got a hard startup refusal, and the obvious way to "fix" that is to loosen the
//! check — which reopens the MITM hole the check exists to close. Supabase and
//! Neon both publish CA chains, so `verify-full` is what they support and what
//! this daemon accepts.
//!
//! | Provider             | URL hint                                        | TLS mode      | Statement cache |
//! |----------------------|-------------------------------------------------|---------------|-----------------|
//! | AWS RDS / Aurora     | `*.rds.amazonaws.com:5432`                      | `verify-full` | default         |
//! | GCP Cloud SQL        | via Cloud SQL Auth Proxy on `127.0.0.1`         | `disable`     | default         |
//! | Azure Flexible       | `*.postgres.database.azure.com:5432`            | `verify-full` | default         |
//! | Supabase (5432)      | `db.<ref>.supabase.co:5432`                     | `verify-full` | default         |
//! | Supabase (pooler)    | `*.pooler.supabase.com:6543`                    | `verify-full` | **0**           |
//! | Neon                 | `ep-*.neon.tech`                                | `verify-full` | default         |
//! | Self-hosted          | (operator chooses)                              | as needed     | default         |

use async_trait::async_trait;
use std::sync::Arc;
use sysknife_types::{JobState, PreviewEnvelope, TransactionRecord};

use crate::audit_chain::{AuditKey, ChainRow, EventRow, VerifyOutcome};
use crate::transactions::{
    NewTransaction, RecordedPreviewedTransaction, TransactionStore, TransactionStoreError,
};

pub mod postgres;

/// Async, polymorphic interface to the audit log. Implemented by
/// [`SqliteStore`] (rusqlite, blocking under the hood) and
/// [`postgres::PostgresStore`] (sqlx, native async).
///
/// All methods on this trait correspond 1:1 to methods on
/// [`TransactionStore`] for the SQLite path; the Postgres path implements
/// the same semantics over a different SQL dialect.
#[async_trait]
pub trait AuditStore: Send + Sync + std::fmt::Debug {
    async fn record(
        &self,
        transaction: NewTransaction,
    ) -> Result<TransactionRecord, TransactionStoreError>;

    async fn record_previewed(
        &self,
        transaction: NewTransaction,
        preview: PreviewEnvelope,
    ) -> Result<RecordedPreviewedTransaction, TransactionStoreError>;

    async fn get(
        &self,
        transaction_id: &str,
    ) -> Result<Option<TransactionRecord>, TransactionStoreError>;

    async fn get_preview(
        &self,
        transaction_id: &str,
    ) -> Result<Option<PreviewEnvelope>, TransactionStoreError>;

    async fn update_status(
        &self,
        transaction_id: &str,
        new_status: JobState,
    ) -> Result<(), TransactionStoreError>;

    async fn approve_transaction(
        &self,
        transaction_id: &str,
    ) -> Result<Option<String>, TransactionStoreError>;

    async fn revoke_unconsumed_approval(
        &self,
        transaction_id: &str,
    ) -> Result<bool, TransactionStoreError>;

    async fn claim_approved_for_execution(
        &self,
        transaction_id: &str,
        receipt_digest: &str,
    ) -> Result<bool, TransactionStoreError>;

    async fn cleanup_stale_queued(&self) -> Result<u64, TransactionStoreError>;

    /// Reconcile transactions orphaned by a daemon crash or restart.
    ///
    /// A row still in `Running` means the daemon was mid-execution of that
    /// action when it exited. On the next start the in-flight work is orphaned
    /// and its true outcome is unknown, so each such row is transitioned
    /// `Running → Failed` (the conservative outcome — the operator must
    /// re-check) and the transition is recorded in the audit chain. Intended to
    /// run exactly once at startup, before the daemon begins accepting
    /// connections, so clients never observe a phantom in-flight row.
    ///
    /// Returns the number of transactions reconciled. The default implementation
    /// is backend-agnostic: it pages through `Running` rows via
    /// [`list_transactions`](Self::list_transactions) and applies
    /// [`update_status`](Self::update_status), both of which every backend
    /// already implements with identical semantics.
    async fn reconcile_interrupted_running(&self) -> Result<u64, TransactionStoreError> {
        let mut total = 0u64;
        loop {
            let running = self
                .list_transactions(100, Some("running"), None, None)
                .await?;
            if running.is_empty() {
                break;
            }
            let batch = running.len();
            for tx in running {
                self.update_status(&tx.transaction_id, JobState::Failed)
                    .await?;
            }
            total += batch as u64;
            // A short page means we drained every remaining Running row.
            if batch < 100 {
                break;
            }
        }
        Ok(total)
    }

    /// Cancel one still-`Queued` transaction (`Queued → Canceled`). Returns
    /// `true` iff a queued row was transitioned; a `Running` (in-flight) or
    /// terminal transaction is never cancelled. See
    /// [`crate::transactions::TransactionStore::cancel_queued`].
    async fn cancel_queued(&self, transaction_id: &str) -> Result<bool, TransactionStoreError>;

    async fn list_transactions(
        &self,
        limit: u32,
        status_filter: Option<&str>,
        action_filter: Option<&str>,
        since_hours: Option<u32>,
    ) -> Result<Vec<TransactionRecord>, TransactionStoreError>;

    /// Structured history rows (with `created_at` and typed `risk_level`) for
    /// programmatic clients. See [`crate::transactions::JobHistoryEntry`].
    async fn list_history(
        &self,
        limit: u32,
        status_filter: Option<&str>,
        action_filter: Option<&str>,
        since_hours: Option<u32>,
    ) -> Result<Vec<crate::transactions::JobHistoryEntry>, TransactionStoreError>;

    async fn fetch_chain_row(
        &self,
        transaction_id: &str,
    ) -> Result<Option<ChainRow>, TransactionStoreError>;

    async fn fetch_chain_rows(&self) -> Result<Vec<ChainRow>, TransactionStoreError>;

    /// Every approval event, in seq order. Backs the second chain that records
    /// grant/consume/revoke — see `audit_chain::AuditEventKind`.
    async fn fetch_event_rows(&self) -> Result<Vec<EventRow>, TransactionStoreError>;

    async fn verify_audit_chain(
        &self,
        key: &AuditKey,
    ) -> Result<VerifyOutcome, TransactionStoreError>;

    /// Walk the approval-event chain.
    ///
    /// Provided rather than required: it is the same signature walk over rows
    /// the backend has already fetched, so a backend that implements
    /// `fetch_event_rows` correctly cannot get this wrong, and an override
    /// would be a second place for the two chains' verification to drift.
    async fn verify_event_chain(
        &self,
        key: &AuditKey,
    ) -> Result<VerifyOutcome, TransactionStoreError> {
        let rows = self.fetch_event_rows().await?;
        Ok(crate::audit_chain::verify_event_chain(key, &rows))
    }

    /// Check that every event tip committed by a transaction row still exists
    /// in the event chain. See `audit_chain::verify_event_binding`.
    async fn verify_event_binding(
        &self,
    ) -> Result<crate::audit_chain::BindingOutcome, TransactionStoreError> {
        let tx_rows = self.fetch_chain_rows().await?;
        let event_rows = self.fetch_event_rows().await?;
        Ok(crate::audit_chain::verify_event_binding(
            &tx_rows,
            &event_rows,
        ))
    }
}

/// Adapter that exposes the existing rusqlite-backed [`TransactionStore`]
/// through the async [`AuditStore`] trait. Each method runs the underlying
/// blocking work on `tokio::task::spawn_blocking` so the executor stays
/// responsive even when the SQLite write takes a millisecond on a slow disk.
#[derive(Debug, Clone)]
pub struct SqliteStore {
    inner: Arc<TransactionStore>,
}

impl SqliteStore {
    pub fn new(inner: TransactionStore) -> Self {
        Self {
            inner: Arc::new(inner),
        }
    }

    /// Borrow the wrapped sync `TransactionStore`. Used by call sites that
    /// genuinely need the blocking handle (test helpers, the audit-verify
    /// CLI subcommand which is itself sync over the result).
    pub fn inner(&self) -> &TransactionStore {
        &self.inner
    }
}

/// Run a blocking closure on the runtime's blocking thread pool.
///
/// `spawn_blocking` returns a `JoinHandle` that resolves to the closure's
/// output; we panic-propagate JoinErrors because they only happen when the
/// runtime is shutting down — at which point the daemon is also shutting
/// down and there's no audit log to write to anyway.
async fn blocking<F, R>(f: F) -> R
where
    F: FnOnce() -> R + Send + 'static,
    R: Send + 'static,
{
    tokio::task::spawn_blocking(f)
        .await
        .expect("audit-store blocking task panicked")
}

#[async_trait]
impl AuditStore for SqliteStore {
    async fn record(
        &self,
        transaction: NewTransaction,
    ) -> Result<TransactionRecord, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        blocking(move || inner.record(transaction)).await
    }

    async fn record_previewed(
        &self,
        transaction: NewTransaction,
        preview: PreviewEnvelope,
    ) -> Result<RecordedPreviewedTransaction, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        blocking(move || inner.record_previewed(transaction, preview)).await
    }

    async fn get(
        &self,
        transaction_id: &str,
    ) -> Result<Option<TransactionRecord>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        blocking(move || inner.get(&id)).await
    }

    async fn get_preview(
        &self,
        transaction_id: &str,
    ) -> Result<Option<PreviewEnvelope>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        blocking(move || inner.get_preview(&id)).await
    }

    async fn update_status(
        &self,
        transaction_id: &str,
        new_status: JobState,
    ) -> Result<(), TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        blocking(move || inner.update_status(&id, new_status)).await
    }

    async fn approve_transaction(
        &self,
        transaction_id: &str,
    ) -> Result<Option<String>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        blocking(move || inner.approve_transaction(&id)).await
    }

    async fn revoke_unconsumed_approval(
        &self,
        transaction_id: &str,
    ) -> Result<bool, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        blocking(move || inner.revoke_unconsumed_approval(&id)).await
    }

    async fn claim_approved_for_execution(
        &self,
        transaction_id: &str,
        receipt_digest: &str,
    ) -> Result<bool, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        let digest = receipt_digest.to_string();
        blocking(move || inner.claim_approved_for_execution(&id, &digest)).await
    }

    async fn cleanup_stale_queued(&self) -> Result<u64, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        blocking(move || inner.cleanup_stale_queued()).await
    }

    async fn cancel_queued(&self, transaction_id: &str) -> Result<bool, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        blocking(move || inner.cancel_queued(&id)).await
    }

    async fn list_transactions(
        &self,
        limit: u32,
        status_filter: Option<&str>,
        action_filter: Option<&str>,
        since_hours: Option<u32>,
    ) -> Result<Vec<TransactionRecord>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let status = status_filter.map(str::to_string);
        let action = action_filter.map(str::to_string);
        blocking(move || {
            inner.list_transactions(limit, status.as_deref(), action.as_deref(), since_hours)
        })
        .await
    }

    async fn list_history(
        &self,
        limit: u32,
        status_filter: Option<&str>,
        action_filter: Option<&str>,
        since_hours: Option<u32>,
    ) -> Result<Vec<crate::transactions::JobHistoryEntry>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let status = status_filter.map(str::to_string);
        let action = action_filter.map(str::to_string);
        blocking(move || {
            inner.list_history(limit, status.as_deref(), action.as_deref(), since_hours)
        })
        .await
    }

    async fn fetch_chain_row(
        &self,
        transaction_id: &str,
    ) -> Result<Option<ChainRow>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        let id = transaction_id.to_string();
        blocking(move || inner.fetch_chain_row(&id)).await
    }

    async fn fetch_chain_rows(&self) -> Result<Vec<ChainRow>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        blocking(move || inner.fetch_chain_rows()).await
    }

    async fn fetch_event_rows(&self) -> Result<Vec<EventRow>, TransactionStoreError> {
        let inner = Arc::clone(&self.inner);
        blocking(move || inner.fetch_event_rows()).await
    }

    async fn verify_audit_chain(
        &self,
        key: &AuditKey,
    ) -> Result<VerifyOutcome, TransactionStoreError> {
        // `key` is borrowed from the caller; we can't move it into a
        // `'static` `spawn_blocking` closure. Verifying is a CPU-only signature
        // walk over rows already fetched, so doing the work on the async
        // thread (after the blocking fetch) is fine — no I/O risk.
        let rows = self.fetch_chain_rows().await?;
        Ok(crate::audit_chain::verify_chain(key, &rows))
    }
}

// Re-export the postgres impl so call sites can `use store::PostgresStore`.
pub use postgres::PostgresStore;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::audit_chain::AuditKey;
    use crate::auth::CallerPrincipal;
    use crate::transactions::NewTransaction;
    use sysknife_types::{CallerRole, RiskLevel};
    use tempfile::tempdir;

    fn sqlite_store(path: std::path::PathBuf) -> SqliteStore {
        let key = Arc::new(AuditKey::from_bytes(vec![0x42; 32]));
        SqliteStore::new(crate::transactions::TransactionStore::open_with_key(path, key).unwrap())
    }

    fn new_tx(request_id: &str) -> NewTransaction {
        NewTransaction {
            request_id: request_id.to_string(),
            request_hash: format!("hash-{request_id}"),
            action_name: "UpdateSystem".to_string(),
            risk_level: RiskLevel::High,
            summary: "Upgrade the system".to_string(),
            warnings: vec![],
            caller_role: CallerRole::Dev,
            caller_principal: CallerPrincipal::Uid(1000),
        }
    }

    #[tokio::test]
    async fn reconcile_interrupted_running_fails_only_running_rows() {
        let dir = tempdir().unwrap();
        let store = sqlite_store(dir.path().join("tx.db"));

        // A row left Running by a crash, plus two rows in terminal/other states
        // that reconciliation must not touch.
        let running = store.record(new_tx("running")).await.unwrap();
        store
            .update_status(&running.transaction_id, JobState::Running)
            .await
            .unwrap();

        let queued = store.record(new_tx("queued")).await.unwrap();

        let succeeded = store.record(new_tx("succeeded")).await.unwrap();
        store
            .update_status(&succeeded.transaction_id, JobState::Running)
            .await
            .unwrap();
        store
            .update_status(&succeeded.transaction_id, JobState::Succeeded)
            .await
            .unwrap();

        let reconciled = store.reconcile_interrupted_running().await.unwrap();
        assert_eq!(reconciled, 1, "only the one Running row is reconciled");

        assert_eq!(
            store
                .get(&running.transaction_id)
                .await
                .unwrap()
                .unwrap()
                .status,
            JobState::Failed,
            "orphaned Running row must become Failed"
        );
        assert_eq!(
            store
                .get(&queued.transaction_id)
                .await
                .unwrap()
                .unwrap()
                .status,
            JobState::Queued,
            "Queued row is untouched"
        );
        assert_eq!(
            store
                .get(&succeeded.transaction_id)
                .await
                .unwrap()
                .unwrap()
                .status,
            JobState::Succeeded,
            "terminal Succeeded row is untouched"
        );

        // Idempotent: a second pass finds nothing left to reconcile.
        assert_eq!(store.reconcile_interrupted_running().await.unwrap(), 0);
    }
}