Skip to content

Commit 6444545

Browse files
committed
MSP mgr instantiation from Block
This change-set introduces a proper implementation of the function that generates an MSP manager out of a Block from the ledger. We expect one ConfigurationItem per MSP, each containing in the Value field the protobuf serialization of an MSPConfig message. The MSP config schema has been further simplified by removing the MSPManagerConfig struct. Furthermore, MSP's and MSPManager's are now immutable types: the reconfig method has been removed and reconfiguring a manager/msp means constructing a new one and letting the old instance go out of scope. Change-Id: I42b12e1bece4084ab5695f60050ce512a5286e87 Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent 4c63856 commit 6444545

16 files changed

+475
-259
lines changed

core/peer/msgvalidation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func checkSignatureFromCreator(creatorBytes []byte, sig []byte, msg []byte, Chai
122122
putilsLogger.Infof("checkSignatureFromCreator info: creator is %s", creator.GetIdentifier())
123123

124124
// ensure that creator is a valid certificate
125-
err = creator.IsValid()
125+
err = creator.Validate()
126126
if err != nil {
127127
return fmt.Errorf("The creator certificate is not valid, err %s", err)
128128
}

core/peer/msp/config.go

+10-29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"github.com/hyperledger/fabric/core/util"
55
"github.com/hyperledger/fabric/msp"
66
"github.com/hyperledger/fabric/protos/common"
7+
mspprotos "github.com/hyperledger/fabric/protos/msp"
8+
"github.com/hyperledger/fabric/protos/msp/utils"
79
)
810

911
func LoadLocalMsp(dir string) error {
@@ -30,7 +32,7 @@ func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error {
3032
return err
3133
}
3234

33-
fakeConfig = &msp.MSPManagerConfig{MspList: []*msp.MSPConfig{conf}, Name: "MGRFORTESTCHAIN"}
35+
fakeConfig := []*mspprotos.MSPConfig{conf}
3436

3537
err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig)
3638
if err != nil {
@@ -40,39 +42,18 @@ func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error {
4042
return nil
4143
}
4244

43-
// FIXME! Every chain needs an MSP config but for now,
44-
// we don't have support for that; we get around it
45-
// temporarily by storing the config the peer loaded
46-
// and using it every time we're asked to get an MSP
47-
// manager via LoadMSPManagerFromBlock
48-
var fakeConfig *msp.MSPManagerConfig
49-
45+
// GetMSPManagerFromBlock returns a new MSP manager from a ConfigurationEnvelope
5046
func GetMSPManagerFromBlock(b *common.Block) (msp.MSPManager, error) {
51-
// FIXME! We need to extract the config item
52-
// that relates to MSP from the contig tx
53-
// inside this block, unmarshal it to extract
54-
// an *MSPManagerConfig that we can then pass
55-
// to the Setup method; for now we wing it by
56-
// passing the same config we got for the
57-
// local manager; this way chain creation tests
58-
// can proceed without being block by this
59-
60-
// this hack is required to give us some configuration
61-
// so that we can return a valid MSP manager when
62-
// someone calls this function; it should work, provided
63-
// that this call occurs after the peer has started
64-
// and called LoadFakeSetupWithLocalMspAndTestChainMsp.
65-
// Notice that this happens very early in the peer
66-
// startup and so the assumption should be safe
67-
if fakeConfig == nil {
68-
panic("fakeConfig is nil")
47+
mgrConfig, err := msputils.GetMSPManagerConfigFromBlock(b)
48+
if err != nil {
49+
return nil, err
6950
}
7051

7152
mgr := msp.NewMSPManager()
72-
err := mgr.Setup(fakeConfig)
53+
err = mgr.Setup(mgrConfig)
7354
if err != nil {
7455
return nil, err
75-
} else {
76-
return mgr, nil
7756
}
57+
58+
return mgr, nil
7859
}

core/peer/msp/peermsp_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import (
44
"testing"
55

66
"github.com/hyperledger/fabric/core/util"
7-
"github.com/hyperledger/fabric/protos/common"
7+
"github.com/hyperledger/fabric/msp"
8+
"github.com/hyperledger/fabric/protos/msp/testutils"
89
)
910

1011
func TestLocalMSP(t *testing.T) {
@@ -41,14 +42,18 @@ func TestFakeSetup(t *testing.T) {
4142
}
4243
}
4344

44-
// TODO: as soon as proper per-chain MSP support is developed, this test will have to be changed
4545
func TestGetMSPManagerFromBlock(t *testing.T) {
46-
err := LoadLocalMsp("../../../msp/sampleconfig/")
46+
conf, err := msp.GetLocalMspConfig("../../../msp/sampleconfig/")
4747
if err != nil {
48-
t.Fatalf("LoadLocalMsp failed, err %s", err)
48+
t.Fatalf("GetLocalMspConfig failed, err %s", err)
49+
}
50+
51+
block, err := msptestutils.GetTestBlockFromMspConfig(conf)
52+
if err != nil {
53+
t.Fatalf("getTestBlockFromMspConfig failed, err %s", err)
4954
}
5055

51-
mgr, err := GetMSPManagerFromBlock(&common.Block{ /* TODO: FILLME! */ })
56+
mgr, err := GetMSPManagerFromBlock(block)
5257
if err != nil {
5358
t.Fatalf("GetMSPManagerFromBlock failed, err %s", err)
5459
} else if mgr == nil {

core/system_chaincode/escc/endorser_onevalidsignature_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func validateProposalResponse(prBytes []byte, proposal *pb.Proposal, visibility
241241
}
242242

243243
// ensure that endorser has a valid certificate
244-
err = endorser.IsValid()
244+
err = endorser.Validate()
245245
if err != nil {
246246
return fmt.Errorf("The endorser certificate is not valid, err %s", err)
247247
}

core/system_chaincode/vscc/validator_onevalidsignature.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (vscc *ValidatorOneValidSignature) Invoke(stub shim.ChaincodeStubInterface)
112112
}
113113

114114
// validate it
115-
err = end.IsValid()
115+
err = end.Validate()
116116
if err != nil {
117117
return nil, fmt.Errorf("Invalid endorser identity, err %s", err)
118118
}

msp/configbuilder.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"encoding/pem"
99
"path/filepath"
10+
11+
"github.com/hyperledger/fabric/protos/msp"
1012
)
1113

1214
func readFile(file string) ([]byte, error) {
@@ -46,7 +48,7 @@ func getPemMaterialFromDir(dir string) ([][]byte, error) {
4648
continue
4749
}
4850

49-
fullName := dir + string(filepath.Separator) + f.Name()
51+
fullName := filepath.Join(dir, string(filepath.Separator), f.Name())
5052
mspLogger.Infof("Inspecting file %s", fullName)
5153

5254
item, err := readPemFile(fullName)
@@ -67,7 +69,7 @@ const (
6769
keystore = "keystore"
6870
)
6971

70-
func GetLocalMspConfig(dir string) (*MSPConfig, error) {
72+
func GetLocalMspConfig(dir string) (*msp.MSPConfig, error) {
7173
cacertDir := dir + string(filepath.Separator) + cacerts
7274
signcertDir := dir + string(filepath.Separator) + signcerts
7375
admincertDir := dir + string(filepath.Separator) + admincerts
@@ -98,15 +100,15 @@ func GetLocalMspConfig(dir string) (*MSPConfig, error) {
98100
// 2) there is exactly one signing key
99101
// 3) the cert and the key match
100102

101-
keyinfo := &KeyInfo{KeyIdentifier: "PEER", KeyMaterial: keys[0]}
103+
keyinfo := &msp.KeyInfo{KeyIdentifier: "PEER", KeyMaterial: keys[0]}
102104

103-
sigid := &SigningIdentityInfo{PublicSigner: signcert[0], PrivateSigner: keyinfo}
105+
sigid := &msp.SigningIdentityInfo{PublicSigner: signcert[0], PrivateSigner: keyinfo}
104106

105-
fmspconf := FabricMSPConfig{Admins: admincert, RootCerts: cacerts, SigningIdentity: sigid, Name: "DEFAULT"}
107+
fmspconf := msp.FabricMSPConfig{Admins: admincert, RootCerts: cacerts, SigningIdentity: sigid, Name: "DEFAULT"}
106108

107109
fmpsjs, _ := json.Marshal(fmspconf)
108110

109-
mspconf := &MSPConfig{Config: fmpsjs, Type: FABRIC}
111+
mspconf := &msp.MSPConfig{Config: fmpsjs, Type: int32(FABRIC)}
110112

111113
return mspconf, nil
112114
}

msp/identities.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (id *identity) GetMSPIdentifier() string {
6161
}
6262

6363
// IsValid returns nil if this instance is a valid identity or an error otherwise
64-
func (id *identity) IsValid() error {
64+
func (id *identity) Validate() error {
6565
return id.msp.Validate(id)
6666
}
6767

msp/msp.go

+20-36
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ limitations under the License.
1616

1717
package msp
1818

19+
import "github.com/hyperledger/fabric/protos/msp"
20+
21+
// FIXME: we need better comments on the interfaces!!
22+
// FIXME: we need better comments on the interfaces!!
23+
// FIXME: we need better comments on the interfaces!!
24+
1925
// Membership service provider APIs for Hyperledger Fabric:
2026
//
2127
// By "membership service provider" we refer to an abstract component of the
@@ -34,25 +40,13 @@ package msp
3440
// MSPManager is an interface defining a manager of one or more MSPs. This
3541
// essentially acts as a mediator to MSP calls and routes MSP related calls
3642
// to the appropriate MSP.
37-
// This object is initialized (once) by calling Setup. Its
38-
// internal configuration may be changed later by calling
39-
// Reconfig. It is otherwise immutable.
43+
// This object is immutable, it is initialized once and never changed.
4044
type MSPManager interface {
4145

4246
// Setup the MSP manager instance according to configuration information
43-
Setup(config *MSPManagerConfig) error
44-
45-
// Process reconfiguration messages (coming e.g., from Blockchain). This
46-
// should take into consideration certain policies related to how, e.g.,
47-
// a certain certificate should be considered valid, what constitutes the
48-
// chain of trust, and who is authorized to change that.
49-
// @param reconfigMessage The message containing the reconfiguration information.
50-
Reconfig(config []byte) error
47+
Setup(msps []*msp.MSPConfig) error
5148

52-
// Name of the MSP manager
53-
GetName() string
54-
55-
// Provides a list of Membership Service providers
49+
// GetMSPs Provides a list of Membership Service providers
5650
GetMSPs() (map[string]MSP, error)
5751

5852
// DeserializeIdentity deserializes an identity
@@ -64,24 +58,15 @@ type MSPManager interface {
6458
type MSP interface {
6559

6660
// Setup the MSP instance according to configuration information
67-
Setup(config *MSPConfig) error
68-
69-
// Process reconfiguration messages coming from the blockchain
70-
// @param reconfigMessage The message containing the reconfiguration command.
71-
Reconfig(config []byte) error
61+
Setup(config *msp.MSPConfig) error
7262

73-
// Get provider type
63+
// GetType returns the provider type
7464
GetType() ProviderType
7565

76-
// Get provider identifier
66+
// GetIdentifier returns the provider identifier
7767
GetIdentifier() (string, error)
7868

79-
// Obtain the policy to govern changes; this can be
80-
// having a json format.
81-
// Note: THIS CAN WAIT!
82-
GetPolicy() string
83-
84-
// GetSingingIdentity returns a signing identity corresponding to the provided identifier
69+
// GetSigningIdentity returns a signing identity corresponding to the provided identifier
8570
GetSigningIdentity(identifier *IdentityIdentifier) (SigningIdentity, error)
8671

8772
// GetDefaultSigningIdentity returns the default signing identity
@@ -90,7 +75,7 @@ type MSP interface {
9075
// DeserializeIdentity deserializes an identity
9176
DeserializeIdentity(serializedIdentity []byte) (Identity, error)
9277

93-
// isValid checks whether the supplied identity is valid
78+
// Validate checks whether the supplied identity is valid
9479
Validate(id Identity) error
9580
}
9681

@@ -104,21 +89,20 @@ type MSP interface {
10489
// with, and verifying signatures that correspond to these certificates.///
10590
type Identity interface {
10691

107-
// Identifier returns the identifier of that identity
92+
// GetIdentifier returns the identifier of that identity
10893
GetIdentifier() *IdentityIdentifier
10994

110-
// Retrieve the provider identifier this identity belongs to
111-
// from the previous field
95+
// GetMSPIdentifier returns the MSP Id for this instance
11296
GetMSPIdentifier() string
11397

114-
// This uses the rules that govern this identity to validate it.
98+
// Validate uses the rules that govern this identity to validate it.
11599
// E.g., if it is a fabric TCert implemented as identity, validate
116100
// will check the TCert signature against the assumed root certificate
117101
// authority.
118-
IsValid() error
102+
Validate() error
119103

120104
// TODO: Fix this comment
121-
// ParticipantID would return the participant this identity is related to
105+
// GetOrganizationUnits returns the participant this identity is related to
122106
// as long as this is public information. In certain implementations
123107
// this could be implemented by certain attributes that are publicly
124108
// associated to that identity, or the identifier of the root certificate
@@ -162,7 +146,7 @@ type SigningIdentity interface {
162146
// SignOpts the message with options
163147
SignOpts(msg []byte, opts SignatureOpts) ([]byte, error)
164148

165-
// NewAttributeProof creates a proof for an attribute
149+
// GetAttributeProof creates a proof for an attribute
166150
GetAttributeProof(spec *AttributeProofSpec) (proof []byte, err error)
167151

168152
// GetPublicVersion returns the public parts of this identity

0 commit comments

Comments
 (0)