Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update metadata contract to be backcompatible with SPDX 2.2 parser #918

Merged
merged 5 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Microsoft.Sbom.Contracts/Contracts/Spdx22Metadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Sbom.Contracts;

public class Spdx22Metadata : SpdxMetadata { }
6 changes: 6 additions & 0 deletions src/Microsoft.Sbom.Contracts/Contracts/Spdx30Metadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Sbom.Contracts;

public class Spdx30Metadata : SpdxMetadata { }
2 changes: 1 addition & 1 deletion src/Microsoft.Sbom.Contracts/Contracts/SpdxMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Sbom.Contracts;
/// <summary>
/// The object representation of all the metadata in an SPDX document.
/// </summary>
public class SpdxMetadata
public abstract class SpdxMetadata
{
/// <summary>
/// The version of the SPDX specification used in this document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public SpdxMetadata GetMetadata()
throw new ParserException($"{nameof(this.GetMetadata)} can only be called after Parsing is complete to ensure that a whole object is returned.");
}

var spdxMetadata = new SpdxMetadata();
var spdxMetadata = new Spdx22Metadata();
foreach (var kvp in this.metadata)
{
switch (kvp.Key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public static class SPDXToSbomFormatConverterExtensions
public static SbomFile ToSbomFile(this SPDXFile spdxFile)
{
var checksums = spdxFile.FileChecksums?.Select(c => c.ToSbomChecksum());
if (!checksums.Any() || checksums.All(c => c.Algorithm != AlgorithmName.SHA256))
{
throw new ParserException("File hash is missing a SHA256 value");
}

return new SbomFile
{
Expand All @@ -49,12 +45,6 @@ public static SbomFile ToSbomFile(this SPDXFile spdxFile)
/// <returns></returns>
public static SbomPackage ToSbomPackage(this SPDXPackage spdxPackage)
{
if (spdxPackage.FilesAnalyzed &&
(spdxPackage.LicenseInfoFromFiles == null || !spdxPackage.LicenseInfoFromFiles.Any()))
{
throw new ParserException("Package license list was empty.");
}

if (spdxPackage.PackageVerificationCode is not null
&& string.IsNullOrEmpty(spdxPackage.PackageVerificationCode.PackageVerificationCodeValue))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SPDX30Parser : ISbomParser

public ComplianceStandard? RequiredComplianceStandard;
public IReadOnlyCollection<string>? EntitiesToEnforceComplianceStandardsFor;
public SpdxMetadata Metadata = new SpdxMetadata();
public SpdxMetadata Metadata = new Spdx30Metadata();
private readonly LargeJsonParser parser;
private readonly IList<string> observedFieldNames = new List<string>();
private readonly bool requiredFieldsCheck = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Sbom.Contracts;
using Microsoft.Sbom.JsonAsynchronousNodeKit.Exceptions;
using Microsoft.Sbom.Parser.Strings;
using Microsoft.Sbom.Parsers.Spdx22SbomParser;
Expand Down Expand Up @@ -41,6 +42,7 @@ public void MetadataPopulates()
var metadata = parser.GetMetadata();

Assert.IsNotNull(metadata);
Assert.IsInstanceOfType(metadata, typeof(Spdx22Metadata));
Assert.IsNotNull(metadata.CreationInfo);
var expectedTime = DateTime.Parse("2023-05-11T00:24:54Z").ToUniversalTime();
Assert.AreEqual(expectedTime, metadata.CreationInfo.Created);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Sbom.Contracts;
using Microsoft.Sbom.JsonAsynchronousNodeKit.Exceptions;
using Microsoft.Sbom.Parser.JsonStrings;
using Microsoft.Sbom.Parsers.Spdx30SbomParser;
Expand All @@ -27,6 +28,7 @@ public void MetadataPopulates()
Assert.AreEqual(5, results.FormatEnforcedSPDX3Result.Graph.Count());

var metadata = parser.GetMetadata();
Assert.IsInstanceOfType(metadata, typeof(Spdx30Metadata));
Assert.IsNotNull(metadata);
Assert.IsNotNull(metadata.DocumentNamespace);
Assert.AreEqual("spdx-doc-name", metadata.Name);
Expand All @@ -49,6 +51,7 @@ public void MetadataEmpty()
var results = this.Parse(parser);

var metadata = parser.GetMetadata();
Assert.IsInstanceOfType(metadata, typeof(Spdx30Metadata));
Assert.IsNotNull(metadata);
Assert.IsNull(metadata.DocumentNamespace);
Assert.IsNull(metadata.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void ValidateSbomPackagesForNTIA(List<Element> elementsList)
/// <param name="result"></param>
public SpdxMetadata SetMetadata(ParserResults result)
{
var metadata = new SpdxMetadata();
var metadata = new Spdx30Metadata();
var spdxDocumentElement = (SpdxDocument?)result.FormatEnforcedSPDX3Result.Graph.FirstOrDefault(element => element.Type == "SpdxDocument");

if (spdxDocumentElement == null)
Expand Down