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

Revert "Added spdx package component information from SPDX file (#766)" #825

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ComponentDetection.Detectors.Spdx;
namespace Microsoft.ComponentDetection.Detectors.Spdx;

using System;
using System.Collections.Generic;
Expand All @@ -9,9 +9,9 @@
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.Internal;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.ComponentDetection.Detectors.Spdx.Contracts;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// Spdx22ComponentDetector discover SPDX SBOM files in JSON format and create components with the information about
Expand Down Expand Up @@ -46,7 +46,6 @@
{
this.Logger.LogDebug("Discovered SPDX2.2 manifest file at: {ManifestLocation}", processRequest.ComponentStream.Location);
var file = processRequest.ComponentStream;
var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder;

try
{
Expand All @@ -59,46 +58,30 @@
using var reader = new JsonTextReader(sr);
var serializer = new JsonSerializer();

var spdxFileData = serializer.Deserialize<SpdxFileData>(reader);

if (spdxFileData == null)
{
this.Logger.LogWarning("Discovered SPDX file at {ManifestLocation} is not a valid document, skipping", processRequest.ComponentStream.Location);
return Task.CompletedTask;
}

if (!this.IsSPDXVersionSupported(spdxFileData.Version))
{
this.Logger.LogWarning("Discovered SPDX at {ManifestLocation} is not SPDX-2.2 document, skipping", processRequest.ComponentStream.Location);
return Task.CompletedTask;
}

var sbomComponent = this.ConvertJObjectToSbomComponent(processRequest, spdxFileData, hash);
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(sbomComponent));

if (spdxFileData.HasPackages())
try
{
foreach (var package in spdxFileData.Packages)
var document = serializer.Deserialize<JObject>(reader);
if (document != null)
{
SpdxPackageComponent spdxPackageComponent;

var extRefLocator = package.ExternalRefs?.FirstOrDefault(x => x.Type == "purl")?.Locator;
if (extRefLocator is not null)
if (this.IsSPDXVersionSupported(document))
{
spdxPackageComponent = new SpdxPackageComponent(package.Name, package.Version, package.Supplier, package.CopyrightText, package.DownloadLocation, extRefLocator);
var sbomComponent = this.ConvertJObjectToSbomComponent(processRequest, document, hash);
processRequest.SingleFileComponentRecorder.RegisterUsage(new DetectedComponent(sbomComponent));
}
else
{
spdxPackageComponent = new SpdxPackageComponent(package.Name, package.Version, package.Supplier, package.CopyrightText, package.DownloadLocation);
this.Logger.LogWarning("Discovered SPDX at {ManifestLocation} is not SPDX-2.2 document, skipping", processRequest.ComponentStream.Location);
}

singleFileComponentRecorder.RegisterUsage(new DetectedComponent(spdxPackageComponent));
}
else
{
this.Logger.LogWarning("Discovered SPDX file at {ManifestLocation} is not a valid document, skipping", processRequest.ComponentStream.Location);

Check warning on line 78 in src/Microsoft.ComponentDetection.Detectors/spdx/Spdx22ComponentDetector.cs

View check run for this annotation

Codecov / codecov/patch

src/Microsoft.ComponentDetection.Detectors/spdx/Spdx22ComponentDetector.cs#L77-L78

Added lines #L77 - L78 were not covered by tests
}
}
}
catch (JsonException je)
{
this.Logger.LogWarning(je, "Unable to parse file at {ManifestLocation}, skipping", processRequest.ComponentStream.Location);
catch (JsonReaderException)
{
this.Logger.LogWarning("Unable to parse file at {ManifestLocation}, skipping", processRequest.ComponentStream.Location);
}
}
catch (Exception e)
{
Expand All @@ -108,13 +91,16 @@
return Task.CompletedTask;
}

private bool IsSPDXVersionSupported(string version) => this.supportedSPDXVersions.Contains(version?.ToString(), StringComparer.OrdinalIgnoreCase);
private bool IsSPDXVersionSupported(JObject document) => this.supportedSPDXVersions.Contains(document["spdxVersion"]?.ToString(), StringComparer.OrdinalIgnoreCase);

private SpdxComponent ConvertJObjectToSbomComponent(ProcessRequest processRequest, SpdxFileData spdxFileData, string fileHash)
private SpdxComponent ConvertJObjectToSbomComponent(ProcessRequest processRequest, JObject document, string fileHash)
{
var rootElements = spdxFileData.DocumentDescribes;
var sbomNamespace = document["documentNamespace"]?.ToString();
var rootElements = document["documentDescribes"]?.ToObject<string[]>();
var name = document["name"]?.ToString();
var spdxVersion = document["spdxVersion"]?.ToString();

if (rootElements?.Count() > 1)
if (rootElements?.Length > 1)
{
this.Logger.LogWarning("SPDX file at {ManifestLocation} has more than one element in documentDescribes, first will be selected as root element.", processRequest.ComponentStream.Location);
}
Expand All @@ -126,7 +112,7 @@

var rootElementId = rootElements?.FirstOrDefault() ?? "SPDXRef-Document";
var path = processRequest.ComponentStream.Location;
var component = new SpdxComponent(spdxFileData.Version, new Uri(spdxFileData.DocumentNamespace), spdxFileData.Name, fileHash, rootElementId, path);
var component = new SpdxComponent(spdxVersion, new Uri(sbomNamespace), name, fileHash, rootElementId, path);

return component;
}
Expand Down
Loading
Loading