|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "strconv" |
| 23 | + "strings" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/hyperledger/fabric/gossip/comm" |
| 27 | + "github.com/hyperledger/fabric/gossip/gossip" |
| 28 | + "github.com/hyperledger/fabric/gossip/proto" |
| 29 | + "google.golang.org/grpc" |
| 30 | +) |
| 31 | + |
| 32 | +// This file is used to bootstrap a gossip instance for integration/demo purposes ONLY |
| 33 | + |
| 34 | +func newConfig(selfEndpoint string, bootPeers ...string) *gossip.Config { |
| 35 | + port, err := strconv.ParseInt(strings.Split(selfEndpoint, ":")[1], 10, 64) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + return &gossip.Config{ |
| 40 | + BindPort: int(port), |
| 41 | + BootstrapPeers: bootPeers, |
| 42 | + ID: selfEndpoint, |
| 43 | + MaxMessageCountToStore: 100, |
| 44 | + MaxPropagationBurstLatency: time.Millisecond * 50, |
| 45 | + MaxPropagationBurstSize: 3, |
| 46 | + PropagateIterations: 1, |
| 47 | + PropagatePeerNum: 3, |
| 48 | + PullInterval: time.Second * 5, |
| 49 | + PullPeerNum: 3, |
| 50 | + SelfEndpoint: selfEndpoint, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func newComm(selfEndpoint string, s *grpc.Server, dialOpts ...grpc.DialOption) comm.Comm { |
| 55 | + comm, err := comm.NewCommInstance(s, NewGossipCryptoService(), []byte(selfEndpoint), dialOpts...) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + return comm |
| 60 | +} |
| 61 | + |
| 62 | +// NewGossipComponent creates a gossip component that attaches itself to the given gRPC server |
| 63 | +func NewGossipComponent(endpoint string, s *grpc.Server, bootPeers ...string) (gossip.Gossip, comm.Comm) { |
| 64 | + conf := newConfig(endpoint, bootPeers...) |
| 65 | + comm := newComm(endpoint, s, grpc.WithInsecure()) |
| 66 | + return gossip.NewGossipService(conf, comm, NewGossipCryptoService()), comm |
| 67 | +} |
| 68 | + |
| 69 | +// GossipCryptoService is an interface that conforms to both |
| 70 | +// the comm.SecurityProvider and to discovery.CryptoService |
| 71 | +type GossipCryptoService interface { |
| 72 | + |
| 73 | + // isEnabled returns whether authentication is enabled |
| 74 | + IsEnabled() bool |
| 75 | + |
| 76 | + // Sign signs msg with this peers signing key and outputs |
| 77 | + // the signature if no error occurred. |
| 78 | + Sign(msg []byte) ([]byte, error) |
| 79 | + |
| 80 | + // Verify checks that signature if a valid signature of message under vkID's verification key. |
| 81 | + // If the verification succeeded, Verify returns nil meaning no error occurred. |
| 82 | + // If vkID is nil, then the signature is verified against this validator's verification key. |
| 83 | + Verify(vkID, signature, message []byte) error |
| 84 | + |
| 85 | + // validateAliveMsg validates that an Alive message is authentic |
| 86 | + ValidateAliveMsg(*proto.AliveMessage) bool |
| 87 | + |
| 88 | + // SignMessage signs an AliveMessage and updates its signature field |
| 89 | + SignMessage(*proto.AliveMessage) *proto.AliveMessage |
| 90 | +} |
| 91 | + |
| 92 | +// NewGossipCryptoService returns an instance that implements naively every security |
| 93 | +// interface that the gossip layer needs |
| 94 | +func NewGossipCryptoService() GossipCryptoService { |
| 95 | + return &naiveCryptoServiceImpl{} |
| 96 | +} |
| 97 | + |
| 98 | +type naiveCryptoServiceImpl struct { |
| 99 | +} |
| 100 | + |
| 101 | +func (cs *naiveCryptoServiceImpl) ValidateAliveMsg(*proto.AliveMessage) bool { |
| 102 | + return true |
| 103 | +} |
| 104 | + |
| 105 | +// SignMessage signs an AliveMessage and updates its signature field |
| 106 | +func (cs *naiveCryptoServiceImpl) SignMessage(msg *proto.AliveMessage) *proto.AliveMessage { |
| 107 | + return msg |
| 108 | +} |
| 109 | + |
| 110 | +// IsEnabled returns true whether authentication is enabled |
| 111 | +func (cs *naiveCryptoServiceImpl) IsEnabled() bool { |
| 112 | + return false |
| 113 | +} |
| 114 | + |
| 115 | +// Sign signs a message with the local peer's private key |
| 116 | +func (cs *naiveCryptoServiceImpl) Sign(msg []byte) ([]byte, error) { |
| 117 | + return msg, nil |
| 118 | +} |
| 119 | + |
| 120 | +// Verify verifies a signature on a message that came from a peer with a certain vkID |
| 121 | +func (cs *naiveCryptoServiceImpl) Verify(vkID, signature, message []byte) error { |
| 122 | + if !bytes.Equal(signature, message) { |
| 123 | + return fmt.Errorf("Invalid signature!") |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
0 commit comments