Skip to content

Commit ed82caa

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

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/macaron/slsa_analyzer/analyzer.py

+4
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,10 @@ def get_artifact_hash(
10661066
if not pypi_asset.download(""):
10671067
return None
10681068

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

src/macaron/slsa_analyzer/package_registry/pypi_registry.py

+20
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)