Skip to content

Commit

Permalink
chore: add SLSA version value to Provenance table
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Selwyn-Smith <[email protected]>
  • Loading branch information
benmss committed Jan 14, 2025
1 parent 7460c86 commit 4eafea1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/macaron/database/table_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class Provenance(ORMBase):
component: Mapped["Component"] = relationship(back_populates="provenance")

#: The SLSA version.
version: Mapped[str] = mapped_column(String, nullable=False)
version: Mapped[str] = mapped_column(String, nullable=True)

#: The SLSA level.
slsa_level: Mapped[int] = mapped_column(Integer, default=0)
Expand Down
38 changes: 33 additions & 5 deletions src/macaron/provenance/provenance_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def extract_repo_and_commit_from_provenance(payload: InTotoPayload) -> tuple[str
If the extraction process fails for any reason.
"""
predicate_type = payload.statement.get("predicateType")
if isinstance(payload, InTotoV1Payload):
if predicate_type == "https://slsa.dev/provenance/v1":
return _extract_from_slsa_v1(payload)
elif isinstance(payload, InTotoV01Payload):
if isinstance(payload, InTotoV1Payload) and predicate_type == "https://slsa.dev/provenance/v1":
return _extract_from_slsa_v1(payload)

if isinstance(payload, InTotoV01Payload):
if predicate_type == "https://slsa.dev/provenance/v0.2":
return _extract_from_slsa_v02(payload)
if predicate_type == "https://slsa.dev/provenance/v0.1":
Expand All @@ -56,12 +56,40 @@ def extract_repo_and_commit_from_provenance(payload: InTotoPayload) -> tuple[str

msg = (
f"Extraction from provenance not supported for versions: "
f"predicate_type {predicate_type}, in-toto {str(type(payload))}."
f"predicate_type {payload.statement.get('predicateType')}, in-toto {str(type(payload))}."
)
logger.debug(msg)
raise ProvenanceError(msg)


def extract_predicate_version(payload: InTotoPayload) -> str | None:
"""Extract and return the SLSA version from the passed payload.
Parameters
----------
payload: InTotoPayload
The payload to extract from.
Returns
-------
str | None
The SLSA version, or None if .
"""
predicate_type = payload.statement.get("predicateType")
if isinstance(payload, InTotoV1Payload) and predicate_type == "https://slsa.dev/provenance/v1":
return "SLSA-1.0"

if isinstance(payload, InTotoV01Payload):
if predicate_type == "https://slsa.dev/provenance/v0.2":
return "SLSA-0.2"
if predicate_type == "https://slsa.dev/provenance/v0.1":
return "SLSA-0.1"
if predicate_type == "https://witness.testifysec.com/attestation-collection/v0.1":
return "WITNESS-0.1"

return None


def _extract_from_slsa_v01(payload: InTotoV01Payload) -> tuple[str | None, str | None]:
"""Extract the repository and commit metadata from the slsa v01 provenance payload."""
predicate: dict[str, JsonType] | None = payload.statement.get("predicate")
Expand Down
7 changes: 6 additions & 1 deletion src/macaron/slsa_analyzer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from macaron.provenance.provenance_extractor import (
check_if_input_purl_provenance_conflict,
check_if_input_repo_provenance_conflict,
extract_predicate_version,
extract_repo_and_commit_from_provenance,
)
from macaron.provenance.provenance_finder import ProvenanceFinder, find_provenance_from_ci
Expand Down Expand Up @@ -487,6 +488,9 @@ def run_single(
slsa_level = determine_provenance_slsa_level(
analyze_ctx, provenance_payload, provenance_is_verified, provenance_l3_verified
)
slsa_version = None
if provenance_payload:
slsa_version = extract_predicate_version(provenance_payload)

analyze_ctx.dynamic_data["provenance_info"] = table_definitions.Provenance(
component=component,
Expand All @@ -495,7 +499,8 @@ def run_single(
verified=provenance_is_verified,
provenance_payload=provenance_payload,
slsa_level=slsa_level,
# TODO Add SLSA version, release tag, release digest.
slsa_version=slsa_version,
# TODO Add release tag, release digest.
)
if provenance_payload:
analyze_ctx.dynamic_data["is_inferred_prov"] = False
Expand Down

0 comments on commit 4eafea1

Please sign in to comment.