Skip to content

Commit

Permalink
Merge "[FAB-16609]Enhancement empty KeyStorePath"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed Sep 19, 2019
2 parents 3457eeb + 83ee65e commit d00e2a7
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
4 changes: 3 additions & 1 deletion bccsp/factory/pkcs11factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package factory

import (
"strings"

"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/bccsp/pkcs11"
"github.com/hyperledger/fabric/bccsp/sw"
Expand Down Expand Up @@ -50,7 +52,7 @@ func (f *PKCS11Factory) Get(config *FactoryOpts) (bccsp.BCCSP, error) {
var ks bccsp.KeyStore
if p11Opts.Ephemeral == true {
ks = sw.NewDummyKeyStore()
} else if p11Opts.FileKeystore != nil {
} else if p11Opts.FileKeystore != nil && len(strings.TrimSpace(p11Opts.FileKeystore.KeyStorePath)) > 0 {
fks, err := sw.NewFileBasedKeyStore(nil, p11Opts.FileKeystore.KeyStorePath, false)
if err != nil {
return nil, errors.Wrapf(err, "Failed to initialize software key store")
Expand Down
33 changes: 33 additions & 0 deletions bccsp/factory/pkcs11factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,36 @@ func TestPKCS11FactoryGet(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, csp)
}

func TestPKCS11FactoryGetEmptyKeyStorePath(t *testing.T) {
f := &PKCS11Factory{}
lib, pin, label := pkcs11.FindPKCS11Lib()

opts := &FactoryOpts{
Pkcs11Opts: &pkcs11.PKCS11Opts{
SecLevel: 256,
HashFamily: "SHA2",
FileKeystore: &pkcs11.FileKeystoreOpts{},
Library: lib,
Pin: pin,
Label: label,
},
}
csp, err := f.Get(opts)
assert.NoError(t, err)
assert.NotNil(t, csp)

opts = &FactoryOpts{
Pkcs11Opts: &pkcs11.PKCS11Opts{
SecLevel: 256,
HashFamily: "SHA2",
FileKeystore: &pkcs11.FileKeystoreOpts{KeyStorePath: ""},
Library: lib,
Pin: pin,
Label: label,
},
}
csp, err = f.Get(opts)
assert.NoError(t, err)
assert.NotNil(t, csp)
}
2 changes: 1 addition & 1 deletion msp/configbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func SetupBCCSPKeystoreConfig(bccspConfig *factory.FactoryOpts, keystoreDir stri
bccspConfig = factory.GetDefaultOpts()
}

if bccspConfig.ProviderName == "SW" {
if bccspConfig.ProviderName == "SW" || bccspConfig.SwOpts != nil {
if bccspConfig.SwOpts == nil {
bccspConfig.SwOpts = factory.GetDefaultOpts().SwOpts
}
Expand Down
68 changes: 68 additions & 0 deletions msp/configbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,78 @@ import (
"path/filepath"
"testing"

"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/core/config/configtest"
"github.com/stretchr/testify/assert"
)

func TestSetupBCCSPKeystoreConfig(t *testing.T) {
keystoreDir := "/tmp"

// Case 1 : Check with empty FactoryOpts
rtnConfig := SetupBCCSPKeystoreConfig(nil, keystoreDir)
assert.NotNil(t, rtnConfig)
assert.Equal(t, rtnConfig.ProviderName, "SW")
assert.NotNil(t, rtnConfig.SwOpts)
assert.NotNil(t, rtnConfig.SwOpts.FileKeystore)
assert.Equal(t, rtnConfig.SwOpts.FileKeystore.KeyStorePath, keystoreDir)

// Case 2 : Check with 'SW' as default provider
// Case 2-1 : without SwOpts
bccspConfig := &factory.FactoryOpts{
ProviderName: "SW",
}
rtnConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
assert.NotNil(t, rtnConfig.SwOpts)
assert.NotNil(t, rtnConfig.SwOpts.FileKeystore)
assert.Equal(t, rtnConfig.SwOpts.FileKeystore.KeyStorePath, keystoreDir)

// Case 2-2 : without SwOpts.FileKeystore
bccspConfig.SwOpts = &factory.SwOpts{
HashFamily: "SHA2",
SecLevel: 256,
Ephemeral: true,
}
rtnConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
assert.NotNil(t, rtnConfig.SwOpts.FileKeystore)
assert.Equal(t, rtnConfig.SwOpts.FileKeystore.KeyStorePath, keystoreDir)

// Case 2-3 : without SwOpts.FileKeystore.KeyStorePath
bccspConfig.SwOpts = &factory.SwOpts{
HashFamily: "SHA2",
SecLevel: 256,
FileKeystore: &factory.FileKeystoreOpts{},
}
rtnConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
assert.Equal(t, rtnConfig.SwOpts.FileKeystore.KeyStorePath, keystoreDir)

// Case 2-4 : with empty SwOpts.FileKeystore.KeyStorePath
bccspConfig.SwOpts = &factory.SwOpts{
HashFamily: "SHA2",
SecLevel: 256,
FileKeystore: &factory.FileKeystoreOpts{KeyStorePath: ""},
}
rtnConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
assert.Equal(t, rtnConfig.SwOpts.FileKeystore.KeyStorePath, keystoreDir)

// Case 3 : Check with 'PKCS11' as default provider
// Case 3-1 : without SwOpts
bccspConfig.ProviderName = "PKCS11"
bccspConfig.SwOpts = nil
rtnConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
assert.Nil(t, rtnConfig.SwOpts)

// Case 3-2 : without SwOpts.FileKeystore
bccspConfig.SwOpts = &factory.SwOpts{
HashFamily: "SHA2",
SecLevel: 256,
Ephemeral: true,
}
rtnConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
assert.NotNil(t, rtnConfig.SwOpts.FileKeystore)
assert.Equal(t, rtnConfig.SwOpts.FileKeystore.KeyStorePath, keystoreDir)
}

func TestGetLocalMspConfig(t *testing.T) {
mspDir, err := configtest.GetDevMspDir()
assert.NoError(t, err)
Expand Down

0 comments on commit d00e2a7

Please sign in to comment.