|
| 1 | +from django.db import migrations, models |
| 2 | +import django.db.models.deletion |
| 3 | +from django.core.validators import MaxValueValidator, MinValueValidator |
| 4 | +from vulnerabilities.improver import MAX_CONFIDENCE |
| 5 | + |
| 6 | +def split_packagerelatedvulnerability(apps, schema_editor): |
| 7 | + PackageRelatedVulnerability = apps.get_model('vulnerabilities', 'PackageRelatedVulnerability') |
| 8 | + FixingPackageRelatedVulnerability = apps.get_model('vulnerabilities', 'FixingPackageRelatedVulnerability') |
| 9 | + AffectedByPackageRelatedVulnerability = apps.get_model('vulnerabilities', 'AffectedByPackageRelatedVulnerability') |
| 10 | + |
| 11 | + for prv in PackageRelatedVulnerability.objects.all(): |
| 12 | + if prv.fix: |
| 13 | + FixingPackageRelatedVulnerability.objects.create( |
| 14 | + package=prv.package, |
| 15 | + vulnerability=prv.vulnerability, |
| 16 | + created_by=prv.created_by, |
| 17 | + confidence=prv.confidence, |
| 18 | + ) |
| 19 | + else: |
| 20 | + AffectedByPackageRelatedVulnerability.objects.create( |
| 21 | + package=prv.package, |
| 22 | + vulnerability=prv.vulnerability, |
| 23 | + created_by=prv.created_by, |
| 24 | + confidence=prv.confidence, |
| 25 | + ) |
| 26 | + |
| 27 | +def reverse_migration(apps, schema_editor): |
| 28 | + FixingPackageRelatedVulnerability = apps.get_model('vulnerabilities', 'FixingPackageRelatedVulnerability') |
| 29 | + AffectedByPackageRelatedVulnerability = apps.get_model('vulnerabilities', 'AffectedByPackageRelatedVulnerability') |
| 30 | + PackageRelatedVulnerability = apps.get_model('vulnerabilities', 'PackageRelatedVulnerability') |
| 31 | + |
| 32 | + for fpv in FixingPackageRelatedVulnerability.objects.all(): |
| 33 | + PackageRelatedVulnerability.objects.create( |
| 34 | + package=fpv.package, |
| 35 | + vulnerability=fpv.vulnerability, |
| 36 | + created_by=fpv.created_by, |
| 37 | + confidence=fpv.confidence, |
| 38 | + fix=True, |
| 39 | + ) |
| 40 | + |
| 41 | + for apv in AffectedByPackageRelatedVulnerability.objects.all(): |
| 42 | + PackageRelatedVulnerability.objects.create( |
| 43 | + package=apv.package, |
| 44 | + vulnerability=apv.vulnerability, |
| 45 | + created_by=apv.created_by, |
| 46 | + confidence=apv.confidence, |
| 47 | + fix=False, |
| 48 | + ) |
| 49 | + |
| 50 | +class Migration(migrations.Migration): |
| 51 | + |
| 52 | + dependencies = [ |
| 53 | + ("vulnerabilities", "0070_alter_advisory_created_by_and_more"), |
| 54 | + ] |
| 55 | + |
| 56 | + operations = [ |
| 57 | + migrations.AlterField( |
| 58 | + model_name="advisory", |
| 59 | + name="created_by", |
| 60 | + field=models.CharField( |
| 61 | + help_text="Fully qualified name of the importer prefixed with themodule name importing the advisory. Eg:vulnerabilities.pipeline.nginx_importer.NginxImporterPipeline", |
| 62 | + max_length=100, |
| 63 | + ), |
| 64 | + ), |
| 65 | + migrations.CreateModel( |
| 66 | + name="FixingPackageRelatedVulnerability", |
| 67 | + fields=[ |
| 68 | + ( |
| 69 | + "id", |
| 70 | + models.AutoField( |
| 71 | + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" |
| 72 | + ), |
| 73 | + ), |
| 74 | + ( |
| 75 | + "created_by", |
| 76 | + models.CharField( |
| 77 | + blank=True, |
| 78 | + help_text="Fully qualified name of the improver prefixed with the module name responsible for creating this relation. Eg: vulnerabilities.importers.nginx.NginxBasicImprover", |
| 79 | + max_length=100, |
| 80 | + ), |
| 81 | + ), |
| 82 | + ( |
| 83 | + "confidence", |
| 84 | + models.PositiveIntegerField( |
| 85 | + default=100, |
| 86 | + help_text="Confidence score for this relation", |
| 87 | + validators=[ |
| 88 | + django.core.validators.MinValueValidator(0), |
| 89 | + django.core.validators.MaxValueValidator(100), |
| 90 | + ], |
| 91 | + ), |
| 92 | + ), |
| 93 | + ( |
| 94 | + "package", |
| 95 | + models.ForeignKey( |
| 96 | + on_delete=django.db.models.deletion.CASCADE, to="vulnerabilities.package" |
| 97 | + ), |
| 98 | + ), |
| 99 | + ( |
| 100 | + "vulnerability", |
| 101 | + models.ForeignKey( |
| 102 | + on_delete=django.db.models.deletion.CASCADE, |
| 103 | + to="vulnerabilities.vulnerability", |
| 104 | + ), |
| 105 | + ), |
| 106 | + ], |
| 107 | + options={ |
| 108 | + "verbose_name_plural": "Fixing Package Related Vulnerabilities", |
| 109 | + "ordering": ["package", "vulnerability"], |
| 110 | + "abstract": False, |
| 111 | + "unique_together": {("package", "vulnerability")}, |
| 112 | + }, |
| 113 | + ), |
| 114 | + migrations.CreateModel( |
| 115 | + name="AffectedByPackageRelatedVulnerability", |
| 116 | + fields=[ |
| 117 | + ( |
| 118 | + "id", |
| 119 | + models.AutoField( |
| 120 | + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" |
| 121 | + ), |
| 122 | + ), |
| 123 | + ( |
| 124 | + "created_by", |
| 125 | + models.CharField( |
| 126 | + blank=True, |
| 127 | + help_text="Fully qualified name of the improver prefixed with the module name responsible for creating this relation. Eg: vulnerabilities.importers.nginx.NginxBasicImprover", |
| 128 | + max_length=100, |
| 129 | + ), |
| 130 | + ), |
| 131 | + ( |
| 132 | + "confidence", |
| 133 | + models.PositiveIntegerField( |
| 134 | + default=100, |
| 135 | + help_text="Confidence score for this relation", |
| 136 | + validators=[ |
| 137 | + django.core.validators.MinValueValidator(0), |
| 138 | + django.core.validators.MaxValueValidator(100), |
| 139 | + ], |
| 140 | + ), |
| 141 | + ), |
| 142 | + ( |
| 143 | + "package", |
| 144 | + models.ForeignKey( |
| 145 | + on_delete=django.db.models.deletion.CASCADE, to="vulnerabilities.package" |
| 146 | + ), |
| 147 | + ), |
| 148 | + ( |
| 149 | + "vulnerability", |
| 150 | + models.ForeignKey( |
| 151 | + on_delete=django.db.models.deletion.CASCADE, |
| 152 | + to="vulnerabilities.vulnerability", |
| 153 | + ), |
| 154 | + ), |
| 155 | + ], |
| 156 | + options={ |
| 157 | + "verbose_name_plural": "Affected By Package Related Vulnerabilities", |
| 158 | + "ordering": ["package", "vulnerability"], |
| 159 | + "abstract": False, |
| 160 | + "unique_together": {("package", "vulnerability")}, |
| 161 | + }, |
| 162 | + ), |
| 163 | + migrations.RunPython(split_packagerelatedvulnerability, reverse_migration), |
| 164 | + ] |
0 commit comments