Skip to content
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
21 changes: 16 additions & 5 deletions src/DockerUpdateGuard/Images/VulnerabilityEnrichmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,24 @@ private async Task<int> UpsertVulnerabilityFindingsAsync(ScanRun scanRun,
}

/// <summary>
/// Deactivate active vulnerability findings whose image version is no longer part of the live inventory
/// Deactivate active vulnerability findings whose image version is neither part of the live inventory nor enriched by the current run
/// </summary>
/// <param name="scanRun">Owning scan run</param>
/// <param name="processedImageVersionIds">Identifiers of the image versions enriched by the current run</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Number of deactivated findings</returns>
private async Task<int> DeactivateStaleFindingsAsync(ScanRun scanRun, CancellationToken cancellationToken)
private async Task<int> DeactivateStaleFindingsAsync(ScanRun scanRun,
IReadOnlyCollection<Guid> processedImageVersionIds,
CancellationToken cancellationToken)
{
var liveImageVersionIds = await _liveImageInventoryQueryService.GetLiveImageVersionIdsAsync(cancellationToken).ConfigureAwait(false);
var liveImageVersionIdList = liveImageVersionIds.ToList();

// Image versions enriched by this run are never stale for this run, no matter whether their enrichment succeeded or failed
var retainedImageVersionIds = liveImageVersionIds.Union(processedImageVersionIds)
.ToList();

var staleFindings = await _dbContext.VulnerabilityFindings.Where(entity => entity.IsActive
&& liveImageVersionIdList.Contains(entity.ImageVersionId) == false)
&& retainedImageVersionIds.Contains(entity.ImageVersionId) == false)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);

Expand Down Expand Up @@ -396,6 +402,9 @@ await _dbContext.SaveChangesAsync(cancellationToken)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);

var processedImageVersionIds = images.Select(entity => entity.Id)
.ToList();

processedImageCount = images.Count;

if (processedImageCount == 0)
Expand All @@ -422,7 +431,9 @@ await _dbContext.SaveChangesAsync(cancellationToken)
.ConfigureAwait(false);
}

var deactivatedFindingCount = await DeactivateStaleFindingsAsync(scanRun, cancellationToken).ConfigureAwait(false);
var deactivatedFindingCount = await DeactivateStaleFindingsAsync(scanRun,
processedImageVersionIds,
cancellationToken).ConfigureAwait(false);

if (deactivatedFindingCount > 0)
{
Expand Down
Loading
Loading