Skip to content

Commit

Permalink
[FAB-3454] BCCSP Factory Options error checks
Browse files Browse the repository at this point in the history
- (Reported) factory now can 'nicely' fail on bad bccsp
  names
- (Part of debugging this problem) had test cases modifying
  default options. Hide default options behind a getter which
  returns a new instance every time, so that it does not get
  clobbered
- (Part of debugging this problem) SetupBCCSPKeystoreConfig
  should did not update bccsp config outside the function

Change-Id: I397a9e12908f5550ffc54e5277d950bf6428b571
Signed-off-by: Volodymyr Paprotski <vpaprots@ca.ibm.com>
  • Loading branch information
Volodymyr Paprotski committed Jun 4, 2017
1 parent a959653 commit b6e1f91
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 29 deletions.
10 changes: 7 additions & 3 deletions bccsp/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func GetDefault() bccsp.BCCSP {
bootBCCSPInitOnce.Do(func() {
var err error
f := &SWFactory{}
bootBCCSP, err = f.Get(&DefaultOpts)
bootBCCSP, err = f.Get(GetDefaultOpts())
if err != nil {
panic("BCCSP Internal error, failed initialization with DefaultOpts!")
panic("BCCSP Internal error, failed initialization with GetDefaultOpts!")
}
})
return bootBCCSP
Expand All @@ -74,7 +74,11 @@ func GetDefault() bccsp.BCCSP {

// GetBCCSP returns a BCCSP created according to the options passed in input.
func GetBCCSP(name string) (bccsp.BCCSP, error) {
return bccspMap[name], nil
csp, ok := bccspMap[name]
if !ok {
return nil, fmt.Errorf("Could not find BCCSP, no '%s' provider", name)
}
return csp, nil
}

func initBCCSP(f BCCSPFactory, config *FactoryOpts) error {
Expand Down
14 changes: 8 additions & 6 deletions bccsp/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/hyperledger/fabric/bccsp/pkcs11"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -115,10 +116,11 @@ func TestGetDefault(t *testing.T) {

func TestGetBCCSP(t *testing.T) {
bccsp, err := GetBCCSP("SW")
if err != nil {
t.Fatalf("Failed getting default BCCSP [%s]", err)
}
if bccsp == nil {
t.Fatal("Failed Software BCCSP. Nil instance.")
}
assert.NoError(t, err)
assert.NotNil(t, bccsp)

bccsp, err = GetBCCSP("BadName")
assert.Error(t, err)
assert.Contains(t, err.Error(), "Could not find BCCSP, no 'BadName' provider")
assert.Nil(t, bccsp)
}
6 changes: 4 additions & 2 deletions bccsp/factory/nopkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func InitFactories(config *FactoryOpts) error {
factoriesInitOnce.Do(func() {
// Take some precautions on default opts
if config == nil {
config = &DefaultOpts
config = GetDefaultOpts()
}

if config.ProviderName == "" {
config.ProviderName = "SW"
}

if config.SwOpts == nil {
config.SwOpts = DefaultOpts.SwOpts
config.SwOpts = GetDefaultOpts().SwOpts
}

// Initialize factories map
Expand Down Expand Up @@ -75,6 +75,8 @@ func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) {
switch config.ProviderName {
case "SW":
f = &SWFactory{}
default:
return nil, fmt.Errorf("Could not find BCCSP, no '%s' provider", config.ProviderName)
}

csp, err := f.Get(config)
Expand Down
19 changes: 11 additions & 8 deletions bccsp/factory/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ limitations under the License.
*/
package factory

// DefaultOpts offers a default implementation for Opts
var DefaultOpts = FactoryOpts{
ProviderName: "SW",
SwOpts: &SwOpts{
HashFamily: "SHA2",
SecLevel: 256,
// GetDefaultOpts offers a default implementation for Opts
// returns a new instance every time
func GetDefaultOpts() *FactoryOpts {
return &FactoryOpts{
ProviderName: "SW",
SwOpts: &SwOpts{
HashFamily: "SHA2",
SecLevel: 256,

Ephemeral: true,
},
Ephemeral: true,
},
}
}

// FactoryName returns the name of the provider
Expand Down
2 changes: 1 addition & 1 deletion bccsp/factory/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ import (
)

func TestFactoryOptsFactoryName(t *testing.T) {
assert.Equal(t, DefaultOpts.FactoryName(), "SW")
assert.Equal(t, GetDefaultOpts().FactoryName(), "SW")
}
6 changes: 4 additions & 2 deletions bccsp/factory/pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func InitFactories(config *FactoryOpts) error {
func setFactories(config *FactoryOpts) error {
// Take some precautions on default opts
if config == nil {
config = &DefaultOpts
config = GetDefaultOpts()
}

if config.ProviderName == "" {
config.ProviderName = "SW"
}

if config.SwOpts == nil {
config.SwOpts = DefaultOpts.SwOpts
config.SwOpts = GetDefaultOpts().SwOpts
}

// Initialize factories map
Expand Down Expand Up @@ -94,6 +94,8 @@ func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) {
f = &SWFactory{}
case "PKCS11":
f = &PKCS11Factory{}
default:
return nil, fmt.Errorf("Could not find BCCSP, no '%s' provider", config.ProviderName)
}

csp, err := f.Get(config)
Expand Down
16 changes: 15 additions & 1 deletion bccsp/factory/pkcs11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
)

func TestInitFactories(t *testing.T) {
// Reset errors from previous negative test runs
factoriesInitError = nil

err := InitFactories(nil)
assert.NoError(t, err)
}
Expand Down Expand Up @@ -53,21 +56,32 @@ func TestSetFactoriesInvalidArgs(t *testing.T) {
}

func TestGetBCCSPFromOpts(t *testing.T) {
opts := &DefaultOpts
opts := GetDefaultOpts()
opts.SwOpts.FileKeystore = &FileKeystoreOpts{KeyStorePath: os.TempDir()}
opts.SwOpts.Ephemeral = false
csp, err := GetBCCSPFromOpts(opts)
assert.NoError(t, err)
assert.NotNil(t, csp)

lib, pin, label := pkcs11.FindPKCS11Lib()
csp, err = GetBCCSPFromOpts(&FactoryOpts{
ProviderName: "PKCS11",
Pkcs11Opts: &pkcs11.PKCS11Opts{
SecLevel: 256,
HashFamily: "SHA2",
Ephemeral: true,
Library: lib,
Pin: pin,
Label: label,
},
})
assert.NoError(t, err)
assert.NotNil(t, csp)

csp, err = GetBCCSPFromOpts(&FactoryOpts{
ProviderName: "BadName",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Could not find BCCSP, no 'BadName' provider")
assert.Nil(t, csp)
}
10 changes: 10 additions & 0 deletions bccsp/factory/pkcs11factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ func TestPKCS11FactoryGetInvalidArgs(t *testing.T) {

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

opts := &FactoryOpts{
Pkcs11Opts: &pkcs11.PKCS11Opts{
SecLevel: 256,
HashFamily: "SHA2",
Library: lib,
Pin: pin,
Label: label,
},
}
csp, err := f.Get(opts)
Expand All @@ -62,6 +66,9 @@ func TestPKCS11FactoryGet(t *testing.T) {
SecLevel: 256,
HashFamily: "SHA2",
FileKeystore: &pkcs11.FileKeystoreOpts{KeyStorePath: os.TempDir()},
Library: lib,
Pin: pin,
Label: label,
},
}
csp, err = f.Get(opts)
Expand All @@ -73,6 +80,9 @@ func TestPKCS11FactoryGet(t *testing.T) {
SecLevel: 256,
HashFamily: "SHA2",
Ephemeral: true,
Library: lib,
Pin: pin,
Label: label,
},
}
csp, err = f.Get(opts)
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func Start(cc Chaincode) error {
return fmt.Errorf("Error chaincode id not provided")
}

err := factory.InitFactories(&factory.DefaultOpts)
err := factory.InitFactories(factory.GetDefaultOpts())
if err != nil {
return fmt.Errorf("Internal error, BCCSP could not be initialized with default options: %s", err)
}
Expand Down
10 changes: 6 additions & 4 deletions msp/configbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ const (
configfilename = "config.yaml"
)

func SetupBCCSPKeystoreConfig(bccspConfig *factory.FactoryOpts, keystoreDir string) {
func SetupBCCSPKeystoreConfig(bccspConfig *factory.FactoryOpts, keystoreDir string) *factory.FactoryOpts {
if bccspConfig == nil {
bccspConfig = &factory.DefaultOpts
bccspConfig = factory.GetDefaultOpts()
}

if bccspConfig.ProviderName == "SW" {
if bccspConfig.SwOpts == nil {
bccspConfig.SwOpts = factory.DefaultOpts.SwOpts
bccspConfig.SwOpts = factory.GetDefaultOpts().SwOpts
}

// Only override the KeyStorePath if it was left empty
Expand All @@ -126,12 +126,14 @@ func SetupBCCSPKeystoreConfig(bccspConfig *factory.FactoryOpts, keystoreDir stri
bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir}
}
}

return bccspConfig
}

func GetLocalMspConfig(dir string, bccspConfig *factory.FactoryOpts, ID string) (*msp.MSPConfig, error) {
signcertDir := filepath.Join(dir, signcerts)
keystoreDir := filepath.Join(dir, keystore)
SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
bccspConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)

err := factory.InitFactories(bccspConfig)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion orderer/localconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var defaults = TopLevel{
LogLevel: "INFO",
LocalMSPDir: "msp",
LocalMSPID: "DEFAULT",
BCCSP: &bccsp.DefaultOpts,
BCCSP: bccsp.GetDefaultOpts(),
},
RAMLedger: RAMLedger{
HistorySize: 10000,
Expand Down

0 comments on commit b6e1f91

Please sign in to comment.