/
stub.go
135 lines (110 loc) · 3.53 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
124
125
126
127
128
129
130
131
132
133
134
135
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package discovery
import (
"context"
"github.com/hyperledger/fabric-protos-go/discovery"
"github.com/hyperledger/fabric/cmd/common"
"github.com/hyperledger/fabric/cmd/common/comm"
"github.com/hyperledger/fabric/cmd/common/signer"
discoveryclient "github.com/hyperledger/fabric/discovery/client"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
//go:generate mockery -dir . -name LocalResponse -case underscore -output mocks/
// LocalResponse is the local interface used to generate mocks for foreign interface.
type LocalResponse interface {
discoveryclient.LocalResponse
}
//go:generate mockery -dir . -name ChannelResponse -case underscore -output mocks/
// ChannelResponse is the local interface used to generate mocks for foreign interface.
type ChannelResponse interface {
discoveryclient.ChannelResponse
}
//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) discoveryclient.ChannelResponse
// ForLocal returns a LocalResponse in the context of no channel
ForLocal() discoveryclient.LocalResponse
// Raw returns the raw response from the server
Raw() *discovery.Response
}
type response struct {
raw *discovery.Response
discoveryclient.Response
}
func (r *response) Raw() *discovery.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 *discoveryclient.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 := discoveryclient.NewClient(comm.NewDialer(server), signer.Sign, 0)
resp, err := disc.Send(timeout, req, &discovery.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 *discoveryclient.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 = &discovery.AuthInfo{
ClientIdentity: signer.Creator,
ClientTlsCertHash: comm.TLSCertHash,
}
payload := protoutil.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 := discovery.NewDiscoveryClient(cc).Discover(timeout, &discovery.SignedRequest{
Payload: payload,
Signature: sig,
})
if err != nil {
return nil, err
}
return &response{
raw: resp,
}, nil
}