ullun-api 0.4.9

Express.js-inspired web framework for Rust - simple, fast, batteries-included
Documentation
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! The status page example, tested over real HTTP against real (local) targets

mod common;

#[allow(dead_code)]
#[path = "../examples/statuspage/app.rs"]
mod statuspage;

use common::{spawn, Server};
use http_body_util::{BodyExt, Empty};
use hyper::body::{Bytes, Incoming};
use hyper_util::rt::TokioIo;
use serde_json::{json, Value};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpListener, TcpStream};
use ullun::prelude::*;

const KEY: &str = "test-admin-key-0123456789";

fn config() -> statuspage::Config {
    statuspage::Config {
        admin_key: KEY.into(),
        allow_private_targets: true,
        // scheduler effectively off: tests trigger checks explicitly
        tick: Duration::from_secs(3600),
        interval_unit: Duration::from_secs(1),
        failure_threshold: 2,
        check_timeout: Duration::from_millis(500),
    }
}

async fn status_app(config: statuspage::Config) -> Server {
    spawn(statuspage::build(config)).await
}

/// A target whose `/health` can be switched between 200 and 503
async fn flaky_target() -> (Server, Arc<AtomicBool>) {
    let healthy = Arc::new(AtomicBool::new(true));
    let flag = healthy.clone();
    let server = spawn(App::new().get("/health", move |_req: Request| {
        let flag = flag.clone();
        async move {
            if flag.load(Ordering::SeqCst) {
                Ok(Response::text("fine"))
            } else {
                Ok(Response::text("broken").with_status(http::StatusCode::SERVICE_UNAVAILABLE))
            }
        }
    }))
    .await;
    (server, healthy)
}

async fn add(s: &Server, body: Value) -> common::Reply {
    s.post("/api/monitors")
        .header("x-api-key", KEY)
        .json(&body)
        .send()
        .await
}

async fn add_ok(s: &Server, name: &str, target: &str) -> u64 {
    let res = add(s, json!({ "name": name, "target": target })).await;
    assert_eq!(res.status, 201, "{}", res.text());
    res.json()["id"].as_u64().unwrap()
}

async fn check(s: &Server, id: u64) -> common::Reply {
    s.post(&format!("/api/monitors/{id}/check"))
        .header("x-api-key", KEY)
        .send()
        .await
}

async fn status_of(s: &Server, id: u64) -> String {
    let st = s.get("/api/status").send().await.json();
    st["monitors"]
        .as_array()
        .unwrap()
        .iter()
        .find(|m| m["id"] == id)
        .map(|m| m["status"].as_str().unwrap().to_string())
        .unwrap_or_default()
}

#[tokio::test]
async fn admin_api_needs_the_key_but_status_is_public() {
    let s = status_app(config()).await;
    for (method, path) in [
        ("GET", "/api/monitors"),
        ("POST", "/api/monitors"),
        ("DELETE", "/api/monitors/1"),
        ("POST", "/api/monitors/1/check"),
        ("GET", "/api/monitors/1/history"),
    ] {
        assert_eq!(
            s.request(method, path).send().await.status,
            401,
            "{method} {path}"
        );
        assert_eq!(
            s.request(method, path)
                .header("x-api-key", "wrong")
                .send()
                .await
                .status,
            401
        );
    }
    for path in ["/", "/api/status", "/healthz", "/app.js", "/openapi.json"] {
        assert_eq!(s.get(path).send().await.status, 200, "{path}");
    }
}

#[tokio::test]
async fn monitor_input_is_validated_all_at_once() {
    let s = status_app(config()).await;

    let res = add(
        &s,
        json!({ "name": "", "target": "ftp://example.com", "interval_secs": 0 }),
    )
    .await;
    assert_eq!(res.status, 422);
    let fields = &res.json()["fields"];
    for f in ["name", "target", "interval_secs"] {
        assert!(fields[f].is_array(), "{f}: {fields}");
    }

    for target in [
        "",
        "example.com",
        "http://",
        "https://example.com",
        "tcp://example.com",
        "javascript:alert(1)",
        "file:///etc/passwd",
    ] {
        let res = add(&s, json!({ "name": "x", "target": target })).await;
        assert_eq!(res.status, 422, "target {target:?}");
        assert!(res.json()["fields"]["target"].is_array());
    }
    let too_slow = add(
        &s,
        json!({ "name": "x", "target": "tcp://127.0.0.1:1", "interval_secs": 3601 }),
    )
    .await;
    assert_eq!(too_slow.status, 422);
    // malformed bodies are a 400, not a crash
    let bad = s
        .post("/api/monitors")
        .header("x-api-key", KEY)
        .text("name=x")
        .send()
        .await;
    assert_eq!(bad.status, 400);
}

#[tokio::test]
async fn private_targets_are_refused_unless_allowed() {
    let strict = status_app(statuspage::Config {
        allow_private_targets: false,
        ..config()
    })
    .await;
    for target in [
        "http://127.0.0.1:8080/",
        "http://localhost/",
        "http://[::1]/",
        "http://10.0.0.5/",
        "http://192.168.1.10/",
        "http://172.16.0.1/",
        "http://169.254.169.254/latest/meta-data/",
        "http://0.0.0.0/",
        "http://100.64.0.1/",
        "http://[::ffff:127.0.0.1]/",
        "http://[fd00::1]/",
        "http://[fe80::1]/",
        "tcp://127.0.0.1:5432",
    ] {
        let res = add(&strict, json!({ "name": "ssrf", "target": target })).await;
        assert_eq!(
            res.status,
            422,
            "{target} should be refused: {}",
            res.text()
        );
        let msg = res.json()["fields"]["target"][0]
            .as_str()
            .unwrap()
            .to_string();
        assert!(
            msg.contains("not public") || msg.contains("resolution"),
            "{target}: {msg}"
        );
    }
    // public literal addresses are fine (nothing is contacted at creation time)
    assert_eq!(
        add(
            &strict,
            json!({ "name": "ok", "target": "tcp://93.184.216.34:443" })
        )
        .await
        .status,
        201
    );

    let relaxed = status_app(config()).await;
    assert_eq!(
        add(
            &relaxed,
            json!({ "name": "local", "target": "tcp://127.0.0.1:1" })
        )
        .await
        .status,
        201
    );
}

#[tokio::test]
async fn http_monitor_goes_degraded_then_down_then_recovers() {
    let (target, healthy) = flaky_target().await;
    let s = status_app(config()).await;
    let id = add_ok(&s, "API", &format!("http://{}/health", target.addr)).await;
    assert_eq!(status_of(&s, id).await, "unknown");
    assert_eq!(
        s.get("/api/status").send().await.json()["overall"],
        "unknown"
    );

    let up = check(&s, id).await.json();
    assert_eq!(
        (up["ok"].as_bool(), up["detail"].as_str()),
        (Some(true), Some("HTTP 200"))
    );
    assert_eq!(status_of(&s, id).await, "up");
    assert_eq!(
        s.get("/api/status").send().await.json()["overall"],
        "operational"
    );

    healthy.store(false, Ordering::SeqCst);
    let failed = check(&s, id).await.json();
    assert_eq!(
        (failed["ok"].as_bool(), failed["detail"].as_str()),
        (Some(false), Some("HTTP 503"))
    );
    // one failure is only a warning ...
    assert_eq!(status_of(&s, id).await, "degraded");
    assert_eq!(
        s.get("/api/status").send().await.json()["overall"],
        "degraded"
    );
    // ... the second in a row makes it an outage
    check(&s, id).await;
    assert_eq!(status_of(&s, id).await, "down");
    assert_eq!(
        s.get("/api/status").send().await.json()["overall"],
        "major_outage"
    );

    healthy.store(true, Ordering::SeqCst);
    check(&s, id).await;
    assert_eq!(status_of(&s, id).await, "up");

    // ok, fail, fail, ok = 2 of 4 = 50 %
    let monitor = &s.get("/api/status").send().await.json()["monitors"][0];
    assert_eq!(monitor["checks"], 4);
    assert_eq!(monitor["uptime_percent"], 50.0);

    let history = s
        .get(&format!("/api/monitors/{id}/history"))
        .header("x-api-key", KEY)
        .send()
        .await
        .json();
    let details: Vec<&str> = history["checks"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["detail"].as_str().unwrap())
        .collect();
    assert_eq!(details, ["HTTP 200", "HTTP 503", "HTTP 503", "HTTP 200"]);
}

#[tokio::test]
async fn public_status_hides_targets_and_admin_list_shows_them() {
    let (target, _) = flaky_target().await;
    let s = status_app(config()).await;
    let url = format!("http://{}/health", target.addr);
    add_ok(&s, "Secret internal API", &url).await;

    let public = s.get("/api/status").send().await.text();
    assert!(public.contains("Secret internal API"));
    assert!(!public.contains(&url) && !public.contains("127.0.0.1"));
    let page = s.get("/").send().await.text();
    assert!(!page.contains("127.0.0.1"));

    let admin = s
        .get("/api/monitors")
        .header("x-api-key", KEY)
        .send()
        .await
        .json();
    assert_eq!(admin["items"][0]["target"], url);
    assert_eq!(admin["items"][0]["interval_secs"], 30);
}

#[tokio::test]
async fn tcp_checks_and_failure_modes() {
    let s = status_app(config()).await;

    // reachable
    let open = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let open_port = open.local_addr().unwrap().port();
    tokio::spawn(async move {
        loop {
            let _ = open.accept().await;
        }
    });
    let id = add_ok(&s, "db", &format!("tcp://127.0.0.1:{open_port}")).await;
    let ok = check(&s, id).await.json();
    assert_eq!(
        (ok["ok"].as_bool(), ok["detail"].as_str()),
        (Some(true), Some("connected"))
    );

    // closed port
    let closed_port = {
        let l = TcpListener::bind("127.0.0.1:0").await.unwrap();
        l.local_addr().unwrap().port()
    };
    let id = add_ok(&s, "gone", &format!("tcp://127.0.0.1:{closed_port}")).await;
    let refused = check(&s, id).await.json();
    assert_eq!(refused["ok"], false);
    // Windows retries refused connections for about a second, which hits the
    // 500 ms check timeout first; both are a correct "unreachable" verdict
    let detail = refused["detail"].as_str().unwrap();
    assert!(
        detail == "connection failed" || detail == "timeout",
        "{detail}"
    );

    // accepts, then never answers HTTP
    let silent = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let silent_port = silent.local_addr().unwrap().port();
    tokio::spawn(async move {
        let mut held = Vec::new();
        loop {
            if let Ok((sock, _)) = silent.accept().await {
                held.push(sock);
            }
        }
    });
    let id = add_ok(&s, "hung", &format!("http://127.0.0.1:{silent_port}/")).await;
    let t = Instant::now();
    let timed_out = check(&s, id).await.json();
    assert_eq!(timed_out["detail"], "timeout");
    assert!(t.elapsed() < Duration::from_secs(5));

    // answers with garbage
    let junk = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let junk_port = junk.local_addr().unwrap().port();
    tokio::spawn(async move {
        loop {
            if let Ok((mut sock, _)) = junk.accept().await {
                let _ = sock.write_all(b"this is not http\r\n\r\n").await;
            }
        }
    });
    let id = add_ok(&s, "junk", &format!("http://127.0.0.1:{junk_port}/")).await;
    let garbage = check(&s, id).await.json();
    assert_eq!(garbage["ok"], false);
    assert!(garbage["detail"].as_str().unwrap().contains("HTTP"));
}

#[tokio::test]
async fn the_background_scheduler_checks_on_its_own_and_stops_after_delete() {
    let (target, _) = flaky_target().await;
    let s = status_app(statuspage::Config {
        tick: Duration::from_millis(10),
        interval_unit: Duration::from_millis(20), // "1 second" = 20 ms
        ..config()
    })
    .await;
    let id = add(&s, json!({ "name": "auto", "target": format!("http://{}/health", target.addr), "interval_secs": 1 }))
        .await
        .json()["id"]
        .as_u64()
        .unwrap();

    let checks = |s: Server| async move {
        s.get("/api/status").send().await.json()["monitors"][0]["checks"]
            .as_u64()
            .unwrap_or(0)
    };
    let deadline = Instant::now() + Duration::from_secs(5);
    while checks(s).await < 3 {
        assert!(
            Instant::now() < deadline,
            "scheduler never reached 3 checks"
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    assert_eq!(status_of(&s, id).await, "up");

    assert_eq!(
        s.delete(&format!("/api/monitors/{id}"))
            .header("x-api-key", KEY)
            .send()
            .await
            .status,
        204
    );
    assert_eq!(
        s.get(&format!("/api/monitors/{id}/history"))
            .header("x-api-key", KEY)
            .send()
            .await
            .status,
        404
    );
    tokio::time::sleep(Duration::from_millis(100)).await; // a late result must not resurrect it
    assert_eq!(
        s.get("/api/status").send().await.json()["monitors"],
        json!([])
    );
}

#[tokio::test]
async fn unknown_ids_and_limits() {
    let s = status_app(config()).await;
    for (method, path) in [
        ("DELETE", "/api/monitors/99"),
        ("POST", "/api/monitors/99/check"),
        ("GET", "/api/monitors/99/history"),
    ] {
        assert_eq!(
            s.request(method, path)
                .header("x-api-key", KEY)
                .send()
                .await
                .status,
            404,
            "{method} {path}"
        );
    }
    assert_eq!(
        s.delete("/api/monitors/abc")
            .header("x-api-key", KEY)
            .send()
            .await
            .status,
        400
    );

    for i in 0..50 {
        add_ok(&s, &format!("m{i}"), "tcp://127.0.0.1:9").await;
    }
    let over = add(
        &s,
        json!({ "name": "one too many", "target": "tcp://127.0.0.1:9" }),
    )
    .await;
    assert_eq!(over.status, 409);
}

#[tokio::test]
async fn status_page_escapes_names_and_sets_a_strict_csp() {
    let s = status_app(config()).await;
    add_ok(&s, "<img src=x onerror=alert(1)>", "tcp://127.0.0.1:9").await;

    let page = s.get("/").send().await;
    let html = page.text();
    assert!(
        html.contains("&lt;img src=x onerror=alert(1)&gt;"),
        "{html}"
    );
    assert!(!html.contains("<img src=x"));
    let csp = page.header("content-security-policy");
    assert!(csp.contains("default-src 'none'") && csp.contains("script-src 'self'"));
    let script_src = csp
        .split(';')
        .map(str::trim)
        .find(|d| d.starts_with("script-src"))
        .unwrap();
    assert_eq!(script_src, "script-src 'self'");
    assert!(html.contains(r#"<script src="/app.js">"#));
    assert_eq!(page.header("x-content-type-options"), "nosniff");

    let js = s.get("/app.js").send().await;
    assert_eq!(
        js.header("content-type"),
        "application/javascript; charset=utf-8"
    );
    assert!(js.text().contains("EventSource"));
    // the client builds the DOM with textContent only
    assert!(!js.text().contains("innerHTML"));
}

async fn open_events(addr: std::net::SocketAddr) -> Incoming {
    let stream = TcpStream::connect(addr).await.unwrap();
    let (mut sender, conn) = hyper::client::conn::http1::handshake(TokioIo::new(stream))
        .await
        .unwrap();
    tokio::spawn(conn);
    let res = sender
        .send_request(
            hyper::Request::builder()
                .uri("/events")
                .header("host", "x")
                .body(Empty::<Bytes>::new())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        res.headers()["content-type"],
        "text/event-stream; charset=utf-8"
    );
    res.into_body()
}

/// Next `status` event's JSON payload
async fn next_status(body: &mut Incoming) -> Value {
    loop {
        let frame = tokio::time::timeout(Duration::from_secs(5), body.frame())
            .await
            .expect("no event within 5 s")
            .expect("stream ended")
            .unwrap();
        let Ok(data) = frame.into_data() else {
            continue;
        };
        let text = String::from_utf8(data.to_vec()).unwrap();
        if let Some(payload) = text.lines().find_map(|l| l.strip_prefix("data: ")) {
            assert!(text.contains("event: status"), "{text}");
            return serde_json::from_str(payload).unwrap();
        }
    }
}

#[tokio::test]
async fn events_stream_the_snapshot_and_then_every_change() {
    let (target, healthy) = flaky_target().await;
    let s = status_app(config()).await;
    let id = add_ok(&s, "API", &format!("http://{}/health", target.addr)).await;

    let mut events = open_events(s.addr).await;
    let snapshot = next_status(&mut events).await;
    assert_eq!(snapshot["monitors"][0]["status"], "unknown");

    check(&s, id).await;
    let up = next_status(&mut events).await;
    assert_eq!(
        (up["overall"].as_str(), up["monitors"][0]["status"].as_str()),
        (Some("operational"), Some("up"))
    );

    healthy.store(false, Ordering::SeqCst);
    check(&s, id).await;
    check(&s, id).await;
    next_status(&mut events).await; // degraded
    let down = next_status(&mut events).await;
    assert_eq!(down["overall"], "major_outage");

    add_ok(&s, "second", "tcp://127.0.0.1:9").await;
    assert_eq!(
        next_status(&mut events).await["monitors"]
            .as_array()
            .unwrap()
            .len(),
        2
    );
}

#[tokio::test]
async fn open_event_streams_do_not_block_graceful_shutdown() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let (stop, stopped) = tokio::sync::oneshot::channel::<()>();
    let server = tokio::spawn(
        statuspage::build(config())
            .shutdown_grace(Duration::from_secs(30))
            .serve_with_shutdown(listener, async {
                let _ = stopped.await;
            }),
    );

    let mut events = open_events(addr).await;
    next_status(&mut events).await;

    let t = Instant::now();
    stop.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(5), server)
        .await
        .expect("shutdown hung on the open event stream")
        .unwrap()
        .unwrap();
    assert!(t.elapsed() < Duration::from_secs(5));
    // the stream ended instead of being cut mid-event
    loop {
        match tokio::time::timeout(Duration::from_secs(5), events.frame())
            .await
            .unwrap()
        {
            None => break,
            Some(Err(_)) => break,
            Some(Ok(_)) => continue,
        }
    }
}

#[tokio::test]
async fn openapi_lists_public_and_admin_operations() {
    let s = status_app(config()).await;
    let doc = s.get("/openapi.json").send().await.json();
    for path in [
        "/api/status",
        "/events",
        "/api/monitors",
        "/api/monitors/{id}",
        "/api/monitors/{id}/check",
        "/api/monitors/{id}/history",
    ] {
        assert!(doc["paths"][path].is_object(), "{path}");
    }
    assert_eq!(
        doc["paths"]["/api/monitors"]["post"]["security"],
        json!([{ "apiKey_x_api_key": [] }])
    );
    assert!(doc["paths"]["/api/status"]["get"].get("security").is_none());
}