provenant-cli 1.0.0

Fast Rust scanner for licenses, copyrights, package metadata, SBOMs, and provenance data.
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
// SPDX-FileCopyrightText: nexB Inc. and others
// ScanCode is a trademark of nexB Inc.
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0
// Derived from ScanCode Toolkit (Apache-2.0); modified. See NOTICE.

use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::Path;

use anyhow::{Result, anyhow};
use serde::Deserialize;

use crate::license_detection::expression::{LicenseExpression, parse_expression};
use crate::models::{ComplianceAlert, FileInfo, LicensePolicyEntry, Package, TopLevelDependency};

#[derive(Debug, Deserialize)]
struct LicensePolicyFile {
    license_policies: Vec<LicensePolicyEntry>,
}

enum PolicyFileStatus {
    Ready(Vec<LicensePolicyEntry>),
    SoftError(String),
}

pub(crate) fn apply_license_policy_from_file(
    files: &mut [FileInfo],
    policy_path: &Path,
) -> Result<Vec<String>> {
    match load_license_policy(policy_path)? {
        PolicyFileStatus::Ready(policies) => {
            apply_license_policy(files, &policies)?;
            Ok(Vec::new())
        }
        PolicyFileStatus::SoftError(error) => {
            for file in files {
                file.license_policy = Some(vec![]);
            }
            Ok(vec![error])
        }
    }
}

fn load_license_policy(policy_path: &Path) -> Result<PolicyFileStatus> {
    let policy_text = fs::read_to_string(policy_path).map_err(|err| {
        anyhow!(
            "Failed to read license policy file {:?}: {err}",
            policy_path
        )
    })?;
    let policy_file: LicensePolicyFile = yaml_serde::from_str(&policy_text).map_err(|err| {
        anyhow!(
            "Failed to parse license policy file {:?}: {err}",
            policy_path
        )
    })?;

    if policy_file.license_policies.is_empty() {
        return Ok(PolicyFileStatus::SoftError(format!(
            "License policy file {:?} is empty",
            policy_path
        )));
    }

    let mut seen = BTreeSet::new();
    for policy in &policy_file.license_policies {
        if !seen.insert(policy.license_key.clone()) {
            return Ok(PolicyFileStatus::SoftError(format!(
                "License policy file {:?} contains duplicate license key {:?}",
                policy_path, policy.license_key
            )));
        }
    }

    Ok(PolicyFileStatus::Ready(policy_file.license_policies))
}

fn apply_license_policy(files: &mut [FileInfo], policies: &[LicensePolicyEntry]) -> Result<()> {
    for file in files {
        if file.file_type != crate::models::FileType::File {
            file.license_policy = Some(vec![]);
            continue;
        }
        let license_keys = file_license_keys(file)?;
        let mut matched_policies: Vec<_> = policies
            .iter()
            .filter(|policy| license_keys.contains(&policy.license_key))
            .cloned()
            .collect();
        matched_policies.sort_by(|left, right| left.license_key.cmp(&right.license_key));
        file.license_policy = Some(matched_policies);
    }

    Ok(())
}

fn file_license_keys(file: &FileInfo) -> Result<BTreeSet<String>> {
    let mut keys = BTreeSet::new();
    for detection in &file.license_detections {
        collect_license_keys(&detection.license_expression, &mut keys)?;
    }
    Ok(keys)
}

fn collect_license_keys(expression: &str, keys: &mut BTreeSet<String>) -> Result<()> {
    if expression.trim().is_empty() {
        return Ok(());
    }

    let parsed = parse_expression(expression)
        .map_err(|err| anyhow!("Failed to parse license expression {:?}: {err}", expression))?;
    collect_expression_keys(&parsed, keys);
    Ok(())
}

fn collect_expression_keys(expression: &LicenseExpression, keys: &mut BTreeSet<String>) {
    match expression {
        LicenseExpression::License(key) | LicenseExpression::LicenseRef(key) => {
            keys.insert(key.clone());
        }
        LicenseExpression::And { left, right }
        | LicenseExpression::Or { left, right }
        | LicenseExpression::With { left, right } => {
            collect_expression_keys(left, keys);
            collect_expression_keys(right, keys);
        }
    }
}

/// Count top-level packages and dependencies whose **declared** license matches a
/// policy entry with a `compliance_alert` at or above `threshold`. This lets the
/// `--fail-on` gate act on package/dependency licenses (SCA-style), not just
/// file-level detections. Returns 0 if the policy carries no severities or cannot
/// be loaded (file findings are gated separately, and an unevaluable policy under
/// `--fail-on` already fails the run before this point).
pub(crate) fn count_declared_license_policy_violations(
    policy_path: &Path,
    packages: &[Package],
    dependencies: &[TopLevelDependency],
    threshold: ComplianceAlert,
) -> Result<usize> {
    // Fail closed: a read/parse failure here (e.g. the policy file was replaced or
    // removed mid-scan) propagates as an error rather than being treated as zero
    // violations. A soft error (empty/duplicate keys) under `--fail-on` already
    // failed the run in the pipeline, so treat it as no declared violations here.
    let policies = match load_license_policy(policy_path)? {
        PolicyFileStatus::Ready(policies) => policies,
        PolicyFileStatus::SoftError(_) => return Ok(0),
    };

    let mut severity: BTreeMap<String, ComplianceAlert> = BTreeMap::new();
    for policy in &policies {
        if let Some(alert) = policy.compliance_alert {
            severity
                .entry(policy.license_key.clone())
                .and_modify(|current| {
                    if alert > *current {
                        *current = alert;
                    }
                })
                .or_insert(alert);
        }
    }
    if severity.is_empty() {
        return Ok(0);
    }

    let violates = |expression: &Option<String>| -> bool {
        expression
            .as_deref()
            .is_some_and(|expr| declared_expression_violates(expr, &severity, threshold))
    };

    let package_hits = packages
        .iter()
        .filter(|package| violates(&package.declared_license_expression))
        .count();
    let dependency_hits = dependencies
        .iter()
        .filter(|dependency| {
            dependency
                .resolved_package
                .as_ref()
                .is_some_and(|resolved| violates(&resolved.declared_license_expression))
        })
        .count();

    Ok(package_hits + dependency_hits)
}

/// True when any license key in `expression` maps to a severity at or above `threshold`.
fn declared_expression_violates(
    expression: &str,
    severity: &BTreeMap<String, ComplianceAlert>,
    threshold: ComplianceAlert,
) -> bool {
    let mut keys = BTreeSet::new();
    if collect_license_keys(expression, &mut keys).is_err() {
        return false;
    }
    keys.iter()
        .any(|key| severity.get(key).is_some_and(|alert| *alert >= threshold))
}

#[cfg(test)]
mod tests {
    use super::{apply_license_policy_from_file, declared_expression_violates};
    use crate::models::{ComplianceAlert, FileInfo, FileType, LicenseDetection};
    use std::collections::BTreeMap;

    #[test]
    fn declared_expression_violates_respects_keys_and_threshold() {
        let severity: BTreeMap<String, ComplianceAlert> = [
            ("gpl-3.0".to_string(), ComplianceAlert::Error),
            ("lgpl-2.1".to_string(), ComplianceAlert::Warning),
        ]
        .into_iter()
        .collect();

        // Error-severity key trips both thresholds.
        assert!(declared_expression_violates(
            "gpl-3.0",
            &severity,
            ComplianceAlert::Error
        ));
        // Present in a compound expression.
        assert!(declared_expression_violates(
            "mit OR gpl-3.0",
            &severity,
            ComplianceAlert::Error
        ));
        // Warning key trips `warning` but not `error`.
        assert!(declared_expression_violates(
            "lgpl-2.1",
            &severity,
            ComplianceAlert::Warning
        ));
        assert!(!declared_expression_violates(
            "lgpl-2.1",
            &severity,
            ComplianceAlert::Error
        ));
        // Unlisted / approved license never violates.
        assert!(!declared_expression_violates(
            "mit",
            &severity,
            ComplianceAlert::Warning
        ));
    }

    #[test]
    fn apply_license_policy_populates_matching_file_entries() {
        let temp = tempfile::tempdir().expect("temp dir");
        let policy_path = temp.path().join("policy.yml");
        std::fs::write(
            &policy_path,
            "license_policies:\n  - license_key: mit\n    label: Approved\n    color_code: '#00ff00'\n    icon: ok\n",
        )
        .expect("policy written");

        let mut files = vec![FileInfo::new(
            "LICENSE".to_string(),
            "LICENSE".to_string(),
            String::new(),
            "LICENSE".to_string(),
            FileType::File,
            None,
            None,
            0,
            None,
            None,
            None,
            None,
            None,
            vec![],
            Some("mit".to_string()),
            vec![LicenseDetection {
                license_expression: "mit".to_string(),
                license_expression_spdx: "MIT".to_string(),
                matches: vec![],
                detection_log: vec![],
                identifier: String::new(),
            }],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
        )];

        apply_license_policy_from_file(&mut files, &policy_path)
            .expect("policy application succeeds");

        let entries = files[0]
            .license_policy
            .as_ref()
            .expect("license policy present");
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].license_key, "mit");
        assert_eq!(entries[0].label, "Approved");
    }

    #[test]
    fn apply_license_policy_keeps_scan_running_on_duplicate_license_keys() {
        let temp = tempfile::tempdir().expect("temp dir");
        let policy_path = temp.path().join("policy.yml");
        std::fs::write(
            &policy_path,
            "license_policies:\n  - license_key: mit\n    label: Approved\n    color_code: '#00ff00'\n    icon: ok\n  - license_key: mit\n    label: Duplicate\n    color_code: '#ff0000'\n    icon: stop\n",
        )
        .expect("policy written");

        let mut files = vec![FileInfo::new(
            "LICENSE".to_string(),
            "LICENSE".to_string(),
            String::new(),
            "LICENSE".to_string(),
            FileType::File,
            None,
            None,
            0,
            None,
            None,
            None,
            None,
            None,
            vec![],
            Some("mit".to_string()),
            vec![LicenseDetection {
                license_expression: "mit".to_string(),
                license_expression_spdx: "MIT".to_string(),
                matches: vec![],
                detection_log: vec![],
                identifier: String::new(),
            }],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
        )];

        let errors = apply_license_policy_from_file(&mut files, &policy_path)
            .expect("duplicate policy should not abort scan");

        assert_eq!(files[0].license_policy, Some(vec![]));
        assert!(files[0].scan_diagnostics.is_empty());
        assert!(
            errors
                .iter()
                .any(|error| error.contains("duplicate license key"))
        );
    }

    #[test]
    fn apply_license_policy_sets_empty_entries_for_directory_resources() {
        let temp = tempfile::tempdir().expect("temp dir");
        let policy_path = temp.path().join("policy.yml");
        std::fs::write(
            &policy_path,
            "license_policies:\n  - license_key: mit\n    label: Approved\n    color_code: '#00ff00'\n    icon: ok\n",
        )
        .expect("policy written");

        let mut files = vec![FileInfo::new(
            "src".to_string(),
            "src".to_string(),
            String::new(),
            "src".to_string(),
            FileType::Directory,
            None,
            None,
            0,
            None,
            None,
            None,
            None,
            None,
            vec![],
            None,
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
        )];

        apply_license_policy_from_file(&mut files, &policy_path)
            .expect("policy application succeeds");

        assert_eq!(files[0].license_policy, Some(vec![]));
    }

    #[test]
    fn apply_license_policy_sets_empty_entries_for_directories_on_soft_error() {
        let temp = tempfile::tempdir().expect("temp dir");
        let policy_path = temp.path().join("policy.yml");
        std::fs::write(
            &policy_path,
            "license_policies:\n  - license_key: mit\n    label: Approved\n    color_code: '#00ff00'\n    icon: ok\n  - license_key: mit\n    label: Duplicate\n    color_code: '#ff0000'\n    icon: stop\n",
        )
        .expect("policy written");

        let mut files = vec![FileInfo::new(
            "src".to_string(),
            "src".to_string(),
            String::new(),
            "src".to_string(),
            FileType::Directory,
            None,
            None,
            0,
            None,
            None,
            None,
            None,
            None,
            vec![],
            None,
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
        )];

        let errors = apply_license_policy_from_file(&mut files, &policy_path)
            .expect("duplicate policy should not abort scan");

        assert_eq!(files[0].license_policy, Some(vec![]));
        assert!(
            errors
                .iter()
                .any(|error| error.contains("duplicate license key"))
        );
    }
}