@@ -18,8 +18,8 @@ import (
18
18
"github.com/sigstore/cosign/pkg/oci"
19
19
"github.com/sigstore/cosign/pkg/oci/layout"
20
20
21
- "github.com/slsa-framework/slsa-verifier/container"
22
21
serrors "github.com/slsa-framework/slsa-verifier/errors"
22
+ "github.com/slsa-framework/slsa-verifier/verifiers/container"
23
23
)
24
24
25
25
func errCmp (e1 , e2 error ) bool {
@@ -32,18 +32,48 @@ func pString(s string) *string {
32
32
33
33
const TEST_DIR = "./testdata"
34
34
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 ()
37
67
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
47
77
// noversion is a special case where we are not testing all builder versions
48
78
// for example, testdata for the builder at head in trusted repo workflows
49
79
// or testdata from malicious untrusted builders.
@@ -338,22 +368,22 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
338
368
builders : []string {"generic" },
339
369
},
340
370
{
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" ,
348
378
},
349
379
// Special case of the e2e test repository building builder from head.
350
380
{
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" ,
357
387
},
358
388
// Malicious builders and workflows.
359
389
{
@@ -407,32 +437,8 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
407
437
tt := tt // Re-initializing variable so it is not changed while executing the closure below
408
438
t .Run (tt .name , func (t * testing.T ) {
409
439
// 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
- }
434
440
435
- checkVersions := getBuildersAndVersions (tt .minversion , tt .builders )
441
+ checkVersions := getBuildersAndVersions (t , tt .minversion , tt .builders , ARTIFACT_PATH_BUILDERS )
436
442
if tt .noversion {
437
443
checkVersions = []string {"" }
438
444
}
@@ -442,7 +448,7 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
442
448
artifactPath := filepath .Clean (filepath .Join (TEST_DIR , v , tt .artifact ))
443
449
provenancePath := fmt .Sprintf ("%s.intoto.jsonl" , artifactPath )
444
450
445
- _ , builderID , err := runVerify ("" , artifactPath ,
451
+ _ , outBuilderId , err := runVerify ("" , artifactPath ,
446
452
provenancePath ,
447
453
tt .source , tt .pbranch , tt .pbuilderID ,
448
454
tt .ptag , tt .pversiontag )
@@ -455,27 +461,29 @@ func Test_runVerifyGoAndGeneric(t *testing.T) {
455
461
return
456
462
}
457
463
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 ))
460
466
}
461
467
}
462
468
})
463
469
}
464
470
}
465
471
466
- func TestContainerVerification (t * testing.T ) {
472
+ func Test_runVerifyArtifactImage (t * testing.T ) {
473
+ t .Parallel ()
474
+
467
475
// Override cosign image verification function for local image testing.
468
476
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 )
471
479
}
472
480
473
481
// TODO: Is there a more uniform way of handling getting image digest for both
474
482
// remote and local images?
475
- container .GetImageDigest = func (imageReference string ) (string , error ) {
483
+ container .GetImageDigest = func (image string ) (string , error ) {
476
484
// This is copied from cosign's VerifyLocalImageAttestation code:
477
485
// https://github.com/sigstore/cosign/blob/fdceee4825dc5d56b130f3f431aab93137359e79/pkg/cosign/verify.go#L654
478
- se , err := layout .SignedImageIndex (imageReference )
486
+ se , err := layout .SignedImageIndex (image )
479
487
if err != nil {
480
488
return "" , err
481
489
}
@@ -507,17 +515,16 @@ func TestContainerVerification(t *testing.T) {
507
515
return strings .TrimPrefix (h .String (), "sha256:" ), nil
508
516
}
509
517
510
- // t.Parallel()
511
518
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
521
528
// noversion is a special case where we are not testing all builder versions
522
529
// for example, testdata for the builder at head in trusted repo workflows
523
530
// or testdata from malicious untrusted builders.
@@ -587,34 +594,17 @@ func TestContainerVerification(t *testing.T) {
587
594
for _ , tt := range tests {
588
595
tt := tt // Re-initializing variable so it is not changed while executing the closure below
589
596
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 ()
608
598
609
- checkVersions := getVersions ( )
599
+ checkVersions := getBuildersAndVersions ( t , "" , nil , ARTIFACT_IMAGE_BUILDERS )
610
600
if tt .noversion {
611
601
checkVersions = []string {"" }
612
602
}
613
603
614
604
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 ))
616
606
617
- _ , builderID , err := runVerify (artifactReference , "" , "" ,
607
+ _ , outBuilderID , err := runVerify (image , "" , "" ,
618
608
tt .source , tt .pbranch , tt .pbuilderID ,
619
609
tt .ptag , tt .pversiontag )
620
610
@@ -626,8 +616,8 @@ func TestContainerVerification(t *testing.T) {
626
616
return
627
617
}
628
618
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 ))
631
621
}
632
622
}
633
623
})
0 commit comments