Skip to content

Commit

Permalink
[FAB-10694] support for cauthdsl.AcceptAllPolicy
Browse files Browse the repository at this point in the history
- fix for 'N mustbe greater than 0' error when
'cauthdsl.AcceptAllPolicy' policy is used.


Change-Id: Icd88bd4027273017e2123df181ea44001646920e
Signed-off-by: Sudesh Shetty <[email protected]>
  • Loading branch information
sudeshrshetty committed Jun 19, 2018
1 parent 88455b4 commit 884d2cd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
mocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
common "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
)

Expand Down Expand Up @@ -100,6 +101,18 @@ func TestPeerGroupResolverPolicy1(t *testing.T) {
testPeerGroupResolver(t, sigPolicyEnv, allPeers, expected, nil)
}

func TestPeerGroupResolverAcceptAllPolicy(t *testing.T) {

sigPolicyEnv := cauthdsl.AcceptAllPolicy

expected := []PeerGroup{
pg(p1), pg(p2), pg(p3), pg(p4), pg(p5), pg(p6),
pg(p7), pg(p8), pg(p9), pg(p10), pg(p11), pg(p12),
}

testPeerGroupResolver(t, sigPolicyEnv, allPeers, expected, nil)
}

// 1 of [(2 of [1,2]),(3 of [3,4,5])]
func TestPeerGroupResolverPolicy2(t *testing.T) {
signedBy, identities, err := GetPolicies(org1, org2, org3, org4, org5)
Expand Down Expand Up @@ -352,7 +365,6 @@ func verify(t *testing.T, pgResolver PeerGroupResolver, peers []fab.Peer, expect
} else if expectedErr != nil {
t.Fatalf("expecting error [%s] but got none", expectedErr)
}

for i := 0; i < len(expectedPeerGroups); i++ {
if !containsPeerGroup(expectedPeerGroups, peerGroup) {
t.Fatalf("peer group %s is not one of the expected peer groups: %v", peerGroup, expectedPeerGroups)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *peerGroupResolver) Resolve(peers []fab.Peer) (PeerGroup, error) {
peerRetriever := func(mspID string) []fab.Peer {
var mspPeers []fab.Peer
for _, peer := range peers {
if peer.MSPID() == mspID {
if mspID == "" || peer.MSPID() == mspID {
mspPeers = append(mspPeers, peer)
}
}
Expand Down Expand Up @@ -188,40 +188,55 @@ func (c *signaturePolicyCompiler) compile(sigPolicy *common.SignaturePolicy, ide
}

switch t := sigPolicy.Type.(type) {

case *common.SignaturePolicy_SignedBy:

return signaturePolicySignedBy(t, identities)

case *common.SignaturePolicy_NOutOf_:
nOutOfPolicy := t.NOutOf
var pfuncs []GroupRetriever
for _, policy := range nOutOfPolicy.Rules {
f, err := c.compile(policy, identities)
if err != nil {
return nil, err
}
pfuncs = append(pfuncs, f)

return c.signaturePolicyNOutOf(t, identities)

default:
errMsg := fmt.Sprintf("unsupported signature policy type: %v", t)
return nil, errors.New(errMsg)

}
}

func (c *signaturePolicyCompiler) signaturePolicyNOutOf(t *common.SignaturePolicy_NOutOf_, identities []*mb.MSPPrincipal) (GroupRetriever, error) {

nOutOfPolicy := t.NOutOf
if nOutOfPolicy.N == 0 {
return signaturePolicySignedByAny()
}

var pfuncs []GroupRetriever
for _, policy := range nOutOfPolicy.Rules {
f, err := c.compile(policy, identities)
if err != nil {
return nil, err
}
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
var groups []Group
for _, f := range pfuncs {
grps, err := f(peerRetriever)
if err != nil {
return nil, err
}
groups = append(groups, grps)
}
pfuncs = append(pfuncs, f)
}

itemGroups, err := NewGroupOfGroups(groups).Nof(nOutOfPolicy.N)
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
var groups []Group
for _, f := range pfuncs {
grps, err := f(peerRetriever)
if err != nil {
return nil, err
}
groups = append(groups, grps)
}

return itemGroups, nil
}, nil
itemGroups, err := NewGroupOfGroups(groups).Nof(nOutOfPolicy.N)
if err != nil {
return nil, err
}

default:
errMsg := fmt.Sprintf("unsupported signature policy type: %v", t)
return nil, errors.New(errMsg)
}
return itemGroups, nil
}, nil
}

func signaturePolicySignedBy(t *common.SignaturePolicy_SignedBy, identities []*mb.MSPPrincipal) (GroupRetriever, error) {
Expand All @@ -234,6 +249,12 @@ func signaturePolicySignedBy(t *common.SignaturePolicy_SignedBy, identities []*m
}, nil
}

func signaturePolicySignedByAny() (GroupRetriever, error) {
return func(peerRetriever MSPPeerRetriever) (GroupOfGroups, error) {
return NewGroupOfGroups([]Group{NewMSPPeerGroup("", peerRetriever)}), nil
}, nil
}

func mspPrincipalToString(principal *mb.MSPPrincipal) (string, error) {
switch principal.PrincipalClassification {
case mb.MSPPrincipal_ROLE:
Expand Down

0 comments on commit 884d2cd

Please sign in to comment.