Skip to content

Commit

Permalink
Fix the Ability to Override CredentialStorePath (#85)
Browse files Browse the repository at this point in the history
The existing implementation pulls the value from the config
file. If a user overrides the CredentialStorePath it does not
supersede the value defined in client.config.credentialStore.Path.
Moreover, if a user performs a configless configuration of the SDK
where there is no configfile, the value is read in as "", and an
inMemoryStore is used as opposed to the expected override.

This change ensures the overridden value supersedes the config value
but retains the config value if no override is passed.

Signed-off-by: Brett Logan <brett.t.logan@ibm.com>

Co-authored-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
Brett Logan and troyronda authored Jul 3, 2020
1 parent d44e5d6 commit d716237
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
24 changes: 11 additions & 13 deletions pkg/fabsdk/factory/defmsp/mspfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@ func NewProviderFactory() *ProviderFactory {

// CreateUserStore creates a UserStore using the SDK's default implementation
func (f *ProviderFactory) CreateUserStore(config msp.IdentityConfig) (msp.UserStore, error) {

stateStorePath := config.Client().CredentialStore.Path
stateStorePath := config.CredentialStorePath()

var userStore msp.UserStore

if stateStorePath == "" {
userStore = mspimpl.NewMemoryUserStore()
} else {
stateStore, err := kvs.New(&kvs.FileKeyValueStoreOptions{Path: stateStorePath})
if err != nil {
return nil, errors.WithMessage(err, "CreateNewFileKeyValueStore failed")
}
userStore, err = mspimpl.NewCertFileUserStore1(stateStore)
if err != nil {
return nil, errors.Wrapf(err, "creating a user store failed")
}
return mspimpl.NewMemoryUserStore(), nil
}

stateStore, err := kvs.New(&kvs.FileKeyValueStoreOptions{Path: stateStorePath})
if err != nil {
return nil, errors.WithMessage(err, "CreateNewFileKeyValueStore failed")
}
userStore, err = mspimpl.NewCertFileUserStore1(stateStore)
if err != nil {
return nil, errors.Wrapf(err, "creating a user store failed")
}

return userStore, nil
Expand Down
26 changes: 6 additions & 20 deletions pkg/fabsdk/factory/defmsp/mspfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,12 @@ func TestCreateUserStore(t *testing.T) {
}

func newMockUserStore(t *testing.T) msp.UserStore {
factory := NewProviderFactory()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockConfig := mockmsp.NewMockIdentityConfig(mockCtrl)
mockConfig.EXPECT().CredentialStorePath().Return("/tmp/fabsdkgo_test/store")

mockClientConfig := msp.ClientConfig{
CredentialStore: msp.CredentialStoreType{
Path: "/tmp/fabsdkgo_test/store",
},
}
mockConfig.EXPECT().Client().Return(&mockClientConfig)

factory := NewProviderFactory()
userStore, err := factory.CreateUserStore(mockConfig)
if err != nil {
t.Fatalf("Unexpected error creating user store %s", err)
Expand All @@ -64,39 +57,32 @@ func newMockUserStore(t *testing.T) msp.UserStore {

func TestCreateUserStoreByConfig(t *testing.T) {
userStore := newMockUserStore(t)

_, ok := userStore.(*mspimpl.CertFileUserStore)
if !ok {
t.Fatal("Unexpected user store created")
}
}

func TestCreateUserStoreEmptyConfig(t *testing.T) {
factory := NewProviderFactory()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockConfig := mockmsp.NewMockIdentityConfig(mockCtrl)
mockConfig.EXPECT().CredentialStorePath().Return("")

mockClientConfig := msp.ClientConfig{}
mockConfig.EXPECT().Client().Return(&mockClientConfig)

factory := NewProviderFactory()
_, err := factory.CreateUserStore(mockConfig)
if err != nil {
t.Fatal("Expected user store created")
}
}

func TestCreateUserStoreFailConfig(t *testing.T) {
factory := NewProviderFactory()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockConfig := mockmsp.NewMockIdentityConfig(mockCtrl)
mockConfig.EXPECT().CredentialStorePath().Return("")

mockClientConfig := msp.ClientConfig{}
mockConfig.EXPECT().Client().Return(&mockClientConfig)

factory := NewProviderFactory()
_, err := factory.CreateUserStore(mockConfig)
if err != nil {
t.Fatal("Expected user store created")
Expand Down

0 comments on commit d716237

Please sign in to comment.