Skip to main content

kanade_shared/wire/
server_settings.rs

1use serde::{Deserialize, Serialize};
2
3/// Upper bound on [`ServerSettings::agent_prune_days`] (100 years). A
4/// value this large already means "effectively never", and it keeps the
5/// cleanup task's `now - Duration::days(n)` subtraction comfortably inside
6/// `chrono::DateTime`'s representable range — `DateTime - Duration` panics
7/// on overflow, and an unbounded `u32` (~11.7 M years) would trip it.
8/// Enforced two ways: the PUT handler rejects a larger value, and
9/// [`ServerSettings::effective_agent_prune_days`] clamps to it so even a
10/// hand-written KV value can never panic the cleanup task.
11pub const MAX_AGENT_PRUNE_DAYS: u32 = 36_500;
12
13/// Value stored in the `server_settings` KV bucket under the single key
14/// [`crate::kv::KEY_SERVER_SETTINGS`]. Operator-editable, backend-side
15/// server configuration that isn't per-agent (so it doesn't belong in
16/// `agent_config`'s layered scopes) and isn't a fleet-wide switch every
17/// agent watches (so it doesn't belong in `fleet_config`). Managed via
18/// the SPA Settings page's "server settings" tab.
19///
20/// Every field is `Option<_>`: `None` (the default / the JSON value
21/// `null` / the field simply absent) means **unset — fall back to the
22/// built-in default** ([`ServerSettings::defaults`]), exactly like the
23/// agent layered-config scopes. The SPA renders the built-in default as a
24/// faint placeholder so a blank field shows what it resolves to, and when
25/// a real default is introduced here it appears in the UI (and takes
26/// effect for already-deployed-but-unset fleets) for free.
27///
28/// `#[serde(default)]` on the container keeps the document backward/forward
29/// compatible: a freshly-created or missing key decodes to all-`None`
30/// (pre-feature behaviour), an older backend reading a newer document
31/// ignores unknown fields, and a newer backend reading an older document
32/// fills the missing field with `None`. Keep that invariant — never add a
33/// field whose `None` doesn't mean "behave as before".
34#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
35#[serde(default)]
36pub struct ServerSettings {
37    /// Days a dead agent (one whose heartbeat stopped arriving) may
38    /// linger in the `agents` registry before the backend cleanup task
39    /// prunes its row.
40    ///
41    /// `None` (unset) falls back to the built-in default; with no default
42    /// configured that resolves to pruning **disabled** (see
43    /// [`ServerSettings::effective_agent_prune_days`]). A positive value
44    /// makes the cleanup sweep delete rows whose `last_heartbeat` is older
45    /// than that many days. The `agents` table is a projection of the
46    /// heartbeat stream, so a machine that's merely offline (not gone)
47    /// reappears on its next heartbeat (~30s cadence); only
48    /// genuinely-retired machines stay gone.
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub agent_prune_days: Option<u32>,
51}
52
53impl ServerSettings {
54    /// Built-in defaults applied when a field is unset (`None`) in the
55    /// stored document. Currently every field is `None`: there's no
56    /// fleet-meaningful default prune window, so leaving it blank means
57    /// "disabled" rather than some arbitrary number of days.
58    ///
59    /// Exposed via `GET /api/server-settings/defaults` so the SPA renders
60    /// these as faint placeholders (mirroring the agent layered-config
61    /// page's built-in floor). Introducing a real default later is a
62    /// one-line change here that automatically shows up in the UI and
63    /// applies to every deployment that hasn't overridden the field.
64    pub fn defaults() -> Self {
65        Self {
66            agent_prune_days: None,
67        }
68    }
69
70    /// The effective dead-agent prune window in days: the stored value if
71    /// set, else the built-in default, else `0` (= pruning disabled). The
72    /// final `unwrap_or(0)` is the absent-everywhere floor, not a
73    /// user-facing default — the cleanup task treats `0` as "don't prune".
74    ///
75    /// Clamped to [`MAX_AGENT_PRUNE_DAYS`] so the cleanup task's
76    /// `now - Duration::days(n)` can never overflow `DateTime` (and panic
77    /// the task), even if a value larger than the PUT handler allows was
78    /// written to the KV out-of-band.
79    pub fn effective_agent_prune_days(&self) -> u32 {
80        self.agent_prune_days
81            .or(Self::defaults().agent_prune_days)
82            .unwrap_or(0)
83            .min(MAX_AGENT_PRUNE_DAYS)
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn default_is_unset() {
93        assert_eq!(ServerSettings::default().agent_prune_days, None);
94    }
95
96    #[test]
97    fn unset_resolves_to_disabled() {
98        // No stored value + no built-in default ⇒ effective 0 (disabled).
99        assert_eq!(ServerSettings::default().effective_agent_prune_days(), 0);
100    }
101
102    #[test]
103    fn stored_value_wins_over_default() {
104        let s = ServerSettings {
105            agent_prune_days: Some(30),
106        };
107        assert_eq!(s.effective_agent_prune_days(), 30);
108    }
109
110    #[test]
111    fn effective_clamps_to_max() {
112        // An out-of-band KV write larger than the PUT cap must not reach
113        // the cleanup task unclamped (else its DateTime subtraction panics).
114        let s = ServerSettings {
115            agent_prune_days: Some(u32::MAX),
116        };
117        assert_eq!(s.effective_agent_prune_days(), MAX_AGENT_PRUNE_DAYS);
118    }
119
120    #[test]
121    fn round_trips_through_json() {
122        let s = ServerSettings {
123            agent_prune_days: Some(30),
124        };
125        let json = serde_json::to_string(&s).unwrap();
126        assert_eq!(json, r#"{"agent_prune_days":30}"#);
127        let back: ServerSettings = serde_json::from_str(&json).unwrap();
128        assert_eq!(back, s);
129    }
130
131    #[test]
132    fn unset_serialises_to_empty_object() {
133        // `skip_serializing_if` keeps an all-unset doc minimal; it must
134        // round-trip back to all-`None`.
135        let s = ServerSettings::default();
136        let json = serde_json::to_string(&s).unwrap();
137        assert_eq!(json, "{}");
138        let back: ServerSettings = serde_json::from_str(&json).unwrap();
139        assert_eq!(back, s);
140    }
141
142    #[test]
143    fn explicit_null_decodes_to_unset() {
144        let s: ServerSettings = serde_json::from_str(r#"{"agent_prune_days":null}"#).unwrap();
145        assert_eq!(s.agent_prune_days, None);
146    }
147
148    #[test]
149    fn empty_object_decodes_to_default() {
150        // A freshly-created key (or one written by an older backend that
151        // didn't know this field) must read back as the pre-feature
152        // behaviour, not fail to decode.
153        let s: ServerSettings = serde_json::from_str("{}").unwrap();
154        assert_eq!(s, ServerSettings::default());
155    }
156
157    #[test]
158    fn accepts_unknown_fields_for_forward_compat() {
159        // A newer backend may have added knobs this build doesn't know;
160        // decoding must drop them rather than error.
161        let json = r#"{"agent_prune_days":7,"some_future_knob":true}"#;
162        let s: ServerSettings = serde_json::from_str(json).unwrap();
163        assert_eq!(s.agent_prune_days, Some(7));
164    }
165}