-
Notifications
You must be signed in to change notification settings - Fork 532
/
protocol.go
245 lines (196 loc) · 5.82 KB
/
protocol.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
package plugin
import (
"context"
"sync"
hplugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
v1 "github.com/ignite/cli/v29/ignite/services/plugin/grpc/v1"
)
var handshakeConfig = hplugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "IGNITE_APP",
MagicCookieValue: "ignite",
}
// HandshakeConfig are used to just do a basic handshake between a plugin and host.
// If the handshake fails, a user friendly error is shown. This prevents users from
// executing bad plugins or executing a plugin directory. It is a UX feature, not a
// security feature.
func HandshakeConfig() hplugin.HandshakeConfig {
return handshakeConfig
}
// NewGRPC returns a new gRPC plugin that implements the interface over gRPC.
func NewGRPC(impl Interface) hplugin.Plugin {
return grpcPlugin{impl: impl}
}
type grpcPlugin struct {
hplugin.NetRPCUnsupportedPlugin
impl Interface
}
// GRPCServer returns a new server that implements the plugin interface over gRPC.
func (p grpcPlugin) GRPCServer(broker *hplugin.GRPCBroker, s *grpc.Server) error {
v1.RegisterInterfaceServiceServer(s, &server{
impl: p.impl,
broker: broker,
})
return nil
}
// GRPCClient returns a new plugin client that allows calling the plugin interface over gRPC.
func (p grpcPlugin) GRPCClient(_ context.Context, broker *hplugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &client{
grpc: v1.NewInterfaceServiceClient(c),
broker: broker,
}, nil
}
type client struct {
grpc v1.InterfaceServiceClient
broker *hplugin.GRPCBroker
}
func (c client) Manifest(ctx context.Context) (*Manifest, error) {
r, err := c.grpc.Manifest(ctx, &v1.ManifestRequest{})
if err != nil {
return nil, err
}
return r.Manifest, nil
}
func (c client) Execute(ctx context.Context, cmd *ExecutedCommand, api ClientAPI) error {
brokerID, stopServer := c.startClientAPIServer(api)
_, err := c.grpc.Execute(ctx, &v1.ExecuteRequest{
Cmd: cmd,
ClientApi: brokerID,
})
stopServer()
return err
}
func (c client) ExecuteHookPre(ctx context.Context, h *ExecutedHook, api ClientAPI) error {
brokerID, stopServer := c.startClientAPIServer(api)
_, err := c.grpc.ExecuteHookPre(ctx, &v1.ExecuteHookPreRequest{
Hook: h,
ClientApi: brokerID,
})
stopServer()
return err
}
func (c client) ExecuteHookPost(ctx context.Context, h *ExecutedHook, api ClientAPI) error {
brokerID, stopServer := c.startClientAPIServer(api)
_, err := c.grpc.ExecuteHookPost(ctx, &v1.ExecuteHookPostRequest{
Hook: h,
ClientApi: brokerID,
})
stopServer()
return err
}
func (c client) ExecuteHookCleanUp(ctx context.Context, h *ExecutedHook, api ClientAPI) error {
brokerID, stopServer := c.startClientAPIServer(api)
_, err := c.grpc.ExecuteHookCleanUp(ctx, &v1.ExecuteHookCleanUpRequest{
Hook: h,
ClientApi: brokerID,
})
stopServer()
return err
}
func (c client) startClientAPIServer(api ClientAPI) (uint32, func()) {
var (
srv *grpc.Server
m sync.Mutex
brokerID = c.broker.NextId()
)
go c.broker.AcceptAndServe(brokerID, func(opts []grpc.ServerOption) *grpc.Server {
m.Lock()
defer m.Unlock()
srv = grpc.NewServer(opts...)
v1.RegisterClientAPIServiceServer(srv, &clientAPIServer{impl: api})
return srv
})
stop := func() {
m.Lock()
if srv != nil {
srv.Stop()
}
m.Unlock()
}
return brokerID, stop
}
type server struct {
v1.UnimplementedInterfaceServiceServer
impl Interface
broker *hplugin.GRPCBroker
}
func (s server) Manifest(ctx context.Context, _ *v1.ManifestRequest) (*v1.ManifestResponse, error) {
m, err := s.impl.Manifest(ctx)
if err != nil {
return nil, err
}
return &v1.ManifestResponse{Manifest: m}, nil
}
func (s server) Execute(ctx context.Context, r *v1.ExecuteRequest) (*v1.ExecuteResponse, error) {
conn, err := s.broker.Dial(r.ClientApi)
if err != nil {
return nil, err
}
defer conn.Close()
err = s.impl.Execute(ctx, r.GetCmd(), newClientAPIClient(conn))
if err != nil {
return nil, err
}
return &v1.ExecuteResponse{}, nil
}
func (s server) ExecuteHookPre(ctx context.Context, r *v1.ExecuteHookPreRequest) (*v1.ExecuteHookPreResponse, error) {
conn, err := s.broker.Dial(r.ClientApi)
if err != nil {
return nil, err
}
defer conn.Close()
err = s.impl.ExecuteHookPre(ctx, r.GetHook(), newClientAPIClient(conn))
if err != nil {
return nil, err
}
return &v1.ExecuteHookPreResponse{}, nil
}
func (s server) ExecuteHookPost(ctx context.Context, r *v1.ExecuteHookPostRequest) (*v1.ExecuteHookPostResponse, error) {
conn, err := s.broker.Dial(r.ClientApi)
if err != nil {
return nil, err
}
defer conn.Close()
err = s.impl.ExecuteHookPost(ctx, r.GetHook(), newClientAPIClient(conn))
if err != nil {
return nil, err
}
return &v1.ExecuteHookPostResponse{}, nil
}
func (s server) ExecuteHookCleanUp(ctx context.Context, r *v1.ExecuteHookCleanUpRequest) (*v1.ExecuteHookCleanUpResponse, error) {
conn, err := s.broker.Dial(r.ClientApi)
if err != nil {
return nil, err
}
defer conn.Close()
err = s.impl.ExecuteHookCleanUp(ctx, r.GetHook(), newClientAPIClient(conn))
if err != nil {
return nil, err
}
return &v1.ExecuteHookCleanUpResponse{}, nil
}
func newClientAPIClient(c *grpc.ClientConn) *clientAPIClient {
return &clientAPIClient{v1.NewClientAPIServiceClient(c)}
}
type clientAPIClient struct {
grpc v1.ClientAPIServiceClient
}
func (c clientAPIClient) GetChainInfo(ctx context.Context) (*ChainInfo, error) {
r, err := c.grpc.GetChainInfo(ctx, &v1.GetChainInfoRequest{})
if err != nil {
return nil, err
}
return r.ChainInfo, nil
}
type clientAPIServer struct {
v1.UnimplementedClientAPIServiceServer
impl ClientAPI
}
func (s clientAPIServer) GetChainInfo(ctx context.Context, _ *v1.GetChainInfoRequest) (*v1.GetChainInfoResponse, error) {
chainInfo, err := s.impl.GetChainInfo(ctx)
if err != nil {
return nil, err
}
return &v1.GetChainInfoResponse{ChainInfo: chainInfo}, nil
}