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

Add redaction workflow logic #581

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/Microsoft.Sbom.Api/FormatValidator/IValidatedSBOM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface IValidatedSBOM
public Task<FormatValidationResults> GetValidationResults();

public Task<FormatEnforcedSPDX2> GetRawSPDXDocument();

public void Dispose();
sfoslund marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions src/Microsoft.Sbom.Api/FormatValidator/ValidatedSBOM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public async Task<FormatEnforcedSPDX2> GetRawSPDXDocument()
return sbom;
}

public void Dispose()
{
this.sbomStream?.Dispose();
}

private async Task Initialize()
{
if (isInitialized)
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.Sbom.Api/Workflows/Helpers/ISbomRedactor.cs
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);
}
2 changes: 1 addition & 1 deletion src/Microsoft.Sbom.Api/Workflows/Helpers/SbomRedactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Sbom.Api.Workflows.Helpers;
/// <summary>
/// SBOM redactor that removes file information from SBOMs
/// </summary>
public class SbomRedactor
public class SbomRedactor: ISbomRedactor
{
private const string SpdxFileRelationshipPrefix = "SPDXRef-File-";

Expand Down
53 changes: 35 additions & 18 deletions src/Microsoft.Sbom.Api/Workflows/SBOMRedactionWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public class SbomRedactionWorkflow : IWorkflow<SbomRedactionWorkflow>

private readonly ValidatedSBOMFactory validatedSBOMFactory;

private readonly SbomRedactor sbomRedactor;
private readonly ISbomRedactor sbomRedactor;

public SbomRedactionWorkflow(
ILogger log,
IConfiguration configuration,
IFileSystemUtils fileSystemUtils,
ValidatedSBOMFactory validatedSBOMFactory,
SbomRedactor sbomRedactor)
ISbomRedactor sbomRedactor)
{
this.log = log ?? throw new ArgumentNullException(nameof(log));
this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
Expand All @@ -50,44 +50,61 @@ public virtual async Task<bool> RunAsync()
var sboms = await GetValidSbomsAsync();
sfoslund marked this conversation as resolved.
Show resolved Hide resolved

log.Information($"Running redaction on the following SBOMs: {string.Join(' ', sboms.Select(sbom => sbom.Key))}");
foreach (var (sbomPath, validatedSbom) in sboms)

try
{
try
foreach (var (sbomPath, validatedSbom) in sboms)
{
var outputPath = GetOutputPath(sbomPath);
var redactedSpdx = await this.sbomRedactor.RedactSBOMAsync(validatedSbom);
using (var outStream = fileSystemUtils.OpenWrite(outputPath))
try
sfoslund marked this conversation as resolved.
Show resolved Hide resolved
{
await JsonSerializer.SerializeAsync(outStream, redactedSpdx);
var outputPath = GetOutputPath(sbomPath);
var redactedSpdx = await this.sbomRedactor.RedactSBOMAsync(validatedSbom);
using (var outStream = fileSystemUtils.OpenWrite(outputPath))
{
await JsonSerializer.SerializeAsync(outStream, redactedSpdx);
}

log.Information($"Redacted SBOM {sbomPath} saved to {outputPath}");
}
catch (Exception ex)
{
throw new Exception($"Failed to redact {sbomPath}: {ex.Message}", ex);
}

log.Information($"Redacted SBOM {sbomPath} saved to {outputPath}");
}
catch (Exception ex)
}
finally
{
foreach (var (_, validatedSbom) in sboms)
{
throw new Exception($"Failed to redact {sbomPath}: {ex.Message}", ex);
validatedSbom.Dispose();
}
}

return true;
}

private async Task<IDictionary<string, IValidatedSBOM>> GetValidSbomsAsync()
private async Task<IReadOnlyDictionary<string, IValidatedSBOM>> GetValidSbomsAsync()
{
var sbomPaths = GetInputSbomPaths();
var validatedSboms = new Dictionary<string, IValidatedSBOM>();
foreach (var sbom in sbomPaths)
foreach (var sbomPath in sbomPaths)
{
log.Information($"Validating SBOM {sbom}");
var validatedSbom = validatedSBOMFactory.CreateValidatedSBOM(sbom);
log.Information($"Validating SBOM {sbomPath}");
var validatedSbom = validatedSBOMFactory.CreateValidatedSBOM(sbomPath);
var validationDetails = await validatedSbom.GetValidationResults();
if (validationDetails.Status != FormatValidationStatus.Valid)
{
throw new InvalidDataException($"Failed to validate {sbom}:\n{string.Join('\n', validationDetails.Errors)}");
validatedSbom.Dispose();
foreach (var (_, sbom) in validatedSboms)
{
sbom.Dispose();
}

throw new InvalidDataException($"Failed to validate {sbomPath}:\n{string.Join('\n', validationDetails.Errors)}");
}
else
{
validatedSboms.Add(sbom, validatedSbom);
validatedSboms.Add(sbomPath, validatedSbom);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SbomRedactionWorkflowTests
private Mock<IConfiguration> configurationMock;
private Mock<IFileSystemUtils> fileSystemUtilsMock;
private Mock<ValidatedSBOMFactory> validatedSBOMFactoryMock;
private Mock<SbomRedactor> sbomRedactorMock;
private Mock<ISbomRedactor> sbomRedactorMock;
private SbomRedactionWorkflow testSubject;

private const string SbomPathStub = "sbom-path";
Expand All @@ -45,7 +45,7 @@ public void Init()
configurationMock = new Mock<IConfiguration>();
fileSystemUtilsMock = new Mock<IFileSystemUtils>();
validatedSBOMFactoryMock = new Mock<ValidatedSBOMFactory>();
sbomRedactorMock = new Mock<SbomRedactor>();
sbomRedactorMock = new Mock<ISbomRedactor>();
testSubject = new SbomRedactionWorkflow(
mockLogger.Object,
configurationMock.Object,
Expand Down Expand Up @@ -116,6 +116,7 @@ public async Task SbomRedactionWorkflow_FailsOnInvalidSboms()
var validationRes = new FormatValidationResults();
validationRes.AggregateValidationStatus(FormatValidationStatus.NotValid);
validatedSbomMock.Setup(m => m.GetValidationResults()).ReturnsAsync(validationRes).Verifiable();
validatedSbomMock.Setup(m => m.Dispose()).Verifiable();

var result = await testSubject.RunAsync();
}
Expand All @@ -135,6 +136,7 @@ public async Task SbomRedactionWorkflow_RunsRedactionOnValidSboms()
sbomRedactorMock.Setup(m => m.RedactSBOMAsync(validatedSbomMock.Object)).ReturnsAsync(redactedContent).Verifiable();
var outStream = new MemoryStream();
fileSystemUtilsMock.Setup(m => m.OpenWrite(OutPathStub)).Returns(outStream).Verifiable();
validatedSbomMock.Setup(m => m.Dispose()).Verifiable();

var result = await testSubject.RunAsync();
Assert.IsTrue(result);
Expand Down