Skip to content

Commit

Permalink
[FAB-17517] Only Initialize specified provider
Browse files Browse the repository at this point in the history
Code currently tries to initialize multiple providers based on
provided config Opts being nil or not.

This update ensures that only specified provider is initialized
based on ProviderName.

This fixes "Failed initializing PKCS11.BCCSP %!s(<nil>)" error
when the code complied with PKCS11 or PLUGINS enabled expect
configuration to not be nil even when Provider is set to SW.

Also adding unit tests.

Signed-off-by: Ahmed Sajid <ahmed.sajid@securekey.com>
  • Loading branch information
ahmedsajid authored and mastersingh24 committed Mar 11, 2020
1 parent defb36c commit 2bb6415
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 37 deletions.
71 changes: 38 additions & 33 deletions bccsp/factory/nopkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,53 @@ type FactoryOpts struct {
// Error is returned only if defaultBCCSP cannot be found
func InitFactories(config *FactoryOpts) error {
factoriesInitOnce.Do(func() {
// Take some precautions on default opts
if config == nil {
config = GetDefaultOpts()
}
factoriesInitError = initFactories(config)
})

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

if config.SwOpts == nil {
config.SwOpts = GetDefaultOpts().SwOpts
}
func initFactories(config *FactoryOpts) error {
// Take some precautions on default opts
if config == nil {
config = GetDefaultOpts()
}

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

// Initialize factories map
bccspMap = make(map[string]bccsp.BCCSP)
if config.SwOpts == nil {
config.SwOpts = GetDefaultOpts().SwOpts
}

// Software-Based BCCSP
if config.SwOpts != nil {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = errors.Wrapf(err, "Failed initializing BCCSP.")
}
}
// Initialize factories map
bccspMap = make(map[string]bccsp.BCCSP)

// BCCSP Plugin
if config.PluginOpts != nil {
f := &PluginFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = errors.Wrapf(err, "Failed initializing PKCS11.BCCSP %s", factoriesInitError)
}
// Software-Based BCCSP
if config.ProviderName == "SW" && config.SwOpts != nil {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
return errors.Wrapf(err, "Failed initializing BCCSP.")
}
}

var ok bool
defaultBCCSP, ok = bccspMap[config.ProviderName]
if !ok {
factoriesInitError = errors.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
// BCCSP Plugin
if config.ProviderName == "PLUGIN" && config.PluginOpts != nil {
f := &PluginFactory{}
err := initBCCSP(f, config)
if err != nil {
return errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP %s", factoriesInitError)
}
})
}

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

// GetBCCSPFromOpts returns a BCCSP created according to the options passed in input.
Expand Down
51 changes: 51 additions & 0 deletions bccsp/factory/nopkcs11_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// +build !pkcs11

/*
Copyright IBM Corp. 2016 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.
*/
package factory

import (
"testing"

"github.com/stretchr/testify/assert"
)

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

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")

}
8 changes: 4 additions & 4 deletions bccsp/factory/pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func setFactories(config *FactoryOpts) error {
bccspMap = make(map[string]bccsp.BCCSP)

// Software-Based BCCSP
if config.SwOpts != nil {
if config.ProviderName == "SW" && config.SwOpts != nil {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
Expand All @@ -70,7 +70,7 @@ func setFactories(config *FactoryOpts) error {
}

// PKCS11-Based BCCSP
if config.Pkcs11Opts != nil {
if config.ProviderName == "PKCS11" && config.Pkcs11Opts != nil {
f := &PKCS11Factory{}
err := initBCCSP(f, config)
if err != nil {
Expand All @@ -79,11 +79,11 @@ func setFactories(config *FactoryOpts) error {
}

// BCCSP Plugin
if config.PluginOpts != nil {
if config.ProviderName == "PLUGIN" && config.PluginOpts != nil {
f := &PluginFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = errors.Wrapf(err, "Failed initializing PKCS11.BCCSP %s", factoriesInitError)
factoriesInitError = errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP %s", factoriesInitError)
}
}

Expand Down
45 changes: 45 additions & 0 deletions bccsp/factory/pkcs11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,51 @@ func TestSetFactories(t *testing.T) {
assert.NoError(t, err)
}

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

err := setFactories(&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

err = setFactories(&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")

// testing PLUGIN Provider and ensuring other providers are not initialized
factoriesInitError = nil

err = setFactories(&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")

}

func TestSetFactoriesInvalidArgs(t *testing.T) {
err := setFactories(&FactoryOpts{
ProviderName: "SW",
Expand Down

0 comments on commit 2bb6415

Please sign in to comment.