cargo_pants/
lib.rs

1// Copyright 2019 Glenn Mohre, Sonatype.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#![allow(dead_code)]
15
16use serde::{Deserialize, Serialize};
17use std::collections::HashSet;
18use std::fs::File;
19use std::io::BufReader;
20use std::path::PathBuf;
21use terminal_size::{terminal_size, Height, Width};
22use tracing::trace;
23
24pub mod client;
25pub mod common;
26pub mod coordinate;
27pub mod cyclonedx;
28pub mod error;
29pub mod iq;
30pub mod package;
31pub mod parse;
32pub mod vulnerability;
33
34pub use crate::{
35    client::*, common::*, coordinate::*, cyclonedx::CycloneDXGenerator, error::*, iq::IQClient,
36    package::*, parse::*, vulnerability::*,
37};
38
39pub fn calculate_term_width() -> u16 {
40    return match terminal_size() {
41        Some((Width(w), Height(_h))) => w,
42        None => 80,
43    };
44}
45
46#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48pub struct FilterList {
49    pub ignore: Vec<Ignore>,
50}
51
52#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
53#[serde(rename_all = "camelCase")]
54pub struct Ignore {
55    pub id: String,
56    pub reason: Option<String>,
57}
58
59pub fn filter_vulnerabilities(packages: &mut Vec<Coordinate>, exclude_vuln_file_path: PathBuf) {
60    match File::open(exclude_vuln_file_path) {
61        Ok(file) => {
62            let exclude_reader = BufReader::new(file);
63            let filter_list_json: FilterList =
64                serde_json::from_reader(exclude_reader).expect("JSON was not well formatted");
65
66            let ignored_ids: HashSet<String> = filter_list_json
67                .ignore
68                .into_iter()
69                .map(|filter| filter.id)
70                .collect();
71
72            packages.iter_mut().for_each(|p| {
73                if p.has_vulnerabilities() {
74                    p.vulnerabilities.retain(|v| !ignored_ids.contains(&v.id))
75                }
76            });
77        }
78        Err(err) => {
79            trace!("No file found at location provided: {}", err.to_string())
80        }
81    }
82}