-
Notifications
You must be signed in to change notification settings - Fork 8
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
Refactor scan results and findings to support multiple types #190
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0fea55d
AlexN/query-rule-findings-results
alex-nork 3cf9ffd
make scan run work with multiple finding/result types
alex-nork 8d52c18
remove get_combined_results function
alex-nork 8d708ed
pull duplicate code into separate template
alex-nork a1f59bc
revert erroneous change
alex-nork 6295d97
Merge branch 'main' into AlexN/refactor-scan-results
alex-nork a267ad9
put back comment
alex-nork 233f21c
refactor function and view to be more performant
alex-nork 597ef63
don't cast unique_rules as a dict
alex-nork File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,45 @@ | ||
{% load scan_filters %} | ||
{% for rule in unique_rules %} | ||
<tr> | ||
<td> | ||
<span style="color:{{ rule.severity.color }};"> | ||
{{ rule.severity.name }} ({{ rule.severity.value }}) | ||
</span> | ||
</td> | ||
<td>{{rule.policy.policy.name}} : {{rule.name}}</td> | ||
<td>{{rule.finding_count}}</td> | ||
<td> | ||
<button class="btn btn-link" type="button" data-bs-toggle="collapse" | ||
data-bs-target="#collapse{{rule_type}}{{rule.id}}" aria-expanded="false" | ||
aria-controls="collapse{{rule_type}}{{rule.id}}"> <i class="fa-solid fa-magnifying-glass"></i> | ||
</button> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td colspan="7"> | ||
<div class="collapse" id="collapse{{rule_type}}{{rule.id}}"> | ||
<div class="card card-body"> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th scope="col">Finding</th> | ||
<th scope="col">Source ID</th> | ||
<th scope="col">Asset</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for finding in rule.findings %} | ||
<tr> | ||
<td>{{finding|surrounding_text_with_preview_size:finding_preview_size}} | ||
</td> | ||
<td>{{ finding.source_id }}</td> | ||
<td>{{ finding.result.scan_asset.asset.name }}</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</td> | ||
</tr> | ||
{% endfor %} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from django.template import loader | ||
from django.utils import timezone | ||
from embedding.models import Embedding | ||
from policy.models import Policy | ||
from policy.models import MultiQueryRule, Policy, RegexRule | ||
|
||
from chirps.celery import app as celery_app | ||
|
||
|
@@ -45,6 +45,32 @@ def view_scan_history(request, scan_id): | |
) | ||
|
||
|
||
def get_unique_rules_and_findings(results): | ||
"""Return a dictionary of unique rules for each rule class from a list of results.""" | ||
rule_dict = {} | ||
finding_count = 0 | ||
finding_severities = defaultdict(int) | ||
unique_rules = defaultdict(set) | ||
|
||
for result in results: | ||
rule = result.rule | ||
|
||
if rule.id not in rule_dict: | ||
rule.finding_count = 0 | ||
rule.findings = [] | ||
rule_dict[rule.id] = rule | ||
Comment on lines
+55
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just an educational note: If/when we finally get a postgres backend, using |
||
|
||
findings = list(result.findings.all()) | ||
count = len(findings) | ||
rule.finding_count += count | ||
rule.findings.extend(findings) | ||
finding_count += count | ||
finding_severities[rule.severity] += count | ||
unique_rules[type(rule)].add(rule) | ||
|
||
return unique_rules, finding_count, finding_severities | ||
|
||
|
||
@login_required | ||
def view_scan_run(request, scan_run_id): | ||
"""View details for a particular scan.""" | ||
|
@@ -54,40 +80,20 @@ def view_scan_run(request, scan_run_id): | |
results = [] | ||
scan_assets = ScanAsset.objects.filter(scan=scan_run) | ||
|
||
# Step 1: build a list of all the results (rules) with findings. | ||
# Build a list of all the results (rules) with findings. | ||
for scan_asset in scan_assets: | ||
# Iterate through the rule set | ||
for result in scan_asset.results.all(): | ||
for result in scan_asset.results: | ||
|
||
if result.has_findings(): | ||
results.append(result) | ||
|
||
# Step 2: aggregate results by rules with matching rule IDs | ||
unique_rules = {result.rule for result in results} | ||
|
||
# Next, walk through all of the results, aggregating the findings count for each unique rule ID | ||
finding_count = 0 | ||
finding_severities = defaultdict(int) | ||
|
||
# Walk through each unique rule | ||
for rule in unique_rules: | ||
rule.finding_count = 0 | ||
rule.findings = [] | ||
|
||
# Iterate through each result that was hit for the rule | ||
for result in results: | ||
|
||
# Increment the number of findings for this rule | ||
if result.rule.id == rule.id: | ||
findings = list(result.findings.all()) | ||
count = len(findings) | ||
rule.finding_count += count | ||
finding_count += count | ||
rule.findings.extend(findings) | ||
|
||
# While we're in this loop, store off the number of times each severity is encountered | ||
# This will be used to render the pie-chart in the UI | ||
finding_severities[rule.severity] += count | ||
# Aggregate results by rules with matching rule IDs | ||
unique_rules, finding_count, finding_severities = get_unique_rules_and_findings(results) | ||
unique_regex_rules = unique_rules.get(RegexRule, []) | ||
unique_multiquery_rules = unique_rules.get(MultiQueryRule, []) | ||
severity_counts = list(finding_severities.values()) | ||
severities = [str(severity).replace("'", '"') for severity in finding_severities.keys()] | ||
|
||
# Retrieve finding_preview_size from the user's profile | ||
finding_preview_size = request.user.profile.finding_preview_size | ||
|
@@ -98,11 +104,10 @@ def view_scan_run(request, scan_run_id): | |
{ | ||
'scan_run': scan_run, # The scan run object | ||
'finding_count': finding_count, # Total number of findings | ||
'unique_rules': unique_rules, # List of unique rules hit by findings | ||
'severities': [ | ||
str(severity).replace("'", '"') for severity in finding_severities.keys() | ||
], # List of all the severities encountered | ||
'severity_counts': list(finding_severities.values()), # List of all the severity counts encountered | ||
'unique_regex_rules': unique_regex_rules, # List of unique regex rules hit by findings | ||
'unique_multiquery_rules': unique_multiquery_rules, # List of unique multiquery rules hit by findings | ||
'severities': severities, # List of all the severities encountered | ||
'severity_counts': severity_counts, # List of all the severity counts encountered | ||
'finding_preview_size': finding_preview_size, | ||
}, | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
viva reusable templates!!! 🍻