Skip to content

Commit 0d56b21

Browse files
committed
Update OSS Index datasource for Sonatype Guide
Signed-off-by: Ghxst <200635707+GHX5T-SOL@users.noreply.github.com>
1 parent f641395 commit 0d56b21

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

vulntotal/datasources/oss_index.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
class OSSDataSource(DataSource):
2424
spdx_license_expression = "TODO"
2525
license_url = "TODO"
26-
api_unauthenticated = "https://ossindex.sonatype.org/api/v3/component-report"
27-
api_authenticated = "https://ossindex.sonatype.org/api/v3/authorized/component-report"
26+
api_unauthenticated = "https://api.guide.sonatype.com/api/v3/component-report"
27+
api_authenticated = "https://api.guide.sonatype.com/api/v3/authorized/component-report"
2828

2929
def fetch_json_response(self, coordinates):
30-
"""Fetch JSON response from OSS Index API for a given list of coordinates.
30+
"""Fetch JSON response from the Sonatype Guide OSS Index compatibility API.
3131
3232
Parameters:
3333
coordinates: A list of strings representing the package coordinates.
3434
3535
Returns:
36-
A dictionary containing the JSON response from the OSS Index API, or None if the response is unsuccessful or an error occurs while fetching data.
36+
A dictionary containing the JSON response from the compatibility API, or None
37+
if the response is unsuccessful or an error occurs while fetching data.
3738
"""
3839
username = os.environ.get("OSS_USERNAME", None)
3940
token = os.environ.get("OSS_TOKEN", None)
@@ -91,7 +92,7 @@ def supported_ecosystem(cls):
9192

9293
def parse_advisory(component, purl) -> Iterable[VendorData]:
9394
"""
94-
Parse component from OSS Index API and yield VendorData.
95+
Parse component from the Sonatype Guide OSS Index compatibility API and yield VendorData.
9596
9697
Parameters:
9798
component: A list containing a dictionary with component details.

vulntotal/tests/test_oss_index.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import json
1111
from pathlib import Path
12+
from unittest.mock import patch
1213

1314
from commoncode import testcase
1415
from packageurl import PackageURL
@@ -20,6 +21,41 @@
2021
class TestDeps(testcase.FileBasedTesting):
2122
test_data_dir = str(Path(__file__).resolve().parent / "test_data" / "oss_index")
2223

24+
def test_fetch_json_response_uses_sonatype_guide_compatibility_api(self):
25+
coordinates = ["pkg:pypi/django@5.2.1"]
26+
datasource = oss_index.OSSDataSource()
27+
28+
with patch.object(oss_index.requests, "post") as mock_post:
29+
mock_response = mock_post.return_value
30+
mock_response.raise_for_status.return_value = None
31+
mock_response.json.return_value = []
32+
33+
assert datasource.fetch_json_response(coordinates) == []
34+
35+
mock_post.assert_called_once_with(
36+
"https://api.guide.sonatype.com/api/v3/component-report",
37+
auth=None,
38+
json={"coordinates": coordinates},
39+
)
40+
41+
def test_fetch_json_response_uses_authenticated_sonatype_guide_compatibility_api(self):
42+
coordinates = ["pkg:pypi/django@5.2.1"]
43+
datasource = oss_index.OSSDataSource()
44+
45+
with patch.dict(oss_index.os.environ, {"OSS_USERNAME": "user", "OSS_TOKEN": "token"}):
46+
with patch.object(oss_index.requests, "post") as mock_post:
47+
mock_response = mock_post.return_value
48+
mock_response.raise_for_status.return_value = None
49+
mock_response.json.return_value = []
50+
51+
assert datasource.fetch_json_response(coordinates) == []
52+
53+
mock_post.assert_called_once_with(
54+
"https://api.guide.sonatype.com/api/v3/authorized/component-report",
55+
auth=("user", "token"),
56+
json={"coordinates": coordinates},
57+
)
58+
2359
def test_parse_advisory(self):
2460
advisory_file = self.get_test_loc("advisory.json")
2561
with open(advisory_file) as f:

0 commit comments

Comments
 (0)