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 access;
24pub mod ops;
25mod ui;
26
27use std::sync::atomic::{AtomicUsize, Ordering};
28use std::sync::{Arc, OnceLock};
29use std::time::{Duration, Instant};
30
31use crate::application::Application;
32use crate::entry_point::Config;
33use crate::header::Header;
34use crate::mcp::json_escape;
35use crate::request::Request;
36use crate::response::{Response, STATUS_CODE_REASON_PHRASE};
37use crate::server::{Address, ConnectionInfo};
38
39/// The token's environment variable when `token_env` is unset.
40pub const DEFAULT_TOKEN_ENV: &str = "RWS_ADMIN_TOKEN";
41/// Admin connections served at once; more are closed at accept.
42pub const MAX_CONNECTIONS: usize = 32;
43/// Path prefix of the API.
44pub const API_PREFIX: &str = "/admin/api/v1";
45
46/// `[admin]` from the environment (`RWS_CONFIG_ADMIN_*`).
47#[derive(Debug, Clone, Default, PartialEq)]
48pub struct AdminConfig {
49    /// `host:port` to listen on; empty = no admin listener.
50    pub listen: String,
51    /// The environment variable holding the bearer token.
52    pub token_env: String,
53    /// Browser origins allowed besides the listener's own.
54    pub allowed_origins: Vec<String>,
55    /// More users: `name:ENV_VAR:role`, comma-separated (18.22.0).
56    pub users: String,
57    /// Append the audit log here (empty: memory only).
58    pub audit_log: String,
59    /// OIDC sign-in to the admin UI (empty issuer and provider: off).
60    pub oidc: AdminOidcConfig,
61}
62
63/// `[admin] oidc_*` (18.22.0): sign in to the admin UI with an OIDC provider.
64#[derive(Debug, Clone, Default, PartialEq)]
65pub struct AdminOidcConfig {
66    pub issuer: String,
67    pub provider: String,
68    pub tenant: String,
69    pub client_id: String,
70    pub client_secret_env: String,
71    pub cookie_secret_env: String,
72    /// `group=role`, comma-separated; `*` matches anyone signed in.
73    pub roles: String,
74    /// The ID-token claim holding groups (default `groups`).
75    pub role_claim: String,
76}
77
78impl AdminOidcConfig {
79    pub fn enabled(&self) -> bool {
80        !self.issuer.is_empty() || !self.provider.is_empty()
81    }
82}
83
84/// Settings without a CLI flag (`[admin] users`, `audit_log`, `oidc_*`).
85pub const RWS_CONFIG_ADMIN_USERS: &str = "RWS_CONFIG_ADMIN_USERS";
86pub const RWS_CONFIG_ADMIN_AUDIT_LOG: &str = "RWS_CONFIG_ADMIN_AUDIT_LOG";
87pub const RWS_CONFIG_ADMIN_OIDC_ISSUER: &str = "RWS_CONFIG_ADMIN_OIDC_ISSUER";
88pub const RWS_CONFIG_ADMIN_OIDC_PROVIDER: &str = "RWS_CONFIG_ADMIN_OIDC_PROVIDER";
89pub const RWS_CONFIG_ADMIN_OIDC_TENANT: &str = "RWS_CONFIG_ADMIN_OIDC_TENANT";
90pub const RWS_CONFIG_ADMIN_OIDC_CLIENT_ID: &str = "RWS_CONFIG_ADMIN_OIDC_CLIENT_ID";
91pub const RWS_CONFIG_ADMIN_OIDC_CLIENT_SECRET_ENV: &str = "RWS_CONFIG_ADMIN_OIDC_CLIENT_SECRET_ENV";
92pub const RWS_CONFIG_ADMIN_OIDC_COOKIE_SECRET_ENV: &str = "RWS_CONFIG_ADMIN_OIDC_COOKIE_SECRET_ENV";
93pub const RWS_CONFIG_ADMIN_OIDC_ROLES: &str = "RWS_CONFIG_ADMIN_OIDC_ROLES";
94pub const RWS_CONFIG_ADMIN_OIDC_ROLE_CLAIM: &str = "RWS_CONFIG_ADMIN_OIDC_ROLE_CLAIM";
95
96impl AdminConfig {
97    pub fn from_env() -> AdminConfig {
98        let read = |name: &str| std::env::var(name).unwrap_or_default().trim().to_string();
99        let token_env = read(Config::RWS_CONFIG_ADMIN_TOKEN_ENV);
100        AdminConfig {
101            listen: read(Config::RWS_CONFIG_ADMIN_LISTEN),
102            token_env: if token_env.is_empty() { DEFAULT_TOKEN_ENV.to_string() } else { token_env },
103            allowed_origins: read(Config::RWS_CONFIG_ADMIN_ALLOWED_ORIGINS)
104                .split(',')
105                .map(|o| o.trim().trim_end_matches('/').to_string())
106                .filter(|o| !o.is_empty())
107                .collect(),
108            users: read(RWS_CONFIG_ADMIN_USERS),
109            audit_log: read(RWS_CONFIG_ADMIN_AUDIT_LOG),
110            oidc: AdminOidcConfig {
111                issuer: read(RWS_CONFIG_ADMIN_OIDC_ISSUER).trim_end_matches('/').to_string(),
112                provider: read(RWS_CONFIG_ADMIN_OIDC_PROVIDER).to_ascii_lowercase(),
113                tenant: read(RWS_CONFIG_ADMIN_OIDC_TENANT),
114                client_id: read(RWS_CONFIG_ADMIN_OIDC_CLIENT_ID),
115                client_secret_env: read(RWS_CONFIG_ADMIN_OIDC_CLIENT_SECRET_ENV),
116                cookie_secret_env: read(RWS_CONFIG_ADMIN_OIDC_COOKIE_SECRET_ENV),
117                roles: read(RWS_CONFIG_ADMIN_OIDC_ROLES),
118                role_claim: read(RWS_CONFIG_ADMIN_OIDC_ROLE_CLAIM),
119            },
120        }
121    }
122
123    pub fn enabled(&self) -> bool {
124        !self.listen.is_empty()
125    }
126
127    /// The token, or why there is none.
128    pub fn token(&self) -> Result<String, String> {
129        match std::env::var(&self.token_env) {
130            Ok(t) if !t.trim().is_empty() => Ok(t.trim().to_string()),
131            _ => Err(format!("the admin token variable {} is unset or empty", self.token_env)),
132        }
133    }
134}
135
136/// When the process started serving the admin API (for `uptime`).
137fn started() -> Instant {
138    static STARTED: OnceLock<Instant> = OnceLock::new();
139    *STARTED.get_or_init(Instant::now)
140}
141
142/// The admin API. See the [module docs](self).
143#[derive(Clone)]
144pub struct AdminApi {
145    inner: Arc<Inner>,
146}
147
148type Reloader = Arc<dyn Fn() + Send + Sync>;
149
150struct Inner {
151    users: Vec<access::TokenUser>,
152    allowed_origins: Vec<String>,
153    reloader: Reloader,
154    reload_wait: Duration,
155    #[cfg(feature = "sso")]
156    oidc: Option<Arc<AdminOidc>>,
157}
158
159/// OIDC sign-in for the admin UI: the lazily discovered layer and the
160/// group → role map.
161#[cfg(feature = "sso")]
162struct AdminOidc {
163    layer: crate::proxy_config::oidc_route::LazyOidc,
164    roles: Vec<(String, access::Role)>,
165}
166
167impl AdminApi {
168    /// The API accepting `token`.
169    pub fn new(token: impl Into<String>) -> AdminApi {
170        started();
171        crate::log::tail::enable();
172        AdminApi {
173            inner: Arc::new(Inner {
174                users: vec![access::TokenUser { name: "admin".to_string(), token: token.into(), role: access::Role::Admin }],
175                allowed_origins: Vec::new(),
176                reloader: Arc::new(crate::config_reload::request_reload),
177                reload_wait: Duration::from_secs(10),
178                #[cfg(feature = "sso")]
179                oidc: None,
180            }),
181        }
182    }
183
184    fn edit(self, f: impl FnOnce(&mut Inner)) -> AdminApi {
185        let mut inner = Inner {
186            users: self.inner.users.clone(),
187            allowed_origins: self.inner.allowed_origins.clone(),
188            reloader: Arc::clone(&self.inner.reloader),
189            reload_wait: self.inner.reload_wait,
190            #[cfg(feature = "sso")]
191            oidc: self.inner.oidc.clone(),
192        };
193        f(&mut inner);
194        AdminApi { inner: Arc::new(inner) }
195    }
196
197    /// More users with their own tokens and roles (18.22.0).
198    pub fn users(self, users: Vec<access::TokenUser>) -> AdminApi {
199        self.edit(|i| i.users.extend(users))
200    }
201
202    /// Sign-in to the admin UI with an OIDC provider; `roles` maps groups
203    /// (or emails, or `*`) to roles (18.22.0).
204    #[cfg(feature = "sso")]
205    pub fn oidc(self, config: &AdminOidcConfig) -> Result<AdminApi, String> {
206        let roles = access::parse_role_map(&config.roles)?;
207        if roles.is_empty() {
208            return Err("[admin] oidc_roles is empty: nobody signed in would get a role".to_string());
209        }
210        let layer = crate::proxy_config::oidc_route::LazyOidc::new(crate::proxy_config::OidcRouteAuth {
211            issuer: config.issuer.clone(),
212            provider: config.provider.clone(),
213            tenant: config.tenant.clone(),
214            client_id: config.client_id.clone(),
215            client_secret_env: config.client_secret_env.clone(),
216            cookie_secret_env: config.cookie_secret_env.clone(),
217            groups_claim: config.role_claim.clone(),
218            session_ttl_secs: 12 * 3600,
219            path_prefix: "/admin".to_string(),
220            ..Default::default()
221        })?;
222        Ok(self.edit(|i| i.oidc = Some(Arc::new(AdminOidc { layer, roles }))))
223    }
224
225    /// Browser origins allowed besides the listener's own.
226    pub fn allowed_origins(self, origins: Vec<String>) -> AdminApi {
227        self.edit(|i| i.allowed_origins = origins)
228    }
229
230    /// What `POST …/reload` runs (default: [`crate::config_reload::request_reload`],
231    /// the same path as `SIGHUP`, so TLS certificates are reloaded too), and
232    /// how long it waits for the reload to finish.
233    pub fn reloader(self, reload: impl Fn() + Send + Sync + 'static, wait: Duration) -> AdminApi {
234        self.edit(|i| {
235            i.reloader = Arc::new(reload);
236            i.reload_wait = wait;
237        })
238    }
239
240    /// Request a reload and wait for it: the reload count moving, then the
241    /// errors it reported.
242    fn reload(&self) -> Response {
243        if !crate::metrics::SERVER_READY.load(Ordering::SeqCst) {
244            // Before the servers run, SIGHUP has no handler yet.
245            return error(STATUS_CODE_REASON_PHRASE.n503_service_unavailable, "the server is starting or stopping");
246        }
247        let (before, _) = crate::config_reload::last_reload();
248        (self.inner.reloader)();
249        let deadline = Instant::now() + self.inner.reload_wait;
250        loop {
251            let (count, errors) = crate::config_reload::last_reload();
252            if count > before {
253                let errors: Vec<String> = errors.iter().map(|e| format!("\"{}\"", json_escape(e))).collect();
254                let applied = errors.is_empty();
255                return json(STATUS_CODE_REASON_PHRASE.n200_ok, format!(r#"{{"reloaded":true,"applied":{applied},"reloads":{count},"errors":[{}]}}"#, errors.join(",")));
256            }
257            if Instant::now() >= deadline {
258                return error(STATUS_CODE_REASON_PHRASE.n504_gateway_timeout, "the reload didn't finish in time; check the server log");
259            }
260            std::thread::sleep(Duration::from_millis(20));
261        }
262    }
263
264    /// Who sent `request`: a bearer token's user, else (with OIDC sign-in)
265    /// the signed-in user with the role the group map gives them.
266    fn authenticate(&self, request: &Request) -> Authenticated {
267        if let Some(sent) = request.header("Authorization").and_then(|h| h.value.strip_prefix("Bearer ").map(str::trim)) {
268            // Every user compared, so timing doesn't tell which name matched.
269            let mut found = None;
270            for user in &self.inner.users {
271                if crate::proxy_config::constant_time_eq(sent.as_bytes(), user.token.as_bytes()) {
272                    found = Some(access::Principal { user: user.name.clone(), role: user.role, via: "token" });
273                }
274            }
275            return found.map_or(Authenticated::No, Authenticated::Yes);
276        }
277        #[cfg(feature = "sso")]
278        if let Some(oidc) = &self.inner.oidc {
279            if let Some(user) = oidc.layer.auth().and_then(|auth| auth.signed_in(request)) {
280                let name = user.email.clone().unwrap_or_else(|| user.sub.clone());
281                return match access::role_for(&oidc.roles, user.email.as_deref(), &user.groups) {
282                    Some(role) => Authenticated::Yes(access::Principal { user: name, role, via: "oidc" }),
283                    None => Authenticated::NoRole(name),
284                };
285            }
286        }
287        Authenticated::No
288    }
289
290    fn oidc_enabled(&self) -> bool {
291        #[cfg(feature = "sso")]
292        return self.inner.oidc.is_some();
293        #[cfg(not(feature = "sso"))]
294        false
295    }
296
297    /// A browser on another origin is refused; non-browser clients send none.
298    fn origin_allowed(&self, request: &Request) -> bool {
299        let Some(origin) = request.header("Origin").map(|h| h.value.trim().trim_end_matches('/').to_string()) else {
300            return true;
301        };
302        let host = request.header("Host").map(|h| h.value.trim().to_string()).unwrap_or_default();
303        let own = [format!("http://{host}"), format!("https://{host}")];
304        own.contains(&origin) || self.inner.allowed_origins.contains(&origin)
305    }
306}
307
308pub(crate) fn json(status: &crate::response::StatusCodeReasonPhrase, body: String) -> Response {
309    let mut response = Response::json(status, body.into_bytes());
310    response.headers.push(Header::new("Cache-Control", "no-store"));
311    response
312}
313
314pub(crate) fn error(status: &crate::response::StatusCodeReasonPhrase, message: &str) -> Response {
315    json(status, format!(r#"{{"error":"{}"}}"#, json_escape(message)))
316}
317
318impl Application for AdminApi {
319    fn execute(&self, request: &Request, connection: &ConnectionInfo) -> Result<Response, String> {
320        if !self.origin_allowed(request) {
321            return Ok(error(STATUS_CODE_REASON_PHRASE.n403_forbidden, "origin not allowed"));
322        }
323        let path = crate::url::URL::request_target_path(&request.request_uri).unwrap_or_default();
324        if request.method == "GET" || request.method == "HEAD" {
325            if path == "/admin" || path == "/" {
326                return Ok(ui::redirect());
327            }
328            if let Some(file) = ui::file(path) {
329                return Ok(file);
330            }
331        }
332        // OIDC sign-in: /admin/auth/login, /callback, /logout.
333        if path.starts_with("/admin/auth/") {
334            #[cfg(feature = "sso")]
335            if let Some(oidc) = &self.inner.oidc {
336                return match oidc.layer.auth() {
337                    Some(auth) => crate::middleware::Middleware::handle(&*auth, request, connection, &NotFound),
338                    None => Ok(error(STATUS_CODE_REASON_PHRASE.n503_service_unavailable, "the identity provider can't be reached; try again shortly")),
339                };
340            }
341            return Ok(error(STATUS_CODE_REASON_PHRASE.n404_not_found, "OIDC sign-in isn't configured"));
342        }
343        let route = path.strip_prefix(API_PREFIX).map(|r| r.trim_end_matches('/'));
344        // How to sign in, for the UI's login page: no credentials needed.
345        if route == Some("/auth") && request.method == "GET" {
346            return Ok(ok(format!(r#"{{"token":true,"oidc":{}}}"#, self.oidc_enabled())));
347        }
348        let address = connection.client.ip.clone();
349        let principal = match self.authenticate(request) {
350            Authenticated::Yes(p) => p,
351            Authenticated::NoRole(name) => {
352                let response = error(STATUS_CODE_REASON_PHRASE.n403_forbidden, &format!("{name} is signed in but has no admin role (see [admin] oidc_roles)"));
353                if !is_read(&request.method) {
354                    access::record_change(&access::Principal { user: name, role: access::Role::Viewer, via: "oidc" }, &address, &request.method, path, &request.body, 403);
355                }
356                return Ok(response);
357            }
358            Authenticated::No => {
359                if !is_read(&request.method) && request.header("Authorization").is_some() {
360                    access::record_change(&access::Principal { user: "(bad token)".to_string(), role: access::Role::Viewer, via: "token" }, &address, &request.method, path, &request.body, 401);
361                }
362                let mut response = error(STATUS_CODE_REASON_PHRASE.n401_unauthorized, "missing or wrong bearer token");
363                response.headers.push(Header::new("WWW-Authenticate", r#"Bearer realm="rws admin""#));
364                return Ok(response);
365            }
366        };
367        let Some(route) = route else {
368            return Ok(error(STATUS_CODE_REASON_PHRASE.n404_not_found, "not found"));
369        };
370        // A browser signed in with a cookie: changes need the header only
371        // this origin's script can set (CSRF), besides the Origin check.
372        if principal.via == "oidc" && !is_read(&request.method) && request.header("X-Rws-Admin").is_none() {
373            access::record_change(&principal, &address, &request.method, path, &request.body, 403);
374            return Ok(error(STATUS_CODE_REASON_PHRASE.n403_forbidden, "missing X-Rws-Admin header"));
375        }
376        let needed = access::required_role(&request.method, route);
377        if principal.role < needed {
378            access::record_change(&principal, &address, &request.method, path, &request.body, 403);
379            return Ok(error(STATUS_CODE_REASON_PHRASE.n403_forbidden, &format!("{} has role {}; this needs {}", principal.user, principal.role.as_str(), needed.as_str())));
380        }
381        let response = match (request.method.as_str(), route) {
382            ("GET", "/whoami") => ok(format!(r#"{{"user":"{}","role":"{}","via":"{}"}}"#, json_escape(&principal.user), principal.role.as_str(), principal.via)),
383            ("GET", "/audit") => ok(access::recent_json(query(request, "n").and_then(|n| n.parse().ok()).unwrap_or(100).min(access::AUDIT_CAPACITY))),
384            (_, "/whoami" | "/audit") => wrong_method("GET"),
385            _ => self.route(request, connection, route),
386        };
387        if !is_read(&request.method) && !(request.method == "POST" && route == "/authz/check") {
388            access::record_change(&principal, &address, &request.method, path, &request.body, response.status_code as u16);
389        }
390        Ok(response)
391    }
392}
393
394/// The outcome of [`AdminApi::authenticate`].
395enum Authenticated {
396    Yes(access::Principal),
397    /// Signed in (OIDC), but no role maps to them.
398    #[cfg_attr(not(feature = "sso"), allow(dead_code))]
399    NoRole(String),
400    No,
401}
402
403fn is_read(method: &str) -> bool {
404    matches!(method, "GET" | "HEAD" | "OPTIONS")
405}
406
407/// The rest of the stack behind the OIDC layer on `/admin/auth/*`: the
408/// layer answers those paths itself.
409#[cfg(feature = "sso")]
410struct NotFound;
411
412#[cfg(feature = "sso")]
413impl Application for NotFound {
414    fn execute(&self, _: &Request, _: &ConnectionInfo) -> Result<Response, String> {
415        Ok(error(STATUS_CODE_REASON_PHRASE.n404_not_found, "not found"))
416    }
417}
418
419fn ok(body: String) -> Response {
420    json(STATUS_CODE_REASON_PHRASE.n200_ok, body)
421}
422
423fn bad_request(message: &str) -> Response {
424    error(STATUS_CODE_REASON_PHRASE.n400_bad_request, message)
425}
426
427/// The JSON body's top-level boolean `key`.
428fn body_bool(request: &Request, key: &str) -> Option<bool> {
429    let body = std::str::from_utf8(&request.body).ok()?;
430    let value = crate::mcp::json_rpc::object_members(body)?.into_iter().find(|(k, _)| k == key)?.1;
431    match value.as_str() {
432        "true" => Some(true),
433        "false" => Some(false),
434        _ => None,
435    }
436}
437
438/// The JSON body's top-level string `key`.
439fn body_str(request: &Request, key: &str) -> Option<String> {
440    let body = std::str::from_utf8(&request.body).ok()?;
441    let value = crate::mcp::json_rpc::object_members(body)?.into_iter().find(|(k, _)| k == key)?.1;
442    value.starts_with('"').then(|| crate::mcp::json_rpc::extract_str(&format!(r#"{{"v":{value}}}"#), "v")).flatten()
443}
444
445/// `405`, naming the methods that work.
446fn wrong_method(allowed: &str) -> Response {
447    let mut response = error(STATUS_CODE_REASON_PHRASE.n405_method_not_allowed, &format!("use {allowed}"));
448    response.headers.push(Header::new("Allow", allowed.to_string()));
449    response
450}
451
452/// `/authz`, `/authz/tuples`, `/authz/check` (#32).
453fn authz_route(method: &str, route: &str, request: &Request) -> Response {
454    let decoded = |key: &str| query(request, key).map(|v| crate::url::URL::percent_decode(&v)).filter(|v| !v.is_empty());
455    // A configuration problem is a 404 ("nothing to manage"), a bad request
456    // a 400.
457    let answer = |result: Result<String, String>, missing_is_404: bool| match result {
458        Ok(body) => ok(body),
459        Err(e) if missing_is_404 && e.starts_with("no authorization configured") => error(STATUS_CODE_REASON_PHRASE.n404_not_found, &e),
460        Err(e) => error(STATUS_CODE_REASON_PHRASE.n400_bad_request, &e),
461    };
462    match (method, route) {
463        ("GET", "/authz") => answer(ops::authz_schema_json(), true),
464        (_, "/authz") => wrong_method("GET"),
465        ("GET", "/authz/tuples") => {
466            let limit = decoded("limit").and_then(|n| n.parse().ok()).unwrap_or(100);
467            answer(ops::authz_tuples_json(decoded("object").as_deref(), decoded("relation").as_deref(), decoded("subject").as_deref(), limit), true)
468        }
469        ("POST", "/authz/tuples") => match body_str(request, "tuple") {
470            Some(tuple) => answer(ops::authz_write(&tuple), true),
471            None => bad_request(r#"expected {"tuple":"doc:42#viewer@user:alice"}"#),
472        },
473        ("DELETE", "/authz/tuples") => match decoded("tuple").or_else(|| body_str(request, "tuple")) {
474            Some(tuple) => answer(ops::authz_delete(&tuple), true),
475            None => bad_request(r#"expected ?tuple=… or {"tuple":"doc:42#viewer@user:alice"}"#),
476        },
477        (_, "/authz/tuples") => wrong_method("GET, POST, DELETE"),
478        ("POST", "/authz/check") => match (body_str(request, "subject"), body_str(request, "relation"), body_str(request, "object")) {
479            (Some(subject), Some(relation), Some(object)) => answer(ops::authz_check(&subject, &relation, &object), true),
480            _ => bad_request(r#"expected {"subject":"user:alice","relation":"viewer","object":"doc:42"}"#),
481        },
482        (_, "/authz/check") => wrong_method("POST"),
483        _ => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "unknown /authz endpoint"),
484    }
485}
486
487/// Query parameter `key` of the request target.
488fn query(request: &Request, key: &str) -> Option<String> {
489    let (_, query) = request.request_uri.split_once('?')?;
490    query.split('&').find_map(|pair| {
491        let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
492        (k == key).then(|| v.to_string())
493    })
494}
495
496fn parse_ip(ip: &str) -> Option<String> {
497    let ip = crate::url::URL::percent_decode(ip.trim());
498    ip.parse::<std::net::IpAddr>().ok().map(|ip| ip.to_string())
499}
500
501impl AdminApi {
502    fn route(&self, request: &Request, connection: &ConnectionInfo, route: &str) -> Response {
503        let method = request.method.as_str();
504        if let Some(ip) = route.strip_prefix("/blocklist/") {
505            return match (method, parse_ip(ip)) {
506                ("DELETE", Some(ip)) => ok(ops::unblock(&ip)),
507                ("DELETE", None) => bad_request("not an IP address"),
508                _ => wrong_method("DELETE"),
509            };
510        }
511        if let Some(name) = route.strip_prefix("/flags/") {
512            let name = crate::url::URL::percent_decode(name);
513            return match method {
514                "PUT" => match body_bool(request, "enabled") {
515                    Some(enabled) if !name.is_empty() => ok(ops::set_flag(&name, enabled)),
516                    _ => bad_request(r#"expected {"enabled":true|false}"#),
517                },
518                _ => wrong_method("PUT"),
519            };
520        }
521        if let Some(name) = route.strip_prefix("/upstreams/").and_then(|r| r.strip_suffix("/drain")) {
522            let name = crate::url::URL::percent_decode(name);
523            return match (method, body_str(request, "backend"), body_bool(request, "drained")) {
524                ("PUT", Some(backend), Some(drained)) => match ops::set_drained(&name, &backend, drained) {
525                    Ok(body) => ok(body),
526                    Err(e) => error(STATUS_CODE_REASON_PHRASE.n404_not_found, &e),
527                },
528                ("PUT", _, _) => bad_request(r#"expected {"backend":"<url>","drained":true|false}"#),
529                _ => wrong_method("PUT"),
530            };
531        }
532        if let Some(name) = route.strip_prefix("/caches/") {
533            let name = crate::url::URL::percent_decode(name);
534            let decoded = |key: &str| query(request, key).map(|v| crate::url::URL::percent_decode(&v));
535            return match method {
536                "DELETE" => match ops::purge_cache(&name, decoded("path").as_deref(), decoded("prefix").as_deref()) {
537                    Some(body) => ok(body),
538                    None => error(STATUS_CODE_REASON_PHRASE.n404_not_found, &format!("no cache named {name:?}")),
539                },
540                _ => wrong_method("DELETE"),
541            };
542        }
543        if route == "/traffic" {
544            if method != "GET" {
545                return wrong_method("GET");
546            }
547            let hours = query(request, "hours").and_then(|h| h.parse().ok()).unwrap_or(24);
548            let host = query(request, "host").map(|h| crate::url::URL::percent_decode(&h));
549            return match ops::traffic_json(hours, host.as_deref()) {
550                Ok(body) => ok(body),
551                Err(e) => error(STATUS_CODE_REASON_PHRASE.n404_not_found, &e),
552            };
553        }
554        if route == "/authz" || route.starts_with("/authz/") {
555            return authz_route(method, route, request);
556        }
557        match (method, route) {
558            ("GET", "/caches") => ok(ops::caches_json()),
559            (_, "/caches") => wrong_method("GET"),
560            ("GET", "/logs") => {
561                let n = query(request, "n").and_then(|n| n.parse().ok()).unwrap_or(100);
562                ok(ops::logs_json(n))
563            }
564            (_, "/logs") => wrong_method("GET"),
565            ("GET", "/logs/stream") => {
566                let (sender, mut response) = crate::sse::SseStream::new().buffer(256).start();
567                if !crate::log::tail::follow(sender) {
568                    return error(STATUS_CODE_REASON_PHRASE.n503_service_unavailable, "too many log followers");
569                }
570                response.headers.push(Header::new("Cache-Control", "no-store"));
571                response
572            }
573            (_, "/logs/stream") => wrong_method("GET"),
574            ("GET", "/upstreams") => ok(ops::upstreams_json()),
575            (_, "/upstreams") => wrong_method("GET"),
576            ("GET", "/certificates") => ok(ops::certificates_json()),
577            (_, "/certificates") => wrong_method("GET"),
578            ("POST", "/certificates/renew") => match ops::request_renewal() {
579                true => json(STATUS_CODE_REASON_PHRASE.n202_accepted, r#"{"requested":true}"#.to_string()),
580                false => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "ACME isn't configured: nothing to renew"),
581            },
582            (_, "/certificates/renew") => wrong_method("POST"),
583            ("GET", "/status") => ok(status_json(connection)),
584            (_, "/status") => wrong_method("GET"),
585            ("GET", "/config") => ok(ops::config_json()),
586            (_, "/config") => wrong_method("GET"),
587            ("POST", "/reload") => self.reload(),
588            (_, "/reload") => wrong_method("POST"),
589            ("GET", "/metrics") => ok(ops::metrics_json()),
590            (_, "/metrics") => wrong_method("GET"),
591            ("GET", "/build") => ok(ops::build_features_json()),
592            (_, "/build") => wrong_method("GET"),
593            ("GET", "/rate-limit") => ok(ops::rate_limit_json()),
594            (_, "/rate-limit") => wrong_method("GET"),
595            ("GET", "/rate-limit/check") => match query(request, "ip").as_deref().and_then(parse_ip) {
596                Some(ip) => ok(ops::check_rate_limit_json(&ip)),
597                None => bad_request("expected ?ip=<address>"),
598            },
599            (_, "/rate-limit/check") => wrong_method("GET"),
600            ("GET", "/cors") => ok(ops::cors_json()),
601            (_, "/cors") => wrong_method("GET"),
602            ("GET", "/maintenance") => ok(ops::maintenance_json()),
603            ("PUT", "/maintenance") => match body_bool(request, "active") {
604                Some(active) => ok(ops::set_maintenance(active)),
605                None => bad_request(r#"expected {"active":true|false}"#),
606            },
607            (_, "/maintenance") => wrong_method("GET, PUT"),
608            ("GET", "/blocklist") => ok(ops::blocklist_json()),
609            ("POST", "/blocklist") => match body_str(request, "ip").as_deref().and_then(parse_ip) {
610                Some(ip) => ok(ops::block(&ip)),
611                None => bad_request(r#"expected {"ip":"<address>"}"#),
612            },
613            (_, "/blocklist") => wrong_method("GET, POST"),
614            ("GET", "/flags") => ok(ops::flags_json()),
615            (_, "/flags") => wrong_method("GET"),
616            ("GET", "/cache") => match ops::registered_cache_json() {
617                Some(stats) => ok(stats),
618                None => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "no response cache in this server"),
619            },
620            ("DELETE", "/cache") => match ops::clear_registered_cache() {
621                true => ok(r#"{"cleared":true}"#.to_string()),
622                false => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "no response cache in this server"),
623            },
624            (_, "/cache") => wrong_method("GET, DELETE"),
625            ("GET", "/routes") => ok(ops::registered_routes_json()),
626            (_, "/routes") => wrong_method("GET"),
627            ("GET", "/requests" | "/errors") => {
628                let n = query(request, "n").and_then(|n| n.parse().ok()).unwrap_or(20);
629                ok(ops::recent_json(n, route == "/errors"))
630            }
631            (_, "/requests" | "/errors") => wrong_method("GET"),
632            _ => error(STATUS_CODE_REASON_PHRASE.n404_not_found, "not found"),
633        }
634    }
635}
636
637/// Compile-time features of this build that matter to an operator.
638pub fn features() -> Vec<&'static str> {
639    let mut on = Vec::new();
640    macro_rules! feature {
641        ($($name:literal),*) => { $( if cfg!(feature = $name) { on.push($name); } )* };
642    }
643    feature!("http1", "http2", "http3", "acme", "auth", "sso", "sso-saml", "sso-server", "gateway-api", "tower", "openapi", "mimalloc", "brotli", "zstd", "wasm-plugins", "fips");
644    on
645}
646
647fn fleet_status() -> String {
648    #[cfg(feature = "http2")]
649    return crate::fleet::status_json().unwrap_or_else(|| "null".to_string());
650    #[cfg(not(feature = "http2"))]
651    "null".to_string()
652}
653
654fn status_json(connection: &ConnectionInfo) -> String {
655    let env = |name: &str| std::env::var(name).unwrap_or_default();
656    let tls = !env(Config::RWS_CONFIG_TLS_CERT_FILE).is_empty() || !env(Config::RWS_CONFIG_ACME_DOMAINS).is_empty();
657    let redirect = env(Config::RWS_CONFIG_HTTP_REDIRECT_PORT);
658    let features: Vec<String> = features().iter().map(|f| format!("\"{f}\"")).collect();
659    // Where rws writes files (access log, disk cache): free space and
660    // whether it's under RWS_CONFIG_MIN_FREE_DISK (18.15.0).
661    let disk: Vec<String> = crate::disk_guard::status()
662        .into_iter()
663        .map(|(dir, free, total, low)| format!(r#"{{"dir":"{}","free_bytes":{free},"total_bytes":{total},"low":{low}}}"#, json_escape(&dir)))
664        .collect();
665    format!(
666        r#"{{"version":"{}","pid":{},"uptime_secs":{},"ready":{},"features":[{}],"listeners":{{"main":{{"address":"{}:{}","tls":{tls}}},"redirect":{},"admin":"{}:{}"}},"disk":[{}],"cluster":{},"fleet":{}}}"#,
667        env!("CARGO_PKG_VERSION"),
668        std::process::id(),
669        started().elapsed().as_secs(),
670        crate::metrics::SERVER_READY.load(Ordering::SeqCst),
671        features.join(","),
672        json_escape(&env(Config::RWS_CONFIG_IP)),
673        json_escape(&env(Config::RWS_CONFIG_PORT)),
674        if redirect.trim().is_empty() { "null".to_string() } else { format!("\"{}\"", json_escape(redirect.trim())) },
675        json_escape(&connection.server.ip),
676        connection.server.port,
677        disk.join(","),
678        crate::cluster::status_json().unwrap_or_else(|| "null".to_string()),
679        fleet_status(),
680    )
681}
682
683/// Semantic checks of `[admin]` for `rws --check`; `get` reads a setting
684/// of the file.
685pub(crate) fn validate(get: &dyn Fn(&str) -> Option<String>, report: &mut crate::config_check::ConfigReport) {
686    let listen = get(Config::RWS_CONFIG_ADMIN_LISTEN).unwrap_or_default();
687    let listen = listen.trim();
688    if listen.is_empty() {
689        for key in [Config::RWS_CONFIG_ADMIN_TOKEN_ENV, Config::RWS_CONFIG_ADMIN_ALLOWED_ORIGINS] {
690            if get(key).is_some_and(|v| !v.trim().is_empty()) {
691                report.warn("[admin] has settings but no listen: the admin API is off");
692                break;
693            }
694        }
695        return;
696    }
697    match listen.parse::<std::net::SocketAddr>() {
698        Err(_) => report.error(format!("[admin] listen = {listen:?}: expected host:port with an IP address, e.g. \"127.0.0.1:9901\"")),
699        Ok(addr) if !addr.ip().is_loopback() => report.warn(format!(
700            "[admin] listen = {listen:?} is reachable from other hosts: firewall it, or listen on 127.0.0.1 and use an SSH tunnel"
701        )),
702        Ok(_) => {}
703    }
704    let token_env = get(Config::RWS_CONFIG_ADMIN_TOKEN_ENV).map(|v| v.trim().to_string()).filter(|v| !v.is_empty());
705    let token_env = token_env.unwrap_or_else(|| DEFAULT_TOKEN_ENV.to_string());
706    if !crate::config_check::offline() && std::env::var(&token_env).unwrap_or_default().trim().is_empty() {
707        report.error(format!("[admin] reads its token from {token_env}, which is unset or empty: the admin API won't start"));
708    }
709    for origin in get(Config::RWS_CONFIG_ADMIN_ALLOWED_ORIGINS).unwrap_or_default().split(',').map(str::trim).filter(|o| !o.is_empty()) {
710        if !(origin.starts_with("http://") || origin.starts_with("https://")) || origin.trim_end_matches('/').matches('/').count() > 2 {
711            report.error(format!("[admin] allowed_origins: {origin:?} is not an origin (scheme://host[:port])"));
712        }
713    }
714    // 18.22.0: users, the audit log, OIDC sign-in.
715    let users = get(RWS_CONFIG_ADMIN_USERS).unwrap_or_default();
716    let env = |name: &str| if crate::config_check::offline() { Some("offline-check-placeholder".to_string()) } else { std::env::var(name).ok() };
717    if let Err(e) = access::parse_users(&users, &env) {
718        report.error(e);
719    }
720    let audit_log = get(RWS_CONFIG_ADMIN_AUDIT_LOG).unwrap_or_default();
721    if let Some(dir) = std::path::Path::new(audit_log.trim()).parent().filter(|d| !audit_log.trim().is_empty() && !d.as_os_str().is_empty()) {
722        if !crate::config_check::offline() && !dir.is_dir() {
723            report.error(format!("[admin] audit_log: directory {} doesn't exist", dir.display()));
724        }
725    }
726    let issuer = get(RWS_CONFIG_ADMIN_OIDC_ISSUER).unwrap_or_default();
727    let provider = get(RWS_CONFIG_ADMIN_OIDC_PROVIDER).unwrap_or_default();
728    if !issuer.trim().is_empty() || !provider.trim().is_empty() {
729        if get(RWS_CONFIG_ADMIN_OIDC_CLIENT_ID).unwrap_or_default().trim().is_empty() {
730            report.error("[admin] oidc sign-in has no oidc_client_id");
731        }
732        match access::parse_role_map(&get(RWS_CONFIG_ADMIN_OIDC_ROLES).unwrap_or_default()) {
733            Ok(map) if map.is_empty() => report.error("[admin] oidc sign-in needs oidc_roles (e.g. [\"rws-admins=admin\", \"*=viewer\"]): nobody would get a role"),
734            Ok(_) => {}
735            Err(e) => report.error(e),
736        }
737        if !provider.trim().is_empty() && !matches!(provider.trim().to_ascii_lowercase().as_str(), "google" | "microsoft" | "github") {
738            report.error(format!("[admin] oidc_provider = {provider:?}: expected google, microsoft or github (others: set oidc_issuer)"));
739        }
740        let cookie = get(RWS_CONFIG_ADMIN_OIDC_COOKIE_SECRET_ENV).unwrap_or_default();
741        if cookie.trim().is_empty() {
742            report.warn("[admin] oidc sign-in without oidc_cookie_secret_env: sign-ins end when rws restarts");
743        }
744        #[cfg(not(feature = "sso"))]
745        report.error("[admin] oidc sign-in needs the sso feature, which this build doesn't include; the admin API won't start");
746    }
747}
748
749/// Start the admin listener `config` describes, on a thread of its own.
750/// `Ok(None)` when it is off.
751pub fn start(config: &AdminConfig) -> Result<Option<std::net::SocketAddr>, String> {
752    if !config.enabled() {
753        return Ok(None);
754    }
755    let token = config.token()?;
756    #[cfg(unix)]
757    let inherited = config.listen.parse().ok().and_then(|addr| crate::systemd::tcp(addr, Some("admin")));
758    #[cfg(not(unix))]
759    let inherited: Option<std::net::TcpListener> = None;
760    let listener = match inherited {
761        Some(listener) => listener,
762        None => std::net::TcpListener::bind(&config.listen).map_err(|e| format!("can't listen on {}: {e}", config.listen))?,
763    };
764    let local = listener.local_addr().map_err(|e| e.to_string())?;
765    let users = access::parse_users(&config.users, &|name| std::env::var(name).ok())?;
766    access::set_audit_file(&config.audit_log);
767    #[allow(unused_mut)]
768    let mut api = AdminApi::new(token).allowed_origins(config.allowed_origins.clone()).users(users);
769    if config.oidc.enabled() {
770        #[cfg(feature = "sso")]
771        {
772            api = api.oidc(&config.oidc)?;
773        }
774        #[cfg(not(feature = "sso"))]
775        return Err("[admin] oidc_* needs the sso feature, which this build doesn't include".to_string());
776    }
777    std::thread::Builder::new()
778        .name("rws-admin".to_string())
779        .spawn(move || serve(listener, api))
780        .map_err(|e| e.to_string())?;
781    Ok(Some(local))
782}
783
784/// [`start`] from `RWS_CONFIG_ADMIN_*`, reporting the outcome: the binary's
785/// entry point. A misconfigured admin API is logged, not fatal.
786pub fn start_from_env() {
787    match start(&AdminConfig::from_env()) {
788        Ok(Some(addr)) => {
789            println!("admin API at http://{addr}{API_PREFIX}/");
790            // Traffic insights are shown by the admin UI: counted when it's
791            // on, unless `[traffic] enabled = false` (18.15.0).
792            if crate::traffic::config::enabled_setting() != Some(false) {
793                crate::traffic::start();
794            }
795        }
796        Ok(None) => {}
797        Err(e) => eprintln!("admin API not started: {e}"),
798    }
799}
800
801fn serve(listener: std::net::TcpListener, api: AdminApi) {
802    let open = Arc::new(AtomicUsize::new(0));
803    for stream in listener.incoming() {
804        let Ok(stream) = stream else { continue };
805        if open.fetch_add(1, Ordering::SeqCst) >= MAX_CONNECTIONS {
806            open.fetch_sub(1, Ordering::SeqCst);
807            continue;
808        }
809        let (api, held) = (api.clone(), Arc::clone(&open));
810        let spawned = std::thread::Builder::new().name("rws-admin-conn".to_string()).spawn(move || {
811            let address = |a: Option<std::net::SocketAddr>| match a {
812                Some(a) => Address { ip: a.ip().to_string(), port: a.port() as i32 },
813                None => Address { ip: String::new(), port: 0 },
814            };
815            let mut connection = ConnectionInfo::new(address(stream.peer_addr().ok()), address(stream.local_addr().ok()));
816            connection.request_size = 16 * 1024;
817            let _ = stream.set_read_timeout(Some(Duration::from_secs(30)));
818            let _ = stream.set_nodelay(true);
819            let _ = crate::server::Server::process(stream, connection, api);
820            held.fetch_sub(1, Ordering::SeqCst);
821        });
822        if spawned.is_err() {
823            open.fetch_sub(1, Ordering::SeqCst);
824        }
825    }
826}
827
828#[cfg(test)]
829mod tests;