Skip to content

Commit db55766

Browse files
committed
feat: refactor 3
Signed-off-by: Juncheng Zhu <junczhu@microsoft.com>
1 parent 8671266 commit db55766

File tree

2 files changed

+56
-59
lines changed

2 files changed

+56
-59
lines changed

cosign/truststore.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,35 @@ type TrustStore interface {
1313
}
1414

1515
type VOptions struct {
16-
HashAlgorithm int
16+
HashAlgorithm crypto.Hash
1717
KeyRef string
1818
Sk bool
1919
Slot string
2020
CertRef string
2121
CertChain string
2222
SCTRef string
23+
IgnoreSCT bool
2324
}
2425

2526
type TrustStoreImp struct {
26-
OptsMap map[string]*VOptions
27+
optsMap map[string]*VOptions
2728
keysMap map[string]crypto.PublicKey
2829
certMap map[string]*x509.Certificate
2930
certChains map[string][]*x509.Certificate
3031
}
3132

3233
func NewWithOpts(opts *VerifierOptions) TrustStore {
34+
// TODO: get maps from opts.VerifyCommand
3335
return &TrustStoreImp{
36+
optsMap: make(map[string]*VOptions),
3437
keysMap: make(map[string]crypto.PublicKey),
3538
certMap: make(map[string]*x509.Certificate),
3639
certChains: make(map[string][]*x509.Certificate),
3740
}
3841
}
3942

4043
func (t *TrustStoreImp) GetVerifyOpts(subjectRef string) (*VOptions, error) {
41-
return t.OptsMap[subjectRef], nil
44+
return t.optsMap[subjectRef], nil
4245
}
4346

4447
func (t *TrustStoreImp) GetKey(keyRef string) (crypto.PublicKey, error) {

cosign/verifier.go

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ type VerifierOptions struct {
5151
Name string
5252

5353
// VerifyCommand is a stuct contains params that executes the verification process.
54-
verify.VerifyCommand
54+
// TODO: Update this field to a struct hold multi-VerfiyCommands
55+
*verify.VerifyCommand
5556
}
5657

5758
// Verifier is a ratify.Verifier implementation that verifies cosign
@@ -63,11 +64,8 @@ type Verifier struct {
6364
}
6465

6566
// NewVerifier creates a new cosign verifier.
66-
//
67-
// Parameters:
68-
// - opts: Options for creating the verifier, including the name and check options.
6967
func NewVerifier(opts *VerifierOptions) (*Verifier, error) {
70-
checkOpts, err := NewCheckOpts(context.Background(), &opts.VerifyCommand)
68+
checkOpts, err := NewCheckOpts(context.Background(), opts.VerifyCommand)
7169
if err != nil {
7270
return nil, fmt.Errorf("failed to update signature verifier keys: %w", err)
7371
}
@@ -82,6 +80,47 @@ func NewVerifier(opts *VerifierOptions) (*Verifier, error) {
8280
}, nil
8381
}
8482

83+
// NewCheckOpts updates the signature verifierOpts by verifierOptions.
84+
func NewCheckOpts(ctx context.Context, c *verify.VerifyCommand) (opts *cosign.CheckOpts, err error) {
85+
// initialize the cosign check options
86+
opts = &cosign.CheckOpts{}
87+
88+
if c.CheckClaims {
89+
opts.ClaimVerifier = cosign.SimpleClaimVerifier
90+
}
91+
92+
// If we are using signed timestamps, we need to load the TSA certificates
93+
if c.TSACertChainPath != "" || c.UseSignedTimestamps {
94+
tsaCertificates, err := cosign.GetTSACerts(ctx, c.TSACertChainPath, cosign.GetTufTargets)
95+
if err != nil {
96+
return nil, fmt.Errorf("unable to load TSA certificates: %w", err)
97+
}
98+
opts.TSACertificate = tsaCertificates.LeafCert
99+
opts.TSARootCertificates = tsaCertificates.RootCert
100+
opts.TSAIntermediateCertificates = tsaCertificates.IntermediateCerts
101+
}
102+
103+
if !c.IgnoreTlog {
104+
if c.RekorURL == "" {
105+
c.RekorURL = defaultRekorURL
106+
}
107+
108+
rekorClient, err := rekor.NewClient(c.RekorURL)
109+
if err != nil {
110+
return nil, fmt.Errorf("creating Rekor client: %w", err)
111+
}
112+
opts.RekorClient = rekorClient
113+
114+
// This performs an online fetch of the Rekor public keys, but this is needed
115+
// for verifying tlog entries (both online and offline).
116+
opts.RekorPubKeys, err = cosign.GetRekorPubs(ctx)
117+
if err != nil {
118+
return nil, fmt.Errorf("getting Rekor public keys: %w", err)
119+
}
120+
}
121+
return opts, nil
122+
}
123+
85124
// Name returns the name of the verifier.
86125
func (v *Verifier) Name() string {
87126
return v.name
@@ -153,7 +192,8 @@ func (v *Verifier) Verify(ctx context.Context, opts *ratify.VerifyOptions) (*rat
153192
// MapSigVerifier maps and returns a signature verifier based on the provided VerifyCommand and CheckOpts.
154193
// It supports different types of verifiers including key references, security keys, and certificate references.
155194
func (v *Verifier) MapSigVerifier(ctx context.Context, opts *ratify.VerifyOptions) (err error) {
156-
c, err := getVerifyCommandFromOpts(v, opts)
195+
// TODO: update default values for the verifier options
196+
c, err := v.truststore.GetVerifyOpts(opts.Subject)
157197
if err != nil {
158198
return fmt.Errorf("failed to get verify command from options: %w", err)
159199
}
@@ -165,16 +205,16 @@ func (v *Verifier) MapSigVerifier(ctx context.Context, opts *ratify.VerifyOption
165205
}
166206
}
167207

168-
var pubKey signature.Verifier
208+
var pubKey signature.Verifier = nil
169209
switch {
170210
case c.KeyRef != "":
171-
if c.HashAlgorithm == 0 {
172-
c.HashAlgorithm = crypto.SHA256
173-
}
174211
key, err := v.truststore.GetKey(c.KeyRef)
175212
if err != nil {
176213
return fmt.Errorf("getting key: %w", err)
177214
}
215+
if c.HashAlgorithm == 0 {
216+
c.HashAlgorithm = crypto.SHA256
217+
}
178218
pubKey, err = signature.LoadVerifier(key, c.HashAlgorithm)
179219
if err != nil {
180220
return err
@@ -249,47 +289,6 @@ func (v *Verifier) MapSigVerifier(ctx context.Context, opts *ratify.VerifyOption
249289
return nil
250290
}
251291

252-
// NewCheckOpts updates the signature verifierOpts by verifierOptions.
253-
func NewCheckOpts(ctx context.Context, c *verify.VerifyCommand) (opts *cosign.CheckOpts, err error) {
254-
// initialize the cosign check options
255-
opts = &cosign.CheckOpts{}
256-
257-
if c.CheckClaims {
258-
opts.ClaimVerifier = cosign.SimpleClaimVerifier
259-
}
260-
261-
// If we are using signed timestamps, we need to load the TSA certificates
262-
if c.TSACertChainPath != "" || c.UseSignedTimestamps {
263-
tsaCertificates, err := cosign.GetTSACerts(ctx, c.TSACertChainPath, cosign.GetTufTargets)
264-
if err != nil {
265-
return nil, fmt.Errorf("unable to load TSA certificates: %w", err)
266-
}
267-
opts.TSACertificate = tsaCertificates.LeafCert
268-
opts.TSARootCertificates = tsaCertificates.RootCert
269-
opts.TSAIntermediateCertificates = tsaCertificates.IntermediateCerts
270-
}
271-
272-
if !c.IgnoreTlog {
273-
if c.RekorURL == "" {
274-
c.RekorURL = defaultRekorURL
275-
}
276-
277-
rekorClient, err := rekor.NewClient(c.RekorURL)
278-
if err != nil {
279-
return nil, fmt.Errorf("creating Rekor client: %w", err)
280-
}
281-
opts.RekorClient = rekorClient
282-
283-
// This performs an online fetch of the Rekor public keys, but this is needed
284-
// for verifying tlog entries (both online and offline).
285-
opts.RekorPubKeys, err = cosign.GetRekorPubs(ctx)
286-
if err != nil {
287-
return nil, fmt.Errorf("getting Rekor public keys: %w", err)
288-
}
289-
}
290-
return opts, nil
291-
}
292-
293292
func getSignatureBlobDesc(ctx context.Context, store ratify.Store, artifactRef registry.Reference, artifactDesc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
294293
manifest, err := store.FetchImageManifest(ctx, artifactRef.Registry+"/"+artifactRef.Repository, artifactDesc)
295294
if err != nil {
@@ -305,11 +304,6 @@ func getSignatureBlobDesc(ctx context.Context, store ratify.Store, artifactRef r
305304
return signatureLayers, nil
306305
}
307306

308-
func getVerifyCommandFromOpts(v *Verifier, opts *ratify.VerifyOptions) (*verify.VerifyCommand, error) {
309-
v.truststore.GetVerifyOpts(opts.Subject)
310-
return nil, nil
311-
}
312-
313307
// staticLayerOpts builds the cosign options for static layer signatures.
314308
func staticLayerOpts(desc ocispec.Descriptor) ([]static.Option, error) {
315309
options := []static.Option{

0 commit comments

Comments
 (0)