Skip to content

Commit 694d7fe

Browse files
author
Matthias Neugschwandtner
committed
[FAB-3300] default chaincode instantiation policy
If a chaincode does not come with an instantiation policy, have the LSCC set a default instantiation policy. The default instantiation policy allows only the peer's MSP admin to instantiate chaincode. Change-Id: I766041f40d16e5ab5daf6adc1e96c1ade36ee144 Signed-off-by: Matthias Neugschwandtner <[email protected]>
1 parent a48169d commit 694d7fe

7 files changed

+204
-146
lines changed

core/chaincode/exectransaction_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func initPeer(chainIDs ...string) (net.Listener, error) {
107107
return nil, err
108108
}
109109
scc.DeploySysCCs(id)
110+
// any chain other than the default testchainid does not have a MSP set up -> create one
111+
if id != util.GetTestChainID() {
112+
mspmgmt.XXXSetMSPManager(id, mspmgmt.GetManagerForChain(util.GetTestChainID()))
113+
}
110114
}
111115

112116
go grpcServer.Serve(lis)
@@ -326,14 +330,13 @@ func deploy2(ctx context.Context, cccid *ccprovider.CCContext, chaincodeDeployme
326330
ccprovider.PutChaincodeIntoFS(chaincodeDeploymentSpec)
327331

328332
sysCCVers := util.GetSysCCVersion()
329-
sprop, prop := putils.MockSignedEndorserProposalOrPanic(cccid.ChainID, cis.ChaincodeSpec, []byte("Admin"), []byte("msg1"))
333+
sprop, prop := putils.MockSignedEndorserProposal2OrPanic(cccid.ChainID, cis.ChaincodeSpec, signer)
330334
lsccid := ccprovider.NewCCContext(cccid.ChainID, cis.ChaincodeSpec.ChaincodeId.Name, sysCCVers, uuid, true, sprop, prop)
331335

332336
//write to lscc
333337
if _, _, err = ExecuteWithErrorFilter(ctx, lsccid, cis); err != nil {
334338
return nil, fmt.Errorf("Error deploying chaincode (1): %s", err)
335339
}
336-
337340
if b, _, err = ExecuteWithErrorFilter(ctx, cccid, chaincodeDeploymentSpec); err != nil {
338341
return nil, fmt.Errorf("Error deploying chaincode(2): %s", err)
339342
}
@@ -1748,8 +1751,8 @@ func TestMain(m *testing.M) {
17481751
msptesttools.LoadMSPSetupForTesting()
17491752
signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
17501753
if err != nil {
1751-
os.Exit(-1)
17521754
fmt.Print("Could not initialize msp/signer")
1755+
os.Exit(-1)
17531756
return
17541757
}
17551758

@@ -1758,7 +1761,6 @@ func TestMain(m *testing.M) {
17581761
}
17591762

17601763
func deployChaincode(ctx context.Context, name string, version string, chaincodeType pb.ChaincodeSpec_Type, path string, args [][]byte, creator []byte, channel string, nextBlockNumber uint64) ([]byte, *ccprovider.CCContext, error) {
1761-
17621764
chaincodeSpec := &pb.ChaincodeSpec{
17631765
ChaincodeId: &pb.ChaincodeID{
17641766
Name: name,
@@ -1771,7 +1773,7 @@ func deployChaincode(ctx context.Context, name string, version string, chaincode
17711773
},
17721774
}
17731775

1774-
signedProposal, proposal := putils.MockSignedEndorserProposalOrPanic(channel, chaincodeSpec, creator, nil)
1776+
signedProposal, proposal := putils.MockSignedEndorserProposal2OrPanic(channel, chaincodeSpec, signer)
17751777

17761778
chaincodeCtx := ccprovider.NewCCContext(channel, name, version, "", false, signedProposal, proposal)
17771779

core/chaincode/upgrade_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/hyperledger/fabric/common/util"
2525
"github.com/hyperledger/fabric/core/common/ccprovider"
2626
pb "github.com/hyperledger/fabric/protos/peer"
27+
putils "github.com/hyperledger/fabric/protos/utils"
2728

2829
"github.com/golang/protobuf/proto"
2930
"golang.org/x/net/context"
@@ -84,7 +85,8 @@ func upgrade2(ctx context.Context, cccid *ccprovider.CCContext,
8485
ccprovider.PutChaincodeIntoFS(chaincodeDeploymentSpec)
8586

8687
sysCCVers := util.GetSysCCVersion()
87-
lsccid := ccprovider.NewCCContext(cccid.ChainID, cis.ChaincodeSpec.ChaincodeId.Name, sysCCVers, uuid, true, nil, nil)
88+
sprop, prop := putils.MockSignedEndorserProposal2OrPanic(cccid.ChainID, cis.ChaincodeSpec, signer)
89+
lsccid := ccprovider.NewCCContext(cccid.ChainID, cis.ChaincodeSpec.ChaincodeId.Name, sysCCVers, uuid, true, sprop, prop)
8890

8991
var cdbytes []byte
9092
//write to lscc

core/scc/lscc/lscc.go

+35-19
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ func (f InstantiationPolicyViolatedErr) Error() string {
229229
return "chaincode instantiation policy violated"
230230
}
231231

232+
//InstantiationPolicyMissing when no existing instantiation policy is found when upgrading CC
233+
type InstantiationPolicyMissing string
234+
235+
func (f InstantiationPolicyMissing) Error() string {
236+
return "instantiation policy missing"
237+
}
238+
232239
//-------------- helper functions ------------------
233240
//create the chaincode on the given chain
234241
func (lscc *LifeCycleSysCC) createChaincode(stub shim.ChaincodeStubInterface, cd *ccprovider.ChaincodeData) error {
@@ -473,16 +480,28 @@ func (lscc *LifeCycleSysCC) executeInstall(stub shim.ChaincodeStubInterface, ccb
473480

474481
// getInstantiationPolicy retrieves the instantiation policy from a SignedCDSPackage
475482
func (lscc *LifeCycleSysCC) getInstantiationPolicy(stub shim.ChaincodeStubInterface, ccpack ccprovider.CCPackage) ([]byte, error) {
476-
//if ccpack is a SignedCDSPackage, evaluate submitter against instantiation policy
483+
var ip []byte
484+
// if ccpack is a SignedCDSPackage, return its IP, otherwise use a default IP
477485
sccpack, isSccpack := ccpack.(*ccprovider.SignedCDSPackage)
478486
if isSccpack {
479-
ip := sccpack.GetInstantiationPolicy()
487+
ip = sccpack.GetInstantiationPolicy()
480488
if ip == nil {
481489
return nil, fmt.Errorf("Instantiation policy cannot be null for a SignedCCDeploymentSpec")
482490
}
483-
return ip, nil
491+
} 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)
500+
if err != nil {
501+
return nil, fmt.Errorf("Marshalling instantiation policy failed: [%s]", err)
502+
}
484503
}
485-
return nil, nil
504+
return ip, nil
486505
}
487506

488507
// checkInstantiationPolicy evaluates an instantiation policy against a signed proposal
@@ -573,11 +592,9 @@ func (lscc *LifeCycleSysCC) executeDeploy(stub shim.ChaincodeStubInterface, chai
573592
if err != nil {
574593
return nil, err
575594
}
576-
if cd.InstantiationPolicy != nil {
577-
err = lscc.checkInstantiationPolicy(stub, chainname, cd.InstantiationPolicy)
578-
if err != nil {
579-
return nil, err
580-
}
595+
err = lscc.checkInstantiationPolicy(stub, chainname, cd.InstantiationPolicy)
596+
if err != nil {
597+
return nil, err
581598
}
582599

583600
err = lscc.createChaincode(stub, cd)
@@ -625,11 +642,12 @@ func (lscc *LifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
625642
}
626643

627644
//do not upgrade if instantiation policy is violated
628-
if cd.InstantiationPolicy != nil {
629-
err = lscc.checkInstantiationPolicy(stub, chainName, cd.InstantiationPolicy)
630-
if err != nil {
631-
return nil, err
632-
}
645+
if cd.InstantiationPolicy == nil {
646+
return nil, InstantiationPolicyMissing("")
647+
}
648+
err = lscc.checkInstantiationPolicy(stub, chainName, cd.InstantiationPolicy)
649+
if err != nil {
650+
return nil, err
633651
}
634652

635653
ccpack, err := ccprovider.GetChaincodeFromFS(chaincodeName, cds.ChaincodeSpec.ChaincodeId.Version)
@@ -650,11 +668,9 @@ func (lscc *LifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
650668
if err != nil {
651669
return nil, err
652670
}
653-
if cd.InstantiationPolicy != nil {
654-
err = lscc.checkInstantiationPolicy(stub, chainName, cd.InstantiationPolicy)
655-
if err != nil {
656-
return nil, err
657-
}
671+
err = lscc.checkInstantiationPolicy(stub, chainName, cd.InstantiationPolicy)
672+
if err != nil {
673+
return nil, err
658674
}
659675

660676
err = lscc.upgradeChaincode(stub, cd)

0 commit comments

Comments
 (0)