Skip to main content

edgeguard/
lib.rs

1//! EdgeGuard library surface.
2//!
3//! The `edgeguard` binary (`src/main.rs`) is a thin CLI on top of this crate. Exposing the
4//! pipeline as a library lets integration tests drive the *same* `build_state` /
5//! `build_router` entry points the binary uses, so tests exercise the real request path
6//! rather than a reimplementation of it.
7
8pub mod acme;
9pub mod auth;
10pub mod config;
11pub mod generate;
12pub mod limiter;
13pub mod metrics;
14pub mod proxy;
15pub mod reload;
16pub mod supervisor;
17pub mod tls;
18pub mod waf;
19
20use std::num::NonZeroU32;
21use std::sync::Arc;
22
23use anyhow::{Context, Result};
24use arc_swap::ArcSwap;
25use axum::{
26    extract::DefaultBodyLimit,
27    routing::{any, get, post},
28    Router,
29};
30use governor::{Quota, RateLimiter};
31use hyper_util::client::legacy::Client;
32use hyper_util::rt::TokioExecutor;
33
34use crate::auth::AuthEngine;
35use crate::config::{parse_duration, parse_rate, parse_size, Config};
36use crate::metrics::Metrics;
37use crate::proxy::{
38    csp_report, metrics_handler, ready, AppState, RouteLimiter, Runtime, StrLimiter,
39};
40
41pub use crate::auth::hash_password;
42
43/// Translate a `rate`/`burst` policy into a GCRA [`Quota`]. Rejects degenerate input (a `0`
44/// rate or burst) rather than silently coercing it to `1/1`, which would mask the operator's
45/// mistake. Shared by the global, per-route, and per-key limiters.
46fn quota(rate: &str, burst: u32) -> Result<Quota> {
47    let (count, period) = parse_rate(rate)?;
48    anyhow::ensure!(count > 0, "rate count must be > 0 (got \"{rate}\")");
49    anyhow::ensure!(burst > 0, "burst must be > 0 (rate \"{rate}\")");
50    // One cell replenishes every (period / count); burst is the bucket depth.
51    let per_cell = period / count;
52    let burst = NonZeroU32::new(burst).unwrap();
53    Ok(Quota::with_period(per_cell)
54        .context("rate too high for a usable replenish interval")?
55        .allow_burst(burst))
56}
57
58/// Build the hot-swappable [`Runtime`] from a fully-resolved [`Config`]: the rate limiters
59/// (global per-IP, per-route, per-key), the auth engine, and the parsed size/timeout limits.
60/// Errors if any size/rate/auth setting is invalid, so a bad config fails fast — at startup
61/// or on reload — rather than per-request. The HTTP client and metric registry live outside
62/// the runtime (in [`AppState`]) so a reload preserves the connection pool and counters.
63pub fn build_runtime(cfg: Arc<Config>) -> Result<Runtime> {
64    let rl = &cfg.ratelimit;
65
66    // Pick the limiter backend. `local` keeps the in-process `governor` limiters below; a
67    // distributed store (`memory`/`redis`) builds a shared-store limiter instead, so the two
68    // are mutually exclusive. An unknown store value fails here rather than silently disabling
69    // limiting.
70    let store_mode = crate::limiter::StoreMode::parse(&rl.store)?;
71    let use_distributed = rl.enabled && store_mode.is_distributed();
72
73    let distributed = if use_distributed {
74        Some(crate::limiter::DistributedLimiter::build(rl, store_mode)?)
75    } else {
76        None
77    };
78
79    // The local `governor` limiters are built only when not using a shared store.
80    let build_local = rl.enabled && !use_distributed;
81
82    let ip_limiter = if build_local {
83        Some(Arc::new(RateLimiter::keyed(quota(&rl.rate, rl.burst)?)))
84    } else {
85        None
86    };
87
88    let mut route_limiters = Vec::new();
89    if build_local {
90        for route in &rl.routes {
91            anyhow::ensure!(
92                !route.path.is_empty(),
93                "ratelimit.routes[].path must not be empty"
94            );
95            route_limiters.push(RouteLimiter {
96                prefix: route.path.clone(),
97                limiter: Arc::new(RateLimiter::keyed(quota(&route.rate, route.burst)?)),
98            });
99        }
100    }
101
102    let key_limiter: Option<Arc<StrLimiter>> = if build_local && rl.per_key.enabled {
103        Some(Arc::new(RateLimiter::keyed(quota(
104            &rl.per_key.rate,
105            rl.per_key.burst,
106        )?)))
107    } else {
108        None
109    };
110
111    let auth = AuthEngine::build(&cfg.auth)?;
112    // Compile the WAF here too, so a bad custom pattern fails fast at startup/reload rather
113    // than per-request (and a broken hot-reload keeps the previous policy).
114    let waf = crate::waf::WafEngine::build(&cfg.waf)?;
115
116    let max_body = parse_size(&cfg.validation.max_body)?;
117    let max_response_body = parse_size(&cfg.validation.max_response_body)?;
118    let max_header_bytes = parse_size(&cfg.validation.max_header_bytes)?;
119    // A zero duration ("0") means "no timeout".
120    let upstream_timeout = parse_duration(&cfg.validation.upstream_timeout)?;
121    let upstream_timeout = (!upstream_timeout.is_zero()).then_some(upstream_timeout);
122
123    Ok(Runtime {
124        upstream_base: Arc::new(cfg.upstream_base()),
125        auth,
126        waf,
127        distributed,
128        ip_limiter,
129        route_limiters,
130        key_limiter,
131        max_body,
132        max_response_body,
133        max_header_bytes,
134        upstream_timeout,
135        cfg,
136    })
137}
138
139/// Build the shared [`AppState`]: a fresh [`Runtime`] wrapped in an [`ArcSwap`] for
140/// hot-reload, the upstream HTTP client, and the metric registry.
141pub fn build_state(cfg: Arc<Config>) -> Result<AppState> {
142    let runtime = build_runtime(cfg)?;
143    let client =
144        Client::builder(TokioExecutor::new()).build_http::<http_body_util::Full<bytes::Bytes>>();
145    Ok(AppState {
146        client,
147        metrics: Arc::new(Metrics::new()),
148        runtime: Arc::new(ArcSwap::from_pointee(runtime)),
149    })
150}
151
152/// Build the combined axum [`Router`]: the internal `/__edgeguard/*` endpoints (health,
153/// readiness, Prometheus metrics, CSP report sink) plus the catch-all proxy handler, all on one
154/// listener. This is the default (single-port) topology; for the public/private split see
155/// [`build_public_router`] / [`build_admin_router`]. Body limits are enforced inside the proxy
156/// handler, so the default layer is disabled there; the CSP sink keeps a small explicit cap
157/// since it parses the body.
158pub fn build_router(state: AppState) -> Router {
159    public_routes()
160        .merge(admin_routes())
161        .layer(DefaultBodyLimit::disable())
162        .with_state(state)
163}
164
165/// The **public** router (used in public/private split mode): the catch-all proxy plus the
166/// browser-facing CSP report sink. The ops endpoints (health/readiness/metrics) are *not* here
167/// — they live on the private [`build_admin_router`] listener, so they aren't exposed publicly.
168pub fn build_public_router(state: AppState) -> Router {
169    public_routes()
170        .layer(DefaultBodyLimit::disable())
171        .with_state(state)
172}
173
174/// The **private/admin** router (used in public/private split mode): the internal ops endpoints
175/// (health, readiness, metrics). It has no proxy fallback, so an unknown path returns `404`
176/// rather than being forwarded upstream. Shares the same [`AppState`] as the public router, so
177/// `/__edgeguard/metrics` reports the live proxy counters.
178pub fn build_admin_router(state: AppState) -> Router {
179    admin_routes().with_state(state)
180}
181
182/// Public-surface routes: the proxy fallback and the CSP report sink (which browsers POST to
183/// from the public web, so it stays on the public listener).
184fn public_routes() -> Router<AppState> {
185    Router::new()
186        .route(
187            "/__edgeguard/csp-report",
188            post(csp_report).layer(DefaultBodyLimit::max(64 * 1024)),
189        )
190        .fallback(any(proxy::handle))
191}
192
193/// Internal ops routes: liveness, readiness, and the Prometheus metrics scrape.
194fn admin_routes() -> Router<AppState> {
195    Router::new()
196        .route("/__edgeguard/health", get(|| async { "ok" }))
197        .route("/__edgeguard/ready", get(ready))
198        .route("/__edgeguard/metrics", get(metrics_handler))
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204    use crate::config::RateLimitCfg;
205
206    fn cfg_with_ratelimit(rate: &str, burst: u32) -> Config {
207        Config {
208            ratelimit: RateLimitCfg {
209                enabled: true,
210                rate: rate.into(),
211                burst,
212                ..Default::default()
213            },
214            ..Default::default()
215        }
216    }
217
218    #[test]
219    fn build_state_rejects_zero_rate() {
220        // `0/min` is a misconfiguration, not "1/min" — validation fails before we ever
221        // build the client, so no async runtime is needed here.
222        assert!(build_state(Arc::new(cfg_with_ratelimit("0/min", 20))).is_err());
223    }
224
225    #[test]
226    fn build_state_rejects_zero_burst() {
227        assert!(build_state(Arc::new(cfg_with_ratelimit("60/min", 0))).is_err());
228    }
229
230    #[test]
231    fn build_runtime_builds_route_and_key_limiters() {
232        let mut cfg = Config::default();
233        cfg.ratelimit.routes = vec![crate::config::RouteRateLimit {
234            path: "/api/".into(),
235            rate: "10/sec".into(),
236            burst: 5,
237        }];
238        cfg.ratelimit.per_key = crate::config::PerKeyRateLimit {
239            enabled: true,
240            rate: "1000/hour".into(),
241            burst: 100,
242        };
243        let rt = build_runtime(Arc::new(cfg)).unwrap();
244        assert_eq!(rt.route_limiters.len(), 1);
245        assert_eq!(rt.route_limiters[0].prefix, "/api/");
246        assert!(rt.key_limiter.is_some());
247    }
248
249    #[test]
250    fn build_runtime_rejects_bad_route_rate() {
251        let mut cfg = Config::default();
252        cfg.ratelimit.routes = vec![crate::config::RouteRateLimit {
253            path: "/api/".into(),
254            rate: "0/sec".into(),
255            burst: 5,
256        }];
257        assert!(build_runtime(Arc::new(cfg)).is_err());
258    }
259}