Skip to content

Commit

Permalink
[FAB-4855]Print error if MSP config folder missing
Browse files Browse the repository at this point in the history
Currently on peer start if MSP configuration folder is misconfigured or
missing peer simply crashes with panic rather than providing an error
message, this commit add check to see if MSP folders actually exists and
provides error message instead of panicing crashing the peer + UT.

Change-Id: Ic0e133603e7ec924225b6bfb4b08ebceaced5a55
Signed-off-by: Artem Barger <bartem@il.ibm.com>
  • Loading branch information
C0rWin committed Jun 18, 2017
1 parent b4eef8e commit 1cc0e2c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
17 changes: 13 additions & 4 deletions peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package common

import (
"fmt"
"os"

"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/configtx"
Expand All @@ -33,7 +34,7 @@ import (
pcommon "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
logging "github.com/op/go-logging"
"github.com/op/go-logging"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -85,16 +86,24 @@ func InitConfig(cmdRoot string) error {

//InitCrypto initializes crypto for this peer
func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
var err error
// Check whenever msp folder exists
_, err = os.Stat(mspMgrConfigDir)
if os.IsNotExist(err) {
// No need to try to load MSP from folder which is not available
return fmt.Errorf("cannot init crypto, missing %s folder", mspMgrConfigDir)
}

// Init the BCCSP
var bccspConfig *factory.FactoryOpts
err := viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
err = viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
if err != nil {
return fmt.Errorf("Could not parse YAML config [%s]", err)
return fmt.Errorf("could not parse YAML config [%s]", err)
}

err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
if err != nil {
return fmt.Errorf("Error when setting up MSP from directory %s: err %s", mspMgrConfigDir, err)
return fmt.Errorf("error when setting up MSP from directory %s: err %s", mspMgrConfigDir, err)
}

return nil
Expand Down
10 changes: 10 additions & 0 deletions peer/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ SPDX-License-Identifier: Apache-2.0
package common_test

import (
"fmt"
"os"
"testing"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/peer/common"
Expand Down Expand Up @@ -50,6 +53,13 @@ func TestInitConfig(t *testing.T) {
}
}

func TestINitCryptoMissingDir(t *testing.T) {
dir := os.TempDir() + "/" + util.GenerateUUID()
err := common.InitCrypto(dir, "DEFAULT")
assert.Error(t, err, "Should be able to initialize crypto with non-existing directory")
assert.Contains(t, err.Error(), fmt.Sprintf("missing %s folder", dir))
}

func TestInitCrypto(t *testing.T) {

mspConfigPath, err := config.GetDevMspDir()
Expand Down
3 changes: 2 additions & 1 deletion peer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func main() {
var mspID = viper.GetString("peer.localMspId")
err = common.InitCrypto(mspMgrConfigDir, mspID)
if err != nil { // Handle errors reading the config file
panic(err.Error())
logger.Errorf("Cannot run peer because %s", err.Error())
os.Exit(1)
}
// On failure Cobra prints the usage message and error string, so we only
// need to exit with a non-0 status
Expand Down

0 comments on commit 1cc0e2c

Please sign in to comment.