Skip to main content

acme_proxy/webadmin/
mod.rs

1//! The web admin interface: a second HTTP listener, serving no ACME.
2//!
3//! ## Where this sits
4//!
5//! `src/cli/` and `src/webadmin/` are the two **front ends**; `src/admin/` is
6//! the operation layer both dispatch to and neither owns. A handler here is a
7//! few lines over an `admin::ops` call and an `admin::render_*_json`, the same
8//! way a `src/cli/` command body is a few lines over the same call and a
9//! `render_*_line`.
10//!
11//! ## Why a second listener
12//!
13//! The ACME listener is public, unauthenticated and often internet-facing.
14//! This one defaults to loopback, requires a session on every route but login,
15//! and carries no admission control or filter chain because neither fits it
16//! (see `build_admin_app`). Keeping them on one socket would have meant one
17//! set of defaults for two very different threat models.
18
19pub mod error;
20pub mod handlers;
21pub mod pages;
22pub mod session;
23
24pub use error::AdminError;
25pub use pages::PageError;
26pub use session::LoginLimiter;
27
28use std::collections::HashMap;
29use std::sync::Arc;
30
31use anyhow::bail;
32use axum::extract::DefaultBodyLimit;
33use axum::http::{HeaderValue, StatusCode, header};
34use axum::response::Redirect;
35use axum::routing::{get, post};
36use axum::{Router, middleware};
37use tower_http::set_header::SetResponseHeaderLayer;
38use tracing::{info, warn};
39
40use crate::Profile;
41use crate::config::Config;
42use crate::middlewares;
43use crate::sqlite::db::Database;
44
45/// Shared state for every admin route.
46///
47/// Not [`crate::AppState`]: that one holds exactly one `Profile`, and this
48/// listener is cross-profile by nature — an operator lists accounts from every
49/// endpoint at once, and revoking an order needs *that order's own* profile's
50/// signer, which may be a different CA from the one the request arrived
51/// through.
52#[derive(Clone)]
53pub struct AdminState {
54    pub database: Arc<Database>,
55    pub config: Arc<Config>,
56    /// Keyed by profile name — the lookup `orders.profile` needs.
57    pub profiles: Arc<HashMap<String, Arc<Profile>>>,
58    pub logins: Arc<LoginLimiter>,
59    /// The `/ui` templates, embedded defaults overlaid by
60    /// `admin.template_dir`. Built once: the loader reads a file per template
61    /// on first use, and rebuilding it per request would mean a disk read per
62    /// page.
63    pub templates: Arc<minijinja::Environment<'static>>,
64    /// The same process-wide auditor the ACME listener holds. Shared rather
65    /// than a second instance: an operator revoking through the panel writes
66    /// into the one trail, and the reverse-lookup cache is worth sharing.
67    pub audit: Arc<crate::audit::Auditor>,
68}
69
70impl AdminState {
71    /// Builds the state from what `serve_on` already has in hand.
72    ///
73    /// Takes a **slice**, not the `Vec`: `build_app` consumes that vec, so the
74    /// admin side has to be built first, and expressing it in the signature is
75    /// what stops the ordering being rediscovered as a borrow error.
76    #[must_use]
77    pub fn new(
78        database: Arc<Database>,
79        config: Arc<Config>,
80        profiles: &[Arc<Profile>],
81        audit: Arc<crate::audit::Auditor>,
82    ) -> Self {
83        Self::with_logins(database, config, profiles, audit, None)
84    }
85
86    /// [`new`](Self::new), carrying the previous generation's login counters.
87    ///
88    /// Only a configuration reload passes `Some`: every other caller is building
89    /// the first generation, where there is nothing to carry. See
90    /// [`LoginLimiter::rebuilt`] for why the counters move but the limits do not.
91    #[must_use]
92    pub fn with_logins(
93        database: Arc<Database>,
94        config: Arc<Config>,
95        profiles: &[Arc<Profile>],
96        audit: Arc<crate::audit::Auditor>,
97        previous_logins: Option<&LoginLimiter>,
98    ) -> Self {
99        let by_name = profiles
100            .iter()
101            .map(|profile| (profile.name.clone(), profile.clone()))
102            .collect();
103        let max_attempts = config.admin.login_max_attempts;
104        let window = config.admin.login_window_seconds;
105        let logins = match previous_logins {
106            Some(previous) => previous.rebuilt(max_attempts, window),
107            None => LoginLimiter::new(max_attempts, window),
108        };
109        let templates = pages::templates::build_environment(&config.admin.template_dir);
110        Self {
111            database,
112            config,
113            profiles: Arc::new(by_name),
114            logins: Arc::new(logins),
115            templates: Arc::new(templates),
116            audit,
117        }
118    }
119}
120
121/// Builds the whole admin service: `/health`, then the JSON API under `/api`.
122///
123/// Takes `profiles` as a **slice** so it can be called before `build_app`
124/// consumes the `Vec` in `cli::serve_on` — the ordering is a real constraint
125/// and the signature is where it is stated.
126///
127/// ## What this router deliberately does *not* have
128///
129/// - **No admission control.** `Admission` exists because the ACME surface is
130///   public and unauthenticated. This one defaults to loopback and needs a
131///   session on every route but login; the real availability concern is
132///   credential brute force, which admission control would not touch and the
133///   login limiter does.
134/// - **No filter chain.** Filters are a per-profile ACME concern, and
135///   `filter.exempt_paths` matches profile-stripped paths. Wiring them here
136///   would be a category error. Access control on this listener is the bind
137///   address, TLS, and the session.
138pub fn build_admin_app(
139    database: Arc<Database>,
140    config: Arc<Config>,
141    profiles: &[Arc<Profile>],
142    audit: Arc<crate::audit::Auditor>,
143) -> Router {
144    build_admin_app_with_logins(database, config, profiles, audit, None).0
145}
146
147/// [`build_admin_app`], carrying login counters across a configuration reload.
148///
149/// Returns the limiter it ended up with as well as the router, because the
150/// generation after this one has to carry it in turn — an `Arc<LoginLimiter>`
151/// that only ever moved forward is the whole point.
152pub fn build_admin_app_with_logins(
153    database: Arc<Database>,
154    config: Arc<Config>,
155    profiles: &[Arc<Profile>],
156    audit: Arc<crate::audit::Auditor>,
157    previous_logins: Option<&LoginLimiter>,
158) -> (Router, Arc<LoginLimiter>) {
159    let state = AdminState::with_logins(database, config.clone(), profiles, audit, previous_logins);
160    let logins = state.logins.clone();
161
162    let api = Router::new()
163        .route(
164            "/session",
165            post(handlers::post_session)
166                .get(handlers::get_session)
167                .delete(handlers::delete_session),
168        )
169        // The second half of signing in: reachable only with a `pending_mfa`
170        // cookie, which every other route here refuses.
171        .route(
172            "/session/mfa",
173            get(handlers::get_session_mfa).post(handlers::post_session_mfa),
174        )
175        // The operator's own second factor. Not a server resource like the ones
176        // below — it is about whoever is holding this cookie, which is why
177        // there is no id in any of these paths.
178        .route("/mfa", get(handlers::get_mfa))
179        .route(
180            "/mfa/totp",
181            post(handlers::begin_totp).delete(handlers::disable_totp),
182        )
183        .route("/mfa/totp/confirm", post(handlers::confirm_totp))
184        .route(
185            "/mfa/recovery-codes",
186            post(handlers::regenerate_recovery_codes),
187        )
188        .route("/accounts", get(handlers::list_accounts))
189        .route(
190            "/accounts/{id}",
191            get(handlers::get_account)
192                .patch(handlers::patch_account)
193                .delete(handlers::delete_account),
194        )
195        .route("/accounts/{id}/orders", get(handlers::list_account_orders))
196        .route(
197            "/accounts/{id}/deactivate",
198            post(handlers::deactivate_account),
199        )
200        .route("/orders", get(handlers::list_orders))
201        .route(
202            "/orders/{id}",
203            get(handlers::get_order).delete(handlers::delete_order),
204        )
205        .route("/orders/{id}/revoke", post(handlers::revoke_order))
206        .route("/eab", get(handlers::list_eab).post(handlers::create_eab))
207        .route("/eab/{kid}", get(handlers::get_eab))
208        .route("/eab/{kid}/revoke", post(handlers::revoke_eab))
209        // Read-only, and therefore absent from `mutating_endpoints()`: there
210        // is no route here that writes an audit row, by design. The trail is
211        // pruned from the host (`acme-proxy audit cleanup`) or by
212        // `audit.retention_days`, never through a browser session — a panel
213        // that can delete its own audit history is a panel whose audit history
214        // proves nothing.
215        .route("/audit", get(handlers::list_audit))
216        .route("/audit/{id}", get(handlers::get_audit))
217        .route("/nonces", get(handlers::get_nonces))
218        .route("/nonces/cleanup", post(handlers::cleanup_nonces))
219        .route("/profiles", get(handlers::list_profiles));
220
221    let router = Router::new()
222        // Unauthenticated and touching no database: an orchestrator probing
223        // this port should not need a session to learn the process is alive.
224        .route("/health", get(crate::handlers::get_health_check))
225        // The panel is what somebody opening this port in a browser wants.
226        .route("/", get(|| async { Redirect::to("/ui/") }))
227        .merge(api_with_fallbacks(api))
228        .merge(pages::pages_router())
229        // HTML, because this listener is browser-facing and every path that
230        // reaches here is one a person typed. `/api`'s own JSON fallback is
231        // scoped inside its nest and is unaffected, so a script still gets the
232        // admin error shape on the paths a script uses.
233        .fallback(|| async { PageError::not_found("no such page") })
234        .with_state(state)
235        .layer(DefaultBodyLimit::max(config.admin.max_body_bytes))
236        // `no-store` is not decoration: account contacts and a freshly minted
237        // EAB secret must not sit in a disk cache after the operator closes
238        // the tab.
239        .layer(SetResponseHeaderLayer::overriding(
240            header::CACHE_CONTROL,
241            HeaderValue::from_static("no-store"),
242        ))
243        .layer(SetResponseHeaderLayer::overriding(
244            header::REFERRER_POLICY,
245            HeaderValue::from_static("same-origin"),
246        ))
247        // The same three the ACME app applies. This router is not nested
248        // inside `build_app` and inherits none of its layers, so they have to
249        // be applied again here — but from the one constructor, since two
250        // hand-written copies of a security control are a control that drifts.
251        .layer(crate::security_headers())
252        // Strict, and affordable only because of how the pages are built:
253        // htmx is served from this origin (`script-src 'self'`) and drives
254        // everything through `hx-*` attributes rather than inline handlers, so
255        // no `'unsafe-inline'` and no `'unsafe-eval'` are needed. `style-src
256        // 'self'` is why `layout.html` sets htmx's `includeIndicatorStyles` to
257        // false -- htmx would otherwise inject a <style> element this refuses.
258        // `default-src 'none'` means anything added later has to be allowed
259        // deliberately rather than inherited by accident.
260        .layer(SetResponseHeaderLayer::overriding(
261            header::CONTENT_SECURITY_POLICY,
262            HeaderValue::from_static(
263                "default-src 'none'; script-src 'self'; style-src 'self'; \
264                 img-src 'self' data:; connect-src 'self'; form-action 'self'; \
265                 frame-ancestors 'none'; base-uri 'none'",
266            ),
267        ))
268        // Outermost, and the same middleware the ACME listener uses: an admin
269        // request gets an `x-request-id` and an access line on identical
270        // terms. Its `profile` field stays `field::Empty` and renders as
271        // absent, which is correct — an admin request belongs to no profile.
272        .layer(middleware::from_fn(
273            middlewares::access::add_access_middleware,
274        ));
275
276    (router, logins)
277}
278
279/// Mounts the API at `/api` with fallbacks that answer in the admin error
280/// shape.
281///
282/// Without these, a typo'd path would fall through to an empty body — the same
283/// reasoning the profile routers already encode, except that there the fallback
284/// has to be an ACME problem document and here it must not be.
285fn api_with_fallbacks(api: Router<AdminState>) -> Router<AdminState> {
286    Router::new().nest(
287        "/api",
288        api.method_not_allowed_fallback(|| async {
289            AdminError::with_code(
290                StatusCode::METHOD_NOT_ALLOWED,
291                "method_not_allowed",
292                "that method is not allowed on this resource",
293            )
294        })
295        .fallback(|| async { AdminError::not_found("no such admin API resource") }),
296    )
297}
298
299/// Rejects an `[admin]` section that cannot work, before anything binds.
300///
301/// Runs only when the panel is enabled: an operator who has not turned it on
302/// must never be stopped from starting by a section they never edited.
303pub fn check_config(config: &Config) -> anyhow::Result<()> {
304    let admin = &config.admin;
305    if !admin.enabled {
306        return Ok(());
307    }
308
309    if admin.bind_address == config.server.bind_address {
310        bail!(
311            "admin.bind_address and server.bind_address are both `{}`: the web admin is a \
312             second listener on its own socket, not a path on the ACME one",
313            admin.bind_address
314        );
315    }
316
317    let url = url::Url::parse(&admin.base_url).map_err(|error| {
318        anyhow::anyhow!("admin.base_url `{}` is not a URL: {error}", admin.base_url)
319    })?;
320    if url.host_str().is_none_or(str::is_empty) {
321        bail!(
322            "admin.base_url `{}` has no host: it names the origin the panel is reached at, \
323             and a generated certificate takes its name from it",
324            admin.base_url
325        );
326    }
327
328    // A hard error, not a warning, and the reasoning is worth keeping: the
329    // session cookie is always sent `Secure` (never conditionally -- that is
330    // how a session cookie leaks). Browsers accept a `Secure` cookie on
331    // `http://localhost` and silently refuse it on `http://192.0.2.10:3001`.
332    // The operator would see "login succeeds, then I am immediately logged
333    // out" with nothing in any log to explain it. Refuse, and name both keys.
334    // Same reasoning as `filter.allowed_ip` with two empty lists being a
335    // startup error rather than an accept-everything default.
336    if !admin.tls.enabled && !binds_loopback_only(&admin.bind_address) {
337        bail!(
338            "admin.bind_address `{}` is not loopback while admin.tls.enabled is false: the \
339             session cookie is sent `Secure`, which a browser will not store over plain HTTP \
340             on anything but localhost, so signing in would appear to succeed and then fail \
341             silently. Set admin.tls.enabled = true, or bind 127.0.0.1 and reach it through \
342             an SSH tunnel",
343            admin.bind_address
344        );
345    }
346
347    // The `tls_base_url_mismatch` treatment: a warning, because the reverse
348    // proxy case (https:// in the URL, TLS terminated in front) is legitimate.
349    if admin.tls.enabled && url.scheme() == "http" {
350        warn!(event = "admin_base_url_mismatch",
351              outcome = "advisory",
352              base_url = %admin.base_url,
353              "admin.base_url names http:// while admin.tls.enabled is true: the CSRF origin \
354               check compares against it, so browser requests will be refused until it names \
355               https://");
356    }
357
358    // `admin.base_url` is load-bearing in three unrelated ways -- the CSRF
359    // origin check, the generated certificate's name, and later the WebAuthn
360    // relying-party id -- and is easy to leave at its default while binding
361    // somewhere else. Logging the resolved origin makes the mismatch visible
362    // at startup rather than at the first refused request.
363    info!(event = "admin_origin_resolved",
364          outcome = "success",
365          origin = %url.origin().ascii_serialization(),
366          bind_address = %admin.bind_address);
367
368    check_templates(&admin.template_dir)?;
369
370    Ok(())
371}
372
373/// Compiles every page template, so a broken override stops the process here
374/// rather than serving a `500` at three in the morning.
375///
376/// The same posture as the rest of startup: a path that can fail fast should.
377/// The cost is one compile of ~20 small templates, once.
378fn check_templates(template_dir: &str) -> anyhow::Result<()> {
379    if !template_dir.is_empty() {
380        let path = std::path::Path::new(template_dir);
381        if !path.is_dir() {
382            bail!(
383                "admin.template_dir `{template_dir}` is not a directory: it holds per-file \
384                 overrides of the compiled-in page templates, checked by name before the \
385                 default. Leave it empty to use the defaults"
386            );
387        }
388    }
389
390    let env = pages::templates::build_environment(template_dir);
391    for name in pages::templates::template_names() {
392        env.get_template(name).map_err(|error| {
393            anyhow::anyhow!(
394                "admin page template `{name}` does not compile: {error}. \
395                 It was loaded from admin.template_dir `{template_dir}`"
396            )
397        })?;
398    }
399
400    if !template_dir.is_empty() {
401        info!(event = "admin_templates_overridden", outcome = "success", template_dir = %template_dir);
402    }
403
404    Ok(())
405}
406
407/// Whether every address `bind` can accept on is a loopback address.
408///
409/// Deliberately conservative: an address that does not parse, or a hostname
410/// this does not resolve, counts as *not* loopback. Being wrong in that
411/// direction costs an operator one explicit `admin.tls.enabled = true`; being
412/// wrong in the other silently ships a panel whose login does not work.
413fn binds_loopback_only(bind: &str) -> bool {
414    let Ok(addr) = bind.parse::<std::net::SocketAddr>() else {
415        // Not a bare socket address -- e.g. `localhost:3001`, which the
416        // listener resolves later. Accept the two spellings that can only
417        // ever be loopback, and refuse everything else.
418        return matches!(
419            bind.rsplit_once(':').map(|(host, _)| host),
420            Some("localhost" | "ip6-localhost")
421        );
422    };
423    addr.ip().is_loopback()
424}
425
426#[cfg(test)]
427mod tests {
428    use super::*;
429    use crate::config::{AdminConfig, Config};
430
431    /// `[admin]` enabled, with everything else at its default.
432    ///
433    /// Built by mutation rather than struct-update syntax: `Config` keeps a
434    /// private `raw` field, so `..Config::default()` is not available outside
435    /// `crate::config`.
436    fn enabled() -> Config {
437        let mut config = Config::default();
438        config.admin = AdminConfig {
439            enabled: true,
440            ..AdminConfig::default()
441        };
442        config
443    }
444
445    #[test]
446    fn a_disabled_panel_is_never_checked() {
447        // Every one of these would be refused if the panel were on.
448        let mut config = Config::default();
449        config.admin = AdminConfig {
450            enabled: false,
451            bind_address: "0.0.0.0:3001".to_string(),
452            base_url: "not a url".to_string(),
453            ..AdminConfig::default()
454        };
455        assert!(check_config(&config).is_ok());
456    }
457
458    #[test]
459    fn the_defaults_are_a_working_configuration() {
460        check_config(&enabled()).expect("loopback + the default base_url must start");
461    }
462
463    /// The compiled-in templates are checked on every start, so a page that
464    /// stopped compiling fails the build's own test suite rather than the first
465    /// operator to open it.
466    #[test]
467    fn the_embedded_templates_all_compile_at_startup() {
468        check_templates("").expect("the shipped templates must compile");
469    }
470
471    #[test]
472    fn a_template_dir_that_is_not_a_directory_is_refused() {
473        let dir = crate::testutil::TempDir::new("admin-template-dir");
474        let file = dir.write("not-a-directory", "");
475
476        let error = check_templates(file.to_str().unwrap()).unwrap_err();
477        assert!(error.to_string().contains("is not a directory"));
478    }
479
480    /// A broken override should stop the process, not serve a `500` at three in
481    /// the morning — the same fail-fast posture as the rest of startup.
482    #[test]
483    fn an_override_that_does_not_compile_is_refused_at_startup() {
484        let dir = crate::testutil::TempDir::new("admin-bad-template");
485        dir.write("index.html", "{% for x in %}");
486
487        let error = check_templates(dir.path().to_str().unwrap()).unwrap_err();
488        let message = error.to_string();
489        assert!(message.contains("index.html"));
490        assert!(message.contains("does not compile"));
491    }
492
493    #[test]
494    fn a_valid_override_directory_starts() {
495        let dir = crate::testutil::TempDir::new("admin-good-template");
496        dir.write("login.html", "<p>{{ flash }}</p>");
497
498        check_templates(dir.path().to_str().unwrap())
499            .expect("one overridden template must not stop the other twenty");
500    }
501
502    #[test]
503    fn a_bind_shared_with_the_acme_listener_is_refused() {
504        let mut config = enabled();
505        config.admin.bind_address = config.server.bind_address.clone();
506        let error = check_config(&config).unwrap_err().to_string();
507        assert!(error.contains("second listener"), "got: {error}");
508        assert!(error.contains(&config.server.bind_address));
509    }
510
511    #[test]
512    fn a_base_url_that_is_not_a_url_or_has_no_host_is_refused() {
513        let mut config = enabled();
514        config.admin.base_url = "not a url".to_string();
515        assert!(
516            check_config(&config)
517                .unwrap_err()
518                .to_string()
519                .contains("is not a URL")
520        );
521
522        // Parses, but names no host.
523        config.admin.base_url = "unix:/run/admin.sock".to_string();
524        let error = check_config(&config).unwrap_err().to_string();
525        assert!(error.contains("has no host"), "got: {error}");
526    }
527
528    #[test]
529    fn a_non_loopback_bind_without_tls_is_a_startup_error_not_a_warning() {
530        for bind in [
531            "0.0.0.0:3001",
532            "192.0.2.10:3001",
533            "[::]:3001",
534            "[2001:db8::1]:3001",
535        ] {
536            let mut config = enabled();
537            config.admin.bind_address = bind.to_string();
538            let error = check_config(&config).unwrap_err().to_string();
539            assert!(
540                error.contains("is not loopback"),
541                "`{bind}` must be refused without TLS, got: {error}"
542            );
543        }
544    }
545
546    #[test]
547    fn a_non_loopback_bind_is_allowed_once_tls_is_on() {
548        let mut config = enabled();
549        config.admin.bind_address = "0.0.0.0:3001".to_string();
550        config.admin.tls.enabled = true;
551        config.admin.base_url = "https://admin.example.com".to_string();
552        check_config(&config).expect("TLS is what the loopback rule was standing in for");
553    }
554
555    #[test]
556    fn every_loopback_spelling_is_accepted_without_tls() {
557        for bind in [
558            "127.0.0.1:3001",
559            "127.0.0.53:3001",
560            "[::1]:3001",
561            "localhost:3001",
562        ] {
563            let mut config = enabled();
564            config.admin.bind_address = bind.to_string();
565            check_config(&config).unwrap_or_else(|error| panic!("`{bind}` must start: {error}"));
566        }
567    }
568
569    #[test]
570    fn an_unparseable_bind_is_treated_as_non_loopback() {
571        // Conservative on purpose: the cost of being wrong this way is one
572        // explicit config key, the other way is a panel nobody can log in to.
573        let mut config = enabled();
574        config.admin.bind_address = "not-a-socket-address".to_string();
575        assert!(
576            check_config(&config)
577                .unwrap_err()
578                .to_string()
579                .contains("is not loopback")
580        );
581    }
582
583    #[test]
584    fn tls_with_an_http_base_url_warns_but_starts() {
585        let mut config = enabled();
586        config.admin.tls.enabled = true;
587        // Still http://, which the CSRF origin check will compare against.
588        check_config(&config).expect("a scheme mismatch is a warning, not a refusal");
589    }
590}