-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
stub.go
123 lines (102 loc) · 3.13 KB
/
stub.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package discovery
import (
"context"
"github.com/hyperledger/fabric/cmd/common"
"github.com/hyperledger/fabric/cmd/common/comm"
"github.com/hyperledger/fabric/cmd/common/signer"
discovery "github.com/hyperledger/fabric/discovery/client"
. "github.com/hyperledger/fabric/protos/discovery"
"github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
)
//go:generate mockery -dir ../client/ -name LocalResponse -case underscore -output mocks/
//go:generate mockery -dir ../client/ -name ChannelResponse -case underscore -output mocks/
//go:generate mockery -dir . -name ServiceResponse -case underscore -output mocks/
// ServiceResponse represents a response sent from the discovery service
type ServiceResponse interface {
// ForChannel returns a ChannelResponse in the context of a given channel
ForChannel(string) discovery.ChannelResponse
// ForLocal returns a LocalResponse in the context of no channel
ForLocal() discovery.LocalResponse
// Raw returns the raw response from the server
Raw() *Response
}
type response struct {
raw *Response
discovery.Response
}
func (r *response) Raw() *Response {
return r.raw
}
// ClientStub is a stub that communicates with the discovery service
// using the discovery client implementation
type ClientStub struct {
}
// Send sends the request, and receives a response
func (stub *ClientStub) Send(server string, conf common.Config, req *discovery.Request) (ServiceResponse, error) {
comm, err := comm.NewClient(conf.TLSConfig)
if err != nil {
return nil, err
}
signer, err := signer.NewSigner(conf.SignerConfig)
if err != nil {
return nil, err
}
timeout, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
disc := discovery.NewClient(comm.NewDialer(server), signer.Sign, 0)
resp, err := disc.Send(timeout, req, &AuthInfo{
ClientIdentity: signer.Creator,
ClientTlsCertHash: comm.TLSCertHash,
})
if err != nil {
return nil, errors.Errorf("failed connecting to %s: %v", server, err)
}
return &response{
Response: resp,
}, nil
}
// RawStub is a stub that communicates with the discovery service
// without any intermediary.
type RawStub struct {
}
// Send sends the request, and receives a response
func (stub *RawStub) Send(server string, conf common.Config, req *discovery.Request) (ServiceResponse, error) {
comm, err := comm.NewClient(conf.TLSConfig)
if err != nil {
return nil, err
}
signer, err := signer.NewSigner(conf.SignerConfig)
if err != nil {
return nil, err
}
timeout, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
req.Authentication = &AuthInfo{
ClientIdentity: signer.Creator,
ClientTlsCertHash: comm.TLSCertHash,
}
payload := utils.MarshalOrPanic(req.Request)
sig, err := signer.Sign(payload)
if err != nil {
return nil, err
}
cc, err := comm.NewDialer(server)()
if err != nil {
return nil, err
}
resp, err := NewDiscoveryClient(cc).Discover(timeout, &SignedRequest{
Payload: payload,
Signature: sig,
})
if err != nil {
return nil, err
}
return &response{
raw: resp,
}, nil
}