Skip to content

Commit

Permalink
[FAB-5152] Make relative paths work for BCCSP conf
Browse files Browse the repository at this point in the history
Prior to this change, if
peer.BCCSP.SW.FileKeyStore.KeyStore is set to
a relative path, the local MSP will fail to
load.

Change-Id: I840ad82f4cda635e4a464b0a473d35289fe5e795
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Dec 10, 2017
1 parent 30aed74 commit 96556c8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
}

// Init the BCCSP
SetBCCSPKeystorePath()
var bccspConfig *factory.FactoryOpts
err = viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
if err != nil {
Expand All @@ -109,6 +110,13 @@ func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
return nil
}

// SetBCCSPKeystorePath sets the file keystore path for the SW BCCSP provider
// to an absolute path relative to the config file
func SetBCCSPKeystorePath() {
viper.Set("peer.BCCSP.SW.FileKeyStore.KeyStore",
config.GetPath("peer.BCCSP.SW.FileKeyStore.KeyStore"))
}

// GetEndorserClient returns a new endorser client connection for this peer
func GetEndorserClient() (pb.EndorserClient, error) {
clientConn, err := peer.NewPeerClientConnection()
Expand Down
34 changes: 34 additions & 0 deletions peer/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ package common_test
import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -73,6 +75,36 @@ func TestInitCrypto(t *testing.T) {
assert.Error(t, err, fmt.Sprintf("Expected error [%s] calling InitCrypto()", err))
}

func TestSetBCCSPKeystorePath(t *testing.T) {
cfgKey := "peer.BCCSP.SW.FileKeyStore.KeyStore"
cfgPath := "./testdata"
absPath, _ := filepath.Abs(cfgPath)
keystorePath := "/msp/keystore"

os.Setenv("FABRIC_CFG_PATH", cfgPath)
viper.Reset()
_ = common.InitConfig("notset")
common.SetBCCSPKeystorePath()
t.Log(viper.GetString(cfgKey))
assert.Equal(t, "", viper.GetString(cfgKey))

viper.Reset()
_ = common.InitConfig("absolute")
common.SetBCCSPKeystorePath()
t.Log(viper.GetString(cfgKey))
assert.Equal(t, keystorePath, viper.GetString(cfgKey))

viper.Reset()
_ = common.InitConfig("relative")
common.SetBCCSPKeystorePath()
t.Log(viper.GetString(cfgKey))
assert.Equal(t, filepath.Join(absPath, keystorePath),
viper.GetString(cfgKey))

viper.Reset()
os.Unsetenv("FABRIC_CFG_PATH")
}

func TestGetEndorserClient(t *testing.T) {
tests := []struct {
name string
Expand All @@ -97,6 +129,8 @@ func TestGetEndorserClient(t *testing.T) {
}

func TestSetLogLevelFromViper(t *testing.T) {
viper.Reset()
common.InitConfig("core")
type args struct {
module string
}
Expand Down
9 changes: 9 additions & 0 deletions peer/common/testdata/absolute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
peer:
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
# absolute path
KeyStore: /msp/keystore
9 changes: 9 additions & 0 deletions peer/common/testdata/notset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
peer:
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
# not set
KeyStore:
9 changes: 9 additions & 0 deletions peer/common/testdata/relative.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
peer:
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
# relative path
KeyStore: msp/keystore

0 comments on commit 96556c8

Please sign in to comment.