Skip to content

Commit 1daaaf8

Browse files
refactor(notation): merge notationoptions with options strut
Signed-off-by: Jason <jagoodse@microsoft.com>
1 parent b9c85ee commit 1daaaf8

File tree

3 files changed

+53
-57
lines changed

3 files changed

+53
-57
lines changed

internal/oci/notation.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,19 @@ 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-
3526
// NotationOptions is a function that configures the options applied to a notation verifier
36-
type NotationOptions func(opts *notationOptions)
27+
type NotationOptions func(opts *options)
3728

3829
// WithInsecureRegistry sets notation to verify against insecure registry.
3930
func WithInsecureRegistry(insecure bool) NotationOptions {
40-
return func(opts *notationOptions) {
31+
return func(opts *options) {
4132
opts.Insecure = insecure
4233
}
4334
}
4435

4536
// WithTrustStore sets the trust store configuration.
4637
func WithTrustStore(trustStore *trustpolicy.Document) NotationOptions {
47-
return func(opts *notationOptions) {
38+
return func(opts *options) {
4839
opts.TrustStore = trustStore
4940
}
5041
}
@@ -55,23 +46,23 @@ func WithTrustStore(trustStore *trustpolicy.Document) NotationOptions {
5546
// The function returns a NotationOptions function option that sets the public certificate
5647
// in the notation options.
5748
func WithNotaryPublicCertificate(data []byte) NotationOptions {
58-
return func(opts *notationOptions) {
59-
opts.PublicCertificate = data
49+
return func(opts *options) {
50+
opts.PublicKey = data
6051
}
6152
}
6253

6354
// WithNotaryRemoteOptions is a functional option for overriding the default
6455
// remote options used by the verifier
6556
func WithNotaryRemoteOptions(opts ...remote.Option) NotationOptions {
66-
return func(o *notationOptions) {
57+
return func(o *options) {
6758
o.ROpt = opts
6859
}
6960
}
7061

7162
// WithNotaryKeychain is a functional option for overriding the default
7263
// remote options used by the verifier
7364
func WithNotaryKeychain(key authn.Keychain) NotationOptions {
74-
return func(o *notationOptions) {
65+
return func(o *options) {
7566
o.Keychain = key
7667
}
7768
}
@@ -106,13 +97,13 @@ func (s trustStore) GetCertificates(ctx context.Context, storeType truststore.Ty
10697

10798
// NewNotaryVerifier initializes a new NotaryVerifier
10899
func NewNotaryVerifier(opts ...NotationOptions) (*NotaryVerifier, error) {
109-
o := notationOptions{}
100+
o := options{}
110101
for _, opt := range opts {
111102
opt(&o)
112103
}
113104

114105
store := &trustStore{
115-
cert: o.PublicCertificate,
106+
cert: o.PublicKey,
116107
}
117108

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

internal/oci/notation_test.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ 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
1616
opts []NotationOptions
17-
want *notationOptions
17+
want *options
1818
}{
1919
{
2020
name: "no options",
21-
want: &notationOptions{},
21+
want: &options{},
2222
},
2323
{
2424
name: "signature option",
2525
opts: []NotationOptions{WithNotaryPublicCertificate([]byte("foo"))},
26-
want: &notationOptions{
27-
PublicCertificate: []byte("foo"),
28-
ROpt: nil,
26+
want: &options{
27+
PublicKey: []byte("foo"),
28+
ROpt: nil,
2929
},
3030
},
3131
{
3232
name: "keychain option",
3333
opts: []NotationOptions{WithNotaryRemoteOptions(remote.WithAuthFromKeychain(authn.DefaultKeychain))},
34-
want: &notationOptions{
35-
PublicCertificate: nil,
36-
ROpt: []remote.Option{remote.WithAuthFromKeychain(authn.DefaultKeychain)},
34+
want: &options{
35+
PublicKey: nil,
36+
ROpt: []remote.Option{remote.WithAuthFromKeychain(authn.DefaultKeychain)},
3737
},
3838
},
3939
{
@@ -42,8 +42,8 @@ func TestNotaryOptions(t *testing.T) {
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),
@@ -57,8 +57,8 @@ func TestNotaryOptions(t *testing.T) {
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),
@@ -69,62 +69,62 @@ func TestNotaryOptions(t *testing.T) {
6969
{
7070
name: "truststore, empty document",
7171
opts: []NotationOptions{WithTrustStore(&trustpolicy.Document{})},
72-
want: &notationOptions{
73-
PublicCertificate: nil,
74-
ROpt: nil,
75-
TrustStore: &trustpolicy.Document{},
72+
want: &options{
73+
PublicKey: nil,
74+
ROpt: nil,
75+
TrustStore: &trustpolicy.Document{},
7676
},
7777
},
7878
{
7979
name: "truststore, dummy document",
8080
opts: []NotationOptions{WithTrustStore(dummyPolicyDocument())},
81-
want: &notationOptions{
82-
PublicCertificate: nil,
83-
ROpt: nil,
84-
TrustStore: dummyPolicyDocument(),
81+
want: &options{
82+
PublicKey: nil,
83+
ROpt: nil,
84+
TrustStore: dummyPolicyDocument(),
8585
},
8686
},
8787
{
8888
name: "insecure, false",
8989
opts: []NotationOptions{WithInsecureRegistry(false)},
90-
want: &notationOptions{
91-
PublicCertificate: nil,
92-
ROpt: nil,
93-
TrustStore: nil,
94-
Insecure: false,
90+
want: &options{
91+
PublicKey: nil,
92+
ROpt: nil,
93+
TrustStore: nil,
94+
Insecure: false,
9595
},
9696
},
9797
{
9898
name: "insecure, true",
9999
opts: []NotationOptions{WithInsecureRegistry(true)},
100-
want: &notationOptions{
101-
PublicCertificate: nil,
102-
ROpt: nil,
103-
TrustStore: nil,
104-
Insecure: true,
100+
want: &options{
101+
PublicKey: nil,
102+
ROpt: nil,
103+
TrustStore: nil,
104+
Insecure: true,
105105
},
106106
},
107107
{
108108
name: "insecure, default",
109109
opts: []NotationOptions{},
110-
want: &notationOptions{
111-
PublicCertificate: nil,
112-
ROpt: nil,
113-
TrustStore: nil,
114-
Insecure: false,
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)