Skip to content

Commit

Permalink
[FAB-8553] Drop CredentialManager interface
Browse files Browse the repository at this point in the history
The only method, GetSigningIdentity, is already a part of the
IdentityManager interface.

OrganizationFactory is also removed, as its only function was
to create a CredentialManager instance.

Change-Id: I95ebbe229a962362cdabbff2e72ff2420d5460b3
Signed-off-by: Aleksandar Likic <[email protected]>
  • Loading branch information
Aleksandar Likic committed Feb 27, 2018
1 parent 4dff6ab commit 6154281
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 260 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ dockerenv-latest-up: clean
mock-gen:
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/pkg/context/api/core Config | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > pkg/context/api/core/mocks/mockconfig.gen.go
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab ProposalProcessor,IdentityManager | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > pkg/context/api/fab/mocks/mockcontextapi.gen.go
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api CoreProviders,SvcProviders,Providers,CoreProviderFactory,ServiceProviderFactory,OrgClientFactory,SessionClientFactory | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > pkg/fabsdk/mocks/mockfabsdkapi.gen.go
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api CoreProviders,SvcProviders,Providers,CoreProviderFactory,ServiceProviderFactory,SessionClientFactory | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > pkg/fabsdk/mocks/mockfabsdkapi.gen.go


# TODO - Add cryptogen
Expand Down
5 changes: 0 additions & 5 deletions pkg/context/api/credentialmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,3 @@ type SigningIdentity struct {
EnrollmentCert []byte
PrivateKey core.Key
}

// CredentialManager retrieves user's signing identity
type CredentialManager interface {
GetSigningIdentity(name string) (*SigningIdentity, error)
}
4 changes: 2 additions & 2 deletions pkg/context/api/fab/identitymgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

// IdentityManager provides management of identities in a Fabric network
type IdentityManager interface {
contextApi.CredentialManager
CAName() string
GetSigningIdentity(name string) (*contextApi.SigningIdentity, error)
Enroll(enrollmentID string, enrollmentSecret string) (core.Key, []byte, error)
Reenroll(user contextApi.User) (core.Key, []byte, error)
Register(request *RegistrationRequest) (string, error)
Revoke(request *RevocationRequest) (*RevocationResponse, error)
CAName() string
}

// AttributeRequest is a request for an attribute.
Expand Down
13 changes: 13 additions & 0 deletions pkg/context/api/fab/mocks/mockcontextapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions pkg/fab/identitymgr/credentialmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

fabricCaUtil "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/util"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite/bccsp/sw"
Expand Down Expand Up @@ -45,7 +46,7 @@ XdsmTcdRvJ3TS/6HCA==

var msp = "Org1"

func TestCredentialManager(t *testing.T) {
func TestGetSigningIdentity(t *testing.T) {

config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")()
if err != nil {
Expand Down Expand Up @@ -84,25 +85,25 @@ func TestCredentialManager(t *testing.T) {
t.Fatalf("Failed to setup userStore: %s", err)
}

credentialMgr, err := New(msp, config, cryptoSuite)
mgr, err := New(msp, config, cryptoSuite)
if err != nil {
t.Fatalf("Failed to setup credential manager: %s", err)
}

_, err = credentialMgr.GetSigningIdentity("")
_, err = mgr.GetSigningIdentity("")
if err == nil {
t.Fatalf("Should have failed to retrieve signing identity for empty user name")
}

_, err = credentialMgr.GetSigningIdentity("Non-Existent")
_, err = mgr.GetSigningIdentity("Non-Existent")
if err == nil {
t.Fatalf("Should have failed to retrieve signing identity for non-existent user")
}

testUserName := createRandomName()

// Should not find the user
if err := checkSigningIdentity(credentialMgr, testUserName); err != api.ErrUserNotFound {
if err := checkSigningIdentity(mgr, testUserName); err != api.ErrUserNotFound {
t.Fatalf("expected ErrUserNotFound, got: %s", err)
}

Expand All @@ -119,13 +120,13 @@ func TestCredentialManager(t *testing.T) {
}

// Should succeed after enrollment
if err := checkSigningIdentity(credentialMgr, testUserName); err != nil {
if err := checkSigningIdentity(mgr, testUserName); err != nil {
t.Fatalf("checkSigningIdentity failed: %s", err)
}
}

func checkSigningIdentity(credentialMgr api.CredentialManager, user string) error {
id, err := credentialMgr.GetSigningIdentity(user)
func checkSigningIdentity(mgr fab.IdentityManager, user string) error {
id, err := mgr.GetSigningIdentity(user)
if err == api.ErrUserNotFound {
return err
}
Expand All @@ -148,7 +149,7 @@ func checkSigningIdentity(credentialMgr api.CredentialManager, user string) erro
return nil
}

func TestInvalidOrgCredentialManager(t *testing.T) {
func TestGetSigningIdentityInvalidOrg(t *testing.T) {

config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")()
if err != nil {
Expand All @@ -163,41 +164,41 @@ func TestInvalidOrgCredentialManager(t *testing.T) {

}

func TestCredentialManagerFromEmbeddedCryptoConfig(t *testing.T) {
func TestGetSigningIdentityFromEmbeddedCryptoConfig(t *testing.T) {
config, err := config.FromFile("../../../test/fixtures/config/config_test_embedded_pems.yaml")()

if err != nil {
t.Fatalf(err.Error())
}

credentialMgr, err := New(msp, config, cryptosuite.GetDefault())
mgr, err := New(msp, config, cryptosuite.GetDefault())
if err != nil {
t.Fatalf("Failed to setup credential manager: %s", err)
}

_, err = credentialMgr.GetSigningIdentity("")
_, err = mgr.GetSigningIdentity("")
if err == nil {
t.Fatalf("Should get error for empty user name")
}

_, err = credentialMgr.GetSigningIdentity("Non-Existent")
_, err = mgr.GetSigningIdentity("Non-Existent")
if err != api.ErrUserNotFound {
t.Fatalf("Should get ErrUserNotFound for non-existent user, got %v", err)
}

if err := checkSigningIdentity(credentialMgr, "EmbeddedUser"); err != nil {
if err := checkSigningIdentity(mgr, "EmbeddedUser"); err != nil {
t.Fatalf("checkSigningIdentity failes: %s", err)
}

if err := checkSigningIdentity(credentialMgr, "EmbeddedUserWithPaths"); err != nil {
if err := checkSigningIdentity(mgr, "EmbeddedUserWithPaths"); err != nil {
t.Fatalf("checkSigningIdentity failes: %s", err)
}

if err := checkSigningIdentity(credentialMgr, "EmbeddedUserMixed"); err != nil {
if err := checkSigningIdentity(mgr, "EmbeddedUserMixed"); err != nil {
t.Fatalf("checkSigningIdentity failes: %s", err)
}

if err := checkSigningIdentity(credentialMgr, "EmbeddedUserMixed2"); err != nil {
if err := checkSigningIdentity(mgr, "EmbeddedUserMixed2"); err != nil {
t.Fatalf("checkSigningIdentity failes: %s", err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fab/identitymgr/enrollment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite/bccsp/sw"
)

func TestCredentialManagerWithEnrollment(t *testing.T) {
func TestGetSigningIdentityWithEnrollment(t *testing.T) {
config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")()
if err != nil {
t.Fatalf(err.Error())
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestCredentialManagerWithEnrollment(t *testing.T) {
t.Fatalf("checkSigningIdentity failed: %s", err)
}

// Refers to the same location used by the CredentialManager
// Refers to the same location used by the IdentityManager
userStore, err := identity.NewCertFileUserStore(clientCofig.CredentialStore.Path, cs)
if err != nil {
t.Fatalf("Failed to setup userStore: %s", err)
Expand Down
29 changes: 0 additions & 29 deletions pkg/fab/mocks/mockcredentialmgr.go

This file was deleted.

56 changes: 56 additions & 0 deletions pkg/fab/mocks/mockidentitymgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package mocks

import (
"github.com/hyperledger/fabric-sdk-go/pkg/context/api"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/pkg/errors"
)

// MockIdentityManager is a mock IdentityManager
type MockIdentityManager struct {
}

// NewMockIdentityManager Constructor for a identity manager.
func NewMockIdentityManager(orgName string, config core.Config, cryptoProvider core.CryptoSuite) (fab.IdentityManager, error) {
mcm := MockIdentityManager{}
return &mcm, nil
}

// GetSigningIdentity will return an identity that can be used to cryptographically sign an object
func (mgr *MockIdentityManager) GetSigningIdentity(userName string) (*api.SigningIdentity, error) {

si := api.SigningIdentity{}
return &si, nil
}

// Enroll enrolls a user with a Fabric network
func (mgr *MockIdentityManager) Enroll(enrollmentID string, enrollmentSecret string) (core.Key, []byte, error) {
return nil, nil, errors.New("not implemented")
}

// Reenroll re-enrolls a user
func (mgr *MockIdentityManager) Reenroll(user api.User) (core.Key, []byte, error) {
return nil, nil, errors.New("not implemented")
}

// Register registers a user with a Fabric network
func (mgr *MockIdentityManager) Register(request *fab.RegistrationRequest) (string, error) {
return "", errors.New("not implemented")
}

// Revoke revokes a user
func (mgr *MockIdentityManager) Revoke(request *fab.RevocationRequest) (*fab.RevocationResponse, error) {
return nil, errors.New("not implemented")
}

// CAName return the name of a CA associated with this identity manager
func (mgr *MockIdentityManager) CAName() string {
return ""
}
6 changes: 0 additions & 6 deletions pkg/fabsdk/api/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ type ServiceProviderFactory interface {
//CreateChannelProvider(ctx Context, channelID string) (ChannelProvider, error)
}

// OrgClientFactory allows overriding default clients and providers of an organization
// Currently, a context is created for each organization that the client app needs.
type OrgClientFactory interface {
CreateCredentialManager(orgName string, config core.Config, cryptoProvider core.CryptoSuite) (api.CredentialManager, error)
}

// SessionClientFactory allows overriding default clients and providers of a session
type SessionClientFactory interface {
CreateChannelClient(sdk Providers, session context.SessionContext, channelID string, targetFilter fab.TargetFilter) (*channel.Client, error)
Expand Down
4 changes: 0 additions & 4 deletions pkg/fabsdk/defpkgsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ func (ps *defPkgSuite) Service() (sdkApi.ServiceProviderFactory, error) {
return defsvc.NewProviderFactory(), nil
}

func (ps *defPkgSuite) Context() (sdkApi.OrgClientFactory, error) {
return defclient.NewOrgClientFactory(), nil
}

func (ps *defPkgSuite) Session() (sdkApi.SessionClientFactory, error) {
return defclient.NewSessionClientFactory(), nil
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/fabsdk/defpkgsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ func TestNewPkgSuite(t *testing.T) {
t.Fatalf("Core is nil")
}

context, err := pkgsuite.Context()
if err != nil {
t.Fatalf("Unexpected error getting default context factory")
}
if context == nil {
t.Fatalf("Context is nil")
}

session, err := pkgsuite.Session()
if err != nil {
t.Fatalf("Unexpected error getting default session factory")
Expand Down
19 changes: 2 additions & 17 deletions pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type FabricSDK struct {
type options struct {
Core sdkApi.CoreProviderFactory
Service sdkApi.ServiceProviderFactory
Context sdkApi.OrgClientFactory
Session sdkApi.SessionClientFactory
Logger api.LoggerProvider
}
Expand Down Expand Up @@ -82,11 +81,6 @@ func fromPkgSuite(config core.Config, pkgSuite PkgSuite, opts ...Option) (*Fabri
return nil, errors.WithMessage(err, "Unable to initialize service pkg")
}

ctx, err := pkgSuite.Context()
if err != nil {
return nil, errors.WithMessage(err, "Unable to initialize context pkg")
}

sess, err := pkgSuite.Session()
if err != nil {
return nil, errors.WithMessage(err, "Unable to initialize session pkg")
Expand All @@ -101,7 +95,6 @@ func fromPkgSuite(config core.Config, pkgSuite PkgSuite, opts ...Option) (*Fabri
opts: options{
Core: core,
Service: svc,
Context: ctx,
Session: sess,
Logger: lg,
},
Expand Down Expand Up @@ -132,14 +125,6 @@ func WithServicePkg(service sdkApi.ServiceProviderFactory) Option {
}
}

// WithContextPkg injects the context implementation into the SDK.
func WithContextPkg(context sdkApi.OrgClientFactory) Option {
return func(opts *options) error {
opts.Context = context
return nil
}
}

// WithSessionPkg injects the session implementation into the SDK.
func WithSessionPkg(session sdkApi.SessionClientFactory) Option {
return func(opts *options) error {
Expand Down Expand Up @@ -261,12 +246,12 @@ func (sdk *FabricSDK) context() *sdkContext {

func (sdk *FabricSDK) newUser(orgID string, userName string) (context.IdentityContext, error) {

credentialMgr, err := sdk.opts.Context.CreateCredentialManager(orgID, sdk.config, sdk.cryptoSuite)
identityMgr, err := sdk.FabricProvider().CreateIdentityManager(orgID)
if err != nil {
return nil, errors.WithMessage(err, "failed to get credential manager")
}

signingIdentity, err := credentialMgr.GetSigningIdentity(userName)
signingIdentity, err := identityMgr.GetSigningIdentity(userName)
if err != nil {
return nil, errors.WithMessage(err, "failed to get signing identity")
}
Expand Down
Loading

0 comments on commit 6154281

Please sign in to comment.