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
pub mod agent_config;
pub mod agent_groups;
pub mod agent_logs;
pub mod agent_releases;
pub mod agents;
pub mod app_packages;
pub mod audit;
pub mod exec;
pub mod executions;
pub mod fleet_perf;
pub mod health;
pub mod host_perf;
pub mod inventory;
pub mod jetstream_status;
pub mod jobs;
pub mod process_perf;
pub mod results;
pub mod run;
pub mod schedules;
pub mod schemas;
pub mod scripts;
pub mod yaml_body;
use axum::Router;
use axum::extract::{DefaultBodyLimit, FromRef};
use axum::routing::{delete, get, post};
use sqlx::SqlitePool;
/// 64 MB upper bound for `POST /api/agents/publish` multipart bodies.
/// kanade-agent.exe is ~13 MB on Windows; 64 MB leaves headroom for
/// debug builds and future on-disk growth without becoming a DoS vector.
const PUBLISH_BODY_LIMIT: usize = 64 * 1024 * 1024;
/// 256 MB upper bound for `POST /api/app-packages/{name}/{version}`.
/// Bigger than `PUBLISH_BODY_LIMIT` because app packages cover
/// third-party installers (Webex / Teams / Office plug-ins) whose
/// MSI bundles routinely run 100-200 MB. If a fleet ships larger
/// payloads (eg. multi-architecture installer bundles), the
/// streaming refactor in `app_packages::download` notes the
/// upgrade path.
const APP_PACKAGE_BODY_LIMIT: usize = 256 * 1024 * 1024;
#[derive(Clone)]
pub struct AppState {
pub pool: SqlitePool,
pub nats: async_nats::Client,
pub jetstream: async_nats::jetstream::Context,
/// v0.35 / #88: explode-spec lookup cache, kept fresh by a KV
/// `watch_all()` on BUCKET_JOBS. The /inventory/.../search/...
/// hot path hits this instead of a NATS round-trip per request.
/// `Clone` is cheap (Arc).
pub explode_spec_cache: crate::projector::spec_cache::ExplodeSpecCache,
}
impl FromRef<AppState> for SqlitePool {
fn from_ref(state: &AppState) -> Self {
state.pool.clone()
}
}
pub fn router(state: AppState) -> Router {
Router::new()
.route("/health", get(health))
.route("/api/agents", get(agents::list))
.route("/api/agents/{pc_id}", get(agents::detail))
// v0.40 Part 1: per-PC host-wide perf time-series. Bucketed
// server-side via `?from=&to=&step=` so the SPA chart can
// feed the response directly into Recharts without further
// down-sampling.
.route("/api/agents/{pc_id}/perf", get(host_perf::perf))
// v0.41 / Phase 3: fleet-wide perf aggregates. Three sibling
// endpoints driving the Dashboard cards — bucketed time-
// series, top-N PC ranking, and a "currently investigating"
// (process_perf-active) list.
.route("/api/perf/fleet", get(fleet_perf::fleet))
.route("/api/perf/top", get(fleet_perf::top))
.route(
"/api/perf/active-investigations",
get(fleet_perf::active_investigations),
)
// v0.41 / Phase 2: latest top-N per-process snapshot for a
// host that an operator has opted into investigation mode.
// Empty `processes` array + null `latest_at` if process_perf
// was never enabled for this PC (or its samples have aged
// out of the 7-day retention).
.route(
"/api/agents/{pc_id}/processes",
get(process_perf::processes),
)
// v0.42: stacked per-process time-series chart driver. Same
// table as /processes, but bucketed in SQL with the window-
// wide top-N names pinned for stable series colouring.
// Anything outside the top-N collapses into one `other` series.
.route(
"/api/agents/{pc_id}/processes/timeline",
get(process_perf::timeline),
)
.route(
"/api/agents/{pc_id}/groups",
get(agent_groups::list_groups)
.put(agent_groups::set_groups)
.post(agent_groups::add_group),
)
.route(
"/api/agents/{pc_id}/groups/{group}",
delete(agent_groups::remove_group),
)
.route(
"/api/agents/{pc_id}/effective_config",
get(agent_config::effective),
)
.route(
"/api/config",
get(agent_config::get_global).put(agent_config::put_global),
)
.route(
"/api/groups/{name}/config",
get(agent_config::get_group)
.put(agent_config::put_group)
.delete(agent_config::delete_group),
)
.route(
"/api/pcs/{pc_id}/config",
get(agent_config::get_pc)
.put(agent_config::put_pc)
.delete(agent_config::delete_pc),
)
.route("/api/results", get(results::list))
// v0.29 / Issue #19: path param is now `result_id` (was
// `request_id`); pre-v0.29 rows backfilled `result_id = request_id`
// so existing browser-cached deep links still resolve.
.route("/api/results/{result_id}", get(results::detail))
.route("/api/executions", get(executions::list))
.route("/api/executions/{exec_id}", get(executions::detail))
.route("/api/audit", get(audit::list))
.route("/api/exec/{job_id}", post(exec::create))
.route(
"/api/schedules",
get(schedules::list).post(schedules::create),
)
.route("/api/schedules/{id}", delete(schedules::delete))
.route("/api/schedules/{id}/disable", post(schedules::disable))
.route("/api/run", post(run::run))
.route("/api/agents/{pc_id}/ping", post(run::ping))
.route("/api/scripts/status", get(scripts::list_status))
.route("/api/scripts/{cmd_id}/revoke", post(scripts::revoke))
.route("/api/scripts/{cmd_id}/unrevoke", post(scripts::unrevoke))
.route("/api/jobs", get(jobs::list).post(jobs::create))
.route("/api/jobs/{id}", delete(jobs::delete))
.route("/api/jobs/{id}/yaml", get(jobs::get_yaml))
.route("/api/jobs/{job_id}/kill", post(jobs::kill))
.route("/api/schedules/{id}/yaml", get(schedules::get_yaml))
.route("/api/schemas/manifest.json", get(schemas::manifest_schema))
.route("/api/schemas/schedule.json", get(schemas::schedule_schema))
.route("/api/jetstream/status", get(jetstream_status::status))
.route("/api/health/fleet", get(health::fleet))
// v0.37 / agent perf: per-job duration aggregates
// (p50 / p95 / p99) over a recent window. Pure SQL over the
// existing execution_results.{started,finished}_at — no
// agent-side instrumentation needed.
.route("/api/health/scan_durations", get(health::scan_durations))
.route("/api/inventory/jobs", get(inventory::list_jobs))
.route(
"/api/inventory/by-job/{manifest_id}",
get(inventory::list_for_job),
)
// v0.31 / #40: cross-PC search over a derived `explode`
// table. `{field}` is the JSON array key, validated against
// the manifest's explode spec.
.route(
"/api/inventory/{manifest_id}/search/{field}",
get(inventory::search),
)
// v0.31 / #41: per-PC inventory history timeline.
.route(
"/api/inventory/{manifest_id}/history/pc/{pc_id}",
get(inventory::history_for_pc),
)
// v0.35 / #90: fleet-wide history search across PCs. Same
// response shape as /history/pc/{pc_id}; query string
// carries optional `field`, `kind`, `since`, `until`,
// `identity.<key>=<value>` filters plus `limit` / `offset`.
// Each row's `pc_id` is what distinguishes it from the
// per-PC variant.
.route(
"/api/inventory/{manifest_id}/history/search",
get(inventory::fleet_history_search),
)
// v0.35 / #91: first_seen-per-PC aggregation. Returns one
// row per matching PC with the earliest observed_at of any
// matching event — operator buckets the result by date
// client-side to draw the rollout-curve chart.
.route(
"/api/inventory/{manifest_id}/history/first_seen",
get(inventory::first_seen),
)
.route("/api/inventory/{pc_id}", get(inventory::list_for_pc))
.route("/api/agents/{pc_id}/logs", get(agent_logs::tail))
.route("/api/agents/releases", get(agent_releases::list_releases))
.route(
"/api/agents/releases/{version}",
delete(agent_releases::delete_release),
)
.route("/api/agents/rollout", post(agent_releases::rollout))
.route(
"/api/agents/publish",
post(agent_releases::publish).layer(DefaultBodyLimit::max(PUBLISH_BODY_LIMIT)),
)
// Generic app-package distribution (kanade-client today;
// third-party installers like Webex / Teams next). Distinct
// from `agent_releases` so the lifecycles + audit channels
// don't overlap — see `kanade-shared::kv::OBJECT_APP_PACKAGES`
// for the rationale.
.route("/api/app-packages", get(app_packages::list_packages))
.route(
"/api/app-packages/{name}/{version}",
get(app_packages::download)
.post(app_packages::publish)
.delete(app_packages::delete_package)
.layer(DefaultBodyLimit::max(APP_PACKAGE_BODY_LIMIT)),
)
.with_state(state)
// Everything else (`/`, `/assets/...`, hash-router paths) is served
// from the rust-embed bundle. The fallback runs after the API routes
// above, so JSON endpoints take precedence.
.fallback(crate::web::serve)
}
async fn health() -> &'static str {
"ok"
}