Skip to content

Commit 4dbae78

Browse files
authored
chore: remove usages of hard-coded provenance build types (#1014)
Signed-off-by: Ben Selwyn-Smith <[email protected]>
1 parent f275e46 commit 4dbae78

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/macaron/provenance/provenance_extractor.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,38 +149,33 @@ def _extract_from_slsa_v1(payload: InTotoV1Payload) -> tuple[str | None, str | N
149149
logger.debug("No predicate in payload statement.")
150150
return None, None
151151

152-
build_def = json_extract(predicate, ["buildDefinition"], dict)
153-
if not build_def:
154-
return None, None
155-
156-
build_type = json_extract(build_def, ["buildType"], str)
157-
if not build_type:
158-
return None, None
152+
build_def = ProvenancePredicate.find_build_def(payload.statement)
159153

160154
# Extract the repository URL.
161-
match build_type:
162-
case "https://slsa-framework.github.io/gcb-buildtypes/triggered-build/v1":
163-
repo = json_extract(build_def, ["externalParameters", "sourceToBuild", "repository"], str)
164-
if not repo:
165-
repo = json_extract(build_def, ["externalParameters", "configSource", "repository"], str)
166-
case "https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1":
167-
repo = json_extract(build_def, ["externalParameters", "workflow", "repository"], str)
168-
case "https://github.com/oracle/macaron/tree/main/src/macaron/resources/provenance-buildtypes/oci/v1":
169-
repo = json_extract(build_def, ["externalParameters", "source"], str)
170-
case _:
171-
logger.debug("Unsupported build type for SLSA v1: %s", build_type)
172-
return None, None
155+
if isinstance(build_def, SLSAGCBBuildDefinitionV1):
156+
repo = json_extract(predicate, ["buildDefinition", "externalParameters", "sourceToBuild", "repository"], str)
157+
if not repo:
158+
repo = json_extract(predicate, ["buildDefinition", "externalParameters", "configSource", "repository"], str)
159+
elif isinstance(build_def, SLSAGithubActionsBuildDefinitionV1):
160+
repo = json_extract(predicate, ["buildDefinition", "externalParameters", "workflow", "repository"], str)
161+
elif isinstance(build_def, SLSAOCIBuildDefinitionV1):
162+
repo = json_extract(predicate, ["buildDefinition", "externalParameters", "source"], str)
163+
else:
164+
logger.debug("Unsupported build type for SLSA v1: %s", type(build_def))
165+
return None, None
173166

174167
if not repo:
175168
logger.debug("Repo URL not found in SLSA v1 payload.")
176169
return None, None
177170

178171
# Extract the commit hash.
179172
commit = None
180-
if build_type == "https://github.com/oracle/macaron/tree/main/src/macaron/resources/provenance-buildtypes/oci/v1":
181-
commit = json_extract(build_def, ["internalParameters", "buildEnvVar", "BLD_COMMIT_HASH"], str)
173+
if isinstance(build_def, SLSAOCIBuildDefinitionV1):
174+
commit = json_extract(
175+
predicate, ["buildDefinition", "internalParameters", "buildEnvVar", "BLD_COMMIT_HASH"], str
176+
)
182177
else:
183-
deps = json_extract(build_def, ["resolvedDependencies"], list)
178+
deps = json_extract(predicate, ["buildDefinition", "resolvedDependencies"], list)
184179
if not deps:
185180
return repo, None
186181
for dep in deps:

0 commit comments

Comments
 (0)