Skip to content

Commit 024c85c

Browse files
committed
updated per code review
Signed-off-by: Patrick Zheng <[email protected]>
1 parent 72962ae commit 024c85c

File tree

10 files changed

+95
-111
lines changed

10 files changed

+95
-111
lines changed

internal/slice/slice.go renamed to internal/slices/slices.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package slice
1+
package slices
22

33
// Contains reports whether v is present in s.
44
func Contains[E comparable](s []E, v E) bool {

notation.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020

2121
const annotationX509ChainThumbprint = "io.cncf.notary.x509chain.thumbprint#S256"
2222

23-
const maxVerificationLimitDefault = 50
24-
2523
var errDoneVerification = errors.New("done verification")
2624

2725
// SignOptions contains parameters for Signer.Sign.
@@ -90,7 +88,7 @@ type VerifyOptions struct {
9088

9189
// MaxSignatureAttempts is the maximum number of signature envelopes that
9290
// can be associated with the target artifact. If set to less than or equals
93-
// to zero, value defaults to 50.
91+
// to zero, an error will be returned.
9492
// Note: this option is scoped to notation.Verify(). verifier.Verify() is
9593
// for signle signature verification, and therefore, does not use it.
9694
MaxSignatureAttempts int
@@ -167,8 +165,7 @@ func Verify(ctx context.Context, verifier Verifier, repo registry.Repository, op
167165

168166
var verificationOutcomes []*VerificationOutcome
169167
if opts.MaxSignatureAttempts <= 0 {
170-
// Set MaxVerificationLimit to 50 as default
171-
opts.MaxSignatureAttempts = maxVerificationLimitDefault
168+
return ocispec.Descriptor{}, verificationOutcomes, ErrorSignatureRetrievalFailed{Msg: fmt.Sprintf("verifyOptions.MaxSignatureAttempts expects a positive number, got %d", opts.MaxSignatureAttempts)}
172169
}
173170
errExceededMaxVerificationLimit := ErrorVerificationFailed{Msg: fmt.Sprintf("total number of signatures associated with an artifact should be less than: %d", opts.MaxSignatureAttempts)}
174171
numOfSignatureProcessed := 0

notation_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRegistryResolveError(t *testing.T) {
2222

2323
// mock the repository
2424
repo.ResolveError = errors.New(errorMessage)
25-
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri}
25+
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri, MaxSignatureAttempts: 50}
2626
_, _, err := Verify(context.Background(), &verifier, repo, opts)
2727

2828
if err == nil || !errors.Is(err, expectedErr) {
@@ -35,7 +35,7 @@ func TestSkippedSignatureVerification(t *testing.T) {
3535
repo := mock.NewRepository()
3636
verifier := dummyVerifier{&policyDocument, mock.PluginManager{}, false, *trustpolicy.LevelSkip}
3737

38-
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri}
38+
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri, MaxSignatureAttempts: 50}
3939
_, outcomes, err := Verify(context.Background(), &verifier, repo, opts)
4040

4141
if err != nil || outcomes[0].VerificationLevel.Name != trustpolicy.LevelSkip.Name {
@@ -52,7 +52,7 @@ func TestRegistryNoSignatureManifests(t *testing.T) {
5252

5353
// mock the repository
5454
repo.ListSignaturesResponse = []ocispec.Descriptor{}
55-
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri}
55+
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri, MaxSignatureAttempts: 50}
5656
_, _, err := Verify(context.Background(), &verifier, repo, opts)
5757

5858
if err == nil || !errors.Is(err, expectedErr) {
@@ -69,7 +69,7 @@ func TestRegistryFetchSignatureBlobError(t *testing.T) {
6969

7070
// mock the repository
7171
repo.FetchSignatureBlobError = errors.New("network error")
72-
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri}
72+
opts := VerifyOptions{ArtifactReference: mock.SampleArtifactUri, MaxSignatureAttempts: 50}
7373
_, _, err := Verify(context.Background(), &verifier, repo, opts)
7474

7575
if err == nil || !errors.Is(err, expectedErr) {

plugin/proto/proto.go

-11
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,3 @@ const (
6363
// capability for a plugin to support verifying revocation checks.
6464
CapabilityRevocationCheckVerifier Capability = "SIGNATURE_VERIFIER.REVOCATION_CHECK"
6565
)
66-
67-
// In returns true if the Capability is present in the given array of
68-
// capabilities.
69-
func (c Capability) In(capabilities []Capability) bool {
70-
for _, capability := range capabilities {
71-
if c == capability {
72-
return true
73-
}
74-
}
75-
return false
76-
}

verifier/helpers.go

+23-44
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"github.com/notaryproject/notation-core-go/signature"
1212
"github.com/notaryproject/notation-go"
1313
"github.com/notaryproject/notation-go/dir"
14-
"github.com/notaryproject/notation-go/internal/pkix"
15-
"github.com/notaryproject/notation-go/internal/slice"
16-
trustpolicyInternal "github.com/notaryproject/notation-go/internal/trustpolicy"
14+
"github.com/notaryproject/notation-go/internal/slices"
15+
"github.com/notaryproject/notation-go/plugin"
16+
"github.com/notaryproject/notation-go/plugin/proto"
1717
"github.com/notaryproject/notation-go/verifier/trustpolicy"
1818
"github.com/notaryproject/notation-go/verifier/truststore"
1919
)
@@ -82,51 +82,11 @@ func isCriticalFailure(result *notation.ValidationResult) bool {
8282
return result.Action == trustpolicy.ActionEnforce && result.Error != nil
8383
}
8484

85-
func verifyX509TrustedIdentitiesCore(certs []*x509.Certificate, trustPolicy *trustpolicy.TrustPolicy) error {
86-
if slice.Contains(trustPolicy.TrustedIdentities, trustpolicyInternal.Wildcard) {
87-
return nil
88-
}
89-
90-
var trustedX509Identities []map[string]string
91-
for _, identity := range trustPolicy.TrustedIdentities {
92-
i := strings.Index(identity, ":")
93-
94-
identityPrefix := identity[:i]
95-
identityValue := identity[i+1:]
96-
97-
if identityPrefix == trustpolicyInternal.X509Subject {
98-
parsedSubject, err := pkix.ParseDistinguishedName(identityValue)
99-
if err != nil {
100-
return err
101-
}
102-
trustedX509Identities = append(trustedX509Identities, parsedSubject)
103-
}
104-
}
105-
106-
if len(trustedX509Identities) == 0 {
107-
return fmt.Errorf("no x509 trusted identities are configured in the trust policy %q", trustPolicy.Name)
108-
}
109-
110-
leafCert := certs[0] // trusted identities only supported on the leaf cert
111-
112-
leafCertDN, err := pkix.ParseDistinguishedName(leafCert.Subject.String()) // parse the certificate subject following rfc 4514 DN syntax
113-
if err != nil {
114-
return fmt.Errorf("error while parsing the certificate subject from the digital signature. error : %q", err)
115-
}
116-
for _, trustedX509Identity := range trustedX509Identities {
117-
if pkix.IsSubsetDN(trustedX509Identity, leafCertDN) {
118-
return nil
119-
}
120-
}
121-
122-
return fmt.Errorf("signing certificate from the digital signature does not match the X.509 trusted identities %q defined in the trust policy %q", trustedX509Identities, trustPolicy.Name)
123-
}
124-
12585
func getNonPluginExtendedCriticalAttributes(signerInfo *signature.SignerInfo) []signature.Attribute {
12686
var criticalExtendedAttrs []signature.Attribute
12787
for _, attr := range signerInfo.SignedAttributes.ExtendedAttributes {
12888
attrStrKey, ok := attr.Key.(string)
129-
if ok && !slice.Contains(VerificationPluginHeaders, attrStrKey) { // filter the plugin extended attributes
89+
if ok && !slices.Contains(VerificationPluginHeaders, attrStrKey) { // filter the plugin extended attributes
13090
// TODO support other attribute types (COSE attribute keys can be numbers)
13191
criticalExtendedAttrs = append(criticalExtendedAttrs, attr)
13292
}
@@ -181,3 +141,22 @@ func getVerificationPluginMinVersion(signerInfo *signature.SignerInfo) (string,
181141
}
182142
return version, nil
183143
}
144+
145+
// getPluginMetadata gets metadata of the plugin
146+
func getPluginMetadata(ctx context.Context, installedPlugin plugin.Plugin, pluginConfig map[string]string) (*proto.GetMetadataResponse, error) {
147+
req := &proto.GetMetadataRequest{
148+
PluginConfig: pluginConfig,
149+
}
150+
metadata, err := installedPlugin.GetMetadata(ctx, req)
151+
if err != nil {
152+
return nil, err
153+
}
154+
if !metadata.SupportsContract(proto.ContractVersion) {
155+
return nil, fmt.Errorf(
156+
"contract version %q is not in the list of the plugin supported versions %v",
157+
proto.ContractVersion, metadata.SupportedContractVersions,
158+
)
159+
}
160+
161+
return metadata, nil
162+
}

verifier/trustpolicy/trustpolicy.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/notaryproject/notation-go/dir"
1313
"github.com/notaryproject/notation-go/internal/pkix"
14-
"github.com/notaryproject/notation-go/internal/slice"
14+
"github.com/notaryproject/notation-go/internal/slices"
1515
"github.com/notaryproject/notation-go/internal/trustpolicy"
1616
"github.com/notaryproject/notation-go/verifier/truststore"
1717
)
@@ -153,7 +153,7 @@ type SignatureVerification struct {
153153
// if any rule is violated, returns an error
154154
func (policyDoc *Document) Validate() error {
155155
// Validate Version
156-
if !slice.Contains(supportedPolicyVersions, policyDoc.Version) {
156+
if !slices.Contains(supportedPolicyVersions, policyDoc.Version) {
157157
return fmt.Errorf("trust policy document uses unsupported version %q", policyDoc.Version)
158158
}
159159

@@ -232,11 +232,11 @@ func (trustPolicyDoc *Document) GetApplicableTrustPolicy(artifactReference strin
232232
var wildcardPolicy *TrustPolicy
233233
var applicablePolicy *TrustPolicy
234234
for _, policyStatement := range trustPolicyDoc.TrustPolicies {
235-
if slice.Contains(policyStatement.RegistryScopes, trustpolicy.Wildcard) {
235+
if slices.Contains(policyStatement.RegistryScopes, trustpolicy.Wildcard) {
236236
// we need to deep copy because we can't use the loop variable
237237
// address. see https://stackoverflow.com/a/45967429
238238
wildcardPolicy = (&policyStatement).clone()
239-
} else if slice.Contains(policyStatement.RegistryScopes, artifactPath) {
239+
} else if slices.Contains(policyStatement.RegistryScopes, artifactPath) {
240240
applicablePolicy = (&policyStatement).clone()
241241
}
242242
}
@@ -365,7 +365,7 @@ func validateTrustedIdentities(statement TrustPolicy) error {
365365

366366
// If there is a wildcard in trusted identies, there shouldn't be any other
367367
//identities
368-
if len(statement.TrustedIdentities) > 1 && slice.Contains(statement.TrustedIdentities, trustpolicy.Wildcard) {
368+
if len(statement.TrustedIdentities) > 1 && slices.Contains(statement.TrustedIdentities, trustpolicy.Wildcard) {
369369
return fmt.Errorf("trust policy statement %q uses a wildcard trusted identity '*', a wildcard identity cannot be used in conjunction with other values", statement.Name)
370370
}
371371

@@ -415,7 +415,7 @@ func validateRegistryScopes(policyDoc *Document) error {
415415
if len(statement.RegistryScopes) == 0 {
416416
return fmt.Errorf("trust policy statement %q has zero registry scopes, it must specify registry scopes with at least one value", statement.Name)
417417
}
418-
if len(statement.RegistryScopes) > 1 && slice.Contains(statement.RegistryScopes, trustpolicy.Wildcard) {
418+
if len(statement.RegistryScopes) > 1 && slices.Contains(statement.RegistryScopes, trustpolicy.Wildcard) {
419419
return fmt.Errorf("trust policy statement %q uses wildcard registry scope '*', a wildcard scope cannot be used in conjunction with other scope values", statement.Name)
420420
}
421421
for _, scope := range statement.RegistryScopes {

verifier/trustpolicy/trustpolicy_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func TestApplicableTrustPolicy(t *testing.T) {
495495
}
496496
}
497497

498-
func TestLoadPolicyDocument(t *testing.T) {
498+
func TestLoadDocument(t *testing.T) {
499499
// non-existing policy file
500500
tempRoot := t.TempDir()
501501
dir.UserConfigDir = tempRoot

verifier/truststore/truststore.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
corex509 "github.com/notaryproject/notation-core-go/x509"
1515
"github.com/notaryproject/notation-go/dir"
16-
"github.com/notaryproject/notation-go/internal/slice"
16+
"github.com/notaryproject/notation-go/internal/slices"
1717
)
1818

1919
// Type is an enum for trust store types supported such as
@@ -128,7 +128,7 @@ func validateCerts(certs []*x509.Certificate, path string) error {
128128

129129
// isValidStoreType checks if storeType is supported
130130
func isValidStoreType(storeType Type) bool {
131-
return slice.Contains(Types, storeType)
131+
return slices.Contains(Types, storeType)
132132
}
133133

134134
// isValidFileName checks if a file name is cross-platform compatible

0 commit comments

Comments
 (0)