forked from tw-bc-group/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
293 lines (267 loc) · 9.44 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package discovery
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/comm"
common2 "github.com/hyperledger/fabric/gossip/common"
discovery2 "github.com/hyperledger/fabric/gossip/discovery"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/discovery"
"github.com/pkg/errors"
)
var (
logger = flogging.MustGetLogger("discovery")
)
var accessDenied = wrapError(errors.New("access denied"))
// certHashExtractor extracts the TLS certificate from a given context
// and returns its hash
type certHashExtractor func(ctx context.Context) []byte
// dispatcher defines a function that dispatches a query
type dispatcher func(q *discovery.Query) *discovery.QueryResult
type service struct {
config Config
channelDispatchers map[discovery.QueryType]dispatcher
localDispatchers map[discovery.QueryType]dispatcher
auth *authCache
Support
}
// Config defines the configuration of the discovery service
type Config struct {
TLS bool
AuthCacheEnabled bool
AuthCacheMaxSize int
AuthCachePurgeRetentionRatio float64
}
// String returns a string representation of this Config
func (c Config) String() string {
if c.AuthCacheEnabled {
return fmt.Sprintf("TLS: %t, authCacheMaxSize: %d, authCachePurgeRatio: %f", c.TLS, c.AuthCacheMaxSize, c.AuthCachePurgeRetentionRatio)
}
return fmt.Sprintf("TLS: %t, auth cache disabled", c.TLS)
}
// peerMapping maps PKI-IDs to Peers
type peerMapping map[string]*discovery.Peer
// NewService creates a new discovery service instance
func NewService(config Config, sup Support) *service {
s := &service{
auth: newAuthCache(sup, authCacheConfig{
enabled: config.AuthCacheEnabled,
maxCacheSize: config.AuthCacheMaxSize,
purgeRetentionRatio: config.AuthCachePurgeRetentionRatio,
}),
Support: sup,
}
s.channelDispatchers = map[discovery.QueryType]dispatcher{
discovery.ConfigQueryType: s.configQuery,
discovery.ChaincodeQueryType: s.chaincodeQuery,
discovery.PeerMembershipQueryType: s.channelMembershipResponse,
}
s.localDispatchers = map[discovery.QueryType]dispatcher{
discovery.LocalMembershipQueryType: s.localMembershipResponse,
}
logger.Info("Created with config", config)
return s
}
func (s *service) Discover(ctx context.Context, request *discovery.SignedRequest) (*discovery.Response, error) {
addr := util.ExtractRemoteAddress(ctx)
req, err := validateStructure(ctx, request, s.config.TLS, comm.ExtractCertificateHashFromContext)
if err != nil {
logger.Warningf("Request from %s is malformed or invalid: %v", addr, err)
return nil, err
}
logger.Debugf("Processing request from %s: %v", addr, req)
var res []*discovery.QueryResult
for _, q := range req.Queries {
res = append(res, s.processQuery(q, request, req.Authentication.ClientIdentity, addr))
}
logger.Debugf("Returning to %s a response containing: %v", addr, res)
return &discovery.Response{
Results: res,
}, nil
}
func (s *service) processQuery(query *discovery.Query, request *discovery.SignedRequest, identity []byte, addr string) *discovery.QueryResult {
if query.Channel != "" && !s.ChannelExists(query.Channel) {
logger.Warning("got query for channel", query.Channel, "from", addr, "but it doesn't exist")
return accessDenied
}
if err := s.auth.EligibleForService(query.Channel, common.SignedData{
Data: request.Payload,
Signature: request.Signature,
Identity: identity,
}); err != nil {
logger.Warning("got query for channel", query.Channel, "from", addr, "but it isn't eligible:", err)
return accessDenied
}
return s.dispatch(query)
}
func (s *service) dispatch(q *discovery.Query) *discovery.QueryResult {
dispatchers := s.channelDispatchers
// Ensure local queries are routed only to channel-less dispatchers
if q.Channel == "" {
dispatchers = s.localDispatchers
}
dispatchQuery, exists := dispatchers[q.GetType()]
if !exists {
return wrapError(errors.New("unknown or missing request type"))
}
return dispatchQuery(q)
}
func (s *service) chaincodeQuery(q *discovery.Query) *discovery.QueryResult {
if err := validateCCQuery(q.GetCcQuery()); err != nil {
return wrapError(err)
}
var descriptors []*discovery.EndorsementDescriptor
for _, interest := range q.GetCcQuery().Interests {
desc, err := s.PeersForEndorsement(common2.ChainID(q.Channel), interest)
if err != nil {
logger.Errorf("Failed constructing descriptor for chaincode %s,: %v", interest, err)
return wrapError(errors.Errorf("failed constructing descriptor for %v", interest))
}
descriptors = append(descriptors, desc)
}
return &discovery.QueryResult{
Result: &discovery.QueryResult_CcQueryRes{
CcQueryRes: &discovery.ChaincodeQueryResult{
Content: descriptors,
},
},
}
}
func (s *service) configQuery(q *discovery.Query) *discovery.QueryResult {
conf, err := s.Config(q.Channel)
if err != nil {
logger.Errorf("Failed fetching config for channel %s: %v", q.Channel, err)
return wrapError(errors.Errorf("failed fetching config for channel %s", q.Channel))
}
return &discovery.QueryResult{
Result: &discovery.QueryResult_ConfigResult{
ConfigResult: conf,
},
}
}
func wrapPeerResponse(peersByOrg map[string]*discovery.Peers) *discovery.QueryResult {
return &discovery.QueryResult{
Result: &discovery.QueryResult_Members{
Members: &discovery.PeerMembershipResult{
PeersByOrg: peersByOrg,
},
},
}
}
func (s *service) channelMembershipResponse(q *discovery.Query) *discovery.QueryResult {
chanPeers, err := s.PeersAuthorizedByCriteria(common2.ChainID(q.Channel), q.GetPeerQuery().Filter)
if err != nil {
return wrapError(err)
}
membersByOrgs := make(map[string]*discovery.Peers)
chanPeerByID := discovery2.Members(chanPeers).ByID()
for org, ids2Peers := range s.computeMembership(q) {
membersByOrgs[org] = &discovery.Peers{}
for id, peer := range ids2Peers {
// Check if the peer is in the channel view
stateInfoMsg, exists := chanPeerByID[string(id)]
// If the peer isn't in the channel view, skip it and don't include it in the response
if !exists {
continue
}
peer.StateInfo = stateInfoMsg.Envelope
membersByOrgs[org].Peers = append(membersByOrgs[org].Peers, peer)
}
}
return wrapPeerResponse(membersByOrgs)
}
func (s *service) localMembershipResponse(q *discovery.Query) *discovery.QueryResult {
membersByOrgs := make(map[string]*discovery.Peers)
for org, ids2Peers := range s.computeMembership(q) {
membersByOrgs[org] = &discovery.Peers{}
for _, peer := range ids2Peers {
membersByOrgs[org].Peers = append(membersByOrgs[org].Peers, peer)
}
}
return wrapPeerResponse(membersByOrgs)
}
func (s *service) computeMembership(_ *discovery.Query) map[string]peerMapping {
peersByOrg := make(map[string]peerMapping)
peerAliveInfo := discovery2.Members(s.Peers()).ByID()
for org, peerIdentities := range s.IdentityInfo().ByOrg() {
peersForCurrentOrg := make(peerMapping)
peersByOrg[org] = peersForCurrentOrg
for _, id := range peerIdentities {
// Check peer exists in alive membership view
aliveInfo, exists := peerAliveInfo[string(id.PKIId)]
if !exists {
continue
}
peersForCurrentOrg[string(id.PKIId)] = &discovery.Peer{
Identity: id.Identity,
MembershipInfo: aliveInfo.Envelope,
}
}
}
return peersByOrg
}
// validateStructure validates that the request contains all the needed fields and that they are computed correctly
func validateStructure(ctx context.Context, request *discovery.SignedRequest, tlsEnabled bool, certHashFromContext certHashExtractor) (*discovery.Request, error) {
if request == nil {
return nil, errors.New("nil request")
}
req, err := request.ToRequest()
if err != nil {
return nil, errors.Wrap(err, "failed parsing request")
}
if req.Authentication == nil {
return nil, errors.New("access denied, no authentication info in request")
}
if len(req.Authentication.ClientIdentity) == 0 {
return nil, errors.New("access denied, client identity wasn't supplied")
}
if !tlsEnabled {
return req, nil
}
computedHash := certHashFromContext(ctx)
if len(computedHash) == 0 {
return nil, errors.New("client didn't send a TLS certificate")
}
if !bytes.Equal(computedHash, req.Authentication.ClientTlsCertHash) {
claimed := hex.EncodeToString(req.Authentication.ClientTlsCertHash)
logger.Warningf("client claimed TLS hash %s doesn't match computed TLS hash from gRPC stream %s", claimed, hex.EncodeToString(computedHash))
return nil, errors.New("client claimed TLS hash doesn't match computed TLS hash from gRPC stream")
}
return req, nil
}
func validateCCQuery(ccQuery *discovery.ChaincodeQuery) error {
if len(ccQuery.Interests) == 0 {
return errors.New("chaincode query must have at least one chaincode interest")
}
for _, interest := range ccQuery.Interests {
if interest == nil {
return errors.New("chaincode interest is nil")
}
if len(interest.Chaincodes) == 0 {
return errors.New("chaincode interest must contain at least one chaincode")
}
for _, cc := range interest.Chaincodes {
if cc.Name == "" {
return errors.New("chaincode name in interest cannot be empty")
}
}
}
return nil
}
func wrapError(err error) *discovery.QueryResult {
return &discovery.QueryResult{
Result: &discovery.QueryResult_Error{
Error: &discovery.Error{
Content: err.Error(),
},
},
}
}