From ffbf60437dce1a185d61c4f202766feda3278384 Mon Sep 17 00:00:00 2001 From: Artem Barger Date: Sun, 23 Apr 2017 00:43:07 +0300 Subject: [PATCH] [FAB-3344] Fix CSCC error messages This commit fixes the error messages of the CSCC: 1. JoinChannel call provided with genesis block, hence args[1] is a block and not channel it as expected in error message, hence to prevent putting entire block into error message, channel id is extracted instead. 2. GetChannel doesn't get any parameters and therefore in order to avoid panic on accessing non existing args[1], this part of error message is removed. Change-Id: Ifcfc543afa5d694c963a65d688056e4d072b9379 Signed-off-by: Artem Barger --- core/scc/cscc/configure.go | 11 +++++++++-- protos/utils/blockutils.go | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/scc/cscc/configure.go b/core/scc/cscc/configure.go index 31f33358bbc..3fa3926ea16 100644 --- a/core/scc/cscc/configure.go +++ b/core/scc/cscc/configure.go @@ -107,7 +107,14 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response { case JoinChain: // 2. check local MSP Admins policy if err = e.policyChecker.CheckPolicyNoChannel(mgmt.Admins, sp); err != nil { - return shim.Error(fmt.Sprintf("\"JoinChain\" request failed authorization check for channel [%s]: [%s]", args[1], err)) + cid, e := utils.GetChainIDFromBlockBytes(args[1]) + errorString := fmt.Sprintf("\"JoinChain\" request failed authorization check "+ + "for channel [%s]: [%s]", cid, err) + if e != nil { + errorString = fmt.Sprintf("\"JoinChain\" request failed authorization [%s] and unable "+ + "to extract channel id from the block due to [%s]", err, e) + } + return shim.Error(errorString) } return joinChain(args[1]) @@ -127,7 +134,7 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response { case GetChannels: // 2. check local MSP Members policy if err = e.policyChecker.CheckPolicyNoChannel(mgmt.Members, sp); err != nil { - return shim.Error(fmt.Sprintf("\"GetChannels\" request failed authorization check for channel [%s]: [%s]", args[1], err)) + return shim.Error(fmt.Sprintf("\"GetChannels\" request failed authorization check: [%s]", err)) } return getChannels() diff --git a/protos/utils/blockutils.go b/protos/utils/blockutils.go index 1388492ffdd..8eb9e16962b 100644 --- a/protos/utils/blockutils.go +++ b/protos/utils/blockutils.go @@ -23,6 +23,16 @@ import ( cb "github.com/hyperledger/fabric/protos/common" ) +// GetChainIDFromBlockBytes returns chain ID given byte array which represents the block +func GetChainIDFromBlockBytes(bytes []byte) (string, error) { + block, err := GetBlockFromBlockBytes(bytes) + if err != nil { + return "", err + } + + return GetChainIDFromBlock(block) +} + // GetChainIDFromBlock returns chain ID in the block func GetChainIDFromBlock(block *cb.Block) (string, error) { if block.Data == nil || block.Data.Data == nil || len(block.Data.Data) == 0 {