From ca93be5566ca62cbc14586b8898fc417c1e46fc7 Mon Sep 17 00:00:00 2001 From: Matthew Sykes Date: Wed, 11 Mar 2020 09:23:40 -0400 Subject: [PATCH] BCCSP initialization cleanup Change-Id: I74314ea8dd7eb8ba09fc0f1431e992ef30481963 Signed-off-by: Matthew Sykes --- bccsp/factory/factory.go | 9 ----- bccsp/factory/factory_test.go | 61 ++++++++++--------------------- bccsp/factory/nopkcs11.go | 23 ++++-------- bccsp/factory/nopkcs11_test.go | 30 +++------------ bccsp/factory/pkcs11.go | 31 ++++++---------- bccsp/factory/pkcs11_test.go | 67 +++++++++------------------------- 6 files changed, 63 insertions(+), 158 deletions(-) diff --git a/bccsp/factory/factory.go b/bccsp/factory/factory.go index 0075fa7bb39..ccfc36a841d 100644 --- a/bccsp/factory/factory.go +++ b/bccsp/factory/factory.go @@ -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 { diff --git a/bccsp/factory/factory_test.go b/bccsp/factory/factory_test.go index 1cde9d85122..9aea23993c6 100644 --- a/bccsp/factory/factory_test.go +++ b/bccsp/factory/factory_test.go @@ -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" } }`) @@ -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: @@ -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") @@ -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) } @@ -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) -} diff --git a/bccsp/factory/nopkcs11.go b/bccsp/factory/nopkcs11.go index a4c79288fae..3e143e13d8f 100644 --- a/bccsp/factory/nopkcs11.go +++ b/bccsp/factory/nopkcs11.go @@ -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 ( @@ -22,6 +13,8 @@ import ( "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"` @@ -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") } } @@ -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 } diff --git a/bccsp/factory/nopkcs11_test.go b/bccsp/factory/nopkcs11_test.go index 8bcc4700eb2..b652a59e92f 100644 --- a/bccsp/factory/nopkcs11_test.go +++ b/bccsp/factory/nopkcs11_test.go @@ -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 ( @@ -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']") } diff --git a/bccsp/factory/pkcs11.go b/bccsp/factory/pkcs11.go index 82691350509..efef9d2d845 100644 --- a/bccsp/factory/pkcs11.go +++ b/bccsp/factory/pkcs11.go @@ -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 ( @@ -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"` @@ -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() @@ -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") } } @@ -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") } } @@ -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. diff --git a/bccsp/factory/pkcs11_test.go b/bccsp/factory/pkcs11_test.go index 3a4b43b0fae..9fc8d1c2663 100644 --- a/bccsp/factory/pkcs11_test.go +++ b/bccsp/factory/pkcs11_test.go @@ -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 ( @@ -25,7 +16,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestInitFactories(t *testing.T) { +func TestExportedInitFactories(t *testing.T) { // Reset errors from previous negative test runs factoriesInitError = nil @@ -33,73 +24,52 @@ func TestInitFactories(t *testing.T) { assert.NoError(t, err) } -func TestSetFactories(t *testing.T) { - err := setFactories(nil) +func TestInitFactories(t *testing.T) { + err := initFactories(nil) assert.NoError(t, err) - err = setFactories(&FactoryOpts{}) + err = initFactories(&FactoryOpts{}) assert.NoError(t, err) } func TestSetFactoriesWithMultipleProviders(t *testing.T) { - // testing SW Provider and ensuring other providers are not initialized - factoriesInitError = nil - - err := setFactories(&FactoryOpts{ + err := initFactories(&FactoryOpts{ ProviderName: "SW", SwOpts: &SwOpts{}, Pkcs11Opts: &pkcs11.PKCS11Opts{}, PluginOpts: &PluginOpts{}, }) - assert.Error(t, err) - assert.Contains(t, err.Error(), "Failed initializing SW.BCCSP") - assert.NotContains(t, err.Error(), "Failed initializing PKCS11.BCCSP") - assert.NotContains(t, err.Error(), "Failed initializing PLUGIN.BCCSP") - - // testing PKCS11 Provider and ensuring other providers are not initialized - factoriesInitError = nil + assert.EqualError(t, err, "Failed initializing SW.BCCSP: Could not initialize BCCSP SW [Failed initializing configuration at [0,]: Hash Family not supported []]") - err = setFactories(&FactoryOpts{ + err = initFactories(&FactoryOpts{ ProviderName: "PKCS11", SwOpts: &SwOpts{}, Pkcs11Opts: &pkcs11.PKCS11Opts{}, PluginOpts: &PluginOpts{}, }) - assert.Error(t, err) - assert.Contains(t, err.Error(), "Failed initializing PKCS11.BCCSP") - assert.NotContains(t, err.Error(), "Failed initializing SW.BCCSP") - assert.NotContains(t, err.Error(), "Failed initializing PLUGIN.BCCSP") + assert.EqualError(t, err, "Failed initializing PKCS11.BCCSP: Could not initialize BCCSP PKCS11 [Failed initializing configuration: Hash Family not supported []]") - // testing PLUGIN Provider and ensuring other providers are not initialized - factoriesInitError = nil - - err = setFactories(&FactoryOpts{ + err = initFactories(&FactoryOpts{ ProviderName: "PLUGIN", SwOpts: &SwOpts{}, Pkcs11Opts: &pkcs11.PKCS11Opts{}, PluginOpts: &PluginOpts{}, }) - assert.Error(t, err) - assert.Contains(t, err.Error(), "Failed initializing PLUGIN.BCCSP") - assert.NotContains(t, err.Error(), "Failed initializing SW.BCCSP") - assert.NotContains(t, err.Error(), "Failed initializing PKCS11.BCCSP") - + assert.EqualError(t, err, "Failed initializing PLUGIN.BCCSP: Could not initialize BCCSP PLUGIN [Invalid config: missing property 'Library']") } func TestSetFactoriesInvalidArgs(t *testing.T) { - err := setFactories(&FactoryOpts{ + err := initFactories(&FactoryOpts{ ProviderName: "SW", SwOpts: &SwOpts{}, }) - assert.Error(t, err) - assert.Contains(t, err.Error(), "Failed initializing SW.BCCSP") + assert.EqualError(t, err, "Failed initializing SW.BCCSP: Could not initialize BCCSP SW [Failed initializing configuration at [0,]: Hash Family not supported []]") - err = setFactories(&FactoryOpts{ + err = initFactories(&FactoryOpts{ ProviderName: "PKCS11", Pkcs11Opts: &pkcs11.PKCS11Opts{}, }) - assert.Error(t, err) - assert.Contains(t, err.Error(), "Failed initializing PKCS11.BCCSP") + assert.EqualError(t, err, "Failed initializing PKCS11.BCCSP: Could not initialize BCCSP PKCS11 [Failed initializing configuration: Hash Family not supported []]") } func TestGetBCCSPFromOpts(t *testing.T) { @@ -128,7 +98,6 @@ func TestGetBCCSPFromOpts(t *testing.T) { csp, err = GetBCCSPFromOpts(&FactoryOpts{ ProviderName: "BadName", }) - assert.Error(t, err) - assert.Contains(t, err.Error(), "Could not find BCCSP, no 'BadName' provider") + assert.EqualError(t, err, "Could not find BCCSP, no 'BadName' provider") assert.Nil(t, csp) }