-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add redaction workflow logic * Fix test * PR feedback pt 1 * PR feedback pt 2 * PR feedback pt 3 * PR feedback pt 4 * PR feedback pt 5
- Loading branch information
Showing
13 changed files
with
564 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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.Api.FormatValidator; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Sbom.Parsers.Spdx22SbomParser.Entities; | ||
|
||
public interface IValidatedSBOM: IDisposable | ||
{ | ||
public Task<FormatValidationResults> GetValidationResults(); | ||
|
||
public Task<FormatEnforcedSPDX2> GetRawSPDXDocument(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Microsoft.Sbom.Api/FormatValidator/ValidatedSBOMFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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.Api.FormatValidator; | ||
|
||
using System.IO; | ||
|
||
public class ValidatedSBOMFactory | ||
{ | ||
public virtual IValidatedSBOM CreateValidatedSBOM(string sbomFilePath) | ||
{ | ||
var sbomStream = new StreamReader(sbomFilePath); | ||
var validatedSbom = new ValidatedSBOM(sbomStream.BaseStream); | ||
return validatedSbom; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.Sbom.Api.FormatValidator; | ||
using Microsoft.Sbom.Parsers.Spdx22SbomParser.Entities; | ||
|
||
namespace Microsoft.Sbom.Api.Workflows.Helpers; | ||
|
||
/// <summary> | ||
/// SBOM redactor that removes file information from SBOMs | ||
/// </summary> | ||
public interface ISbomRedactor | ||
{ | ||
public Task<FormatEnforcedSPDX2> RedactSBOMAsync(IValidatedSBOM sbom); | ||
} |
101 changes: 101 additions & 0 deletions
101
src/Microsoft.Sbom.Api/Workflows/Helpers/SbomRedactor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Sbom.Api.FormatValidator; | ||
using Microsoft.Sbom.Common.Utils; | ||
using Microsoft.Sbom.Parsers.Spdx22SbomParser.Entities; | ||
using Serilog; | ||
|
||
namespace Microsoft.Sbom.Api.Workflows.Helpers; | ||
|
||
/// <summary> | ||
/// SBOM redactor that removes file information from SBOMs | ||
/// </summary> | ||
public class SbomRedactor: ISbomRedactor | ||
{ | ||
private const string SpdxFileRelationshipPrefix = "SPDXRef-File-"; | ||
|
||
private readonly ILogger log; | ||
|
||
public SbomRedactor( | ||
ILogger log) | ||
{ | ||
this.log = log ?? throw new ArgumentNullException(nameof(log)); | ||
} | ||
|
||
public virtual async Task<FormatEnforcedSPDX2> RedactSBOMAsync(IValidatedSBOM sbom) | ||
{ | ||
var spdx = await sbom.GetRawSPDXDocument(); | ||
|
||
if (spdx.Files != null) | ||
{ | ||
this.log.Debug("Removing files section from SBOM."); | ||
spdx.Files = null; | ||
} | ||
|
||
RemovePackageFileRefs(spdx); | ||
RemoveRelationshipsWithFileRefs(spdx); | ||
UpdateDocumentNamespace(spdx); | ||
|
||
return spdx; | ||
} | ||
|
||
private void RemovePackageFileRefs(FormatEnforcedSPDX2 spdx) | ||
{ | ||
if (spdx.Packages != null) | ||
{ | ||
foreach (var package in spdx.Packages) | ||
{ | ||
if (package.HasFiles != null) | ||
{ | ||
this.log.Debug($"Removing has files property from package {package.Name}."); | ||
package.HasFiles = null; | ||
} | ||
|
||
if (package.SourceInfo != null) | ||
{ | ||
this.log.Debug($"Removing has sourceInfo property from package {package.Name}."); | ||
package.SourceInfo = null; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void RemoveRelationshipsWithFileRefs(FormatEnforcedSPDX2 spdx) | ||
{ | ||
if (spdx.Relationships != null) | ||
{ | ||
var relationshipsToRemove = new List<SPDXRelationship>(); | ||
foreach (var relationship in spdx.Relationships) | ||
{ | ||
if (relationship.SourceElementId.Contains(SpdxFileRelationshipPrefix) || relationship.TargetElementId.Contains(SpdxFileRelationshipPrefix)) | ||
{ | ||
relationshipsToRemove.Add(relationship); | ||
} | ||
} | ||
|
||
if (relationshipsToRemove.Any()) | ||
{ | ||
this.log.Debug($"Removing {relationshipsToRemove.Count()} relationships with file references from SBOM."); | ||
spdx.Relationships = spdx.Relationships.Except(relationshipsToRemove); | ||
} | ||
} | ||
} | ||
|
||
private void UpdateDocumentNamespace(FormatEnforcedSPDX2 spdx) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(spdx.DocumentNamespace) && spdx.CreationInfo.Creators.Any(c => c.StartsWith("Tool: Microsoft.SBOMTool", StringComparison.OrdinalIgnoreCase))) | ||
{ | ||
var existingNamespaceComponents = spdx.DocumentNamespace.Split('/'); | ||
var uniqueComponent = IdentifierUtils.GetShortGuid(Guid.NewGuid()); | ||
existingNamespaceComponents[^1] = uniqueComponent; | ||
spdx.DocumentNamespace = string.Join("/", existingNamespaceComponents); | ||
|
||
this.log.Debug($"Updated document namespace to {spdx.DocumentNamespace}."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.