Skip to content

Commit d8a85f0

Browse files
author
zaihaoyin
committed
refactor:clean code
Signed-off-by: zaihaoyin <[email protected]>
1 parent bfccb79 commit d8a85f0

12 files changed

+38
-128
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/go-ldap/ldap/v3 v3.4.4
7-
github.com/notaryproject/notation-core-go v0.1.0-alpha.3.0.20220921042126-b9264de6f2c9
7+
github.com/notaryproject/notation-core-go v0.1.0-alpha.3.0.20220921054535-009c09a9628e
88
github.com/opencontainers/go-digest v1.0.0
99
github.com/opencontainers/image-spec v1.0.2
1010
github.com/oras-project/artifacts-spec v1.0.0-rc.2

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ github.com/go-ldap/ldap/v3 v3.4.4 h1:qPjipEpt+qDa6SI/h1fzuGWoRUY+qqQ9sOZq67/PYUs
88
github.com/go-ldap/ldap/v3 v3.4.4/go.mod h1:fe1MsuN5eJJ1FeLT/LEBVdWfNWKh459R7aXgXtJC+aI=
99
github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
1010
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
11-
github.com/notaryproject/notation-core-go v0.1.0-alpha.3.0.20220921042126-b9264de6f2c9 h1:XX6d8zEwuW+TLzxU1bGJ16fS2w8SWf5udR8Lt6n7+fQ=
12-
github.com/notaryproject/notation-core-go v0.1.0-alpha.3.0.20220921042126-b9264de6f2c9/go.mod h1:mM4M9wPdu0CGgh8f3wOcu0XMiXwEKWQurjBG4nmqQ4g=
11+
github.com/notaryproject/notation-core-go v0.1.0-alpha.3.0.20220921054535-009c09a9628e h1:n3wJRhIVbEGg497rtKV3IMaZJv2hFKYHCOtNIOAyLYw=
12+
github.com/notaryproject/notation-core-go v0.1.0-alpha.3.0.20220921054535-009c09a9628e/go.mod h1:mM4M9wPdu0CGgh8f3wOcu0XMiXwEKWQurjBG4nmqQ4g=
1313
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220620172159-4ab4752c3b86 h1:Oumw+lPnO8qNLTY2mrqPJZMoGExLi/0h/DdikoLTXVU=
1414
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220620172159-4ab4752c3b86/go.mod h1:aA4vdXRS8E1TG7pLZOz85InHi3BiPdErh8IpJN6E0x4=
1515
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=

notation.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import (
99
"github.com/opencontainers/go-digest"
1010
)
1111

12-
const (
13-
// SigningAgent is the unprotected header field used by signature.
14-
SigningAgent = "Notation/1.0.0"
12+
// MediaTypePayloadV1 is the supported content type for signature's payload.
13+
const MediaTypePayloadV1 = "application/vnd.cncf.notary.payload.v1+json"
1514

16-
// MediaTypePayloadV1 is the supported content type for signature's payload.
17-
MediaTypePayloadV1 = "application/vnd.cncf.notary.payload.v1+json"
18-
)
15+
// SigningAgent is the unprotected header field used by signature.
16+
var SigningAgent = "Notation/1.0.0"
1917

2018
// Descriptor describes the artifact that needs to be signed.
2119
type Descriptor struct {

signature/algorithm.go plugin/algorithm.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package signature
1+
package plugin
22

33
import (
44
"errors"
@@ -39,11 +39,8 @@ const (
3939
RSASSA_PSS_SHA_512 = "RSASSA-PSS-SHA-512"
4040
)
4141

42-
// InvalidKeySpec is the invalid value of keySpec.
43-
var InvalidKeySpec = signature.KeySpec{}
44-
4542
// KeySpecName returns the name of a keySpec according to the spec.
46-
func KeySpecName(k signature.KeySpec) string {
43+
func KeySpecString(k signature.KeySpec) string {
4744
switch k.Type {
4845
case signature.KeyTypeEC:
4946
switch k.Size {
@@ -68,7 +65,7 @@ func KeySpecName(k signature.KeySpec) string {
6865
}
6966

7067
// KeySpecHashName returns the name of hash function according to the spec.
71-
func KeySpecHashName(k signature.KeySpec) string {
68+
func KeySpecHashString(k signature.KeySpec) string {
7269
switch k.Type {
7370
case signature.KeyTypeEC:
7471
switch k.Size {
@@ -93,7 +90,7 @@ func KeySpecHashName(k signature.KeySpec) string {
9390
}
9491

9592
// ParseKeySpecFromName parses keySpec name to a signature.keySpec type.
96-
func ParseKeySpecFromName(raw string) (keySpec signature.KeySpec, err error) {
93+
func ParseKeySpec(raw string) (keySpec signature.KeySpec, err error) {
9794
switch raw {
9895
case RSA_2048:
9996
keySpec.Size = 2048
@@ -114,14 +111,14 @@ func ParseKeySpecFromName(raw string) (keySpec signature.KeySpec, err error) {
114111
keySpec.Size = 521
115112
keySpec.Type = signature.KeyTypeEC
116113
default:
117-
keySpec = InvalidKeySpec
118-
err = errors.New("parse KeySpec error, keySpec not supported")
114+
keySpec = signature.KeySpec{}
115+
err = errors.New("unknown key spec")
119116
}
120117
return
121118
}
122119

123120
// SigningAlgorithmName returns the signing algorithm name of an algorithm according to the spec.
124-
func SigningAlgorithmName(alg signature.Algorithm) string {
121+
func SigningAlgorithmString(alg signature.Algorithm) string {
125122
switch alg {
126123
case signature.AlgorithmES256:
127124
return ECDSA_SHA_256
@@ -140,7 +137,7 @@ func SigningAlgorithmName(alg signature.Algorithm) string {
140137
}
141138

142139
// ParseSigningAlgorithFromName parses the signing algorithm name from a given string.
143-
func ParseSigningAlgorithFromName(raw string) (signature.Algorithm, error) {
140+
func ParseSigningAlgorithm(raw string) (signature.Algorithm, error) {
144141
switch raw {
145142
case ECDSA_SHA_256:
146143
return signature.AlgorithmES256, nil
@@ -155,5 +152,5 @@ func ParseSigningAlgorithFromName(raw string) (signature.Algorithm, error) {
155152
case RSASSA_PSS_SHA_512:
156153
return signature.AlgorithmPS512, nil
157154
}
158-
return 0, errors.New("parse Signing algorithm error, signing algorithm not supported")
155+
return 0, errors.New("unknown signing algorithm")
159156
}

plugin/plugin.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,12 @@ type DescribeKeyResponse struct {
129129
}
130130

131131
// GenerateSignatureRequest contains the parameters passed in a generate-signature request.
132-
// do we still need keyspec and hash?
133132
type GenerateSignatureRequest struct {
134133
ContractVersion string `json:"contractVersion"`
135134
KeyID string `json:"keyId"`
136-
Payload []byte `json:"payload"`
137135
KeySpec string `json:"keySpec"`
138136
Hash string `json:"hashAlgorithm"`
137+
Payload []byte `json:"payload"`
139138
PluginConfig map[string]string `json:"pluginConfig,omitempty"`
140139
}
141140

signature/envelope.go

+1-20
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,14 @@ import (
44
"errors"
55

66
"github.com/notaryproject/notation-core-go/signature"
7-
// "github.com/notaryproject/notation-core-go/signature/cose"
8-
9-
"github.com/notaryproject/notation-core-go/signature/jws"
107
)
118

12-
// SpeculateSignatureEnvelopeFormat speculates envelope format by looping all builtin envelope format.
13-
//
14-
// TODO: find a better way to inspect the type of envelope.
15-
// TODO: support inspecting cose format
16-
func SpeculateSignatureEnvelopeFormat(raw []byte) (string, error) {
17-
// var msg gcose.Sign1Message
18-
// if err := msg.UnmarshalCBOR(raw); err == nil {
19-
// return cose.MediaTypeEnvelope, nil
20-
// }
21-
if len(raw) == 0 || raw[0] != '{' {
22-
// very certain
23-
return "", errors.New("unsupported signature format")
24-
}
25-
return jws.MediaTypeEnvelope, nil
26-
}
27-
289
// ValidateEnvelopeMediaType validetes envelope media type is supported by notation-core-go.
2910
func ValidateEnvelopeMediaType(mediaType string) error {
3011
for _, types := range signature.RegisteredEnvelopeTypes() {
3112
if mediaType == types {
3213
return nil
3314
}
3415
}
35-
return errors.New("signing mediaTypeEnvelope invalid")
16+
return errors.New("invalid envelope media type")
3617
}

signature/envelope_test.go

+1-62
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
package signature
22

33
import (
4-
"encoding/json"
54
"errors"
65
"testing"
76

87
"github.com/notaryproject/notation-core-go/signature/jws"
9-
// gcose "github.com/veraison/go-cose"
108
)
119

12-
var (
13-
validJwsSignatureEnvelope, _ = json.Marshal(struct{}{})
14-
validCoseSignatureEnvelope []byte
15-
invalidSignatureEnvelope = []byte("invalid")
16-
)
17-
18-
func init() {
19-
// msg := gcose.Sign1Message{
20-
// Headers: gcose.NewSign1Message().Headers,
21-
// Payload: []byte("valid"),
22-
// Signature: []byte("valid"),
23-
// }
24-
// validCoseSignatureEnvelope, _ = msg.MarshalCBOR()
25-
}
26-
2710
const invalidMediaType = "invalid"
2811

2912
func checkErrorEqual(expected, got error) bool {
@@ -36,45 +19,6 @@ func checkErrorEqual(expected, got error) bool {
3619
return false
3720
}
3821

39-
func TestSpeculateSignatureEnvelopeFormat(t *testing.T) {
40-
tests := []struct {
41-
name string
42-
raw []byte
43-
expectedType string
44-
expectedErr error
45-
}{
46-
{
47-
name: "jws signature media type",
48-
raw: validJwsSignatureEnvelope,
49-
expectedType: jws.MediaTypeEnvelope,
50-
expectedErr: nil,
51-
},
52-
// {
53-
// name: "cose signature media type",
54-
// raw: validCoseSignatureEnvelope,
55-
// expectedType: cose.MediaTypeEnvelope,
56-
// expectedErr: nil,
57-
// },
58-
{
59-
name: "invalid signature media type",
60-
raw: invalidSignatureEnvelope,
61-
expectedType: "",
62-
expectedErr: errors.New("unsupported signature format"),
63-
},
64-
}
65-
for _, tt := range tests {
66-
t.Run(tt.name, func(t *testing.T) {
67-
eType, err := SpeculateSignatureEnvelopeFormat(tt.raw)
68-
if !checkErrorEqual(tt.expectedErr, err) {
69-
t.Fatalf("expected speculate signature envelope format err: %v, got: %v", tt.expectedErr, err)
70-
}
71-
if eType != tt.expectedType {
72-
t.Fatalf("expected signature envelopeType: %v, got: %v", tt.expectedType, eType)
73-
}
74-
})
75-
}
76-
}
77-
7822
func TestValidateEnvelopeMediaType(t *testing.T) {
7923
tests := []struct {
8024
name string
@@ -86,15 +30,10 @@ func TestValidateEnvelopeMediaType(t *testing.T) {
8630
mediaType: jws.MediaTypeEnvelope,
8731
expectedErr: nil,
8832
},
89-
// {
90-
// name: "cose signature media type",
91-
// mediaType: cose.MediaTypeEnvelope,
92-
// expectedErr: nil,
93-
// },
9433
{
9534
name: "invalid media type",
9635
mediaType: invalidMediaType,
97-
expectedErr: errors.New("signing mediaTypeEnvelope invalid"),
36+
expectedErr: errors.New("invalid envelope media type"),
9837
},
9938
}
10039
for _, tt := range tests {

signature/plugin_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
"github.com/notaryproject/notation-go"
1919
"github.com/notaryproject/notation-go/plugin"
20-
// gcose "github.com/veraison/go-cose"
2120
)
2221

2322
const unsupported = "unsupported"
@@ -32,11 +31,10 @@ var (
3231
}
3332
validSignDescriptor, validSignOpts = generateSigningContent(nil)
3433
invalidJwsEnvelope, _ = json.Marshal(struct{}{})
35-
// invalidCoseEnvelope, _ = gcose.NewSign1Message().MarshalCBOR()
36-
envelopeTypeToData = map[string][]byte{
34+
envelopeTypeToData = map[string][]byte{
3735
jws.MediaTypeEnvelope: invalidJwsEnvelope,
38-
// cose.MediaTypeEnvelope: invalidCoseEnvelope,
3936
}
37+
invalidSignatureEnvelope = []byte("invalid")
4038
)
4139

4240
var (
@@ -179,7 +177,7 @@ func (p *mockProvider) Run(ctx context.Context, req plugin.Request) (interface{}
179177
}
180178
return &plugin.DescribeKeyResponse{
181179
KeyID: p.keyID,
182-
KeySpec: KeySpecName(keySpec),
180+
KeySpec: plugin.KeySpecString(keySpec),
183181
}, nil
184182
case plugin.CommandGenerateSignature:
185183
if p.generateSignature != nil {
@@ -201,7 +199,7 @@ func (p *mockProvider) Run(ctx context.Context, req plugin.Request) (interface{}
201199
return &plugin.GenerateSignatureResponse{
202200
KeyID: p.keyID,
203201
Signature: sig,
204-
SigningAlgorithm: SigningAlgorithmName(keySpec.SignatureAlgorithm()),
202+
SigningAlgorithm: plugin.SigningAlgorithmString(keySpec.SignatureAlgorithm()),
205203
CertificateChain: certs,
206204
}, nil
207205
case plugin.CommandGenerateEnvelope:

signature/provider.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/notaryproject/notation-go/plugin"
1111
)
1212

13-
// builtInPluginMetaData is the builtin metadata used by builtinProvider.
13+
// builtInPluginMetaData is the metadata used by builtinProvider.
1414
var builtInPluginMetaData = plugin.Metadata{
1515
SupportedContractVersions: []string{plugin.ContractVersion},
1616
Capabilities: []plugin.Capability{plugin.CapabilitySignatureGenerator},
@@ -80,7 +80,7 @@ func (p *builtinProvider) Run(_ context.Context, req plugin.Request) (interface{
8080
//
8181
// The detail implementation depends on the underlying plugin.
8282
//
83-
// It wraps a signature.Signature to support external signing.
83+
// It wraps a signature.Signer to support external signing.
8484
type externalProvider struct {
8585
plugin.Runner
8686
keyID string
@@ -129,8 +129,8 @@ func (p *externalProvider) Sign(payload []byte) ([]byte, []*x509.Certificate, er
129129
req := &plugin.GenerateSignatureRequest{
130130
ContractVersion: plugin.ContractVersion,
131131
KeyID: p.keyID,
132-
KeySpec: KeySpecName(keySpec),
133-
Hash: KeySpecHashName(keySpec),
132+
KeySpec: plugin.KeySpecString(keySpec),
133+
Hash: plugin.KeySpecHashString(keySpec),
134134
Payload: payload,
135135
PluginConfig: p.config,
136136
}
@@ -159,7 +159,7 @@ func (p *externalProvider) Sign(payload []byte) ([]byte, []*x509.Certificate, er
159159

160160
// KeySpec returns the keySpec of a keyID by calling describeKey and do some keySpec validation.
161161
func (p *externalProvider) KeySpec() (signature.KeySpec, error) {
162-
if p.keySpec != InvalidKeySpec {
162+
if p.keySpec != (signature.KeySpec{}) {
163163
return p.keySpec, nil
164164
}
165165
keyResp, err := p.describeKey(context.Background())
@@ -171,6 +171,6 @@ func (p *externalProvider) KeySpec() (signature.KeySpec, error) {
171171
if p.keyID != keyResp.KeyID {
172172
return signature.KeySpec{}, fmt.Errorf("keyID in describeKey response %q does not match request %q", keyResp.KeyID, p.keyID)
173173
}
174-
p.keySpec, err = ParseKeySpecFromName(keyResp.KeySpec)
174+
p.keySpec, err = plugin.ParseKeySpec(keyResp.KeySpec)
175175
return p.keySpec, err
176176
}

signature/signer_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/notaryproject/notation-core-go/testhelper"
2121
"github.com/notaryproject/notation-go"
2222
"github.com/notaryproject/notation-go/crypto/timestamp/timestamptest"
23+
"github.com/notaryproject/notation-go/plugin"
2324
"github.com/opencontainers/go-digest"
2425
)
2526

@@ -45,7 +46,7 @@ func setUpKeyCertPairCollections() []*keyCertPair {
4546
panic(fmt.Sprintf("setUpKeyCertPairCollections() failed, invalid keySpec: %v", err))
4647
}
4748
keyCertPairs = append(keyCertPairs, &keyCertPair{
48-
keySpecName: KeySpecName(keySpec),
49+
keySpecName: plugin.KeySpecString(keySpec),
4950
key: certTuple.PrivateKey,
5051
certs: []*x509.Certificate{certTuple.Cert, rsaRoot.Cert},
5152
})
@@ -60,7 +61,7 @@ func setUpKeyCertPairCollections() []*keyCertPair {
6061
panic(fmt.Sprintf("setUpKeyCertPairCollections() failed, invalid keySpec: %v", err))
6162
}
6263
keyCertPairs = append(keyCertPairs, &keyCertPair{
63-
keySpecName: KeySpecName(keySpec),
64+
keySpecName: plugin.KeySpecString(keySpec),
6465
key: certTuple.PrivateKey,
6566
certs: []*x509.Certificate{certTuple.Cert, ecdsaRoot.Cert},
6667
})

0 commit comments

Comments
 (0)