Skip to content

Commit 20155c3

Browse files
committed
chore: address PR feedback
Signed-off-by: behnazh-w <[email protected]>
1 parent 079f592 commit 20155c3

File tree

6 files changed

+33
-22
lines changed

6 files changed

+33
-22
lines changed

src/macaron/config/defaults.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ inspector_url_scheme = https
543543
[deps_dev]
544544
url_netloc = api.deps.dev
545545
url_scheme = https
546-
v3alpha_purl_endpoint = v3alpha/purl
546+
purl_endpoint = v3alpha/purl
547547

548548
# Configuration options for selecting the checks to run.
549549
# Both the exclude and include are defined as list of strings:

src/macaron/errors.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 2025, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module contains error classes for Macaron."""
@@ -56,6 +56,17 @@ class InvalidHTTPResponseError(MacaronError):
5656
"""Happens when the HTTP response is invalid or unexpected."""
5757

5858

59+
class APIAccessError(MacaronError):
60+
"""Happens when a service API cannot be accessed.
61+
62+
Reasons can include:
63+
* misconfiguration issues
64+
* invalid API request
65+
* network errors
66+
* unexpected response returned by the API
67+
"""
68+
69+
5970
class CheckRegistryError(MacaronError):
6071
"""The Check Registry Error class."""
6172

src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from macaron.database.db_custom_types import DBJsonDict
1313
from macaron.database.table_definitions import CheckFacts
14-
from macaron.errors import HeuristicAnalyzerValueError, InvalidHTTPResponseError
14+
from macaron.errors import HeuristicAnalyzerValueError
1515
from macaron.json_tools import JsonType, json_extract
1616
from macaron.malware_analyzer.pypi_heuristics.base_analyzer import BaseHeuristicAnalyzer
1717
from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult, Heuristics
@@ -28,7 +28,7 @@
2828
from macaron.slsa_analyzer.build_tool.poetry import Poetry
2929
from macaron.slsa_analyzer.checks.base_check import BaseCheck
3030
from macaron.slsa_analyzer.checks.check_result import CheckResultData, CheckResultType, Confidence, JustificationType
31-
from macaron.slsa_analyzer.package_registry.deps_dev import DepsDevService
31+
from macaron.slsa_analyzer.package_registry.deps_dev import APIAccessError, DepsDevService
3232
from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIPackageJsonAsset, PyPIRegistry
3333
from macaron.slsa_analyzer.registry import registry
3434
from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo
@@ -270,7 +270,7 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData:
270270

271271
try:
272272
package_exists = bool(DepsDevService.get_package_info(ctx.component.purl))
273-
except InvalidHTTPResponseError as error:
273+
except APIAccessError as error:
274274
logger.debug(error)
275275

276276
# Known malicious packages must have been removed.

src/macaron/slsa_analyzer/package_registry/deps_dev.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from urllib.parse import quote as encode
1111

1212
from macaron.config.defaults import defaults
13-
from macaron.errors import ConfigurationError, InvalidHTTPResponseError
13+
from macaron.errors import APIAccessError
1414
from macaron.util import send_get_http_raw
1515

1616
logger: logging.Logger = logging.getLogger(__name__)
@@ -35,8 +35,9 @@ def get_package_info(purl: str) -> dict | None:
3535
3636
Raises
3737
------
38-
InvalidHTTPResponseError
39-
If a network error happens or unexpected response is returned by the API.
38+
APIAccessError
39+
If the service is misconfigured, the API is invalid, a network error happens,
40+
or unexpected response is returned by the API.
4041
"""
4142
section_name = "deps_dev"
4243
if not defaults.has_section(section_name):
@@ -45,17 +46,17 @@ def get_package_info(purl: str) -> dict | None:
4546

4647
url_netloc = section.get("url_netloc")
4748
if not url_netloc:
48-
raise ConfigurationError(
49+
raise APIAccessError(
4950
f'The "url_netloc" key is missing in section [{section_name}] of the .ini configuration file.'
5051
)
5152
url_scheme = section.get("url_scheme", "https")
52-
v3alpha_purl_endpoint = section.get("v3alpha_purl_endpoint")
53-
if not v3alpha_purl_endpoint:
54-
raise ConfigurationError(
55-
f'The "v3alpha_purl_endpoint" key is missing in section [{section_name}] of the .ini configuration file.'
53+
purl_endpoint = section.get("purl_endpoint")
54+
if not purl_endpoint:
55+
raise APIAccessError(
56+
f'The "purl_endpoint" key is missing in section [{section_name}] of the .ini configuration file.'
5657
)
5758

58-
path_params = "/".join([v3alpha_purl_endpoint, encode(purl, safe="")])
59+
path_params = "/".join([purl_endpoint, encode(purl, safe="")])
5960
try:
6061
url = urllib.parse.urlunsplit(
6162
urllib.parse.SplitResult(
@@ -67,16 +68,16 @@ def get_package_info(purl: str) -> dict | None:
6768
)
6869
)
6970
except ValueError as error:
70-
raise InvalidHTTPResponseError("Failed to construct the API URL.") from error
71+
raise APIAccessError("Failed to construct the API URL.") from error
7172

7273
response = send_get_http_raw(url)
7374
if response and response.text:
7475
try:
7576
metadata: dict = json.loads(response.text)
7677
except JSONDecodeError as error:
77-
raise InvalidHTTPResponseError(f"Failed to process response from deps.dev for {url}.") from error
78+
raise APIAccessError(f"Failed to process response from deps.dev for {url}.") from error
7879
if not metadata:
79-
raise InvalidHTTPResponseError(f"Empty response returned by {url} .")
80+
raise APIAccessError(f"Empty response returned by {url} .")
8081
return metadata
8182

8283
return None

src/macaron/slsa_analyzer/package_registry/package_registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from macaron.errors import InvalidHTTPResponseError
1111
from macaron.json_tools import json_extract
1212
from macaron.slsa_analyzer.build_tool.base_build_tool import BaseBuildTool
13-
from macaron.slsa_analyzer.package_registry.deps_dev import DepsDevService
13+
from macaron.slsa_analyzer.package_registry.deps_dev import APIAccessError, DepsDevService
1414

1515
logger: logging.Logger = logging.getLogger(__name__)
1616

@@ -81,7 +81,7 @@ def find_publish_timestamp(self, purl: str) -> datetime:
8181
# is available for subsequent processing.
8282
try:
8383
metadata = DepsDevService.get_package_info(purl)
84-
except InvalidHTTPResponseError as error:
84+
except APIAccessError as error:
8585
raise InvalidHTTPResponseError(f"Invalid response from deps.dev for {purl}.") from error
8686
if metadata:
8787
timestamp = json_extract(metadata, ["version", "publishedAt"], str)

tests/slsa_analyzer/package_registry/test_deps_dev.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
from werkzeug import Response
1313

1414
from macaron.config.defaults import load_defaults
15-
from macaron.errors import InvalidHTTPResponseError
16-
from macaron.slsa_analyzer.package_registry.deps_dev import DepsDevService
15+
from macaron.slsa_analyzer.package_registry.deps_dev import APIAccessError, DepsDevService
1716

1817

1918
@pytest.mark.parametrize(
@@ -65,5 +64,5 @@ def test_get_package_info_exception(httpserver: HTTPServer, tmp_path: Path) -> N
6564
purl = "pkg%3Apypi%2Fexample"
6665
httpserver.expect_request(f"/v3alpha/purl/{purl}").respond_with_data("Not Valid")
6766

68-
with pytest.raises(InvalidHTTPResponseError):
67+
with pytest.raises(APIAccessError):
6968
DepsDevService.get_package_info(purl)

0 commit comments

Comments
 (0)