Skip to content

Commit ea49398

Browse files
committed
chore: add pypi sha256 support
Signed-off-by: Ben Selwyn-Smith <[email protected]>
1 parent 167b34a commit ea49398

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,10 @@ def get_artifact_hash(
10651065
if not pypi_asset.download(""):
10661066
return None
10671067

1068+
artifact_hash = pypi_asset.get_sha256()
1069+
if artifact_hash:
1070+
return artifact_hash
1071+
10681072
source_url = pypi_asset.get_sourcecode_url("bdist_wheel")
10691073
if not source_url:
10701074
return None

src/macaron/slsa_analyzer/package_registry/pypi_registry.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,26 @@ def get_sourcecode(self) -> dict[str, str] | None:
543543
return source_code
544544
return None
545545

546+
def get_sha256(self) -> str | None:
547+
"""Get the sha256 hash of the artifact from its payload.
548+
549+
Returns
550+
-------
551+
str | None
552+
The sha256 hash of the artifact, or None if not found.
553+
"""
554+
if not self.package_json and not self.download(""):
555+
return None
556+
557+
if not self.component_version:
558+
artifact_hash = json_extract(self.package_json, ["urls", 0, "digests", "sha256"], str)
559+
else:
560+
artifact_hash = json_extract(
561+
self.package_json, ["releases", self.component_version, "digests", "sha256"], str
562+
)
563+
logger.debug("Found sha256 hash: %s", artifact_hash)
564+
return artifact_hash
565+
546566

547567
def find_or_create_pypi_asset(
548568
asset_name: str, asset_version: str | None, pypi_registry_info: PackageRegistryInfo

0 commit comments

Comments
 (0)