Skip to content

Commit

Permalink
BCCSP initialization cleanup
Browse files Browse the repository at this point in the history
Change-Id: I74314ea8dd7eb8ba09fc0f1431e992ef30481963
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm authored and denyeart committed Mar 12, 2020
1 parent 2bb6415 commit ca93be5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 158 deletions.
9 changes: 0 additions & 9 deletions bccsp/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@ func GetDefault() bccsp.BCCSP {
return defaultBCCSP
}

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

func initBCCSP(f BCCSPFactory, config *FactoryOpts) error {
csp, err := f.Get(config)
if err != nil {
Expand Down
61 changes: 20 additions & 41 deletions bccsp/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@ package factory
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"os"
"testing"

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

func TestMain(m *testing.M) {
flag.Parse()
lib, pin, label := pkcs11.FindPKCS11Lib()

var jsonBCCSP, yamlBCCSP *FactoryOpts
jsonCFG := []byte(
`{ "default": "SW", "SW":{ "security": 384, "hash": "SHA3" } }`)
Expand All @@ -42,7 +37,16 @@ func TestMain(m *testing.M) {
os.Exit(-1)
}

yamlCFG := fmt.Sprintf(`
yamlCFG := `
BCCSP:
default: SW
SW:
Hash: SHA3
Security: 256`

if pkcs11Enabled {
lib, pin, label := pkcs11.FindPKCS11Lib()
yamlCFG = fmt.Sprintf(`
BCCSP:
default: PKCS11
SW:
Expand All @@ -56,15 +60,6 @@ BCCSP:
Pin: '%s'
Label: %s
`, lib, pin, label)

if lib == "" {
fmt.Printf("Could not find PKCS11 libraries, running without\n")
yamlCFG = `
BCCSP:
default: SW
SW:
Hash: SHA3
Security: 256`
}

viper.SetConfigType("yaml")
Expand All @@ -81,28 +76,23 @@ BCCSP:
}

cfgVariations := []*FactoryOpts{
{
ProviderName: "SW",
SwOpts: &SwOpts{
HashFamily: "SHA2",
SecLevel: 256,

Ephemeral: true,
},
},
{},
{
ProviderName: "SW",
},
{ProviderName: "SW"},
{ProviderName: "SW", SwOpts: &SwOpts{HashFamily: "SHA2", SecLevel: 256, Ephemeral: true}},
jsonBCCSP,
yamlBCCSP,
}

for index, config := range cfgVariations {
fmt.Printf("Trying configuration [%d]\n", index)
InitFactories(config)
InitFactories(nil)
m.Run()
factoriesInitError = initFactories(config)
if factoriesInitError != nil {
fmt.Fprintf(os.Stderr, "initFactories failed: %s", factoriesInitError)
os.Exit(1)
}
if rc := m.Run(); rc != 0 {
os.Exit(rc)
}
}
os.Exit(0)
}
Expand All @@ -113,14 +103,3 @@ func TestGetDefault(t *testing.T) {
t.Fatal("Failed getting default BCCSP. Nil instance.")
}
}

func TestGetBCCSP(t *testing.T) {
bccsp, err := GetBCCSP("SW")
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)
}
23 changes: 8 additions & 15 deletions bccsp/factory/nopkcs11.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
// +build !pkcs11

/*
Copyright IBM Corp. 2017 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package factory

import (
"github.com/hyperledger/fabric/bccsp"
"github.com/pkg/errors"
)

const pkcs11Enabled = false

// FactoryOpts holds configuration information used to initialize factory implementations
type FactoryOpts struct {
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
Expand Down Expand Up @@ -63,7 +56,7 @@ func initFactories(config *FactoryOpts) error {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
return errors.Wrapf(err, "Failed initializing BCCSP.")
return errors.Wrapf(err, "Failed initializing BCCSP")
}
}

Expand All @@ -72,14 +65,14 @@ func initFactories(config *FactoryOpts) error {
f := &PluginFactory{}
err := initBCCSP(f, config)
if err != nil {
return errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP %s", factoriesInitError)
return errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP")
}
}

var ok bool
defaultBCCSP, ok = bccspMap[config.ProviderName]
if !ok {
return errors.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
return errors.Errorf("Could not find default `%s` BCCSP", config.ProviderName)
}
return nil
}
Expand Down
30 changes: 5 additions & 25 deletions bccsp/factory/nopkcs11_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// +build !pkcs11

/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package factory

import (
Expand All @@ -24,28 +15,17 @@ import (
)

func TestInitFactoriesWithMultipleProviders(t *testing.T) {
// testing SW Provider and ensuring other providers are not initialized
factoriesInitError = nil

err := initFactories(&FactoryOpts{
ProviderName: "SW",
SwOpts: &SwOpts{},
PluginOpts: &PluginOpts{},
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Failed initializing BCCSP")
assert.NotContains(t, err.Error(), "Failed initializing PLUGIN.BCCSP")

// testing PLUGIN Provider and ensuring other providers are not initialized
factoriesInitError = nil
assert.EqualError(t, err, "Failed initializing BCCSP: Could not initialize BCCSP SW [Failed initializing configuration at [0,]: Hash Family not supported []]")

err = initFactories(&FactoryOpts{
ProviderName: "PLUGIN",
SwOpts: &SwOpts{},
PluginOpts: &PluginOpts{},
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Failed initializing PLUGIN.BCCSP")
assert.NotContains(t, err.Error(), "Failed initializing BCCSP")

assert.EqualError(t, err, "Failed initializing PLUGIN.BCCSP: Could not initialize BCCSP PLUGIN [Invalid config: missing property 'Library']")
}
31 changes: 12 additions & 19 deletions bccsp/factory/pkcs11.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// +build pkcs11

/*
Copyright IBM Corp. 2017 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package factory

import (
Expand All @@ -23,6 +14,8 @@ import (
"github.com/pkg/errors"
)

const pkcs11Enabled = true

// FactoryOpts holds configuration information used to initialize factory implementations
type FactoryOpts struct {
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
Expand All @@ -37,13 +30,13 @@ type FactoryOpts struct {
// Error is returned only if defaultBCCSP cannot be found
func InitFactories(config *FactoryOpts) error {
factoriesInitOnce.Do(func() {
setFactories(config)
factoriesInitError = initFactories(config)
})

return factoriesInitError
}

func setFactories(config *FactoryOpts) error {
func initFactories(config *FactoryOpts) error {
// Take some precautions on default opts
if config == nil {
config = GetDefaultOpts()
Expand All @@ -65,7 +58,7 @@ func setFactories(config *FactoryOpts) error {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = errors.Wrap(err, "Failed initializing SW.BCCSP")
return errors.Wrap(err, "Failed initializing SW.BCCSP")
}
}

Expand All @@ -74,7 +67,7 @@ func setFactories(config *FactoryOpts) error {
f := &PKCS11Factory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = errors.Wrapf(err, "Failed initializing PKCS11.BCCSP %s", factoriesInitError)
return errors.Wrapf(err, "Failed initializing PKCS11.BCCSP")
}
}

Expand All @@ -83,17 +76,17 @@ func setFactories(config *FactoryOpts) error {
f := &PluginFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP %s", factoriesInitError)
return errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP")
}
}

var ok bool
defaultBCCSP, ok = bccspMap[config.ProviderName]
if !ok {
factoriesInitError = errors.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
return errors.Errorf("Could not find default `%s` BCCSP", config.ProviderName)
}

return factoriesInitError
return nil
}

// GetBCCSPFromOpts returns a BCCSP created according to the options passed in input.
Expand Down
Loading

0 comments on commit ca93be5

Please sign in to comment.