Skip to content

Commit aad1832

Browse files
committed
Removing mock policies from MCS
This change-set modifies peer's implementation of the gossip MessageCryptoService interface in the following way 1. removes the mocked policyManager from VerifyByChannel 2. MCS's implementation takes in input a PolicyManager whose role is to give access to the PolicyManager of a specific channel. This object is then used by VerifyByChannel to retrieve the PolicyManager for a specific channel. Change-Id: Ibeb37ccdb4233d146f50675ce227191d45faebb6 Signed-off-by: Angelo De Caro <[email protected]>
1 parent e829d2e commit aad1832

File tree

10 files changed

+105
-28
lines changed

10 files changed

+105
-28
lines changed

common/mocks/policies/policies.go

+18
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,21 @@ func (m *Manager) GetPolicy(id string) (policies.Policy, bool) {
7777
}
7878
return m.Policy, m.Policy != nil
7979
}
80+
81+
type PolicyManagerMgmt struct{}
82+
83+
func (m *PolicyManagerMgmt) GetPolicy(id string) (policies.Policy, bool) {
84+
panic("implement me")
85+
}
86+
87+
func (m *PolicyManagerMgmt) Manager(path []string) (policies.Manager, bool) {
88+
return &Manager{Policy: &Policy{Err: nil}}, false
89+
}
90+
91+
func (m *PolicyManagerMgmt) BasePath() string {
92+
panic("implement me")
93+
}
94+
95+
func (m *PolicyManagerMgmt) PolicyNames() []string {
96+
panic("implement me")
97+
}

common/policies/policy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
)
2626

2727
const (
28-
// ChannelReaders is the label for the channel's readers policy
29-
ChannelReaders = "ChannelReaders"
28+
// ChannelApplicationReaders is the label for the channel's application readers policy
29+
ChannelApplicationReaders = "/channel/Application/Readers"
3030
)
3131

3232
var logger = logging.MustGetLogger("common/policies")

core/peer/peer.go

+50
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/hyperledger/fabric/common/configtx"
2626
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2727
configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
28+
"github.com/hyperledger/fabric/common/policies"
2829
"github.com/hyperledger/fabric/core/comm"
2930
"github.com/hyperledger/fabric/core/committer"
3031
"github.com/hyperledger/fabric/core/committer/txvalidator"
@@ -247,6 +248,17 @@ func GetLedger(cid string) ledger.PeerLedger {
247248
return nil
248249
}
249250

251+
// GetPolicyManager returns the policy manager of the chain with chain ID. Note that this
252+
// call returns nil if chain cid has not been created.
253+
func GetPolicyManager(cid string) policies.Manager {
254+
chains.RLock()
255+
defer chains.RUnlock()
256+
if c, ok := chains.list[cid]; ok {
257+
return c.cs.PolicyManager()
258+
}
259+
return nil
260+
}
261+
250262
// GetMSPMgr returns the MSP manager of the chain with chain ID.
251263
// Note that this call returns nil if chain cid has not been created.
252264
func GetMSPMgr(cid string) msp.MSPManager {
@@ -333,3 +345,41 @@ func NewPeerClientConnectionWithAddress(peerAddress string) (*grpc.ClientConn, e
333345
}
334346
return comm.NewClientConnectionWithAddress(peerAddress, true, false, nil)
335347
}
348+
349+
// GetPolicyManagerMgmt returns a special PolicyManager whose
350+
// only function is to give access to the policy manager of
351+
// a given channel. If the channel does not exists then,
352+
// it returns nil.
353+
// The only method implemented is therefore 'Manager'.
354+
func GetPolicyManagerMgmt() policies.Manager {
355+
return &policyManagerMgmt{}
356+
}
357+
358+
type policyManagerMgmt struct{}
359+
360+
func (c *policyManagerMgmt) GetPolicy(id string) (policies.Policy, bool) {
361+
panic("implement me")
362+
}
363+
364+
// Manager returns the policy manager associated to a channel
365+
// specified by a path of length 1 that has the name of the channel as the only
366+
// coordinate available.
367+
// If the path has length different from 1, then the method returns (nil, false).
368+
// If the channel does not exists, then the method returns (nil, false)
369+
// Nothing is created.
370+
func (c *policyManagerMgmt) Manager(path []string) (policies.Manager, bool) {
371+
if len(path) != 1 {
372+
return nil, false
373+
}
374+
375+
policyManager := GetPolicyManager(path[0])
376+
return policyManager, policyManager != nil
377+
}
378+
379+
func (c *policyManagerMgmt) BasePath() string {
380+
panic("implement me")
381+
}
382+
383+
func (c *policyManagerMgmt) PolicyNames() []string {
384+
panic("implement me")
385+
}

core/peer/peer_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ import (
2323
"testing"
2424

2525
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
26+
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
2627
ccp "github.com/hyperledger/fabric/core/common/ccprovider"
2728
"github.com/hyperledger/fabric/core/deliverservice"
2829
"github.com/hyperledger/fabric/core/deliverservice/blocksprovider"
2930
"github.com/hyperledger/fabric/core/mocks/ccprovider"
3031
"github.com/hyperledger/fabric/gossip/service"
3132
"github.com/hyperledger/fabric/msp/mgmt"
3233
"github.com/hyperledger/fabric/msp/mgmt/testtools"
34+
"github.com/hyperledger/fabric/peer/gossip/mcs"
3335
"github.com/spf13/viper"
3436
"github.com/stretchr/testify/assert"
3537
"google.golang.org/grpc"
@@ -86,7 +88,8 @@ func TestCreateChainFromBlock(t *testing.T) {
8688
msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig")
8789

8890
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
89-
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{})
91+
messageCryptoService := mcs.New(&mockpolicies.PolicyManagerMgmt{})
92+
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{}, messageCryptoService)
9093

9194
err = CreateChainFromBlock(block)
9295
if err != nil {

core/scc/cscc/configure_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/golang/protobuf/proto"
2626
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
27+
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
2728
"github.com/hyperledger/fabric/core/chaincode"
2829
"github.com/hyperledger/fabric/core/chaincode/shim"
2930
"github.com/hyperledger/fabric/core/deliverservice"
@@ -33,6 +34,7 @@ import (
3334
"github.com/hyperledger/fabric/gossip/service"
3435
"github.com/hyperledger/fabric/msp/mgmt"
3536
"github.com/hyperledger/fabric/msp/mgmt/testtools"
37+
"github.com/hyperledger/fabric/peer/gossip/mcs"
3638
"github.com/hyperledger/fabric/protos/common"
3739
pb "github.com/hyperledger/fabric/protos/peer"
3840
"github.com/hyperledger/fabric/protos/utils"
@@ -145,8 +147,8 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
145147

146148
msptesttools.LoadMSPSetupForTesting("../../../msp/sampleconfig")
147149
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
148-
149-
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{})
150+
messageCryptoService := mcs.New(&mockpolicies.PolicyManagerMgmt{})
151+
service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{}, messageCryptoService)
150152

151153
// Successful path for JoinChain
152154
blockBytes := mockConfigBlock()

gossip/service/gossip_service.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/hyperledger/fabric/gossip/integration"
3030
"github.com/hyperledger/fabric/gossip/state"
3131
"github.com/hyperledger/fabric/gossip/util"
32-
"github.com/hyperledger/fabric/peer/gossip/mcs"
3332
"github.com/hyperledger/fabric/peer/gossip/sa"
3433
"github.com/hyperledger/fabric/protos/common"
3534
proto "github.com/hyperledger/fabric/protos/gossip"
@@ -99,13 +98,13 @@ func (jcm *joinChannelMessage) AnchorPeers() []api.AnchorPeer {
9998
var logger = util.GetLogger(util.LoggingServiceModule, "")
10099

101100
// InitGossipService initialize gossip service
102-
func InitGossipService(peerIdentity []byte, endpoint string, s *grpc.Server, bootPeers ...string) {
103-
InitGossipServiceCustomDeliveryFactory(peerIdentity, endpoint, s, &deliveryFactoryImpl{}, bootPeers...)
101+
func InitGossipService(peerIdentity []byte, endpoint string, s *grpc.Server, mcs api.MessageCryptoService, bootPeers ...string) {
102+
InitGossipServiceCustomDeliveryFactory(peerIdentity, endpoint, s, &deliveryFactoryImpl{}, mcs, bootPeers...)
104103
}
105104

106105
// InitGossipService initialize gossip service with customize delivery factory
107106
// implementation, might be useful for testing and mocking purposes
108-
func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string, s *grpc.Server, factory DeliveryServiceFactory, bootPeers ...string) {
107+
func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string, s *grpc.Server, factory DeliveryServiceFactory, mcs api.MessageCryptoService, bootPeers ...string) {
109108
once.Do(func() {
110109
logger.Info("Initialize gossip with endpoint", endpoint, "and bootstrap set", bootPeers)
111110
dialOpts := []grpc.DialOption{}
@@ -115,7 +114,6 @@ func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string
115114
dialOpts = append(dialOpts, grpc.WithInsecure())
116115
}
117116

118-
cryptSvc := mcs.NewMessageCryptoService()
119117
secAdv := sa.NewSecurityAdvisor()
120118

121119
if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" {
@@ -124,14 +122,14 @@ func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string
124122

125123
if viper.GetBool("peer.gossip.ignoreSecurity") {
126124
sec := &secImpl{[]byte(endpoint)}
127-
cryptSvc = sec
125+
mcs = sec
128126
secAdv = sec
129127
peerIdentity = []byte(endpoint)
130128
}
131129

132-
idMapper := identity.NewIdentityMapper(cryptSvc)
130+
idMapper := identity.NewIdentityMapper(mcs)
133131

134-
gossip := integration.NewGossipComponent(peerIdentity, endpoint, s, secAdv, cryptSvc, idMapper, dialOpts, bootPeers...)
132+
gossip := integration.NewGossipComponent(peerIdentity, endpoint, s, secAdv, mcs, idMapper, dialOpts, bootPeers...)
135133
gossipServiceInstance = &gossipServiceImpl{
136134
gossipSvc: gossip,
137135
chains: make(map[string]state.GossipStateProvider),

gossip/service/gossip_service_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424

2525
"time"
2626

27+
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
2728
"github.com/hyperledger/fabric/gossip/api"
2829
"github.com/hyperledger/fabric/msp/mgmt"
2930
"github.com/hyperledger/fabric/msp/mgmt/testtools"
31+
"github.com/hyperledger/fabric/peer/gossip/mcs"
3032
"github.com/stretchr/testify/assert"
3133
"google.golang.org/grpc"
3234
)
@@ -47,7 +49,8 @@ func TestInitGossipService(t *testing.T) {
4749
wg.Add(10)
4850
for i := 0; i < 10; i++ {
4951
go func() {
50-
InitGossipService(identity, "localhost:5611", grpcServer)
52+
InitGossipService(identity, "localhost:5611", grpcServer, mcs.New(&mockpolicies.PolicyManagerMgmt{}))
53+
5154
wg.Done()
5255
}()
5356
}

peer/gossip/mcs/mcs.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/hyperledger/fabric/bccsp"
2424
"github.com/hyperledger/fabric/bccsp/factory"
25-
mockpolicy "github.com/hyperledger/fabric/common/mocks/policies"
2625
"github.com/hyperledger/fabric/common/policies"
2726
"github.com/hyperledger/fabric/gossip/api"
2827
"github.com/hyperledger/fabric/gossip/common"
@@ -43,16 +42,18 @@ var logger = logging.MustGetLogger("peer/gossip/mcs")
4342
//
4443
// A similar mechanism needs to be in place to update the local MSP, as well.
4544
// This implementation assumes that these mechanisms are all in place and working.
46-
//
47-
// TODO: The code currently does not validate an identity against the channel
48-
// read policy for the channel related gossip message.
4945
type mspMessageCryptoService struct {
46+
manager policies.Manager
5047
}
5148

52-
// NewMessageCryptoService creates a new instance of mspMessageCryptoService
53-
// that implements MessageCryptoService
54-
func NewMessageCryptoService() api.MessageCryptoService {
55-
return &mspMessageCryptoService{}
49+
// New creates a new instance of mspMessageCryptoService
50+
// that implements MessageCryptoService.
51+
// The method takes in input a policy manager that gives
52+
// access to the policy manager of a given channel via the Manager method.
53+
// See fabric/core/peer/peer.go#NewPolicyManagerMgmt and
54+
// fabric/common/mocks/policies/policies.go#PolicyManagerMgmt
55+
func New(manager policies.Manager) api.MessageCryptoService {
56+
return &mspMessageCryptoService{manager: manager}
5657
}
5758

5859
// ValidateIdentity validates the identity of a remote peer.
@@ -146,12 +147,11 @@ func (s *mspMessageCryptoService) VerifyByChannel(chainID common.ChainID, peerId
146147
}
147148

148149
// Get the policy manager for channel chainID
149-
// TODO: replace this mock with the proper lookup once in place
150-
// For now, we accept all
151-
policyManager := mockpolicy.Manager{Policy: &mockpolicy.Policy{Err: nil}}
150+
cpm, flag := s.manager.Manager([]string{string(chainID)})
151+
logger.Debugf("Got policy manager for channel [%s] with flag [%s]", string(chainID), flag)
152152

153153
// Get channel reader policy
154-
policy, flag := policyManager.GetPolicy(policies.ChannelReaders)
154+
policy, flag := cpm.GetPolicy(policies.ChannelApplicationReaders)
155155
logger.Debugf("Got reader policy for channel [%s] with flag [%s]", string(chainID), flag)
156156

157157
return policy.Evaluate(

peer/gossip/mcs/mcs_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/hyperledger/fabric/bccsp"
2626
"github.com/hyperledger/fabric/bccsp/factory"
27+
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
2728
"github.com/hyperledger/fabric/gossip/api"
2829
"github.com/hyperledger/fabric/msp/mgmt"
2930
"github.com/hyperledger/fabric/msp/mgmt/testtools"
@@ -47,7 +48,7 @@ func TestMain(m *testing.M) {
4748
}
4849

4950
// Init the MSP-based MessageCryptoService
50-
msgCryptoService = NewMessageCryptoService()
51+
msgCryptoService = New(&mockpolicies.PolicyManagerMgmt{})
5152

5253
os.Exit(m.Run())
5354
}

peer/node/start.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/hyperledger/fabric/gossip/service"
4242
"github.com/hyperledger/fabric/msp/mgmt"
4343
"github.com/hyperledger/fabric/peer/common"
44+
"github.com/hyperledger/fabric/peer/gossip/mcs"
4445
pb "github.com/hyperledger/fabric/protos/peer"
4546
"github.com/spf13/cobra"
4647
"github.com/spf13/viper"
@@ -149,7 +150,8 @@ func serve(args []string) error {
149150
panic(fmt.Sprintf("Failed serializing self identity: %v", err))
150151
}
151152

152-
service.InitGossipService(serializedIdentity, peerEndpoint.Address, grpcServer.Server(), bootstrap...)
153+
messageCryptoService := mcs.New(peer.GetPolicyManagerMgmt())
154+
service.InitGossipService(serializedIdentity, peerEndpoint.Address, grpcServer.Server(), messageCryptoService, bootstrap...)
153155
defer service.GetGossipService().Stop()
154156

155157
//initialize system chaincodes

0 commit comments

Comments
 (0)