BREP_plm 0.22.0

The next.BREP.io PLM server for the Rust B-rep CAD kernel: parts, revisions, lifecycle, checkout, BOMs and configurable workflows over an embedded SQLite store, with a browser UI and the document API the CAD app stores through.
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
//! `brep-plm` — serve the PLM.
//!
//! ```text
//! brep-plm serve [--data <dir>] [--bind <addr>] [--scripts <dir>]
//!                [--secure-cookies off|on|auto] [--trust-proxy]
//!                [--tls-cert <pem> --tls-key <pem>] [--lock-script-editor]
//!                [--max-attachment-mb <n>] [--max-document-mb <n>]
//!                [--min-client-version <v>]
//!                [--backup-dir <dir> [--backup-every <minutes>] [--backup-keep <n>]]
//!                [--web-dir <dir>]
//!                [--bake-worker-executable <file> --bake-worker-token-file <file>
//!                 --bake-worker-url <url>]
//!                [--cad-app <dir>] [--cad-connect-src <origin>]...
//! brep-plm backup  [--data <dir>] [--scripts <dir>] --out <file.tar.gz>
//! brep-plm restore --from <file.tar.gz> --data <new dir>
//! brep-plm export  [--data <dir>] --out <file> [--format json|parts-csv|structure-csv]
//! ```
//!
//! The hardening flags are described in `brep_plm::security` and GUIDE.md.
//!
//! The data directory holds `plm.sqlite` (the metadata and the audit log) and
//! a `models/` tree; both are created on first run. The first run also seeds one
//! administrator and prints its generated password ONCE. No build of this
//! server ships a known password, which is why it cannot be recovered — make
//! another admin before you lose it.
//!
//! `--scripts` names the administrator's scripts directory (default
//! `<data>/scripts`), typically a git checkout. See `brep_plm::scripting`.
//!
//! `--cad-app` names the directory the wasm CAD app is served from (default
//! `<data>/cad-app`), at `/cad/app/web/index.html`. See `brep_plm::cad`.

use std::sync::Arc;

use brep_plm::api;
use brep_plm::db::Db;
use brep_plm::security::{CookieSecurity, ServerConfig};

const DEFAULT_BIND: &str = "127.0.0.1:8088";
const DEFAULT_DATA: &str = "./plm-data";

fn usage() -> ! {
    use brep_plm::version::MIN_CLIENT_VERSION as MIN_CLIENT;
    eprintln!(
        "brep-plm — the BREP PLM server\n\
         \n\
         USAGE:\n    \
             brep-plm serve [OPTIONS]\n    \
             brep-plm backup  [--data <dir>] [--scripts <dir>] --out <file.tar.gz>\n    \
             brep-plm restore --from <file.tar.gz> --data <new dir>\n    \
             brep-plm export  [--data <dir>] --out <file> [--format json|parts-csv|structure-csv]\n\
         \n\
         OPTIONS:\n    \
             --data <dir>             where plm.sqlite and models/ live [default: {DEFAULT_DATA}]\n    \
             --bind <addr>            the listen address              [default: {DEFAULT_BIND}]\n    \
             --scripts <dir>          the admin's hook scripts        [default: <data>/scripts]\n    \
             --secure-cookies <mode>  off | on | auto                 [default: off; on with --tls-cert]\n    \
             --trust-proxy            believe X-Forwarded-For / -Proto from a reverse proxy\n    \
             --tls-cert <pem>         serve HTTPS with this certificate chain\n    \
             --tls-key <pem>          ... and this private key\n    \
             --lock-script-editor     turn the in-browser script editor off, whatever Settings say\n    \
             --max-attachment-mb <n>  the largest attachment upload       [default: 100]\n    \
             --max-document-mb <n>    the largest document or recovery write [default: 64]\n    \
             --min-client-version <v> refuse CAD apps older than v (raise only) [default: {MIN_CLIENT}]\n    \
             --backup-dir <dir>       where \"Back up now\" and scheduled backups are saved\n    \
             --backup-every <min>     take a backup every <min> minutes    [needs --backup-dir]\n    \
             --backup-keep <n>        how many saved backups to keep       [default: 7]\n    \
             --web-dir <dir>          live frontend overrides (missing files use embedded assets)\n    \
             --bake-worker-executable <file>  native brep-app for admin Start/Stop\n    \
             --bake-worker-token-file <file>  worker API token file (never the token itself)\n    \
             --bake-worker-url <url>   address the worker uses to reach this server\n    \
             --cad-app <dir>          the hosted CAD app's web/ and pkg/   [default: <data>/cad-app]\n    \
             --cad-connect-src <url>  an origin the hosted app may call (repeatable)\n"
    );
    std::process::exit(2)
}

#[tokio::main]
async fn main() {
    let mut args = std::env::args().skip(1);
    let command = args.next().unwrap_or_else(|| "serve".into());
    if command == "-h" || command == "--help" {
        usage();
    }
    if command == "backup" {
        backup(args);
    }
    if command == "restore" {
        restore(args);
    }
    if command == "export" {
        export(args);
    }
    if command != "serve" {
        eprintln!("brep-plm: unknown command '{command}'");
        usage();
    }

    let mut bind = DEFAULT_BIND.to_string();
    let mut data = DEFAULT_DATA.to_string();
    let mut scripts: Option<std::path::PathBuf> = None;
    let mut config = ServerConfig::default();
    let mut worker_executable: Option<std::path::PathBuf> = None;
    let mut worker_token: Option<std::path::PathBuf> = None;
    let mut worker_url: Option<String> = None;
    let mut secure_cookies: Option<CookieSecurity> = None;
    let mut tls_cert: Option<std::path::PathBuf> = None;
    let mut tls_key: Option<std::path::PathBuf> = None;
    while let Some(flag) = args.next() {
        match flag.as_str() {
            "--bind" => bind = args.next().unwrap_or_else(|| usage()),
            "--data" => data = args.next().unwrap_or_else(|| usage()),
            "--scripts" => scripts = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--secure-cookies" => {
                let mode = args.next().unwrap_or_else(|| usage());
                secure_cookies = Some(mode.parse().unwrap_or_else(|e| {
                    eprintln!("brep-plm: --secure-cookies: {e}");
                    usage()
                }));
            }
            "--trust-proxy" => config.trust_proxy = true,
            "--tls-cert" => tls_cert = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--tls-key" => tls_key = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--lock-script-editor" => config.lock_script_editor = true,
            "--backup-dir" => config.backup_dir = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--backup-every" => {
                let text = args.next().unwrap_or_else(|| usage());
                config.backup_every_minutes = text.parse().ok().filter(|n| *n > 0).unwrap_or_else(|| {
                    eprintln!("brep-plm: --backup-every takes a whole number of minutes above 0, not '{text}'");
                    usage()
                });
            }
            "--backup-keep" => {
                let text = args.next().unwrap_or_else(|| usage());
                config.backup_keep = text.parse().ok().filter(|n| *n > 0).unwrap_or_else(|| {
                    eprintln!("brep-plm: --backup-keep takes a whole number above 0, not '{text}'");
                    usage()
                });
            }
            "--web-dir" => config.web_dir = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--bake-worker-executable" => worker_executable = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--bake-worker-token-file" => worker_token = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--bake-worker-url" => worker_url = Some(args.next().unwrap_or_else(|| usage())),
            "--cad-app" => config.cad_app_dir = Some(args.next().unwrap_or_else(|| usage()).into()),
            "--cad-connect-src" => {
                let origin = args.next().unwrap_or_else(|| usage());
                if let Err(error) = brep_plm::cad::check_connect_origin(&origin) {
                    eprintln!("brep-plm: --cad-connect-src: {error}");
                    usage();
                }
                config.cad_connect_src.push(origin);
            }
            "--max-attachment-mb" => {
                let text = args.next().unwrap_or_else(|| usage());
                let mb: u64 = text.parse().ok().filter(|n| *n > 0).unwrap_or_else(|| {
                    eprintln!("brep-plm: --max-attachment-mb takes a whole number of megabytes above 0, not '{text}'");
                    usage()
                });
                config.max_attachment_bytes = mb.saturating_mul(1024 * 1024);
            }
            "--max-document-mb" => {
                let text = args.next().unwrap_or_else(|| usage());
                let mb: u64 = text.parse().ok().filter(|n| *n > 0).unwrap_or_else(|| {
                    eprintln!("brep-plm: --max-document-mb takes a whole number of megabytes above 0, not '{text}'");
                    usage()
                });
                config.max_document_bytes = mb.saturating_mul(1024 * 1024);
            }
            "--min-client-version" => {
                let text = args.next().unwrap_or_else(|| usage());
                if let Err(error) = brep_plm::version::check_floor(&text) {
                    eprintln!("brep-plm: --min-client-version: {error}");
                    std::process::exit(2);
                }
                config.min_client_version = Some(text);
            }
            other => {
                eprintln!("brep-plm: unknown option '{other}'");
                usage();
            }
        }
    }
    if config.backup_every_minutes > 0 && config.backup_dir.is_none() {
        eprintln!("brep-plm: --backup-every needs --backup-dir");
        usage();
    }
    if let Some(dir) = &config.backup_dir {
        if let Err(error) = std::fs::create_dir_all(dir) {
            eprintln!("brep-plm: --backup-dir {}: {error}", dir.display());
            std::process::exit(1);
        }
    }
    let tls = match (&tls_cert, &tls_key) {
        (Some(cert), Some(key)) => match brep_plm::tls::load(cert, key) {
            Ok(tls) => Some(tls),
            Err(error) => {
                eprintln!("brep-plm: TLS: {error}");
                std::process::exit(1);
            }
        },
        (None, None) => None,
        _ => {
            eprintln!("brep-plm: --tls-cert and --tls-key go together");
            usage();
        }
    };
    config.tls = tls.is_some();
    // Serving HTTPS itself, the server knows every request is secure.
    config.secure_cookies = secure_cookies.unwrap_or(if config.tls { CookieSecurity::On } else { CookieSecurity::Off });

    if let Some(dir) = config.web_dir.take() {
        match dir.canonicalize() {
            Ok(path) if path.is_dir() => config.web_dir = Some(path),
            _ => {
                eprintln!("brep-plm: --web-dir must name an existing directory: {}", dir.display());
                std::process::exit(1);
            }
        }
    }

    if worker_executable.is_some() || worker_token.is_some() || worker_url.is_some() {
        let (Some(executable), Some(token_file), Some(server_url)) = (worker_executable, worker_token, worker_url) else {
            eprintln!("brep-plm: configure all three --bake-worker-executable, --bake-worker-token-file and --bake-worker-url options");
            usage();
        };
        config.bake_worker = Some(brep_plm::bake_worker::Config { executable, token_file, server_url });
    }
    let (db, seeded) = match Db::open_with_scripts(&data, scripts) {
        Ok(opened) => opened,
        Err(error) => {
            eprintln!("brep-plm: cannot open {data}: {error}");
            std::process::exit(1);
        }
    };

    if let Some(password) = seeded {
        // Printed once, to stdout, and never stored in recoverable form.
        println!("\n  ── first run ──────────────────────────────────────────");
        println!("  an administrator account was created:");
        println!("      username  admin");
        println!("      password  {password}");
        println!("  this password is shown ONCE and is not recoverable.");
        println!("  ───────────────────────────────────────────────────────\n");
    }

    if config.cad_app_dir.is_none() {
        config.cad_app_dir = Some(std::path::Path::new(&data).join(brep_plm::cad::DEFAULT_DIR));
    }
    if let Some(dir) = &config.cad_app_dir {
        let state = if dir.join("web/index.html").is_file() { "" } else { " (no web/index.html there yet)" };
        println!("brep-plm: CAD app from {}{state}", dir.display());
    }
    if let Some(dir) = &config.web_dir {
        println!("brep-plm: live frontend overrides from {} (embedded fallback)", dir.display());
    }
    let db = db.with_config(config);
    println!("brep-plm: scripts in {}", db.scripts().dir().display());
    if db.security().config.lock_script_editor {
        println!("brep-plm: the in-browser script editor is locked off");
    }
    let db = Arc::new(db);
    let every = db.security().config.backup_every_minutes;
    if every > 0 {
        println!(
            "brep-plm: a backup every {every} minute(s) into {}",
            db.security().config.backup_dir.as_ref().map(|d| d.display().to_string()).unwrap_or_default()
        );
        let scheduled = Arc::clone(&db);
        tokio::spawn(async move {
            let mut tick = tokio::time::interval(std::time::Duration::from_secs(every * 60));
            tick.tick().await; // the first tick is immediate; the first backup is one period in
            loop {
                tick.tick().await;
                let db = Arc::clone(&scheduled);
                match tokio::task::spawn_blocking(move || db.backup_now("schedule")).await {
                    Ok(Ok(last)) => println!("brep-plm: backup {} ({} files, {} bytes)", last.file, last.files, last.size),
                    Ok(Err(error)) => eprintln!("brep-plm: scheduled backup failed: {}", error.message),
                    Err(error) => eprintln!("brep-plm: scheduled backup panicked: {error}"),
                }
            }
        });
    }
    let worker_db = db.clone();
    let app = api::router(db);
    let listener = match tokio::net::TcpListener::bind(&bind).await {
        Ok(listener) => listener,
        Err(error) => {
            eprintln!("brep-plm: cannot bind {bind}: {error}");
            std::process::exit(1);
        }
    };
    let shutdown = async move {
        #[cfg(unix)]
        {
            let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("SIGTERM handler");
            tokio::select! { _ = tokio::signal::ctrl_c() => {}, _ = terminate.recv() => {} }
        }
        #[cfg(not(unix))]
        let _ = tokio::signal::ctrl_c().await;
        if let Err(error) = worker_db.stop_bake_worker() {
            eprintln!("brep-plm: stopping bake worker: {error}");
        }
        println!("\nbrep-plm: shutting down");
    };
    // One line naming the address actually bound, so a runner that asked
    // for port 0 learns the port without racing for one itself.
    match listener.local_addr() {
        Ok(addr) => println!("brep-plm: listening on {addr}"),
        Err(error) => eprintln!("brep-plm: cannot read the bound address: {error}"),
    }
    if let Some(tls) = tls {
        println!("brep-plm: serving {data} over https");
        brep_plm::tls::serve(listener, app, tls, shutdown).await;
        return;
    }
    println!("brep-plm: serving {data} over http");
    let service = app.into_make_service_with_connect_info::<std::net::SocketAddr>();
    if let Err(error) = axum::serve(listener, service).with_graceful_shutdown(shutdown).await {
        eprintln!("brep-plm: {error}");
        std::process::exit(1);
    }
}

/// Pull `--flag value` pairs out of the arguments.
fn flags(args: impl Iterator<Item = String>, known: &[&str]) -> std::collections::BTreeMap<String, String> {
    let mut out = std::collections::BTreeMap::new();
    let mut args = args;
    while let Some(flag) = args.next() {
        if !known.contains(&flag.as_str()) {
            eprintln!("brep-plm: unknown option '{flag}'");
            usage();
        }
        let value = args.next().unwrap_or_else(|| usage());
        out.insert(flag, value);
    }
    out
}

/// `brep-plm backup`: a consistent backup, while a server runs or not.
fn backup(args: impl Iterator<Item = String>) -> ! {
    let f = flags(args, &["--data", "--scripts", "--out"]);
    let data = std::path::PathBuf::from(f.get("--data").cloned().unwrap_or_else(|| DEFAULT_DATA.into()));
    let Some(out) = f.get("--out").map(std::path::PathBuf::from) else {
        eprintln!("brep-plm: backup needs --out <file.tar.gz>");
        usage()
    };
    let scripts = f.get("--scripts").map(std::path::PathBuf::from);
    match brep_plm::backup::backup(&data, scripts.as_deref(), &out) {
        Ok(manifest) => {
            println!(
                "brep-plm: backed up {} to {}: {} files, {} bytes of data, seq {}{}",
                data.display(),
                out.display(),
                manifest.files.len(),
                manifest.total_size(),
                manifest.seq,
                if manifest.attempts > 1 { format!(", {} tries", manifest.attempts) } else { String::new() }
            );
            if let Some(notice) = manifest.adjusted_notice() {
                println!("brep-plm: note: {notice}");
            }
            std::process::exit(0)
        }
        Err(error) => {
            eprintln!("brep-plm: backup {}: {error}", data.display());
            std::process::exit(1)
        }
    }
}

/// `brep-plm restore`: into an empty directory, every file checked.
fn restore(args: impl Iterator<Item = String>) -> ! {
    let f = flags(args, &["--from", "--data"]);
    let (Some(from), Some(data)) = (f.get("--from"), f.get("--data")) else {
        eprintln!("brep-plm: restore needs --from <file.tar.gz> and --data <new dir>");
        usage()
    };
    match brep_plm::backup::restore(std::path::Path::new(from), std::path::Path::new(data)) {
        Ok(manifest) => {
            println!(
                "brep-plm: restored {} files ({} bytes) into {}, seq {}; serve it with --data {data}",
                manifest.files.len(),
                manifest.total_size(),
                data,
                manifest.seq
            );
            if let Some(notice) = manifest.adjusted_notice() {
                println!("brep-plm: note: {notice}");
            }
            std::process::exit(0)
        }
        Err(error) => {
            eprintln!("brep-plm: restore {from}: {error}");
            std::process::exit(1)
        }
    }
}

/// `brep-plm export`: the metadata as JSON, or parts or structure as CSV.
fn export(args: impl Iterator<Item = String>) -> ! {
    let f = flags(args, &["--data", "--out", "--format"]);
    let data = std::path::PathBuf::from(f.get("--data").cloned().unwrap_or_else(|| DEFAULT_DATA.into()));
    let Some(out) = f.get("--out") else {
        eprintln!("brep-plm: export needs --out <file>");
        usage()
    };
    let format = f.get("--format").map(String::as_str).unwrap_or("json");
    let state = match brep_plm::backup::load_snapshot(&data) {
        Ok(state) => state,
        Err(error) => {
            eprintln!("brep-plm: export {}: {error}", data.display());
            std::process::exit(1)
        }
    };
    let text = match format {
        "json" => serde_json::to_string_pretty(&brep_plm::backup::export_json(&state)).expect("JSON"),
        "parts-csv" => brep_plm::backup::parts_csv(&state),
        "structure-csv" => brep_plm::backup::structure_csv(&state),
        other => {
            eprintln!("brep-plm: --format is json, parts-csv or structure-csv, not '{other}'");
            usage()
        }
    };
    if let Err(error) = std::fs::write(out, text) {
        eprintln!("brep-plm: export {out}: {error}");
        std::process::exit(1)
    }
    println!("brep-plm: exported {} parts from {} to {out} ({format})", state.parts.len(), data.display());
    std::process::exit(0)
}