/
manager.go
388 lines (317 loc) · 9.83 KB
/
manager.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package subservers
import (
"context"
"fmt"
"io/ioutil"
"sync"
"time"
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/lightninglabs/lightning-terminal/perms"
"github.com/lightninglabs/lightning-terminal/status"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc"
grpcProxy "github.com/mwitkow/grpc-proxy/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon.v2"
)
var (
// maxMsgRecvSize is the largest message our REST proxy will receive. We
// set this to 200MiB atm.
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
// defaultConnectTimeout is the default timeout for connecting to the
// backend.
defaultConnectTimeout = 15 * time.Second
)
// Manager manages a set of subServer objects.
type Manager struct {
servers []*subServerWrapper
permsMgr *perms.Manager
statusServer *status.Manager
mu sync.RWMutex
}
// NewManager constructs a new Manager.
func NewManager(permsMgr *perms.Manager,
statusServer *status.Manager) *Manager {
return &Manager{
permsMgr: permsMgr,
statusServer: statusServer,
}
}
// AddServer adds a new subServer to the manager's set.
func (s *Manager) AddServer(ss SubServer, enable bool) {
// Register all sub-servers with the status server.
s.statusServer.RegisterSubServer(ss.Name())
// If the sub-server has explicitly been disabled, then we don't add it
// to the set of servers tracked by the Manager.
if !enable {
return
}
s.mu.Lock()
defer s.mu.Unlock()
// Add the enabled server to the set of servers tracked by the Manager.
s.servers = append(s.servers, &subServerWrapper{
SubServer: ss,
quit: make(chan struct{}),
})
// Register the sub-server's permissions with the permission manager.
s.permsMgr.RegisterSubServer(
ss.Name(), ss.Permissions(), ss.WhiteListedURLs(),
)
// Mark the sub-server as enabled with the status manager.
s.statusServer.SetEnabled(ss.Name())
}
// StartIntegratedServers starts all the manager's sub-servers that should be
// started in integrated mode.
func (s *Manager) StartIntegratedServers(lndClient lnrpc.LightningClient,
lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool) {
s.mu.Lock()
defer s.mu.Unlock()
for _, ss := range s.servers {
if ss.Remote() {
continue
}
err := ss.startIntegrated(
lndClient, lndGrpc, withMacaroonService,
func(err error) {
s.statusServer.SetErrored(
ss.Name(), err.Error(),
)
},
)
if err != nil {
s.statusServer.SetErrored(ss.Name(), err.Error())
continue
}
s.statusServer.SetRunning(ss.Name())
}
}
// ConnectRemoteSubServers creates connections to all the manager's sub-servers
// that are running remotely.
func (s *Manager) ConnectRemoteSubServers() {
s.mu.Lock()
defer s.mu.Unlock()
for _, ss := range s.servers {
if !ss.Remote() {
continue
}
err := ss.connectRemote()
if err != nil {
s.statusServer.SetErrored(ss.Name(), err.Error())
continue
}
s.statusServer.SetRunning(ss.Name())
}
}
// RegisterRPCServices registers all the manager's sub-servers with the given
// grpc registrar.
func (s *Manager) RegisterRPCServices(server grpc.ServiceRegistrar) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
// In remote mode the "director" of the RPC proxy will act as
// a catch-all for any gRPC request that isn't known because we
// didn't register any server for it. The director will then
// forward the request to the remote service.
if ss.Remote() {
continue
}
ss.RegisterGrpcService(server)
}
}
// RegisterRestServices registers all the manager's sub-servers REST handlers
// with the given endpoint.
func (s *Manager) RegisterRestServices(ctx context.Context,
mux *restProxy.ServeMux, endpoint string,
dialOpts []grpc.DialOption) error {
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
// In remote mode the "director" of the RPC proxy will act as
// a catch-all for any gRPC request that isn't known because we
// didn't register any server for it. The director will then
// forward the request to the remote service.
if ss.Remote() {
continue
}
err := ss.RegisterRestService(ctx, mux, endpoint, dialOpts)
if err != nil {
return err
}
}
return nil
}
// GetRemoteConn checks if any of the manager's sub-servers owns the given uri
// and if so, the remote connection to that sub-server is returned. The bool
// return value indicates if the uri is managed by one of the sub-servers
// running in remote mode.
func (s *Manager) GetRemoteConn(uri string) (bool, *grpc.ClientConn, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
continue
}
if !ss.Remote() {
return false, nil, nil
}
if ss.remoteConn == nil {
return true, nil, fmt.Errorf("not yet connected to "+
"remote sub-server(%s)", ss.Name())
}
return true, ss.remoteConn, nil
}
return false, nil, nil
}
// ValidateMacaroon checks if any of the manager's sub-servers owns the given
// uri and if so, if it is running in remote mode, then true is returned since
// the macaroon will be validated by the remote subserver itself when the
// request arrives. Otherwise, the integrated sub-server's validator validates
// the macaroon.
func (s *Manager) ValidateMacaroon(ctx context.Context,
requiredPermissions []bakery.Op, uri string) (bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
continue
}
// If the sub-server is running in remote mode, then we don't
// need to validate the macaroon here since the remote server
// will do it when the request arrives. But we have handled the
// request, as we were able to identify it.
if ss.Remote() {
return true, nil
}
// If the sub-server hasn't started yet, then we can't validate
// the macaroon. But we know that we can handle the request, as
// we were able to identify it.
if !ss.started() {
return true, fmt.Errorf("%s is not yet ready for "+
"requests, the subserver has not started or "+
"lnd still starting/syncing",
ss.Name())
}
// Validate the macaroon with the integrated sub-server's own
// validator.
err := ss.ValidateMacaroon(ctx, requiredPermissions, uri)
if err != nil {
return true, fmt.Errorf("invalid macaroon: %v", err)
}
// The macaroon is valid for this sub-server, we can return
// early.
return true, nil
}
// No sub-server owns the given uri, so we haven't handled this call.
return false, nil
}
// MacaroonPath checks if any of the manager's sub-servers owns the given uri
// and if so, the appropriate macaroon path is returned for that sub-server.
func (s *Manager) MacaroonPath(uri string) (bool, string) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
continue
}
if ss.Remote() {
return true, ss.RemoteConfig().MacaroonPath
}
return true, ss.MacPath()
}
return false, ""
}
// ReadRemoteMacaroon checks if any of the manager's sub-servers running in
// remote mode owns the given uri and if so, the appropriate macaroon path is
// returned for that sub-server.
func (s *Manager) ReadRemoteMacaroon(uri string) (bool, []byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
continue
}
if !ss.Remote() {
return false, nil, nil
}
macBytes, err := readMacaroon(lncfg.CleanAndExpandPath(
ss.RemoteConfig().MacaroonPath,
))
return true, macBytes, err
}
return false, nil, nil
}
// Handles returns true if one of its sub-servers owns the given URI along with
// the name of the service.
func (s *Manager) Handles(uri string) (bool, string) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
continue
}
return true, ss.Name()
}
return false, ""
}
// Stop stops all the manager's sub-servers
func (s *Manager) Stop() error {
var returnErr error
s.mu.RLock()
defer s.mu.RUnlock()
for _, ss := range s.servers {
if ss.Remote() {
continue
}
err := ss.stop()
if err != nil {
log.Errorf("Error stopping %s: %v", ss.Name(), err)
returnErr = err
}
s.statusServer.SetStopped(ss.Name())
}
return returnErr
}
func dialBackend(name, dialAddr, tlsCertPath string) (*grpc.ClientConn, error) {
tlsConfig, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
if err != nil {
return nil, fmt.Errorf("could not read %s TLS cert %s: %v",
name, tlsCertPath, err)
}
opts := []grpc.DialOption{
// From the grpcProxy doc: This codec is *crucial* to the
// functioning of the proxy.
grpc.WithCodec(grpcProxy.Codec()), // nolint
grpc.WithTransportCredentials(tlsConfig),
grpc.WithDefaultCallOptions(maxMsgRecvSize),
grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.DefaultConfig,
MinConnectTimeout: defaultConnectTimeout,
}),
}
log.Infof("Dialing %s gRPC server at %s", name, dialAddr)
cc, err := grpc.Dial(dialAddr, opts...)
if err != nil {
return nil, fmt.Errorf("failed dialing %s backend: %v", name,
err)
}
return cc, nil
}
// readMacaroon tries to read the macaroon file at the specified path.
func readMacaroon(macPath string) ([]byte, error) {
// Load the specified macaroon file.
macBytes, err := ioutil.ReadFile(macPath)
if err != nil {
return nil, fmt.Errorf("unable to read macaroon path: %v", err)
}
// Make sure it actually is a macaroon by parsing it.
mac := &macaroon.Macaroon{}
if err := mac.UnmarshalBinary(macBytes); err != nil {
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
}
// It's a macaroon alright, let's return the binary data now.
return macBytes, nil
}