Skip to content

Commit ed9c77d

Browse files
author
Alistair Michael
committed
fix: Use NamedTuple and comment result enum
1 parent 155b695 commit ed9c77d

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/macaron/slsa_analyzer/checks/provenance_l3_check.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from dataclasses import dataclass
1515
from enum import Enum
1616
from pathlib import Path
17+
from typing import NamedTuple
1718

1819
from macaron.config.defaults import defaults
1920
from macaron.config.global_config import global_config
@@ -32,10 +33,15 @@
3233
class _VerifyArtefactResultType(Enum):
3334
"""Result of attempting to verify an asset."""
3435

36+
# slsa-verifier succeeded and the artefact passed verification
3537
PASSED = "verify passed"
38+
# slsa-verifier succeeded and the artefact failed verification
3639
FAILED = "verify failed"
40+
# An error occured running slsa-verifier or downloading the artefact
3741
ERROR = "verify error"
42+
# The artefact was unable to be downloaded because the url was missing or malformed
3843
NO_DOWNLOAD = "unable to download asset"
44+
# The artefact was unable to be downloaded because the file was too large
3945
TOO_LARGE = "asset file too large to download"
4046

4147

@@ -230,7 +236,15 @@ def run_check(self, ctx: AnalyzeContext, check_result: CheckResult) -> CheckResu
230236
"""
231237
# TODO: During verification, we need to fetch the workflow and verify that it's not
232238
# using self-hosted runners, custom containers or services, etc.
233-
all_feedback: list[tuple[str, str, _VerifyArtefactResult]] = []
239+
240+
class Feedback(NamedTuple):
241+
"""Store feedback item."""
242+
243+
ci_service_name: str
244+
asset_url: str
245+
verify_result: _VerifyArtefactResult
246+
247+
all_feedback: list[Feedback] = []
234248
ci_services = ctx.dynamic_data["ci_services"]
235249
for ci_info in ci_services:
236250
ci_service = ci_info["service"]
@@ -283,10 +297,10 @@ def run_check(self, ctx: AnalyzeContext, check_result: CheckResult) -> CheckResu
283297
if not sub_asset:
284298
logger.info("Could not find provenance subject %s. Skip verifying...", subject)
285299
all_feedback.append(
286-
(
287-
ci_service.name,
288-
prov_asset["url"],
289-
_VerifyArtefactResult(
300+
Feedback(
301+
ci_service_name=ci_service.name,
302+
asset_url=prov_asset["url"],
303+
verify_result=_VerifyArtefactResult(
290304
result=_VerifyArtefactResultType.NO_DOWNLOAD, artefact_name=subject["name"]
291305
),
292306
)
@@ -299,10 +313,10 @@ def run_check(self, ctx: AnalyzeContext, check_result: CheckResult) -> CheckResu
299313
"Skip verifying the artifact %s: asset size too large.", sub_asset["name"]
300314
)
301315
all_feedback.append(
302-
(
303-
ci_service.name,
304-
prov_asset["url"],
305-
_VerifyArtefactResult(
316+
Feedback(
317+
ci_service_name=ci_service.name,
318+
asset_url=prov_asset["url"],
319+
verify_result=_VerifyArtefactResult(
306320
result=_VerifyArtefactResultType.TOO_LARGE,
307321
artefact_name=sub_asset["name"],
308322
),
@@ -315,10 +329,10 @@ def run_check(self, ctx: AnalyzeContext, check_result: CheckResult) -> CheckResu
315329
):
316330
logger.info("Could not download artifact %s. Skip verifying...", sub_asset["name"])
317331
all_feedback.append(
318-
(
319-
ci_service.name,
320-
prov_asset["url"],
321-
_VerifyArtefactResult(
332+
Feedback(
333+
ci_service_name=ci_service.name,
334+
asset_url=prov_asset["url"],
335+
verify_result=_VerifyArtefactResult(
322336
result=_VerifyArtefactResultType.NO_DOWNLOAD,
323337
artefact_name=sub_asset["name"],
324338
),
@@ -329,7 +343,11 @@ def run_check(self, ctx: AnalyzeContext, check_result: CheckResult) -> CheckResu
329343
feedback = self._verify_slsa(
330344
ctx.macaron_path, temp_path, prov_asset, sub_asset["name"], ctx.remote_path
331345
)
332-
all_feedback.append((ci_service.name, prov_asset["url"], feedback))
346+
all_feedback.append(
347+
Feedback(
348+
ci_service_name=ci_service.name, asset_url=prov_asset["url"], verify_result=feedback
349+
)
350+
)
333351
if feedback.result != _VerifyArtefactResultType.PASSED:
334352
logger.info("Could not verify SLSA Level three integrity for: %s.", sub_asset["name"])
335353

@@ -346,7 +364,7 @@ def run_check(self, ctx: AnalyzeContext, check_result: CheckResult) -> CheckResu
346364

347365
result_value = CheckResultType.FAILED
348366
if all_feedback:
349-
all_results = [result for _, _, result in all_feedback]
367+
all_results = [feedback.verify_result for feedback in all_feedback]
350368
failed = [
351369
result
352370
for ci_name, prov_url, result in all_feedback

0 commit comments

Comments
 (0)