Files

152 lines
5.5 KiB
Rego

# IntegraCI attestation gate (#2143 phase 2).
#
# Decides whether ONE image digest carries the supply-chain evidence a
# promotion needs. It judges the CONTENT of attestations whose signatures
# have already been verified; it never sees a signature itself.
#
# Signature verification belongs to cosign. OPA has no builtin that verifies
# a DSSE signature, so the caller runs
# cosign verify-attestation --key <pub> --type <t> <image@digest>
# and hands this policy the envelopes cosign printed. cosign prints only the
# envelopes whose signature verified, so "present in input.envelopes" means
# "signed by the key the caller trusted". The cosign module's
# `verify-attestation-gate` function is that caller; a copy of this file is
# embedded there (infra/dagger/cosign/policy/attestation_gate.rego, kept
# byte-identical by apps/api/tests/test_supply_chain_phase2.py).
#
# The payload is DECODED and matched field by field. A substring search over
# the raw cosign output, which is what `cosign verify-provenance` does today,
# would accept a statement about another digest that merely mentions this one.
#
# Input:
# {
# "image": { "digest": "sha256:<64 hex>" },
# "envelopes": [ # cosign verify-attestation output lines
# { "payloadType": "application/vnd.in-toto+json",
# "payload": "<base64 in-toto Statement>", "signatures": [...] }
# ],
# "policy": {
# "trusted_builders": ["https://ci.example/job/orders/"], # exact builder.id
# "require_sbom": false
# }
# }
#
# Every gap DENIES: no digest, no trusted builder configured, no provenance
# for this digest, provenance from an untrusted builder, provenance that pins
# no commit, and (when required) no SBOM for this digest.
#
# Run: opa test infra/policies/bundles/attestation-gate -v
# METADATA
# title: Attestation gate
# description: |
# CI-side check that a signature-verified SLSA v1 provenance statement about
# the image digest, from a trusted builder, is attached. Evaluated by the
# cosign module's verify-attestation-gate inside the pipeline; NOT evaluated
# at promotion (nothing in the deploy gate queries this package yet).
package integraci.supply_chain.attestation_gate
import rego.v1
intoto_payload_type := "application/vnd.in-toto+json"
# cosign `attest --predicate` wraps the predicate in a Statement it builds
# itself, and cosign 2.x may emit v0.1 rather than v1. Both carry the same
# subject/predicateType/predicate fields; anything else is not in-toto.
statement_types := {"https://in-toto.io/Statement/v1", "https://in-toto.io/Statement/v0.1"}
slsa_provenance_v1 := "https://slsa.dev/provenance/v1"
sbom_predicate_types := {"https://cyclonedx.org/bom", "https://spdx.dev/Document"}
# ── The digest under judgement ──────────────────────────────────────────────
digest_hex := hex if {
regex.match(`^sha256:[0-9a-f]{64}$`, input.image.digest)
hex := substring(input.image.digest, 7, -1)
}
deny contains "image digest must be sha256:<64 lowercase hex>; a tag is not evidence" if {
not digest_hex
}
# ── Decoded statements ──────────────────────────────────────────────────────
#
# An envelope that is not in-toto, is not base64, or is not JSON is skipped,
# not trusted: a builtin error leaves the comprehension element undefined.
statements contains st if {
some env in input.envelopes
env.payloadType == intoto_payload_type
st := json.unmarshal(base64.decode(env.payload))
st._type in statement_types
}
about_digest(st) if {
some subject in st.subject
subject.digest.sha256 == digest_hex
}
provenance_for_digest contains st if {
some st in statements
st.predicateType == slsa_provenance_v1
about_digest(st)
}
trusted_builders := {b | some b in input.policy.trusted_builders; is_string(b); b != ""}
pins_commit(st) if {
some dep in st.predicate.buildDefinition.resolvedDependencies
regex.match(`^([0-9a-f]{40}|[0-9a-f]{64})$`, dep.digest.gitCommit)
}
trusted_provenance contains st if {
some st in provenance_for_digest
st.predicate.runDetails.builder.id in trusted_builders
pins_commit(st)
}
# ── Gates ───────────────────────────────────────────────────────────────────
deny contains "no trusted builder is configured; refusing to trust any provenance" if {
count(trusted_builders) == 0
}
deny contains msg if {
digest_hex
count(provenance_for_digest) == 0
msg := sprintf("no signature-verified SLSA v1 provenance names %s", [input.image.digest])
}
deny contains msg if {
digest_hex
count(trusted_builders) > 0
count(provenance_for_digest) > 0
count(trusted_provenance) == 0
builders := sort({b | some st in provenance_for_digest; b := object.get(st, ["predicate", "runDetails", "builder", "id"], "<none>")})
msg := sprintf(
"provenance for %s comes from an untrusted builder or pins no full commit: %v",
[input.image.digest, builders],
)
}
sbom_for_digest contains st if {
some st in statements
st.predicateType in sbom_predicate_types
about_digest(st)
}
deny contains msg if {
input.policy.require_sbom == true
digest_hex
count(sbom_for_digest) == 0
msg := sprintf("no signature-verified SBOM attestation names %s", [input.image.digest])
}
allow if count(deny) == 0
result := {
"allowed": count(deny) == 0,
"deny": sort(deny),
}