Skip to content

Commit 1f4b004

Browse files
committed
Refactor MSP package and msp config w/o json
This change-set presents a better organisation of the MSP package: code that belongs to msp proper is in fabric/msp, whereas core that instantiates and manages msps for the peer was moved in fabric/core/peer (a new package was created to avoid import cycles). Furthermore, configuration of the local MSP no longer requires a specific json file: it is sufficient to place certificates and keys in a directory with the appropriate structure (sample in msp/sampleconfig). Change-Id: Ic0c696a6e0fb406d8e482240e24e5d5774efc5c5 Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent cea4adf commit 1f4b004

38 files changed

+701
-605
lines changed

Makefile

+20-3
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ build/image/javaenv/payload: build/javashim.tar.bz2 \
180180
build/protos.tar.bz2 \
181181
settings.gradle
182182
build/image/peer/payload: build/docker/bin/peer \
183-
peer/core.yaml \
184-
msp/peer-config.json
183+
peer/core.yaml
185184
build/image/orderer/payload: build/docker/bin/orderer \
186185
orderer/orderer.yaml
187186
build/image/testenv/payload: build/gotools.tar.bz2
@@ -191,7 +190,25 @@ build/image/%/payload:
191190
mkdir -p $@
192191
cp $^ $@
193192

194-
build/image/%/$(DUMMY): Makefile build/image/%/payload
193+
# the target below is required to produce a valid
194+
# local MSP config when we build the container; there
195+
# might be a better way of structuring it, but we'll
196+
# leave as a TODO for now
197+
.PHONY: mspconfig
198+
mspconfig: msp/sampleconfig/signcerts/peer.pem \
199+
msp/sampleconfig/admincerts/admincert.pem \
200+
msp/sampleconfig/keystore/key.pem \
201+
msp/sampleconfig/cacerts/cacert.pem
202+
mkdir -p build/image/peer/payload/msp/sampleconfig/signcerts
203+
cp msp/sampleconfig/signcerts/peer.pem build/image/peer/payload/msp/sampleconfig/signcerts
204+
mkdir -p build/image/peer/payload/msp/sampleconfig/admincerts
205+
cp msp/sampleconfig/admincerts/admincert.pem build/image/peer/payload/msp/sampleconfig/admincerts
206+
mkdir -p build/image/peer/payload/msp/sampleconfig/keystore
207+
cp msp/sampleconfig/keystore/key.pem build/image/peer/payload/msp/sampleconfig/keystore
208+
mkdir -p build/image/peer/payload/msp/sampleconfig/cacerts
209+
cp msp/sampleconfig/cacerts/cacert.pem build/image/peer/payload/msp/sampleconfig/cacerts
210+
211+
build/image/%/$(DUMMY): Makefile build/image/%/payload mspconfig
195212
$(eval TARGET = ${patsubst build/image/%/$(DUMMY),%,${@}})
196213
@echo "Building docker $(TARGET)-image"
197214
@cat images/$(TARGET)/Dockerfile.in \

core/chaincode/exectransaction_test.go

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

4040
"github.com/golang/protobuf/proto"
41-
"github.com/hyperledger/fabric/core/config"
4241
"github.com/hyperledger/fabric/core/crypto/primitives"
42+
"github.com/hyperledger/fabric/core/peer/msp"
4343
"github.com/hyperledger/fabric/msp"
4444
"github.com/hyperledger/fabric/protos/common"
4545
"github.com/spf13/viper"
@@ -1070,9 +1070,9 @@ func TestMain(m *testing.M) {
10701070
primitives.SetSecurityLevel("SHA2", 256)
10711071

10721072
// setup the MSP manager so that we can sign/verify
1073-
mspMgrConfigFile := "../../msp/peer-config.json"
1074-
config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
1075-
signer, err = msp.GetLocalMSP().GetDefaultSigningIdentity()
1073+
mspMgrConfigDir := "../../msp/sampleconfig/"
1074+
mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
1075+
signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
10761076
if err != nil {
10771077
os.Exit(-1)
10781078
fmt.Printf("Could not initialize msp/signer")

core/config/config.go

-61
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ 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"
3025
"github.com/op/go-logging"
3126
"github.com/spf13/viper"
3227
)
@@ -80,59 +75,3 @@ func SetupTestConfig(pathToOpenchainYaml string) {
8075
configLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2))
8176

8277
}
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

+11-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import (
2727

2828
"github.com/golang/protobuf/proto"
2929
"github.com/hyperledger/fabric/core/chaincode"
30-
"github.com/hyperledger/fabric/core/config"
3130
"github.com/hyperledger/fabric/core/container"
3231
"github.com/hyperledger/fabric/core/crypto/primitives"
3332
"github.com/hyperledger/fabric/core/ledger/kvledger"
3433
"github.com/hyperledger/fabric/core/peer"
34+
"github.com/hyperledger/fabric/core/peer/msp"
3535
"github.com/hyperledger/fabric/core/util"
3636
"github.com/hyperledger/fabric/msp"
3737
pb "github.com/hyperledger/fabric/protos/peer"
@@ -43,7 +43,7 @@ import (
4343
)
4444

4545
var endorserServer pb.EndorserServer
46-
var mspInstance msp.PeerMSP
46+
var mspInstance msp.MSP
4747
var signer msp.SigningIdentity
4848

4949
//initialize peer and start up. If security==enabled, login as vp
@@ -437,12 +437,18 @@ func TestMain(m *testing.M) {
437437
endorserServer = NewEndorserServer()
438438

439439
// setup the MSP manager so that we can sign/verify
440-
mspMgrConfigFile := "../../msp/peer-config.json"
441-
config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
442-
signer, err = msp.GetLocalMSP().GetDefaultSigningIdentity()
440+
mspMgrConfigDir := "../../msp/sampleconfig/"
441+
err = mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
443442
if err != nil {
443+
fmt.Printf("Could not initialize msp/signer, err %s", err)
444444
os.Exit(-1)
445+
finitPeer(lis)
446+
return
447+
}
448+
signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
449+
if err != nil {
445450
fmt.Printf("Could not initialize msp/signer")
451+
os.Exit(-1)
446452
finitPeer(lis)
447453
return
448454
}

core/peer/fullflow_test.go

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

27-
"github.com/hyperledger/fabric/core/config"
2827
"github.com/hyperledger/fabric/core/crypto/primitives"
28+
"github.com/hyperledger/fabric/core/peer/msp"
2929
"github.com/hyperledger/fabric/core/util"
3030
"github.com/hyperledger/fabric/msp"
3131
"github.com/hyperledger/fabric/protos/peer"
@@ -316,25 +316,25 @@ func TestMain(m *testing.M) {
316316
// setup crypto algorithms
317317
primitives.SetSecurityLevel("SHA2", 256)
318318
// setup the MSP manager so that we can sign/verify
319-
mspMgrConfigFile := "../../msp/peer-config.json"
320-
err := config.SetupFakeMSPInfrastructureForTests(mspMgrConfigFile)
319+
mspMgrConfigDir := "../../msp/sampleconfig/"
320+
err := mspmgmt.LoadFakeSetupWithLocalMspAndTestChainMsp(mspMgrConfigDir)
321321
if err != nil {
322+
fmt.Printf("Could not initialize msp, err %s", err)
322323
os.Exit(-1)
323-
fmt.Printf("Could not initialize msp")
324324
return
325325
}
326326

327-
signer, err = msp.GetLocalMSP().GetDefaultSigningIdentity()
327+
signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
328328
if err != nil {
329-
os.Exit(-1)
330329
fmt.Printf("Could not get signer")
330+
os.Exit(-1)
331331
return
332332
}
333333

334334
signerSerialized, err = signer.Serialize()
335335
if err != nil {
336-
os.Exit(-1)
337336
fmt.Printf("Could not serialize identity")
337+
os.Exit(-1)
338338
return
339339
}
340340

core/peer/msgvalidation.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"bytes"
2323

24-
"github.com/hyperledger/fabric/msp"
24+
"github.com/hyperledger/fabric/core/peer/msp"
2525
"github.com/hyperledger/fabric/protos/common"
2626
pb "github.com/hyperledger/fabric/protos/peer"
2727
"github.com/hyperledger/fabric/protos/utils"
@@ -114,29 +114,25 @@ func checkSignatureFromCreator(creatorBytes []byte, sig []byte, msg []byte, Chai
114114
}
115115

116116
// get the identity of the creator
117-
creator, err := msp.GetManagerForChain(ChainID).DeserializeIdentity(creatorBytes)
117+
creator, err := mspmgmt.GetManagerForChain(ChainID).DeserializeIdentity(creatorBytes)
118118
if err != nil {
119119
return fmt.Errorf("Failed to deserialize creator identity, err %s", err)
120120
}
121121

122-
putilsLogger.Infof("checkSignatureFromCreator info: creator is %s", creator.Identifier())
122+
putilsLogger.Infof("checkSignatureFromCreator info: creator is %s", creator.GetIdentifier())
123123

124124
// ensure that creator is a valid certificate
125-
valid, err := creator.Validate()
125+
err = creator.IsValid()
126126
if err != nil {
127-
return fmt.Errorf("Could not determine whether the identity is valid, err %s", err)
128-
} else if !valid {
129-
return fmt.Errorf("The creator certificate is not valid, aborting")
127+
return fmt.Errorf("The creator certificate is not valid, err %s", err)
130128
}
131129

132130
putilsLogger.Infof("checkSignatureFromCreator info: creator is valid")
133131

134132
// validate the signature
135-
verified, err := creator.Verify(msg, sig)
133+
err = creator.Verify(msg, sig)
136134
if err != nil {
137-
return fmt.Errorf("Could not determine whether the signature is valid, err %s", err)
138-
} else if !verified {
139-
return fmt.Errorf("The creator's signature over the proposal is not valid, aborting")
135+
return fmt.Errorf("The creator's signature over the proposal is not valid, err %s", err)
140136
}
141137

142138
putilsLogger.Infof("checkSignatureFromCreator exists successfully")

core/peer/msp/config.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package mspmgmt
2+
3+
import (
4+
"github.com/hyperledger/fabric/core/util"
5+
"github.com/hyperledger/fabric/msp"
6+
"github.com/hyperledger/fabric/protos/common"
7+
)
8+
9+
func LoadLocalMsp(dir string) error {
10+
conf, err := msp.GetLocalMspConfig(dir)
11+
if err != nil {
12+
return err
13+
}
14+
15+
return GetLocalMSP().Setup(conf)
16+
}
17+
18+
// FIXME: this is required for now because we need a local MSP
19+
// and also the MSP mgr for the test chain; as soon as the code
20+
// to setup chains is ready, the chain should be setup using
21+
// the method below and this method should disappear
22+
func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error {
23+
conf, err := msp.GetLocalMspConfig(dir)
24+
if err != nil {
25+
return err
26+
}
27+
28+
err = GetLocalMSP().Setup(conf)
29+
if err != nil {
30+
return err
31+
}
32+
33+
fakeConfig = &msp.MSPManagerConfig{MspList: []*msp.MSPConfig{conf}, Name: "MGRFORTESTCHAIN"}
34+
35+
err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig)
36+
if err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}
42+
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+
50+
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")
69+
}
70+
71+
mgr := msp.NewMSPManager()
72+
err := mgr.Setup(fakeConfig)
73+
if err != nil {
74+
return nil, err
75+
} else {
76+
return mgr, nil
77+
}
78+
}

0 commit comments

Comments
 (0)