From 033a01f9024cd12a44d2aea66436592e0de8e704 Mon Sep 17 00:00:00 2001 From: k-charansai Date: Thu, 12 Nov 2026 10:09:13 +0530 Subject: [PATCH] Replace OSS Index with Sonatype Guide in VulnTotal OSS Index has transitioned to Sonatype Guide with compatibility API endpoints hosted under https://api.guide.sonatype.com. Update VulnTotal's OSS Index datasource to use the new Sonatype Guide endpoints and support Personal Access Token (PAT) authentication via SONATYPE_GUIDE_TOKEN while maintaining full backward compatibility with legacy OSS_USERNAME and OSS_TOKEN settings. - Update API endpoints to api.guide.sonatype.com - Introduce SonatypeGuideDataSource (aliased to OSSDataSource) - Register sonatype_guide in DATASOURCE_REGISTRY and CLI range mappings - Add unit tests for unauthenticated, PAT-only, and legacy authentication Closes #2311 Signed-off-by: k-charansai --- vulntotal/datasources/__init__.py | 1 + vulntotal/datasources/oss_index.py | 27 +++++--- vulntotal/tests/test_oss_index.py | 100 ++++++++++++++++++++++++++++- vulntotal/vulntotal_cli.py | 1 + 4 files changed, 119 insertions(+), 10 deletions(-) diff --git a/vulntotal/datasources/__init__.py b/vulntotal/datasources/__init__.py index fb269a201..134504252 100644 --- a/vulntotal/datasources/__init__.py +++ b/vulntotal/datasources/__init__.py @@ -23,6 +23,7 @@ "safetydb": safetydb.SafetydbDataSource, "gitlab": gitlab.GitlabDataSource, "oss_index": oss_index.OSSDataSource, + "sonatype_guide": oss_index.SonatypeGuideDataSource, "osv": osv.OSVDataSource, "snyk": snyk.SnykDataSource, "vulnerablecode": vulnerablecode.VulnerableCodeDataSource, diff --git a/vulntotal/datasources/oss_index.py b/vulntotal/datasources/oss_index.py index a18e65d21..be35fec4a 100644 --- a/vulntotal/datasources/oss_index.py +++ b/vulntotal/datasources/oss_index.py @@ -20,26 +20,31 @@ logger = logging.getLogger(__name__) -class OSSDataSource(DataSource): +class SonatypeGuideDataSource(DataSource): spdx_license_expression = "TODO" license_url = "TODO" - api_unauthenticated = "https://ossindex.sonatype.org/api/v3/component-report" - api_authenticated = "https://ossindex.sonatype.org/api/v3/authorized/component-report" + api_unauthenticated = "https://api.guide.sonatype.com/api/v3/component-report" + api_authenticated = "https://api.guide.sonatype.com/api/v3/authorized/component-report" def fetch_json_response(self, coordinates): - """Fetch JSON response from OSS Index API for a given list of coordinates. + """Fetch JSON response from Sonatype Guide API for a given list of coordinates. Parameters: coordinates: A list of strings representing the package coordinates. Returns: - 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. + A dictionary containing the JSON response from the Sonatype Guide API, + or None if the response is unsuccessful or an error occurs while fetching data. """ - username = os.environ.get("OSS_USERNAME", None) - token = os.environ.get("OSS_TOKEN", None) + token = os.environ.get("SONATYPE_GUIDE_TOKEN") or os.environ.get("OSS_TOKEN") + username = ( + os.environ.get("SONATYPE_GUIDE_USERNAME") + or os.environ.get("OSS_USERNAME") + or ("token" if token else None) + ) auth = None url = self.api_unauthenticated - if username and token: + if token: auth = (username, token) url = self.api_authenticated response = requests.post(url, auth=auth, json={"coordinates": coordinates}) @@ -53,7 +58,7 @@ def fetch_json_response(self, coordinates): msg = ( "Too many requests" if auth - else "Too many requests: add OSS_USERNAME and OSS_TOKEN in .env file" + else "Too many requests: add SONATYPE_GUIDE_TOKEN (or legacy OSS_TOKEN) in .env file" ) logger.error(msg) else: @@ -89,6 +94,10 @@ def supported_ecosystem(cls): } +# Maintain backward compatibility for existing imports and usages +OSSDataSource = SonatypeGuideDataSource + + def parse_advisory(component, purl) -> Iterable[VendorData]: """ Parse component from OSS Index API and yield VendorData. diff --git a/vulntotal/tests/test_oss_index.py b/vulntotal/tests/test_oss_index.py index 6ba02e634..69e3a4b11 100644 --- a/vulntotal/tests/test_oss_index.py +++ b/vulntotal/tests/test_oss_index.py @@ -8,8 +8,11 @@ # import json +import os from pathlib import Path +from unittest import mock +import requests from commoncode import testcase from packageurl import PackageURL @@ -17,7 +20,7 @@ from vulntotal.datasources import oss_index -class TestDeps(testcase.FileBasedTesting): +class TestSonatypeGuide(testcase.FileBasedTesting): test_data_dir = str(Path(__file__).resolve().parent / "test_data" / "oss_index") def test_parse_advisory(self): @@ -32,3 +35,98 @@ def test_parse_advisory(self): ] expected_file = self.get_test_loc("parse_advisory-expected.json", must_exist=False) util_tests.check_results_against_json(results, expected_file) + + @mock.patch.dict(os.environ, {}, clear=True) + @mock.patch("requests.post") + def test_fetch_json_response_unauthenticated(self, mock_post): + mock_response = mock.MagicMock() + mock_response.json.return_value = [{"coordinates": "pkg:npm/foo@1.0"}] + mock_post.return_value = mock_response + + ds = oss_index.SonatypeGuideDataSource() + result = ds.fetch_json_response(["pkg:npm/foo@1.0"]) + + mock_post.assert_called_once_with( + "https://api.guide.sonatype.com/api/v3/component-report", + auth=None, + json={"coordinates": ["pkg:npm/foo@1.0"]}, + ) + assert result == [{"coordinates": "pkg:npm/foo@1.0"}] + + @mock.patch.dict(os.environ, {"SONATYPE_GUIDE_TOKEN": "test-pat-123"}, clear=True) + @mock.patch("requests.post") + def test_fetch_json_response_with_pat_only(self, mock_post): + mock_response = mock.MagicMock() + mock_response.json.return_value = [{"coordinates": "pkg:npm/foo@1.0"}] + mock_post.return_value = mock_response + + ds = oss_index.SonatypeGuideDataSource() + result = ds.fetch_json_response(["pkg:npm/foo@1.0"]) + + mock_post.assert_called_once_with( + "https://api.guide.sonatype.com/api/v3/authorized/component-report", + auth=("token", "test-pat-123"), + json={"coordinates": ["pkg:npm/foo@1.0"]}, + ) + assert result == [{"coordinates": "pkg:npm/foo@1.0"}] + + @mock.patch.dict( + os.environ, + {"SONATYPE_GUIDE_USERNAME": "developer@example.com", "SONATYPE_GUIDE_TOKEN": "test-pat-123"}, + clear=True, + ) + @mock.patch("requests.post") + def test_fetch_json_response_with_username_and_pat(self, mock_post): + mock_response = mock.MagicMock() + mock_response.json.return_value = [{"coordinates": "pkg:npm/foo@1.0"}] + mock_post.return_value = mock_response + + ds = oss_index.SonatypeGuideDataSource() + result = ds.fetch_json_response(["pkg:npm/foo@1.0"]) + + mock_post.assert_called_once_with( + "https://api.guide.sonatype.com/api/v3/authorized/component-report", + auth=("developer@example.com", "test-pat-123"), + json={"coordinates": ["pkg:npm/foo@1.0"]}, + ) + assert result == [{"coordinates": "pkg:npm/foo@1.0"}] + + @mock.patch.dict( + os.environ, + {"OSS_USERNAME": "legacy@example.com", "OSS_TOKEN": "legacy-token"}, + clear=True, + ) + @mock.patch("requests.post") + def test_fetch_json_response_legacy_env_fallback(self, mock_post): + mock_response = mock.MagicMock() + mock_response.json.return_value = [{"coordinates": "pkg:npm/foo@1.0"}] + mock_post.return_value = mock_response + + ds = oss_index.OSSDataSource() + result = ds.fetch_json_response(["pkg:npm/foo@1.0"]) + + mock_post.assert_called_once_with( + "https://api.guide.sonatype.com/api/v3/authorized/component-report", + auth=("legacy@example.com", "legacy-token"), + json={"coordinates": ["pkg:npm/foo@1.0"]}, + ) + assert result == [{"coordinates": "pkg:npm/foo@1.0"}] + + @mock.patch.dict(os.environ, {}, clear=True) + @mock.patch("requests.post") + def test_fetch_json_response_http_errors(self, mock_post): + for status_code in (401, 429, 500): + mock_response = mock.MagicMock() + mock_response.status_code = status_code + error = requests.exceptions.HTTPError(response=mock_response) + mock_response.raise_for_status.side_effect = error + mock_post.return_value = mock_response + mock_post.side_effect = None + + ds = oss_index.SonatypeGuideDataSource() + assert ds.fetch_json_response(["pkg:npm/foo@1.0"]) is None + + def test_datasource_advisory_unsupported_purl(self): + ds = oss_index.SonatypeGuideDataSource() + unsupported = PackageURL.from_string("pkg:unsupported/foo@1.0") + assert ds.datasource_advisory(unsupported) is None diff --git a/vulntotal/vulntotal_cli.py b/vulntotal/vulntotal_cli.py index d60ceeb85..eea16ca1c 100755 --- a/vulntotal/vulntotal_cli.py +++ b/vulntotal/vulntotal_cli.py @@ -579,6 +579,7 @@ def get_range_from_discrete_version_string(schema, versions): "github": build_range_from_github_advisory_constraint, "gitlab": from_gitlab_native, "oss_index": None, + "sonatype_guide": None, "osv": get_range_from_discrete_version_string, "snyk": build_range_from_snyk_advisory_string, "safetydb": build_range_from_snyk_advisory_string,