acme-proxy 0.5.0

An ACME (RFC 8555) server that issues from a local CA, relays to an upstream CA, or delegates to a script
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Request correlation and access logging.
//!
//! Assigns or propagates an `x-request-id` header, opens the `request` span
//! every other log line of that request is nested under, and emits the one
//! access line per request (`event = "request_completed"`).
//!
//! The span names the client twice over, and both halves are load-bearing. This
//! layer seeds `client_ip` from the peer address, which is the only source the
//! routes outside every profile router have — `/health`, the http-01 responder
//! and the admission-control refusals. The per-profile filter middleware then
//! overwrites it with the address `ProxyPolicy` resolved, since
//! `filter.trusted_proxies` is per-profile configuration and a request through
//! a trusted reverse proxy would otherwise be attributed to the proxy.
//!
//! Both halves live in one middleware on purpose. They used to be two — this
//! `x-request-id` layer plus a `tower_http::trace::TraceLayer` built inside
//! each profile's router — and the split cost three things:
//!
//! - the id never reached the span. `Span::current().record("request_id", …)`
//!   ran here, *outside* the span `TraceLayer` had not created yet, so it wrote
//!   to nothing;
//! - the root routes (`/health`, `/`, the http-01 responder) and the
//!   admission-control layer sat outside the profile routers entirely, so
//!   `request_shed` and `request_deadline_exceeded` — the two events an
//!   operator most wants to correlate — carried no id at all;
//! - `DefaultOnResponse` emits under the `tower_http::trace::on_response`
//!   target, which the shipped `logging.filter` set to `warn`. The access line
//!   was configured at `INFO` and silenced by default.
//!
//! Writing the access line by hand also buys what `TraceLayer` structurally
//! cannot: `on_response` receives `(&Response, Latency, &Span)` and never the
//! request, so it cannot vary its level by path — and a liveness probe once a
//! second must not drown the info stream.

use std::net::SocketAddr;
use std::time::Instant;

use axum::{
    body::Body,
    extract::ConnectInfo,
    http::{HeaderName, HeaderValue, Method, Request},
    middleware::Next,
    response::IntoResponse,
};
use tracing::{Instrument, debug, field, info, info_span, warn};
use uuid::Uuid;

pub const X_REQUEST_ID: HeaderName = HeaderName::from_static("x-request-id");

/// Extension wrapper for the HTTP Request ID.
#[derive(Debug, Clone)]
pub struct RequestId(pub String);

/// Returns true for the probe routes whose access line belongs at `debug`.
///
/// `/health` is polled by a load balancer, `/` only redirects to it. Neither
/// says anything about ACME, and at one probe per second per node they would
/// be most of the log.
fn is_probe(method: &Method, path: &str) -> bool {
    matches!(*method, Method::GET | Method::HEAD) && matches!(path, "/health" | "/")
}

/// Longest `x-request-id` accepted from a caller.
///
/// `crate::audit::USER_AGENT_MAX`'s reasoning, for a header that goes further:
/// a UUID is 36 characters and the longest correlation id any reverse proxy
/// generates is well inside this, while the value reaches the `request` span
/// (so every log line of the request), the response header, and the
/// `request_id` column of both `audit_log` and `upstream_orders`. Unbounded, an
/// unauthenticated caller decides how large all four of those get.
const REQUEST_ID_MAX: usize = 128;

/// Whether `value` is safe to adopt as this request's correlation id.
///
/// Deliberately narrower than "printable": the id is interpolated into the
/// non-JSON `tracing` format as `request_id=<value>`, where a space or an `=`
/// lets a caller write fields that were never emitted. Restricting to the
/// characters correlation ids actually use costs nothing real and takes the
/// question away.
fn usable_request_id(value: &str) -> bool {
    !value.is_empty()
        && value.len() <= REQUEST_ID_MAX
        && value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b':'))
}

pub async fn add_access_middleware(mut request: Request<Body>, next: Next) -> impl IntoResponse {
    let id_str = match request.headers().get(&X_REQUEST_ID) {
        Some(value) => match value.to_str() {
            Ok(value) => {
                let value = value.trim();
                if usable_request_id(value) {
                    value.to_string()
                } else {
                    // Too long, empty, or carrying characters that would let
                    // the caller forge log structure. A fresh id rather than a
                    // truncation: half of somebody's correlation id correlates
                    // with nothing and reads like a real one.
                    debug!(event = "request_id_header_invalid", outcome = "failure");
                    String::new()
                }
            }
            Err(_) => {
                // Non-ASCII bytes in the header. Falling back to a fresh id is
                // right — but silently, an operator correlating with the
                // reverse proxy's own logs would find nothing and have no clue
                // why.
                debug!(event = "request_id_header_invalid", outcome = "failure");
                String::new()
            }
        },
        None => String::new(),
    };

    let id_str = if id_str.is_empty() {
        Uuid::now_v7().to_string()
    } else {
        id_str
    };

    request.extensions_mut().insert(RequestId(id_str.clone()));

    let probe = is_probe(request.method(), request.uri().path());

    // The peer address, canonicalized the way `ProxyPolicy::resolve` does, so
    // the two sources of this field never disagree on the spelling of a
    // v4-mapped v6 address (the dual-stack `[::]:3000` bind sees every IPv4
    // client as `::ffff:…`).
    let peer = request
        .extensions()
        .get::<ConnectInfo<SocketAddr>>()
        .map(|ConnectInfo(addr)| crate::filter::canonical(addr.ip()));

    // `profile` is filled in by each profile router (see `build_router`): this
    // layer is server-wide and mounted above the `/profile/<name>` nesting, so
    // it is not yet known here.
    //
    // `client_ip` is seeded here with the *peer* and overwritten by the filter
    // middleware with the `ProxyPolicy`-resolved address, because
    // `filter.trusted_proxies` is per-profile configuration this layer cannot
    // see. Seeding it is not redundant: `/health`, the http-01 responder and
    // every admission-control refusal sit outside all profile routers, so
    // without it the routes that never reach a filter would name nobody.
    //
    // The `ClientIp` *extension* is seeded from the same value and for the same
    // reason, `middlewares::filter` likewise overwriting it. Its one reader is
    // `audit::RequestContext::gather`, and until it was seeded here every row
    // the web admin wrote carried no address and no reverse name at all: that
    // listener deliberately runs no filter layer, so nothing ever inserted the
    // extension on it. The peer is the right answer there — the admin listener
    // trusts no forwarded header, which is the same choice `AdminClientIp`
    // makes — and on the ACME side the filter still replaces it before any
    // handler builds a record.
    if peer.is_some() {
        request
            .extensions_mut()
            .insert(crate::filter::ClientIp(peer));
    }

    let span = info_span!(
        "request",
        method = %request.method(),
        uri = %request.uri(),
        version = ?request.version(),
        request_id = %id_str,
        profile = field::Empty,
        client_ip = field::Empty,
    );
    if let Some(peer) = peer {
        span.record("client_ip", field::display(peer));
    }

    let started = Instant::now();
    let mut response = next.run(request).instrument(span.clone()).await;
    let latency_ms = crate::millis(started.elapsed());
    let status = response.status().as_u16();

    span.in_scope(|| {
        // The one event name emitted at three levels, and the one whose
        // `outcome` is the response status rather than the name: a 5xx is this
        // server failing, and every other status is it answering.
        if response.status().is_server_error() {
            warn!(
                event = "request_completed",
                outcome = "failure",
                status,
                latency_ms
            );
        } else if probe {
            debug!(
                event = "request_completed",
                outcome = "success",
                status,
                latency_ms
            );
        } else {
            info!(
                event = "request_completed",
                outcome = "success",
                status,
                latency_ms
            );
        }
    });

    if let Ok(header_val) = HeaderValue::from_str(&id_str) {
        response.headers_mut().insert(X_REQUEST_ID, header_val);
    } else {
        // Only reachable for a client-supplied id: a generated UUID is always a
        // valid header value.
        debug!(event = "request_id_header_not_returned", outcome = "success", request_id = %id_str);
    }

    response
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{Router, http::StatusCode, middleware, routing::get};
    use tower::ServiceExt;

    fn app() -> Router {
        Router::new()
            .route("/test", get(|| async { "ok" }))
            .route("/health", get(|| async { "ok" }))
            .route("/boom", get(|| async { StatusCode::INTERNAL_SERVER_ERROR }))
            .layer(middleware::from_fn(add_access_middleware))
    }

    /// Drives one request under a subscriber capturing the `request` span, and
    /// returns what it recorded.
    async fn fields_for(request: Request<Body>) -> crate::testutil::SpanFields {
        crate::testutil::capture_request_span(app().oneshot(request)).await
    }

    /// The seeded half: with no filter middleware in front, the peer address is
    /// the only thing that can name the client — this is what `/health` and the
    /// http-01 responder get.
    #[tokio::test]
    async fn the_peer_address_is_recorded_as_the_client() {
        let mut req = Request::builder().uri("/test").body(Body::empty()).unwrap();
        req.extensions_mut()
            .insert(ConnectInfo(SocketAddr::from(([192, 0, 2, 7], 4711))));

        let fields = fields_for(req).await;

        assert_eq!(fields.get("client_ip").as_deref(), Some("192.0.2.7"));
    }

    /// The dual-stack `[::]:3000` bind sees IPv4 clients as `::ffff:…`. The
    /// access line must spell them the same way `ProxyPolicy::resolve` does, or
    /// one address reads as two across the two layers that write this field.
    #[tokio::test]
    async fn a_v4_mapped_peer_is_canonicalized() {
        let mapped: std::net::IpAddr = "::ffff:192.0.2.7".parse().unwrap();
        let mut req = Request::builder().uri("/test").body(Body::empty()).unwrap();
        req.extensions_mut()
            .insert(ConnectInfo(SocketAddr::from((mapped, 4711))));

        let fields = fields_for(req).await;

        assert_eq!(fields.get("client_ip").as_deref(), Some("192.0.2.7"));
    }

    /// A router driven through `oneshot` has no socket, so there is no peer to
    /// name. The field stays empty rather than being recorded as some
    /// placeholder an operator would read as an address.
    #[tokio::test]
    async fn no_connect_info_leaves_the_client_unnamed() {
        let req = Request::builder().uri("/test").body(Body::empty()).unwrap();

        let fields = fields_for(req).await;

        assert_eq!(fields.get("client_ip"), None);
        // The span was captured at all — otherwise the assertion above passes
        // for the wrong reason.
        assert!(fields.get("request_id").is_some());
    }

    #[tokio::test]
    async fn request_id_header_generated_when_absent() {
        let req = Request::builder().uri("/test").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();

        assert!(res.headers().contains_key(&X_REQUEST_ID));
    }

    #[tokio::test]
    async fn request_id_header_preserved_when_present() {
        let req = Request::builder()
            .uri("/test")
            .header("x-request-id", "custom-req-id-123")
            .body(Body::empty())
            .unwrap();

        let res = app().oneshot(req).await.unwrap();

        let id = res.headers().get(&X_REQUEST_ID).unwrap().to_str().unwrap();
        assert_eq!(id, "custom-req-id-123");
    }

    /// A header whose bytes are not ASCII cannot be read back as a string, so
    /// the middleware falls back to a generated id rather than propagating a
    /// value it cannot echo.
    #[tokio::test]
    async fn a_non_ascii_request_id_is_replaced() {
        let req = Request::builder()
            .uri("/test")
            .header(
                "x-request-id",
                HeaderValue::from_bytes(&[0xff, 0xfe]).unwrap(),
            )
            .body(Body::empty())
            .unwrap();

        let res = app().oneshot(req).await.unwrap();

        let id = res.headers().get(&X_REQUEST_ID).unwrap().to_str().unwrap();
        assert_eq!(id.len(), Uuid::now_v7().to_string().len());
    }

    /// Whitespace-only is as good as absent — otherwise the id echoed back is
    /// an empty string, which correlates nothing.
    #[tokio::test]
    async fn a_blank_request_id_is_replaced() {
        let req = Request::builder()
            .uri("/test")
            .header("x-request-id", "   ")
            .body(Body::empty())
            .unwrap();

        let res = app().oneshot(req).await.unwrap();

        let id = res.headers().get(&X_REQUEST_ID).unwrap().to_str().unwrap();
        assert!(!id.trim().is_empty());
    }

    /// An oversized id is not truncated but replaced.
    ///
    /// The value reaches the `request` span, the response header and two
    /// database columns, so an unauthenticated caller must not decide how large
    /// any of them get. Truncating would be worse than replacing: half of
    /// somebody's correlation id still looks like one and correlates with
    /// nothing.
    #[tokio::test]
    async fn an_oversized_request_id_is_replaced() {
        let req = Request::builder()
            .uri("/test")
            .header("x-request-id", "a".repeat(REQUEST_ID_MAX + 1))
            .body(Body::empty())
            .unwrap();

        let res = app().oneshot(req).await.unwrap();

        let id = res.headers().get(&X_REQUEST_ID).unwrap().to_str().unwrap();
        assert_eq!(id.len(), Uuid::now_v7().to_string().len());

        // The boundary itself is accepted, so the ceiling is a ceiling and not
        // an off-by-one.
        let req = Request::builder()
            .uri("/test")
            .header("x-request-id", "b".repeat(REQUEST_ID_MAX))
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(
            res.headers().get(&X_REQUEST_ID).unwrap().to_str().unwrap(),
            "b".repeat(REQUEST_ID_MAX)
        );
    }

    /// A caller must not be able to write log *structure*.
    ///
    /// Under the non-JSON `tracing` format the id is rendered as
    /// `request_id=<value>`, so a space or an `=` in the value lets the caller
    /// append fields that were never emitted — and a newline lets it write a
    /// whole line. Each of these is replaced by a generated id.
    #[tokio::test]
    async fn a_request_id_that_could_forge_log_structure_is_replaced() {
        let generated = Uuid::now_v7().to_string().len();
        for forged in [
            "abc outcome=success",
            "abc=def",
            "abc\tstatus=200",
            "abc\"quoted\"",
        ] {
            let Ok(header) = HeaderValue::from_str(forged) else {
                continue;
            };
            let req = Request::builder()
                .uri("/test")
                .header("x-request-id", header)
                .body(Body::empty())
                .unwrap();

            let res = app().oneshot(req).await.unwrap();
            let id = res.headers().get(&X_REQUEST_ID).unwrap().to_str().unwrap();
            assert_eq!(
                id.len(),
                generated,
                "`{forged}` must not be adopted verbatim"
            );
        }
    }

    /// The shapes a real correlation id takes are still adopted unchanged: a
    /// UUID, a hyphenated token, and the `trace:span` spelling some proxies use.
    #[tokio::test]
    async fn ordinary_correlation_ids_are_still_honoured() {
        for ordinary in [
            "550e8400-e29b-41d4-a716-446655440000",
            "custom-req-id-123",
            "trace.4bf92f:span_1",
        ] {
            let req = Request::builder()
                .uri("/test")
                .header("x-request-id", ordinary)
                .body(Body::empty())
                .unwrap();

            let res = app().oneshot(req).await.unwrap();
            assert_eq!(
                res.headers().get(&X_REQUEST_ID).unwrap().to_str().unwrap(),
                ordinary
            );
        }
    }

    /// The three access-line levels, driven end to end. Nothing here asserts on
    /// the emitted record — the point is that each branch runs.
    #[tokio::test]
    async fn every_access_line_level_is_reachable() {
        for (uri, expected) in [
            ("/test", StatusCode::OK),
            ("/health", StatusCode::OK),
            ("/boom", StatusCode::INTERNAL_SERVER_ERROR),
        ] {
            let req = Request::builder().uri(uri).body(Body::empty()).unwrap();
            let res = app().oneshot(req).await.unwrap();
            assert_eq!(res.status(), expected);
        }
    }

    #[test]
    fn probe_routes_are_recognized() {
        assert!(is_probe(&Method::GET, "/health"));
        assert!(is_probe(&Method::HEAD, "/health"));
        assert!(is_probe(&Method::GET, "/"));
        assert!(!is_probe(&Method::POST, "/health"));
        assert!(!is_probe(&Method::GET, "/directory"));
    }
}