kanade-backend 0.44.35

axum + SQLite projection backend for the kanade endpoint-management system. Hosts /api/* and the embedded SPA dashboard, projects JetStream streams into SQLite, drives the cron scheduler
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
//! Fleet-wide perf aggregates (v0.41 / Phase 3).
//!
//! Three endpoints, all driven off the existing `host_perf_samples`
//! and `process_perf_samples` time-series:
//!
//! * `GET /api/perf/fleet` — bucketed AVG / MAX of one metric across
//!   every PC in the fleet. Drives the Dashboard sparkline card.
//! * `GET /api/perf/top` — top-N PCs ranked by one metric averaged
//!   over a recent window. Drives the Dashboard "Top-5" cards.
//! * `GET /api/perf/active-investigations` — PCs currently
//!   publishing process_perf samples (i.e. an operator has them in
//!   investigation mode right now). Drives the "currently
//!   investigating" card so a forgotten ON toggle is visible at a
//!   glance.

use std::str::FromStr;

use axum::Json;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use sqlx::{Row, SqlitePool};
use tracing::warn;

use super::time_bounds::bounds_in_range;

/// One-hour default window mirrors `/api/agents/{pc_id}/perf`. The
/// Dashboard's "24h sparkline" sends `from=` explicitly so it
/// doesn't ride this default — the default is there for curl
/// inspection convenience.
const DEFAULT_WINDOW_SECS: i64 = 60 * 60;
/// 5 min bucket — same default as the per-PC endpoint.
const DEFAULT_STEP_SECS: i64 = 5 * 60;
/// Same hard ceiling as `/api/agents/{pc_id}/perf` to keep a
/// runaway `from=1y&step=1s` query from generating millions of
/// rows. Fleet aggregation is one row per bucket regardless of PC
/// count, so the same ceiling is conservatively safe.
const MAX_BUCKETS: i64 = 10_000;

/// Top-N defaults. 5 min is wide enough that a host that just sent
/// one stale sample doesn't dominate, narrow enough that the
/// ranking reflects "what's happening right now". 5 entries is the
/// Dashboard card's display budget.
const DEFAULT_TOP_WINDOW_SECS: i64 = 5 * 60;
const DEFAULT_TOP_LIMIT: i64 = 5;
const MAX_TOP_LIMIT: i64 = 50;

/// 5 min look-back for "active investigations" — the agent publishes
/// process_perf at the host_perf_interval cadence (default 60 s), so
/// 5 min covers ~5 consecutive ticks. A PC that genuinely stopped
/// publishing 5 min ago is no longer investigating from the
/// operator's standpoint.
const ACTIVE_INVESTIGATION_WINDOW_SECS: i64 = 5 * 60;

/// Whitelist of metric column names the API will splice into the
/// bucket query. Spliced as a literal (not bound) because SQLite
/// can't parameterise column identifiers; the enum-style match
/// keeps that splice injection-safe.
#[derive(Clone, Copy, Debug)]
enum Metric {
    CpuPct,
    MemUsedBytes,
    DiskReadBytesPerSec,
    DiskWrittenBytesPerSec,
    NetRxBytesPerSec,
    NetTxBytesPerSec,
}

impl Metric {
    fn column(self) -> &'static str {
        match self {
            Self::CpuPct => "cpu_pct",
            Self::MemUsedBytes => "mem_used_bytes",
            Self::DiskReadBytesPerSec => "disk_read_bytes_per_sec",
            Self::DiskWrittenBytesPerSec => "disk_written_bytes_per_sec",
            Self::NetRxBytesPerSec => "net_rx_bytes_per_sec",
            Self::NetTxBytesPerSec => "net_tx_bytes_per_sec",
        }
    }
}

impl FromStr for Metric {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "cpu_pct" | "cpu" => Ok(Self::CpuPct),
            "mem_used_bytes" | "mem" | "memory" => Ok(Self::MemUsedBytes),
            "disk_read_bytes_per_sec" | "disk_read" => Ok(Self::DiskReadBytesPerSec),
            "disk_written_bytes_per_sec" | "disk_written" => Ok(Self::DiskWrittenBytesPerSec),
            "net_rx_bytes_per_sec" | "net_rx" => Ok(Self::NetRxBytesPerSec),
            "net_tx_bytes_per_sec" | "net_tx" => Ok(Self::NetTxBytesPerSec),
            _ => Err(()),
        }
    }
}

#[derive(Clone, Copy, Debug)]
enum Aggregate {
    Avg,
    Max,
}

impl Aggregate {
    fn sql(self) -> &'static str {
        match self {
            Self::Avg => "AVG",
            Self::Max => "MAX",
        }
    }
}

impl FromStr for Aggregate {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "avg" | "mean" => Ok(Self::Avg),
            "max" => Ok(Self::Max),
            _ => Err(()),
        }
    }
}

// ----- /api/perf/fleet -----

#[derive(Deserialize)]
pub struct FleetPerfQuery {
    metric: Option<String>,
    agg: Option<String>,
    from: Option<DateTime<Utc>>,
    to: Option<DateTime<Utc>>,
    step: Option<String>,
}

#[derive(Serialize)]
pub struct FleetPerfPoint {
    pub at: DateTime<Utc>,
    pub value: Option<f64>,
}

#[derive(Serialize)]
pub struct FleetPerfResponse {
    pub metric: String,
    pub agg: String,
    pub from: DateTime<Utc>,
    pub to: DateTime<Utc>,
    pub step_seconds: i64,
    pub points: Vec<FleetPerfPoint>,
}

pub async fn fleet(
    State(pool): State<SqlitePool>,
    Query(q): Query<FleetPerfQuery>,
) -> Result<Json<FleetPerfResponse>, StatusCode> {
    let metric = Metric::from_str(q.metric.as_deref().unwrap_or("cpu_pct"))
        .map_err(|_| StatusCode::BAD_REQUEST)?;
    let agg = Aggregate::from_str(q.agg.as_deref().unwrap_or("avg"))
        .map_err(|_| StatusCode::BAD_REQUEST)?;
    // Issue #1126: `from`/`to` gate the string-stored `at` column
    // byte-wise; an expanded-year bound would invert the window. Reject
    // before defaults are applied. (The MAX_BUCKETS ceiling below only
    // catches wide windows, not a narrow one anchored at a huge year.)
    if !bounds_in_range([q.from, q.to]) {
        return Err(StatusCode::BAD_REQUEST);
    }
    let to = q.to.unwrap_or_else(Utc::now);
    let from = q
        .from
        .unwrap_or_else(|| to - Duration::seconds(DEFAULT_WINDOW_SECS));
    let step_secs = match q.step.as_deref() {
        None => DEFAULT_STEP_SECS,
        Some(raw) => match humantime::parse_duration(raw) {
            Ok(d) => i64::try_from(d.as_secs()).unwrap_or(DEFAULT_STEP_SECS),
            Err(_) => return Err(StatusCode::BAD_REQUEST),
        },
    };
    if step_secs <= 0 || from >= to {
        return Err(StatusCode::BAD_REQUEST);
    }
    if (to - from).num_seconds() / step_secs > MAX_BUCKETS {
        return Err(StatusCode::BAD_REQUEST);
    }

    // `metric.column()` and `agg.sql()` are spliced as literals
    // because SQLite can't parameterise column / function names.
    // Both come from a closed enum match above, so the splice is
    // injection-safe.
    let sql = format!(
        "SELECT
             (CAST(strftime('%s', at) AS INTEGER) / ?) * ? AS bucket_unix,
             {agg}({metric}) AS value
         FROM host_perf_samples
         WHERE at >= ? AND at < ?
         GROUP BY bucket_unix
         ORDER BY bucket_unix ASC",
        agg = agg.sql(),
        metric = metric.column(),
    );

    let rows = sqlx::query(sqlx::AssertSqlSafe(sql))
        .bind(step_secs)
        .bind(step_secs)
        .bind(from)
        .bind(to)
        .fetch_all(&pool)
        .await
        .map_err(|e| {
            warn!(error = %e, metric = ?metric, "fleet perf query");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    let points = rows
        .into_iter()
        .map(|r| {
            let bucket: i64 = r.try_get("bucket_unix").unwrap_or(0);
            FleetPerfPoint {
                at: DateTime::<Utc>::from_timestamp(bucket, 0).unwrap_or(from),
                value: r.try_get("value").ok(),
            }
        })
        .collect();

    Ok(Json(FleetPerfResponse {
        metric: metric.column().to_string(),
        agg: match agg {
            Aggregate::Avg => "avg".into(),
            Aggregate::Max => "max".into(),
        },
        from,
        to,
        step_seconds: step_secs,
        points,
    }))
}

// ----- /api/perf/top -----

#[derive(Deserialize)]
pub struct TopPerfQuery {
    metric: Option<String>,
    window: Option<String>,
    limit: Option<i64>,
}

#[derive(Serialize)]
pub struct TopPerfRow {
    pub pc_id: String,
    pub hostname: Option<String>,
    pub value: f64,
}

#[derive(Serialize)]
pub struct TopPerfResponse {
    pub metric: String,
    pub window_seconds: i64,
    pub rows: Vec<TopPerfRow>,
}

pub async fn top(
    State(pool): State<SqlitePool>,
    Query(q): Query<TopPerfQuery>,
) -> Result<Json<TopPerfResponse>, StatusCode> {
    let metric = Metric::from_str(q.metric.as_deref().unwrap_or("cpu_pct"))
        .map_err(|_| StatusCode::BAD_REQUEST)?;
    let window_secs = match q.window.as_deref() {
        None => DEFAULT_TOP_WINDOW_SECS,
        Some(raw) => match humantime::parse_duration(raw) {
            Ok(d) => i64::try_from(d.as_secs()).unwrap_or(DEFAULT_TOP_WINDOW_SECS),
            Err(_) => return Err(StatusCode::BAD_REQUEST),
        },
    };
    if window_secs <= 0 {
        return Err(StatusCode::BAD_REQUEST);
    }
    let limit = q.limit.unwrap_or(DEFAULT_TOP_LIMIT).clamp(1, MAX_TOP_LIMIT);

    // LEFT JOIN agents so hostnames render alongside pc_id where
    // available — falls back to NULL for hosts that haven't sent a
    // heartbeat yet. Window cutoff via `at > datetime('now', '-Ns')`
    // is a literal because SQLite's relative-time format wants the
    // sign inside the modifier.
    let from = Utc::now() - Duration::seconds(window_secs);
    let sql = format!(
        "SELECT h.pc_id,
                a.hostname AS hostname,
                AVG(h.{metric}) AS value
         FROM host_perf_samples h
         LEFT JOIN agents a ON a.pc_id = h.pc_id
         WHERE h.at > ?
           AND h.{metric} IS NOT NULL
         GROUP BY h.pc_id
         ORDER BY value DESC NULLS LAST
         LIMIT ?",
        metric = metric.column(),
    );

    let rows = sqlx::query(sqlx::AssertSqlSafe(sql))
        .bind(from)
        .bind(limit)
        .fetch_all(&pool)
        .await
        .map_err(|e| {
            warn!(error = %e, metric = ?metric, "top perf query");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    let rows = rows
        .into_iter()
        .map(|r| TopPerfRow {
            pc_id: r.try_get("pc_id").unwrap_or_default(),
            hostname: r.try_get("hostname").ok(),
            value: r.try_get("value").unwrap_or(0.0),
        })
        .collect();

    Ok(Json(TopPerfResponse {
        metric: metric.column().to_string(),
        window_seconds: window_secs,
        rows,
    }))
}

// ----- /api/perf/active-investigations -----

#[derive(Serialize)]
pub struct ActiveInvestigation {
    pub pc_id: String,
    pub hostname: Option<String>,
    pub latest_at: DateTime<Utc>,
}

#[derive(Serialize)]
pub struct ActiveInvestigationsResponse {
    pub window_seconds: i64,
    pub rows: Vec<ActiveInvestigation>,
}

pub async fn active_investigations(
    State(pool): State<SqlitePool>,
) -> Result<Json<ActiveInvestigationsResponse>, StatusCode> {
    let from = Utc::now() - Duration::seconds(ACTIVE_INVESTIGATION_WINDOW_SECS);
    // A PC is "actively investigating" if it has published any
    // process_perf sample in the last window. Aggregating MAX(at)
    // also surfaces the freshest sample so the SPA can show "last
    // sample N s ago" alongside the badge.
    let rows = sqlx::query(
        "SELECT p.pc_id, a.hostname AS hostname, MAX(p.at) AS latest_at
         FROM process_perf_samples p
         LEFT JOIN agents a ON a.pc_id = p.pc_id
         WHERE p.at > ?
         GROUP BY p.pc_id
         ORDER BY latest_at DESC",
    )
    .bind(from)
    .fetch_all(&pool)
    .await
    .map_err(|e| {
        warn!(error = %e, "active_investigations query");
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    let rows = rows
        .into_iter()
        .filter_map(|r| {
            let pc_id: String = r.try_get("pc_id").ok()?;
            let latest_at: DateTime<Utc> = r.try_get("latest_at").ok()?;
            Some(ActiveInvestigation {
                pc_id,
                hostname: r.try_get("hostname").ok(),
                latest_at,
            })
        })
        .collect();

    Ok(Json(ActiveInvestigationsResponse {
        window_seconds: ACTIVE_INVESTIGATION_WINDOW_SECS,
        rows,
    }))
}

#[cfg(test)]
mod tests {
    use chrono::TimeZone;

    use super::*;

    fn bounds_query(from: Option<DateTime<Utc>>, to: Option<DateTime<Utc>>) -> FleetPerfQuery {
        FleetPerfQuery {
            metric: None,
            agg: None,
            from,
            to,
            step: None,
        }
    }

    // A bare `host_perf_samples` so the in-range control query runs for
    // real against a live DB — the string-compared `at` gate and the
    // strftime bucketing only execute there.
    async fn seeded_pool() -> SqlitePool {
        let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
        sqlx::query(
            "CREATE TABLE host_perf_samples ( \
               id INTEGER PRIMARY KEY AUTOINCREMENT, pc_id TEXT NOT NULL, \
               at TIMESTAMP NOT NULL, cpu_pct REAL )",
        )
        .execute(&pool)
        .await
        .unwrap();
        sqlx::query("INSERT INTO host_perf_samples (pc_id, at, cpu_pct) VALUES (?, ?, ?)")
            .bind("p1")
            .bind(Utc.with_ymd_and_hms(2026, 6, 17, 10, 0, 0).unwrap())
            .bind(50.0_f64)
            .execute(&pool)
            .await
            .unwrap();
        pool
    }

    // Issue #1126: an expanded-year *upper* bound is the silently-wrong
    // case here — a too-large `from` is already caught by the `from >= to`
    // ordering check, but a year-10000 `to` sorts below every stored row
    // and used to answer an empty window as `200 OK`. It must now 400. (An
    // empty pool suffices: the guard fires before the query, so neutralising
    // it would fall through to a table-missing 500 — a clean mutation signal.)
    #[tokio::test]
    async fn fleet_rejects_expanded_year_to_bound() {
        let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
        let q = bounds_query(
            None,
            Some(Utc.with_ymd_and_hms(10000, 1, 1, 0, 0, 0).unwrap()),
        );
        let res = fleet(State(pool), Query(q)).await;
        assert!(matches!(res, Err(StatusCode::BAD_REQUEST)));
    }

    // Control: an ordinary window straddling the seeded row is accepted and
    // returns it — proves the guard doesn't reject legitimate bounds.
    #[tokio::test]
    async fn fleet_accepts_in_range_bounds() {
        let pool = seeded_pool().await;
        // A narrow window straddling the 10:00 row (wide windows trip the
        // MAX_BUCKETS ceiling, an unrelated 400).
        let q = bounds_query(
            Some(Utc.with_ymd_and_hms(2026, 6, 17, 9, 0, 0).unwrap()),
            Some(Utc.with_ymd_and_hms(2026, 6, 17, 11, 0, 0).unwrap()),
        );
        let res = fleet(State(pool), Query(q))
            .await
            .expect("in-range bounds must be accepted");
        assert_eq!(res.0.points.len(), 1);
    }
}