From 96556c8ab14d219f8e61655a4e30e73d0390fbdc Mon Sep 17 00:00:00 2001 From: Gari Singh Date: Sun, 10 Dec 2017 13:30:48 -0500 Subject: [PATCH] [FAB-5152] Make relative paths work for BCCSP conf 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 --- peer/common/common.go | 8 +++++++ peer/common/common_test.go | 34 ++++++++++++++++++++++++++++++ peer/common/testdata/absolute.yaml | 9 ++++++++ peer/common/testdata/notset.yaml | 9 ++++++++ peer/common/testdata/relative.yaml | 9 ++++++++ 5 files changed, 69 insertions(+) create mode 100644 peer/common/testdata/absolute.yaml create mode 100644 peer/common/testdata/notset.yaml create mode 100644 peer/common/testdata/relative.yaml diff --git a/peer/common/common.go b/peer/common/common.go index 761396e522d..fdb53ba16e9 100644 --- a/peer/common/common.go +++ b/peer/common/common.go @@ -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 { @@ -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() diff --git a/peer/common/common_test.go b/peer/common/common_test.go index 049064daebe..334a15faaa3 100644 --- a/peer/common/common_test.go +++ b/peer/common/common_test.go @@ -9,6 +9,7 @@ package common_test import ( "fmt" "os" + "path/filepath" "testing" "github.com/hyperledger/fabric/common/util" @@ -16,6 +17,7 @@ import ( "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" ) @@ -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 @@ -97,6 +129,8 @@ func TestGetEndorserClient(t *testing.T) { } func TestSetLogLevelFromViper(t *testing.T) { + viper.Reset() + common.InitConfig("core") type args struct { module string } diff --git a/peer/common/testdata/absolute.yaml b/peer/common/testdata/absolute.yaml new file mode 100644 index 00000000000..439d552e60d --- /dev/null +++ b/peer/common/testdata/absolute.yaml @@ -0,0 +1,9 @@ +peer: + BCCSP: + Default: SW + SW: + Hash: SHA2 + Security: 256 + FileKeyStore: + # absolute path + KeyStore: /msp/keystore diff --git a/peer/common/testdata/notset.yaml b/peer/common/testdata/notset.yaml new file mode 100644 index 00000000000..1d33e319e7b --- /dev/null +++ b/peer/common/testdata/notset.yaml @@ -0,0 +1,9 @@ +peer: + BCCSP: + Default: SW + SW: + Hash: SHA2 + Security: 256 + FileKeyStore: + # not set + KeyStore: diff --git a/peer/common/testdata/relative.yaml b/peer/common/testdata/relative.yaml new file mode 100644 index 00000000000..463af34af26 --- /dev/null +++ b/peer/common/testdata/relative.yaml @@ -0,0 +1,9 @@ +peer: + BCCSP: + Default: SW + SW: + Hash: SHA2 + Security: 256 + FileKeyStore: + # relative path + KeyStore: msp/keystore