Skip to content

Commit 13894e3

Browse files
committed
updated comments
Signed-off-by: Asra Ali <[email protected]>
1 parent 8e3d3d0 commit 13894e3

20 files changed

+251
-177
lines changed

cli/slsa-verifier/main.go

+25-19
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ import (
99
"io"
1010
"os"
1111

12-
"github.com/slsa-framework/slsa-verifier/container"
1312
"github.com/slsa-framework/slsa-verifier/options"
1413
"github.com/slsa-framework/slsa-verifier/verifiers"
14+
"github.com/slsa-framework/slsa-verifier/verifiers/container"
1515
)
1616

1717
var (
18-
provenancePath string
19-
builderID string
20-
artifactPath string
21-
artifactReference string
22-
source string
23-
branch string
24-
tag string
25-
versiontag string
26-
printProvenance bool
18+
provenancePath string
19+
builderID string
20+
artifactPath string
21+
artifactImage string
22+
source string
23+
branch string
24+
tag string
25+
versiontag string
26+
printProvenance bool
2727
)
2828

2929
func main() {
3030
flag.StringVar(&builderID, "builder-id", "", "EXPERIMENTAL: the unique builder ID who created the provenance")
3131
flag.StringVar(&provenancePath, "provenance", "", "path to a provenance file")
3232
flag.StringVar(&artifactPath, "artifact-path", "", "path to an artifact to verify")
33-
flag.StringVar(&artifactReference, "artifact-reference", "", "reference to an OCI image to verify")
33+
flag.StringVar(&artifactImage, "artifact-image", "", "name of the OCI image to verify")
3434
flag.StringVar(&source, "source", "",
3535
"expected source repository that should have produced the binary, e.g. github.com/some/repo")
3636
flag.StringVar(&branch, "branch", "", "[optional] expected branch the binary was compiled from")
@@ -41,8 +41,14 @@ func main() {
4141
"print the verified provenance to std out")
4242
flag.Parse()
4343

44-
if (provenancePath == "" || artifactPath == "") && artifactReference == "" {
45-
fmt.Fprintf(os.Stderr, "either 'provenance' and 'artifact-path' or '' must be specified\n")
44+
if (provenancePath == "" || artifactPath == "") && artifactImage == "" {
45+
fmt.Fprintf(os.Stderr, "either 'provenance' and 'artifact-path' or 'artifact-image' must be specified\n")
46+
flag.Usage()
47+
os.Exit(1)
48+
}
49+
50+
if artifactImage != "" && (provenancePath != "" || artifactPath != "") {
51+
fmt.Fprintf(os.Stderr, "'provenance' and 'artifact-path' should not be specified when 'artifact-image' is provided\n")
4652
flag.Usage()
4753
os.Exit(1)
4854
}
@@ -73,7 +79,7 @@ func main() {
7379
os.Exit(1)
7480
}
7581

76-
verifiedProvenance, _, err := runVerify(artifactReference, artifactPath, provenancePath,
82+
verifiedProvenance, _, err := runVerify(artifactImage, artifactPath, provenancePath,
7783
source, pbranch, pbuilderID, ptag, pversiontag)
7884
if err != nil {
7985
fmt.Fprintf(os.Stderr, "FAILED: SLSA verification failed: %v\n", err)
@@ -98,13 +104,13 @@ func isFlagPassed(name string) bool {
98104
return found
99105
}
100106

101-
func runVerify(artifactReference, artifactPath, provenancePath, source string,
107+
func runVerify(artifactImage, artifactPath, provenancePath, source string,
102108
branch, builderID, ptag, pversiontag *string,
103109
) ([]byte, string, error) {
104110
ctx := context.Background()
105111

106112
// Artifact hash retrieval depends on the artifact type.
107-
artifactHash, err := getArtifactHash(artifactReference, artifactPath)
113+
artifactHash, err := getArtifactHash(artifactImage, artifactPath)
108114
if err != nil {
109115
return nil, "", err
110116
}
@@ -129,10 +135,10 @@ func runVerify(artifactReference, artifactPath, provenancePath, source string,
129135
}
130136
}
131137

132-
return verifiers.Verify(ctx, artifactReference, provenance, artifactHash, provenanceOpts, builderOpts)
138+
return verifiers.Verify(ctx, artifactImage, provenance, artifactHash, provenanceOpts, builderOpts)
133139
}
134140

135-
func getArtifactHash(artifactReference, artifactPath string) (string, error) {
141+
func getArtifactHash(artifactImage, artifactPath string) (string, error) {
136142
if artifactPath != "" {
137143
f, err := os.Open(artifactPath)
138144
if err != nil {
@@ -146,5 +152,5 @@ func getArtifactHash(artifactReference, artifactPath string) (string, error) {
146152
return hex.EncodeToString(h.Sum(nil)), nil
147153
}
148154
// Retrieve image digest
149-
return container.GetImageDigest(artifactReference)
155+
return container.GetImageDigest(artifactImage)
150156
}

cli/slsa-verifier/main_test.go

+81-91
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"github.com/sigstore/cosign/pkg/oci"
1919
"github.com/sigstore/cosign/pkg/oci/layout"
2020

21-
"github.com/slsa-framework/slsa-verifier/container"
2221
serrors "github.com/slsa-framework/slsa-verifier/errors"
22+
"github.com/slsa-framework/slsa-verifier/verifiers/container"
2323
)
2424

2525
func errCmp(e1, e2 error) bool {
@@ -32,18 +32,48 @@ func pString(s string) *string {
3232

3333
const TEST_DIR = "./testdata"
3434

35-
func Test_runVerifyGoAndGeneric(t *testing.T) {
36-
// t.Parallel()
35+
var ARTIFACT_PATH_BUILDERS = []string{"go", "generic"}
36+
var ARTIFACT_IMAGE_BUILDERS = []string{"generic_container"}
37+
38+
func getBuildersAndVersions(t *testing.T,
39+
optionalMinVersion string, specifiedBuilders []string,
40+
defaultBuilders []string) []string {
41+
res := []string{}
42+
builders := specifiedBuilders
43+
if len(builders) == 0 {
44+
builders = defaultBuilders
45+
}
46+
// Get versions for each builder.
47+
for _, builder := range builders {
48+
builderDir, err := ioutil.ReadDir(filepath.Join(TEST_DIR, builder))
49+
if err != nil {
50+
t.Error(err)
51+
}
52+
for _, f := range builderDir {
53+
// Builder subfolders are semantic version strings.
54+
// Compare if a min version is given.
55+
if f.IsDir() && (optionalMinVersion == "" ||
56+
semver.Compare(optionalMinVersion, f.Name()) <= 0) {
57+
// These are the supported versions of the builder
58+
res = append(res, filepath.Join(builder, f.Name()))
59+
}
60+
}
61+
}
62+
return res
63+
}
64+
65+
func Test_runVerifyArtifactPath(t *testing.T) {
66+
t.Parallel()
3767
tests := []struct {
38-
name string
39-
artifact string
40-
source string
41-
pbranch *string
42-
ptag *string
43-
pversiontag *string
44-
pbuilderID *string
45-
builderID string
46-
err error
68+
name string
69+
artifact string
70+
source string
71+
pbranch *string
72+
ptag *string
73+
pversiontag *string
74+
pbuilderID *string
75+
outBuilderId string
76+
err error
4777
// noversion is a special case where we are not testing all builder versions
4878
// for example, testdata for the builder at head in trusted repo workflows
4979
// or testdata from malicious untrusted builders.
@@ -338,22 +368,22 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
338368
builders: []string{"generic"},
339369
},
340370
{
341-
name: "multiple subject second match - builderID",
342-
artifact: "binary-linux-amd64-multi-subject-second",
343-
source: "github.com/slsa-framework/example-package",
344-
minversion: "v1.2.0",
345-
builders: []string{"generic"},
346-
pbuilderID: pString("https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml"),
347-
builderID: "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml",
371+
name: "multiple subject second match - builderID",
372+
artifact: "binary-linux-amd64-multi-subject-second",
373+
source: "github.com/slsa-framework/example-package",
374+
minversion: "v1.2.0",
375+
builders: []string{"generic"},
376+
pbuilderID: pString("https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml"),
377+
outBuilderId: "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml",
348378
},
349379
// Special case of the e2e test repository building builder from head.
350380
{
351-
name: "e2e test repository verified with builder at head",
352-
artifact: "binary-linux-amd64-e2e-builder-repo",
353-
source: "github.com/slsa-framework/example-package",
354-
pbranch: pString("main"),
355-
noversion: true,
356-
builderID: "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/builder_go_slsa3.yml",
381+
name: "e2e test repository verified with builder at head",
382+
artifact: "binary-linux-amd64-e2e-builder-repo",
383+
source: "github.com/slsa-framework/example-package",
384+
pbranch: pString("main"),
385+
noversion: true,
386+
outBuilderId: "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/builder_go_slsa3.yml",
357387
},
358388
// Malicious builders and workflows.
359389
{
@@ -407,32 +437,8 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
407437
tt := tt // Re-initializing variable so it is not changed while executing the closure below
408438
t.Run(tt.name, func(t *testing.T) {
409439
// t.Parallel()
410-
getBuildersAndVersions := func(minversion string, ttBuilders []string) []string {
411-
res := []string{}
412-
builders := tt.builders
413-
if len(builders) == 0 {
414-
// This tests both generic and go
415-
builders = []string{"go", "generic"}
416-
}
417-
// Get versions for each builder.
418-
for _, builder := range builders {
419-
builderDir, err := ioutil.ReadDir(filepath.Join(TEST_DIR, builder))
420-
if err != nil {
421-
t.Error(err)
422-
}
423-
for _, f := range builderDir {
424-
// Builder subfolders are semantic version strings.
425-
// Compare if a min version is given.
426-
if f.IsDir() && semver.Compare(minversion, f.Name()) <= 0 {
427-
// These are the supported versions of the builder
428-
res = append(res, filepath.Join(builder, f.Name()))
429-
}
430-
}
431-
}
432-
return res
433-
}
434440

435-
checkVersions := getBuildersAndVersions(tt.minversion, tt.builders)
441+
checkVersions := getBuildersAndVersions(t, tt.minversion, tt.builders, ARTIFACT_PATH_BUILDERS)
436442
if tt.noversion {
437443
checkVersions = []string{""}
438444
}
@@ -442,7 +448,7 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
442448
artifactPath := filepath.Clean(filepath.Join(TEST_DIR, v, tt.artifact))
443449
provenancePath := fmt.Sprintf("%s.intoto.jsonl", artifactPath)
444450

445-
_, builderID, err := runVerify("", artifactPath,
451+
_, outBuilderId, err := runVerify("", artifactPath,
446452
provenancePath,
447453
tt.source, tt.pbranch, tt.pbuilderID,
448454
tt.ptag, tt.pversiontag)
@@ -455,27 +461,29 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
455461
return
456462
}
457463

458-
if tt.builderID != "" && builderID != tt.builderID {
459-
t.Errorf(cmp.Diff(builderID, tt.builderID))
464+
if tt.outBuilderId != "" && outBuilderId != tt.outBuilderId {
465+
t.Errorf(cmp.Diff(outBuilderId, tt.outBuilderId))
460466
}
461467
}
462468
})
463469
}
464470
}
465471

466-
func TestContainerVerification(t *testing.T) {
472+
func Test_runVerifyArtifactImage(t *testing.T) {
473+
t.Parallel()
474+
467475
// Override cosign image verification function for local image testing.
468476
container.RunCosignImageVerification = func(ctx context.Context,
469-
imageReference string, co *cosign.CheckOpts) ([]oci.Signature, bool, error) {
470-
return cosign.VerifyLocalImageAttestations(ctx, imageReference, co)
477+
image string, co *cosign.CheckOpts) ([]oci.Signature, bool, error) {
478+
return cosign.VerifyLocalImageAttestations(ctx, image, co)
471479
}
472480

473481
// TODO: Is there a more uniform way of handling getting image digest for both
474482
// remote and local images?
475-
container.GetImageDigest = func(imageReference string) (string, error) {
483+
container.GetImageDigest = func(image string) (string, error) {
476484
// This is copied from cosign's VerifyLocalImageAttestation code:
477485
// https://github.com/sigstore/cosign/blob/fdceee4825dc5d56b130f3f431aab93137359e79/pkg/cosign/verify.go#L654
478-
se, err := layout.SignedImageIndex(imageReference)
486+
se, err := layout.SignedImageIndex(image)
479487
if err != nil {
480488
return "", err
481489
}
@@ -507,17 +515,16 @@ func TestContainerVerification(t *testing.T) {
507515
return strings.TrimPrefix(h.String(), "sha256:"), nil
508516
}
509517

510-
// t.Parallel()
511518
tests := []struct {
512-
name string
513-
artifact string
514-
source string
515-
pbranch *string
516-
ptag *string
517-
pversiontag *string
518-
pbuilderID *string
519-
builderID string
520-
err error
519+
name string
520+
artifact string
521+
source string
522+
pbranch *string
523+
ptag *string
524+
pversiontag *string
525+
pbuilderID *string
526+
outBuilderID string
527+
err error
521528
// noversion is a special case where we are not testing all builder versions
522529
// for example, testdata for the builder at head in trusted repo workflows
523530
// or testdata from malicious untrusted builders.
@@ -587,34 +594,17 @@ func TestContainerVerification(t *testing.T) {
587594
for _, tt := range tests {
588595
tt := tt // Re-initializing variable so it is not changed while executing the closure below
589596
t.Run(tt.name, func(t *testing.T) {
590-
// t.Parallel()
591-
getVersions := func() []string {
592-
res := []string{}
593-
builder := "container"
594-
595-
builderDir, err := ioutil.ReadDir(filepath.Join(TEST_DIR, builder))
596-
if err != nil {
597-
t.Error(err)
598-
}
599-
for _, f := range builderDir {
600-
if f.IsDir() {
601-
// These are the supported versions of the builder
602-
res = append(res, filepath.Join(builder, f.Name()))
603-
}
604-
}
605-
606-
return res
607-
}
597+
t.Parallel()
608598

609-
checkVersions := getVersions()
599+
checkVersions := getBuildersAndVersions(t, "", nil, ARTIFACT_IMAGE_BUILDERS)
610600
if tt.noversion {
611601
checkVersions = []string{""}
612602
}
613603

614604
for _, v := range checkVersions {
615-
artifactReference := filepath.Clean(filepath.Join(TEST_DIR, v, tt.artifact))
605+
image := filepath.Clean(filepath.Join(TEST_DIR, v, tt.artifact))
616606

617-
_, builderID, err := runVerify(artifactReference, "", "",
607+
_, outBuilderID, err := runVerify(image, "", "",
618608
tt.source, tt.pbranch, tt.pbuilderID,
619609
tt.ptag, tt.pversiontag)
620610

@@ -626,8 +616,8 @@ func TestContainerVerification(t *testing.T) {
626616
return
627617
}
628618

629-
if tt.builderID != "" && builderID != tt.builderID {
630-
t.Errorf(cmp.Diff(builderID, tt.builderID))
619+
if tt.outBuilderID != "" && outBuilderID != tt.outBuilderID {
620+
t.Errorf(cmp.Diff(outBuilderID, tt.outBuilderID))
631621
}
632622
}
633623
})

container/container.go

-27
This file was deleted.

0 commit comments

Comments
 (0)