csaf-crud 0.3.4

CSAF 2.0 / 2.1 advisory CRUD server with HATEOAS JSON API and HTML UI (TLS 1.3, HTTP/1.1 + HTTP/2 + HTTP/3)
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Pierre Gronau, ndaal in Cologne

//! CSAF CRUD web server entry point.
//!
//! Starts an HTTPS server with TLS 1.3 supporting:
//! - HTTP/1.1 + HTTP/2 auto-detection over TCP (via hyper-util)
//! - HTTP/3 over QUIC/UDP (via quinn + h3, feature-gated)
//!
//! Self-signed certificates are generated automatically if no TLS cert is
//! configured, following the Let's Encrypt short-lived cert approach (45 days).

#![allow(
    clippy::redundant_closure_for_method_calls,
    clippy::ignored_unit_patterns,
    clippy::map_unwrap_or
)]

use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;

use hyper::body::Incoming;
use hyper::service::service_fn;
use hyper_util::rt::{TokioExecutor, TokioIo};
use hyper_util::server::conn::auto::Builder;
use tokio::net::TcpListener;

use csaf_core::config::AppConfig;
use csaf_core::storage::CsafStorage;
use csaf_models::db::DbPool;

use csaf_crud::api;
use csaf_crud::app_state::AppState;
use csaf_crud::router::{Router, SharedRouter, handler_fn};
use csaf_crud::routes;
use csaf_crud::static_files;

// ---------------------------------------------------------------------------
// Tracing / logging
// ---------------------------------------------------------------------------

/// Install the tracing subscriber with stderr + daily rolling file
/// appender rooted at `log_directory`.
///
/// Returns a [`tracing_appender::non_blocking::WorkerGuard`] that MUST be
/// held for the lifetime of the process — dropping it flushes buffered
/// log lines. If the log directory cannot be created or opened, a
/// warning is emitted to stderr and the subscriber falls back to
/// stderr-only (the process never fails to start because of logging).
fn init_tracing(log_directory: &str) -> Option<tracing_appender::non_blocking::WorkerGuard> {
    use tracing_subscriber::{EnvFilter, Layer, layer::SubscriberExt, util::SubscriberInitExt};

    // Build *two* independent EnvFilters (they do not impl Clone in a
    // way that preserves the parse state, so construct each from
    // scratch). Both honour `RUST_LOG` and default to `info`, which
    // keeps rustls / quinn / h2 TRACE noise out of the rolling file.
    let filter_for =
        || EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));

    // Try to create the log directory + open a daily-rolling appender.
    // Any failure falls back to stderr-only logging with a warning.
    let file_layer = match std::fs::create_dir_all(log_directory) {
        Ok(()) => {
            let appender = tracing_appender::rolling::daily(log_directory, "csaf-crud.log");
            let (writer, guard) = tracing_appender::non_blocking(appender);
            let layer = tracing_subscriber::fmt::layer()
                .with_ansi(false)
                .with_writer(writer)
                .with_filter(filter_for());
            Some((layer, guard))
        },
        Err(err) => {
            eprintln!(
                "warning: failed to create log directory {log_directory:?}: {err}; \
                 falling back to stderr-only logging"
            );
            None
        },
    };

    let stderr_layer = tracing_subscriber::fmt::layer()
        .with_writer(std::io::stderr)
        .with_filter(filter_for());

    let registry = tracing_subscriber::registry().with(stderr_layer);

    let guard = if let Some((file_layer, guard)) = file_layer {
        registry.with(file_layer).init();
        Some(guard)
    } else {
        registry.init();
        None
    };

    tracing::info!(log_directory, "tracing subscriber initialized");
    guard
}

// ---------------------------------------------------------------------------
// TLS helpers
// ---------------------------------------------------------------------------

/// Generate a self-signed TLS certificate valid for 45 days.
fn generate_self_signed_cert() -> anyhow::Result<(
    Vec<rustls::pki_types::CertificateDer<'static>>,
    rustls::pki_types::PrivateKeyDer<'static>,
)> {
    use rcgen::{CertificateParams, SanType};
    use std::net::{Ipv4Addr, Ipv6Addr};

    let mut params = CertificateParams::new(vec!["localhost".to_owned()])?;
    params.subject_alt_names = vec![
        SanType::DnsName("localhost".try_into()?),
        SanType::IpAddress(Ipv4Addr::LOCALHOST.into()),
        SanType::IpAddress(Ipv6Addr::LOCALHOST.into()),
    ];

    let now = time::OffsetDateTime::now_utc();
    params.not_before = now;
    params.not_after = now + time::Duration::days(45);

    let key_pair = rcgen::KeyPair::generate()?;
    let cert = params.self_signed(&key_pair)?;
    let cert_der = rustls::pki_types::CertificateDer::from(cert);
    let key_der = rustls::pki_types::PrivateKeyDer::try_from(key_pair.serialize_der())
        .map_err(|e| anyhow::anyhow!("Failed to parse private key: {e}"))?;

    Ok((vec![cert_der], key_der))
}

/// Build a TLS 1.3-only server configuration.
fn build_tls13_server_config(
    cert_chain: Vec<rustls::pki_types::CertificateDer<'static>>,
    key_der: rustls::pki_types::PrivateKeyDer<'static>,
    alpn: Vec<Vec<u8>>,
) -> anyhow::Result<rustls::ServerConfig> {
    let mut config =
        rustls::ServerConfig::builder_with_protocol_versions(&[&rustls::version::TLS13])
            .with_no_client_auth()
            .with_single_cert(cert_chain, key_der)?;

    config.alpn_protocols = alpn;
    Ok(config)
}

// ---------------------------------------------------------------------------
// HTTP/3 QUIC server (feature-gated)
// ---------------------------------------------------------------------------

#[cfg(feature = "quic")]
async fn run_quic_server(
    addr: SocketAddr,
    router: SharedRouter,
    cert_chain: Vec<rustls::pki_types::CertificateDer<'static>>,
    key_der: rustls::pki_types::PrivateKeyDer<'static>,
) -> anyhow::Result<()> {
    let mut tls_config = build_tls13_server_config(cert_chain, key_der, vec![b"h3".to_vec()])?;
    tls_config.max_early_data_size = u32::MAX;

    let server_config = quinn::ServerConfig::with_crypto(Arc::new(
        quinn::crypto::rustls::QuicServerConfig::try_from(tls_config)?,
    ));

    let endpoint = quinn::Endpoint::server(server_config, addr)?;
    tracing::info!(%addr, "QUIC/HTTP3 endpoint listening (TLS 1.3)");

    while let Some(incoming) = endpoint.accept().await {
        let router = router.clone();
        tokio::spawn(async move {
            if let Err(e) = handle_quic_connection(incoming, router).await {
                tracing::debug!(error = %e, "QUIC connection error");
            }
        });
    }

    Ok(())
}

#[cfg(feature = "quic")]
async fn handle_quic_connection(
    incoming: quinn::Incoming,
    router: SharedRouter,
) -> anyhow::Result<()> {
    let connection = incoming.await?;
    let mut h3_conn = h3::server::Connection::new(h3_quinn::Connection::new(connection)).await?;

    loop {
        match h3_conn.accept().await {
            Ok(Some(resolver)) => {
                let (req, mut stream) = resolver.resolve_request().await?;
                let router = router.clone();
                tokio::spawn(async move {
                    let (parts, _) = req.into_parts();
                    let response = router.handle_parts(parts).await;

                    let resp_headers = http::Response::builder()
                        .status(response.status())
                        .body(())
                        .unwrap_or_default();

                    if let Err(e) = stream.send_response(resp_headers).await {
                        tracing::debug!(error = %e, "Failed to send h3 response headers");
                        return;
                    }

                    let body_bytes = http_body_util::BodyExt::collect(response.into_body())
                        .await
                        .map(|b| b.to_bytes())
                        .unwrap_or_default();

                    if let Err(e) = stream.send_data(body_bytes).await {
                        tracing::debug!(error = %e, "Failed to send h3 response body");
                    }

                    let _ = stream.finish().await;
                });
            },
            Ok(None) => break,
            Err(e) => {
                tracing::debug!(error = %e, "H3 accept error");
                break;
            },
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Install the rustls crypto provider (ring). `install_default` is
    // infallible in practice — it only errors if a provider is already
    // installed, which cannot happen at the top of `main`. We still
    // handle the error explicitly rather than panicking.
    if rustls::crypto::ring::default_provider()
        .install_default()
        .is_err()
    {
        return Err(anyhow::anyhow!(
            "rustls crypto provider was already installed"
        ));
    }

    // Load configuration.
    let config_path = std::env::args()
        .nth(1)
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("config/default.toml"));

    let config = AppConfig::load(&config_path)?;

    // Ensure data directory exists.
    tokio::fs::create_dir_all(&config.data_dir).await.ok();

    // Open databases.
    let csaf_storage = CsafStorage::open(&config.redb_path())?;
    let db_pool = DbPool::open(&config.sqlite_path())
        .map_err(|e| anyhow::anyhow!("SQLite open failed: {e}"))?;

    // Load settings from storage (or defaults) — needed below to bootstrap
    // the rolling log appender with the user-configured log directory.
    let settings = csaf_storage.get_settings().unwrap_or_default();

    // Install the tracing subscriber. Emit every line both to stderr
    // (keeps Docker-style `docker logs` friendly) and to a daily-rotated
    // file in `settings.log_directory`. A failure to open the log dir is
    // not fatal — stderr keeps working.
    let _log_guard = init_tracing(&settings.log_directory);

    // Build application state and router.
    let state = AppState::new(csaf_storage, db_pool, config.clone(), settings);
    let shared_router = build_router(state)?.into_shared();

    // Generate self-signed TLS certificate.
    let (certs, key) = generate_self_signed_cert()?;

    // --- TCP listener (HTTP/1.1 + HTTP/2 over TLS 1.3) ---
    let tcp_addr: SocketAddr = config.listen_address().parse()?;
    let tcp_tls_config = build_tls13_server_config(
        certs.clone(),
        key.clone_key(),
        vec![b"h2".to_vec(), b"http/1.1".to_vec()],
    )?;
    let tcp_tls_acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(tcp_tls_config));
    let listener = TcpListener::bind(tcp_addr).await?;
    tracing::info!(%tcp_addr, "Listening (TLS 1.3 -- HTTP/1.1 + HTTP/2 over TCP)");

    // --- QUIC/HTTP3 listener (UDP, TLS 1.3) ---
    #[cfg(feature = "quic")]
    {
        let quic_port = config.listen_port + 1;
        let quic_addr: SocketAddr = format!("{}:{quic_port}", config.listen_addr).parse()?;
        let quic_router = shared_router.clone();
        let quic_certs = certs;
        let quic_key = key;
        tokio::spawn(async move {
            if let Err(e) = run_quic_server(quic_addr, quic_router, quic_certs, quic_key).await {
                tracing::error!(error = %e, "QUIC server failed");
            }
        });
        tracing::info!(%quic_addr, "QUIC/HTTP3 listener spawned (UDP, TLS 1.3)");
    }

    // --- TCP/TLS accept loop ---
    loop {
        let (stream, remote_addr) = match listener.accept().await {
            Ok(pair) => pair,
            Err(e) => {
                tracing::warn!(error = %e, "TCP accept error");
                continue;
            },
        };

        let router = shared_router.clone();
        let acceptor = tcp_tls_acceptor.clone();

        tokio::spawn(async move {
            let tls_stream = match acceptor.accept(stream).await {
                Ok(s) => s,
                Err(e) => {
                    tracing::debug!(error = %e, %remote_addr, "TLS handshake failed");
                    return;
                },
            };

            let io = TokioIo::new(tls_stream);
            let service = service_fn(move |req: http::Request<Incoming>| {
                let router = router.clone();
                async move { Ok::<_, std::convert::Infallible>(router.handle(req).await) }
            });

            if let Err(e) = Builder::new(TokioExecutor::new())
                .serve_connection(io, service)
                .await
            {
                tracing::debug!(error = %e, %remote_addr, "Connection error");
            }
        });
    }
}

/// Build the router with all routes.
///
/// # Errors
///
/// Returns `matchit::InsertError` if any route template is malformed or
/// conflicts with a previously registered route. All templates here are
/// hard-coded so this is effectively a compile-time check performed at
/// startup.
fn build_router(state: AppState) -> Result<Router, matchit::InsertError> {
    Router::new(state)
        // Static files
        .get("/static/{*path}", handler_fn(static_files::serve_static))?
        // UI routes
        .get("/", handler_fn(routes::home::dashboard))?
        .get("/csaf", handler_fn(routes::csaf::list))?
        .get("/csaf/new", handler_fn(routes::csaf::new_form))?
        .post("/csaf", handler_fn(routes::csaf::create))?
        .get("/csaf/{id}", handler_fn(routes::csaf::view))?
        .get("/csaf/{id}/edit", handler_fn(routes::csaf::edit_form))?
        .post("/csaf/{id}/update", handler_fn(routes::csaf::update))?
        .post("/csaf/{id}/delete", handler_fn(routes::csaf::delete))?
        .get("/csaf/{id}/json", handler_fn(routes::csaf::json_download))?
        .get("/admin/import", handler_fn(routes::admin::import_page))?
        .post("/admin/import", handler_fn(routes::admin::import_execute))?
        .get("/admin/export", handler_fn(routes::admin::export_page))?
        .post("/admin/export", handler_fn(routes::admin::export_execute))?
        .post("/admin/dump", handler_fn(routes::admin::dump_execute))?
        .post(
            "/admin/export-audit",
            handler_fn(routes::admin::audit_export_execute),
        )?
        .get("/settings", handler_fn(routes::settings::settings_form))?
        .post("/settings", handler_fn(routes::settings::settings_update))?
        .post(
            "/settings/reset",
            handler_fn(routes::settings::settings_reset),
        )?
        .post(
            "/settings/toggle-theme",
            handler_fn(routes::settings::toggle_theme),
        )?
        .get("/info/about", handler_fn(routes::info::about))?
        .get("/info/license", handler_fn(routes::info::license))?
        .get("/info/system", handler_fn(routes::info::system_info))?
        .get("/info/privacy", handler_fn(routes::info::privacy))?
        .get("/info/security", handler_fn(routes::info::security))?
        // API v1 routes (HATEOAS)
        .get("/api/v1", handler_fn(api::v1::csaf::api_root))?
        .get("/api/v1/csaf", handler_fn(api::v1::csaf::list_csaf))?
        .post("/api/v1/csaf", handler_fn(api::v1::csaf::create_csaf))?
        .get("/api/v1/csaf/{id}", handler_fn(api::v1::csaf::get_csaf))?
        .put("/api/v1/csaf/{id}", handler_fn(api::v1::csaf::update_csaf))?
        .delete("/api/v1/csaf/{id}", handler_fn(api::v1::csaf::delete_csaf))?
        .get(
            "/api/v1/csaf/{id}/validate",
            handler_fn(api::v1::csaf::validate_csaf),
        )?
        .get(
            "/api/v1/audit-log",
            handler_fn(api::v1::audit_log::list_audit_log),
        )?
        .get(
            "/api/v1/settings",
            handler_fn(api::v1::settings::get_settings),
        )?
        .put(
            "/api/v1/settings",
            handler_fn(api::v1::settings::update_settings),
        )?
        .get(
            "/api/v1/system/info",
            handler_fn(api::v1::system::system_info),
        )?
        .get(
            "/api/v1/system/health",
            handler_fn(api::v1::system::health_check),
        )
}