chore: publish IntegraCI build modules 732ec6c83925
This commit is contained in:
1 parent
7a84930dea
commit
5f8a24d014
133 files changed
+18990
-2
No files matched your search
@@ -0,0 +1,158 @@
|
||||
package attestation
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
digestA = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
digestB = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
|
||||
commit = "89abcdef0123456789abcdef0123456789abcdef"
|
||||
)
|
||||
|
||||
func stmt(subjectDigest, predicateType, typ string) []byte {
|
||||
b, _ := json.Marshal(map[string]any{
|
||||
"_type": typ,
|
||||
"predicateType": predicateType,
|
||||
"subject": []map[string]any{{
|
||||
"name": "registry.example/team/orders",
|
||||
"digest": map[string]string{"sha256": subjectDigest},
|
||||
}},
|
||||
"predicate": map[string]any{"buildDefinition": map[string]any{"buildType": "https://x.example/t"}},
|
||||
})
|
||||
return b
|
||||
}
|
||||
|
||||
const imageA = "registry.example/team/orders:abc@sha256:" + digestA
|
||||
|
||||
func TestPredicateFromStatementReturnsPredicate(t *testing.T) {
|
||||
p, err := PredicateFromStatement(stmt(digestA, SLSAProvenanceV1, "https://in-toto.io/Statement/v1"), imageA)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(p, &got); err != nil {
|
||||
t.Fatalf("predicate is not JSON: %v", err)
|
||||
}
|
||||
if _, ok := got["buildDefinition"]; !ok {
|
||||
t.Fatalf("predicate lost its body: %s", p)
|
||||
}
|
||||
if _, ok := got["_type"]; ok {
|
||||
t.Fatal("must return the predicate, not the whole statement")
|
||||
}
|
||||
}
|
||||
|
||||
// The reason the function exists: provenance about another digest must never
|
||||
// be attached to this image.
|
||||
func TestPredicateFromStatementRefusesOtherDigest(t *testing.T) {
|
||||
if _, err := PredicateFromStatement(stmt(digestB, SLSAProvenanceV1, "https://in-toto.io/Statement/v1"), imageA); err == nil {
|
||||
t.Fatal("a statement about digest B must not attach to image A")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPredicateFromStatementRefusals(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
raw []byte
|
||||
image string
|
||||
}{
|
||||
"tag-only image": {stmt(digestA, SLSAProvenanceV1, "https://in-toto.io/Statement/v1"), "registry.example/team/orders:abc"},
|
||||
"not json": {[]byte("{"), imageA},
|
||||
"wrong statement": {stmt(digestA, SLSAProvenanceV1, "https://example.test/Statement"), imageA},
|
||||
"wrong predicate": {stmt(digestA, "https://cyclonedx.org/bom", "https://in-toto.io/Statement/v1"), imageA},
|
||||
"no subject": {[]byte(`{"_type":"https://in-toto.io/Statement/v1","predicateType":"` + SLSAProvenanceV1 + `","subject":[],"predicate":{}}`), imageA},
|
||||
"null predicate": {[]byte(`{"_type":"https://in-toto.io/Statement/v1","predicateType":"` + SLSAProvenanceV1 + `","subject":[{"name":"x","digest":{"sha256":"` + digestA + `"}}],"predicate":null}`), imageA},
|
||||
"sha512 only subject": {[]byte(`{"_type":"https://in-toto.io/Statement/v1","predicateType":"` + SLSAProvenanceV1 + `","subject":[{"name":"x","digest":{"sha512":"` + digestA + `"}}],"predicate":{}}`), imageA},
|
||||
}
|
||||
for name, c := range cases {
|
||||
if _, err := PredicateFromStatement(c.raw, c.image); err == nil {
|
||||
t.Fatalf("%s: must be refused", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPredicateFromStatementAcceptsLegitimate(t *testing.T) {
|
||||
// in-toto v0.1 statements are still what many tools write.
|
||||
if _, err := PredicateFromStatement(stmt(digestA, SLSAProvenanceV1, "https://in-toto.io/Statement/v0.1"), imageA); err != nil {
|
||||
t.Fatalf("v0.1 statement refused: %v", err)
|
||||
}
|
||||
// An image reference with a registry port and no tag.
|
||||
if _, err := PredicateFromStatement(stmt(digestA, SLSAProvenanceV1, "https://in-toto.io/Statement/v1"),
|
||||
"registry.example:5000/team/orders@sha256:"+digestA); err != nil {
|
||||
t.Fatalf("port + digest refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func validInput() ProvenanceInput {
|
||||
return ProvenanceInput{
|
||||
BuilderID: "https://github.com/acme/app/.github/workflows/release.yml@refs/tags/v1.2.3",
|
||||
BuildType: "https://actions.github.io/buildtypes/workflow/v1",
|
||||
SourceURI: "git+https://github.com/acme/app@refs/tags/v1.2.3",
|
||||
SourceDigest: "sha256:" + commit, // what the old docstring told callers to pass
|
||||
InvocationID: "123",
|
||||
EngineVersion: "v0.21.7",
|
||||
Now: time.Date(2026, 10, 11, 8, 0, 0, 0, time.UTC),
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvenancePredicateHasNoPlaceholders(t *testing.T) {
|
||||
p, err := ProvenancePredicate(validInput())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := string(p)
|
||||
if strings.Contains(s, "${") {
|
||||
t.Fatalf("unexpanded placeholder in predicate:\n%s", s)
|
||||
}
|
||||
if strings.Contains(s, `"sha1"`) {
|
||||
t.Fatalf("commit id must be labelled gitCommit, not sha1:\n%s", s)
|
||||
}
|
||||
if !strings.Contains(s, `"gitCommit": "`+commit+`"`) {
|
||||
t.Fatalf("source commit missing:\n%s", s)
|
||||
}
|
||||
if !strings.Contains(s, `"startedOn": "2026-10-11T08:00:00Z"`) {
|
||||
t.Fatalf("defaulted timestamp missing:\n%s", s)
|
||||
}
|
||||
if strings.Contains(s, "v0.15.0") || !strings.Contains(s, "v0.21.7") {
|
||||
t.Fatalf("engine version wrong:\n%s", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvenancePredicateRefusals(t *testing.T) {
|
||||
mutate := map[string]func(*ProvenanceInput){
|
||||
"relative builder": func(in *ProvenanceInput) { in.BuilderID = "release.yml" },
|
||||
"relative type": func(in *ProvenanceInput) { in.BuildType = "workflow" },
|
||||
"empty source": func(in *ProvenanceInput) { in.SourceURI = "" },
|
||||
"short commit": func(in *ProvenanceInput) { in.SourceDigest = commit[:7] },
|
||||
"placeholder time": func(in *ProvenanceInput) { in.StartedOn = "${BUILD_STARTED_ON}" },
|
||||
"newline run id": func(in *ProvenanceInput) { in.InvocationID = "1\n2" },
|
||||
}
|
||||
for name, m := range mutate {
|
||||
in := validInput()
|
||||
m(&in)
|
||||
if _, err := ProvenancePredicate(in); err == nil {
|
||||
t.Fatalf("%s: must be refused", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvenancePredicateAcceptsLegitimate(t *testing.T) {
|
||||
ok := map[string]func(*ProvenanceInput){
|
||||
"bare commit": func(in *ProvenanceInput) { in.SourceDigest = commit },
|
||||
"sha1-prefixed": func(in *ProvenanceInput) { in.SourceDigest = "sha1:" + commit },
|
||||
"sha256 repository": func(in *ProvenanceInput) { in.SourceDigest = digestA },
|
||||
"explicit times": func(in *ProvenanceInput) {
|
||||
in.StartedOn, in.FinishedOn = "2026-10-11T08:00:00+07:00", "2026-10-11T09:00:00Z"
|
||||
},
|
||||
"no run id": func(in *ProvenanceInput) { in.InvocationID = "" },
|
||||
}
|
||||
for name, m := range ok {
|
||||
in := validInput()
|
||||
m(&in)
|
||||
if _, err := ProvenancePredicate(in); err != nil {
|
||||
t.Fatalf("%s: legitimate value refused: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user