174 lines
6.1 KiB
Go
174 lines
6.1 KiB
Go
// Package attestation holds the cosign module's pure predicate logic, standard
|
|
// library only, so it runs under a plain `go test` (the generated `dagger`
|
|
// package panics at init without a session — see db-migration/verdict/).
|
|
//
|
|
// Two jobs:
|
|
//
|
|
// - PredicateFromStatement takes the in-toto Statement standard-build's
|
|
// `evidence` wrote and returns the predicate cosign should attach — but
|
|
// only after checking the statement is about THIS image's digest. Attaching
|
|
// another build's provenance to an image is the one mistake an attestation
|
|
// must never make, and nothing downstream could tell.
|
|
// - ProvenancePredicate builds the inline SLSA v1 predicate for callers that
|
|
// do not pass a statement. It replaces a template that shipped literal
|
|
// `${BUILD_STARTED_ON}` / `${ACTOR_ID}` placeholders nothing ever
|
|
// expanded, labelled a commit id as `sha1` after stripping `sha256:`, and
|
|
// claimed `dagger v0.15.0` while every module pinned v0.21.7.
|
|
package attestation
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// SLSAProvenanceV1 is the predicate type cosign's `slsaprovenance1` maps to.
|
|
const SLSAProvenanceV1 = "https://slsa.dev/provenance/v1"
|
|
|
|
var digestRef = regexp.MustCompile(`^[^@\s]+@sha256:([0-9a-f]{64})$`)
|
|
|
|
var commitID = regexp.MustCompile(`^([0-9a-f]{40}|[0-9a-f]{64})$`)
|
|
|
|
// ImageDigest returns the hex sha256 of a digest-pinned reference.
|
|
func ImageDigest(image string) (string, error) {
|
|
m := digestRef.FindStringSubmatch(strings.TrimSpace(image))
|
|
if m == nil {
|
|
return "", fmt.Errorf("image %q is not pinned to a sha256 digest", image)
|
|
}
|
|
return m[1], nil
|
|
}
|
|
|
|
type statement struct {
|
|
Type string `json:"_type"`
|
|
PredicateType string `json:"predicateType"`
|
|
Subject []subject `json:"subject"`
|
|
Predicate json.RawMessage `json:"predicate"`
|
|
}
|
|
|
|
type subject struct {
|
|
Name string `json:"name"`
|
|
Digest map[string]string `json:"digest"`
|
|
}
|
|
|
|
// PredicateFromStatement validates an in-toto Statement against the image it
|
|
// is about to be attached to and returns its predicate bytes.
|
|
//
|
|
// Refused: a malformed statement, a statement type other than in-toto v0.1/v1,
|
|
// a predicate type other than SLSA v1 provenance, a tag-only image, and — the
|
|
// point of the function — a statement with no subject whose sha256 equals the
|
|
// image's digest.
|
|
func PredicateFromStatement(raw []byte, image string) ([]byte, error) {
|
|
want, err := ImageDigest(image)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var st statement
|
|
if err := json.Unmarshal(raw, &st); err != nil {
|
|
return nil, fmt.Errorf("statement is not valid JSON: %w", err)
|
|
}
|
|
switch st.Type {
|
|
case "https://in-toto.io/Statement/v1", "https://in-toto.io/Statement/v0.1":
|
|
default:
|
|
return nil, fmt.Errorf("statement _type %q is not an in-toto Statement", st.Type)
|
|
}
|
|
if st.PredicateType != SLSAProvenanceV1 {
|
|
return nil, fmt.Errorf("predicateType %q is not %s", st.PredicateType, SLSAProvenanceV1)
|
|
}
|
|
matched := false
|
|
for _, s := range st.Subject {
|
|
if strings.EqualFold(s.Digest["sha256"], want) {
|
|
matched = true
|
|
break
|
|
}
|
|
}
|
|
if !matched {
|
|
return nil, fmt.Errorf("statement has no subject with sha256 %s: it describes a different artifact", want)
|
|
}
|
|
p := strings.TrimSpace(string(st.Predicate))
|
|
if p == "" || p == "null" || !strings.HasPrefix(p, "{") {
|
|
return nil, fmt.Errorf("statement predicate is missing or not an object")
|
|
}
|
|
return []byte(p + "\n"), nil
|
|
}
|
|
|
|
// ProvenanceInput is what the inline predicate records. Supplied by the caller;
|
|
// nothing is read from the environment.
|
|
type ProvenanceInput struct {
|
|
BuilderID string
|
|
BuildType string
|
|
SourceURI string
|
|
SourceDigest string // full commit id; a `sha1:`/`sha256:` prefix is tolerated
|
|
InvocationID string
|
|
StartedOn string // RFC 3339; empty -> Now
|
|
FinishedOn string // RFC 3339; empty -> Now
|
|
EngineVersion string
|
|
Now time.Time
|
|
}
|
|
|
|
// ProvenancePredicate renders the SLSA v1 provenance predicate (the predicate
|
|
// only — cosign wraps it in a Statement whose subject is the image digest).
|
|
func ProvenancePredicate(in ProvenanceInput) ([]byte, error) {
|
|
for what, raw := range map[string]string{"builder id": in.BuilderID, "build type": in.BuildType} {
|
|
u, err := url.Parse(raw)
|
|
if err != nil || u.Scheme == "" || (u.Host == "" && u.Opaque == "") || strings.ContainsAny(raw, " \t\r\n") {
|
|
return nil, fmt.Errorf("%s %q must be an absolute URI", what, raw)
|
|
}
|
|
}
|
|
if strings.TrimSpace(in.SourceURI) == "" || strings.ContainsAny(in.SourceURI, " \t\r\n") {
|
|
return nil, fmt.Errorf("source uri %q must be a non-empty URI without whitespace", in.SourceURI)
|
|
}
|
|
rev := strings.ToLower(strings.TrimSpace(in.SourceDigest))
|
|
for _, prefix := range []string{"sha1:", "sha256:"} {
|
|
rev = strings.TrimPrefix(rev, prefix)
|
|
}
|
|
if !commitID.MatchString(rev) {
|
|
return nil, fmt.Errorf("source digest %q is not a full commit id (40 or 64 hex)", in.SourceDigest)
|
|
}
|
|
now := in.Now.UTC().Format(time.RFC3339)
|
|
started, finished := in.StartedOn, in.FinishedOn
|
|
if started == "" {
|
|
started = now
|
|
}
|
|
if finished == "" {
|
|
finished = now
|
|
}
|
|
for what, ts := range map[string]string{"started-on": started, "finished-on": finished} {
|
|
if _, err := time.Parse(time.RFC3339, ts); err != nil {
|
|
return nil, fmt.Errorf("%s %q is not RFC 3339", what, ts)
|
|
}
|
|
}
|
|
if strings.ContainsAny(in.InvocationID, "\r\n") {
|
|
return nil, fmt.Errorf("invocation id must be a single line")
|
|
}
|
|
|
|
predicate := map[string]any{
|
|
"buildDefinition": map[string]any{
|
|
"buildType": in.BuildType,
|
|
"externalParameters": map[string]string{"source": in.SourceURI, "revision": rev},
|
|
"resolvedDependencies": []map[string]any{{
|
|
"uri": in.SourceURI,
|
|
"digest": map[string]string{"gitCommit": rev},
|
|
}},
|
|
},
|
|
"runDetails": map[string]any{
|
|
"builder": map[string]any{
|
|
"id": in.BuilderID,
|
|
"version": map[string]string{"dagger-engine": in.EngineVersion},
|
|
},
|
|
"metadata": map[string]string{
|
|
"invocationId": in.InvocationID,
|
|
"startedOn": started,
|
|
"finishedOn": finished,
|
|
},
|
|
},
|
|
}
|
|
out, err := json.MarshalIndent(predicate, "", " ")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append(out, '\n'), nil
|
|
}
|