Skip to content

Commit 96637cf

Browse files
committed
Rework of MSP (config and factories)
This change-set introduces a local MSP and per-chain MSPs for the peer to use. The local MSP is configured from local config files and is used to get the signing identity for the peer. Per-chain MSPs are used on each chain to validate creators/signatures on the various messages. Additionally, in msp/configfilegenerator there is a sample program file that shows how the client config may be built out of certificates and keys. It can be removed later on but it is useful for now to show how to construct that file. Change-Id: Id9b92a96800a67b27124cc98a6228ad3e03f531a Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent 3ea19f3 commit 96637cf

33 files changed

+727
-546
lines changed

core/chaincode/exectransaction_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
putils "github.com/hyperledger/fabric/protos/utils"
3939

4040
"github.com/golang/protobuf/proto"
41+
"github.com/hyperledger/fabric/core/config"
4142
"github.com/hyperledger/fabric/core/crypto/primitives"
4243
"github.com/hyperledger/fabric/msp"
4344
"github.com/hyperledger/fabric/protos/common"
@@ -1061,11 +1062,8 @@ func TestMain(m *testing.M) {
10611062

10621063
// setup the MSP manager so that we can sign/verify
10631064
mspMgrConfigFile := "../../msp/peer-config.json"
1064-
msp.GetManager().Setup(mspMgrConfigFile)
1065-
mspID := "DEFAULT"
1066-
id := "PEER"
1067-
signingIdentity := &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: mspID}, Value: id}
1068-
signer, err = msp.GetManager().GetSigningIdentity(signingIdentity)
1065+
config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
1066+
signer, err = msp.GetLocalMSP().GetDefaultSigningIdentity()
10691067
if err != nil {
10701068
os.Exit(-1)
10711069
fmt.Printf("Could not initialize msp/signer")

core/chaincode/importsysccs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var systemChaincodes = []*SystemChaincode{
3535
Enabled: true,
3636
Name: "escc",
3737
Path: "github.com/hyperledger/fabric/core/system_chaincode/escc",
38-
InitArgs: [][]byte{[]byte("DEFAULT"), []byte("PEER")}, // TODO: retrieve these aruments properly
38+
InitArgs: [][]byte{[]byte("")},
3939
Chaincode: &escc.EndorserOneValidSignature{},
4040
},
4141
{

core/config/config.go

+61
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import (
2222
"runtime"
2323
"strings"
2424

25+
"encoding/json"
26+
"io/ioutil"
27+
28+
"github.com/hyperledger/fabric/core/util"
29+
"github.com/hyperledger/fabric/msp"
2530
"github.com/op/go-logging"
2631
"github.com/spf13/viper"
2732
)
@@ -75,3 +80,59 @@ func SetupTestConfig(pathToOpenchainYaml string) {
7580
configLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2))
7681

7782
}
83+
84+
func getPeerConfFromFile(configFile string) (*msp.NodeLocalConfig, error) {
85+
file, err := ioutil.ReadFile(configFile)
86+
if err != nil {
87+
return nil, fmt.Errorf("Could not read file %s, err %s", configFile, err)
88+
}
89+
90+
var localConf msp.NodeLocalConfig
91+
err = json.Unmarshal(file, &localConf)
92+
if err != nil {
93+
return nil, fmt.Errorf("Could not unmarshal config, err %s", err)
94+
}
95+
96+
return &localConf, nil
97+
}
98+
99+
func LoadLocalMSPConfig(configFile string) error {
100+
localConf, err := getPeerConfFromFile(configFile)
101+
if err != nil {
102+
return err
103+
}
104+
105+
if localConf.LocalMSP == nil {
106+
return fmt.Errorf("nil LocalMSP")
107+
}
108+
109+
err = msp.GetLocalMSP().Setup(localConf.LocalMSP)
110+
if err != nil {
111+
return fmt.Errorf("Could not setup local msp, err %s", err)
112+
}
113+
114+
// TODO: setup BCCSP here using localConf.BCCSP
115+
116+
return nil
117+
}
118+
119+
func SetupFakeMSPInfrastructureForTests(configFile string) error {
120+
err := LoadLocalMSPConfig(configFile)
121+
if err != nil {
122+
return err
123+
}
124+
125+
localConf, err := getPeerConfFromFile(configFile)
126+
if err != nil {
127+
return err
128+
}
129+
130+
mgrconf := &msp.MSPManagerConfig{MspList: []*msp.MSPConfig{localConf.LocalMSP}, Name: "MGRFORTESTCHAIN"}
131+
132+
err = msp.GetManagerForChain(util.GetTestChainID()).Setup(mgrconf)
133+
if err != nil {
134+
return err
135+
}
136+
137+
return nil
138+
}

core/endorser/endorser_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/golang/protobuf/proto"
2929
"github.com/hyperledger/fabric/core/chaincode"
30+
"github.com/hyperledger/fabric/core/config"
3031
"github.com/hyperledger/fabric/core/container"
3132
"github.com/hyperledger/fabric/core/crypto/primitives"
3233
"github.com/hyperledger/fabric/core/ledger/kvledger"
@@ -369,11 +370,8 @@ func TestMain(m *testing.M) {
369370

370371
// setup the MSP manager so that we can sign/verify
371372
mspMgrConfigFile := "../../msp/peer-config.json"
372-
msp.GetManager().Setup(mspMgrConfigFile)
373-
mspID := "DEFAULT"
374-
id := "PEER"
375-
signingIdentity := &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: mspID}, Value: id}
376-
signer, err = msp.GetManager().GetSigningIdentity(signingIdentity)
373+
config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
374+
signer, err = msp.GetLocalMSP().GetDefaultSigningIdentity()
377375
if err != nil {
378376
os.Exit(-1)
379377
fmt.Printf("Could not initialize msp/signer")

core/peer/fullflow_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"os"
2626

27+
"github.com/hyperledger/fabric/core/config"
2728
"github.com/hyperledger/fabric/core/crypto/primitives"
2829
"github.com/hyperledger/fabric/core/util"
2930
"github.com/hyperledger/fabric/msp"
@@ -160,7 +161,7 @@ func TestBadProp(t *testing.T) {
160161
}
161162

162163
// get a bad signing identity
163-
badSigner, err := msp.NewNoopMsp().GetSigningIdentity(nil)
164+
badSigner, err := msp.NewNoopMsp().GetDefaultSigningIdentity()
164165
if err != nil {
165166
t.Fatalf("Couldn't get noop signer")
166167
return
@@ -316,16 +317,14 @@ func TestMain(m *testing.M) {
316317
primitives.SetSecurityLevel("SHA2", 256)
317318
// setup the MSP manager so that we can sign/verify
318319
mspMgrConfigFile := "../../msp/peer-config.json"
319-
err := msp.GetManager().Setup(mspMgrConfigFile)
320+
err := config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
320321
if err != nil {
321322
os.Exit(-1)
322323
fmt.Printf("Could not initialize msp")
323324
return
324325
}
325-
mspId := "DEFAULT"
326-
id := "PEER"
327-
signingIdentity := &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: mspId}, Value: id}
328-
signer, err = msp.GetManager().GetSigningIdentity(signingIdentity)
326+
327+
signer, err = msp.GetLocalMSP().GetDefaultSigningIdentity()
329328
if err != nil {
330329
os.Exit(-1)
331330
fmt.Printf("Could not get signer")

core/peer/msgvalidation.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ValidateProposalMessage(signedProp *pb.SignedProposal) (*pb.Proposal, *comm
7878
}
7979

8080
// validate the signature
81-
err = checkSignatureFromCreator(hdr.SignatureHeader.Creator, signedProp.Signature, signedProp.ProposalBytes)
81+
err = checkSignatureFromCreator(hdr.SignatureHeader.Creator, signedProp.Signature, signedProp.ProposalBytes, hdr.ChainHeader.ChainID)
8282
if err != nil {
8383
return nil, nil, nil, err
8484
}
@@ -105,7 +105,7 @@ func ValidateProposalMessage(signedProp *pb.SignedProposal) (*pb.Proposal, *comm
105105
// given a creator, a message and a signature,
106106
// this function returns nil if the creator
107107
// is a valid cert and the signature is valid
108-
func checkSignatureFromCreator(creatorBytes []byte, sig []byte, msg []byte) error {
108+
func checkSignatureFromCreator(creatorBytes []byte, sig []byte, msg []byte, ChainID string) error {
109109
putilsLogger.Infof("checkSignatureFromCreator starts")
110110

111111
// check for nil argument
@@ -114,7 +114,7 @@ func checkSignatureFromCreator(creatorBytes []byte, sig []byte, msg []byte) erro
114114
}
115115

116116
// get the identity of the creator
117-
creator, err := msp.GetManager().DeserializeIdentity(creatorBytes)
117+
creator, err := msp.GetManagerForChain(ChainID).DeserializeIdentity(creatorBytes)
118118
if err != nil {
119119
return fmt.Errorf("Failed to deserialize creator identity, err %s", err)
120120
}
@@ -321,7 +321,7 @@ func ValidateTransaction(e *common.Envelope) (*common.Payload, []*pb.Transaction
321321
}
322322

323323
// validate the signature in the envelope
324-
err = checkSignatureFromCreator(payload.Header.SignatureHeader.Creator, e.Signature, e.Payload)
324+
err = checkSignatureFromCreator(payload.Header.SignatureHeader.Creator, e.Signature, e.Payload, payload.Header.ChainHeader.ChainID)
325325
if err != nil {
326326
return nil, nil, err
327327
}

core/system_chaincode/escc/endorser_onevalidsignature.go

+9-24
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,11 @@ var logger = logging.MustGetLogger("escc")
3333
// EndorserOneValidSignature implements the default endorsement policy, which is to
3434
// sign the proposal hash and the read-write set
3535
type EndorserOneValidSignature struct {
36-
signerId *msp.IdentityIdentifier
3736
}
3837

3938
// Init is called once when the chaincode started the first time
40-
// There are 2 mandatory arguments
41-
// args[0] the msp identifier for the ESCC's signer
42-
// args[1] the identifier for the ESCC's signer within the msp
4339
func (e *EndorserOneValidSignature) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
44-
// Obtain the identifier of the identity that will be used to sign
45-
// Note that we cache this identity once and for all. If there is
46-
// the need to change the signing identity, there are several options:
47-
// 1) pass the desired signing identity as an optional argument to ESCC
48-
// 2) expose an ESCC Invoke function that changes the siging identity
49-
args := stub.GetArgs()
50-
if len(args) != 2 {
51-
return nil, fmt.Errorf("Incorrect number of arguments (expected 2, provided %d)", len(args))
52-
}
53-
e.signerId = &msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: string(args[0])}, Value: string(args[1])}
54-
55-
logger.Infof("Successfully initialized ESCC with identity: %s", e.signerId)
40+
logger.Infof("Successfully initialized ESCC")
5641

5742
return nil, nil
5843
}
@@ -127,15 +112,15 @@ func (e *EndorserOneValidSignature) Invoke(stub shim.ChaincodeStubInterface) ([]
127112
visibility = args[5]
128113
}
129114

130-
// obtain the identity that will sign this proposal response
131-
// NOTE: we must obtain it every time: while e.signerId remains
132-
// constant, the corresponding cert might (and will) change
133-
// and so we cannot cache the result of this call; GetSigningIdentity
134-
// on the other hand will cache the identity as long as it
135-
// doesn't change
136-
signingEndorser, err := msp.GetManager().GetSigningIdentity(e.signerId)
115+
// obtain the default signing identity for this peer; it will be used to sign this proposal response
116+
localMsp := msp.GetLocalMSP()
117+
if localMsp == nil {
118+
return nil, fmt.Errorf("Nil local MSP manager")
119+
}
120+
121+
signingEndorser, err := localMsp.GetDefaultSigningIdentity()
137122
if err != nil {
138-
return nil, fmt.Errorf("Could not obtain the signing identity for %s, err %s", e.signerId, err)
123+
return nil, fmt.Errorf("Could not obtain the default signing identity, err %s", err)
139124
}
140125

141126
// obtain a proposal response

core/system_chaincode/escc/endorser_onevalidsignature_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525

2626
"github.com/hyperledger/fabric/core/chaincode/shim"
27+
"github.com/hyperledger/fabric/core/config"
2728
"github.com/hyperledger/fabric/core/crypto/primitives"
2829
"github.com/hyperledger/fabric/core/peer"
2930
"github.com/hyperledger/fabric/core/util"
@@ -101,7 +102,7 @@ func TestInvoke(t *testing.T) {
101102

102103
cis := &pb.ChaincodeInvocationSpec{ChaincodeSpec: cs}
103104

104-
sId, err := msp.GetManager().GetSigningIdentity(&msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: "DEFAULT"}, Value: "PEER"})
105+
sId, err := msp.GetLocalMSP().GetDefaultSigningIdentity()
105106
if err != nil {
106107
t.Fail()
107108
t.Fatalf("couldn't obtain identity: err %s", err)
@@ -235,7 +236,7 @@ func validateProposalResponse(prBytes []byte, proposal *pb.Proposal, visibility
235236
}
236237

237238
// get the identity of the endorser
238-
endorser, err := msp.GetManager().DeserializeIdentity(pResp.Endorsement.Endorser)
239+
endorser, err := msp.GetManagerForChain(util.GetTestChainID()).DeserializeIdentity(pResp.Endorsement.Endorser)
239240
if err != nil {
240241
return fmt.Errorf("Failed to deserialize endorser identity, err %s", err)
241242
}
@@ -258,7 +259,7 @@ func validateProposalResponse(prBytes []byte, proposal *pb.Proposal, visibility
258259
// as extra, we assemble a transaction, sign it and then validate it
259260

260261
// obtain signer for the transaction
261-
sId, err := msp.GetManager().GetSigningIdentity(&msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: "DEFAULT"}, Value: "PEER"})
262+
sId, err := msp.GetLocalMSP().GetDefaultSigningIdentity()
262263
if err != nil {
263264
return fmt.Errorf("couldn't obtain identity: err %s", err)
264265
}
@@ -283,7 +284,7 @@ func TestMain(m *testing.M) {
283284
// setup the MSP manager so that we can sign/verify
284285
// TODO: determine the config file for the MSP
285286
mspMgrConfigFile := "../../../msp/peer-config.json"
286-
msp.GetManager().Setup(mspMgrConfigFile)
287+
config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
287288

288289
os.Exit(m.Run())
289290
}

core/system_chaincode/vscc/validator_onevalidsignature.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (vscc *ValidatorOneValidSignature) Invoke(stub shim.ChaincodeStubInterface)
105105
// loop through each of the endorsements
106106
for _, endorsement := range cap.Action.Endorsements {
107107
// extract the identity of the signer
108-
end, err := msp.GetManager().DeserializeIdentity(endorsement.Endorser)
108+
end, err := msp.GetManagerForChain(payl.Header.ChainHeader.ChainID).DeserializeIdentity(endorsement.Endorser)
109109
if err != nil {
110110
logger.Errorf("VSCC error: DeserializeIdentity failed, err %s", err)
111111
return nil, err

core/system_chaincode/vscc/validator_onevalidsignature_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323

2424
"github.com/hyperledger/fabric/core/chaincode/shim"
25+
"github.com/hyperledger/fabric/core/config"
2526
"github.com/hyperledger/fabric/core/crypto/primitives"
2627
"github.com/hyperledger/fabric/core/util"
2728
"github.com/hyperledger/fabric/msp"
@@ -103,9 +104,9 @@ func TestMain(m *testing.M) {
103104
primitives.InitSecurityLevel("SHA2", 256)
104105
// setup the MSP manager so that we can sign/verify
105106
mspMgrConfigFile := "../../../msp/peer-config.json"
106-
msp.GetManager().Setup(mspMgrConfigFile)
107+
config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
107108

108-
id, err = msp.GetManager().GetSigningIdentity(&msp.IdentityIdentifier{Mspid: msp.ProviderIdentifier{Value: "DEFAULT"}, Value: "PEER"})
109+
id, err = msp.GetLocalMSP().GetDefaultSigningIdentity()
109110
if err != nil {
110111
fmt.Printf("GetSigningIdentity failed with err %s", err)
111112
os.Exit(-1)

msp/FABRIC-MSPconfig.json

-32
This file was deleted.

msp/MSPManager-config.json

-19
This file was deleted.

0 commit comments

Comments
 (0)