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

Import/Reimport: Add toggle for applying tags to endpoints #9740

Merged
merged 2 commits into from
Mar 18, 2024
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
12 changes: 12 additions & 0 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,10 @@ class ImportScanSerializer(serializers.Serializer):
help_text="If set to True, the tags will be applied to the findings",
required=False,
)
apply_tags_to_endpoints = serializers.BooleanField(
help_text="If set to True, the tags will be applied to the endpoints",
required=False,
)

def save(self, push_to_jira=False):
data = self.validated_data
Expand All @@ -2181,6 +2185,7 @@ def save(self, push_to_jira=False):
api_scan_configuration = data.get("api_scan_configuration", None)
service = data.get("service", None)
apply_tags_to_findings = data.get("apply_tags_to_findings", False)
apply_tags_to_endpoints = data.get("apply_tags_to_endpoints", False)
source_code_management_uri = data.get(
"source_code_management_uri", None
)
Expand Down Expand Up @@ -2274,6 +2279,7 @@ def save(self, push_to_jira=False):
title=test_title,
create_finding_groups_for_all_findings=create_finding_groups_for_all_findings,
apply_tags_to_findings=apply_tags_to_findings,
apply_tags_to_endpoints=apply_tags_to_endpoints,
)

if test:
Expand Down Expand Up @@ -2446,6 +2452,10 @@ class ReImportScanSerializer(TaggitSerializer, serializers.Serializer):
help_text="If set to True, the tags will be applied to the findings",
required=False
)
apply_tags_to_endpoints = serializers.BooleanField(
help_text="If set to True, the tags will be applied to the endpoints",
required=False,
)

def save(self, push_to_jira=False):
logger.debug("push_to_jira: %s", push_to_jira)
Expand All @@ -2459,6 +2469,7 @@ def save(self, push_to_jira=False):
"close_old_findings_product_scope"
)
apply_tags_to_findings = data.get("apply_tags_to_findings", False)
apply_tags_to_endpoints = data.get("apply_tags_to_endpoints", False)
do_not_reactivate = data.get("do_not_reactivate", False)
version = data.get("version", None)
build_id = data.get("build_id", None)
Expand Down Expand Up @@ -2560,6 +2571,7 @@ def save(self, push_to_jira=False):
do_not_reactivate=do_not_reactivate,
create_finding_groups_for_all_findings=create_finding_groups_for_all_findings,
apply_tags_to_findings=apply_tags_to_findings,
apply_tags_to_endpoints=apply_tags_to_endpoints,
)

if test_import:
Expand Down
3 changes: 2 additions & 1 deletion dojo/engagement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ def post(self, request, eid=None, pid=None):
service = form.cleaned_data.get('service', None)
close_old_findings = form.cleaned_data.get('close_old_findings', None)
apply_tags_to_findings = form.cleaned_data.get('apply_tags_to_findings', False)
apply_tags_to_endpoints = form.cleaned_data.get('apply_tags_to_endpoints', False)
# close_old_findings_prodct_scope is a modifier of close_old_findings.
# If it is selected, close_old_findings should also be selected.
close_old_findings_product_scope = form.cleaned_data.get('close_old_findings_product_scope', None)
Expand Down Expand Up @@ -804,7 +805,7 @@ def post(self, request, eid=None, pid=None):
minimum_severity=minimum_severity, endpoints_to_add=list(form.cleaned_data['endpoints']) + added_endpoints, scan_date=scan_date,
version=version, branch_tag=branch_tag, build_id=build_id, commit_hash=commit_hash, push_to_jira=push_to_jira,
close_old_findings=close_old_findings, close_old_findings_product_scope=close_old_findings_product_scope, group_by=group_by, api_scan_configuration=api_scan_configuration, service=service,
create_finding_groups_for_all_findings=create_finding_groups_for_all_findings, apply_tags_to_findings=apply_tags_to_findings)
create_finding_groups_for_all_findings=create_finding_groups_for_all_findings, apply_tags_to_findings=apply_tags_to_findings, apply_tags_to_endpoints=apply_tags_to_endpoints)

message = f'{scan_type} processed a total of {finding_count} findings'

Expand Down
12 changes: 12 additions & 0 deletions dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,12 @@ class ImportScanForm(forms.Form):
required=False,
initial=False
)
apply_tags_to_endpoints = forms.BooleanField(
help_text="If set to True, the tags will be applied to the endpoints",
label="Apply Tags to Endpoints",
required=False,
initial=False
)

if is_finding_groups_enabled():
group_by = forms.ChoiceField(required=False, choices=Finding_Group.GROUP_BY_OPTIONS, help_text='Choose an option to automatically group new findings by the chosen option.')
Expand Down Expand Up @@ -577,6 +583,12 @@ class ReImportScanForm(forms.Form):
required=False,
initial=False
)
apply_tags_to_endpoints = forms.BooleanField(
help_text="If set to True, the tags will be applied to the endpoints",
label="Apply Tags to Endpoints",
required=False,
initial=False
)

if is_finding_groups_enabled():
group_by = forms.ChoiceField(required=False, choices=Finding_Group.GROUP_BY_OPTIONS, help_text='Choose an option to automatically group new findings by the chosen option')
Expand Down
9 changes: 8 additions & 1 deletion dojo/importers/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ def close_old_findings(self, test, scan_date_time, user, push_to_jira=None, serv
def import_scan(self, scan, scan_type, engagement, lead, environment, active=None, verified=None, tags=None, minimum_severity=None,
user=None, endpoints_to_add=None, scan_date=None, version=None, branch_tag=None, build_id=None,
commit_hash=None, push_to_jira=None, close_old_findings=False, close_old_findings_product_scope=False,
group_by=None, api_scan_configuration=None, service=None, title=None, create_finding_groups_for_all_findings=True, apply_tags_to_findings=False):
group_by=None, api_scan_configuration=None, service=None, title=None, create_finding_groups_for_all_findings=True,
apply_tags_to_findings=False, apply_tags_to_endpoints=False):

logger.debug(f'IMPORT_SCAN: parameters: {locals()}')

Expand Down Expand Up @@ -389,6 +390,12 @@ def import_scan(self, scan, scan_type, engagement, lead, environment, active=Non
for tag in tags:
finding.tags.add(tag)

if apply_tags_to_endpoints and tags:
for finding in test_import.findings_affected.all():
for endpoint in finding.endpoints.all():
for tag in tags:
endpoint.tags.add(tag)

logger.debug('IMPORT_SCAN: Generating notifications')
notifications_helper.notify_test_created(test)
updated_count = len(new_findings) + len(closed_findings)
Expand Down
18 changes: 13 additions & 5 deletions dojo/importers/reimporter/reimporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def process_parsed_findings(
scan_date=None,
do_not_reactivate=False,
create_finding_groups_for_all_findings=True,
apply_tags_to_findings=False,
**kwargs,
):

Expand Down Expand Up @@ -576,6 +575,7 @@ def reimport_scan(
do_not_reactivate=False,
create_finding_groups_for_all_findings=True,
apply_tags_to_findings=False,
apply_tags_to_endpoints=False,
):

logger.debug(f"REIMPORT_SCAN: parameters: {locals()}")
Expand Down Expand Up @@ -746,10 +746,18 @@ def reimport_scan(
reactivated_findings,
untouched_findings,
)
if apply_tags_to_findings and tags:
for finding in test_import.findings_affected.all():
for tag in tags:
finding.tags.add(tag)

if apply_tags_to_findings and tags:
for finding in test_import.findings_affected.all():
for tag in tags:
finding.tags.add(tag)

if apply_tags_to_endpoints and tags:
for finding in test_import.findings_affected.all():
for endpoint in finding.endpoints.all():
for tag in tags:
endpoint.tags.add(tag)

logger.debug("REIMPORT_SCAN: Generating notifications")

updated_count = (
Expand Down
7 changes: 5 additions & 2 deletions dojo/test/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,8 @@ def re_import_scan_results(request, tid):

group_by = form.cleaned_data.get('group_by', None)
create_finding_groups_for_all_findings = form.cleaned_data.get('create_finding_groups_for_all_findings')
apply_tags_to_findings = form.cleaned_data.get('apply_tags_to_findings', False)
apply_tags_to_endpoints = form.cleaned_data.get('apply_tags_to_endpoints', False)

active = None
if activeChoice:
Expand Down Expand Up @@ -860,13 +862,14 @@ def re_import_scan_results(request, tid):
try:
test, finding_count, new_finding_count, closed_finding_count, reactivated_finding_count, untouched_finding_count, test_import = \
reimporter.reimport_scan(scan, scan_type, test, active=active, verified=verified,
tags=None, minimum_severity=minimum_severity,
tags=tags, minimum_severity=minimum_severity,
endpoints_to_add=endpoints_to_add, scan_date=scan_date,
version=version, branch_tag=branch_tag, build_id=build_id,
commit_hash=commit_hash, push_to_jira=push_to_jira,
close_old_findings=close_old_findings, group_by=group_by,
api_scan_configuration=api_scan_configuration, service=service, do_not_reactivate=do_not_reactivate,
create_finding_groups_for_all_findings=create_finding_groups_for_all_findings)
create_finding_groups_for_all_findings=create_finding_groups_for_all_findings,
apply_tags_to_findings=apply_tags_to_findings, apply_tags_to_endpoints=apply_tags_to_endpoints)
except Exception as e:
logger.exception(e)
add_error_message_to_response('An exception error occurred during the report import:%s' % str(e))
Expand Down
Loading