Skip to content

Commit

Permalink
[FAB-8943] error codes for entity matchers
Browse files Browse the repository at this point in the history
Error codes for signature validation and entity matchers

Change-Id: Id64e3c88aadd1bbb26aadfdc984bba23688264f5
Signed-off-by: Pavan Kappara <pavan.kappara@securekey.com>
  • Loading branch information
Pavan Kappara committed Mar 19, 2018
1 parent 0cc07c8 commit 4498a41
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
6 changes: 3 additions & 3 deletions pkg/client/channel/invoke/signature_test.go
Expand Up @@ -7,13 +7,13 @@ SPDX-License-Identifier: Apache-2.0
package invoke

import (
"errors"
"strings"
"testing"

txnmocks "github.com/hyperledger/fabric-sdk-go/pkg/client/common/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
fcmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/util/errors/status"
"github.com/stretchr/testify/assert"
)

Expand All @@ -33,7 +33,7 @@ func TestSignatureValidationHandlerSuccess(t *testing.T) {
}

func TestSignatureValidationCreatorValidateError(t *testing.T) {
validateErr := errors.New("ValidateErr")
validateErr := status.New(status.EndorserClientStatus, status.SignatureVerificationFailed.ToInt32(), "", nil)
// Sample request
request := Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("query"), []byte("b")}}
requestContext := prepareRequestContext(request, Opts{}, t)
Expand All @@ -47,7 +47,7 @@ func TestSignatureValidationCreatorValidateError(t *testing.T) {
}

func TestSignatureValidationCreatorVerifyError(t *testing.T) {
verifyErr := errors.New("Verify")
verifyErr := status.New(status.EndorserClientStatus, status.SignatureVerificationFailed.ToInt32(), "", nil)

// Sample request
request := Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("query"), []byte("b")}}
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/common/verifier/signature.go
Expand Up @@ -32,13 +32,13 @@ func (v *Signature) Verify(response *fab.TransactionProposalResponse) error {
res := response.ProposalResponse

if res.GetEndorsement() == nil {
return errors.Errorf("Missing endorsement in proposal response")
return errors.WithStack(status.New(status.EndorserClientStatus, status.MissingEndorsement.ToInt32(), "missing endorsement in proposal response", nil))
}
creatorID := res.GetEndorsement().Endorser

err := v.Membership.Validate(creatorID)
if err != nil {
return errors.WithMessage(err, "The creator certificate is not valid")
return errors.WithStack(status.New(status.EndorserClientStatus, status.SignatureVerificationFailed.ToInt32(), "the creator certificate is not valid", []interface{}{err.Error()}))
}

// check the signature against the endorser and payload hash
Expand All @@ -47,7 +47,7 @@ func (v *Signature) Verify(response *fab.TransactionProposalResponse) error {
// validate the signature
err = v.Membership.Verify(creatorID, digest, res.GetEndorsement().Signature)
if err != nil {
return errors.WithMessage(err, "The creator's signature over the proposal is not valid")
return errors.WithStack(status.New(status.EndorserClientStatus, status.SignatureVerificationFailed.ToInt32(), "the creator's signature over the proposal is not valid", []interface{}{err.Error()}))
}

return nil
Expand Down
12 changes: 8 additions & 4 deletions pkg/core/config/config.go
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/cryptoutil"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
"github.com/hyperledger/fabric-sdk-go/pkg/util/errors/status"
"github.com/pkg/errors"

"regexp"
Expand Down Expand Up @@ -494,8 +495,8 @@ func (c *Config) tryMatchingCAConfig(caName string) (*core.CAConfig, string, err
return &caConfig, certAuthorityMatchConfig.MappedHost, nil
}
}
return nil, "", errors.New("no matching certAuthority config found")

return nil, "", errors.WithStack(status.New(status.ClientStatus, status.NoMatchingCertificateAuthorityEntity.ToInt32(), "no matching certAuthority config found", nil))
}

// CAClientCertPem Read configuration option for the fabric CA client cert pem embedded in the client config
Expand Down Expand Up @@ -870,7 +871,8 @@ func (c *Config) tryMatchingPeerConfig(peerName string) (*core.PeerConfig, error
return &peerConfig, nil
}
}
return nil, errors.New("no matching peer config found")

return nil, errors.WithStack(status.New(status.ClientStatus, status.NoMatchingPeerEntity.ToInt32(), "no matching peer config found", nil))
}

func (c *Config) tryMatchingOrdererConfig(ordererName string) (*core.OrdererConfig, error) {
Expand Down Expand Up @@ -952,7 +954,8 @@ func (c *Config) tryMatchingOrdererConfig(ordererName string) (*core.OrdererConf
return &ordererConfig, nil
}
}
return nil, errors.New("no matching orderer config found")

return nil, errors.WithStack(status.New(status.ClientStatus, status.NoMatchingOrdererEntity.ToInt32(), "no matching orderer config found", nil))
}

func copyPropertiesMap(origMap map[string]interface{}) map[string]interface{} {
Expand Down Expand Up @@ -989,7 +992,8 @@ func (c *Config) findMatchingPeer(peerName string) (string, error) {
return peerMatchConfig.MappedHost, nil
}
}
return "", errors.New("no matching peers found")

return "", errors.WithStack(status.New(status.ClientStatus, status.NoMatchingPeerEntity.ToInt32(), "no matching peer config found", nil))
}

func (c *Config) compileMatchers() error {
Expand Down
3 changes: 2 additions & 1 deletion pkg/fab/channel/responsevalidator.go
Expand Up @@ -9,6 +9,7 @@ package channel
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/util/errors/status"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -53,7 +54,7 @@ func (tprv *TransactionProposalResponseVerifier) Match(transactionProposalRespon
}

if !proto.Equal(block.Data, b.Data) {
return errors.New("payloads for config block do not match")
return errors.WithStack(status.New(status.EndorserClientStatus, status.EndorsementMismatch.ToInt32(), "payloads for config block do not match", nil))
}
}

Expand Down
36 changes: 28 additions & 8 deletions pkg/util/errors/status/codes.go
Expand Up @@ -41,18 +41,38 @@ const (

// MultipleErrors multiple errors occurred
MultipleErrors Code = 7

// SignatureVerificationFailed is when signature fails verification
SignatureVerificationFailed Code = 8

// MissingEndorsement is if an endoresement is missing
MissingEndorsement Code = 9

// NoMatchingCertificateAuthorityEntity is if entityMatchers are unable to find any matchingCertificateAuthority
NoMatchingCertificateAuthorityEntity Code = 21

// NoMatchingPeerEntity is if entityMatchers are unable to find any matchingPeer
NoMatchingPeerEntity Code = 22

// NoMatchingOrdererEntity is if entityMatchers are unable to find any matchingOrderer
NoMatchingOrdererEntity Code = 23
)

// CodeName maps the codes in this packages to human-readable strings
var CodeName = map[int32]string{
0: "OK",
1: "UNKNOWN",
2: "CONNECTION_FAILED",
3: "ENDORSEMENT_MISMATCH",
4: "EMPTY_CERT",
5: "TIMEOUT",
6: "NO_PEERS_FOUND",
7: "MULTIPLE_ERRORS",
0: "OK",
1: "UNKNOWN",
2: "CONNECTION_FAILED",
3: "ENDORSEMENT_MISMATCH",
4: "EMPTY_CERT",
5: "TIMEOUT",
6: "NO_PEERS_FOUND",
7: "MULTIPLE_ERRORS",
8: "SIGNATURE_VERIFICATION_FAILED",
9: "MISSING_ENDORSEMENT",
21: "NO_MATCHING_CERTIFICATE_AUTHORITY_ENTITY",
22: "NO_MATCHING_PEER_ENTITY",
23: "NO_MATCHING_ORDERER_ENTITY",
}

// ToInt32 cast to int32
Expand Down

0 comments on commit 4498a41

Please sign in to comment.