Skip to content

Commit d068a41

Browse files
refactor(notation): merge notationoptions with options struct
Signed-off-by: Jason <[email protected]>
1 parent b9c85ee commit d068a41

File tree

5 files changed

+70
-77
lines changed

5 files changed

+70
-77
lines changed

internal/controller/helmchart_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ func (r *HelmChartReconciler) makeVerifiers(ctx context.Context, obj *helmv1.Hel
13931393
return nil, err
13941394
}
13951395

1396-
defaultNotaryOciOpts := []soci.NotationOptions{
1396+
defaultNotaryOciOpts := []soci.Options{
13971397
soci.WithTrustStore(&doc),
13981398
soci.WithNotaryRemoteOptions(verifyOpts...),
13991399
}

internal/controller/ocirepository_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv
713713

714714
signatureVerified := false
715715

716-
defaultNotaryOciOpts := []soci.NotationOptions{
716+
defaultNotaryOciOpts := []soci.Options{
717717
soci.WithTrustStore(&doc),
718718
soci.WithNotaryRemoteOptions(opt...),
719719
}

internal/oci/notation.go

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,16 @@ import (
2323
oauth "oras.land/oras-go/v2/registry/remote/auth"
2424
)
2525

26-
// notationOptions is a struct that holds options for notation verifier
27-
type notationOptions struct {
28-
PublicCertificate []byte
29-
TrustStore *trustpolicy.Document
30-
Keychain authn.Keychain
31-
ROpt []remote.Option
32-
Insecure bool
33-
}
34-
35-
// NotationOptions is a function that configures the options applied to a notation verifier
36-
type NotationOptions func(opts *notationOptions)
37-
3826
// WithInsecureRegistry sets notation to verify against insecure registry.
39-
func WithInsecureRegistry(insecure bool) NotationOptions {
40-
return func(opts *notationOptions) {
27+
func WithInsecureRegistry(insecure bool) Options {
28+
return func(opts *options) {
4129
opts.Insecure = insecure
4230
}
4331
}
4432

4533
// WithTrustStore sets the trust store configuration.
46-
func WithTrustStore(trustStore *trustpolicy.Document) NotationOptions {
47-
return func(opts *notationOptions) {
34+
func WithTrustStore(trustStore *trustpolicy.Document) Options {
35+
return func(opts *options) {
4836
opts.TrustStore = trustStore
4937
}
5038
}
@@ -54,24 +42,24 @@ func WithTrustStore(trustStore *trustpolicy.Document) NotationOptions {
5442
// It takes in the certificate data as a byte slice and the name of the certificate.
5543
// The function returns a NotationOptions function option that sets the public certificate
5644
// in the notation options.
57-
func WithNotaryPublicCertificate(data []byte) NotationOptions {
58-
return func(opts *notationOptions) {
59-
opts.PublicCertificate = data
45+
func WithNotaryPublicCertificate(data []byte) Options {
46+
return func(opts *options) {
47+
opts.PublicKey = data
6048
}
6149
}
6250

6351
// WithNotaryRemoteOptions is a functional option for overriding the default
6452
// remote options used by the verifier
65-
func WithNotaryRemoteOptions(opts ...remote.Option) NotationOptions {
66-
return func(o *notationOptions) {
53+
func WithNotaryRemoteOptions(opts ...remote.Option) Options {
54+
return func(o *options) {
6755
o.ROpt = opts
6856
}
6957
}
7058

7159
// WithNotaryKeychain is a functional option for overriding the default
7260
// remote options used by the verifier
73-
func WithNotaryKeychain(key authn.Keychain) NotationOptions {
74-
return func(o *notationOptions) {
61+
func WithNotaryKeychain(key authn.Keychain) Options {
62+
return func(o *options) {
7563
o.Keychain = key
7664
}
7765
}
@@ -105,14 +93,14 @@ func (s trustStore) GetCertificates(ctx context.Context, storeType truststore.Ty
10593
}
10694

10795
// NewNotaryVerifier initializes a new NotaryVerifier
108-
func NewNotaryVerifier(opts ...NotationOptions) (*NotaryVerifier, error) {
109-
o := notationOptions{}
96+
func NewNotaryVerifier(opts ...Options) (*NotaryVerifier, error) {
97+
o := options{}
11098
for _, opt := range opts {
11199
opt(&o)
112100
}
113101

114102
store := &trustStore{
115-
cert: o.PublicCertificate,
103+
cert: o.PublicKey,
116104
}
117105

118106
verifier, err := verifier.New(o.TrustStore, store, nil)

internal/oci/notation_test.go

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,40 @@ import (
1010
"github.com/notaryproject/notation-go/verifier/trustpolicy"
1111
)
1212

13-
func TestNotaryOptions(t *testing.T) {
13+
func TestOptionsForNotary(t *testing.T) {
1414
testCases := []struct {
1515
name string
16-
opts []NotationOptions
17-
want *notationOptions
16+
opts []Options
17+
want *options
1818
}{
1919
{
2020
name: "no options",
21-
want: &notationOptions{},
21+
want: &options{},
2222
},
2323
{
2424
name: "signature option",
25-
opts: []NotationOptions{WithNotaryPublicCertificate([]byte("foo"))},
26-
want: &notationOptions{
27-
PublicCertificate: []byte("foo"),
28-
ROpt: nil,
25+
opts: []Options{WithNotaryPublicCertificate([]byte("foo"))},
26+
want: &options{
27+
PublicKey: []byte("foo"),
28+
ROpt: nil,
2929
},
3030
},
3131
{
3232
name: "keychain option",
33-
opts: []NotationOptions{WithNotaryRemoteOptions(remote.WithAuthFromKeychain(authn.DefaultKeychain))},
34-
want: &notationOptions{
35-
PublicCertificate: nil,
36-
ROpt: []remote.Option{remote.WithAuthFromKeychain(authn.DefaultKeychain)},
33+
opts: []Options{WithNotaryRemoteOptions(remote.WithAuthFromKeychain(authn.DefaultKeychain))},
34+
want: &options{
35+
PublicKey: nil,
36+
ROpt: []remote.Option{remote.WithAuthFromKeychain(authn.DefaultKeychain)},
3737
},
3838
},
3939
{
4040
name: "keychain and authenticator option",
41-
opts: []NotationOptions{WithNotaryRemoteOptions(
41+
opts: []Options{WithNotaryRemoteOptions(
4242
remote.WithAuth(&authn.Basic{Username: "foo", Password: "bar"}),
4343
remote.WithAuthFromKeychain(authn.DefaultKeychain),
4444
)},
45-
want: &notationOptions{
46-
PublicCertificate: nil,
45+
want: &options{
46+
PublicKey: nil,
4747
ROpt: []remote.Option{
4848
remote.WithAuth(&authn.Basic{Username: "foo", Password: "bar"}),
4949
remote.WithAuthFromKeychain(authn.DefaultKeychain),
@@ -52,13 +52,13 @@ func TestNotaryOptions(t *testing.T) {
5252
},
5353
{
5454
name: "keychain, authenticator and transport option",
55-
opts: []NotationOptions{WithNotaryRemoteOptions(
55+
opts: []Options{WithNotaryRemoteOptions(
5656
remote.WithAuth(&authn.Basic{Username: "foo", Password: "bar"}),
5757
remote.WithAuthFromKeychain(authn.DefaultKeychain),
5858
remote.WithTransport(http.DefaultTransport),
5959
)},
60-
want: &notationOptions{
61-
PublicCertificate: nil,
60+
want: &options{
61+
PublicKey: nil,
6262
ROpt: []remote.Option{
6363
remote.WithAuth(&authn.Basic{Username: "foo", Password: "bar"}),
6464
remote.WithAuthFromKeychain(authn.DefaultKeychain),
@@ -68,63 +68,63 @@ func TestNotaryOptions(t *testing.T) {
6868
},
6969
{
7070
name: "truststore, empty document",
71-
opts: []NotationOptions{WithTrustStore(&trustpolicy.Document{})},
72-
want: &notationOptions{
73-
PublicCertificate: nil,
74-
ROpt: nil,
75-
TrustStore: &trustpolicy.Document{},
71+
opts: []Options{WithTrustStore(&trustpolicy.Document{})},
72+
want: &options{
73+
PublicKey: nil,
74+
ROpt: nil,
75+
TrustStore: &trustpolicy.Document{},
7676
},
7777
},
7878
{
7979
name: "truststore, dummy document",
80-
opts: []NotationOptions{WithTrustStore(dummyPolicyDocument())},
81-
want: &notationOptions{
82-
PublicCertificate: nil,
83-
ROpt: nil,
84-
TrustStore: dummyPolicyDocument(),
80+
opts: []Options{WithTrustStore(dummyPolicyDocument())},
81+
want: &options{
82+
PublicKey: nil,
83+
ROpt: nil,
84+
TrustStore: dummyPolicyDocument(),
8585
},
8686
},
8787
{
8888
name: "insecure, false",
89-
opts: []NotationOptions{WithInsecureRegistry(false)},
90-
want: &notationOptions{
91-
PublicCertificate: nil,
92-
ROpt: nil,
93-
TrustStore: nil,
94-
Insecure: false,
89+
opts: []Options{WithInsecureRegistry(false)},
90+
want: &options{
91+
PublicKey: nil,
92+
ROpt: nil,
93+
TrustStore: nil,
94+
Insecure: false,
9595
},
9696
},
9797
{
9898
name: "insecure, true",
99-
opts: []NotationOptions{WithInsecureRegistry(true)},
100-
want: &notationOptions{
101-
PublicCertificate: nil,
102-
ROpt: nil,
103-
TrustStore: nil,
104-
Insecure: true,
99+
opts: []Options{WithInsecureRegistry(true)},
100+
want: &options{
101+
PublicKey: nil,
102+
ROpt: nil,
103+
TrustStore: nil,
104+
Insecure: true,
105105
},
106106
},
107107
{
108108
name: "insecure, default",
109-
opts: []NotationOptions{},
110-
want: &notationOptions{
111-
PublicCertificate: nil,
112-
ROpt: nil,
113-
TrustStore: nil,
114-
Insecure: false,
109+
opts: []Options{},
110+
want: &options{
111+
PublicKey: nil,
112+
ROpt: nil,
113+
TrustStore: nil,
114+
Insecure: false,
115115
},
116116
},
117117
}
118118

119119
// Run the test cases
120120
for _, tc := range testCases {
121121
t.Run(tc.name, func(t *testing.T) {
122-
o := notationOptions{}
122+
o := options{}
123123
for _, opt := range tc.opts {
124124
opt(&o)
125125
}
126-
if !reflect.DeepEqual(o.PublicCertificate, tc.want.PublicCertificate) {
127-
t.Errorf("got %#v, want %#v", &o.PublicCertificate, tc.want.PublicCertificate)
126+
if !reflect.DeepEqual(o.PublicKey, tc.want.PublicKey) {
127+
t.Errorf("got %#v, want %#v", &o.PublicKey, tc.want.PublicKey)
128128
}
129129

130130
if !reflect.DeepEqual(o.TrustStore, tc.want.TrustStore) {

internal/oci/verifier.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import (
2121
"crypto"
2222
"fmt"
2323

24+
"github.com/google/go-containerregistry/pkg/authn"
2425
"github.com/google/go-containerregistry/pkg/name"
2526
"github.com/google/go-containerregistry/pkg/v1/remote"
27+
"github.com/notaryproject/notation-go/verifier/trustpolicy"
2628
"github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio"
2729
coptions "github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
2830
"github.com/sigstore/cosign/v2/cmd/cosign/cli/rekor"
@@ -43,6 +45,9 @@ type options struct {
4345
PublicKey []byte
4446
ROpt []remote.Option
4547
Identities []cosign.Identity
48+
TrustStore *trustpolicy.Document
49+
Keychain authn.Keychain
50+
Insecure bool
4651
}
4752

4853
// Options is a function that configures the options applied to a Verifier.

0 commit comments

Comments
 (0)