|
| 1 | +from django.db import migrations |
| 2 | +import logging |
| 3 | + |
| 4 | + |
| 5 | +logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +PARSER_REFERENCES = ['Mobsfscan Scan'] |
| 9 | + |
| 10 | + |
| 11 | +def update_parser_test(test, parser_test_type) -> None: |
| 12 | + if test.test_type.name in PARSER_REFERENCES or test.scan_type in PARSER_REFERENCES: |
| 13 | + test.test_type = parser_test_type |
| 14 | + test.scan_type = parser_test_type.name |
| 15 | + test.save() |
| 16 | + |
| 17 | + |
| 18 | +# Update the found_by field to remove Mobsfscan Scan and add MobSF Scan |
| 19 | +def update_parser_finding(finding, newparser_test_type, parser_test_type) -> None: |
| 20 | + # Check if nessus is in found by list and remove |
| 21 | + if parser_test_type in finding.found_by.all(): |
| 22 | + finding.found_by.remove(parser_test_type.id) |
| 23 | + # Check if tenable is already in list somehow before adding it |
| 24 | + if newparser_test_type not in finding.found_by.all(): |
| 25 | + finding.found_by.add(newparser_test_type.id) |
| 26 | + finding.save() |
| 27 | + |
| 28 | + |
| 29 | +# Update all finding objects that came from Mobsfscan Scan reports |
| 30 | +def forward_merge_parser(apps, schema_editor): |
| 31 | + finding_model = apps.get_model('dojo', 'Finding') |
| 32 | + test_type_model = apps.get_model('dojo', 'Test_Type') |
| 33 | + # Get or create MobSF Scan Test Type and fetch the Mobsfscan Scan test types |
| 34 | + newparser_test_type, _ = test_type_model.objects.get_or_create(name="MobSF Scan", defaults={"active": True}) |
| 35 | + parser_test_type = test_type_model.objects.filter(name="Mobsfscan Scan").first() |
| 36 | + # Get all the findings found by Mobsfscan Scan |
| 37 | + findings = finding_model.objects.filter(test__scan_type__in=PARSER_REFERENCES) |
| 38 | + logger.warning(f'We identified {findings.count()} Mobsfscan Scan findings to migrate to MobSF Scan findings') |
| 39 | + # Iterate over all findings and change |
| 40 | + for finding in findings: |
| 41 | + # Update the found by field |
| 42 | + update_parser_finding(finding, newparser_test_type, parser_test_type) |
| 43 | + # Update the test object |
| 44 | + update_parser_test(finding.test, newparser_test_type) |
| 45 | + |
| 46 | + |
| 47 | +class Migration(migrations.Migration): |
| 48 | + |
| 49 | + dependencies = [ |
| 50 | + ('dojo', '0228_alter_jira_username_password'), |
| 51 | + ] |
| 52 | + |
| 53 | + operations = [ |
| 54 | + migrations.RunPython(forward_merge_parser), |
| 55 | + ] |
0 commit comments