kanade-agent 0.60.0

Windows-side resident daemon for the kanade endpoint-management system. Subscribes to commands.* over NATS, runs scripts, publishes WMI inventory + heartbeats, watches for self-updates
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
//! `support.*` method handlers — the helpdesk unlock gate.
//!
//! - `support.unlock` — redeem an operator-issued code, revealing the calling
//!   OS user's `client.unlock`-scoped jobs for a limited window.
//! - `support.lock` — drop every grant that user holds, now.
//! - `support.status` — what they hold (so a reconnecting client can restore
//!   its banner without re-asking for the code).
//!
//! The grant gates **`jobs.list` only** — it decides what the Client App
//! offers, not what the agent will run (see `ClientHint::unlock`). So this is
//! a UX affordance, not an authorization boundary: the guard rails below
//! (rate limiting, local hashes, audit) exist to keep helpdesk-only buttons
//! from appearing in front of end users, not to protect a capability.
//!
//! Verification is **local**: the argon2id hashes live in the
//! `support_codes` key of the `fleet_config` KV bucket — a projection the
//! backend derives from `server_settings` carrying nothing but the codes — so
//! a desk can still unlock a machine while the backend is down, which is
//! exactly when they are most likely to need to. The agent deliberately does
//! not read `server_settings` itself: that document also holds the installer's
//! NATS token and other operator secrets. (Only while the projection key is
//! absent — a backend that has not published it yet — does it fall back to
//! that legacy document, so the broker cannot deny it until every agent and
//! the backend are upgraded.) The KV is reachable only with
//! the NATS token, which lives under an HKLM key ACL'd to SYSTEM +
//! Administrators, so an ordinary end user cannot read the hashes at all.
//! (A local *administrator* can, and already holds far stronger capabilities
//! than any unlock scope confers — see #1155. This gate is a control on
//! standard users, not on machine admins.)
//!
//! Every attempt is audited to the per-PC observability timeline, success
//! and failure alike: the operator value of this feature is as much "who
//! opened this machine, when, and what did they run" as it is the unlocking.

use kanade_shared::ipc::error::{ErrorKind, RpcError};
use kanade_shared::ipc::support::{
    SupportLockParams, SupportLockResult, SupportStatusParams, SupportStatusResult,
    SupportUnlockParams, SupportUnlockResult,
};
use kanade_shared::kv::{
    BUCKET_FLEET_CONFIG, BUCKET_SERVER_SETTINGS, KEY_SERVER_SETTINGS, KEY_SUPPORT_CODES,
};
use kanade_shared::wire::{ObsEvent, ServerSettings, SupportCode, SupportCodesProjection};
use tracing::{info, warn};

use super::super::connection::ConnectionState;
use super::super::unlock;
use super::system::HandlerResult;
use crate::obs_outbox;

/// `support.unlock` — verify a typed support code and grant its scope.
///
/// Failure modes are deliberately indistinguishable to the caller: a wrong
/// code, a code for a disabled scope, and no codes configured at all every
/// answer the same `Unauthorized`. Telling them apart would let someone at
/// the keyboard enumerate which scopes exist before guessing at one.
pub async fn handle_support_unlock(
    conn: &ConnectionState,
    params: SupportUnlockParams,
) -> HandlerResult<SupportUnlockResult> {
    let sid = conn.peer.user_sid.clone();

    // Rate limit BEFORE touching KV or argon2: a locked-out caller must cost
    // us nothing (argon2 is deliberately expensive, which makes an unbounded
    // attempt loop a local DoS as well as a guessing oracle).
    if let Some(remaining) = unlock::lockout_remaining(&sid) {
        warn!(
            user = %conn.peer.user,
            secs = remaining.as_secs(),
            "support.unlock: refused, caller is rate-limited",
        );
        return Err(RpcError::new(
            ErrorKind::RateLimit,
            format!(
                "too many failed attempts; try again in {} seconds",
                remaining.as_secs().max(1)
            ),
        ));
    }

    let code = params.code.trim().to_string();
    if code.is_empty() {
        // Not counted as a failure: an empty box is a UI slip, not a guess.
        return Err(RpcError::new(
            ErrorKind::InvalidParams,
            "support.unlock: code must not be empty",
        ));
    }

    let client = conn.nats.as_ref().ok_or_else(|| {
        RpcError::new(
            ErrorKind::InternalError,
            "support.unlock: NATS client not wired into the connection",
        )
    })?;
    let codes = read_support_codes(client).await?;

    let usable: Vec<SupportCode> = codes.iter().filter(|c| c.is_usable()).cloned().collect();

    // argon2 is CPU-bound by design (tens of ms per verify). Off the async
    // worker it would stall every other connection's handlers on this
    // thread, so the whole match loop goes to a blocking thread.
    let matched = tokio::task::spawn_blocking(move || match_code(&code, &usable))
        .await
        .map_err(|e| {
            warn!(error = %e, "support.unlock: verify task failed");
            RpcError::new(ErrorKind::InternalError, "support.unlock: verify failed")
        })?;

    let Some(code) = matched else {
        let lockout = unlock::record_failure(&sid);
        warn!(
            user = %conn.peer.user,
            locked_out = lockout.is_some(),
            "support.unlock: rejected an unrecognised code",
        );
        audit(conn, "support_unlock_failed", serde_json::json!({}));
        return Err(RpcError::new(
            ErrorKind::Unauthorized,
            "support.unlock: code not recognised",
        ));
    };

    unlock::clear_failures(&sid);
    let ttl = code.effective_ttl_minutes();
    let grants = unlock::grant(&sid, &code.scope, code.label.clone(), ttl);
    info!(
        user = %conn.peer.user,
        scope = %code.scope,
        ttl_minutes = ttl,
        "support.unlock: granted",
    );
    audit(
        conn,
        "support_unlock",
        serde_json::json!({ "scope": code.scope, "ttl_minutes": ttl, "label": code.label }),
    );
    Ok(SupportUnlockResult { grants })
}

/// `support.lock` — drop every grant the caller holds. Never an error: the
/// desk pressing "終了" on an already-lapsed session should see it close, not
/// see a failure.
pub fn handle_support_lock(
    conn: &ConnectionState,
    _params: SupportLockParams,
) -> HandlerResult<SupportLockResult> {
    let released = unlock::lock(&conn.peer.user_sid);
    if released > 0 {
        info!(user = %conn.peer.user, released, "support.lock: grants released");
        audit(
            conn,
            "support_lock",
            serde_json::json!({ "released": released }),
        );
    }
    Ok(SupportLockResult { released })
}

/// `support.status` — the caller's live grants (empty ⇒ locked). Read-only
/// and unaudited: it reports state the caller already caused.
pub fn handle_support_status(
    conn: &ConnectionState,
    _params: SupportStatusParams,
) -> HandlerResult<SupportStatusResult> {
    Ok(SupportStatusResult {
        grants: unlock::grants(&conn.peer.user_sid),
    })
}

/// The first configured code whose hash matches `code`, or `None`.
///
/// Every candidate is verified even after a match, so the work done is a
/// function of how many codes are configured and not of which one was typed
/// — a timing side channel here would leak scope ordering to someone who can
/// measure it, which is anyone sitting at the machine.
fn match_code(code: &str, codes: &[SupportCode]) -> Option<SupportCode> {
    use argon2::{Argon2, PasswordHash, PasswordVerifier};

    let mut hit: Option<SupportCode> = None;
    for candidate in codes {
        let parsed = match PasswordHash::new(&candidate.hash) {
            Ok(p) => p,
            Err(e) => {
                // A malformed stored hash can never match anything, so this
                // fails closed on its own — but it also means a scope the
                // operator believes is configured silently opens for nobody,
                // which is worth a log line.
                warn!(scope = %candidate.scope, error = %e, "support code hash is unparseable");
                continue;
            }
        };
        if Argon2::default()
            .verify_password(code.as_bytes(), &parsed)
            .is_ok()
            && hit.is_none()
        {
            hit = Some(candidate.clone());
        }
    }
    hit
}

/// Read the configured support codes. A missing projection *and* a missing
/// legacy document both mean "never configured" (no codes ⇒ nothing
/// unlockable); a read or decode failure is surfaced so the desk sees
/// "couldn't check" rather than an indistinguishable "wrong code".
async fn read_support_codes(client: &async_nats::Client) -> HandlerResult<Vec<SupportCode>> {
    let js = async_nats::jetstream::new(client.clone());
    let projection = read_projection_bytes(&js).await;
    select_codes(projection, || read_legacy_codes(&js)).await
}

/// Choose the code source from what the projection read produced.
///
/// The legacy `server_settings` read happens **only** when the projection key
/// is strictly absent: an unreadable or corrupt projection is an outage to
/// report, not a licence to reach for the broader document, and a present
/// projection — even an empty one — is the backend's authoritative answer.
async fn select_codes<F, Fut>(
    projection: HandlerResult<Option<Vec<u8>>>,
    legacy: F,
) -> HandlerResult<Vec<SupportCode>>
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = HandlerResult<Vec<SupportCode>>>,
{
    match projection? {
        Some(bytes) => serde_json::from_slice::<SupportCodesProjection>(&bytes)
            .map(|p| p.support_codes)
            .map_err(|e| {
                warn!(error = %e, "support.unlock: decode support_codes projection");
                RpcError::new(
                    ErrorKind::InternalError,
                    "support.unlock: server settings are corrupt",
                )
            }),
        None => legacy().await,
    }
}

/// `Ok(None)` ⇒ the projection key does not exist.
async fn read_projection_bytes(
    js: &async_nats::jetstream::Context,
) -> HandlerResult<Option<Vec<u8>>> {
    let kv = js.get_key_value(BUCKET_FLEET_CONFIG).await.map_err(|e| {
        warn!(error = %e, "support.unlock: open fleet_config bucket");
        RpcError::new(
            ErrorKind::InternalError,
            "support.unlock: server settings unavailable",
        )
    })?;
    match kv.get(KEY_SUPPORT_CODES).await {
        Ok(v) => Ok(v.map(|b| b.to_vec())),
        Err(e) => {
            warn!(error = %e, "support.unlock: read support_codes projection");
            Err(RpcError::new(
                ErrorKind::InternalError,
                "support.unlock: server settings unavailable",
            ))
        }
    }
}

/// Pre-projection source, kept only for a backend that has not published the
/// projection yet. Same failure semantics the agent always had.
async fn read_legacy_codes(js: &async_nats::jetstream::Context) -> HandlerResult<Vec<SupportCode>> {
    let kv = js
        .get_key_value(BUCKET_SERVER_SETTINGS)
        .await
        .map_err(|e| {
            warn!(error = %e, "support.unlock: open server_settings bucket");
            RpcError::new(
                ErrorKind::InternalError,
                "support.unlock: server settings unavailable",
            )
        })?;
    match kv.get(KEY_SERVER_SETTINGS).await {
        Ok(Some(bytes)) => serde_json::from_slice::<ServerSettings>(&bytes)
            .map(|s| s.support_codes)
            .map_err(|e| {
                warn!(error = %e, "support.unlock: decode server_settings");
                RpcError::new(
                    ErrorKind::InternalError,
                    "support.unlock: server settings are corrupt",
                )
            }),
        Ok(None) => Ok(Vec::new()),
        Err(e) => {
            warn!(error = %e, "support.unlock: read server_settings");
            Err(RpcError::new(
                ErrorKind::InternalError,
                "support.unlock: server settings unavailable",
            ))
        }
    }
}

/// Queue one audit event onto the per-PC observability timeline.
///
/// Best-effort and fire-and-forget: the outbox makes delivery durable across
/// a broker outage, but a failure to *queue* must not fail the unlock itself
/// — refusing to open a machine because an audit file couldn't be written
/// would turn a disk hiccup into "the helpdesk can't help you".
///
/// `payload` never carries the code, hashed or otherwise. It records who
/// (SID + friendly name), which scope, and how long — the questions an
/// operator reviewing the timeline actually asks.
fn audit(conn: &ConnectionState, kind: &str, mut payload: serde_json::Value) {
    if let Some(obj) = payload.as_object_mut() {
        obj.insert("user".into(), conn.peer.user.clone().into());
        obj.insert("user_sid".into(), conn.peer.user_sid.clone().into());
    }
    let event = ObsEvent {
        pc_id: conn.pc_id.clone(),
        at: chrono::Utc::now(),
        kind: kind.to_string(),
        source: "agent:support".to_string(),
        // Every attempt is its own event — nothing here should ever dedup
        // (two identical unlocks a minute apart are two facts, not one).
        event_record_id: Some(format!("support_{}", uuid::Uuid::new_v4().simple())),
        payload,
    };
    let dir = obs_outbox::default_dir();
    let res = obs_outbox::ensure_outbox_dir(&dir)
        .and_then(|()| obs_outbox::enqueue(&dir, &event).map(|_| ()));
    if let Err(e) = res {
        warn!(error = %e, kind, "failed to queue support audit event");
    }
}

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

    fn hash_of(code: &str) -> String {
        use argon2::password_hash::{PasswordHasher, SaltString, rand_core::OsRng};
        let salt = SaltString::generate(&mut OsRng);
        argon2::Argon2::default()
            .hash_password(code.as_bytes(), &salt)
            .unwrap()
            .to_string()
    }

    fn code(scope: &str, plain: &str) -> SupportCode {
        SupportCode {
            scope: scope.into(),
            hash: hash_of(plain),
            label: None,
            ttl_minutes: None,
            disabled: false,
        }
    }

    #[test]
    fn matches_the_right_scope() {
        let codes = vec![code("support", "hunter2"), code("admin", "correct-horse")];
        assert_eq!(match_code("hunter2", &codes).unwrap().scope, "support");
        assert_eq!(match_code("correct-horse", &codes).unwrap().scope, "admin");
    }

    #[test]
    fn rejects_a_wrong_code() {
        let codes = vec![code("support", "hunter2")];
        assert!(match_code("hunter3", &codes).is_none());
        assert!(match_code("", &codes).is_none());
    }

    #[test]
    fn no_configured_codes_matches_nothing() {
        // The fresh-deployment state: every `client.unlock` job stays hidden
        // from everyone until an operator sets a code.
        assert!(match_code("anything", &[]).is_none());
    }

    #[test]
    fn skips_an_unparseable_hash_without_matching() {
        // A hand-corrupted (or API-redacted, i.e. blank) hash must fail
        // closed rather than panic or match. `is_usable()` already filters
        // blanks upstream; this covers the garbage case.
        let codes = vec![SupportCode {
            scope: "broken".into(),
            hash: "not-a-phc-string".into(),
            ..Default::default()
        }];
        assert!(match_code("not-a-phc-string", &codes).is_none());
        assert!(match_code("anything", &codes).is_none());
    }

    #[test]
    fn a_later_scope_still_matches_after_an_earlier_miss() {
        // Guards the "verify every candidate" loop against an early return
        // sneaking back in and shadowing scopes past the first.
        let codes = vec![code("a", "aaa"), code("b", "bbb"), code("c", "ccc")];
        assert_eq!(match_code("ccc", &codes).unwrap().scope, "c");
    }

    use std::sync::atomic::{AtomicUsize, Ordering};

    fn projection_bytes(codes: Vec<SupportCode>) -> Vec<u8> {
        serde_json::to_vec(&SupportCodesProjection {
            support_codes: codes,
        })
        .unwrap()
    }

    /// Run `select_codes` and report how many times the legacy source was hit.
    async fn select(
        projection: HandlerResult<Option<Vec<u8>>>,
        legacy: HandlerResult<Vec<SupportCode>>,
    ) -> (HandlerResult<Vec<SupportCode>>, usize) {
        let hits = AtomicUsize::new(0);
        let out = select_codes(projection, || async {
            hits.fetch_add(1, Ordering::SeqCst);
            legacy
        })
        .await;
        (out, hits.load(Ordering::SeqCst))
    }

    fn unavailable() -> RpcError {
        RpcError::new(ErrorKind::InternalError, "unavailable")
    }

    /// Select, then run the same filter + match the handler does.
    async fn unlock_with(typed: &str, projection: Vec<SupportCode>) -> Option<String> {
        let (codes, _) = select(Ok(Some(projection_bytes(projection))), Err(unavailable())).await;
        let usable: Vec<SupportCode> = codes
            .unwrap()
            .into_iter()
            .filter(|c| c.is_usable())
            .collect();
        match_code(typed, &usable).map(|c| c.scope)
    }

    #[tokio::test]
    async fn projection_unlocks_with_the_right_code_and_refuses_a_wrong_one() {
        let codes = vec![code("support", "hunter2")];
        assert_eq!(
            unlock_with("hunter2", codes.clone()).await.as_deref(),
            Some("support")
        );
        assert_eq!(unlock_with("hunter3", codes).await, None);
    }

    #[tokio::test]
    async fn a_disabled_scope_does_not_unlock() {
        let mut c = code("support", "hunter2");
        c.disabled = true;
        assert_eq!(unlock_with("hunter2", vec![c]).await, None);
    }

    #[tokio::test]
    async fn present_projection_never_touches_the_legacy_document() {
        let (out, hits) = select(
            Ok(Some(projection_bytes(vec![code("a", "aaa")]))),
            Ok(vec![code("old", "old")]),
        )
        .await;
        assert_eq!(out.unwrap()[0].scope, "a");
        assert_eq!(hits, 0);
    }

    #[tokio::test]
    async fn empty_projection_is_authoritative() {
        // Cleared codes must not resurrect from the legacy document.
        let (out, hits) = select(
            Ok(Some(projection_bytes(vec![]))),
            Ok(vec![code("old", "o")]),
        )
        .await;
        assert!(out.unwrap().is_empty());
        assert_eq!(hits, 0);
    }

    #[tokio::test]
    async fn unreadable_projection_is_unavailable_without_fallback() {
        let (out, hits) = select(Err(unavailable()), Ok(vec![code("old", "o")])).await;
        assert!(out.is_err());
        assert_eq!(hits, 0);
    }

    #[tokio::test]
    async fn corrupt_projection_is_surfaced_without_fallback() {
        let (out, hits) = select(Ok(Some(b"{not json".to_vec())), Ok(vec![code("old", "o")])).await;
        assert!(out.is_err());
        assert_eq!(hits, 0);
    }

    #[tokio::test]
    async fn absent_projection_falls_back_to_the_legacy_document() {
        let (out, hits) = select(Ok(None), Ok(vec![code("old", "o")])).await;
        assert_eq!(out.unwrap()[0].scope, "old");
        assert_eq!(hits, 1);
        // The legacy source keeps its own failure semantics.
        let (out, hits) = select(Ok(None), Err(unavailable())).await;
        assert!(out.is_err());
        assert_eq!(hits, 1);
    }
}