|
| 1 | + |
| 2 | +from django.db import migrations, models |
| 3 | + |
| 4 | + |
| 5 | +class Migration(migrations.Migration): |
| 6 | + |
| 7 | + """ |
| 8 | + This script will create endpoint status objects for findings and endpoints for |
| 9 | + databases that already contain those objects. |
| 10 | + """ |
| 11 | + def create_status_objects(apps, schema_editor): |
| 12 | + # Retreive the correct models |
| 13 | + Finding = apps.get_model('dojo', 'Finding') |
| 14 | + Endpoint_Status = apps.get_model('dojo', 'Endpoint_Status') |
| 15 | + # Get a list of findings that have endpoints |
| 16 | + findings = Finding.objects.annotate(count=models.Count('endpoints')).filter(count__gt=0) |
| 17 | + for finding in findings: |
| 18 | + # Get the list of endpoints on the current finding |
| 19 | + endpoints = finding.endpoints.all() |
| 20 | + for endpoint in endpoints: |
| 21 | + # Superflous error checking |
| 22 | + try: |
| 23 | + # Create a new status for each endpoint |
| 24 | + status, created = Endpoint_Status.objects.get_or_create( |
| 25 | + finding=finding, |
| 26 | + endpoint=endpoint, |
| 27 | + ) |
| 28 | + # Check if the status object was created, otherwise, there is nothing to do |
| 29 | + if created: |
| 30 | + status.date = finding.date |
| 31 | + # If the parent endpoint was mitigated with the old system, |
| 32 | + # reflect the same on the endpoint status object |
| 33 | + if endpoint.mitigated: |
| 34 | + status.mitigated = True |
| 35 | + status.mitigated_by = finding.reporter |
| 36 | + # Save the status object with at least one updated field |
| 37 | + status.save() |
| 38 | + # Attach the status to the endpoint and finding |
| 39 | + endpoint.endpoint_status.add(status) |
| 40 | + finding.endpoint_status.add(status) |
| 41 | + except Exception as e: |
| 42 | + # Something wild happened |
| 43 | + print(e) |
| 44 | + pass |
| 45 | + |
| 46 | + dependencies = [ |
| 47 | + ('dojo', '0047_jira_minimum_severity_default'), |
| 48 | + ] |
| 49 | + |
| 50 | + operations = [migrations.RunPython(create_status_objects)] |
0 commit comments