praxis-proxy-protocol 0.6.0

HTTP, TCP, and protocol adapters for Praxis
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Praxis Contributors

//! Shared TLS settings builder for HTTP and TCP listeners.
//!
//! This module provides [`build_tls_settings`], the unified entry point for
//! constructing Pingora [`TlsSettings`] from a Praxis [`ListenerTls`] config.
//! It is used by both HTTP and TCP protocol adapters to set up listener TLS,
//! handling the distinction between hot-reload and static certificate modes.
//!
//! # Hot-reload vs static certificates
//!
//! When the `config-reload` feature is compiled in AND the listener has
//! `hot_reload: true`, this module builds a reloadable TLS configuration via
//! [`praxis_tls::setup::build_reloadable_server_config`] and spawns a
//! [`CertWatcher`] background task to monitor certificate files for changes.
//! The watcher runs for the process lifetime and swaps certificates
//! atomically when they change on disk, without dropping connections.
//!
//! Otherwise (no `config-reload` feature, or `hot_reload: false`), a static
//! `rustls::ServerConfig` is built via [`praxis_tls::setup::build_server_config`]
//! and certificate changes require a full config reload.
//!
//! # Certificate watcher lifecycle
//!
//! The returned shutdown sender allows the caller to stop the cert watcher
//! early by sending `true`. Dropping the sender does NOT stop the watcher;
//! it continues monitoring until explicitly stopped or the process exits.
//! Callers typically keep the sender alive for the server lifetime.
//!
//! [`build_tls_settings`]: crate::tls_setup::build_tls_settings
//! [`TlsSettings`]: pingora_core::listeners::tls::TlsSettings
//! [`ListenerTls`]: praxis_tls::ListenerTls
//! [`CertWatcher`]: praxis_tls::watcher::CertWatcher

use pingora_core::listeners::tls::TlsSettings;
use praxis_core::ProxyError;
use praxis_tls::ListenerTls;
use tokio::sync::watch;

// -----------------------------------------------------------------------------
// TLS Settings Builder
// -----------------------------------------------------------------------------

/// Build [`TlsSettings`] for a listener.
///
/// When `hot_reload` is enabled (and the `config-reload` feature is
/// compiled in), uses a [`ReloadableCertResolver`] and spawns a
/// [`CertWatcher`] background task. Otherwise, or in a build without
/// `config-reload`, builds a static `ServerConfig` via
/// [`build_server_config`].
///
/// `context_label` appears in debug tracing to distinguish HTTP
/// from TCP callers (e.g. `"HTTP"`, `"TCP"`).
///
/// Returns the settings and an optional shutdown sender for the
/// cert watcher. The watcher runs for the process lifetime; the caller
/// keeps the sender to stop it early via `send(true)` (dropping it does
/// not stop the watcher).
///
/// [`TlsSettings`]: pingora_core::listeners::tls::TlsSettings
/// [`build_server_config`]: praxis_tls::setup::build_server_config
/// [`ReloadableCertResolver`]: praxis_tls::reload::ReloadableCertResolver
/// [`CertWatcher`]: praxis_tls::watcher::CertWatcher
#[expect(clippy::too_many_lines, reason = "hot-reload vs static TLS branching")]
pub(crate) fn build_tls_settings(
    tls: &ListenerTls,
    address: &str,
    context_label: &str,
    advertise_http_alpn: bool,
) -> Result<(TlsSettings, Option<watch::Sender<bool>>), ProxyError> {
    macro_rules! tls_err {
        ($e:expr) => {{
            let err = $e;
            ProxyError::Config(format!("TLS for {address}: {err}"))
        }};
    }

    #[cfg(feature = "config-reload")]
    if tls.is_hot_reload() {
        tracing::debug!(address, context_label, "building TLS ServerConfig with hot-reload");
        let result = praxis_tls::setup::build_reloadable_server_config(tls, advertise_http_alpn)
            .map_err(|e| ProxyError::Config(format!("TLS hot-reload for {address}: {e}")))?;

        let pair =
            tls.certificates.first().cloned().ok_or_else(|| {
                ProxyError::Config(format!("TLS hot-reload for {address}: no certificate configured"))
            })?;

        let verifier_reload = result.verifier_handle.and_then(|handle| {
            tls.client_ca
                .as_ref()
                .map(|ca_cfg| praxis_tls::watcher::ClientVerifierReload {
                    ca_path: ca_cfg.ca_path.clone(),
                    crl_paths: ca_cfg.crl_paths.clone(),
                    mode: tls.client_cert_mode,
                    trusted_spiffe_ids: tls.trusted_spiffe_ids.clone(),
                    swap_handle: handle,
                })
        });

        let (shutdown_tx, shutdown_rx) = watch::channel(false);
        praxis_tls::watcher::CertWatcher::spawn(result.cert_handle, pair, verifier_reload, shutdown_rx);

        let settings = TlsSettings::with_server_config(result.config).map_err(|e| tls_err!(e))?;
        return Ok((settings, Some(shutdown_tx)));
    }

    // Built without the `config-reload` feature: honor the static cert but warn
    // that a `hot_reload: true` listener will not actually watch for changes.
    #[cfg(not(feature = "config-reload"))]
    if tls.is_hot_reload() {
        tracing::warn!(
            address,
            context_label,
            "listener requests TLS hot_reload but this build lacks the `config-reload` feature; \
             serving a static certificate"
        );
    }

    tracing::debug!(address, context_label, "building TLS ServerConfig");
    let server_config = praxis_tls::setup::build_server_config(tls, advertise_http_alpn).map_err(|e| tls_err!(e))?;
    let settings = TlsSettings::with_server_config(server_config).map_err(|e| tls_err!(e))?;
    Ok((settings, None))
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

#[cfg(test)]
#[expect(clippy::allow_attributes, reason = "blanket test suppressions")]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::assertions_on_result_states,
    clippy::significant_drop_tightening,
    clippy::must_use_candidate,
    clippy::semicolon_if_nothing_returned,
    clippy::must_use_unit,
    clippy::let_underscore_must_use,
    clippy::too_many_lines,
    reason = "tests"
)]
mod tests {
    use praxis_tls::{CaConfig, CertKeyPair, ClientCertMode};

    use super::*;

    /// Generate test certificates for TLS testing.
    ///
    /// Returns a temporary directory and paths to CA, server cert, and server key.
    fn gen_test_certs() -> (tempfile::TempDir, String, String, String) {
        use rcgen::{CertificateParams, DnType, IsCa, Issuer, KeyPair};

        let temp_dir = tempfile::TempDir::new().expect("create temp dir");

        // Generate CA
        let ca_key = KeyPair::generate().expect("generate CA key");
        let mut ca_params = CertificateParams::new(Vec::<String>::new()).expect("CA params");
        ca_params.is_ca = IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
        ca_params.distinguished_name.push(DnType::CommonName, "Test CA");
        let ca_cert = ca_params.self_signed(&ca_key).expect("self-sign CA");
        let issuer = Issuer::from_params(&ca_params, &ca_key);

        // Generate server certificate
        let server_key = KeyPair::generate().expect("generate server key");
        let mut server_params = CertificateParams::new(vec!["localhost".to_owned()]).expect("server params");
        server_params.distinguished_name.push(DnType::CommonName, "localhost");
        let server_cert = server_params.signed_by(&server_key, &issuer).expect("sign server cert");

        // Write files
        let ca_path = temp_dir.path().join("ca.pem");
        let cert_path = temp_dir.path().join("server.pem");
        let key_path = temp_dir.path().join("server-key.pem");

        std::fs::write(&ca_path, ca_cert.pem()).expect("write CA");
        std::fs::write(&cert_path, server_cert.pem()).expect("write cert");
        std::fs::write(&key_path, server_key.serialize_pem()).expect("write key");

        (
            temp_dir,
            ca_path.to_str().unwrap().to_owned(),
            cert_path.to_str().unwrap().to_owned(),
            key_path.to_str().unwrap().to_owned(),
        )
    }

    /// Install crypto provider for rustls (required for tests).
    fn ensure_crypto_provider() {
        use rustls::crypto::aws_lc_rs;

        drop(aws_lc_rs::default_provider().install_default());
    }

    #[test]
    fn static_config_single_cert() {
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert.clone(),
                key_path: key.clone(),
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: Some(false), // Explicitly disabled
            min_version: None,
        };

        let result = build_tls_settings(&tls, "127.0.0.1:8443", "TEST", false);
        assert!(result.is_ok(), "should build static config");

        let (settings, shutdown_tx) = result.unwrap();
        assert!(shutdown_tx.is_none(), "static config should not return shutdown sender");

        // Verify settings are valid (non-null)
        drop(settings);
    }

    #[test]
    fn static_config_with_alpn() {
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert,
                key_path: key,
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: Some(false),
            min_version: None,
        };

        // With HTTP ALPN
        let result = build_tls_settings(&tls, "127.0.0.1:8443", "HTTP", true);
        assert!(result.is_ok(), "should build with HTTP ALPN: ");
        let (_, shutdown_tx) = result.unwrap();
        assert!(shutdown_tx.is_none());
    }

    #[test]
    fn static_config_multi_cert_disables_hot_reload() {
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        // Multi-cert config (>1 certificate)
        let tls = ListenerTls {
            certificates: vec![
                CertKeyPair {
                    cert_path: cert.clone(),
                    key_path: key.clone(),
                    default: false,
                    server_names: vec!["example.com".to_owned()],
                },
                CertKeyPair {
                    cert_path: cert.clone(),
                    key_path: key.clone(),
                    default: true,
                    server_names: vec![],
                },
            ],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: None, // Default, but multi-cert disables it
            min_version: None,
        };

        // Multi-cert configs return is_hot_reload() == false
        assert!(!tls.is_hot_reload(), "multi-cert should disable hot-reload");

        let result = build_tls_settings(&tls, "127.0.0.1:8443", "HTTP", true);
        assert!(result.is_ok(), "should build multi-cert config: ");

        let (_, shutdown_tx) = result.unwrap();
        assert!(shutdown_tx.is_none(), "multi-cert should use static path");
    }

    #[test]
    fn static_config_with_client_ca() {
        ensure_crypto_provider();
        let (_temp, ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert,
                key_path: key,
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: Some(CaConfig {
                ca_path: ca,
                crl_paths: vec![],
            }),
            client_cert_mode: ClientCertMode::Request,
            trusted_spiffe_ids: vec![],
            hot_reload: Some(false),
            min_version: None,
        };

        let result = build_tls_settings(&tls, "127.0.0.1:8443", "mTLS", false);
        assert!(result.is_ok(), "should build with client CA: ");

        let (_, shutdown_tx) = result.unwrap();
        assert!(shutdown_tx.is_none());
    }

    #[test]
    fn static_config_invalid_cert_path() {
        ensure_crypto_provider();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: "/nonexistent/cert.pem".to_owned(),
                key_path: "/nonexistent/key.pem".to_owned(),
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: Some(false),
            min_version: None,
        };

        let result = build_tls_settings(&tls, "127.0.0.1:8443", "TEST", false);
        assert!(result.is_err(), "should fail with invalid cert path");

        if let Err(err) = result {
            let err_msg = err.to_string();
            assert!(err_msg.contains("127.0.0.1:8443"), "error should include address");
        }
    }

    #[test]
    #[cfg(not(feature = "config-reload"))]
    fn hot_reload_without_feature_warns_and_uses_static() {
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert,
                key_path: key,
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: None, // Default (treated as true for single cert)
            min_version: None,
        };

        assert!(
            tls.is_hot_reload(),
            "single cert with hot_reload=None should be hot-reload"
        );

        // Without config-reload feature, this should warn and use static config
        let result = build_tls_settings(&tls, "127.0.0.1:8443", "HTTP", true);
        assert!(result.is_ok(), "should succeed with static fallback: ");

        let (_, shutdown_tx) = result.unwrap();
        assert!(
            shutdown_tx.is_none(),
            "without config-reload feature, should not return watcher"
        );
    }

    #[test]
    #[cfg(feature = "config-reload")]
    fn hot_reload_with_feature_spawns_watcher() {
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert,
                key_path: key,
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: None, // Default enables hot-reload
            min_version: None,
        };

        assert!(tls.is_hot_reload(), "single cert should enable hot-reload by default");

        let result = build_tls_settings(&tls, "127.0.0.1:8443", "HTTP", true);
        assert!(result.is_ok(), "should build hot-reload config: ");

        let (_, shutdown_tx) = result.unwrap();
        assert!(
            shutdown_tx.is_some(),
            "with config-reload feature, should return shutdown sender"
        );

        // Send shutdown signal to cleanup watcher
        if let Some(tx) = shutdown_tx {
            let _ = tx.send(true);
        }
    }

    #[test]
    #[cfg(feature = "config-reload")]
    fn hot_reload_with_client_ca() {
        ensure_crypto_provider();
        let (_temp, ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert,
                key_path: key,
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: Some(CaConfig {
                ca_path: ca,
                crl_paths: vec![],
            }),
            client_cert_mode: ClientCertMode::Require,
            trusted_spiffe_ids: vec![],
            hot_reload: None,
            min_version: None,
        };

        let result = build_tls_settings(&tls, "127.0.0.1:8443", "mTLS", false);
        assert!(result.is_ok(), "should build hot-reload with client CA: ");

        let (_, shutdown_tx) = result.unwrap();
        assert!(shutdown_tx.is_some(), "should spawn watcher for client CA");

        // Cleanup
        if let Some(tx) = shutdown_tx {
            let _ = tx.send(true);
        }
    }

    #[test]
    #[cfg(feature = "config-reload")]
    fn hot_reload_without_client_ca() {
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert,
                key_path: key,
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None, // No client CA
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: None,
            min_version: None,
        };

        let result = build_tls_settings(&tls, "127.0.0.1:8443", "HTTP", true);
        assert!(result.is_ok(), "should build hot-reload without client CA: ");

        let (_, shutdown_tx) = result.unwrap();
        assert!(shutdown_tx.is_some());

        // Cleanup
        if let Some(tx) = shutdown_tx {
            let _ = tx.send(true);
        }
    }

    #[test]
    fn context_label_used_in_logging() {
        // This test verifies that different context labels can be passed
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert.clone(),
                key_path: key.clone(),
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: Some(false),
            min_version: None,
        };

        // HTTP context
        let result = build_tls_settings(&tls, "127.0.0.1:8443", "HTTP", true);
        assert!(result.is_ok());

        // TCP context
        let result = build_tls_settings(&tls, "127.0.0.1:8443", "TCP", false);
        assert!(result.is_ok());

        // Custom context
        let result = build_tls_settings(&tls, "127.0.0.1:8443", "CUSTOM", false);
        assert!(result.is_ok());
    }

    #[test]
    fn address_included_in_error_message() {
        ensure_crypto_provider();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: "/nonexistent/cert.pem".to_owned(),
                key_path: "/nonexistent/key.pem".to_owned(),
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: Some(false),
            min_version: None,
        };

        // Test with different addresses
        let result = build_tls_settings(&tls, "192.168.1.1:443", "HTTP", false);
        assert!(result.is_err());
        if let Err(err) = result {
            assert!(err.to_string().contains("192.168.1.1:443"));
        }

        let result = build_tls_settings(&tls, "[::1]:8443", "TCP", false);
        assert!(result.is_err());
        if let Err(err) = result {
            assert!(err.to_string().contains("[::1]:8443"));
        }
    }

    #[test]
    fn alpn_variation_coverage() {
        ensure_crypto_provider();
        let (_temp, _ca, cert, key) = gen_test_certs();

        let tls = ListenerTls {
            certificates: vec![CertKeyPair {
                cert_path: cert,
                key_path: key,
                default: false,
                server_names: vec![],
            }],
            cipher_suites: None,
            client_ca: None,
            client_cert_mode: ClientCertMode::None,
            trusted_spiffe_ids: vec![],
            hot_reload: Some(false),
            min_version: None,
        };

        // advertise_http_alpn = true
        let result = build_tls_settings(&tls, "127.0.0.1:8443", "HTTP", true);
        assert!(result.is_ok(), "should succeed with ALPN enabled");

        // advertise_http_alpn = false
        let result = build_tls_settings(&tls, "127.0.0.1:8443", "TCP", false);
        assert!(result.is_ok(), "should succeed with ALPN disabled");
    }
}