Skip to content

Commit 84ea4a7

Browse files
author
Srinivasan Muralidharan
committed
[FAB-3698] def inst. policy needs to include channel
Instantiation policy needs to be defined as part of the install package. Short of that, the default policy needs to include all the admin MSPs in the the channel instead of just the Local MSP of the peer on which the instantiation occurs. This - among other things - allows any endorser in the channel to be part of instantiation. Change-Id: I16d02e168bcc9a28b0156d734e310268769ea1c7 Signed-off-by: Srinivasan Muralidharan <[email protected]>
1 parent 4342105 commit 84ea4a7

File tree

10 files changed

+111
-37
lines changed

10 files changed

+111
-37
lines changed

common/cauthdsl/cauthdsl_builder.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ func SignedByMspAdmin(mspId string) *cb.SignaturePolicyEnvelope {
113113
return p
114114
}
115115

116-
// SignedByAnyMember returns a policy that requires one valid
117-
// signature from a member of any of the orgs whose ids are
118-
// listed in the supplied string array
119-
func SignedByAnyMember(ids []string) []byte {
116+
//wrapper for generating "any of a given role" type policies
117+
func signedByAnyOfGivenRole(role msp.MSPRole_MSPRoleType, ids []string) *cb.SignaturePolicyEnvelope {
120118
// we create an array of principals, one principal
121119
// per application MSP defined on this chain
122120
sort.Strings(ids)
@@ -125,7 +123,7 @@ func SignedByAnyMember(ids []string) []byte {
125123
for i, id := range ids {
126124
principals[i] = &msp.MSPPrincipal{
127125
PrincipalClassification: msp.MSPPrincipal_ROLE,
128-
Principal: utils.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: id})}
126+
Principal: utils.MarshalOrPanic(&msp.MSPRole{Role: role, MspIdentifier: id})}
129127
sigspolicy[i] = SignedBy(int32(i))
130128
}
131129

@@ -136,7 +134,21 @@ func SignedByAnyMember(ids []string) []byte {
136134
Identities: principals,
137135
}
138136

139-
return utils.MarshalOrPanic(p)
137+
return p
138+
}
139+
140+
// SignedByAnyMember returns a policy that requires one valid
141+
// signature from a member of any of the orgs whose ids are
142+
// listed in the supplied string array
143+
func SignedByAnyMember(ids []string) *cb.SignaturePolicyEnvelope {
144+
return signedByAnyOfGivenRole(msp.MSPRole_MEMBER, ids)
145+
}
146+
147+
// SignedByAnyAdmin returns a policy that requires one valid
148+
// signature from a admin of any of the orgs whose ids are
149+
// listed in the supplied string array
150+
func SignedByAnyAdmin(ids []string) *cb.SignaturePolicyEnvelope {
151+
return signedByAnyOfGivenRole(msp.MSPRole_ADMIN, ids)
140152
}
141153

142154
// And is a convenience method which utilizes NOutOf to produce And equivalent behavior

core/chaincode/exectransaction_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func initPeer(chainIDs ...string) (net.Listener, error) {
6969

7070
peer.MockInitialize()
7171

72+
mspGetter := func(cid string) []string {
73+
return []string{"DEFAULT"}
74+
}
75+
76+
peer.MockSetMSPIDGetter(mspGetter)
77+
7278
var opts []grpc.ServerOption
7379
if viper.GetBool("peer.tls.enabled") {
7480
creds, err := credentials.NewServerTLSFromFile(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file"))

core/chaincode/systemchaincode_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, error) {
5151

5252
peer.MockInitialize()
5353

54+
mspGetter := func(cid string) []string {
55+
return []string{"DEFAULT"}
56+
}
57+
58+
peer.MockSetMSPIDGetter(mspGetter)
59+
5460
//use a different address than what we usually use for "peer"
5561
//we override the peerAddress set in chaincode_support.go
5662
// FIXME: Use peer.GetLocalAddress()

core/committer/txvalidator/validator.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ func (v *vsccValidatorImpl) GetInfoForValidate(txid, chID, ccID string) (*sysccp
361361
cc := &sysccprovider.ChaincodeInstance{ChainID: chID}
362362
vscc := &sysccprovider.ChaincodeInstance{ChainID: chID}
363363
var policy []byte
364+
var err error
364365
if ccID != "lscc" {
365366
// when we are validating any chaincode other than
366367
// LSCC, we need to ask LSCC to give us the name
@@ -383,7 +384,11 @@ func (v *vsccValidatorImpl) GetInfoForValidate(txid, chID, ccID string) (*sysccp
383384
cc.ChaincodeName = "lscc"
384385
cc.ChaincodeVersion = coreUtil.GetSysCCVersion()
385386
vscc.ChaincodeName = "vscc"
386-
policy = cauthdsl.SignedByAnyMember(v.support.GetMSPIDs(chID))
387+
p := cauthdsl.SignedByAnyMember(v.support.GetMSPIDs(chID))
388+
policy, err = utils.Marshal(p)
389+
if err != nil {
390+
return nil, nil, nil, err
391+
}
387392
}
388393

389394
// Get vscc version

core/committer/txvalidator/validator_test.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ import (
4343
"github.com/stretchr/testify/assert"
4444
)
4545

46+
func signedByAnyMember(ids []string) []byte {
47+
p := cauthdsl.SignedByAnyMember(ids)
48+
return utils.MarshalOrPanic(p)
49+
}
50+
4651
func setupLedgerAndValidator(t *testing.T) (ledger.PeerLedger, Validator) {
4752
viper.Set("peer.fileSystemPath", "/tmp/fabric/validatortest")
4853
ledgermgmt.InitializeTestEnv()
@@ -193,7 +198,7 @@ func TestInvokeOK(t *testing.T) {
193198

194199
ccID := "mycc"
195200

196-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
201+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
197202

198203
tx := getEnv(ccID, createRWset(t, ccID), t)
199204
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -210,7 +215,7 @@ func TestInvokeOKSCC(t *testing.T) {
210215

211216
ccID := "lscc"
212217

213-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
218+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
214219

215220
tx := getEnv(ccID, createRWset(t, ccID), t)
216221
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -227,7 +232,7 @@ func TestInvokeNOKWritesToLSCC(t *testing.T) {
227232

228233
ccID := "mycc"
229234

230-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
235+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
231236

232237
tx := getEnv(ccID, createRWset(t, ccID, "lscc"), t)
233238
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -244,7 +249,7 @@ func TestInvokeNOKWritesToESCC(t *testing.T) {
244249

245250
ccID := "mycc"
246251

247-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
252+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
248253

249254
tx := getEnv(ccID, createRWset(t, ccID, "escc"), t)
250255
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -261,7 +266,7 @@ func TestInvokeNOKWritesToNotExt(t *testing.T) {
261266

262267
ccID := "mycc"
263268

264-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
269+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
265270

266271
tx := getEnv(ccID, createRWset(t, ccID, "notext"), t)
267272
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -278,7 +283,7 @@ func TestInvokeNOKInvokesNotExt(t *testing.T) {
278283

279284
ccID := "notext"
280285

281-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
286+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
282287

283288
tx := getEnv(ccID, createRWset(t, ccID), t)
284289
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -295,7 +300,7 @@ func TestInvokeNOKInvokesEmptyCCName(t *testing.T) {
295300

296301
ccID := ""
297302

298-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
303+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
299304

300305
tx := getEnv(ccID, createRWset(t, ccID), t)
301306
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -312,7 +317,7 @@ func TestInvokeNOKExpiredCC(t *testing.T) {
312317

313318
ccID := "mycc"
314319

315-
putCCInfoWithVSCCAndVer(l, ccID, "vscc", "badversion", cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
320+
putCCInfoWithVSCCAndVer(l, ccID, "vscc", "badversion", signedByAnyMember([]string{"DEFAULT"}), t)
316321

317322
tx := getEnv(ccID, createRWset(t, ccID), t)
318323
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -329,7 +334,7 @@ func TestInvokeNOKBogusActions(t *testing.T) {
329334

330335
ccID := "mycc"
331336

332-
putCCInfo(l, ccID, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
337+
putCCInfo(l, ccID, signedByAnyMember([]string{"DEFAULT"}), t)
333338

334339
tx := getEnv(ccID, []byte("barf"), t)
335340
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
@@ -361,7 +366,7 @@ func TestInvokeNOKVSCCUnspecified(t *testing.T) {
361366

362367
ccID := "mycc"
363368

364-
putCCInfoWithVSCCAndVer(l, ccID, "", ccVersion, cauthdsl.SignedByAnyMember([]string{"DEFAULT"}), t)
369+
putCCInfoWithVSCCAndVer(l, ccID, "", ccVersion, signedByAnyMember([]string{"DEFAULT"}), t)
365370

366371
tx := getEnv(ccID, createRWset(t, ccID), t)
367372
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}

core/endorser/endorser_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ func initPeer(chainID string) (*testEnvironment, error) {
8787
//initialize ledger
8888
peer.MockInitialize()
8989

90+
mspGetter := func(cid string) []string {
91+
return []string{"DEFAULT"}
92+
}
93+
94+
peer.MockSetMSPIDGetter(mspGetter)
95+
9096
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
9197
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil
9298
}

core/peer/peer.go

+12
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func MockInitialize() {
9393

9494
var chainInitializer func(string)
9595

96+
var mockMSPIDGetter func(string) []string
97+
98+
func MockSetMSPIDGetter(mspIDGetter func(string) []string) {
99+
mockMSPIDGetter = mspIDGetter
100+
}
101+
96102
// Initialize sets up any chains that the peer has from the persistence. This
97103
// function should be called at the start up when the ledger and gossip
98104
// ready
@@ -419,6 +425,12 @@ func buildTrustedRootsForChain(cm configtxapi.Manager) {
419425
func GetMSPIDs(cid string) []string {
420426
chains.RLock()
421427
defer chains.RUnlock()
428+
429+
//if mock is set, use it to return MSPIDs
430+
//used for tests without a proper join
431+
if mockMSPIDGetter != nil {
432+
return mockMSPIDGetter(cid)
433+
}
422434
if c, ok := chains.list[cid]; ok {
423435
if c == nil || c.cs == nil ||
424436
c.cs.ApplicationConfig() == nil ||

core/scc/lscc/lscc.go

+24-16
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (f InvalidCCOnFSError) Error() string {
226226
type InstantiationPolicyViolatedErr string
227227

228228
func (f InstantiationPolicyViolatedErr) Error() string {
229-
return "chaincode instantiation policy violated"
229+
return fmt.Sprintf("chaincode instantiation policy violated(%s)", string(f))
230230
}
231231

232232
//InstantiationPolicyMissing when no existing instantiation policy is found when upgrading CC
@@ -479,8 +479,9 @@ func (lscc *LifeCycleSysCC) executeInstall(stub shim.ChaincodeStubInterface, ccb
479479
}
480480

481481
// getInstantiationPolicy retrieves the instantiation policy from a SignedCDSPackage
482-
func (lscc *LifeCycleSysCC) getInstantiationPolicy(stub shim.ChaincodeStubInterface, ccpack ccprovider.CCPackage) ([]byte, error) {
482+
func (lscc *LifeCycleSysCC) getInstantiationPolicy(channel string, ccpack ccprovider.CCPackage) ([]byte, error) {
483483
var ip []byte
484+
var err error
484485
// if ccpack is a SignedCDSPackage, return its IP, otherwise use a default IP
485486
sccpack, isSccpack := ccpack.(*ccprovider.SignedCDSPackage)
486487
if isSccpack {
@@ -489,17 +490,16 @@ func (lscc *LifeCycleSysCC) getInstantiationPolicy(stub shim.ChaincodeStubInterf
489490
return nil, fmt.Errorf("Instantiation policy cannot be null for a SignedCCDeploymentSpec")
490491
}
491492
} else {
492-
// the default instantiation policy requires the peer's msp admin
493-
// it assumes that the peer's MSP does not change over time
494-
mspid, err := mspmgmt.GetLocalMSP().GetIdentifier()
495-
if err != nil {
496-
return nil, fmt.Errorf("Error creating default instantiation policy: could not retrieve local MSP identifier %s", err)
497-
}
498-
ipEnvelope := cauthdsl.SignedByMspAdmin(mspid)
499-
ip, err = proto.Marshal(ipEnvelope)
493+
// the default instantiation policy allows any of the channel MSP admins
494+
// to be able to instantiate
495+
mspids := peer.GetMSPIDs(channel)
496+
497+
p := cauthdsl.SignedByAnyAdmin(mspids)
498+
ip, err = utils.Marshal(p)
500499
if err != nil {
501-
return nil, fmt.Errorf("Marshalling instantiation policy failed: [%s]", err)
500+
return nil, fmt.Errorf("Error marshalling default instantiation policy")
502501
}
502+
503503
}
504504
return ip, nil
505505
}
@@ -542,7 +542,7 @@ func (lscc *LifeCycleSysCC) checkInstantiationPolicy(stub shim.ChaincodeStubInte
542542
}}
543543
err = instPol.Evaluate(sd)
544544
if err != nil {
545-
return InstantiationPolicyViolatedErr("")
545+
return InstantiationPolicyViolatedErr(err.Error())
546546
}
547547
return nil
548548
}
@@ -588,7 +588,7 @@ func (lscc *LifeCycleSysCC) executeDeploy(stub shim.ChaincodeStubInterface, chai
588588
cd.Policy = policy
589589

590590
// retrieve and evaluate instantiation policy
591-
cd.InstantiationPolicy, err = lscc.getInstantiationPolicy(stub, ccpack)
591+
cd.InstantiationPolicy, err = lscc.getInstantiationPolicy(chainname, ccpack)
592592
if err != nil {
593593
return nil, err
594594
}
@@ -664,7 +664,7 @@ func (lscc *LifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
664664
cd.Policy = policy
665665

666666
// retrieve and evaluate new instantiation policy
667-
cd.InstantiationPolicy, err = lscc.getInstantiationPolicy(stub, ccpack)
667+
cd.InstantiationPolicy, err = lscc.getInstantiationPolicy(chainName, ccpack)
668668
if err != nil {
669669
return nil, err
670670
}
@@ -757,7 +757,11 @@ func (lscc *LifeCycleSysCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response
757757
if len(args) > 3 && len(args[3]) > 0 {
758758
policy = args[3]
759759
} else {
760-
policy = cauthdsl.SignedByAnyMember(peer.GetMSPIDs(chainname))
760+
p := cauthdsl.SignedByAnyMember(peer.GetMSPIDs(chainname))
761+
policy, err = utils.Marshal(p)
762+
if err != nil {
763+
return shim.Error(err.Error())
764+
}
761765
}
762766

763767
var escc []byte
@@ -806,7 +810,11 @@ func (lscc *LifeCycleSysCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response
806810
if len(args) > 3 && len(args[3]) > 0 {
807811
policy = args[3]
808812
} else {
809-
policy = cauthdsl.SignedByAnyMember(peer.GetMSPIDs(chainname))
813+
p := cauthdsl.SignedByAnyMember(peer.GetMSPIDs(chainname))
814+
policy, err = utils.Marshal(p)
815+
if err != nil {
816+
return shim.Error(err.Error())
817+
}
810818
}
811819

812820
var escc []byte

core/scc/lscc/lscc_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ import (
2222
"strings"
2323
"testing"
2424

25+
"archive/tar"
26+
"bytes"
27+
"compress/gzip"
28+
2529
"github.com/golang/protobuf/proto"
2630
"github.com/hyperledger/fabric/common/cauthdsl"
2731
"github.com/hyperledger/fabric/common/util"
2832
"github.com/hyperledger/fabric/core/chaincode/shim"
2933
"github.com/hyperledger/fabric/core/common/ccpackage"
3034
"github.com/hyperledger/fabric/core/common/ccprovider"
3135
"github.com/hyperledger/fabric/core/common/sysccprovider"
32-
//"github.com/hyperledger/fabric/core/container"
33-
"archive/tar"
34-
"bytes"
35-
"compress/gzip"
36+
"github.com/hyperledger/fabric/core/peer"
3637

3738
"github.com/stretchr/testify/assert"
3839

@@ -1214,6 +1215,12 @@ func TestMain(m *testing.M) {
12141215
ccprovider.SetChaincodesPath(lscctestpath)
12151216
sysccprovider.RegisterSystemChaincodeProviderFactory(&scc.MocksccProviderFactory{})
12161217

1218+
mspGetter := func(cid string) []string {
1219+
return []string{"DEFAULT"}
1220+
}
1221+
1222+
peer.MockSetMSPIDGetter(mspGetter)
1223+
12171224
var err error
12181225

12191226
// setup the MSP manager so that we can sign/verify

core/scc/vscc/validator_onevalidsignature_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/hyperledger/fabric/core/common/sysccprovider"
3838
cutils "github.com/hyperledger/fabric/core/container/util"
3939
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
40+
per "github.com/hyperledger/fabric/core/peer"
4041
"github.com/hyperledger/fabric/core/policy"
4142
"github.com/hyperledger/fabric/core/scc/lscc"
4243
"github.com/hyperledger/fabric/msp"
@@ -1393,6 +1394,12 @@ func TestMain(m *testing.M) {
13931394
sysccprovider.RegisterSystemChaincodeProviderFactory(&scc.MocksccProviderFactory{})
13941395
policy.RegisterPolicyCheckerFactory(&mockPolicyCheckerFactory{})
13951396

1397+
mspGetter := func(cid string) []string {
1398+
return []string{"DEFAULT"}
1399+
}
1400+
1401+
per.MockSetMSPIDGetter(mspGetter)
1402+
13961403
var err error
13971404

13981405
// setup the MSP manager so that we can sign/verify

0 commit comments

Comments
 (0)