Skip to main content

rust_web_server/admin/
mod.rs

1//! The admin API (18.10.0, #29): change and inspect a running server on a
2//! listener of its own, never on the public routes.
3//!
4//! ```toml
5//! [admin]
6//! listen          = "127.0.0.1:9901"     # off when unset
7//! token_env       = "RWS_ADMIN_TOKEN"    # env var holding the bearer token (this is the default)
8//! allowed_origins = ["https://ops.example.com"]   # browsers on other origins (the UI's own is always allowed)
9//! ```
10//!
11//! Every request needs `Authorization: Bearer <token>`, on loopback too;
12//! without a token the listener doesn't start (and `rws --check` says so).
13//! A browser request from an origin other than the listener's own or one in
14//! `allowed_origins` is refused, so a page elsewhere can't drive it with a
15//! token it learned.
16//!
17//! The API lives under `/admin/api/v1/`; [`AdminApi`] is an
18//! [`Application`], so tests and embedders can drive it without a socket.
19//! Requests are served one thread per connection (at most
20//! [`MAX_CONNECTIONS`] at once), apart from the server's own workers, so a
21//! saturated server can still be administered.
22
23pub mod ops;
24mod ui;
25
26use std::sync::atomic::{AtomicUsize, Ordering};
27use std::sync::{Arc, OnceLock};
28use std::time::{Duration, Instant};
29
30use crate::application::Application;
31use crate::entry_point::Config;
32use crate::header::Header;
33use crate::mcp::json_escape;
34use crate::request::Request;
35use crate::response::{Response, STATUS_CODE_REASON_PHRASE};
36use crate::server::{Address, ConnectionInfo};
37
38/// The token's environment variable when `token_env` is unset.
39pub const DEFAULT_TOKEN_ENV: &str = "RWS_ADMIN_TOKEN";
40/// Admin connections served at once; more are closed at accept.
41pub const MAX_CONNECTIONS: usize = 32;
42/// Path prefix of the API.
43pub const API_PREFIX: &str = "/admin/api/v1";
44
45/// `[admin]` from the environment (`RWS_CONFIG_ADMIN_*`).
46#[derive(Debug, Clone, Default, PartialEq)]
47pub struct AdminConfig {
48    /// `host:port` to listen on; empty = no admin listener.
49    pub listen: String,
50    /// The environment variable holding the bearer token.
51    pub token_env: String,
52    /// Browser origins allowed besides the listener's own.
53    pub allowed_origins: Vec<String>,
54}
55
56impl AdminConfig {
57    pub fn from_env() -> AdminConfig {
58        let read = |name: &str| std::env::var(name).unwrap_or_default().trim().to_string();
59        let token_env = read(Config::RWS_CONFIG_ADMIN_TOKEN_ENV);
60        AdminConfig {
61            listen: read(Config::RWS_CONFIG_ADMIN_LISTEN),
62            token_env: if token_env.is_empty() { DEFAULT_TOKEN_ENV.to_string() } else { token_env },
63            allowed_origins: read(Config::RWS_CONFIG_ADMIN_ALLOWED_ORIGINS)
64                .split(',')
65                .map(|o| o.trim().trim_end_matches('/').to_string())
66                .filter(|o| !o.is_empty())
67                .collect(),
68        }
69    }
70
71    pub fn enabled(&self) -> bool {
72        !self.listen.is_empty()
73    }
74
75    /// The token, or why there is none.
76    pub fn token(&self) -> Result<String, String> {
77        match std::env::var(&self.token_env) {
78            Ok(t) if !t.trim().is_empty() => Ok(t.trim().to_string()),
79            _ => Err(format!("the admin token variable {} is unset or empty", self.token_env)),
80        }
81    }
82}
83
84/// When the process started serving the admin API (for `uptime`).
85fn started() -> Instant {
86    static STARTED: OnceLock<Instant> = OnceLock::new();
87    *STARTED.get_or_init(Instant::now)
88}
89
90/// The admin API. See the [module docs](self).
91#[derive(Clone)]
92pub struct AdminApi {
93    inner: Arc<Inner>,
94}
95
96type Reloader = Arc<dyn Fn() + Send + Sync>;
97
98struct Inner {
99    token: String,
100    allowed_origins: Vec<String>,
101    reloader: Reloader,
102    reload_wait: Duration,
103}
104
105impl AdminApi {
106    /// The API accepting `token`.
107    pub fn new(token: impl Into<String>) -> AdminApi {
108        started();
109        crate::log::tail::enable();
110        AdminApi {
111            inner: Arc::new(Inner {
112                token: token.into(),
113                allowed_origins: Vec::new(),
114                reloader: Arc::new(crate::config_reload::request_reload),
115                reload_wait: Duration::from_secs(10),
116            }),
117        }
118    }
119
120    fn edit(self, f: impl FnOnce(&mut Inner)) -> AdminApi {
121        let mut inner = Inner {
122            token: self.inner.token.clone(),
123            allowed_origins: self.inner.allowed_origins.clone(),
124            reloader: Arc::clone(&self.inner.reloader),
125            reload_wait: self.inner.reload_wait,
126        };
127        f(&mut inner);
128        AdminApi { inner: Arc::new(inner) }
129    }
130
131    /// Browser origins allowed besides the listener's own.
132    pub fn allowed_origins(self, origins: Vec<String>) -> AdminApi {
133        self.edit(|i| i.allowed_origins = origins)
134    }
135
136    /// What `POST …/reload` runs (default: [`crate::config_reload::request_reload`],
137    /// the same path as `SIGHUP`, so TLS certificates are reloaded too), and
138    /// how long it waits for the reload to finish.
139    pub fn reloader(self, reload: impl Fn() + Send + Sync + 'static, wait: Duration) -> AdminApi {
140        self.edit(|i| {
141            i.reloader = Arc::new(reload);
142            i.reload_wait = wait;
143        })
144    }
145
146    /// Request a reload and wait for it: the reload count moving, then the
147    /// errors it reported.
148    fn reload(&self) -> Response {
149        if !crate::metrics::SERVER_READY.load(Ordering::SeqCst) {
150            // Before the servers run, SIGHUP has no handler yet.
151            return error(STATUS_CODE_REASON_PHRASE.n503_service_unavailable, "the server is starting or stopping");
152        }
153        let (before, _) = crate::config_reload::last_reload();
154        (self.inner.reloader)();
155        let deadline = Instant::now() + self.inner.reload_wait;
156        loop {
157            let (count, errors) = crate::config_reload::last_reload();
158            if count > before {
159                let errors: Vec<String> = errors.iter().map(|e| format!("\"{}\"", json_escape(e))).collect();
160                let applied = errors.is_empty();
161                return json(STATUS_CODE_REASON_PHRASE.n200_ok, format!(r#"{{"reloaded":true,"applied":{applied},"reloads":{count},"errors":[{}]}}"#, errors.join(",")));
162            }
163            if Instant::now() >= deadline {
164                return error(STATUS_CODE_REASON_PHRASE.n504_gateway_timeout, "the reload didn't finish in time; check the server log");
165            }
166            std::thread::sleep(Duration::from_millis(20));
167        }
168    }
169
170    fn authorized(&self, request: &Request) -> bool {
171        let Some(sent) = request.header("Authorization").and_then(|h| h.value.strip_prefix("Bearer ").map(str::trim)) else {
172            return false;
173        };
174        crate::proxy_config::constant_time_eq(sent.as_bytes(), self.inner.token.as_bytes())
175    }
176
177    /// A browser on another origin is refused; non-browser clients send none.
178    fn origin_allowed(&self, request: &Request) -> bool {
179        let Some(origin) = request.header("Origin").map(|h| h.value.trim().trim_end_matches('/').to_string()) else {
180            return true;
181        };
182        let host = request.header("Host").map(|h| h.value.trim().to_string()).unwrap_or_default();
183        let own = [format!("http://{host}"), format!("https://{host}")];
184        own.contains(&origin) || self.inner.allowed_origins.contains(&origin)
185    }
186}
187
188pub(crate) fn json(status: &crate::response::StatusCodeReasonPhrase, body: String) -> Response {
189    let mut response = Response::json(status, body.into_bytes());
190    response.headers.push(Header::new("Cache-Control", "no-store"));
191    response
192}
193
194pub(crate) fn error(status: &crate::response::StatusCodeReasonPhrase, message: &str) -> Response {
195    json(status, format!(r#"{{"error":"{}"}}"#, json_escape(message)))
196}
197
198impl Application for AdminApi {
199    fn execute(&self, request: &Request, connection: &ConnectionInfo) -> Result<Response, String> {
200        if !self.origin_allowed(request) {
201            return Ok(error(STATUS_CODE_REASON_PHRASE.n403_forbidden, "origin not allowed"));
202        }
203        let path = crate::url::URL::request_target_path(&request.request_uri).unwrap_or_default();
204        if request.method == "GET" || request.method == "HEAD" {
205            if path == "/admin" || path == "/" {
206                return Ok(ui::redirect());
207            }
208            if let Some(file) = ui::file(path) {
209                return Ok(file);
210            }
211        }
212        if !self.authorized(request) {
213            let mut response = error(STATUS_CODE_REASON_PHRASE.n401_unauthorized, "missing or wrong bearer token");
214            response.headers.push(Header::new("WWW-Authenticate", r#"Bearer realm="rws admin""#));
215            return Ok(response);
216        }
217        let Some(route) = path.strip_prefix(API_PREFIX) else {
218            return Ok(error(STATUS_CODE_REASON_PHRASE.n404_not_found, "not found"));
219        };
220        Ok(self.route(request, connection, route.trim_end_matches('/')))
221    }
222}
223
224fn ok(body: String) -> Response {
225    json(STATUS_CODE_REASON_PHRASE.n200_ok, body)
226}
227
228fn bad_request(message: &str) -> Response {
229    error(STATUS_CODE_REASON_PHRASE.n400_bad_request, message)
230}
231
232/// The JSON body's top-level boolean `key`.
233fn body_bool(request: &Request, key: &str) -> Option<bool> {
234    let body = std::str::from_utf8(&request.body).ok()?;
235    let value = crate::mcp::json_rpc::object_members(body)?.into_iter().find(|(k, _)| k == key)?.1;
236    match value.as_str() {
237        "true" => Some(true),
238        "false" => Some(false),
239        _ => None,
240    }
241}
242
243/// The JSON body's top-level string `key`.
244fn body_str(request: &Request, key: &str) -> Option<String> {
245    let body = std::str::from_utf8(&request.body).ok()?;
246    let value = crate::mcp::json_rpc::object_members(body)?.into_iter().find(|(k, _)| k == key)?.1;
247    value.starts_with('"').then(|| crate::mcp::json_rpc::extract_str(&format!(r#"{{"v":{value}}}"#), "v")).flatten()
248}
249
250/// Query parameter `key` of the request target.
251fn query(request: &Request, key: &str) -> Option<String> {
252    let (_, query) = request.request_uri.split_once('?')?;
253    query.split('&').find_map(|pair| {
254        let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
255        (k == key).then(|| v.to_string())
256    })
257}
258
259fn parse_ip(ip: &str) -> Option<String> {
260    let ip = crate::url::URL::percent_decode(ip.trim());
261    ip.parse::<std::net::IpAddr>().ok().map(|ip| ip.to_string())
262}
263
264impl AdminApi {
265    fn route(&self, request: &Request, connection: &ConnectionInfo, route: &str) -> Response {
266        let method = request.method.as_str();
267        let wrong_method = |allowed: &str| {
268            let mut response = error(STATUS_CODE_REASON_PHRASE.n405_method_not_allowed, &format!("use {allowed}"));
269            response.headers.push(Header::new("Allow", allowed.to_string()));
270            response
271        };
272        if let Some(ip) = route.strip_prefix("/blocklist/") {
273            return match (method, parse_ip(ip)) {
274                ("DELETE", Some(ip)) => ok(ops::unblock(&ip)),
275                ("DELETE", None) => bad_request("not an IP address"),
276                _ => wrong_method("DELETE"),
277            };
278        }
279        if let Some(name) = route.strip_prefix("/flags/") {
280            let name = crate::url::URL::percent_decode(name);
281            return match method {
282                "PUT" => match body_bool(request, "enabled") {
283                    Some(enabled) if !name.is_empty() => ok(ops::set_flag(&name, enabled)),
284                    _ => bad_request(r#"expected {"enabled":true|false}"#),
285                },
286                _ => wrong_method("PUT"),
287            };
288        }
289        if let Some(name) = route.strip_prefix("/upstreams/").and_then(|r| r.strip_suffix("/drain")) {
290            let name = crate::url::URL::percent_decode(name);
291            return match (method, body_str(request, "backend"), body_bool(request, "drained")) {
292                ("PUT", Some(backend), Some(drained)) => match ops::set_drained(&name, &backend, drained) {
293                    Ok(body) => ok(body),
294                    Err(e) => error(STATUS_CODE_REASON_PHRASE.n404_not_found, &e),
295                },
296                ("PUT", _, _) => bad_request(r#"expected {"backend":"<url>","drained":true|false}"#),
297                _ => wrong_method("PUT"),
298            };
299        }
300        match (method, route) {
301            ("GET", "/logs") => {
302                let n = query(request, "n").and_then(|n| n.parse().ok()).unwrap_or(100);
303                ok(ops::logs_json(n))
304            }
305            (_, "/logs") => wrong_method("GET"),
306            ("GET", "/logs/stream") => {
307                let (sender, mut response) = crate::sse::SseStream::new().buffer(256).start();
308                if !crate::log::tail::follow(sender) {
309                    return error(STATUS_CODE_REASON_PHRASE.n503_service_unavailable, "too many log followers");
310                }
311                response.headers.push(Header::new("Cache-Control", "no-store"));
312                response
313            }
314            (_, "/logs/stream") => wrong_method("GET"),
315            ("GET", "/upstreams") => ok(ops::upstreams_json()),
316            (_, "/upstreams") => wrong_method("GET"),
317            ("GET", "/certificates") => ok(ops::certificates_json()),
318            (_, "/certificates") => wrong_method("GET"),
319            ("POST", "/certificates/renew") => match ops::request_renewal() {
320                true => json(STATUS_CODE_REASON_PHRASE.n202_accepted, r#"{"requested":true}"#.to_string()),
321                false => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "ACME isn't configured: nothing to renew"),
322            },
323            (_, "/certificates/renew") => wrong_method("POST"),
324            ("GET", "/status") => ok(status_json(connection)),
325            (_, "/status") => wrong_method("GET"),
326            ("GET", "/config") => ok(ops::config_json()),
327            (_, "/config") => wrong_method("GET"),
328            ("POST", "/reload") => self.reload(),
329            (_, "/reload") => wrong_method("POST"),
330            ("GET", "/metrics") => ok(ops::metrics_json()),
331            (_, "/metrics") => wrong_method("GET"),
332            ("GET", "/build") => ok(ops::build_features_json()),
333            (_, "/build") => wrong_method("GET"),
334            ("GET", "/rate-limit") => ok(ops::rate_limit_json()),
335            (_, "/rate-limit") => wrong_method("GET"),
336            ("GET", "/rate-limit/check") => match query(request, "ip").as_deref().and_then(parse_ip) {
337                Some(ip) => ok(ops::check_rate_limit_json(&ip)),
338                None => bad_request("expected ?ip=<address>"),
339            },
340            (_, "/rate-limit/check") => wrong_method("GET"),
341            ("GET", "/cors") => ok(ops::cors_json()),
342            (_, "/cors") => wrong_method("GET"),
343            ("GET", "/maintenance") => ok(ops::maintenance_json()),
344            ("PUT", "/maintenance") => match body_bool(request, "active") {
345                Some(active) => ok(ops::set_maintenance(active)),
346                None => bad_request(r#"expected {"active":true|false}"#),
347            },
348            (_, "/maintenance") => wrong_method("GET, PUT"),
349            ("GET", "/blocklist") => ok(ops::blocklist_json()),
350            ("POST", "/blocklist") => match body_str(request, "ip").as_deref().and_then(parse_ip) {
351                Some(ip) => ok(ops::block(&ip)),
352                None => bad_request(r#"expected {"ip":"<address>"}"#),
353            },
354            (_, "/blocklist") => wrong_method("GET, POST"),
355            ("GET", "/flags") => ok(ops::flags_json()),
356            (_, "/flags") => wrong_method("GET"),
357            ("GET", "/cache") => match ops::registered_cache_json() {
358                Some(stats) => ok(stats),
359                None => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "no response cache in this server"),
360            },
361            ("DELETE", "/cache") => match ops::clear_registered_cache() {
362                true => ok(r#"{"cleared":true}"#.to_string()),
363                false => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "no response cache in this server"),
364            },
365            (_, "/cache") => wrong_method("GET, DELETE"),
366            ("GET", "/routes") => ok(ops::registered_routes_json()),
367            (_, "/routes") => wrong_method("GET"),
368            ("GET", "/requests" | "/errors") => {
369                let n = query(request, "n").and_then(|n| n.parse().ok()).unwrap_or(20);
370                ok(ops::recent_json(n, route == "/errors"))
371            }
372            (_, "/requests" | "/errors") => wrong_method("GET"),
373            _ => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "not found"),
374        }
375    }
376}
377
378/// Compile-time features of this build that matter to an operator.
379pub fn features() -> Vec<&'static str> {
380    let mut on = Vec::new();
381    macro_rules! feature {
382        ($($name:literal),*) => { $( if cfg!(feature = $name) { on.push($name); } )* };
383    }
384    feature!("http1", "http2", "http3", "acme", "auth", "sso", "sso-saml", "sso-server", "gateway-api", "tower", "openapi", "mimalloc", "brotli", "zstd");
385    on
386}
387
388fn status_json(connection: &ConnectionInfo) -> String {
389    let env = |name: &str| std::env::var(name).unwrap_or_default();
390    let tls = !env(Config::RWS_CONFIG_TLS_CERT_FILE).is_empty() || !env(Config::RWS_CONFIG_ACME_DOMAINS).is_empty();
391    let redirect = env(Config::RWS_CONFIG_HTTP_REDIRECT_PORT);
392    let features: Vec<String> = features().iter().map(|f| format!("\"{f}\"")).collect();
393    format!(
394        r#"{{"version":"{}","pid":{},"uptime_secs":{},"ready":{},"features":[{}],"listeners":{{"main":{{"address":"{}:{}","tls":{tls}}},"redirect":{},"admin":"{}:{}"}}}}"#,
395        env!("CARGO_PKG_VERSION"),
396        std::process::id(),
397        started().elapsed().as_secs(),
398        crate::metrics::SERVER_READY.load(Ordering::SeqCst),
399        features.join(","),
400        json_escape(&env(Config::RWS_CONFIG_IP)),
401        json_escape(&env(Config::RWS_CONFIG_PORT)),
402        if redirect.trim().is_empty() { "null".to_string() } else { format!("\"{}\"", json_escape(redirect.trim())) },
403        json_escape(&connection.server.ip),
404        connection.server.port,
405    )
406}
407
408/// Semantic checks of `[admin]` for `rws --check`; `get` reads a setting
409/// of the file.
410pub(crate) fn validate(get: &dyn Fn(&str) -> Option<String>, report: &mut crate::config_check::ConfigReport) {
411    let listen = get(Config::RWS_CONFIG_ADMIN_LISTEN).unwrap_or_default();
412    let listen = listen.trim();
413    if listen.is_empty() {
414        for key in [Config::RWS_CONFIG_ADMIN_TOKEN_ENV, Config::RWS_CONFIG_ADMIN_ALLOWED_ORIGINS] {
415            if get(key).is_some_and(|v| !v.trim().is_empty()) {
416                report.warn("[admin] has settings but no listen: the admin API is off");
417                break;
418            }
419        }
420        return;
421    }
422    match listen.parse::<std::net::SocketAddr>() {
423        Err(_) => report.error(format!("[admin] listen = {listen:?}: expected host:port with an IP address, e.g. \"127.0.0.1:9901\"")),
424        Ok(addr) if !addr.ip().is_loopback() => report.warn(format!(
425            "[admin] listen = {listen:?} is reachable from other hosts: firewall it, or listen on 127.0.0.1 and use an SSH tunnel"
426        )),
427        Ok(_) => {}
428    }
429    let token_env = get(Config::RWS_CONFIG_ADMIN_TOKEN_ENV).map(|v| v.trim().to_string()).filter(|v| !v.is_empty());
430    let token_env = token_env.unwrap_or_else(|| DEFAULT_TOKEN_ENV.to_string());
431    if !crate::config_check::offline() && std::env::var(&token_env).unwrap_or_default().trim().is_empty() {
432        report.error(format!("[admin] reads its token from {token_env}, which is unset or empty: the admin API won't start"));
433    }
434    for origin in get(Config::RWS_CONFIG_ADMIN_ALLOWED_ORIGINS).unwrap_or_default().split(',').map(str::trim).filter(|o| !o.is_empty()) {
435        if !(origin.starts_with("http://") || origin.starts_with("https://")) || origin.trim_end_matches('/').matches('/').count() > 2 {
436            report.error(format!("[admin] allowed_origins: {origin:?} is not an origin (scheme://host[:port])"));
437        }
438    }
439}
440
441/// Start the admin listener `config` describes, on a thread of its own.
442/// `Ok(None)` when it is off.
443pub fn start(config: &AdminConfig) -> Result<Option<std::net::SocketAddr>, String> {
444    if !config.enabled() {
445        return Ok(None);
446    }
447    let token = config.token()?;
448    let listener = std::net::TcpListener::bind(&config.listen).map_err(|e| format!("can't listen on {}: {e}", config.listen))?;
449    let local = listener.local_addr().map_err(|e| e.to_string())?;
450    let api = AdminApi::new(token).allowed_origins(config.allowed_origins.clone());
451    std::thread::Builder::new()
452        .name("rws-admin".to_string())
453        .spawn(move || serve(listener, api))
454        .map_err(|e| e.to_string())?;
455    Ok(Some(local))
456}
457
458/// [`start`] from `RWS_CONFIG_ADMIN_*`, reporting the outcome: the binary's
459/// entry point. A misconfigured admin API is logged, not fatal.
460pub fn start_from_env() {
461    match start(&AdminConfig::from_env()) {
462        Ok(Some(addr)) => println!("admin API at http://{addr}{API_PREFIX}/"),
463        Ok(None) => {}
464        Err(e) => eprintln!("admin API not started: {e}"),
465    }
466}
467
468fn serve(listener: std::net::TcpListener, api: AdminApi) {
469    let open = Arc::new(AtomicUsize::new(0));
470    for stream in listener.incoming() {
471        let Ok(stream) = stream else { continue };
472        if open.fetch_add(1, Ordering::SeqCst) >= MAX_CONNECTIONS {
473            open.fetch_sub(1, Ordering::SeqCst);
474            continue;
475        }
476        let (api, held) = (api.clone(), Arc::clone(&open));
477        let spawned = std::thread::Builder::new().name("rws-admin-conn".to_string()).spawn(move || {
478            let address = |a: Option<std::net::SocketAddr>| match a {
479                Some(a) => Address { ip: a.ip().to_string(), port: a.port() as i32 },
480                None => Address { ip: String::new(), port: 0 },
481            };
482            let mut connection = ConnectionInfo::new(address(stream.peer_addr().ok()), address(stream.local_addr().ok()));
483            connection.request_size = 16 * 1024;
484            let _ = stream.set_read_timeout(Some(Duration::from_secs(30)));
485            let _ = stream.set_nodelay(true);
486            let _ = crate::server::Server::process(stream, connection, api);
487            held.fetch_sub(1, Ordering::SeqCst);
488        });
489        if spawned.is_err() {
490            open.fetch_sub(1, Ordering::SeqCst);
491        }
492    }
493}
494
495#[cfg(test)]
496mod tests;