Debian Codegraph @

docs/source/examples.rst

raw
1 Examples
2 ========
3
4 The following examples are based on common use-cases.
5
6 Generate
7 ~~~~~~~~
8
9 Generation happens fully offline and can run against an arbitrary root directory.
10
11 Local System
12 ^^^^^^^^^^^^
13
14 Generate a CycloneDX SBOM of the current system.
15
16 .. code-block:: bash
17
18 debsbom --progress generate -t cdx -o sbom
19 # output in sbom.cdx.json
20
21 Container Rootfs
22 ^^^^^^^^^^^^^^^^
23
24 Create the SBOM of a Debian-based container (rootless).
25 The ``debsbom`` tool hereby is used from the host (e.g. from a Python venv).
26
27 Using `Podman <https://podman.io/>`__:
28
29 .. code-block:: bash
30
31 CRT=$(podman create debian:trixie)
32 CHROOT=$(podman unshare podman mount $CRT)
33 podman unshare debsbom generate -t spdx --root $CHROOT
34
35 In CI-like environments, it is often more practical to use `skopeo <https://github.com/podman-container-tools/skopeo>`__
36 and `umoci <https://github.com/opencontainers/umoci>`__ instead:
37
38 .. code-block:: bash
39
40 skopeo copy docker://debian:trixie oci:debian:trixie
41 umoci raw unpack --rootless --image debian:trixie ./rootfs
42 debsbom generate -t spdx --root ./rootfs
43
44 You can also scan a container export directly, without unpacking the complete root filesystem:
45
46 .. code-block:: bash
47
48 CRT=$(podman create debian:trixie)
49 podman export "$CRT" -o rootfs.tar
50 debsbom generate -t spdx --root rootfs.tar
51
52 To avoid the temporary tar file as well, stream the uncompressed export through stdin:
53
54 .. code-block:: bash
55
56 podman export "$CRT" | debsbom generate -t spdx --root -
57
58 Compressed tar files use their filename extension to select the decompressor. The supported
59 extensions are ``.bz2``, ``.gz``, ``.xz``, ``.zst``, and ``.lz4``. Compression cannot be
60 selected by filename when reading from stdin, so ``podman export`` should be piped directly.
61
62 From Package List
63 ^^^^^^^^^^^^^^^^^
64
65 Create the SBOM from a package list. The so provided packages will still be enriched with any available data from the apt cache.
66
67 .. code-block:: bash
68
69 echo "htop 3.4.1-5 amd64" | debsbom generate --from-pkglist
70 # or in isar manifest format
71 echo "json-c|0.16-2|libjson-c5:amd64|0.16-2" | debsbom generate --from-pkglist
72 # or with PURLs
73 echo "pkg:deb/debian/[email protected]?arch=amd64" | debsbom generate --from-pkglist
74
75 It further is possible to inject a dpkg status file via stdin (e.g. if you only have that file).
76 The data is then also resolved from the apt-cache (if available), but this usually only makes sense if you don't have a
77 chroot and want to create the sbom just from the data in the file.
78
79 .. code-block:: bash
80
81 cat path/to/dpkg/status | debsbom generate --from-pkglist
82
83 Please mind that only packages that are passed via the package list are included in the resulting SBOM. This leaves out any dependencies
84 that you might expect to see for a package. If you want to have these included you also need to explicitly pass them in the package list.
85
86 Download
87 ~~~~~~~~
88
89 Lookup all packages on the ``snapshot.debian.org`` mirror and download all binary and source artifacts referenced in an SBOM:
90
91 .. code-block:: bash
92
93 debsbom --progress \
94 download --outdir downloads --sources --binaries sbom.cdx.json
95 find downloads -mindepth 1 -maxdepth 1
96 # downloads/.cache <- debsbom metadata to map packages to artifacts
97 # downloads/sources <- files related to source packages (e.g. .dsc, .orig.tar)
98 # downloads/binaries <- .deb files
99
100 It is also possible to download multiple packages by name, version and architecture:
101
102 .. code-block:: bash
103
104 cat <<EOF | debsbom download --binaries --sources
105 cpp 4:12.2.0-3 amd64
106 guestfs-tools 1.52.3-1 source
107 EOF
108
109 Alternatively, the download can be executed from the container image:
110
111 .. code-block:: bash
112
113 echo "guestfs-tools 1.52.3-1 source" | \
114 docker run -v$(pwd)/downloads:/mnt/downloads -i ghcr.io/siemens/debsbom:latest \
115 debsbom download --outdir /mnt/downloads --sources
116
117 Merge Source Packages
118 ~~~~~~~~~~~~~~~~~~~~~
119
120 Debian source packages consist of a ``.dsc`` file along with one or more related artifacts.
121 The :doc:`/commands/source-merge` takes care of merging all referenced artifacts of a debian source package into a single archive.
122 All referenced files have to be downloaded upfront, by using the :doc:`/commands/download`.
123
124 .. note::
125 Internally, the ``dpkg-source`` command from the ``dpkg-dev`` package is used to perform the merge.
126
127 The following example merges all debian source packages referenced in the ``sbom.cdx.json``, applies the debian patches and compresses the new artifacts with ZStandard.
128
129 .. code-block:: bash
130
131 debsbom --progress \
132 source-merge \
133 --compress zstd \
134 --apply-patches \
135 sbom.cdx.json
136
137 Repack Artifacts
138 ~~~~~~~~~~~~~~~~
139
140 The :doc:`/commands/repack` is similar to the :doc:`/commands/source-merge` but performs additional steps to re-layout the downloaded artifacts and recreate the SBOM.
141 The following example generates a ``standard-bom-package`` source distribution archive.
142
143 .. code-block:: bash
144
145 debsbom --progress repack \
146 --dldir downloads \
147 --outdir source-archive \
148 --compress zstd \
149 --apply-patches \
150 --validate \
151 sbom.cdx.json sbom.packed.cdx.json
152
153 It further is possible to only repack (and update in the SBOM) a subset of packages.
154 For that, provide both an SBOM, as well as a set of "to-be-processed" packages via stdin.
155
156 .. code-block:: bash
157
158 echo "bash 5.2.37-2 source" | debsbom -v repack sbom-in.json sbom-out.json
159
160 Compare SBOMs
161 ~~~~~~~~~~~~~
162
163 The SBOMs produced by ``debsbom`` can be further processed with existing tools – for example, the `CycloneDX CLI <https://github.com/CycloneDX/cyclonedx-cli>`_.
164 Comparing two SBOMs directly is outside the scope of ``debsbom``, but you can determine which components have changed by using a short snippet such as the one shown below.
165
166 Locate Changes
167 ^^^^^^^^^^^^^^
168
169 .. code-block:: bash
170
171 cyclonedx-cli diff --component-versions --output-format json \
172 sbom.old.cdx.json sbom.cdx.json | \
173 jq -r '.componentVersions[] | select(.added!=[] or .removed!=[]) | {"added": .added[0].purl, "removed": .removed[0].purl}'
174 # {"added", "purl-a-1.1", "removed": "purl-a-1.0"}
175 # {...}
176
177 A similar output can be generated by just using ``jq`` and ``diff``:
178
179 .. code-block:: bash
180
181 diff --color \
182 <(jq -r --sort-keys '.components[].purl' sbom.old.cdx.json) \
183 <(jq -r --sort-keys '.components[].purl' sbom.cdx.json)
184
185 Identify new Components
186 ^^^^^^^^^^^^^^^^^^^^^^^
187
188 Consider you only want to know the changed and added components, e.g. for license clearing.
189
190 .. code-block:: bash
191
192 PURLS=$( \
193 diff -U0 \
194 <(jq -r --sort-keys '.components[].purl' sbom.old.cdx.json) \
195 <(jq -r --sort-keys '.components[].purl' sbom.cdx.json) \
196 | grep ^+pkg | sed 's/^+//' \
197 )
198
199 The PURLs can be used as input to debsbom to download / merge components:
200
201 .. code-block:: bash
202
203 echo "$PURLS" | debsbom download --sources --binaries
204
205 Once downloaded, it is possible to merge the source packages:
206
207 .. code-block:: bash
208
209 echo "$PURLS" | debsbom source-merge --apply-patches
210
211 And the same list of packages can be repacked:
212
213 .. code-block:: bash
214
215 echo "$PURLS" | debsbom repack \
216 --apply-patches
217 sbom.cdx.json \
218 sbom.cdx.repacked.json
219
220 Delta SBOMs
221 ~~~~~~~~~~~
222
223 The :doc:`/commands/delta` compares a base (reference) SBOM with a target (new) SBOM and produces
224 a new SBOM containing only the components present in the target. The typical use-case is identifying
225 newly added or changed components between two builds or releases.
226
227 Use ``debsbom delta`` when you only want to see changed or added components, e.g., to generate an
228 SBOM for license clearance.
229
230 .. code-block:: bash
231
232 debsbom delta sbom.old.cdx.json sbom.cdx.json extras.cdx.json
233
234 You can also pass SBOMs via stdin, but you also have to pass the SBOM type in this case:
235
236 .. code-block:: bash
237
238 cat sbom.old.spdx.json sbom.spdx.json | debsbom delta -t spdx - - -o -
239
240 Export as Graph
241 ~~~~~~~~~~~~~~~
242
243 The :doc:`/commands/export` allows one to convert the SBOM into various graph representations.
244 These can be used as input to graph visualization and analysis tooling (like Gephi).
245
246 .. note::
247 We recommend to use the SPDX format as input, as this describes inter package relations
248 more precisely.
249
250 Convert the SPDX SBOM to GraphML:
251
252 .. code-block:: bash
253
254 debsbom export sbom.spdx.json sbom-graph.graphml
255
256 Merging multiple SBOMs
257 ~~~~~~~~~~~~~~~~~~~~~~
258
259 The :doc:`/commands/merge` merges multiple SBOMs hierarchically. The intended use-case is to
260 combine multiple SBOMs describing a Debian-based distribution. A good example is the rootfs
261 and the initrd of a Linux distribution.
262
263 Merge two SBOMs representing the above case:
264
265 .. code-block:: bash
266
267 debsbom merge rootfs.spdx.json initrd.spdx.json -o merged.spdx.json
268
269 You can also pass SBOMs via stdin, but you also have to pass the SBOM type in this case:
270
271 .. code-block:: bash
272
273 cat rootfs.spdx.json initrd.spdx.json | debsbom merge -t spdx -o merged.spdx.json -
274
275 Filter packages of an SBOM
276 ~~~~~~~~~~~~~~~~~~~~~~~~~~
277
278 The :doc:`/commands/filter` filters selected packages in an SBOM.
279
280 You can filter to include only binary or source packages:
281
282 .. code-block:: bash
283
284 debsbom filter --binaries sbom.spdx.json filtered.spdx.json
285 debsbom filter --sources sbom.spdx.json filtered.spdx.json
286
287 You can also get the subgraph for a specific package:
288
289 .. code-block:: bash
290
291 debsbom filter --package debsbom sbom.spdx.json package-filtered.spdx.json
292
293 Combining multiple filters are logically ``and``ed. The following would give you all source packages ``debsbom`` transitively depends on:
294
295 .. code-block:: bash
296
297 debsbom filter --sources --package debsbom sbom.spdx.json package-source-filtered.spdx.json
298
299
300 License-Clearing Workflow
301 ~~~~~~~~~~~~~~~~~~~~~~~~~
302
303 ``debsbom`` can be used for license clearing. The license clearing workflow could look like this:
304
305 First, generate a CycloneDX SBOM of a rootfs:
306
307 .. code-block:: bash
308
309 debsbom --progress generate -r path/to/the/rootfs -t cdx -o sbom
310 # output in sbom.cdx.json
311
312 Use the generated SBOM to download all source packages:
313
314 .. code-block:: bash
315
316 debsbom --progress download --outdir downloads --sources sbom.cdx.json
317 # the downloaded files will be in downloads/sources/<archive>
318
319 You will notice that there is no single file for each source package. Instead there is multiple:
320 the .dsc file, an .orig.tar tarball, maybe some patches and more. ``debsbom`` provides an easy
321 way to combine them into a single tarball that can be used in most license clearing platforms:
322
323 .. code-block:: bash
324
325 debsbom --progress source-merge --compress zstd --apply-patches sbom.cdx.json
326 # merged and patched compressed tarballs are in downloads/sources/<archive>
327
328 Now there is a single compressed file for each source package.
329
330 .. note::
331 If you only need to work on a smaller subset of packages you can pass a package list
332 via stdin. See the above sections for concrete examples how to do that.
333
334 Alternatively you can use the :doc:`/commands/repack` to rewrite the SBOM and repack the downloaded
335 artifacts in a format-specific way:
336
337 .. code-block:: bash
338
339 debsbom --progress repack \
340 --format standard-bom-package \
341 --dldir downloads \
342 --compress zstd \
343 --apply-patches \
344 --validate \
345 sbom.cdx.json sbom.packed.cdx.json
346
347 This step is very specific to the actual use-case you have. Right now the only available format
348 is ``standard-bom-package``, which created a directory structure and rewrites the SBOM to reference
349 all source packages directly in there. If you want to see more formats you can open an issue,
350 or even better, contribute it directly.
351
352 Vulnerability Analysis
353 ~~~~~~~~~~~~~~~~~~~~~~
354
355 ``debsbom`` can check a Debian SBOM for known vulnerabilities based on of the
356 `Debian Security Tracker <https://security-tracker.debian.org/tracker/>`_.
357 Reports can be generated in various formats, including OpenVEX and SARIF.
358
359 The following example demonstrates how to generate an OpenVEX report for all components in the
360 SBOM. The result can be used as input to other vulnerability analysis tooling that is not
361 capable of correctly handling components with patch versions.
362
363 .. code-block:: bash
364
365 debsbom sec-scan \
366 --update-db \
367 --author "Test <[email protected]>" \
368 --format vex \
369 sbom.cdx.json \
370 > result.vex
371
372 When scanning SBOMs of existing products, it is often necessary to evaluate the impact of a
373 vulnerability in the context of the overall system. For example, a vulnerability in a
374 component may not be exploitable because the vulnerable code path is not in use. To identify
375 who can make this assessment, you can emit the path from the vulnerable component to
376 the root component (similar to ``debsbom trace-path``) on a per-vulnerability basis:
377
378 .. code-block:: bash
379
380 debsbom sec-scan \
381 --format json \
382 --with-paths-to-root \
383 sbom.cdx.json \
384 | jq | less