diff --git a/core/peer/peer_test.go b/core/peer/peer_test.go index 67ab4b81a69..e5a566d3ba4 100644 --- a/core/peer/peer_test.go +++ b/core/peer/peer_test.go @@ -32,7 +32,8 @@ import ( "github.com/hyperledger/fabric/gossip/service" "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/msp/mgmt/testtools" - "github.com/hyperledger/fabric/peer/gossip/mcs" + peergossip "github.com/hyperledger/fabric/peer/gossip" + "github.com/hyperledger/fabric/peer/gossip/mocks" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "google.golang.org/grpc" @@ -94,7 +95,7 @@ func TestCreateChainFromBlock(t *testing.T) { msptesttools.LoadMSPSetupForTesting() identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() - messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) + messageCryptoService := peergossip.NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) service.InitGossipServiceCustomDeliveryFactory(identity, "localhost:13611", grpcServer, &mockDeliveryClientFactory{}, messageCryptoService) err = CreateChainFromBlock(block) diff --git a/core/scc/cscc/configure_test.go b/core/scc/cscc/configure_test.go index d63525f8ffd..43065742825 100644 --- a/core/scc/cscc/configure_test.go +++ b/core/scc/cscc/configure_test.go @@ -37,7 +37,8 @@ import ( "github.com/hyperledger/fabric/gossip/service" "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/msp/mgmt/testtools" - "github.com/hyperledger/fabric/peer/gossip/mcs" + peergossip "github.com/hyperledger/fabric/peer/gossip" + "github.com/hyperledger/fabric/peer/gossip/mocks" "github.com/hyperledger/fabric/protos/common" pb "github.com/hyperledger/fabric/protos/peer" "github.com/hyperledger/fabric/protos/utils" @@ -164,7 +165,7 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) { ) identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() - messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) + messageCryptoService := peergossip.NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) service.InitGossipServiceCustomDeliveryFactory(identity, peerEndpoint, nil, &mockDeliveryClientFactory{}, messageCryptoService) // Successful path for JoinChain diff --git a/gossip/service/gossip_service.go b/gossip/service/gossip_service.go index 6394c975d69..ab550390382 100644 --- a/gossip/service/gossip_service.go +++ b/gossip/service/gossip_service.go @@ -31,7 +31,7 @@ import ( "github.com/hyperledger/fabric/gossip/integration" "github.com/hyperledger/fabric/gossip/state" "github.com/hyperledger/fabric/gossip/util" - "github.com/hyperledger/fabric/peer/gossip/sa" + peergossip "github.com/hyperledger/fabric/peer/gossip" "github.com/hyperledger/fabric/protos/common" proto "github.com/hyperledger/fabric/protos/gossip" "github.com/spf13/viper" @@ -141,7 +141,7 @@ func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string dialOpts = append(dialOpts, grpc.WithInsecure()) } - secAdv := sa.NewSecurityAdvisor() + secAdv := peergossip.NewSecurityAdvisor() if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" { endpoint = overrideEndpoint diff --git a/gossip/service/gossip_service_test.go b/gossip/service/gossip_service_test.go index b4d3c1b0fc1..f6bf91d005c 100644 --- a/gossip/service/gossip_service_test.go +++ b/gossip/service/gossip_service_test.go @@ -37,7 +37,8 @@ import ( "github.com/hyperledger/fabric/gossip/util" "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/msp/mgmt/testtools" - "github.com/hyperledger/fabric/peer/gossip/mcs" + peergossip "github.com/hyperledger/fabric/peer/gossip" + "github.com/hyperledger/fabric/peer/gossip/mocks" "github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/peer" "github.com/op/go-logging" @@ -66,7 +67,7 @@ func TestInitGossipService(t *testing.T) { wg.Add(10) for i := 0; i < 10; i++ { go func() { - messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) + messageCryptoService := peergossip.NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) InitGossipService(identity, "localhost:5611", grpcServer, messageCryptoService) wg.Done() diff --git a/peer/gossip/mcs/mcs.go b/peer/gossip/mcs.go similarity index 89% rename from peer/gossip/mcs/mcs.go rename to peer/gossip/mcs.go index a4abb366945..740bfd08413 100644 --- a/peer/gossip/mcs/mcs.go +++ b/peer/gossip/mcs.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mcs +package gossip import ( "bytes" @@ -35,7 +35,7 @@ import ( "github.com/hyperledger/fabric/protos/utils" ) -var logger = flogging.MustGetLogger("peer/gossip/mcs") +var mcsLogger = flogging.MustGetLogger("peer/gossip/mcs") // mspMessageCryptoService implements the MessageCryptoService interface // using the peer MSPs (local and channel-related) @@ -52,13 +52,13 @@ type mspMessageCryptoService struct { deserializer mgmt.DeserializersManager } -// New creates a new instance of mspMessageCryptoService +// NewMCS creates a new instance of mspMessageCryptoService // that implements MessageCryptoService. // The method takes in input: // 1. a policies.ChannelPolicyManagerGetter that gives access to the policy manager of a given channel via the Manager method. // 2. an instance of crypto.LocalSigner // 3. an identity deserializer manager -func New(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localSigner crypto.LocalSigner, deserializer mgmt.DeserializersManager) api.MessageCryptoService { +func NewMCS(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localSigner crypto.LocalSigner, deserializer mgmt.DeserializersManager) api.MessageCryptoService { return &mspMessageCryptoService{channelPolicyManagerGetter: channelPolicyManagerGetter, localSigner: localSigner, deserializer: deserializer} } @@ -83,14 +83,14 @@ func (s *mspMessageCryptoService) ValidateIdentity(peerIdentity api.PeerIdentity func (s *mspMessageCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType { // Validate arguments if len(peerIdentity) == 0 { - logger.Error("Invalid Peer Identity. It must be different from nil.") + mcsLogger.Error("Invalid Peer Identity. It must be different from nil.") return nil } sid, err := s.deserializer.Deserialize(peerIdentity) if err != nil { - logger.Errorf("Failed getting validated identity from peer identity [% x]: [%s]", peerIdentity, err) + mcsLogger.Errorf("Failed getting validated identity from peer identity [% x]: [%s]", peerIdentity, err) return nil } @@ -105,7 +105,7 @@ func (s *mspMessageCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityTy // Hash digest, err := factory.GetDefault().Hash(raw, &bccsp.SHA256Opts{}) if err != nil { - logger.Errorf("Failed computing digest of serialized identity [% x]: [%s]", peerIdentity, err) + mcsLogger.Errorf("Failed computing digest of serialized identity [% x]: [%s]", peerIdentity, err) return nil } @@ -160,12 +160,12 @@ func (s *mspMessageCryptoService) VerifyBlock(chainID common.ChainID, signedBloc return fmt.Errorf("Could not acquire policy manager for channel %s", channelID) } // ok is true if it was the manager requested, or false if it is the default manager - logger.Debugf("Got policy manager for channel [%s] with flag [%s]", channelID, ok) + mcsLogger.Debugf("Got policy manager for channel [%s] with flag [%s]", channelID, ok) // Get block validation policy policy, ok := cpm.GetPolicy(policies.BlockValidation) // ok is true if it was the policy requested, or false if it is the default policy - logger.Debugf("Got block validation policy for channel [%s] with flag [%s]", channelID, ok) + mcsLogger.Debugf("Got block validation policy for channel [%s] with flag [%s]", channelID, ok) // - Prepare SignedData signatureSet := []*pcommon.SignedData{} @@ -200,7 +200,7 @@ func (s *mspMessageCryptoService) Sign(msg []byte) ([]byte, error) { func (s *mspMessageCryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error { identity, chainID, err := s.getValidatedIdentity(peerIdentity) if err != nil { - logger.Errorf("Failed getting validated identity from peer identity [%s]", err) + mcsLogger.Errorf("Failed getting validated identity from peer identity [%s]", err) return err } @@ -234,11 +234,11 @@ func (s *mspMessageCryptoService) VerifyByChannel(chainID common.ChainID, peerId if cpm == nil { return fmt.Errorf("Could not acquire policy manager for channel %s", string(chainID)) } - logger.Debugf("Got policy manager for channel [%s] with flag [%s]", string(chainID), flag) + mcsLogger.Debugf("Got policy manager for channel [%s] with flag [%s]", string(chainID), flag) // Get channel reader policy policy, flag := cpm.GetPolicy(policies.ChannelApplicationReaders) - logger.Debugf("Got reader policy for channel [%s] with flag [%s]", string(chainID), flag) + mcsLogger.Debugf("Got reader policy for channel [%s] with flag [%s]", string(chainID), flag) return policy.Evaluate( []*pcommon.SignedData{{ @@ -290,7 +290,7 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden // Deserialize identity identity, err := mspManager.DeserializeIdentity([]byte(peerIdentity)) if err != nil { - logger.Debugf("Failed deserialization identity [% x] on [%s]: [%s]", peerIdentity, chainID, err) + mcsLogger.Debugf("Failed deserialization identity [% x] on [%s]: [%s]", peerIdentity, chainID, err) continue } @@ -300,11 +300,11 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden // This will be done by the caller function, if needed. if err := identity.Validate(); err != nil { - logger.Debugf("Failed validating identity [% x] on [%s]: [%s]", peerIdentity, chainID, err) + mcsLogger.Debugf("Failed validating identity [% x] on [%s]: [%s]", peerIdentity, chainID, err) continue } - logger.Debugf("Validation succeeded [% x] on [%s]", peerIdentity, chainID) + mcsLogger.Debugf("Validation succeeded [% x] on [%s]", peerIdentity, chainID) return identity, common.ChainID(chainID), nil } diff --git a/peer/gossip/mcs/mocks.go b/peer/gossip/mcs/mocks.go deleted file mode 100644 index 670b53b9f98..00000000000 --- a/peer/gossip/mcs/mocks.go +++ /dev/null @@ -1,163 +0,0 @@ -/* -Copyright IBM Corp. 2017 All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package mcs - -import ( - "bytes" - - "fmt" - - "errors" - - mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" - "github.com/hyperledger/fabric/common/policies" - "github.com/hyperledger/fabric/msp" - "github.com/hyperledger/fabric/protos/common" - mspproto "github.com/hyperledger/fabric/protos/msp" -) - -type MockChannelPolicyManagerGetter struct{} - -func (c *MockChannelPolicyManagerGetter) Manager(channelID string) (policies.Manager, bool) { - return &mockpolicies.Manager{Policy: &mockpolicies.Policy{Err: nil}}, false -} - -type mockChannelPolicyManagerGetter2 struct { - managers map[string]policies.Manager -} - -func (c *mockChannelPolicyManagerGetter2) Manager(channelID string) (policies.Manager, bool) { - return c.managers[channelID], true -} - -type mockChannelPolicyManager struct { - mockPolicy policies.Policy -} - -func (m *mockChannelPolicyManager) GetPolicy(id string) (policies.Policy, bool) { - return m.mockPolicy, true -} - -func (m *mockChannelPolicyManager) Manager(path []string) (policies.Manager, bool) { - panic("Not implemented") -} - -func (m *mockChannelPolicyManager) BasePath() string { - panic("Not implemented") -} - -func (m *mockChannelPolicyManager) PolicyNames() []string { - panic("Not implemented") -} - -type mockPolicy struct { - deserializer msp.IdentityDeserializer -} - -// Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy -func (m *mockPolicy) Evaluate(signatureSet []*common.SignedData) error { - fmt.Printf("Evaluate [%s], [% x], [% x]\n", string(signatureSet[0].Identity), string(signatureSet[0].Data), string(signatureSet[0].Signature)) - identity, err := m.deserializer.DeserializeIdentity(signatureSet[0].Identity) - if err != nil { - return err - } - - return identity.Verify(signatureSet[0].Data, signatureSet[0].Signature) -} - -type mockDeserializersManager struct { - localDeserializer msp.IdentityDeserializer - channelDeserializers map[string]msp.IdentityDeserializer -} - -func (m *mockDeserializersManager) Deserialize(raw []byte) (*mspproto.SerializedIdentity, error) { - return &mspproto.SerializedIdentity{Mspid: "mock", IdBytes: raw}, nil -} - -func (m *mockDeserializersManager) GetLocalMSPIdentifier() string { - return "mock" -} - -func (m *mockDeserializersManager) GetLocalDeserializer() msp.IdentityDeserializer { - return m.localDeserializer -} - -func (m *mockDeserializersManager) GetChannelDeserializers() map[string]msp.IdentityDeserializer { - return m.channelDeserializers -} - -type mockIdentityDeserializer struct { - identity []byte - msg []byte -} - -func (d *mockIdentityDeserializer) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) { - fmt.Printf("id : [%s], [%s]\n", string(serializedIdentity), string(d.identity)) - if bytes.Equal(d.identity, serializedIdentity) { - fmt.Printf("GOT : [%s], [%s]\n", string(serializedIdentity), string(d.identity)) - return &mockIdentity{msg: d.msg}, nil - } - - return nil, errors.New("Invalid identity") -} - -type mockIdentity struct { - msg []byte -} - -func (id *mockIdentity) SatisfiesPrincipal(*mspproto.MSPPrincipal) error { - return nil -} - -func (id *mockIdentity) GetIdentifier() *msp.IdentityIdentifier { - return &msp.IdentityIdentifier{Mspid: "mock", Id: "mock"} -} - -func (id *mockIdentity) GetMSPIdentifier() string { - return "mock" -} - -func (id *mockIdentity) Validate() error { - return nil -} - -func (id *mockIdentity) GetOrganizationalUnits() []mspproto.FabricOUIdentifier { - return nil -} - -func (id *mockIdentity) Verify(msg []byte, sig []byte) error { - fmt.Printf("VERIFY [% x], [% x], [% x]\n", string(id.msg), string(msg), string(sig)) - if bytes.Equal(id.msg, msg) { - if bytes.Equal(msg, sig) { - return nil - } - } - - return errors.New("Invalid Signature") -} - -func (id *mockIdentity) VerifyOpts(msg []byte, sig []byte, opts msp.SignatureOpts) error { - return nil -} - -func (id *mockIdentity) VerifyAttributes(proof []byte, spec *msp.AttributeProofSpec) error { - return nil -} - -func (id *mockIdentity) Serialize() ([]byte, error) { - return []byte("cert"), nil -} diff --git a/peer/gossip/mcs/mcs_test.go b/peer/gossip/mcs_test.go similarity index 72% rename from peer/gossip/mcs/mcs_test.go rename to peer/gossip/mcs_test.go index c2cac152319..468c15fc8a0 100644 --- a/peer/gossip/mcs/mcs_test.go +++ b/peer/gossip/mcs_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mcs +package gossip import ( "fmt" @@ -33,6 +33,7 @@ import ( "github.com/hyperledger/fabric/gossip/api" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/msp/mgmt" + "github.com/hyperledger/fabric/peer/gossip/mocks" "github.com/hyperledger/fabric/protos/common" protospeer "github.com/hyperledger/fabric/protos/peer" "github.com/hyperledger/fabric/protos/utils" @@ -40,10 +41,10 @@ import ( ) func TestPKIidOfCert(t *testing.T) { - deserializersManager := &mockDeserializersManager{ - localDeserializer: &mockIdentityDeserializer{[]byte("Alice"), []byte("msg1")}, + deserializersManager := &mocks.DeserializersManager{ + LocalDeserializer: &mocks.IdentityDeserializer{[]byte("Alice"), []byte("msg1")}, } - msgCryptoService := New(&mockChannelPolicyManagerGetter2{}, + msgCryptoService := NewMCS(&mocks.ChannelPolicyManagerGetterWithManager{}, &mockscrypto.LocalSigner{Identity: []byte("Alice")}, deserializersManager, ) @@ -68,7 +69,7 @@ func TestPKIidOfCert(t *testing.T) { } func TestPKIidOfNil(t *testing.T) { - msgCryptoService := New(&MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) + msgCryptoService := NewMCS(&mocks.ChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) pkid := msgCryptoService.GetPKIidOfCert(nil) // Check pkid is not nil @@ -76,8 +77,8 @@ func TestPKIidOfNil(t *testing.T) { } func TestSign(t *testing.T) { - msgCryptoService := New( - &MockChannelPolicyManagerGetter{}, + msgCryptoService := NewMCS( + &mocks.ChannelPolicyManagerGetter{}, &mockscrypto.LocalSigner{Identity: []byte("Alice")}, mgmt.NewDeserializersManager(), ) @@ -89,21 +90,21 @@ func TestSign(t *testing.T) { } func TestVerify(t *testing.T) { - msgCryptoService := New( - &mockChannelPolicyManagerGetter2{ + msgCryptoService := NewMCS( + &mocks.ChannelPolicyManagerGetterWithManager{ map[string]policies.Manager{ - "A": &mockChannelPolicyManager{&mockPolicy{&mockIdentityDeserializer{[]byte("Bob"), []byte("msg2")}}}, - "B": &mockChannelPolicyManager{&mockPolicy{&mockIdentityDeserializer{[]byte("Charlie"), []byte("msg3")}}}, + "A": &mocks.ChannelPolicyManager{&mocks.Policy{&mocks.IdentityDeserializer{[]byte("Bob"), []byte("msg2")}}}, + "B": &mocks.ChannelPolicyManager{&mocks.Policy{&mocks.IdentityDeserializer{[]byte("Charlie"), []byte("msg3")}}}, "C": nil, }, }, &mockscrypto.LocalSigner{Identity: []byte("Alice")}, - &mockDeserializersManager{ - localDeserializer: &mockIdentityDeserializer{[]byte("Alice"), []byte("msg1")}, - channelDeserializers: map[string]msp.IdentityDeserializer{ - "A": &mockIdentityDeserializer{[]byte("Bob"), []byte("msg2")}, - "B": &mockIdentityDeserializer{[]byte("Charlie"), []byte("msg3")}, - "C": &mockIdentityDeserializer{[]byte("Dave"), []byte("msg4")}, + &mocks.DeserializersManager{ + LocalDeserializer: &mocks.IdentityDeserializer{[]byte("Alice"), []byte("msg1")}, + ChannelDeserializers: map[string]msp.IdentityDeserializer{ + "A": &mocks.IdentityDeserializer{[]byte("Bob"), []byte("msg2")}, + "B": &mocks.IdentityDeserializer{[]byte("Charlie"), []byte("msg3")}, + "C": &mocks.IdentityDeserializer{[]byte("Dave"), []byte("msg4")}, }, }, ) @@ -130,36 +131,36 @@ func TestVerify(t *testing.T) { func TestVerifyBlock(t *testing.T) { aliceSigner := &mockscrypto.LocalSigner{Identity: []byte("Alice")} - policyManagerGetter := &mockChannelPolicyManagerGetter2{ + policyManagerGetter := &mocks.ChannelPolicyManagerGetterWithManager{ map[string]policies.Manager{ - "A": &mockChannelPolicyManager{&mockPolicy{&mockIdentityDeserializer{[]byte("Bob"), []byte("msg2")}}}, - "B": &mockChannelPolicyManager{&mockPolicy{&mockIdentityDeserializer{[]byte("Charlie"), []byte("msg3")}}}, - "C": &mockChannelPolicyManager{&mockPolicy{&mockIdentityDeserializer{[]byte("Alice"), []byte("msg1")}}}, - "D": &mockChannelPolicyManager{&mockPolicy{&mockIdentityDeserializer{[]byte("Alice"), []byte("msg1")}}}, + "A": &mocks.ChannelPolicyManager{&mocks.Policy{&mocks.IdentityDeserializer{[]byte("Bob"), []byte("msg2")}}}, + "B": &mocks.ChannelPolicyManager{&mocks.Policy{&mocks.IdentityDeserializer{[]byte("Charlie"), []byte("msg3")}}}, + "C": &mocks.ChannelPolicyManager{&mocks.Policy{&mocks.IdentityDeserializer{[]byte("Alice"), []byte("msg1")}}}, + "D": &mocks.ChannelPolicyManager{&mocks.Policy{&mocks.IdentityDeserializer{[]byte("Alice"), []byte("msg1")}}}, }, } - msgCryptoService := New( + msgCryptoService := NewMCS( policyManagerGetter, aliceSigner, - &mockDeserializersManager{ - localDeserializer: &mockIdentityDeserializer{[]byte("Alice"), []byte("msg1")}, - channelDeserializers: map[string]msp.IdentityDeserializer{ - "A": &mockIdentityDeserializer{[]byte("Bob"), []byte("msg2")}, - "B": &mockIdentityDeserializer{[]byte("Charlie"), []byte("msg3")}, + &mocks.DeserializersManager{ + LocalDeserializer: &mocks.IdentityDeserializer{[]byte("Alice"), []byte("msg1")}, + ChannelDeserializers: map[string]msp.IdentityDeserializer{ + "A": &mocks.IdentityDeserializer{[]byte("Bob"), []byte("msg2")}, + "B": &mocks.IdentityDeserializer{[]byte("Charlie"), []byte("msg3")}, }, }, ) // - Prepare testing valid block, Alice signs it. blockRaw, msg := mockBlock(t, "C", aliceSigner, nil) - policyManagerGetter.managers["C"].(*mockChannelPolicyManager).mockPolicy.(*mockPolicy).deserializer.(*mockIdentityDeserializer).msg = msg + policyManagerGetter.Managers["C"].(*mocks.ChannelPolicyManager).Policy.(*mocks.Policy).Deserializer.(*mocks.IdentityDeserializer).Msg = msg blockRaw2, msg2 := mockBlock(t, "D", aliceSigner, nil) - policyManagerGetter.managers["D"].(*mockChannelPolicyManager).mockPolicy.(*mockPolicy).deserializer.(*mockIdentityDeserializer).msg = msg2 + policyManagerGetter.Managers["D"].(*mocks.ChannelPolicyManager).Policy.(*mocks.Policy).Deserializer.(*mocks.IdentityDeserializer).Msg = msg2 // - Verify block assert.NoError(t, msgCryptoService.VerifyBlock([]byte("C"), blockRaw)) - delete(policyManagerGetter.managers, "D") + delete(policyManagerGetter.Managers, "D") nilPolMgrErr := msgCryptoService.VerifyBlock([]byte("D"), blockRaw2) assert.Contains(t, fmt.Sprintf("%v", nilPolMgrErr), "Could not acquire policy manager") assert.Error(t, nilPolMgrErr) @@ -168,7 +169,7 @@ func TestVerifyBlock(t *testing.T) { // - Prepare testing invalid block (wrong data has), Alice signs it. blockRaw, msg = mockBlock(t, "C", aliceSigner, []byte{0}) - policyManagerGetter.managers["C"].(*mockChannelPolicyManager).mockPolicy.(*mockPolicy).deserializer.(*mockIdentityDeserializer).msg = msg + policyManagerGetter.Managers["C"].(*mocks.ChannelPolicyManager).Policy.(*mocks.Policy).Deserializer.(*mocks.IdentityDeserializer).Msg = msg // - Verify block assert.Error(t, msgCryptoService.VerifyBlock([]byte("C"), blockRaw)) diff --git a/peer/gossip/mocks/mocks.go b/peer/gossip/mocks/mocks.go new file mode 100644 index 00000000000..3124c5304db --- /dev/null +++ b/peer/gossip/mocks/mocks.go @@ -0,0 +1,163 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package mocks + +import ( + "bytes" + + "fmt" + + "errors" + + mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" + "github.com/hyperledger/fabric/common/policies" + "github.com/hyperledger/fabric/msp" + "github.com/hyperledger/fabric/protos/common" + mspproto "github.com/hyperledger/fabric/protos/msp" +) + +type ChannelPolicyManagerGetter struct{} + +func (c *ChannelPolicyManagerGetter) Manager(channelID string) (policies.Manager, bool) { + return &mockpolicies.Manager{Policy: &mockpolicies.Policy{Err: nil}}, false +} + +type ChannelPolicyManagerGetterWithManager struct { + Managers map[string]policies.Manager +} + +func (c *ChannelPolicyManagerGetterWithManager) Manager(channelID string) (policies.Manager, bool) { + return c.Managers[channelID], true +} + +type ChannelPolicyManager struct { + Policy policies.Policy +} + +func (m *ChannelPolicyManager) GetPolicy(id string) (policies.Policy, bool) { + return m.Policy, true +} + +func (m *ChannelPolicyManager) Manager(path []string) (policies.Manager, bool) { + panic("Not implemented") +} + +func (m *ChannelPolicyManager) BasePath() string { + panic("Not implemented") +} + +func (m *ChannelPolicyManager) PolicyNames() []string { + panic("Not implemented") +} + +type Policy struct { + Deserializer msp.IdentityDeserializer +} + +// Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy +func (m *Policy) Evaluate(signatureSet []*common.SignedData) error { + fmt.Printf("Evaluate [%s], [% x], [% x]\n", string(signatureSet[0].Identity), string(signatureSet[0].Data), string(signatureSet[0].Signature)) + identity, err := m.Deserializer.DeserializeIdentity(signatureSet[0].Identity) + if err != nil { + return err + } + + return identity.Verify(signatureSet[0].Data, signatureSet[0].Signature) +} + +type DeserializersManager struct { + LocalDeserializer msp.IdentityDeserializer + ChannelDeserializers map[string]msp.IdentityDeserializer +} + +func (m *DeserializersManager) Deserialize(raw []byte) (*mspproto.SerializedIdentity, error) { + return &mspproto.SerializedIdentity{Mspid: "mock", IdBytes: raw}, nil +} + +func (m *DeserializersManager) GetLocalMSPIdentifier() string { + return "mock" +} + +func (m *DeserializersManager) GetLocalDeserializer() msp.IdentityDeserializer { + return m.LocalDeserializer +} + +func (m *DeserializersManager) GetChannelDeserializers() map[string]msp.IdentityDeserializer { + return m.ChannelDeserializers +} + +type IdentityDeserializer struct { + Identity []byte + Msg []byte +} + +func (d *IdentityDeserializer) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) { + fmt.Printf("id : [%s], [%s]\n", string(serializedIdentity), string(d.Identity)) + if bytes.Equal(d.Identity, serializedIdentity) { + fmt.Printf("GOT : [%s], [%s]\n", string(serializedIdentity), string(d.Identity)) + return &Identity{Msg: d.Msg}, nil + } + + return nil, errors.New("Invalid Identity") +} + +type Identity struct { + Msg []byte +} + +func (id *Identity) SatisfiesPrincipal(*mspproto.MSPPrincipal) error { + return nil +} + +func (id *Identity) GetIdentifier() *msp.IdentityIdentifier { + return &msp.IdentityIdentifier{Mspid: "mock", Id: "mock"} +} + +func (id *Identity) GetMSPIdentifier() string { + return "mock" +} + +func (id *Identity) Validate() error { + return nil +} + +func (id *Identity) GetOrganizationalUnits() []mspproto.FabricOUIdentifier { + return nil +} + +func (id *Identity) Verify(msg []byte, sig []byte) error { + fmt.Printf("VERIFY [% x], [% x], [% x]\n", string(id.Msg), string(msg), string(sig)) + if bytes.Equal(id.Msg, msg) { + if bytes.Equal(msg, sig) { + return nil + } + } + + return errors.New("Invalid Signature") +} + +func (id *Identity) VerifyOpts(msg []byte, sig []byte, opts msp.SignatureOpts) error { + return nil +} + +func (id *Identity) VerifyAttributes(proof []byte, spec *msp.AttributeProofSpec) error { + return nil +} + +func (id *Identity) Serialize() ([]byte, error) { + return []byte("cert"), nil +} diff --git a/peer/gossip/sa/sa.go b/peer/gossip/sa.go similarity index 88% rename from peer/gossip/sa/sa.go rename to peer/gossip/sa.go index 8dad738a674..778466d63ce 100644 --- a/peer/gossip/sa/sa.go +++ b/peer/gossip/sa.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package sa +package gossip import ( "github.com/hyperledger/fabric/common/flogging" @@ -22,7 +22,7 @@ import ( "github.com/hyperledger/fabric/msp/mgmt" ) -var logger = flogging.MustGetLogger("peer/gossip/sa") +var saLogger = flogging.MustGetLogger("peer/gossip/sa") // mspSecurityAdvisor implements the SecurityAdvisor interface // using peer's MSPs. @@ -49,7 +49,7 @@ func NewSecurityAdvisor() api.SecurityAdvisor { func (advisor *mspSecurityAdvisor) OrgByPeerIdentity(peerIdentity api.PeerIdentityType) api.OrgIdentityType { // Validate arguments if len(peerIdentity) == 0 { - logger.Error("Invalid Peer Identity. It must be different from nil.") + saLogger.Error("Invalid Peer Identity. It must be different from nil.") return nil } @@ -74,14 +74,14 @@ func (advisor *mspSecurityAdvisor) OrgByPeerIdentity(peerIdentity api.PeerIdenti // Deserialize identity identity, err := mspManager.DeserializeIdentity([]byte(peerIdentity)) if err != nil { - logger.Debug("Failed deserialization identity [% x] on [%s]: [%s]", peerIdentity, chainID, err) + saLogger.Debug("Failed deserialization identity [% x] on [%s]: [%s]", peerIdentity, chainID, err) continue } return []byte(identity.GetMSPIdentifier()) } - logger.Warning("Peer Identity [% x] cannot be desirialized. No MSP found able to do that.", peerIdentity) + saLogger.Warning("Peer Identity [% x] cannot be desirialized. No MSP found able to do that.", peerIdentity) return nil } diff --git a/peer/gossip/sa/sa_test.go b/peer/gossip/sa_test.go similarity index 99% rename from peer/gossip/sa/sa_test.go rename to peer/gossip/sa_test.go index 6fe2f37b288..7fcb54910e0 100644 --- a/peer/gossip/sa/sa_test.go +++ b/peer/gossip/sa_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package sa +package gossip import ( "testing" diff --git a/peer/node/start.go b/peer/node/start.go index 827c301a290..b2c2a7e0461 100644 --- a/peer/node/start.go +++ b/peer/node/start.go @@ -45,7 +45,7 @@ import ( "github.com/hyperledger/fabric/gossip/service" "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/peer/common" - "github.com/hyperledger/fabric/peer/gossip/mcs" + peergossip "github.com/hyperledger/fabric/peer/gossip" cb "github.com/hyperledger/fabric/protos/common" pb "github.com/hyperledger/fabric/protos/peer" "github.com/spf13/cobra" @@ -157,7 +157,7 @@ func serve(args []string) error { logger.Panicf("Failed serializing self identity: %v", err) } - messageCryptoService := mcs.New( + messageCryptoService := peergossip.NewMCS( peer.NewChannelPolicyManagerGetter(), localmsp.NewSigner(), mgmt.NewDeserializersManager())