1
1
package cvo
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
6
7
"io/ioutil"
@@ -20,7 +21,6 @@ import (
20
21
apiextclientv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
21
22
"k8s.io/apimachinery/pkg/api/errors"
22
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24
24
"k8s.io/apimachinery/pkg/labels"
25
25
"k8s.io/apimachinery/pkg/runtime"
26
26
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -3396,190 +3396,97 @@ func fakeClientsetWithUpdates(obj *configv1.ClusterVersion) *fake.Clientset {
3396
3396
}
3397
3397
3398
3398
func Test_loadReleaseVerifierFromConfigMap (t * testing.T ) {
3399
- redhatData , err := ioutil . ReadFile ( filepath . Join ( ".." , "verify" , "testdata" , "keyrings" , "redhat.txt" ))
3400
- if err != nil {
3401
- t . Fatal ( err )
3402
- }
3399
+ const (
3400
+ ExpectedError = "the config map openshift-config-managed/release-verification did not provide any signature stores to read from and cannot be used"
3401
+ ExpectedVerifier = "All release image digests must have GPG signatures from verifier-public-key-redhat (567E347AD0044ADE55BA8A5F199E2F91FD431D51: Red Hat, Inc. (release key 2) <[email protected] >) - will check for signatures in containers/image format at serial signature store wrapping config maps in openshift-config-managed with label \" release.openshift.io/verification-signatures \" , parallel signature store wrapping file:///verify/testdata/signatures"
3402
+ )
3403
3403
3404
3404
tests := []struct {
3405
- name string
3406
- update * payload.Update
3407
- want bool
3408
- wantErr bool
3409
- wantVerifiers int
3405
+ name string
3406
+ fileName string
3407
+ update * payload.Update
3408
+ expectedError string
3409
+ expectedVerifier string
3410
+ expectStore bool
3410
3411
}{
3411
3412
{
3412
- name : "is a no-op when no objects are found" ,
3413
- update : & payload.Update {},
3413
+ name : "is a no-op when no objects are found" ,
3414
+ fileName : "" ,
3415
+ update : & payload.Update {},
3414
3416
},
3415
3417
{
3416
- name : "requires data" ,
3417
- update : & payload.Update {
3418
- Manifests : []lib.Manifest {
3419
- {
3420
- GVK : schema.GroupVersionKind {Version : "v1" , Kind : "ConfigMap" },
3421
- Obj : & unstructured.Unstructured {
3422
- Object : map [string ]interface {}{
3423
- "metadata" : map [string ]interface {}{
3424
- "name" : "release-verification" ,
3425
- "namespace" : "openshift-config-managed" ,
3426
- "annotations" : map [string ]interface {}{
3427
- "release.openshift.io/verification-config-map" : "" ,
3428
- },
3429
- },
3430
- },
3431
- },
3432
- },
3433
- },
3434
- },
3435
- wantErr : true ,
3418
+ name : "requires data" ,
3419
+ fileName : "requires-data.yaml" ,
3420
+ update : & payload.Update {},
3421
+ expectedError : ExpectedError ,
3436
3422
},
3437
3423
{
3438
- name : "requires stores" ,
3439
- update : & payload.Update {
3440
- Manifests : []lib.Manifest {
3441
- {
3442
- GVK : schema.GroupVersionKind {Version : "v1" , Kind : "ConfigMap" },
3443
- Obj : & unstructured.Unstructured {
3444
- Object : map [string ]interface {}{
3445
- "metadata" : map [string ]interface {}{
3446
- "name" : "verification" ,
3447
- "namespace" : "openshift-config" ,
3448
- "annotations" : map [string ]interface {}{
3449
- "release.openshift.io/verification-config-map" : "" ,
3450
- },
3451
- },
3452
- "data" : map [string ]interface {}{
3453
- "verifier-public-key-redhat" : string (redhatData ),
3454
- },
3455
- },
3456
- },
3457
- },
3458
- },
3459
- },
3460
- wantErr : true ,
3424
+ name : "requires stores" ,
3425
+ fileName : "requires-stores.yaml" ,
3426
+ update : & payload.Update {},
3427
+ expectedError : ExpectedError ,
3461
3428
},
3462
3429
{
3463
- name : "requires verifiers" ,
3464
- update : & payload.Update {
3465
- Manifests : []lib.Manifest {
3466
- {
3467
- GVK : schema.GroupVersionKind {Version : "v1" , Kind : "ConfigMap" },
3468
- Obj : & unstructured.Unstructured {
3469
- Object : map [string ]interface {}{
3470
- "metadata" : map [string ]interface {}{
3471
- "name" : "release-verification" ,
3472
- "namespace" : "openshift-config-managed" ,
3473
- "annotations" : map [string ]interface {}{
3474
- "release.openshift.io/verification-config-map" : "" ,
3475
- },
3476
- },
3477
- "data" : map [string ]interface {}{
3478
- "store-local" : "file://../verify/testdata/signatures" ,
3479
- },
3480
- },
3481
- },
3482
- },
3483
- },
3484
- },
3485
- wantErr : true ,
3430
+ name : "requires verifiers" ,
3431
+ fileName : "requires-verifiers.yaml" ,
3432
+ update : & payload.Update {},
3433
+ expectedError : ExpectedError ,
3486
3434
},
3487
3435
{
3488
- name : "loads valid configuration" ,
3489
- update : & payload.Update {
3490
- Manifests : []lib.Manifest {
3491
- {
3492
- GVK : schema.GroupVersionKind {Version : "v1" , Kind : "ConfigMap" },
3493
- Obj : & unstructured.Unstructured {
3494
- Object : map [string ]interface {}{
3495
- "metadata" : map [string ]interface {}{
3496
- "name" : "release-verification" ,
3497
- "namespace" : "openshift-config-managed" ,
3498
- "annotations" : map [string ]interface {}{
3499
- "release.openshift.io/verification-config-map" : "" ,
3500
- },
3501
- },
3502
- "data" : map [string ]interface {}{
3503
- "verifier-public-key-redhat" : string (redhatData ),
3504
- "store-local" : "file://../verify/testdata/signatures" ,
3505
- },
3506
- },
3507
- },
3508
- },
3509
- },
3510
- },
3511
- want : true ,
3512
- wantVerifiers : 1 ,
3436
+ name : "loads valid configuration" ,
3437
+ fileName : "loads-valid.yaml" ,
3438
+ update : & payload.Update {},
3439
+ expectedVerifier : ExpectedVerifier ,
3440
+ expectStore : true ,
3513
3441
},
3514
3442
{
3515
- name : "only the first valid configuration is used" ,
3516
- update : & payload.Update {
3517
- Manifests : []lib.Manifest {
3518
- {
3519
- GVK : schema.GroupVersionKind {Version : "v1" , Kind : "ConfigMap" },
3520
- Obj : & unstructured.Unstructured {
3521
- Object : map [string ]interface {}{
3522
- "metadata" : map [string ]interface {}{
3523
- "name" : "release-verification" ,
3524
- "namespace" : "openshift-config-managed" ,
3525
- "annotations" : map [string ]interface {}{
3526
- "release.openshift.io/verification-config-map" : "" ,
3527
- },
3528
- },
3529
- "data" : map [string ]interface {}{
3530
- "verifier-public-key-redhat" : string (redhatData ),
3531
- "store-local" : "\n file://../verify/testdata/signatures\n " ,
3532
- },
3533
- },
3534
- },
3535
- },
3536
- {
3537
- GVK : schema.GroupVersionKind {Version : "v1" , Kind : "ConfigMap" },
3538
- Obj : & unstructured.Unstructured {
3539
- Object : map [string ]interface {}{
3540
- "metadata" : map [string ]interface {}{
3541
- "name" : "release-verificatio-2n" ,
3542
- "namespace" : "openshift-config-managed" ,
3543
- "annotations" : map [string ]interface {}{
3544
- "release.openshift.io/verification-config-map" : "" ,
3545
- },
3546
- },
3547
- "data" : map [string ]interface {}{
3548
- "verifier-public-key-redhat" : string (redhatData ),
3549
- "verifier-public-key-redhat-2" : string (redhatData ),
3550
- "store-local" : "file://../verify/testdata/signatures" ,
3551
- },
3552
- },
3553
- },
3554
- },
3555
- },
3556
- },
3557
- want : true ,
3558
- wantVerifiers : 1 ,
3443
+ name : "only the first valid configuration is used" ,
3444
+ fileName : "only-first-used.yaml" ,
3445
+ update : & payload.Update {},
3446
+ expectedVerifier : ExpectedVerifier ,
3447
+ expectStore : true ,
3559
3448
},
3560
3449
}
3561
3450
for _ , tt := range tests {
3451
+ if tt .fileName != "" {
3452
+ raw , err := ioutil .ReadFile (filepath .Join (".." , "verify" , "testdata" , "manifests" , tt .fileName ))
3453
+ if err != nil {
3454
+ t .Fatal (err )
3455
+ }
3456
+ ms , err := lib .ParseManifests (bytes .NewReader (raw ))
3457
+ if err != nil {
3458
+ t .Fatalf ("failed to parse file %s as a manifest, error = %v" , tt .fileName , err )
3459
+ }
3460
+ tt .update .Manifests = ms
3461
+ }
3562
3462
t .Run (tt .name , func (t * testing.T ) {
3563
3463
f := kfake .NewSimpleClientset ()
3564
3464
got , store , err := loadConfigMapVerifierDataFromUpdate (tt .update , sigstore .DefaultClient , f .CoreV1 ())
3565
- if (err != nil ) != tt .wantErr {
3566
- t .Fatalf ("loadReleaseVerifierFromPayload() error = %v, wantErr %v" , err , tt .wantErr )
3567
- }
3568
- if (got != nil ) != tt .want {
3569
- t .Fatal (got )
3570
- }
3571
- if tt .want && store == nil {
3572
- t .Fatalf ("expected valid store" )
3573
- }
3574
- if err != nil {
3575
- return
3465
+ if err == nil {
3466
+ if tt .expectedError != "" {
3467
+ t .Fatalf ("loadReleaseVerifierFromPayload succeeded when we expected error \" %s\" " , tt .expectedError )
3468
+ }
3469
+ } else if tt .expectedError == "" {
3470
+ t .Fatalf ("loadReleaseVerifierFromPayload failed when we expected success: %v" , err )
3471
+ } else if tt .expectedError != err .Error () {
3472
+ t .Fatalf ("loadReleaseVerifierFromPayload failed with \" %v\" (expected \" %s\" )" , err , tt .expectedError )
3576
3473
}
3474
+
3577
3475
if got == nil {
3578
- return
3476
+ if tt .expectedVerifier != "" {
3477
+ t .Fatalf ("loadReleaseVerifierFromPayload did not return a verifier when expected" )
3478
+ }
3479
+ } else if tt .expectedVerifier == "" {
3480
+ t .Fatalf ("loadReleaseVerifierFromPayload returned a verifer when not expected" )
3481
+ } else {
3482
+ rvString := got .(* verify.ReleaseVerifier ).String ()
3483
+ if rvString != tt .expectedVerifier {
3484
+ t .Fatalf ("loadReleaseVerifierFromPayload returned \" %v\" when we expected \" %v\" " , rvString , tt .expectedVerifier )
3485
+ }
3579
3486
}
3580
- rv := got .( * verify. ReleaseVerifier )
3581
- if len ( rv . Verifiers ()) != tt . wantVerifiers {
3582
- t .Fatalf ("unexpected release verifier: %#v" , rv )
3487
+
3488
+ if tt . expectStore && store == nil {
3489
+ t .Fatalf ("loadReleaseVerifierFromPayload did not return a store when expected" )
3583
3490
}
3584
3491
})
3585
3492
}
0 commit comments