Skip to content

Commit

Permalink
[FAB-6324] MSP versioning
Browse files Browse the repository at this point in the history
This change-set does the following:
- Introduce an MSP factory that allows
the creation of MSP instances based on passed options.
An option specifies the MSP's type to be created
and its version.
This is to address backwards incompatibility
by proving the ability to change the MSP version to be used.

Change-Id: I7dcfdd783260d98896487147e50cd0e8ea62971c
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Oct 11, 2017
1 parent fe125bd commit 40c423f
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 33 deletions.
12 changes: 6 additions & 6 deletions common/channelconfig/msp.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ func (bh *MSPConfigHandler) ProposeMSP(mspConfig *mspprotos.MSPConfig) (msp.MSP,
}

// create the msp instance
bccspMSP, err := msp.NewBccspMsp()
mspInst, err := msp.New(&msp.BCCSPNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: msp.MSPv1_0}})
if err != nil {
return nil, fmt.Errorf("Creating the MSP manager failed, err %s", err)
}

mspInst, err := cache.New(bccspMSP)
casheMSP, err := cache.New(mspInst)
if err != nil {
return nil, fmt.Errorf("Creating the MSP manager failed, err %s", err)
}

// set it up
err = mspInst.Setup(mspConfig)
err = casheMSP.Setup(mspConfig)
if err != nil {
return nil, fmt.Errorf("Setting up the MSP manager failed, err %s", err)
}

// add the MSP to the map of pending MSPs
mspID, _ := mspInst.GetIdentifier()
mspID, _ := casheMSP.GetIdentifier()

existingPendingMSPConfig, ok := bh.idMap[mspID]
if ok && !proto.Equal(existingPendingMSPConfig.mspConfig, mspConfig) {
Expand All @@ -67,11 +67,11 @@ func (bh *MSPConfigHandler) ProposeMSP(mspConfig *mspprotos.MSPConfig) (msp.MSP,
if !ok {
bh.idMap[mspID] = &pendingMSPConfig{
mspConfig: mspConfig,
msp: mspInst,
msp: casheMSP,
}
}

return mspInst, nil
return casheMSP, nil
}

func (bh *MSPConfigHandler) CreateMSPManager() (msp.MSPManager, error) {
Expand Down
8 changes: 4 additions & 4 deletions common/channelconfig/msp_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ func TemplateGroupMSPWithAdminRolePrincipal(configPath []string, mspConfig *mspp
}

// create the msp instance
bccspMSP, err := msp.NewBccspMsp()
mspInst, err := msp.New(&msp.BCCSPNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: msp.MSPv1_0}})
if err != nil {
logger.Panicf("Creating the MSP manager failed, err %s", err)
}

mspInst, err := cache.New(bccspMSP)
cacheMSP, err := cache.New(mspInst)
if err != nil {
logger.Panicf("Creating the MSP manager failed, err %s", err)
}

// set it up
err = mspInst.Setup(mspConfig)
err = cacheMSP.Setup(mspConfig)
if err != nil {
logger.Panicf("Setting up the MSP manager failed, err %s", err)
}

// add the MSP to the map of pending MSPs
mspID, _ := mspInst.GetIdentifier()
mspID, _ := cacheMSP.GetIdentifier()

memberPolicy := &cb.ConfigPolicy{
Policy: &cb.Policy{
Expand Down
4 changes: 2 additions & 2 deletions common/tools/cryptogen/msp/msp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestGenerateLocalMSP(t *testing.T) {
// finally check to see if we can load this as a local MSP config
testMSPConfig, err := fabricmsp.GetLocalMspConfig(mspDir, nil, testName)
assert.NoError(t, err, "Error parsing local MSP config")
testMSP, err := fabricmsp.NewBccspMsp()
testMSP, err := fabricmsp.New(&fabricmsp.BCCSPNewOpts{NewBaseOpts: fabricmsp.NewBaseOpts{Version: fabricmsp.MSPv1_0}})
assert.NoError(t, err, "Error creating new BCCSP MSP")
err = testMSP.Setup(testMSPConfig)
assert.NoError(t, err, "Error setting up local MSP")
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestGenerateVerifyingMSP(t *testing.T) {
// finally check to see if we can load this as a verifying MSP config
testMSPConfig, err := fabricmsp.GetVerifyingMspConfig(mspDir, testName)
assert.NoError(t, err, "Error parsing verifying MSP config")
testMSP, err := fabricmsp.NewBccspMsp()
testMSP, err := fabricmsp.New(&fabricmsp.BCCSPNewOpts{NewBaseOpts: fabricmsp.NewBaseOpts{Version: fabricmsp.MSPv1_0}})
assert.NoError(t, err, "Error creating new BCCSP MSP")
err = testMSP.Setup(testMSPConfig)
assert.NoError(t, err, "Error setting up verifying MSP")
Expand Down
65 changes: 65 additions & 0 deletions msp/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package msp

import (
"github.com/pkg/errors"
)

type MSPVersion int

const (
MSPv1_0 = iota
MSPv1_1
)

// NewOpts represent
type NewOpts interface {
// GetVersion returns the MSP's version to be instantiated
GetVersion() MSPVersion
}

// NewBaseOpts is the default base type for all MSP instantiation Opts
type NewBaseOpts struct {
Version MSPVersion
}

func (o *NewBaseOpts) GetVersion() MSPVersion {
return o.Version
}

// BCCSPNewOpts contains the options to instantiate a new BCCSP-based (X509) MSP
type BCCSPNewOpts struct {
NewBaseOpts
}

// IdemixNewOpts contains the options to instantiate a new Idemix-based MSP
type IdemixNewOpts struct {
NewBaseOpts
}

// New create a new MSP instance depending on the passed Opts
func New(opts NewOpts) (MSP, error) {
switch opts.(type) {
case *BCCSPNewOpts:
switch opts.GetVersion() {
case MSPv1_0:
return newBccspMsp()
default:
return nil, errors.Errorf("Invalid *BCCSPNewOpts. Version not recognized [%v]", opts.GetVersion())
}
case *IdemixNewOpts:
switch opts.GetVersion() {
case MSPv1_0:
return newIdemixMsp()
default:
return nil, errors.Errorf("Invalid *IdemixNewOpts. Version not recognized [%v]", opts.GetVersion())
}
default:
return nil, errors.Errorf("Invalid msp.NewOpts instance. It must be either *BCCSPNewOpts or *IdemixNewOpts. It was [%v]", opts)
}
}
40 changes: 40 additions & 0 deletions msp/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package msp

import (
"testing"

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

func TestNewInvalidOpts(t *testing.T) {
i, err := New(nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Invalid msp.NewOpts instance. It must be either *BCCSPNewOpts or *IdemixNewOpts. It was [<nil>]")
assert.Nil(t, i)

i, err = New(&BCCSPNewOpts{NewBaseOpts{Version: -1}})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Invalid *BCCSPNewOpts. Version not recognized [-1]")
assert.Nil(t, i)

i, err = New(&IdemixNewOpts{NewBaseOpts{Version: -1}})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Invalid *IdemixNewOpts. Version not recognized [-1]")
assert.Nil(t, i)
}

func TestNew(t *testing.T) {
i, err := New(&BCCSPNewOpts{NewBaseOpts{Version: MSPv1_0}})
assert.NoError(t, err)
assert.NotNil(t, i)

i, err = New(&IdemixNewOpts{NewBaseOpts{Version: MSPv1_0}})
assert.NoError(t, err)
assert.NotNil(t, i)
}
4 changes: 2 additions & 2 deletions msp/idemixmsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type idemixmsp struct {
name string
}

// NewIdemixMsp creates a new instance of idemixmsp
func NewIdemixMsp() (MSP, error) {
// newIdemixMsp creates a new instance of idemixmsp
func newIdemixMsp() (MSP, error) {
mspLogger.Debugf("Creating Idemix-based MSP instance")

msp := idemixmsp{}
Expand Down
4 changes: 2 additions & 2 deletions msp/idemixmsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func setup(configPath string) (MSP, error) {
msp, err := NewIdemixMsp()
msp, err := newIdemixMsp()
if err != nil {
return nil, errors.Wrap(err, "Getting MSP failed")
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestSetupBad(t *testing.T) {
_, err := setup("testdata/idemix/badpath")
assert.Error(t, err)

msp1, err := NewIdemixMsp()
msp1, err := newIdemixMsp()
assert.NoError(t, err)

// Setup with nil config
Expand Down
6 changes: 3 additions & 3 deletions msp/mgmt/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ func GetLocalMSP() msp.MSP {
var err error
created = true

bccspMSP, err := msp.NewBccspMsp()
mspInst, err := msp.New(&msp.BCCSPNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: msp.MSPv1_0}})
if err != nil {
mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
}

lclMsp, err = cache.New(bccspMSP)
lclMsp, err = cache.New(mspInst)
if err != nil {
mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
}
localMsp = bccspMSP
localMsp = mspInst
}
}

Expand Down
18 changes: 9 additions & 9 deletions msp/msp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestMSPSetupNoCryptoConf(t *testing.T) {
b, err := proto.Marshal(mspconf)
assert.NoError(t, err)
conf.Config = b
newmsp, err := NewBccspMsp()
newmsp, err := newBccspMsp()
assert.NoError(t, err)
err = newmsp.Setup(conf)
assert.NoError(t, err)
Expand All @@ -97,7 +97,7 @@ func TestMSPSetupNoCryptoConf(t *testing.T) {
b, err = proto.Marshal(mspconf)
assert.NoError(t, err)
conf.Config = b
newmsp, err = NewBccspMsp()
newmsp, err = newBccspMsp()
assert.NoError(t, err)
err = newmsp.Setup(conf)
assert.NoError(t, err)
Expand All @@ -109,7 +109,7 @@ func TestMSPSetupNoCryptoConf(t *testing.T) {
b, err = proto.Marshal(mspconf)
assert.NoError(t, err)
conf.Config = b
newmsp, err = NewBccspMsp()
newmsp, err = newBccspMsp()
assert.NoError(t, err)
err = newmsp.Setup(conf)
assert.NoError(t, err)
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestNotFoundInBCCSP(t *testing.T) {

assert.NoError(t, err)

thisMSP, err := NewBccspMsp()
thisMSP, err := newBccspMsp()
assert.NoError(t, err)
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
assert.NoError(t, err)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestGetSigningIdentityFromVerifyingMSP(t *testing.T) {
os.Exit(-1)
}

newmsp, err := NewBccspMsp()
newmsp, err := newBccspMsp()
assert.NoError(t, err)
err = newmsp.Setup(conf)
assert.NoError(t, err)
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestBadAdminIdentity(t *testing.T) {
conf, err := GetLocalMspConfig("testdata/badadmin", nil, "DEFAULT")
assert.NoError(t, err)

thisMSP, err := NewBccspMsp()
thisMSP, err := newBccspMsp()
assert.NoError(t, err)
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join("testdata/badadmin", "keystore"), true)
assert.NoError(t, err)
Expand Down Expand Up @@ -855,13 +855,13 @@ func TestMain(m *testing.M) {
os.Exit(-1)
}

localMsp, err = NewBccspMsp()
localMsp, err = newBccspMsp()
if err != nil {
fmt.Printf("Constructor for msp should have succeeded, got err %s instead", err)
os.Exit(-1)
}

localMspBad, err = NewBccspMsp()
localMspBad, err = newBccspMsp()
if err != nil {
fmt.Printf("Constructor for msp should have succeeded, got err %s instead", err)
os.Exit(-1)
Expand Down Expand Up @@ -924,7 +924,7 @@ func getLocalMSP(t *testing.T, dir string) MSP {
conf, err := GetLocalMspConfig(dir, nil, "DEFAULT")
assert.NoError(t, err)

thisMSP, err := NewBccspMsp()
thisMSP, err := newBccspMsp()
assert.NoError(t, err)
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions msp/mspimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ type bccspmsp struct {
cryptoConfig *m.FabricCryptoConfig
}

// NewBccspMsp returns an MSP instance backed up by a BCCSP
// newBccspMsp returns an MSP instance backed up by a BCCSP
// crypto provider. It handles x.509 certificates and can
// generate identities and signing identities backed by
// certificates and keypairs
func NewBccspMsp() (MSP, error) {
func newBccspMsp() (MSP, error) {
mspLogger.Debugf("Creating BCCSP-based MSP instance")

bccsp := factory.GetDefault()
Expand Down
4 changes: 2 additions & 2 deletions msp/ouconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestBadConfigOUCert(t *testing.T) {
conf, err := GetLocalMspConfig("testdata/badconfigoucert", nil, "DEFAULT")
assert.NoError(t, err)

thisMSP, err := NewBccspMsp()
thisMSP, err := newBccspMsp()
assert.NoError(t, err)

err = thisMSP.Setup(conf)
Expand All @@ -68,7 +68,7 @@ func TestValidateIntermediateConfigOU(t *testing.T) {
conf, err := GetLocalMspConfig("testdata/external", nil, "DEFAULT")
assert.NoError(t, err)

thisMSP, err = NewBccspMsp()
thisMSP, err = newBccspMsp()
assert.NoError(t, err)
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join("testdata/external", "keystore"), true)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion msp/revocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestRevokedIntermediateCA(t *testing.T) {
conf, err := GetLocalMspConfig(dir, nil, "DEFAULT")
assert.NoError(t, err)

thisMSP, err := NewBccspMsp()
thisMSP, err := newBccspMsp()
assert.NoError(t, err)
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
assert.NoError(t, err)
Expand Down

0 comments on commit 40c423f

Please sign in to comment.