Skip to content

Commit b9be659

Browse files
committed
[FAB-11323] added pvtdata membership info provider
this change set adds pvtdata membership info provider interface and an implementation of the interface. Change-Id: I90b31f26141d292dd52d2524e2a1527dd4ba818f Signed-off-by: nirro <nirro@il.ibm.com>
1 parent b81fb36 commit b9be659

File tree

8 files changed

+137
-7
lines changed

8 files changed

+137
-7
lines changed

core/common/privdata/collection.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ type CollectionStore interface {
8585

8686
// RetrieveCollectionPersistenceConfigs retrieves the collection's persistence related configurations
8787
RetrieveCollectionPersistenceConfigs(cc common.CollectionCriteria) (CollectionPersistenceConfigs, error)
88+
89+
CollectionFilter
90+
}
91+
92+
type CollectionFilter interface {
93+
// AccessFilter retrieves the collection's filter that matches a given channel and a collectionPolicyConfig
94+
AccessFilter(channelName string, collectionPolicyConfig *common.CollectionPolicyConfig) (Filter, error)
8895
}
8996

9097
const (
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package privdata
8+
9+
import (
10+
"github.com/hyperledger/fabric/protos/common"
11+
)
12+
13+
// MembershipInfoProvider interface defines an interface to check whether a peer is eligible to a collection or not
14+
type MembershipInfoProvider interface {
15+
// AmMemberOf checks whether the current peer is a member of the given collection config
16+
AmMemberOf(collectionPolicyConfig *common.CollectionPolicyConfig) (bool, error)
17+
}
18+
19+
type membershipProvider struct {
20+
selfSignedData common.SignedData
21+
cf CollectionFilter
22+
channelName string
23+
}
24+
25+
func NewMembershipInfoProvider(channelName string, selfSignedData common.SignedData, filter CollectionFilter) MembershipInfoProvider {
26+
return &membershipProvider{channelName: channelName, selfSignedData: selfSignedData, cf: filter}
27+
}
28+
29+
func (m *membershipProvider) AmMemberOf(collectionPolicyConfig *common.CollectionPolicyConfig) (bool, error) {
30+
filt, err := m.cf.AccessFilter(m.channelName, collectionPolicyConfig)
31+
if err != nil {
32+
return false, err
33+
}
34+
return filt(m.selfSignedData), nil
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package privdata
8+
9+
import (
10+
"testing"
11+
12+
"github.com/hyperledger/fabric/common/cauthdsl"
13+
"github.com/hyperledger/fabric/protos/common"
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestMembershipInfoProvider(t *testing.T) {
18+
// define identity of self peer as peer0
19+
peerSelfSignedData := common.SignedData{
20+
Identity: []byte("peer0"),
21+
Signature: []byte{1, 2, 3},
22+
Data: []byte{4, 5, 6},
23+
}
24+
25+
collectionStore := NewSimpleCollectionStore(&mockStoreSupport{})
26+
27+
// verify membership provider returns true
28+
membershipProvider := NewMembershipInfoProvider("test1", peerSelfSignedData, collectionStore)
29+
res, err := membershipProvider.AmMemberOf(getAccessPolicy([]string{"peer0", "peer1"}))
30+
assert.True(t, res)
31+
assert.Nil(t, err)
32+
33+
// verify membership provider returns false
34+
res, err = membershipProvider.AmMemberOf(getAccessPolicy([]string{"peer2", "peer3"}))
35+
assert.False(t, res)
36+
assert.Nil(t, err)
37+
38+
// verify membership provider returns nil and error
39+
res, err = membershipProvider.AmMemberOf(nil)
40+
assert.False(t, res)
41+
assert.Error(t, err)
42+
assert.Equal(t, "Collection config policy is nil", err.Error())
43+
}
44+
45+
func getAccessPolicy(signers []string) *common.CollectionPolicyConfig {
46+
var data [][]byte
47+
for _, signer := range signers {
48+
data = append(data, []byte(signer))
49+
}
50+
policyEnvelope := cauthdsl.Envelope(cauthdsl.Or(cauthdsl.SignedBy(0), cauthdsl.SignedBy(1)), data)
51+
return createCollectionPolicyConfig(policyEnvelope)
52+
}

core/common/privdata/simplecollection.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ func (sc *SimpleCollection) Setup(collectionConfig *common.StaticCollectionConfi
8181
return errors.New("Collection config access policy is nil")
8282
}
8383

84-
// create access policy from the envelope
85-
npp := cauthdsl.NewPolicyProvider(deserializer)
86-
polBytes, err := proto.Marshal(accessPolicyEnvelope)
87-
if err != nil {
88-
return err
89-
}
90-
sc.accessPolicy, _, err = npp.NewPolicy(polBytes)
84+
err := sc.setupAccessPolicy(collectionPolicyConfig, deserializer)
9185
if err != nil {
9286
return err
9387
}
@@ -124,6 +118,27 @@ func (sc *SimpleCollection) Setup(collectionConfig *common.StaticCollectionConfi
124118
return nil
125119
}
126120

121+
// Setup configures a simple collection object based on a given
122+
// StaticCollectionConfig proto that has all the necessary information
123+
func (sc *SimpleCollection) setupAccessPolicy(collectionPolicyConfig *common.CollectionPolicyConfig, deserializer msp.IdentityDeserializer) error {
124+
if collectionPolicyConfig == nil {
125+
return errors.New("Collection config policy is nil")
126+
}
127+
accessPolicyEnvelope := collectionPolicyConfig.GetSignaturePolicy()
128+
if accessPolicyEnvelope == nil {
129+
return errors.New("Collection config access policy is nil")
130+
}
131+
132+
// create access policy from the envelope
133+
npp := cauthdsl.NewPolicyProvider(deserializer)
134+
polBytes, err := proto.Marshal(accessPolicyEnvelope)
135+
if err != nil {
136+
return err
137+
}
138+
sc.accessPolicy, _, err = npp.NewPolicy(polBytes)
139+
return err
140+
}
141+
127142
// BlockToLive return collection's block to live configuration
128143
func (s *SimpleCollectionPersistenceConfigs) BlockToLive() uint64 {
129144
return s.blockToLive

core/common/privdata/store.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ func (c *simpleCollectionStore) retrieveSimpleCollection(cc common.CollectionCri
120120
return sc, nil
121121
}
122122

123+
func (c *simpleCollectionStore) AccessFilter(channelName string, collectionPolicyConfig *common.CollectionPolicyConfig) (Filter, error) {
124+
sc := &SimpleCollection{}
125+
err := sc.setupAccessPolicy(collectionPolicyConfig, c.s.GetIdentityDeserializer(channelName))
126+
if err != nil {
127+
return nil, err
128+
}
129+
return sc.AccessFilter(), nil
130+
}
131+
123132
func (c *simpleCollectionStore) RetrieveCollection(cc common.CollectionCriteria) (Collection, error) {
124133
return c.retrieveSimpleCollection(cc)
125134
}

core/ledger/pvtdatapolicy/testutil/mock_collection_store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func (m *MockCollectionStore) RetrieveCollectionPersistenceConfigs(cc common.Col
4343
return nil, privdata.NoSuchCollectionError{}
4444
}
4545

46+
func (m *MockCollectionStore) AccessFilter(channelName string, collectionPolicyConfig *common.CollectionPolicyConfig) (privdata.Filter, error) {
47+
return nil, errors.New("not implemented")
48+
}
49+
4650
func (m *MockCollectionStore) SetBTL(ns, collection string, btl uint64) {
4751
m.dummyData[[2]string{ns, collection}] = btl
4852
}

gossip/privdata/coordinator_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ func (cs *collectionStore) RetrieveCollectionPersistenceConfigs(cc common.Collec
406406
panic("implement me")
407407
}
408408

409+
func (cs *collectionStore) AccessFilter(channelName string, collectionPolicyConfig *common.CollectionPolicyConfig) (privdata.Filter, error) {
410+
panic("implement me")
411+
}
412+
409413
type collectionAccessPolicy struct {
410414
cs *collectionStore
411415
n uint64

gossip/privdata/pull_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func (cs mockCollectionStore) RetrieveCollectionPersistenceConfigs(cc fcommon.Co
8989
return cs.m[cc.Collection], nil
9090
}
9191

92+
func (cs mockCollectionStore) AccessFilter(channelName string, collectionPolicyConfig *fcommon.CollectionPolicyConfig) (privdata.Filter, error) {
93+
panic("implement me")
94+
}
95+
9296
type mockCollectionAccess struct {
9397
cs *mockCollectionStore
9498
btl uint64

0 commit comments

Comments
 (0)