|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Microsoft.Sbom.Targets; |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Diagnostics.Tracing; |
| 9 | +using System.IO; |
| 10 | +using Microsoft.Build.Framework; |
| 11 | +using Microsoft.Build.Utilities; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Microsoft.Extensions.Hosting; |
| 14 | +using Microsoft.Sbom.Api.Manifest.ManifestConfigHandlers; |
| 15 | +using Microsoft.Sbom.Api.Metadata; |
| 16 | +using Microsoft.Sbom.Api.Providers; |
| 17 | +using Microsoft.Sbom.Api.Providers.ExternalDocumentReferenceProviders; |
| 18 | +using Microsoft.Sbom.Api.Providers.FilesProviders; |
| 19 | +using Microsoft.Sbom.Api.Providers.PackagesProviders; |
| 20 | +using Microsoft.Sbom.Contracts; |
| 21 | +using Microsoft.Sbom.Contracts.Entities; |
| 22 | +using Microsoft.Sbom.Contracts.Interfaces; |
| 23 | +using Microsoft.Sbom.Extensions; |
| 24 | +using Microsoft.Sbom.Extensions.DependencyInjection; |
| 25 | +using Microsoft.Sbom.Parsers.Spdx22SbomParser; |
| 26 | + |
| 27 | +/// <summary> |
| 28 | +/// MSBuild task for generating SBOMs from build output. |
| 29 | +/// </summary> |
| 30 | +public partial class GenerateSbom : Task |
| 31 | +{ |
| 32 | + private ISBOMGenerator Generator { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Constructor for the GenerateSbomTask. |
| 36 | + /// </summary> |
| 37 | + public GenerateSbom() |
| 38 | + { |
| 39 | + var host = Host.CreateDefaultBuilder() |
| 40 | + .ConfigureServices((host, services) => |
| 41 | + services |
| 42 | + .AddSbomTool() |
| 43 | + /* Manually adding some dependencies since `AddSbomTool()` does not add them when |
| 44 | + * running the MSBuild Task from another project. |
| 45 | + */ |
| 46 | + .AddSingleton<ISourcesProvider, SBOMPackagesProvider>() |
| 47 | + .AddSingleton<ISourcesProvider, CGExternalDocumentReferenceProvider>() |
| 48 | + .AddSingleton<ISourcesProvider, DirectoryTraversingFileToJsonProvider>() |
| 49 | + .AddSingleton<ISourcesProvider, ExternalDocumentReferenceFileProvider>() |
| 50 | + .AddSingleton<ISourcesProvider, ExternalDocumentReferenceProvider>() |
| 51 | + .AddSingleton<ISourcesProvider, FileListBasedFileToJsonProvider>() |
| 52 | + .AddSingleton<ISourcesProvider, SbomFileBasedFileToJsonProvider>() |
| 53 | + .AddSingleton<ISourcesProvider, CGScannedExternalDocumentReferenceFileProvider>() |
| 54 | + .AddSingleton<ISourcesProvider, CGScannedPackagesProvider>() |
| 55 | + .AddSingleton<IAlgorithmNames, AlgorithmNames>() |
| 56 | + .AddSingleton<IManifestGenerator, Generator>() |
| 57 | + .AddSingleton<IMetadataProvider, LocalMetadataProvider>() |
| 58 | + .AddSingleton<IMetadataProvider, SBOMApiMetadataProvider>() |
| 59 | + .AddSingleton<IManifestInterface, Validator>() |
| 60 | + .AddSingleton<IManifestConfigHandler, SPDX22ManifestConfigHandler>()) |
| 61 | + .Build(); |
| 62 | + this.Generator = host.Services.GetRequiredService<ISBOMGenerator>(); |
| 63 | + } |
| 64 | + |
| 65 | + /// <inheritdoc/> |
| 66 | + public override bool Execute() |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + // Validate required args and args that take paths as input. |
| 71 | + if (!ValidateAndSanitizeRequiredParams() || !ValidateAndSanitizeNamespaceUriUniquePart()) |
| 72 | + { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + // Set other configurations. The GenerateSBOMAsync() already sanitizes and checks for |
| 77 | + // a valid namespace URI and generates a random guid for NamespaceUriUniquePart if |
| 78 | + // one is not provided. |
| 79 | + var sbomMetadata = new SBOMMetadata |
| 80 | + { |
| 81 | + PackageSupplier = this.PackageSupplier, |
| 82 | + PackageName = this.PackageName, |
| 83 | + PackageVersion = this.PackageVersion, |
| 84 | + }; |
| 85 | + var runtimeConfiguration = new RuntimeConfiguration |
| 86 | + { |
| 87 | + NamespaceUriBase = this.NamespaceBaseUri, |
| 88 | + NamespaceUriUniquePart = this.NamespaceUriUniquePart, |
| 89 | + DeleteManifestDirectoryIfPresent = this.DeleteManifestDirIfPresent, |
| 90 | + Verbosity = ValidateAndAssignVerbosity(), |
| 91 | + }; |
| 92 | +#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits |
| 93 | + var result = System.Threading.Tasks.Task.Run(() => this.Generator.GenerateSbomAsync( |
| 94 | + rootPath: this.BuildDropPath, |
| 95 | + manifestDirPath: this.ManifestDirPath, |
| 96 | + metadata: sbomMetadata, |
| 97 | + componentPath: this.BuildComponentPath, |
| 98 | + runtimeConfiguration: runtimeConfiguration, |
| 99 | + specifications: ValidateAndAssignSpecifications(), |
| 100 | + externalDocumentReferenceListFile: this.ExternalDocumentListFile)).GetAwaiter().GetResult(); |
| 101 | +#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits |
| 102 | + |
| 103 | + return result.IsSuccessful; |
| 104 | + } |
| 105 | + catch (Exception e) |
| 106 | + { |
| 107 | + Log.LogError($"SBOM generation failed: {e.Message}"); |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Check for ManifestInfo and create an SbomSpecification accordingly. |
| 114 | + /// </summary> |
| 115 | + /// <returns>A list of the parsed manifest info. Null if the manifest info is null or empty.</returns> |
| 116 | + private IList<SbomSpecification> ValidateAndAssignSpecifications() |
| 117 | + { |
| 118 | + if (!string.IsNullOrWhiteSpace(this.ManifestInfo)) |
| 119 | + { |
| 120 | + return [SbomSpecification.Parse(this.ManifestInfo)]; |
| 121 | + } |
| 122 | + |
| 123 | + return null; |
| 124 | + } |
| 125 | +} |
0 commit comments