Skip to content

Commit

Permalink
[FAB-9540] bccsp provider type - case insensitive
Browse files Browse the repository at this point in the history
Change-Id: I956ce938785a44a27baaf106349c11ca5f2c4e15
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Apr 19, 2018
1 parent 95c8574 commit f7ec0aa
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 16 deletions.
11 changes: 11 additions & 0 deletions pkg/core/config/lookup/lookup.go
Expand Up @@ -9,6 +9,8 @@ package lookup
import (
"time"

"strings"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cast"
Expand Down Expand Up @@ -63,6 +65,15 @@ func (c *ConfigLookup) GetString(key string) string {
return cast.ToString(value)
}

//GetLowerString returns lower case string value for given key
func (c *ConfigLookup) GetLowerString(key string) string {
value, ok := c.backend.Lookup(key)
if !ok {
return ""
}
return strings.ToLower(cast.ToString(value))
}

//GetInt returns int value for given key
func (c *ConfigLookup) GetInt(key string) int {
value, ok := c.backend.Lookup(key)
Expand Down
18 changes: 18 additions & 0 deletions pkg/core/config/lookup/lookup_test.go
Expand Up @@ -55,12 +55,27 @@ func TestGetInt(t *testing.T) {
func TestGetString(t *testing.T) {
testLookup := New(backend)
assert.True(t, testLookup.GetString("key.string.valid") == "valid-string", "expected lookup to return valid string value")
assert.True(t, testLookup.GetString("key.string.valid.lower.case") == "valid-string", "expected lookup to return valid string value")
assert.True(t, testLookup.GetString("key.string.valid.upper.case") == "VALID-STRING", "expected lookup to return valid string value")
assert.True(t, testLookup.GetString("key.string.valid.mixed.case") == "VaLiD-StRiNg", "expected lookup to return valid string value")
assert.True(t, testLookup.GetString("key.string.empty") == "", "expected lookup to return empty string value")
assert.True(t, testLookup.GetString("key.string.nil") == "", "expected lookup to return empty string value")
assert.True(t, testLookup.GetString("key.string.number") == "1234", "expected lookup to return valid string value")
assert.True(t, testLookup.GetString("key.string.not existing") == "", "expected lookup to return empty string value")
}

func TestGetLowerString(t *testing.T) {
testLookup := New(backend)
assert.True(t, testLookup.GetLowerString("key.string.valid") == "valid-string", "expected lookup to return valid lowercase string value")
assert.True(t, testLookup.GetLowerString("key.string.valid.lower.case") == "valid-string", "expected lookup to return valid lowercase string value")
assert.True(t, testLookup.GetLowerString("key.string.valid.upper.case") == "valid-string", "expected lookup to return valid lowercase string value")
assert.True(t, testLookup.GetLowerString("key.string.valid.mixed.case") == "valid-string", "expected lookup to return valid lowercase string value")
assert.True(t, testLookup.GetLowerString("key.string.empty") == "", "expected lookup to return empty string value")
assert.True(t, testLookup.GetLowerString("key.string.nil") == "", "expected lookup to return empty string value")
assert.True(t, testLookup.GetLowerString("key.string.number") == "1234", "expected lookup to return valid string value")
assert.True(t, testLookup.GetLowerString("key.string.not existing") == "", "expected lookup to return empty string value")
}

func TestGetDuration(t *testing.T) {
testLookup := New(backend)
assert.True(t, testLookup.GetDuration("key.duration.valid.hour").String() == (24*time.Hour).String(), "expected valid time value")
Expand Down Expand Up @@ -219,6 +234,9 @@ func setupCustomBackend() {
backendMap["key.int.invalid"] = "INVALID"

backendMap["key.string.valid"] = "valid-string"
backendMap["key.string.valid.mixed.case"] = "VaLiD-StRiNg"
backendMap["key.string.valid.lower.case"] = "valid-string"
backendMap["key.string.valid.upper.case"] = "VALID-STRING"
backendMap["key.string.empty"] = ""
backendMap["key.string.nil"] = nil
backendMap["key.string.number"] = 1234
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/cryptosuite/bccsp/multisuite/cryptosuiteimpl.go
Expand Up @@ -16,9 +16,9 @@ import (
//GetSuiteByConfig returns cryptosuite adaptor for bccsp loaded according to given config
func GetSuiteByConfig(config core.CryptoSuiteConfig) (core.CryptoSuite, error) {
switch config.SecurityProvider() {
case "SW":
case "sw":
return sw.GetSuiteByConfig(config)
case "PKCS11":
case "pkcs11":
return pkcs11.GetSuiteByConfig(config)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/core/cryptosuite/bccsp/multisuite/cryptosuiteimpl_test.go
Expand Up @@ -37,8 +37,8 @@ func TestCryptoSuiteByConfigSW(t *testing.T) {
defer mockCtrl.Finish()

mockConfig := mockcore.NewMockCryptoSuiteConfig(mockCtrl)
mockConfig.EXPECT().SecurityProvider().Return("SW")
mockConfig.EXPECT().SecurityProvider().Return("SW")
mockConfig.EXPECT().SecurityProvider().Return("sw")
mockConfig.EXPECT().SecurityProvider().Return("sw")
mockConfig.EXPECT().SecurityAlgorithm().Return("SHA2")
mockConfig.EXPECT().SecurityLevel().Return(256)
mockConfig.EXPECT().KeyStorePath().Return("/tmp/msp")
Expand All @@ -61,8 +61,8 @@ func TestCryptoSuiteByConfigPKCS11(t *testing.T) {
providerLib, softHSMPin, softHSMTokenLabel := pkcs11.FindPKCS11Lib()

mockConfig := mockcore.NewMockCryptoSuiteConfig(mockCtrl)
mockConfig.EXPECT().SecurityProvider().Return("PKCS11")
mockConfig.EXPECT().SecurityProvider().Return("PKCS11")
mockConfig.EXPECT().SecurityProvider().Return("pkcs11")
mockConfig.EXPECT().SecurityProvider().Return("pkcs11")
mockConfig.EXPECT().SecurityAlgorithm().Return("SHA2")
mockConfig.EXPECT().SecurityLevel().Return(256)
mockConfig.EXPECT().KeyStorePath().Return("/tmp/msp")
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/cryptosuite/bccsp/pkcs11/cryptosuiteimpl.go
Expand Up @@ -21,7 +21,7 @@ var logger = logging.NewLogger("fabsdk/core")
//GetSuiteByConfig returns cryptosuite adaptor for bccsp loaded according to given config
func GetSuiteByConfig(config core.CryptoSuiteConfig) (core.CryptoSuite, error) {
// TODO: delete this check?
if config.SecurityProvider() != "PKCS11" {
if config.SecurityProvider() != "pkcs11" {
return nil, errors.Errorf("Unsupported BCCSP Provider: %s", config.SecurityProvider())
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/core/cryptosuite/bccsp/pkcs11/cryptosuiteimpl_test.go
Expand Up @@ -51,7 +51,7 @@ func TestCryptoSuiteByConfigPKCS11(t *testing.T) {
providerLib, softHSMPin, softHSMTokenLabel := pkcs11.FindPKCS11Lib()

mockConfig := mockcore.NewMockCryptoSuiteConfig(mockCtrl)
mockConfig.EXPECT().SecurityProvider().Return("PKCS11")
mockConfig.EXPECT().SecurityProvider().Return("pkcs11")
mockConfig.EXPECT().SecurityAlgorithm().Return("SHA2")
mockConfig.EXPECT().SecurityLevel().Return(256)
mockConfig.EXPECT().KeyStorePath().Return("/tmp/msp")
Expand All @@ -76,7 +76,7 @@ func TestCryptoSuiteByConfigPKCS11Failure(t *testing.T) {
defer mockCtrl.Finish()
//Prepare Config
mockConfig := mockcore.NewMockCryptoSuiteConfig(mockCtrl)
mockConfig.EXPECT().SecurityProvider().Return("PKCS11")
mockConfig.EXPECT().SecurityProvider().Return("pkcs11")
mockConfig.EXPECT().SecurityAlgorithm().Return("SHA2")
mockConfig.EXPECT().SecurityLevel().Return(256)
mockConfig.EXPECT().KeyStorePath().Return("/tmp/msp")
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/cryptosuite/bccsp/sw/cryptosuiteimpl.go
Expand Up @@ -21,7 +21,7 @@ var logger = logging.NewLogger("fabsdk/core")
//GetSuiteByConfig returns cryptosuite adaptor for bccsp loaded according to given config
func GetSuiteByConfig(config core.CryptoSuiteConfig) (core.CryptoSuite, error) {
// TODO: delete this check?
if config.SecurityProvider() != "SW" {
if config.SecurityProvider() != "sw" {
return nil, errors.Errorf("Unsupported BCCSP Provider: %s", config.SecurityProvider())
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/core/cryptosuite/bccsp/sw/cryptosuiteimpl_test.go
Expand Up @@ -37,7 +37,7 @@ func TestCryptoSuiteByConfigSW(t *testing.T) {
defer mockCtrl.Finish()

mockConfig := mockcore.NewMockCryptoSuiteConfig(mockCtrl)
mockConfig.EXPECT().SecurityProvider().Return("SW")
mockConfig.EXPECT().SecurityProvider().Return("sw").AnyTimes()
mockConfig.EXPECT().SecurityAlgorithm().Return("SHA2")
mockConfig.EXPECT().SecurityLevel().Return(256)
mockConfig.EXPECT().KeyStorePath().Return("/tmp/msp")
Expand All @@ -56,7 +56,7 @@ func TestCryptoSuiteByBadConfigSW(t *testing.T) {
defer mockCtrl.Finish()

mockConfig := mockcore.NewMockCryptoSuiteConfig(mockCtrl)
mockConfig.EXPECT().SecurityProvider().Return("SW")
mockConfig.EXPECT().SecurityProvider().Return("sw")
mockConfig.EXPECT().SecurityAlgorithm().Return("SHA0")
mockConfig.EXPECT().SecurityLevel().Return(256)
mockConfig.EXPECT().KeyStorePath().Return("")
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/cryptosuite/cryptoconfig.go
Expand Up @@ -43,7 +43,7 @@ func (c *Config) SecurityLevel() int {

//SecurityProvider provider SW or PKCS11
func (c *Config) SecurityProvider() string {
return c.backend.GetString("client.BCCSP.security.default.provider")
return c.backend.GetLowerString("client.BCCSP.security.default.provider")
}

//SoftVerify flag
Expand Down
54 changes: 52 additions & 2 deletions pkg/core/cryptosuite/cryptoconfig_test.go
Expand Up @@ -10,6 +10,10 @@ import (
"path"
"testing"

"os"

"strings"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/core/mocks"
Expand Down Expand Up @@ -113,8 +117,8 @@ func TestCAConfigSecurityProvider(t *testing.T) {
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(string) != cryptoConfig.SecurityProvider() {
t.Fatalf("Incorrect BCCSP SecurityProvider provider")
if !strings.EqualFold(val.(string), cryptoConfig.SecurityProvider()) {
t.Fatalf("Incorrect BCCSP SecurityProvider provider : %s", cryptoConfig.SecurityProvider())
}
}

Expand Down Expand Up @@ -178,6 +182,52 @@ func TestCAConfigSecurityProviderLabel(t *testing.T) {
}
}

func TestCAConfigSecurityProviderCase(t *testing.T) {

// we expect the following values
const expectedPkcs11Value = "pkcs11"
const expectedSwValue = "sw"

// map key represents what we will input
providerTestValues := map[string]string{
// all upper case
"SW": expectedSwValue,
"PKCS11": expectedPkcs11Value,
// all lower case
"sw": expectedSwValue,
"pkcs11": expectedPkcs11Value,
// mixed case
"Sw": expectedSwValue,
"Pkcs11": expectedPkcs11Value,
}

for inputValue, expectedValue := range providerTestValues {

// set the input value, overriding what's in file
os.Setenv("FABRIC_SDK_CLIENT_BCCSP_SECURITY_DEFAULT_PROVIDER", inputValue)

backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// expected values should be uppercase
if expectedValue != cryptoConfig.SecurityProvider() {
t.Fatalf(
"Incorrect BCCSP SecurityProvider - input:%s actual:%s, expected:%s",
inputValue,
cryptoConfig.SecurityProvider(),
expectedValue,
)
}

}
}

//getCustomBackend returns custom backend to override config values and to avoid using new config file for test scenarios
func getCustomBackend(configBackend core.ConfigBackend) *mocks.MockConfigBackend {
backendMap := make(map[string]interface{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/fab/mocks/mockconfig.go
Expand Up @@ -294,7 +294,7 @@ func (c *MockConfig) NetworkPeers() ([]fab.NetworkPeer, error) {

// SecurityProvider ...
func (c *MockConfig) SecurityProvider() string {
return "SW"
return "sw"
}

// SecurityProviderLabel ...
Expand Down

0 comments on commit f7ec0aa

Please sign in to comment.