Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: clean up notation CLI #485

Merged
merged 10 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cmd/notation/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"reflect"

"github.com/notaryproject/notation-go"
"github.com/notaryproject/notation-go/log"
notationregistry "github.com/notaryproject/notation-go/registry"
"github.com/notaryproject/notation-go/verifier"
"github.com/notaryproject/notation-go/verifier/trustpolicy"
Expand Down Expand Up @@ -63,7 +62,6 @@ Example - Verify a signature on an OCI artifact identified by a tag (Notation w
func runVerify(command *cobra.Command, opts *verifyOpts) error {
// set log level
ctx := opts.LoggingFlagOpts.SetLoggerLevel(command.Context())
logger := log.GetLogger(ctx)

// initialize
reference := opts.reference
Expand Down Expand Up @@ -101,10 +99,15 @@ func runVerify(command *cobra.Command, opts *verifyOpts) error {
MaxSignatureAttempts: math.MaxInt64,
}

// core process
// core verify process.
_, outcomes, err := notation.Verify(ctx, verifier, sigRepo, verifyOpts)
if err != nil {
logger.Error(err)
var errorVerificationFailed *notation.ErrorVerificationFailed
if !errors.As(err, &errorVerificationFailed) {
// print out the error if it's not the general
// ErrorVerificationFailed
fmt.Println("Verification error:", err)
}
}
// write out on failure
if err != nil || len(outcomes) == 0 {
Expand Down
17 changes: 0 additions & 17 deletions internal/envelope/envelope.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package envelope

import (
"errors"
"fmt"

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

// Supported envelope format.
Expand All @@ -15,21 +13,6 @@ const (
JWS = "jws"
)

// SpeculateSignatureEnvelopeFormat speculates envelope format by looping all builtin envelope format.
//
// TODO: abandon this fature in RC1.
func SpeculateSignatureEnvelopeFormat(raw []byte) (string, error) {
var msg gcose.Sign1Message
if err := msg.UnmarshalCBOR(raw); err == nil {
return cose.MediaTypeEnvelope, nil
}
if len(raw) == 0 || raw[0] != '{' {
// very certain
return "", errors.New("unsupported signature format")
}
return jws.MediaTypeEnvelope, nil
}

// GetEnvelopeMediaType converts the envelope type to mediaType name.
func GetEnvelopeMediaType(sigFormat string) (string, error) {
switch sigFormat {
Expand Down
70 changes: 0 additions & 70 deletions internal/envelope/envelope_test.go
Original file line number Diff line number Diff line change
@@ -1,79 +1,9 @@
package envelope

import (
"encoding/json"
"errors"
"testing"

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

var (
validJwsSignatureEnvelope, _ = json.Marshal(struct{}{})
validCoseSignatureEnvelope []byte
invalidSignatureEnvelope = []byte("invalid")
)

func init() {
msg := gcose.Sign1Message{
Headers: gcose.NewSign1Message().Headers,
Payload: []byte("valid"),
Signature: []byte("valid"),
}
validCoseSignatureEnvelope, _ = msg.MarshalCBOR()
}

func checkErrorEqual(expected, got error) bool {
if expected == nil && got == nil {
return true
}
if expected != nil && got != nil {
return expected.Error() == got.Error()
}
return false
}

func TestSpeculateSignatureEnvelopeFormat(t *testing.T) {
tests := []struct {
name string
raw []byte
expectedType string
expectedErr error
}{
{
name: "jws signature media type",
raw: validJwsSignatureEnvelope,
expectedType: jws.MediaTypeEnvelope,
expectedErr: nil,
},
{
name: "cose signature media type",
raw: validCoseSignatureEnvelope,
expectedType: cose.MediaTypeEnvelope,
expectedErr: nil,
},
{
name: "invalid signature media type",
raw: invalidSignatureEnvelope,
expectedType: "",
expectedErr: errors.New("unsupported signature format"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
eType, err := SpeculateSignatureEnvelopeFormat(tt.raw)
if !checkErrorEqual(tt.expectedErr, err) {
t.Fatalf("expected speculate signature envelope format err: %v, got: %v", tt.expectedErr, err)
}
if eType != tt.expectedType {
t.Fatalf("expected signatureFormat: %v, got: %v", tt.expectedType, eType)
}
})
}
}

func TestGetEnvelopeMediaType(t *testing.T) {
type args struct {
sigFormat string
Expand Down