Skip to content

Commit

Permalink
fix: error message for dangling reference index (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeyJeyGao committed May 29, 2024
1 parent 9f89059 commit 8bdc5e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions notation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ import (
"strings"
"time"

orasRegistry "oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote"

"github.com/notaryproject/notation-core-go/signature"
"github.com/notaryproject/notation-go/internal/envelope"
"github.com/notaryproject/notation-go/log"
"github.com/notaryproject/notation-go/registry"
"github.com/notaryproject/notation-go/verifier/trustpolicy"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
orasRegistry "oras.land/oras-go/v2/registry"
)

var errDoneVerification = errors.New("done verification")

var reservedAnnotationPrefixes = [...]string{"io.cncf.notary"}

// SignerSignOptions contains parameters for Signer.Sign.
Expand Down Expand Up @@ -145,7 +148,11 @@ func Sign(ctx context.Context, signer Signer, repo registry.Repository, signOpts
logger.Debugf("Pushing signature of artifact descriptor: %+v, signature media type: %v", targetDesc, signOpts.SignatureMediaType)
_, _, err = repo.PushSignature(ctx, signOpts.SignatureMediaType, sig, targetDesc, annotations)
if err != nil {
logger.Error("Failed to push the signature")
var referrerError *remote.ReferrersError
// do not log an error for failing to delete referral index
if !errors.As(err, &referrerError) || !referrerError.IsReferrersIndexDelete() {
logger.Error("Failed to push the signature")
}
return ocispec.Descriptor{}, ErrorPushSignatureFailed{Msg: err.Error()}
}

Expand Down
17 changes: 17 additions & 0 deletions notation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/notaryproject/notation-go/registry"
"github.com/notaryproject/notation-go/verifier/trustpolicy"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry/remote"
)

var expectedMetadata = map[string]string{"foo": "bar", "bar": "foo"}
Expand Down Expand Up @@ -74,6 +75,22 @@ func TestSignSuccessWithUserMetadata(t *testing.T) {
}
}

func TestSignWithDanglingReferrersIndex(t *testing.T) {
repo := mock.NewRepository()
repo.PushSignatureError = &remote.ReferrersError{
Op: "DeleteReferrersIndex",
Err: errors.New("error"),
}
opts := SignOptions{}
opts.ArtifactReference = mock.SampleArtifactUri
opts.SignatureMediaType = jws.MediaTypeEnvelope

_, err := Sign(context.Background(), &dummySigner{}, repo, opts)
if err == nil {
t.Fatalf("no error occurred, expected error")
}
}

func TestSignWithNilRepo(t *testing.T) {
opts := SignOptions{}
opts.ArtifactReference = mock.SampleArtifactUri
Expand Down

0 comments on commit 8bdc5e5

Please sign in to comment.