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
//! # cargo-sbom
//!
//! This crate provides a command line tool to create software bill of materials (SBOM) for Cargo / Rust workspaces. It supports both SPDX and CycloneDX outputs.
//!
//! The latest [documentation can be found here](https://docs.rs/cargo_sbom).
//!
//! SBOM or Software Bill of Materials is an industry standard term used to trace and maintain the supply chain security of software.
//!
//! ## Installation
//!
//! `cargo-sbom` may be installed via `cargo`
//!
//! ```shell
//! cargo install cargo-sbom
//! ```
//!
//! via [cargo-binstall](https://github.com/cargo-bins/cargo-binstall)
//!
//! ```shell
//! cargo binstall cargo-sbom
//! ```
//!
//! or downloaded directly from Github Releases
//!
//! ```shell
//! # make sure to adjust the target and version (you may also want to pin to a specific version)
//! curl -sSL https://github.com/psastras/sbom-rs/releases/download/cargo-sbom-latest/cargo-sbom-x86_64-unknown-linux-gnu -o cargo-sbom
//! ```
//!
//! ## Usage
//!
//! For most cases, simply `cd` into a cargo workspace and run `cargo sbom`.
//!
//! ### `--help`
//!
//! ```
//! Create software bill of materials (SBOM) for Rust
//!
//! Usage: cargo sbom [OPTIONS]
//!
//! Options:
//! --cargo-package <CARGO_PACKAGE>
//! The specific package (in a Cargo workspace) to generate an SBOM for. If not specified this is all packages in the workspace.
//! --output-format <OUTPUT_FORMAT>
//! The SBOM output format. [default: spdx_json_2_3] [possible values: spdx_json_2_3, cyclone_dx_json_1_4, cyclone_dx_json_1_5, cyclone_dx_json_1_6]
//! --project-directory <PROJECT_DIRECTORY>
//! The directory to the Cargo project. [default: .]
//! -h, --help
//! Print help
//! -V, --version
//! Print version
//! ```
//!
//! ## Examples
//!
//! ### Create a SPDX SBOM for a Cargo project
//!
//! In a shell:
//!
//! ```shell
//! $ cargo sbom
//! {
//! "SPDXID": "SPDXRef-DOCUMENT",
//! "creationInfo": {
//! "created": "2023-07-04T12:38:15.211Z",
//! "creators": [
//! "Tool: cargo-sbom-v0.10.0"
//! ]
//! },
//! "dataLicense": "CC0-1.0",
//! "documentNamespace": "https://docs.rs/cargo_sbom/spdxdocs/cargo-sbom-0.10.0-9cae390a-4b46-457c-95b9-e59a5e62b57d",
//! "files": [
//! {
//! <rest of output omitted>
//! ```
//!
//! ### Create a CycloneDx SBOM in Github Actions
//!
//! In a Github Actions workflow:
//!
//! ```yaml
//! jobs:
//! sbom:
//! runs-on: ubuntu-latest
//! steps:
//! - uses: actions/checkout@v3
//! - uses: psastras/sbom-rs/actions/install-cargo-sbom@cargo-sbom-latest
//! - name: Run cargo-sbom
//! run: cargo-sbom --output-format=cyclone_dx_json_1_4
//! ```
//!
//! ### Create a CycloneDx 1.6 SBOM in Github Actions
//!
//! ```yaml
//! runs-on: ubuntu-latest
//! steps:
//! - uses: actions/checkout@v3
//! - uses: psastras/sbom-rs/actions/install-cargo-sbom@cargo-sbom-latest
//! - name: Run cargo-sbom
//! run: cargo-sbom --output-format=cyclone_dx_json_1_6
//! ```
//!
//! ### Create a CycloneDx 1.5 SBOM in Github Actions
//!
//! ```yaml
//! runs-on: ubuntu-latest
//! steps:
//! - uses: actions/checkout@v3
//! - uses: psastras/sbom-rs/actions/install-cargo-sbom@cargo-sbom-latest
//! - name: Run cargo-sbom
//! run: cargo-sbom --output-format=cyclone_dx_json_1_5
//! ```
//!
//! ### Check Dependencies against the Open Source Vulnerability Database (OSV)
//!
//! Assumming `osv-scanner` is installed (see [https://osv.dev/](https://osv.dev/))
//!
//! ```shell
//! $ cargo-sbom > sbom.spdx.json
//! $ osv-scanner --sbom=sbom.spdx.json
//! Scanned sbom.json as SPDX SBOM and found 91 packages
//! ╭─────────────────────────────────────┬──────┬───────────┬─────────┬─────────┬───────────╮
//! │ OSV URL │ CVSS │ ECOSYSTEM │ PACKAGE │ VERSION │ SOURCE │
//! ├─────────────────────────────────────┼──────┼───────────┼─────────┼─────────┼───────────┤
//! │ https://osv.dev/GHSA-wcg3-cvx6-7396 │ 6.2, │ crates.io │ time │ 0.1.45 │ sbom.json │
//! │ https://osv.dev/RUSTSEC-2020-0071 │ 6.2 │ │ │ │ │
//! ╰─────────────────────────────────────┴──────┴───────────┴─────────┴─────────┴───────────╯
//! ```
//!
//! More examples can be found by browsing the [examples section](https://github.com/psastras/sbom-rs/tree/main/examples).
//!
//! ## Supported SBOM Features
//!
//! ### SPDX
//!
//! | SPDX Field | Source |
//! |----------------------------------|----------------------------------------------------------------------------------------------|
//! | SPDXID | Set to "SPDXRef-Document" |
//! | creationInfo.created | Set as the current time |
//! | creationInfo.creators | Set to "Tool: cargo-sbom-v(tool version) |
//! | dataLicense | Set to "CC0-1.0" |
//! | documentNamespace | set to "https://spdx.org/spdxdocs/(crate-name)-(uuidv4)" |
//! | files | parsed from Cargo.toml target names |
//! | name | Set to the project folder name |
//! | packages | Set to dependencies parsed from cargo-metadata |
//! | packages.SPDXID | Written as SPDXRef-Package-(crate name)-(crate version) |
//! | packages.description | Read from Cargo.toml's "description" field |
//! | packages.downloadLocation | Read from `cargo metadata` (usually "registry+https://github.com/rust-lang/crates.io-index") |
//! | packages.externalRefs | If packages.downloadLocation is crates.io, written as a package url formatted string |
//! | packages.homepage | Read from Cargo.toml's "homepage" field |
//! | packages.licenseConcluded | Parsed into a SPDX compliant license identifier from Cargo.toml's "license" field |
//! | packages.licenseDeclared | Read from Cargo.toml's "license" field |
//! | packages.name | Read from Cargo.toml's "name" field |
//! | relationships | Set to dependency relationships parsed from cargo-metadata |
//! | relationships.relationshipType | Set to dependency relationship parsed from cargo-metadata |
//! | relationships.spdxElementId | Set to dependency relationship source parsed from cargo-metadata |
//! | relationships.relatedSpdxElement | Set to dependency relationship target parsed from cargo-metadata |
//!
//!
//! ### CycloneDx
//!
//! | CycloneDx Field | Source |
//! |-------------------------------|-----------------------------------------------------------------------------------|
//! | bomFormat | Set to "CycloneDX" |
//! | serialNumber | Set to "urn:uuid:(uuidv4)" |
//! | specVersion | Set to 1.4 |
//! | version | Set to 1 |
//! | metadata | |
//! | metadata.component | parsed from the root workspace |
//! | metadata.component.name | Set to the root workspace folder name |
//! | metadata.component.type | Set to "application" |
//! | metadata.component.components | Set to each of the cargo workspace package components |
//! | components | Set to the componennts parse from cargo-metadata |
//! | components.author | Read from Cargo.toml's "authors" field |
//! | components.bom-ref | Set to "CycloneDxRef-Component-(crate-name)-(crate-version)" |
//! | components.description | Read from Cargo.toml's "description" field |
//! | copmonents.licenses | Parsed into a SPDX compliant license identifier from Cargo.toml's "license" field |
//! | components.name | Read from Cargo.toml's "name" field |
//! | components.purl | If the download location is crates.io, written as a package url formatted string |
//! | components.type | Read from cargo-metadata crate type |
//! | components.version | Read from Cargo.toml's "version" field |
//! | dependencies | Set to dependency relationships parsed from cargo-metadata |
//! | dependencies.ref | Set to source dependency reference id string |
//! | dependencies.dependsOnn | Set to target dependencies reference id strings |
use ;
use ;
use ;
use Write;
use ;