Skip to content

Commit 1dc19ea

Browse files
authored
feat: VCIO v3 advisories migration (#534)
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 97efa59 commit 1dc19ea

49 files changed

Lines changed: 700 additions & 818 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ shell:
2727
# Open a bash session in a standalone container (no stack required)
2828
docker run -it $(IMAGE_NAME) bash
2929

30+
# make test - full suite
31+
# make test k=<pattern> - filter by name, e.g. make test k=test_name
3032
test:
31-
@echo "-> Run the test suite"
32-
${MANAGE} test --noinput --parallel auto
33+
${MANAGE} test --noinput --parallel auto $(if $(k),-k $(k),)
3334

3435
migrations:
3536
@echo "-> Creates new database migrations"

component_catalog/api.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ class ComponentFilterSet(DataspacedAPIFilterSet):
436436
field_name="affected_by_vulnerabilities",
437437
)
438438
affected_by = django_filters.CharFilter(
439-
field_name="affected_by_vulnerabilities__vulnerability_id",
440-
label="Affected by (vulnerability_id)",
439+
field_name="affected_by_vulnerabilities__advisory_id",
440+
label="Affected by (advisory_id)",
441441
)
442442

443443
class Meta:
@@ -630,7 +630,8 @@ class PackageSerializer(
630630
read_only=True,
631631
many=True,
632632
fields=[
633-
"vulnerability_id",
633+
"advisory_uid",
634+
"advisory_id",
634635
"api_url",
635636
"uuid",
636637
],
@@ -809,8 +810,8 @@ class PackageAPIFilterSet(DataspacedAPIFilterSet):
809810
field_name="affected_by_vulnerabilities",
810811
)
811812
affected_by = django_filters.CharFilter(
812-
field_name="affected_by_vulnerabilities__vulnerability_id",
813-
label="Affected by (vulnerability_id)",
813+
field_name="affected_by_vulnerabilities__advisory_id",
814+
label="Affected by (advisory_id)",
814815
)
815816
risk_score = ScoreRangeFilter(score_ranges=RISK_SCORE_RANGES)
816817

component_catalog/filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class ComponentFilterSet(DataspacedFilterSet):
119119
widget=DropDownRightWidget(link_content='<i class="fas fa-bug"></i>'),
120120
)
121121
affected_by = django_filters.CharFilter(
122-
field_name="affected_by_vulnerabilities__vulnerability_id",
122+
field_name="affected_by_vulnerabilities__advisory_id",
123123
label=_("Affected by"),
124124
)
125125

@@ -267,7 +267,7 @@ class PackageFilterSet(DataspacedFilterSet):
267267
widget=DropDownRightWidget(link_content='<i class="fas fa-bug"></i>'),
268268
)
269269
affected_by = django_filters.CharFilter(
270-
field_name="affected_by_vulnerabilities__vulnerability_id",
270+
field_name="affected_by_vulnerabilities__advisory_id",
271271
label=_("Affected by"),
272272
)
273273
affected_by_last_modified_date = django_filters.DateRangeFilter(

component_catalog/license_expression_dje.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_dataspace_licensing(dataspace, license_keys=None):
5959
# Bypass cache if license_keys is provided
6060
return fetch_licensing_for_dataspace(dataspace, license_keys)
6161

62-
cache_key = str({dataspace.name})
62+
cache_key = str(dataspace.uuid)
6363
# First look in the cache for an existing Licensing for this Dataspace
6464
licensing = licensing_cache.get(cache_key)
6565

component_catalog/templates/component_catalog/tabs/tab_vulnerabilities.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
<strong>
5252
{% if vulnerability.resource_url %}
5353
<a href="{{ vulnerability.resource_url }}" target="_blank">
54-
{{ vulnerability.vulnerability_id }}
54+
{{ vulnerability.advisory_id }}
5555
<i class="fa-solid fa-up-right-from-square mini"></i>
5656
</a>
5757
{% else %}
58-
{{ vulnerability.vulnerability_id }}
58+
{{ vulnerability.advisory_id }}
5959
{% endif %}
6060
</strong>
6161
<div class="mt-2">

component_catalog/tests/test_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,17 +1350,17 @@ def test_api_package_endpoint_vulnerabilities_features(self):
13501350
results = response.data["results"]
13511351
self.assertEqual("9.0", results[0]["risk_score"])
13521352
self.assertEqual(
1353-
vulnerability1.vulnerability_id,
1354-
results[0]["affected_by_vulnerabilities"][0]["vulnerability_id"],
1353+
vulnerability1.advisory_id,
1354+
results[0]["affected_by_vulnerabilities"][0]["advisory_id"],
13551355
)
13561356

1357-
data = {"affected_by": vulnerability1.vulnerability_id}
1357+
data = {"affected_by": vulnerability1.advisory_id}
13581358
response = self.client.get(self.package_list_url, data)
13591359
self.assertEqual(1, response.data["count"])
13601360
self.assertContains(response, self.package1_detail_url)
13611361
self.assertNotContains(response, self.package2_detail_url)
13621362

1363-
data = {"affected_by": vulnerability2.vulnerability_id}
1363+
data = {"affected_by": vulnerability2.advisory_id}
13641364
response = self.client.get(self.package_list_url, data)
13651365
self.assertEqual(0, response.data["count"])
13661366
self.assertNotContains(response, self.package1_detail_url)

component_catalog/tests/test_filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,6 @@ def test_package_filterset_affected_by_filter(self):
406406
self.assertIn(package1, filterset.qs)
407407
self.assertIn(package2, filterset.qs)
408408

409-
data = {"affected_by": vulnerability1.vulnerability_id}
409+
data = {"affected_by": vulnerability1.advisory_id}
410410
filterset = PackageFilterSet(dataspace=self.dataspace, data=data)
411411
self.assertQuerySetEqual(filterset.qs, [package1])

component_catalog/tests/test_license_expression_dje.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def test_fetch_licensing_for_dataspace(self):
299299

300300
def test_get_dataspace_licensing(self):
301301
licensing_cache = caches["licensing"]
302-
cache_key = str({self.dataspace.name})
302+
cache_key = str(self.dataspace.uuid)
303303
self.assertFalse(licensing_cache.has_key(cache_key))
304304

305305
licensing = get_dataspace_licensing(self.dataspace)

component_catalog/tests/test_views.py

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ def test_component_details_view_tab_vulnerabilities(self):
10551055
)
10561056
self.assertContains(response, expected)
10571057
self.assertContains(response, 'id="tab_vulnerabilities"')
1058-
self.assertContains(response, vulnerability1.vcid)
1058+
self.assertContains(response, vulnerability1.advisory_id)
10591059

10601060
def test_component_catalog_component_create_ajax_view(self):
10611061
component_create_ajax_url = reverse("component_catalog:component_add_ajax")
@@ -3021,7 +3021,7 @@ def test_package_details_view_tab_vulnerabilities(self):
30213021
)
30223022
self.assertContains(response, expected)
30233023
self.assertContains(response, 'id="tab_vulnerabilities"')
3024-
self.assertContains(response, self.vulnerability1.vcid)
3024+
self.assertContains(response, self.vulnerability1.advisory_id)
30253025

30263026
def test_vulnerablecode_get_plain_purls(self):
30273027
purls = get_plain_purls(packages=[])
@@ -3057,65 +3057,34 @@ def test_vulnerablecode_get_vulnerable_purls(self):
30573057
with mock.patch(
30583058
"dejacode_toolkit.vulnerablecode.VulnerableCode.bulk_search_by_purl"
30593059
) as bulk_search:
3060-
bulk_search.return_value = []
3060+
bulk_search.return_value = {"count": 0, "results": []}
30613061
vulnerable_purls = vulnerablecode.get_vulnerable_purls(packages=[self.package1])
30623062
self.assertEqual([], vulnerable_purls)
30633063

3064-
bulk_search.return_value = ["pkg:pypi/django@2.1"]
3064+
bulk_search.return_value = {"count": 1, "results": ["pkg:pypi/django@2.1"]}
30653065
vulnerable_purls = vulnerablecode.get_vulnerable_purls(packages=[self.package1])
30663066
self.assertEqual(["pkg:pypi/django@2.1"], vulnerable_purls)
30673067

3068-
def test_vulnerablecode_get_vulnerable_cpes(self):
3069-
vulnerablecode = VulnerableCode(self.dataspace)
3070-
vulnerable_cpes = vulnerablecode.get_vulnerable_cpes(components=[])
3071-
self.assertEqual([], vulnerable_cpes)
3072-
3073-
components = [self.component1, self.component2]
3074-
vulnerable_cpes = vulnerablecode.get_vulnerable_cpes(components=components)
3075-
self.assertEqual([], vulnerable_cpes)
3076-
3077-
self.component1.cpe = "cpe:2.3:a:djangoproject:django:0.95:*:*:*:*:*:*:*"
3078-
self.component1.save()
3079-
3080-
with mock.patch(
3081-
"dejacode_toolkit.vulnerablecode.VulnerableCode.bulk_search_by_cpes"
3082-
) as bulk_search:
3083-
bulk_search.return_value = [
3084-
{
3085-
"vulnerability_id": "VCID-188m-1bke-aaae",
3086-
"summary": "The administrative interface in django.contrib.admin ",
3087-
"references": [
3088-
{"reference_id": ""},
3089-
],
3090-
}
3091-
]
3092-
vulnerable_cpes = vulnerablecode.get_vulnerable_cpes(components=components)
3093-
self.assertEqual([], vulnerable_cpes)
3094-
3095-
bulk_search.return_value[0]["references"] = [{"reference_id": self.component1.cpe}]
3096-
vulnerable_cpes = vulnerablecode.get_vulnerable_cpes(components=components)
3097-
self.assertEqual([self.component1.cpe], vulnerable_cpes)
3098-
3099-
@mock.patch("dejacode_toolkit.vulnerablecode.VulnerableCode.request_get")
3100-
def test_vulnerablecode_get_vulnerabilities_cache(self, mock_request_get):
3068+
@mock.patch("dejacode_toolkit.vulnerablecode.VulnerableCode.bulk_search_by_purl")
3069+
def test_vulnerablecode_get_vulnerabilities_cache(self, mock_bulk_search):
31013070
vulnerablecode = VulnerableCode(self.dataspace)
31023071

31033072
self.package1.set_package_url("pkg:pypi/django@2.1")
31043073
self.package1.save()
31053074

3106-
mock_request_get.return_value = {
3075+
mock_bulk_search.return_value = {
31073076
"count": 1,
31083077
"results": True,
31093078
}
31103079

31113080
results = vulnerablecode.get_vulnerabilities_by_purl(self.package1.package_url)
3112-
self.assertEqual(1, mock_request_get.call_count)
3081+
self.assertEqual(1, mock_bulk_search.call_count)
31133082
self.assertTrue(results)
31143083

31153084
results = vulnerablecode.get_vulnerabilities_by_purl(self.package1.package_url)
3116-
# request.get was only called once since the results are returned from the cached
3085+
# bulk_search_by_purl was only called once since the results are returned from the cache
31173086
# on the second call of `get_vulnerabilities_by_purl`.
3118-
self.assertEqual(1, mock_request_get.call_count)
3087+
self.assertEqual(1, mock_bulk_search.call_count)
31193088
self.assertTrue(results)
31203089

31213090
def test_send_scan_notification(self):

dejacode/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ def get_fake_redis_connection(config, use_strict_redis):
725725
VULNERABLECODE_USER_AGENT = env.str("VULNERABLECODE_USER_AGENT", default="VCIO_API_AGENT")
726726
CREATE_DEPENDENCIES_DEFAULT = env.bool("CREATE_DEPENDENCIES_DEFAULT", default=True)
727727

728+
728729
if IS_TESTS:
729730
# Silent the django-axes logging during tests
730731
LOGGING["loggers"].update({"axes": {"handlers": ["null"]}})

0 commit comments

Comments
 (0)