vex2pdf 2.0.1

A tool to convert CycloneDX(VEX) JSON or XML documents to PDF reports
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! # vex2pdf - CycloneDX to PDF Converter
//!
//! A Rust library for converting CycloneDX VEX/VDR/SBOM documents (JSON and XML formats)
//! to professional PDF reports with embedded fonts, color-coded vulnerability analysis,
//! and concurrent processing support.
//!
//! ## CycloneDX Compatibility
//!
//! This library fully supports CycloneDX schema version 1.5 and provides compatibility
//! for version 1.6 documents that only use 1.5 fields. Documents using 1.6-specific
//! fields may not process correctly.
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use vex2pdf::lib_utils::config::Config;
//! use vex2pdf::run;
//!
//! let config = Config::default()
//!     .working_path("./input")
//!     .output_dir("./output")
//!     .max_jobs(Some(4));
//!
//! run(config).expect("Failed to process files");
//! ```
//!
//! ## Configuration
//!
//! Configuration is managed through the [`Config`] struct
//! using the builder pattern for flexibility.
//!
//! ### Builder Pattern (Recommended)
//!
//! Use method chaining to configure exactly what you need:
//!
//! ```rust
//! use vex2pdf::lib_utils::config::Config;
//!
//! let config = Config::default()
//!     .working_path("./input")
//!     .output_dir("./output")
//!     .max_jobs(Some(4))
//!     .report_title("Q4 2024 Security Report")
//!     .show_components(true);
//! ```
//!
//! ### Available Builder Methods
//!
//! - [`working_path()`](lib_utils::config::Config::working_path) - Set input directory
//! - [`output_dir()`](lib_utils::config::Config::output_dir) - Set output directory
//! - [`max_jobs()`](lib_utils::config::Config::max_jobs) - Control concurrent processing
//! - [`report_title()`](lib_utils::config::Config::report_title) - Custom report title
//! - [`pdf_meta_name()`](lib_utils::config::Config::pdf_meta_name) - Custom PDF metadata
//! - [`show_novulns_msg()`](lib_utils::config::Config::show_novulns_msg) - Show/hide "no vulnerabilities" message
//! - [`show_components()`](lib_utils::config::Config::show_components) - Show/hide components list
//! - [`pure_bom_novulns()`](lib_utils::config::Config::pure_bom_novulns) - Treat as pure BOM
//! - And more...
//!
//! See [`Config`] documentation for the complete list.
//!
//! ### For CLI Applications
//!
//! CLI applications should use `Config::build_from_env_cli()` to parse
//! command-line arguments and environment variables.
//!
//! For detailed CLI documentation:
//! - [Configuration Guide](https://gitlab.com/jurassicLizard/vex2pdf/-/blob/master/README.md#configuration)
//! - [Environment Variables Reference](https://gitlab.com/jurassicLizard/vex2pdf/-/blob/master/README.md#environment-variables)
//!
//! ## Features
//!
//! - **Multi-format support**: JSON and XML CycloneDX documents
//! - **Document types**: VEX, VDR, and SBOM/BOM
//! - **Vulnerability analysis rendering**: Color-coded states (Exploitable, Resolved, In Triage, etc.) and response actions
//! - **Optional concurrent processing**: Feature flag enables threadpool with configurable job limits (single-threaded to max parallelism)
//! - **Embedded fonts**: Liberation Sans fonts built-in, no external dependencies
//! - **Structured logging**: Info/debug to stdout, warnings/errors to stderr
//! - **Memory safe**: Unsafe code forbidden at compile-time
//! - **CLI and library**: Use as standalone tool or integrate into your application
//! - **Flexible feature flags**: Choose exactly the dependencies you need (`cli`, `concurrency`, or both)
//!
//! ## Documentation
//!
//! - **[README](https://gitlab.com/jurassicLizard/vex2pdf/-/blob/master/README.md)** - Installation instructions, CLI usage, environment variables, and configuration
//! - **[Developer Notes](https://gitlab.com/jurassicLizard/vex2pdf/-/blob/master/docs/DEVELOPER_NOTES.md)** - Testing, code coverage, architecture details, and trait system
//! - **[API Documentation](https://docs.rs/vex2pdf)** - Full API reference on docs.rs
//! - **[Changelog](https://gitlab.com/jurassicLizard/vex2pdf/-/blob/master/CHANGELOG.md)** - Version history and release notes
//!
//! ## Library Architecture
//!
//! The library is organized into modules:
//! - `pdf`: PDF generation functionality
//!   - `font_config`: Embedded font management
//!   - `generator`: PDF document generation with analysis rendering
//! - `lib_utils`: Configuration, CLI arguments, environment variables, and utility functions
//! - `files_proc`: File discovery, processing pipeline, and trait system
//!   - `processor`: Main processing logic with trait abstractions (uses optional concurrency)
//!   - `model`: File identification and processing state
//!
//! **Concurrency**: When the `concurrency` feature is enabled (default), the library uses
//! `jlizard-simple-threadpool` for concurrent file processing. Without this feature, files
//! are processed sequentially in the main thread.
//!

#![forbid(unsafe_code)]
// Re-export and simplify paths for consumers of this library
pub use crate::lib_utils::run_utils as utils;
// re-export upstream cyclondx bom path
pub use cyclonedx_bom;

pub mod files_proc {
    pub mod model {
        pub mod file_ident;
        pub mod files_pending_proc;
        pub mod input_file_type;
    }
    pub mod processor;
    pub mod traits;
}
pub mod pdf {
    pub mod font_config;
    pub mod generator;
}

pub mod lib_utils {
    #[cfg(feature = "cli")]
    pub mod cli_args;
    pub mod config;
    pub mod env_vars;
    pub mod errors;
    pub mod run_utils;
}

use crate::files_proc::processor::DefaultFilesProcessor;
use crate::files_proc::traits::{FileSearchProvider, MultipleFilesProcProvider};
use crate::lib_utils::errors::Vex2PdfError;
use lib_utils::config::Config;

/// Processes CycloneDX VEX documents according to the provided configuration.
///
/// This function serves as the main entry point for the library's functionality.
/// It finds and processes both JSON and XML files as specified in the configuration,
/// converting them to PDF format with embedded fonts.
///
/// # Arguments
///
/// * `config` - Configuration settings that control processing behavior
///
/// # Returns
///
/// * `Result<(), Vex2PdfError>` - Success (`Ok`) if processing completes without errors,
///   or an error (`Err`) if something goes wrong
///
/// # Behavior
///
/// The function performs these operations in sequence:
/// 1. Finds JSON and XML files according to the configuration
/// 2. Processes found files to generate PDFs
///
/// **Processing Mode:**
/// - With `concurrency` feature (default): Files are processed concurrently using a threadpool.
///   The number of concurrent jobs is controlled by `Config::max_jobs` (defaults to system CPU count).
/// - Without `concurrency` feature: Files are processed sequentially in the main thread.
///
/// # Fonts
///
/// Liberation Sans fonts are embedded in the generated PDFs, eliminating the need
/// for font installation on the system viewing the PDFs.
///
/// # Environment Variables
///
/// Various aspects of PDF generation can be controlled through environment variables:
/// - `VEX2PDF_NOVULNS_MSG`: Controls whether to show a message when no vulnerabilities exist
/// - `VEX2PDF_REPORT_TITLE`: Sets a custom title for the report
/// - `VEX2PDF_PDF_META_NAME`: Sets the PDF metadata name
/// - `VEX2PDF_MAX_JOBS`: Controls concurrent processing (requires `concurrency` feature)
///
/// For CLI usage, see `Config::build_from_env_cli()` which parses command-line arguments.
///
/// # Example
///
/// ```
/// use vex2pdf::lib_utils::config::Config;
/// use vex2pdf::run;
/// use std::env;
///
/// // Create config using builder pattern
/// let config = Config::default()
///     .working_path(env::current_dir().unwrap())
///     .output_dir(env::current_dir().unwrap());
///
/// if let Err(e) = vex2pdf::run(config) {
///     eprintln!("Application error: {e}");
/// }
/// ```
pub fn run(config: Config) -> Result<(), Vex2PdfError> {
    let _ = DefaultFilesProcessor::new(config).find_files()?.process();

    Ok(())
}

/// Helper to show OSS License information
pub fn show_full_licenses() {
    let main_license_text = r#"VEX2PDF is licensed under either MIT or Apache License, Version 2.0 at your option.
license text can be found under: https://gitlab.com/jurassicLizard/vex2pdf/-/blob/master/README.md#license"#;

    println!("{main_license_text}");
    println!();
    println!("-----------------------------------------------------------------------------\n");
    println!("This software makes use of Liberation Fonts licensed under SIL as follows : ");
    println!();
    let sil_license_text = include_bytes!("../external/fonts/liberation-fonts/LICENSE");

    println!("{}", String::from_utf8_lossy(sil_license_text));
}

#[cfg(test)]
mod tests {
    use cyclonedx_bom::models::bom::Bom;
    use cyclonedx_bom::models::metadata::Metadata;
    use cyclonedx_bom::models::tool::{Tool, Tools};
    use cyclonedx_bom::models::vulnerability::{Vulnerabilities, Vulnerability};

    use cyclonedx_bom::models::vulnerability_rating::{
        Score, ScoreMethod, Severity, VulnerabilityRating, VulnerabilityRatings,
    };
    use cyclonedx_bom::prelude::{DateTime, NormalizedString, SpecVersion, UrnUuid};
    use std::fs;
    use std::io::BufReader;

    fn create_sample_vex() -> Bom {
        // Create a VEX document following CycloneDX structure

        Bom {
            spec_version: SpecVersion::V1_5,
            version: 1,
            serial_number: Some(
                UrnUuid::new("urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79".to_string())
                    .expect("Unable to create urn:uuid"),
            ),
            metadata: Some(Metadata {
                timestamp: Some(DateTime::now().expect("failed to convert date")),
                tools: Some(Tools::List(vec![Tool {
                    name: Some(NormalizedString::new("my_tool")),
                    ..Tool::default()
                }])),
                ..Metadata::default()
            }),
            vulnerabilities: Some(Vulnerabilities(vec![
                Vulnerability {
                    bom_ref: None,
                    id: None,
                    vulnerability_source: None,
                    description: Some(
                        "Known vulnerability in library that allows unauthorized access"
                            .to_string(),
                    ),
                    detail: Some(
                        "Detailed explanation of the vulnerability and its potential impact."
                            .to_string(),
                    ),
                    recommendation: Some("Upgrade to version 1.2.4 or later".to_string()),
                    workaround: None,
                    proof_of_concept: None,
                    advisories: None,
                    created: None,
                    published: None,
                    updated: None,
                    rejected: None,
                    vulnerability_credits: None,
                    tools: None,
                    vulnerability_analysis: None,
                    vulnerability_targets: None,
                    vulnerability_ratings: Some(VulnerabilityRatings(vec![VulnerabilityRating {
                        score: Some(Score::from(8.1)),
                        severity: Some(Severity::High),
                        score_method: Some(ScoreMethod::CVSSv31),
                        vector: Some(NormalizedString::new(
                            "CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H",
                        )),
                        vulnerability_source: None,
                        justification: None,
                    }])),

                    vulnerability_references: None,
                    cwes: None,
                    properties: None,
                },
                Vulnerability {
                    bom_ref: None,
                    id: None,
                    vulnerability_source: None,
                    description: Some("Component does not use the affected library".to_string()),
                    detail: Some(
                        "Detailed explanation of the vulnerability and its potential impact."
                            .to_string(),
                    ),
                    recommendation: Some("Upgrade to version 1.2.3 or later".to_string()),
                    workaround: None,
                    proof_of_concept: None,
                    advisories: None,
                    created: None,
                    published: None,
                    updated: None,
                    rejected: None,
                    vulnerability_credits: None,
                    tools: None,
                    vulnerability_analysis: None,
                    vulnerability_targets: None,
                    vulnerability_ratings: Some(VulnerabilityRatings(vec![VulnerabilityRating {
                        score: Some(Score::from(6.5)),
                        severity: Some(Severity::High),
                        score_method: Some(ScoreMethod::CVSSv31),
                        vector: Some(NormalizedString::new(
                            "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:L/I:L/A:L",
                        )),
                        vulnerability_source: None,
                        justification: None,
                    }])),

                    vulnerability_references: None,
                    cwes: None,
                    properties: None,
                },
            ])),
            ..Bom::default()
        }
    }

    #[test]
    fn test_vex_serialization() {
        let vex = create_sample_vex();

        // Test serialization
        let mut output = Vec::<u8>::new();
        vex.clone()
            .output_as_json_v1_5(&mut output)
            .expect("failed to read vex object");

        let json_str = String::from_utf8(output).expect("failed to serialize json object");

        println!("Serialized VEX: {}", json_str);

        let parsed_json =
            serde_json::from_str(&json_str).expect("serde failed to read json from string object");
        let deserialization_result = Bom::parse_json_value(parsed_json);

        // Test deserialization
        match deserialization_result {
            Ok(deserialized) => {
                println!("Deserialized CycloneDX: {:?}", deserialized);
                // Verify the round trip works
                assert_eq!(vex.serial_number, deserialized.serial_number);
                assert_eq!(vex.spec_version, deserialized.spec_version);
            }
            Err(err) => {
                panic!("Deserialization failed: {:?}", err);
            }
        }
    }

    #[test]
    fn test_vex_json_file_io() {
        use std::io::Write;

        let vex = create_sample_vex();
        let mut output = Vec::<u8>::new();
        vex.clone()
            .output_as_json_v1_5(&mut output)
            .expect("failed to read vex object");
        let json_str = String::from_utf8(output).expect("failed to serialize json object");

        // Create a temporary file
        let mut temp_file = std::env::temp_dir();
        temp_file.push("test_vex.json");

        // Write the VEX to the file
        let mut file = fs::File::create(&temp_file).expect("Failed to create temp file");
        file.write_all(json_str.as_bytes())
            .expect("Failed to write to temp file");

        // Read it back
        let content_reader =
            BufReader::new(fs::File::open(&temp_file).expect("failed to open file"));
        let loaded_vex: Bom = Bom::parse_from_json(content_reader).expect("Failed to parse JSON");

        // Clean up
        fs::remove_file(&temp_file).expect("Failed to remove temp file");

        // Verify
        assert_eq!(vex.serial_number, loaded_vex.serial_number);
    }

    #[test]
    fn test_vex_xml_file_io() {
        use std::io::Write;

        let vex = create_sample_vex();
        let mut output = Vec::<u8>::new();
        vex.clone()
            .output_as_xml_v1_5(&mut output)
            .expect("failed to read vex object");
        let xml_str = String::from_utf8(output).expect("failed to serialize json object");

        // Create a temporary file
        let mut temp_file = std::env::temp_dir();
        temp_file.push("test_vex.xml");

        // Write the VEX to the file
        let mut file = fs::File::create(&temp_file).expect("Failed to create temp file");
        file.write_all(xml_str.as_bytes())
            .expect("Failed to write to temp file");

        // Read it back
        let content_reader =
            BufReader::new(fs::File::open(&temp_file).expect("failed to open file"));
        let loaded_vex: Bom =
            Bom::parse_from_xml_v1_5(content_reader).expect("Failed to parse JSON");

        // Clean up
        fs::remove_file(&temp_file).expect("Failed to remove temp file");

        // Verify
        assert_eq!(vex.serial_number, loaded_vex.serial_number);
    }

    #[test]
    fn test_generate_sample_file() {
        let vex = create_sample_vex();
        let mut output = Vec::<u8>::new();
        vex.clone()
            .output_as_json_v1_5(&mut output)
            .expect("failed to read vex object");
        let json_str = String::from_utf8(output).expect("failed to serialize json object");

        // Create a sample file in the current directory
        fs::write("sample_vex.json", json_str).expect("Failed to write sample file");

        println!("Sample VEX file created at sample_vex.json");
    }

    #[test]
    fn test_novulns_msg_env_var_handling() {
        use crate::lib_utils::env_vars::EnvVarNames;
        use std::env;

        // Save original env var value
        let original = env::var(EnvVarNames::NoVulnsMsg.as_str()).ok();

        // Test setting and retrieving the env var
        env::remove_var(EnvVarNames::NoVulnsMsg.as_str());
        assert_eq!(
            env::var(EnvVarNames::NoVulnsMsg.as_str()).is_err(),
            true,
            "Env var should not exist initially"
        );

        env::set_var(EnvVarNames::NoVulnsMsg.as_str(), "false");
        assert_eq!(
            env::var(EnvVarNames::NoVulnsMsg.as_str()).unwrap(),
            "false",
            "Env var should be retrievable with correct value"
        );

        // Clean up
        if let Some(val) = original {
            env::set_var(EnvVarNames::NoVulnsMsg.as_str(), val);
        } else {
            env::remove_var(EnvVarNames::NoVulnsMsg.as_str());
        }
    }

    #[test]
    fn test_embedded_fonts_load_correctly() {
        use crate::pdf::font_config::FontsDir;

        FontsDir::build();
    }

    #[cfg(test)]
    mod tests {
        use crate::lib_utils::env_vars::EnvVarNames;
        use std::env;

        #[test]
        fn test_env_var_behavior() {
            // Use a different enum variant for each test section to avoid interference

            // Test is_on when var not set
            {
                let var = EnvVarNames::ProcessXml;
                env::remove_var(var.as_str());
                assert_eq!(
                    var.is_on(),
                    false,
                    "is_on() should return false when var not set"
                );
            }

            // Test is_on with true values
            {
                let var = EnvVarNames::ProcessXml;
                for value in &["true", "True", "TRUE", "yes", "YES", "1", "on", "ON"] {
                    env::set_var(var.as_str(), value);
                    assert_eq!(var.is_on(), true, "is_on() failed for value: {}", value);
                    env::remove_var(var.as_str()); // Clean up after each test
                }
            }

            // Test is_on with false values
            {
                let var = EnvVarNames::ProcessXml;
                for value in &["false", "False", "FALSE", "no", "NO", "0", "off", "OFF"] {
                    env::set_var(var.as_str(), value);
                    assert_eq!(var.is_on(), false, "is_on() failed for value: {}", value);
                    env::remove_var(var.as_str()); // Clean up after each test
                }
            }

            // Test is_on_or_unset when var not set
            {
                let var = EnvVarNames::ProcessXml;
                env::remove_var(var.as_str());
                assert_eq!(
                    var.is_on_or_unset(),
                    true,
                    "is_on_or_unset() should return true when var not set"
                );
            }

            // Test is_on_or_unset with true values
            {
                let var = EnvVarNames::ProcessXml;
                for value in &["true", "True", "TRUE", "yes", "YES", "1", "on", "ON"] {
                    env::set_var(var.as_str(), value);
                    assert_eq!(
                        var.is_on_or_unset(),
                        true,
                        "is_on_or_unset() failed for value: {}",
                        value
                    );
                    env::remove_var(var.as_str()); // Clean up after each test
                }
            }

            // Test is_on_or_unset with false values
            {
                let var = EnvVarNames::ProcessXml;
                for value in &["false", "False", "FALSE", "no", "NO", "0", "off", "OFF"] {
                    env::set_var(var.as_str(), value);
                    assert_eq!(
                        var.is_on_or_unset(),
                        false,
                        "is_on_or_unset() failed for value: {}",
                        value
                    );
                    env::remove_var(var.as_str()); // Clean up after each test
                }
            }
        }

        #[test]
        fn test_get_value() {
            use std::env;

            // Test with an environment variable that doesn't exist
            {
                let var = EnvVarNames::ReportTitle;
                env::remove_var(var.as_str());
                assert_eq!(
                    var.get_value(),
                    None,
                    "Should return None for non-existent env var"
                );
            }

            // Test with an environment variable that exists
            {
                let var = EnvVarNames::PdfName;
                let test_value = "Test PDF Name";
                env::set_var(var.as_str(), test_value);
                assert_eq!(
                    var.get_value(),
                    Some(test_value.to_string()),
                    "Should return the value of the env var"
                );
                env::remove_var(var.as_str()); // Clean up
            }

            // Test with an empty string value
            {
                let var = EnvVarNames::ReportTitle;
                env::set_var(var.as_str(), "");
                assert_eq!(
                    var.get_value(),
                    Some("".to_string()),
                    "Should return an empty string for empty env var"
                );
                env::remove_var(var.as_str()); // Clean up
            }
        }
    }
}