Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vulntotal/datasources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 18 additions & 9 deletions vulntotal/datasources/oss_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
100 changes: 99 additions & 1 deletion vulntotal/tests/test_oss_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
#

import json
import os
from pathlib import Path
from unittest import mock

import requests
from commoncode import testcase
from packageurl import PackageURL

from vulnerabilities.tests import util_tests
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):
Expand All @@ -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
1 change: 1 addition & 0 deletions vulntotal/vulntotal_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down