Skip to content

Commit

Permalink
[FAB-5379] Pass signature policy to Instantiate CC
Browse files Browse the repository at this point in the history
Change-Id: Ibbd652c2d2680d40c574273c87d5b961b321d992
Signed-off-by: Bob Stasyszyn <bob.stasyszyn@securekey.com>
  • Loading branch information
bstasyszyn committed Jul 19, 2017
1 parent 1765d77 commit 1ee9a93
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 47 deletions.
2 changes: 1 addition & 1 deletion api/apifabclient/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Channel interface {
Initialize(data []byte) error
IsInitialized() bool
LoadConfigUpdateEnvelope(data []byte) error
SendInstantiateProposal(chaincodeName string, args []string, chaincodePath string, chaincodeVersion string, targets []txn.ProposalProcessor) ([]*txn.TransactionProposalResponse, txn.TransactionID, error)
SendInstantiateProposal(chaincodeName string, args []string, chaincodePath string, chaincodeVersion string, chaincodePolicy *common.SignaturePolicyEnvelope, targets []txn.ProposalProcessor) ([]*txn.TransactionProposalResponse, txn.TransactionID, error)

// Network
// TODO: Use PeerEndorser
Expand Down
41 changes: 5 additions & 36 deletions pkg/fabric-client/channel/txnsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/internal/txnproc"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/protos/common"
mspprotos "github.com/hyperledger/fabric/protos/msp"
pb "github.com/hyperledger/fabric/protos/peer"
protos_utils "github.com/hyperledger/fabric/protos/utils"
)
Expand Down Expand Up @@ -161,7 +160,8 @@ func (c *Channel) SendTransaction(tx *apitxn.Transaction) (*apitxn.TransactionRe
// chaincodePath: required - string of the path to the location of the source code of the chaincode
// chaincodeVersion: required - string of the version of the chaincode
func (c *Channel) SendInstantiateProposal(chaincodeName string,
args []string, chaincodePath string, chaincodeVersion string, targets []apitxn.ProposalProcessor) ([]*apitxn.TransactionProposalResponse, apitxn.TransactionID, error) {
args []string, chaincodePath string, chaincodeVersion string,
chaincodePolicy *common.SignaturePolicyEnvelope, targets []apitxn.ProposalProcessor) ([]*apitxn.TransactionProposalResponse, apitxn.TransactionID, error) {

if chaincodeName == "" {
return nil, apitxn.TransactionID{}, fmt.Errorf("Missing 'chaincodeName' parameter")
Expand All @@ -170,9 +170,11 @@ func (c *Channel) SendInstantiateProposal(chaincodeName string,
return nil, apitxn.TransactionID{}, fmt.Errorf("Missing 'chaincodePath' parameter")
}
if chaincodeVersion == "" {

return nil, apitxn.TransactionID{}, fmt.Errorf("Missing 'chaincodeVersion' parameter")
}
if chaincodePolicy == nil {
return nil, apitxn.TransactionID{}, fmt.Errorf("Missing 'chaincodePolicy' parameter")
}

// TODO: We should validate that targets are added to the channel.
if targets == nil || len(targets) < 1 {
Expand All @@ -195,10 +197,6 @@ func (c *Channel) SendInstantiateProposal(chaincodeName string,
if err != nil {
return nil, apitxn.TransactionID{}, fmt.Errorf("Error getting creator: %v", err)
}
chaincodePolicy, err := buildChaincodePolicy(c.clientContext.UserContext().MspID())
if err != nil {
return nil, apitxn.TransactionID{}, err
}
chaincodePolicyBytes, err := protos_utils.Marshal(chaincodePolicy)
if err != nil {
return nil, apitxn.TransactionID{}, err
Expand Down Expand Up @@ -380,32 +378,3 @@ func BuildChannelHeader(headerType common.HeaderType, channelID string, txID str
}
return channelHeader, nil
}

// internal utility method to build chaincode policy
// FIXME: for now always construct a 'Signed By any member of an organization by mspid' policy
func buildChaincodePolicy(mspid string) (*common.SignaturePolicyEnvelope, error) {
// Define MSPRole
memberRole, err := proto.Marshal(&mspprotos.MSPRole{Role: mspprotos.MSPRole_MEMBER, MspIdentifier: mspid})
if err != nil {
return nil, fmt.Errorf("Error marshal MSPRole: %s", err)
}

// construct a list of msp principals to select from using the 'n out of' operator
onePrn := &mspprotos.MSPPrincipal{
PrincipalClassification: mspprotos.MSPPrincipal_ROLE,
Principal: memberRole}

// construct 'signed by msp principal at index 0'
signedBy := &common.SignaturePolicy{Type: &common.SignaturePolicy_SignedBy{SignedBy: 0}}

// construct 'one of one' policy
oneOfone := &common.SignaturePolicy{Type: &common.SignaturePolicy_NOutOf_{NOutOf: &common.SignaturePolicy_NOutOf{
N: 1, Rules: []*common.SignaturePolicy{signedBy}}}}

p := &common.SignaturePolicyEnvelope{
Version: 0,
Rule: oneOfone,
Identities: []*mspprotos.MSPPrincipal{onePrn},
}
return p, nil
}
19 changes: 13 additions & 6 deletions pkg/fabric-client/channel/txnsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
"github.com/hyperledger/fabric-sdk-go/api/apitxn/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
)
Expand Down Expand Up @@ -149,38 +150,44 @@ func TestSendInstantiateProposal(t *testing.T) {
channel.AddPeer(&peer)

tresponse, txnid, err := channel.SendInstantiateProposal("", nil, "",
"", targets)
"", cauthdsl.SignedByMspMember("Org1MSP"), targets)

if err == nil || err.Error() != "Missing 'chaincodeName' parameter" {
t.Fatal("Validation for chain code name parameter for send Instantiate Proposal failed")
}

tresponse, txnid, err = channel.SendInstantiateProposal("qscc", nil, "",
"", targets)
"", cauthdsl.SignedByMspMember("Org1MSP"), targets)

tresponse, txnid, err = channel.SendInstantiateProposal("qscc", nil, "",
"", targets)
"", cauthdsl.SignedByMspMember("Org1MSP"), targets)

if err == nil || err.Error() != "Missing 'chaincodePath' parameter" {
t.Fatal("Validation for chain code path for send Instantiate Proposal failed")
}

tresponse, txnid, err = channel.SendInstantiateProposal("qscc", nil, "test",
"", targets)
"", cauthdsl.SignedByMspMember("Org1MSP"), targets)

if err == nil || err.Error() != "Missing 'chaincodeVersion' parameter" {
t.Fatal("Validation for chain code version for send Instantiate Proposal failed")
}

tresponse, txnid, err = channel.SendInstantiateProposal("qscc", nil, "test",
"1", targets)
"1", nil, nil)
if err == nil || err.Error() != "Missing 'chaincodePolicy' parameter" {
t.Fatal("Validation for chain code policy for send Instantiate Proposal failed")
}

tresponse, txnid, err = channel.SendInstantiateProposal("qscc", nil, "test",
"1", cauthdsl.SignedByMspMember("Org1MSP"), targets)

if err != nil || len(tresponse) == 0 || txnid.ID == "" {
t.Fatal("Send Instantiate Proposal Test failed")
}

tresponse, txnid, err = channel.SendInstantiateProposal("qscc", nil, "test",
"1", nil)
"1", cauthdsl.SignedByMspMember("Org1MSP"), nil)
if err == nil || err.Error() != "Missing peer objects for instantiate CC proposal" {
t.Fatal("Missing peer objects validation is not working as expected")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fabric-txn/admin/transactionconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func SendInstallCC(client fab.FabricClient, chainCodeID string, chainCodePath st

// SendInstantiateCC Sends instantiate CC proposal to one or more endorsing peers
func SendInstantiateCC(channel fab.Channel, chainCodeID string, args []string,
chaincodePath string, chaincodeVersion string, targets []apitxn.ProposalProcessor, eventHub fab.EventHub) error {
chaincodePath string, chaincodeVersion string, chaincodePolicy *common.SignaturePolicyEnvelope, targets []apitxn.ProposalProcessor, eventHub fab.EventHub) error {

transactionProposalResponse, txID, err := channel.SendInstantiateProposal(chainCodeID,
args, chaincodePath, chaincodeVersion, targets)
args, chaincodePath, chaincodeVersion, chaincodePolicy, targets)
if err != nil {
return fmt.Errorf("SendInstantiateProposal returned error: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/orderer"
fabricTxn "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn"
admin "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/admin"
"github.com/hyperledger/fabric/common/cauthdsl"
pb "github.com/hyperledger/fabric/protos/peer"
)

Expand Down Expand Up @@ -162,7 +163,9 @@ func (setup *BaseSetupImpl) InstantiateCC(chainCodeID string, chainCodePath stri
// must reset client user context to normal user once done with Admin privilieges
defer setup.Client.SetUserContext(setup.NormalUser)

if err := admin.SendInstantiateCC(setup.Channel, chainCodeID, args, chainCodePath, chainCodeVersion, []apitxn.ProposalProcessor{setup.Channel.PrimaryPeer()}, setup.EventHub); err != nil {
chaincodePolicy := cauthdsl.SignedByMspMember(setup.Client.UserContext().MspID())

if err := admin.SendInstantiateCC(setup.Channel, chainCodeID, args, chainCodePath, chainCodeVersion, chaincodePolicy, []apitxn.ProposalProcessor{setup.Channel.PrimaryPeer()}, setup.EventHub); err != nil {
return err
}
return nil
Expand Down
6 changes: 5 additions & 1 deletion test/integration/orgs/test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/admin"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/cauthdsl"
)

var org1 = "peerorg1"
Expand Down Expand Up @@ -129,8 +130,11 @@ func installAndInstantiate(t *testing.T) {
"github.com/example_cc", "0", nil, []fab.Peer{orgTestPeer1}, "../../fixtures")
failTestIfError(err, t)

chaincodePolicy := cauthdsl.SignedByAnyMember([]string{
org1AdminUser.MspID(), org2AdminUser.MspID()})

err = admin.SendInstantiateCC(orgTestChannel, "exampleCC",
generateInitArgs(), "github.com/example_cc", "0", []apitxn.ProposalProcessor{orgTestPeer1}, peer1EventHub)
generateInitArgs(), "github.com/example_cc", "0", chaincodePolicy, []apitxn.ProposalProcessor{orgTestPeer1}, peer1EventHub)
failTestIfError(err, t)
}

Expand Down

0 comments on commit 1ee9a93

Please sign in to comment.