Skip to content

Commit 2fcc15d

Browse files
committed
chore: minor fixes
Signed-off-by: Ben Selwyn-Smith <[email protected]>
1 parent 76ec567 commit 2fcc15d

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

src/macaron/repo_finder/repo_finder.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ def find_repo(
111111
logger.debug("Analyzing %s with Repo Finder: %s", purl, type(repo_finder))
112112
found_repo, outcome = repo_finder.find_repo(purl)
113113

114+
print(package_registries_info)
115+
114116
if not found_repo:
115117
found_repo, outcome = find_repo_alternative(purl, outcome, package_registries_info)
116118

@@ -166,7 +168,12 @@ def find_repo_alternative(
166168
found_repo, outcome = repo_finder_pypi.find_repo(purl, package_registries_info)
167169

168170
if not found_repo:
169-
logger.debug("Could not find repository using type specific (%s) methods for PURL: %s", purl.type, purl)
171+
logger.debug(
172+
"Could not find repository using type specific (%s) methods for PURL %s. Outcome: %s",
173+
purl.type,
174+
purl,
175+
outcome,
176+
)
170177

171178
return found_repo, outcome
172179

src/macaron/repo_finder/repo_finder_pypi.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
from macaron.repo_finder.repo_finder_enums import RepoFinderInfo
1010
from macaron.repo_finder.repo_validator import find_valid_repository_url
11-
from macaron.slsa_analyzer.package_registry import PyPIRegistry
12-
from macaron.slsa_analyzer.package_registry.pypi_registry import find_or_create_pypi_asset
11+
from macaron.slsa_analyzer.package_registry import PACKAGE_REGISTRIES, PyPIRegistry
12+
from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIPackageJsonAsset, find_or_create_pypi_asset
1313
from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo
1414

1515
logger: logging.Logger = logging.getLogger(__name__)
@@ -44,14 +44,21 @@ def find_repo(
4444
),
4545
None,
4646
)
47-
48-
if not pypi_info:
49-
return "", RepoFinderInfo.PYPI_NO_REGISTRY
47+
if not pypi_info:
48+
return "", RepoFinderInfo.PYPI_NO_REGISTRY
5049

5150
if not purl.version:
5251
return "", RepoFinderInfo.NO_VERSION_PROVIDED
5352

54-
pypi_asset = find_or_create_pypi_asset(purl.name, purl.version, pypi_info)
53+
# Create the asset.
54+
if pypi_info:
55+
pypi_asset = find_or_create_pypi_asset(purl.name, purl.version, pypi_info)
56+
else:
57+
# If this function has been reached via find-source, we do not store the asset.
58+
pypi_registry = next((registry for registry in PACKAGE_REGISTRIES if isinstance(registry, PyPIRegistry)), None)
59+
if not pypi_registry:
60+
return "", RepoFinderInfo.PYPI_NO_REGISTRY
61+
pypi_asset = PyPIPackageJsonAsset(purl.name, purl.version, False, pypi_registry, {})
5562

5663
if not pypi_asset:
5764
# This should be unreachable, as the pypi_registry has already been confirmed to be of type PyPIRegistry.

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@
7676
from macaron.slsa_analyzer.package_registry import PACKAGE_REGISTRIES, MavenCentralRegistry, PyPIRegistry
7777
from macaron.slsa_analyzer.package_registry.pypi_registry import find_or_create_pypi_asset
7878
from macaron.slsa_analyzer.provenance.expectations.expectation_registry import ExpectationRegistry
79-
from macaron.slsa_analyzer.provenance.intoto import InTotoPayload, InTotoV01Payload
80-
from macaron.slsa_analyzer.provenance.loader import load_provenance_payload
79+
from macaron.slsa_analyzer.provenance.intoto import (
80+
InTotoPayload,
81+
InTotoV01Payload,
82+
ValidateInTotoPayloadError,
83+
validate_intoto_payload,
84+
)
85+
from macaron.slsa_analyzer.provenance.loader import decode_provenance
8186
from macaron.slsa_analyzer.provenance.slsa import SLSAProvenanceData
8287
from macaron.slsa_analyzer.registry import registry
8388
from macaron.slsa_analyzer.specs.ci_spec import CIInfo
@@ -1102,9 +1107,13 @@ def get_github_attestation_payload(
11021107
if not git_attestation_list:
11031108
return None
11041109

1105-
git_attestation: str = git_attestation_list[0]
1110+
payload = decode_provenance(git_attestation_list[0])
11061111

1107-
return load_provenance_payload(git_attestation)
1112+
try:
1113+
return validate_intoto_payload(payload)
1114+
except ValidateInTotoPayloadError as error:
1115+
logger.debug("Invalid attestation payload: %s", error)
1116+
return None
11081117

11091118
def _determine_git_service(self, analyze_ctx: AnalyzeContext) -> BaseGitService:
11101119
"""Determine the Git service used by the software component."""

src/macaron/slsa_analyzer/provenance/loader.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,33 @@ def _load_provenance_file_content(
8080
try:
8181
decompressed_file_content = gzip.decompress(file_content)
8282
decoded_file_content = decompressed_file_content.decode()
83-
provenance = json.loads(decoded_file_content)
83+
return decode_provenance(json.loads(decoded_file_content))
8484
except (gzip.BadGzipFile, EOFError, zlib.error):
8585
decoded_file_content = file_content.decode()
86-
provenance = json.loads(decoded_file_content)
86+
return decode_provenance(json.loads(decoded_file_content))
8787
except (json.JSONDecodeError, TypeError, UnicodeDecodeError) as error:
8888
raise LoadIntotoAttestationError(
8989
"Cannot deserialize the file content as JSON.",
9090
) from error
9191

92+
93+
def decode_provenance(provenance: dict) -> dict[str, JsonType]:
94+
"""Find and decode the provenance payload.
95+
96+
Parameters
97+
----------
98+
provenance: dict
99+
The contents of the provenance from which the payload will be decoded.
100+
101+
Returns
102+
-------
103+
The decoded payload.
104+
105+
Raises
106+
------
107+
LoadIntotoAttestationError
108+
If the payload could not be decoded.
109+
"""
92110
# The GitHub Attestation stores the DSSE envelope in `dsseEnvelope` property.
93111
dsse_envelope = provenance.get("dsseEnvelope", None)
94112
if dsse_envelope:

tests/integration/cases/github_pypi_attestation/policy.dl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Policy("test_policy", component_id, "") :-
77
check_passed(component_id, "mcn_provenance_available_1").
88

99
apply_policy_to("test_policy", component_id) :-
10-
is_component(component_id, "pkg:pypi/toga@0.5.0").
10+
is_component(component_id, "pkg:pypi/toga@0.4.8").

0 commit comments

Comments
 (0)