-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.rs
189 lines (172 loc) · 6.01 KB
/
main.rs
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
/*
* This file is part of CycloneDX Rust Cargo.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) OWASP Foundation. All Rights Reserved.
*/
/**
* A special acknowledgement Ossi Herrala from SensorFu for providing a
* starting point in which to develop this plugin. The original project
* this plugin was derived from is sensorfu/cargo-bom v0.3.1 (MIT licensed)
* https://github.com/sensorfu/cargo-bom
*
* MIT License
*
* Copyright (c) 2017-2019 SensorFu Oy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use cyclonedx_bom::generator::{Generator, SbomGenerator};
use cyclonedx_bom::traits::ToXml;
use std::{
fs::File,
io::{self, LineWriter},
path, str,
};
use anyhow::Result;
use cargo::util::Config;
use structopt::StructOpt;
use xml_writer::XmlWriter;
mod format;
use format::Format;
#[derive(StructOpt)]
#[structopt(bin_name = "cargo")]
enum Opts {
#[structopt(name = "cyclonedx")]
/// Creates a CycloneDX Software Bill-of-Materials (SBOM) for Rust project
Bom(Args),
}
#[derive(StructOpt)]
struct Args {
/// List all dependencies instead of only top level ones
#[structopt(long = "all", short = "a")]
all: bool,
/// Directory for all generated artifacts
#[structopt(long = "target-dir", value_name = "DIRECTORY", parse(from_os_str))]
target_dir: Option<path::PathBuf>,
#[structopt(long = "manifest-path", value_name = "PATH", parse(from_os_str))]
/// Path to Cargo.toml
manifest_path: Option<path::PathBuf>,
/// Output BOM format: json, xml
#[structopt(long = "format", short = "f", value_name = "FORMAT", default_value)]
format: Format,
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
/// Use verbose output (-vv very verbose/build.rs output)
verbose: u32,
#[structopt(long = "quiet", short = "q")]
/// No output printed to stdout other than the tree
quiet: Option<bool>,
#[structopt(long = "color", value_name = "WHEN")]
/// Coloring: auto, always, never
color: Option<String>,
#[structopt(long = "frozen")]
/// Require Cargo.lock and cache are up to date
frozen: bool,
#[structopt(long = "locked")]
/// Require Cargo.lock is up to date
locked: bool,
#[structopt(long = "offline")]
/// Run without accessing the network
offline: bool,
#[structopt(short = "Z", value_name = "FLAG")]
/// Unstable (nightly-only) flags to Cargo
unstable_flags: Vec<String>,
#[structopt(long = "config", value_name = "KEY=VALUE")]
/// Override a configuration value
config_args: Vec<String>,
}
fn main() -> Result<(), Error> {
let mut config = Config::default()?;
let Opts::Bom(args) = Opts::from_args();
real_main(&mut config, args)
}
fn real_main(config: &mut Config, args: Args) -> Result<(), Error> {
config.configure(
args.verbose,
args.quiet.unwrap_or(false),
args.color.as_deref(),
args.frozen,
args.locked,
args.offline,
&args.target_dir,
&args.unstable_flags,
&args.config_args,
)?;
let manifest = args
.manifest_path
.unwrap_or_else(|| config.cwd().join("Cargo.toml"));
let generator = SbomGenerator {
all: Some(args.all),
};
let bom = generator.create_sbom(manifest)?;
match args.format {
Format::Json => {
serde_json::to_writer_pretty(File::create("bom.json")?, &bom)
.map_err(anyhow::Error::from)?;
}
Format::Xml => {
let file = File::create("bom.xml")?;
let file = LineWriter::new(file);
let mut xml = XmlWriter::new(file);
bom.to_xml(&mut xml)?;
xml.close()?;
xml.flush()?;
let _actual = xml.into_inner();
}
}
Ok(())
}
#[derive(Debug)]
struct Error;
impl From<anyhow::Error> for Error {
fn from(err: anyhow::Error) -> Self {
cargo_exit(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
cargo_exit(anyhow::Error::new(err))
}
}
impl From<cyclonedx_bom::generator::GeneratorError> for Error {
fn from(err: cyclonedx_bom::generator::GeneratorError) -> Self {
cargo_exit(anyhow::Error::new(err))
}
}
impl From<cargo_metadata::Error> for Error {
fn from(err: cargo_metadata::Error) -> Self {
cargo_exit(anyhow::Error::new(err))
}
}
fn cargo_exit<E: Into<cargo::CliError>>(err: E) -> ! {
let mut shell = cargo::core::shell::Shell::new();
cargo::exit_with_error(err.into(), &mut shell)
}