Microsoft SBOM Tool

repository·main·Indexed 24 days ago

https://github.com/microsoft/sbom-tool

An enterprise-ready utility for creating SPDX 2.2 and 3.0 compatible Software Bill of Materials (SBOMs). It features a CLI for generating, validating, and redacting SBOMs, a C# API via the Microsoft.Sbom.Api NuGet package, and MSBuild integration through Microsoft.Sbom.Targets for automatic generation during .NET project packing. The tool utilizes component detection libraries and the ClearlyDefined API to identify dependencies and populate license information.

Tokens
14.3K
Snippets
37
Records
59
Agent score
79%

What's inside microsoft-sbom-tool

  1. Understand the Files section in the generated SBOM

    main

    The files section is generated by traversing all directories and files located at the BuildDropPath provided to the tool. For every file found, the tool generates an SPDXID, a SHA1 hash, and a SHA256 hash. This section provides a granular inventory of the actual files present in the build drop.

    "files": [
        {
          "fileName": "./sbom-tool-win-x64.exe",
          "SPDXID": "SPDXRef-File--sbom-tool-win-x64.exe-E55F25E239D8D3572D75D5CDC5CA24899FD4993F",
          "checksums": [
            {
              "algorithm": "SHA256",
              "checksumValue": "56624d8ab67ac0e323bcac0ae1ec0656f1721c6bb60640ecf9b30e861062aad5"
            },
            {
              "algorithm": "SHA1",
              "checksumValue": "e55f25e239d8d3572d75d5cdc5ca24899fd4993f"
            }
          ],
          "licenseConcluded": "NOASSERTION",
          "licenseInfoInFiles": [
            "NOASSERTION"
          ],
          "copyrightText": "NOASSERTION"
        }
      ]
  2. How Microsoft.Sbom.Targets handles different MSBuild runtimes

    main

    The Microsoft.Sbom.Targets package uses different implementation logic depending on the MSBuild runtime being used, ensuring compatibility across environments:

    1. .NET Core CLI (.NET Core runtime): Uses GenerateSbomTask.cs, which invokes the SBOM API directly to generate the SBOM.
    2. Visual Studio (Full .NET Framework runtime): Uses SbomCLIToolTask.cs, which invokes the SBOM CLI Tool because the SBOM API does not support the .NET Framework.
  3. Understand the SBOM tool source structure

    main

    The repository is organized into several key projects:

    • src: Contains all source code for the tool.
    • test: Contains the test suite.
    • Microsoft.Sbom.Api: The core engine responsible for generating SBOMs.
    • Microsoft.Sbom.Extensions: Contains interfaces for extending the tool (e.g., IManifestGenerator for serializing SBOMs to specific formats like SPDX 2.2 or 3.0).
    • Microsoft.Sbom.Common: Base code and constants used across all projects.
    • Microsoft.Sbom.Contracts: Defines interfaces for calling the tool via a C# API (e.g., the ISbomGenerator class).
    • Microsoft.Sbom.Tool: Defines the Command Line Interface (CLI).
  4. Specify a custom output directory for SBOM files

    main
    By default, the SBOM tool places the generated SBOM file in the build drop folder specified by the -b parameter. If your organization requires SBOMs to be stored in a centrally-controlled repository or a specific directory, use the -manifestDirPath parameter to define the intended folder location for the SBOM output file.
  5. Handle External Document References in SBOM

    main

    If the sbom-tool detects an existing SPDX 2.2 SBOM within the project being scanned, it performs two actions:

    1. It includes that SBOM file in the files section (often marked with "fileTypes": ["SPDX"]).
    2. It adds an entry to the externalDocumentRefs section to link the current SBOM to the detected external document.
     "files": [
        {
          "fileName": "./manifest.spdx.json",
          "SPDXID": "SPDXRef-File--manifest.spdx.json-9E563530C733C814FC04FF5C61D8DC9FB4FD29CB",
          "checksums": [
            {
              "algorithm": "SHA256",
              "checksumValue": "63789f7d621728cb197dcc5655d7b18ab35f825b596496e633b61291cfa00f4b"
            },
            {
              "algorithm": "SHA1",
              "checksumValue": "9e563530c733c814fc04ff5c61d8dc9fb4fd29cb"
            }
          ],
          "licenseConcluded": "NOASSERTION",
          "licenseInfoInFiles": [
            "NOASSERTION"
          ],
          "copyrightText": "NOASSERTION",
          "fileTypes": [
            "SPDX"
          ]
        },
     ]
    
    "externalDocumentRefs": [
        {
          "externalDocumentId": "DocumentRef-TEST.GIT-bd801c3b7553a08b6874d6bc129203c5a8a95a04-f3bca9cb-67f5-1320-d17e-188a244fc472-1.0-9e563530c733c814fc04ff5c61d8dc9fb4fd29cb",
          "spdxDocument": "https://sbom.microsoft/TEST.GIT/bd801c3b7553a08b6874d6bc129203c5a8a95a04/f3bca9cb-67f5-1320-d17e-188a244fc472/1.0/fa0d13e3-0136-4cc8-af85-622c04063a0f",
          "checksum": {
            "algorithm": "SHA1",
            "checksumValue": "9e563530c733c814fc04ff5c61d8dc9fb4fd29cb"
          }
        }
      ]
  6. Understand the Packages section in the generated SBOM

    main

    The packages section identifies software components used in the project by scanning the BuildComponentPath using component detection libraries.

    To enable populating advanced license and package information via the ClearlyDefinedApi or custom implementations, you must set both the -pm and -li arguments to true when running the tool.

    Supported ecosystems include:

    • Cargo: via *.cargo-sbom.json, Cargo.toml, or Cargo.lock.
    • Ruby: via gemfile.lock.
    • Pip (Python): via setup.py, requirements.txt, or dist package METADATA. Requires Python 2/3 and internet connection.
    • Maven: via pom.xml. Requires Maven.
    • NPM: via package.json.
    • NuGet: via project.assets.json, *.nupkg, *.nuspec, packages.config, or nuget.config.
    • Linux (Debian, Alpine, Rhel, etc.): via syft.
    • CocoaPods: via podfile.lock.
    • Conda (Python): via conda-lock.yml or *.conda-lock.yml.
    • Gradle: via *.lockfile. Requires Gradle 7 or prior.
    • Go: via go list -m -json all or go mod graph. Fallback to go.mod/go.sum. Requires Go 1.11+.
    • Yarn (v1, v2): via package.json and yarn.lock.
    • Pnpm: via shrinkwrap.yaml or pnpm-lock.yaml.
    • Poetry (Python): via poetry.lock.
    "packages": [
        {
          "name": "Microsoft.VisualStudio.Threading.Analyzers",
          "SPDXID": "SPDXRef-Package-CCB741BD164B5B2F9771AD784B281D62BDB0E0707EE703E76AF22BFFB4503941",
          "downloadLocation": "NOASSERTION",
          "filesAnalyzed": false,
          "licenseConcluded": "MIT",
          "licenseDeclared": "MIT",
          "copyrightText": "NOASSERTION",
          "versionInfo": "17.7.30",
          "externalRefs": [
            {
              "referenceCategory": "PACKAGE-MANAGER",
              "referenceType": "purl",
              "referenceLocator": "pkg:nuget/Microsoft.VisualStudio.Threading.Analyzers@17.7.30"
            }
          ],
          "supplier": "Organization: Microsoft"
        }
    ]
  7. Integrate SBOM generation into GitHub Actions

    main

    To integrate the SBOM tool into a GitHub Actions workflow, you must download the tool executable, make it executable, and then run the generate command targeting your build output directory.

    In a typical workflow, the SBOM generation step should occur after the build step but before the artifact upload step. This ensures the generated SBOM file is included in the uploaded build artifacts.

    Key parameters for the generate command used in this integration:

    • -b <path>: The build drop folder (the directory containing the binaries you want to scan).
    • -bc <path>: The build components path (the directory containing project files like .csproj that hold dependency information).
    • -pn <name>: Package name.
    • -pv <version>: Package version.
    • -ps <publisher>: Package publisher.
    • -nsb <uri>: Namespace base URI.
    • -V <verbosity>: Verbosity level (e.g., Verbose).
        - name: Generate SBOM
          run: |
            curl -Lo $RUNNER_TEMP/sbom-tool https://github.com/microsoft/sbom-tool/releases/latest/download/sbom-tool-linux-x64
            chmod +x $RUNNER_TEMP/sbom-tool
            $RUNNER_TEMP/sbom-tool generate -b ./buildOutput -bc . -pn Test -pv 1.0.0 -ps MyCompany -nsb https://sbom.mycompany.com -V Verbose
  8. Set up a Generation Service with Microsoft Dependency Injection

    main
    To integrate the SBOM API into a .NET application, it is recommended to set up a Generation Service using the Microsoft Dependency Injection library. The ISbomGenerator interface is the primary entry point for triggering SBOM generation tasks within your service lifecycle (e.g., implementing IHostedService).
  9. Aggregate SBOMs using a configuration file

    main

    To aggregate multiple SBOMs into a single package, use the --ConfigFilePath argument to point to a JSON configuration file. This file allows you to map multiple artifacts to their respective SBOM locations and define metadata for the resulting aggregated SBOM.

    Important Constraints:

    • Aggregation currently only supports SPDX 2.2 SBOMs. Support for SPDX 3.0 is planned for a future release.
    • Environment variables (e.g., $(UserProfile)) are supported and will be expanded during parsing.
    {
      "ArtifactInfoMap": {
        "C:\\Artifact1\\Bin\\Release\\net8.0": { },
        "C:\\Artifact2\\Bin": {
          "ExternalManifestDir": "C:\\Artifact2\\_manifest"
        }
      },
      "ManifestDirPath": "$(UserProfile)\\testing",
      "PackageName": "CombinedPackageName",
      "PackageVersion": "$(CombinedPackagedVersion)",
      "PackageSupplier": "$(CombinedPackageSupplier)"
    }
  10. Build and debug using Visual Studio 2022

    main
    1. Open Microsoft.Sbom.sln in Visual Studio 2022.
    2. Right-click the Microsoft.Sbom.Tool project and select Set as Startup Project.
    3. Configure debug arguments: Right-click Microsoft.Sbom.Tool -> Properties -> Debug. Set the following minimum arguments: generate -b <drop path> -bc <build components path> -pn <package name> -pv <package version> -ps <company name> -nsb <namespace uri base>
    4. Build the solution using Ctrl + Shift + B or select Build from the menu.
    5. Press F5 to build and start the process in debug mode.