Repository navigation
Expand file tree
/
Copy pathhttp.rs
More file actions
570 lines (497 loc) · 19.4 KB
/
Copy pathhttp.rs
File metadata and controls
570 lines (497 loc) · 19.4 KB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! HTTP health endpoints using Axum.
//!
//! Three endpoints with distinct semantics:
//! - `/healthz` — Kubernetes liveness probe. Returns `200 OK` whenever the
//! process is responsive. Intentionally does NOT depend on the database
//! so a transient outage does not cascade into a `CrashLoopBackOff`.
//! - `/readyz` — Kubernetes readiness probe. Reads the cached state
//! published by [`crate::readiness::DatabaseHealthMonitor`] and returns
//! `503 Service Unavailable` when the latest background check failed.
//! Handler latency is sub-millisecond: the database is never pinged from
//! inside the request path, so the response cannot race the kubelet's
//! probe timeout.
//! - `/health` — Alias of `/readyz` for external monitors
//! that conventionally probe `/health`.
use axum::{
Json, Router,
extract::{Request, State},
http::{HeaderMap, StatusCode, header},
middleware::{self, Next},
response::IntoResponse,
routing::get,
};
use metrics_exporter_prometheus::PrometheusHandle;
use serde::Serialize;
use std::sync::Arc;
use tokio::sync::watch;
use crate::persistence::Store;
use crate::readiness::{DatabaseHealthMonitor, HealthError, HealthState};
const STATUS_HEALTHY: &str = "healthy";
const STATUS_UNHEALTHY: &str = "unhealthy";
const DATABASE_INITIALIZING_ERROR: &str = "readiness monitor still initializing";
const DATABASE_UNAVAILABLE_ERROR: &str = "database unavailable";
const DATABASE_TIMEOUT_ERROR: &str = "database health check timed out";
#[derive(Clone)]
struct HealthRouterState {
health: watch::Receiver<HealthState>,
}
/// Per-dependency check entry exposed under `checks` in the JSON payload.
#[derive(Debug, Serialize)]
pub struct DependencyCheck {
/// `"healthy"` or `"unhealthy"`.
pub status: &'static str,
/// Wall-clock time of the latest background ping, when measurable.
#[serde(skip_serializing_if = "Option::is_none")]
pub latency_ms: Option<u64>,
/// Failure detail. Absent on success.
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
/// Aggregated dependency results.
#[derive(Debug, Serialize)]
pub struct HealthChecks {
pub database: DependencyCheck,
}
/// Readiness response payload.
#[derive(Debug, Serialize)]
pub struct HealthResponse {
/// Overall status: `"healthy"` if every dependency is healthy.
pub status: &'static str,
/// Service version.
pub version: &'static str,
/// Per-dependency breakdown.
pub checks: HealthChecks,
}
/// Kubernetes liveness probe — process responsiveness only.
async fn healthz() -> impl IntoResponse {
StatusCode::OK
}
/// Kubernetes readiness probe — reflects the cached background DB state.
async fn readyz(State(state): State<Arc<HealthRouterState>>) -> impl IntoResponse {
render_response(&state.health.borrow())
}
/// Convenience alias of [`readyz`] for monitors that probe `/health`.
async fn health(State(state): State<Arc<HealthRouterState>>) -> impl IntoResponse {
render_response(&state.health.borrow())
}
fn render_response(state: &HealthState) -> (StatusCode, Json<HealthResponse>) {
let database = render_database(state);
let healthy = state.is_healthy();
let response = HealthResponse {
status: if healthy {
STATUS_HEALTHY
} else {
STATUS_UNHEALTHY
},
version: openshell_core::VERSION,
checks: HealthChecks { database },
};
let code = if healthy {
StatusCode::OK
} else {
StatusCode::SERVICE_UNAVAILABLE
};
(code, Json(response))
}
fn render_database(state: &HealthState) -> DependencyCheck {
match state {
HealthState::Initializing => DependencyCheck {
status: STATUS_UNHEALTHY,
latency_ms: None,
error: Some(DATABASE_INITIALIZING_ERROR.to_string()),
},
HealthState::Healthy { latency_ms } => DependencyCheck {
status: STATUS_HEALTHY,
latency_ms: Some(*latency_ms),
error: None,
},
HealthState::Unhealthy(HealthError::Unavailable { latency_ms }) => DependencyCheck {
status: STATUS_UNHEALTHY,
latency_ms: Some(*latency_ms),
error: Some(DATABASE_UNAVAILABLE_ERROR.to_string()),
},
HealthState::Unhealthy(HealthError::Timeout) => DependencyCheck {
status: STATUS_UNHEALTHY,
latency_ms: None,
error: Some(DATABASE_TIMEOUT_ERROR.to_string()),
},
}
}
/// Build the health router by spawning a background [`DatabaseHealthMonitor`]
/// for `store` and wiring its receiver into the handlers.
///
/// Returns immediately so the listener is responsive from t=0. The router's
/// initial state is [`HealthState::Initializing`] — `/readyz` and `/health`
/// will return `503` with a structured `{"checks": {"database": {"status":
/// "initializing"}}}` payload until the background monitor publishes its
/// first real probe outcome (within one [`crate::readiness::DEFAULT_CHECK_INTERVAL`]).
/// The background task continues running detached for the remainder of the
/// runtime.
pub fn health_router(store: Arc<Store>) -> Router {
let monitor = DatabaseHealthMonitor::spawn(store);
health_router_from_receiver(monitor.subscribe())
}
/// Build the health router from an existing monitor receiver.
///
/// Crate-internal: used by [`health_router`] and by tests that drive the
/// `HealthState` directly without spinning up the polling task.
pub fn health_router_from_receiver(receiver: watch::Receiver<HealthState>) -> Router {
let state = Arc::new(HealthRouterState { health: receiver });
Router::new()
.route("/health", get(health))
.route("/healthz", get(healthz))
.route("/readyz", get(readyz))
.with_state(state)
}
/// Create the metrics router for the dedicated metrics port.
pub fn metrics_router(handle: PrometheusHandle) -> Router {
Router::new()
.route("/metrics", get(render_metrics))
.with_state(handle)
}
async fn render_metrics(State(handle): State<PrometheusHandle>) -> impl IntoResponse {
handle.render()
}
/// Create the HTTP router served on the multiplexed gateway port.
pub fn http_router(state: Arc<crate::ServerState>) -> Router {
let mut router = crate::auth::router(state.clone());
if state.config.enable_websocket_tunnel {
router = router.merge(crate::ws_tunnel::router(state.clone()));
}
router.layer(middleware::from_fn_with_state(
state,
sandbox_service_routing_first,
))
}
/// Create the plaintext loopback-only router for browser service endpoints.
///
/// This router intentionally exposes only sandbox service routing. It does not
/// include gRPC, auth, health, metrics, or WebSocket tunnel routes.
pub fn service_http_router(state: Arc<crate::ServerState>) -> Router {
Router::new()
.fallback(sandbox_service_routing_only)
.with_state(state)
}
async fn sandbox_service_routing_first(
State(state): State<Arc<crate::ServerState>>,
req: Request,
next: Next,
) -> impl IntoResponse {
let host = crate::service_routing::service_request_host(&req);
if crate::service_routing::is_sandbox_service_request_with_state(host, &state).await {
return crate::service_routing::proxy_sandbox_service_request(state, req)
.await
.into_response();
}
next.run(req).await.into_response()
}
async fn sandbox_service_routing_only(
State(state): State<Arc<crate::ServerState>>,
req: Request,
) -> impl IntoResponse {
let host = crate::service_routing::service_request_host(&req);
if !crate::service_routing::is_sandbox_service_request_with_state(host, &state).await {
return StatusCode::NOT_FOUND.into_response();
}
if !browser_context_allows_plaintext_service_request(&req) {
crate::service_routing::emit_cross_origin_service_http_rejection(&state, &req);
return crate::service_routing::service_error_response(
StatusCode::FORBIDDEN,
"Cross-origin service request rejected",
);
}
crate::service_routing::proxy_sandbox_service_request(state, req)
.await
.into_response()
}
fn browser_context_allows_plaintext_service_request(req: &Request) -> bool {
if let Some(fetch_site) = header_str(req.headers(), "sec-fetch-site")
&& !matches!(
fetch_site.to_ascii_lowercase().as_str(),
"same-origin" | "none"
)
{
return false;
}
if let Some(origin) = header_str(req.headers(), header::ORIGIN.as_str()) {
let Some(request_origin) = request_origin(req) else {
return false;
};
return parse_origin(origin).is_some_and(|origin| origin == request_origin);
}
if let Some(referer) = header_str(req.headers(), header::REFERER.as_str()) {
let Some(request_origin) = request_origin(req) else {
return false;
};
return parse_origin(referer).is_some_and(|origin| origin == request_origin);
}
true
}
fn header_str<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {
headers.get(name)?.to_str().ok()
}
#[derive(Debug, Eq, PartialEq)]
struct Origin {
scheme: String,
host: String,
port: u16,
}
fn request_origin(req: &Request) -> Option<Origin> {
let host = crate::service_routing::request_host(req)?;
parse_origin_authority("http", host)
}
fn parse_origin(value: &str) -> Option<Origin> {
if value.eq_ignore_ascii_case("null") {
return None;
}
let (scheme, rest) = value.split_once("://")?;
let authority_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
parse_origin_authority(scheme, &rest[..authority_end])
}
fn parse_origin_authority(scheme: &str, authority: &str) -> Option<Origin> {
let scheme = scheme.to_ascii_lowercase();
let default_port = match scheme.as_str() {
"http" => 80,
"https" => 443,
_ => return None,
};
let authority = authority.trim();
if authority.is_empty() || authority.contains('@') {
return None;
}
let (host, port) = split_host_port(authority)?;
let host = normalize_host(host)?;
Some(Origin {
scheme,
host,
port: port.unwrap_or(default_port),
})
}
fn split_host_port(authority: &str) -> Option<(&str, Option<u16>)> {
if let Some(rest) = authority.strip_prefix('[') {
let (host, rest) = rest.split_once(']')?;
let port = if rest.is_empty() {
None
} else {
Some(rest.strip_prefix(':')?.parse().ok()?)
};
return Some((host, port));
}
match authority.rsplit_once(':') {
Some((host, port)) if !port.is_empty() && port.chars().all(|ch| ch.is_ascii_digit()) => {
Some((host, Some(port.parse().ok()?)))
}
Some(_) if authority.matches(':').count() == 1 => None,
_ => Some((authority, None)),
}
}
fn normalize_host(host: &str) -> Option<String> {
let host = host.trim().trim_end_matches('.').to_ascii_lowercase();
(!host.is_empty()).then_some(host)
}
#[cfg(test)]
mod tests {
use super::*;
fn service_request(headers: &[(&str, &str)]) -> Request {
let mut builder = Request::builder()
.uri("/some/path")
.header(header::HOST, "sandbox--web.dev.openshell.localhost:8080");
for (name, value) in headers {
builder = builder.header(*name, *value);
}
builder.body(axum::body::Body::empty()).unwrap()
}
#[test]
fn plaintext_service_browser_context_allows_direct_tools() {
let req = service_request(&[]);
assert!(browser_context_allows_plaintext_service_request(&req));
}
#[test]
fn plaintext_service_browser_context_allows_same_origin_fetch_metadata() {
let req = service_request(&[("sec-fetch-site", "same-origin")]);
assert!(browser_context_allows_plaintext_service_request(&req));
}
#[test]
fn plaintext_service_browser_context_allows_direct_navigation_fetch_metadata() {
let req = service_request(&[("sec-fetch-site", "none")]);
assert!(browser_context_allows_plaintext_service_request(&req));
}
#[test]
fn plaintext_service_browser_context_rejects_cross_site_fetch_metadata() {
let req = service_request(&[("sec-fetch-site", "cross-site")]);
assert!(!browser_context_allows_plaintext_service_request(&req));
}
#[test]
fn plaintext_service_browser_context_rejects_same_site_sibling_requests() {
let req = service_request(&[("sec-fetch-site", "same-site")]);
assert!(!browser_context_allows_plaintext_service_request(&req));
}
#[test]
fn plaintext_service_browser_context_requires_matching_origin() {
let req =
service_request(&[("origin", "http://sandbox--web.dev.openshell.localhost:8080")]);
assert!(browser_context_allows_plaintext_service_request(&req));
let req = service_request(&[(
"origin",
"http://sandbox--other.dev.openshell.localhost:8080",
)]);
assert!(!browser_context_allows_plaintext_service_request(&req));
}
#[test]
fn plaintext_service_browser_context_requires_matching_referer() {
let req = service_request(&[(
"referer",
"http://sandbox--web.dev.openshell.localhost:8080/page",
)]);
assert!(browser_context_allows_plaintext_service_request(&req));
let req = service_request(&[(
"referer",
"http://sandbox--other.dev.openshell.localhost:8080/page",
)]);
assert!(!browser_context_allows_plaintext_service_request(&req));
}
#[test]
fn plaintext_service_browser_context_rejects_mismatched_origin_scheme() {
let req = service_request(&[(
"origin",
"https://sandbox--web.dev.openshell.localhost:8080",
)]);
assert!(!browser_context_allows_plaintext_service_request(&req));
}
}
#[cfg(test)]
mod readiness_tests {
use super::*;
use axum::body::Body;
use http::Request;
use http_body_util::BodyExt;
use tower::ServiceExt;
async fn in_memory_store() -> Arc<Store> {
Arc::new(
Store::connect("sqlite::memory:")
.await
.expect("connect in-memory sqlite store"),
)
}
/// Build a [`health_router`] that has already observed its first probe
/// outcome. Test-only — production code must not block the listener on
/// the first poll (see [`health_router`]).
async fn polled_health_router(store: Arc<Store>) -> Router {
let mut monitor = DatabaseHealthMonitor::spawn(store);
monitor.wait_until_polled().await;
health_router_from_receiver(monitor.subscribe())
}
async fn get(router: Router, path: &str) -> (StatusCode, serde_json::Value) {
let response = router
.oneshot(Request::get(path).body(Body::empty()).unwrap())
.await
.expect("router responds");
let status = response.status();
let bytes = response
.into_body()
.collect()
.await
.expect("collect body")
.to_bytes();
let body = if bytes.is_empty() {
serde_json::Value::Null
} else {
serde_json::from_slice(&bytes).expect("response is valid JSON")
};
(status, body)
}
/// Build a router whose state is driven by a `HealthState` we control,
/// so each handler-shape test can pin the exact mapping under test.
fn router_with_state(state: HealthState) -> Router {
let (_tx, rx) = watch::channel(state);
health_router_from_receiver(rx)
}
#[tokio::test]
async fn healthz_is_minimal_and_does_not_touch_the_database() {
// Liveness must succeed even when the database is unreachable —
// otherwise a transient outage would CrashLoopBackOff the gateway.
let store = in_memory_store().await;
store.close().await;
let (status, body) = get(health_router(store), "/healthz").await;
assert_eq!(status, StatusCode::OK);
assert!(body.is_null(), "healthz must return an empty body");
}
#[tokio::test]
async fn readyz_returns_200_with_healthy_payload_when_db_is_reachable() {
let store = in_memory_store().await;
let (status, body) = get(polled_health_router(store).await, "/readyz").await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body["status"], "healthy");
assert_eq!(body["checks"]["database"]["status"], "healthy");
assert!(
body["checks"]["database"]["latency_ms"].is_number(),
"expected latency_ms in healthy payload"
);
assert!(
body["checks"]["database"]["error"].is_null(),
"healthy payload must omit the error field"
);
}
#[tokio::test]
async fn health_alias_mirrors_readyz_when_db_is_reachable() {
let store = in_memory_store().await;
let (status, body) = get(polled_health_router(store).await, "/health").await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body["status"], "healthy");
assert_eq!(body["checks"]["database"]["status"], "healthy");
}
#[tokio::test]
async fn readyz_returns_503_with_unhealthy_payload_when_db_is_unreachable() {
let store = in_memory_store().await;
store.close().await;
let (status, body) = get(polled_health_router(store).await, "/readyz").await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body["status"], "unhealthy");
assert_eq!(body["checks"]["database"]["status"], "unhealthy");
assert_eq!(
body["checks"]["database"]["error"],
DATABASE_UNAVAILABLE_ERROR
);
}
#[tokio::test]
async fn health_alias_returns_503_when_db_is_unreachable() {
let store = in_memory_store().await;
store.close().await;
let (status, body) = get(polled_health_router(store).await, "/health").await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body["status"], "unhealthy");
assert_eq!(body["checks"]["database"]["status"], "unhealthy");
}
#[tokio::test]
async fn readyz_reports_initializing_state_as_unhealthy_with_explicit_reason() {
let (status, body) = get(router_with_state(HealthState::Initializing), "/readyz").await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body["status"], "unhealthy");
assert_eq!(body["checks"]["database"]["status"], "unhealthy");
assert_eq!(
body["checks"]["database"]["error"],
DATABASE_INITIALIZING_ERROR
);
assert!(
body["checks"]["database"]["latency_ms"].is_null(),
"initializing state has no latency to report yet"
);
}
#[tokio::test]
async fn readyz_renders_timeout_state_with_dedicated_error_string() {
let (status, body) = get(
router_with_state(HealthState::Unhealthy(HealthError::Timeout)),
"/readyz",
)
.await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body["status"], "unhealthy");
assert_eq!(body["checks"]["database"]["error"], DATABASE_TIMEOUT_ERROR);
assert!(
body["checks"]["database"]["latency_ms"].is_null(),
"timeout state has no completed-call latency"
);
}
}