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,211 @@
|
||||
package evidence
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
hexDigest = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
commit = "89abcdef0123456789abcdef0123456789abcdef"
|
||||
)
|
||||
|
||||
func validInput() ProvenanceInput {
|
||||
return ProvenanceInput{
|
||||
ImageRef: "registry.example:5000/team/orders:abc123@sha256:" + hexDigest,
|
||||
BuilderID: "https://jenkins.example/job/orders/",
|
||||
SourceURI: "git+https://git.example/team/orders@refs/heads/main",
|
||||
SourceRevision: commit,
|
||||
InvocationID: "42",
|
||||
StartedOn: "2026-10-11T08:00:00Z",
|
||||
FinishedOn: "2026-10-11T08:05:00Z",
|
||||
EngineVersion: "v0.21.7",
|
||||
Language: "go",
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDigestRefKeepsPortStripsTag(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"registry.example:5000/team/orders:abc@sha256:" + hexDigest: "registry.example:5000/team/orders",
|
||||
"registry.example:5000/team/orders@sha256:" + hexDigest: "registry.example:5000/team/orders",
|
||||
"ghcr.io/acme/app@sha256:" + hexDigest: "ghcr.io/acme/app",
|
||||
}
|
||||
for ref, want := range cases {
|
||||
s, err := ParseDigestRef(ref)
|
||||
if err != nil {
|
||||
t.Fatalf("%q: %v", ref, err)
|
||||
}
|
||||
if s.Name != want || s.Digest["sha256"] != hexDigest {
|
||||
t.Fatalf("%q -> %+v, want name %q", ref, s, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A tag is never accepted as a subject: it moves, and a statement about a tag
|
||||
// says nothing about the bytes that get deployed.
|
||||
func TestParseDigestRefRefusesUnpinned(t *testing.T) {
|
||||
for _, ref := range []string{
|
||||
"",
|
||||
"ghcr.io/acme/app:1.0.0",
|
||||
"ghcr.io/acme/app@sha256:" + hexDigest[:63], // short
|
||||
"ghcr.io/acme/app@sha256:" + strings.ToUpper(hexDigest), // not lowercase hex
|
||||
"ghcr.io/acme/app@sha512:" + hexDigest,
|
||||
"@sha256:" + hexDigest,
|
||||
"ghcr.io/acme/app@sha256:" + hexDigest + "\nextra",
|
||||
} {
|
||||
if _, err := ParseDigestRef(ref); err == nil {
|
||||
t.Fatalf("%q must be refused", ref)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvenanceStatementShape(t *testing.T) {
|
||||
out, err := ProvenanceStatement(validInput())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var st map[string]any
|
||||
if err := json.Unmarshal(out, &st); err != nil {
|
||||
t.Fatalf("not JSON: %v", err)
|
||||
}
|
||||
if st["_type"] != StatementType || st["predicateType"] != SLSAProvenanceV1 {
|
||||
t.Fatalf("wrong types: %v / %v", st["_type"], st["predicateType"])
|
||||
}
|
||||
subj := st["subject"].([]any)[0].(map[string]any)
|
||||
if subj["name"] != "registry.example:5000/team/orders" {
|
||||
t.Fatalf("subject name %v", subj["name"])
|
||||
}
|
||||
if subj["digest"].(map[string]any)["sha256"] != hexDigest {
|
||||
t.Fatalf("subject digest %v", subj["digest"])
|
||||
}
|
||||
bd := st["predicate"].(map[string]any)["buildDefinition"].(map[string]any)
|
||||
if bd["buildType"] != DefaultBuildType {
|
||||
t.Fatalf("buildType %v", bd["buildType"])
|
||||
}
|
||||
dep := bd["resolvedDependencies"].([]any)[0].(map[string]any)
|
||||
if dep["digest"].(map[string]any)["gitCommit"] != commit {
|
||||
t.Fatalf("source dependency digest %v", dep["digest"])
|
||||
}
|
||||
// The old cosign predicate shipped literal shell placeholders that nothing
|
||||
// ever expanded. None may appear here.
|
||||
if strings.Contains(string(out), "${") {
|
||||
t.Fatalf("statement carries an unexpanded placeholder:\n%s", out)
|
||||
}
|
||||
if !strings.HasSuffix(string(out), "}\n") {
|
||||
t.Fatal("statement must end with a newline")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvenanceStatementIsDeterministic(t *testing.T) {
|
||||
a, err := ProvenanceStatement(validInput())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := ProvenanceStatement(validInput())
|
||||
if string(a) != string(b) {
|
||||
t.Fatal("same input must give the same bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvenanceStatementOmitsUnsetMetadata(t *testing.T) {
|
||||
in := validInput()
|
||||
in.InvocationID, in.StartedOn, in.FinishedOn = "", "", ""
|
||||
out, err := ProvenanceStatement(in)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(out), "metadata") {
|
||||
t.Fatalf("unset metadata must be omitted, not invented:\n%s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvenanceStatementRefusesBadInput(t *testing.T) {
|
||||
mutate := map[string]func(*ProvenanceInput){
|
||||
"tag-only image": func(in *ProvenanceInput) { in.ImageRef = "ghcr.io/acme/app:1.0.0" },
|
||||
"relative builder": func(in *ProvenanceInput) { in.BuilderID = "jenkins/job/orders" },
|
||||
"empty builder": func(in *ProvenanceInput) { in.BuilderID = "" },
|
||||
"builder whitespace": func(in *ProvenanceInput) { in.BuilderID = "https://ci.example/a b" },
|
||||
"relative build type": func(in *ProvenanceInput) { in.BuildType = "standard-build" },
|
||||
"empty source": func(in *ProvenanceInput) { in.SourceURI = "" },
|
||||
"source newline": func(in *ProvenanceInput) { in.SourceURI = "git+https://x\n\"evil\"" },
|
||||
"short revision": func(in *ProvenanceInput) { in.SourceRevision = commit[:12] },
|
||||
"branch as revision": func(in *ProvenanceInput) { in.SourceRevision = "main" },
|
||||
"bad started-on": func(in *ProvenanceInput) { in.StartedOn = "yesterday" },
|
||||
"placeholder ts": func(in *ProvenanceInput) { in.FinishedOn = "${BUILD_FINISHED_ON}" },
|
||||
"multiline run id": func(in *ProvenanceInput) { in.InvocationID = "1\n2" },
|
||||
}
|
||||
for name, m := range mutate {
|
||||
in := validInput()
|
||||
m(&in)
|
||||
if _, err := ProvenanceStatement(in); err == nil {
|
||||
t.Fatalf("%s: must be refused", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Legitimate values a strict-looking validator must not reject.
|
||||
func TestProvenanceStatementAcceptsLegitimateValues(t *testing.T) {
|
||||
ok := map[string]func(*ProvenanceInput){
|
||||
"sha256 repository commit": func(in *ProvenanceInput) { in.SourceRevision = hexDigest },
|
||||
"uppercase commit": func(in *ProvenanceInput) { in.SourceRevision = strings.ToUpper(commit) },
|
||||
"urn builder": func(in *ProvenanceInput) { in.BuilderID = "urn:integraci:builder:jenkins" },
|
||||
"ssh source": func(in *ProvenanceInput) { in.SourceURI = "git+ssh://git@git.example:2222/team/orders.git" },
|
||||
"offset timestamp": func(in *ProvenanceInput) { in.StartedOn = "2026-10-11T15:00:00+07:00" },
|
||||
"no tag before digest": func(in *ProvenanceInput) { in.ImageRef = "ghcr.io/acme/app@sha256:" + hexDigest },
|
||||
"no language": func(in *ProvenanceInput) { in.Language = "" },
|
||||
}
|
||||
for name, m := range ok {
|
||||
in := validInput()
|
||||
m(&in)
|
||||
if _, err := ProvenanceStatement(in); err != nil {
|
||||
t.Fatalf("%s: legitimate value refused: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSHA256SumsFormat(t *testing.T) {
|
||||
out, err := SHA256Sums(map[string][]byte{"b.json": []byte("b"), "a.json": []byte("")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 a.json\n" +
|
||||
"3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d b.json\n"
|
||||
if string(out) != want {
|
||||
t.Fatalf("got\n%s\nwant\n%s", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSHA256SumsRefusesPathNames(t *testing.T) {
|
||||
for _, name := range []string{"../x", "dir/x", "", ".hidden", "a b", "x\ny"} {
|
||||
if _, err := SHA256Sums(map[string][]byte{name: nil}); err == nil {
|
||||
t.Fatalf("%q must be refused", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSBOMFileName(t *testing.T) {
|
||||
if n, o, err := SBOMFileName(""); err != nil || n != "sbom.cdx.json" || o != "cyclonedx-json" {
|
||||
t.Fatalf("default: %q %q %v", n, o, err)
|
||||
}
|
||||
if n, o, err := SBOMFileName("spdx"); err != nil || n != "sbom.spdx.json" || o != "spdx-json" {
|
||||
t.Fatalf("spdx: %q %q %v", n, o, err)
|
||||
}
|
||||
if _, _, err := SBOMFileName("cyclonedx-json; rm -rf /"); err == nil {
|
||||
t.Fatal("unknown format must be refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDigestFromPushOutput(t *testing.T) {
|
||||
ref := "registry.example:5000/team/orders@sha256:" + hexDigest
|
||||
out := "logged in\n2026/10/11 pushed blob: sha256:aaaa\n" + ref + "\nPUSHED 'registry.example:5000/team/orders:abc'\ncrane exit=0\n"
|
||||
if got := DigestFromPushOutput(out); got != ref {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
if got := DigestFromPushOutput("pushed blob: sha256:" + hexDigest + "\n"); got != "" {
|
||||
t.Fatalf("a bare blob digest is not an image reference, got %q", got)
|
||||
}
|
||||
if DigestOf(ref) != "sha256:"+hexDigest || DigestOf("ghcr.io/a:1") != "" {
|
||||
t.Fatal("DigestOf")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user