servlet 1.1.0

A small application runtime with an explicit operational contract
Documentation
//! Isolated-process probe; python3 -B scripts/client_security_test.py owns TLS roots and servers.
#![cfg(feature = "client")]
use servlet::{
    App, AppConfig,
    client::{ClientConfig, ClientError, ClientLimits},
    ecosystem::tokio,
    sync::oneshot,
    time::Instant,
};
use std::time::Duration;

#[tokio::test]
#[ignore = "requires owned TLS/proxy/reset fixtures; python3 -B scripts/client_security_test.py"]
async fn security_probe() {
    let mode = std::env::var("SERVLET_CLIENT_CASE").unwrap();
    let origin = std::env::var("SERVLET_CLIENT_ORIGIN").unwrap();
    let mut config = ClientConfig::new(&origin)
        .unwrap()
        .with_limits(ClientLimits {
            // Trust checks need the normal DNS / address-fallback / TLS budget.
            // Only the deliberately stalled handshake tests the short cutoff.
            connect_timeout: if mode == "handshake-stall" {
                Duration::from_millis(300)
            } else {
                Duration::from_secs(3)
            },
            read_timeout: Duration::from_secs(2),
            total_timeout: Duration::from_secs(5),
            ..Default::default()
        })
        .unwrap()
        .with_header("authorization", "Bearer private-client-token")
        .unwrap();
    if !matches!(mode.as_str(), "platform" | "ambient") {
        let pem = std::fs::read(std::env::var("SERVLET_CLIENT_ROOT").unwrap()).unwrap();
        config = config.with_root_certificate(&pem).unwrap();
    }
    let (send, receive) = oneshot::channel();
    let ambient = mode == "ambient";
    let metrics = servlet::metrics::Registry::new();
    let mut runtime = AppConfig::default();
    runtime.bind = "127.0.0.1:0".parse().unwrap();
    let app = App::new("client-security")
        .metrics(metrics.clone())
        .config(runtime)
        .setup(move |mut ctx| async move {
            let value = ctx.http_client("approved", config);
            let client = if ambient {
                assert_eq!(value.err().unwrap(), ClientError::AmbientConfiguration);
                None
            } else {
                Some(value.unwrap())
            };
            let _ = send.send(client);
            Ok(())
        })
        .start()
        .await
        .unwrap();
    if let Some(client) = receive.await.unwrap() {
        let request = if mode == "reset" {
            client
                .request(servlet::client::native::Method::POST, "/write")
                .unwrap()
                .body(b"write".to_vec())
                .unwrap()
        } else {
            client.get("/trusted").unwrap()
        };
        let result = request.send(Instant::now() + Duration::from_secs(6)).await;
        match mode.as_str() {
            "wrong-root" | "wrong-host" | "platform" | "reset" => {
                assert_eq!(result.err().unwrap(), ClientError::Transport)
            }
            "handshake-stall" => assert_eq!(result.err().unwrap(), ClientError::ConnectTimeout),
            "trusted" | "explicit-over-ambient" | "proxy" => {
                assert_eq!(result.unwrap().bytes().await.unwrap(), b"trusted")
            }
            _ => panic!("unknown owned fixture mode"),
        }
    }
    let encoded = metrics.encode().unwrap();
    if ambient {
        assert!(!encoded.contains("servlet_client_requests_total"));
    } else {
        let outcome = match mode.as_str() {
            "wrong-root" | "wrong-host" | "platform" | "reset" => "transport_error",
            "handshake-stall" => "connect_timeout",
            _ => "ok",
        };
        assert!(encoded.contains(&format!(
            "servlet_client_requests_total{{client=\"approved\",outcome=\"{outcome}\"}} 1"
        )));
        assert!(encoded.contains("servlet_client_active_requests{client=\"approved\"} 0"));
    }
    app.handle().shutdown();
    app.wait().await.unwrap();
    println!("CLIENT_SECURITY_OK {mode}");
}