Skip to content

Commit 8eb5372

Browse files
authored
Merge pull request #539 from ellemouton/subserverSetup
multi: modularise subserver handling
2 parents f316bfe + f725c3a commit 8eb5372

File tree

11 files changed

+329
-291
lines changed

11 files changed

+329
-291
lines changed

config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,19 @@ func loadAndValidateConfig(interceptor signal.Interceptor) (*Config, error) {
328328
os.Exit(0)
329329
}
330330

331+
// Before we validate the config, we first hook up our own loggers.
332+
// This must be done before the config is validated if LND is running
333+
// in integrated mode so that the log levels for various non-LND related
334+
// subsystems can be set via the `lnd.debuglevel` flag.
335+
SetupLoggers(preCfg.Lnd.LogWriter, interceptor)
336+
331337
// Load the main configuration file and parse any command line options.
332338
// This function will also set up logging properly.
333339
cfg, err := loadConfigFile(preCfg, interceptor)
334340
if err != nil {
335341
return nil, err
336342
}
337343

338-
// With the validated config obtained, we now know that the root logging
339-
// system of lnd is initialized and we can hook up our own loggers now.
340-
SetupLoggers(cfg.Lnd.LogWriter, interceptor)
341-
342344
// Translate the more user friendly string modes into the more developer
343345
// friendly internal bool variables now.
344346
cfg.lndRemote = cfg.LndMode == ModeRemote

itest/litd_mode_integrated_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ import (
1818
"github.com/btcsuite/btcd/btcec/v2"
1919
"github.com/btcsuite/btcd/btcutil"
2020
"github.com/lightninglabs/faraday/frdrpc"
21+
faraday "github.com/lightninglabs/faraday/frdrpcserver/perms"
2122
"github.com/lightninglabs/lightning-node-connect/mailbox"
2223
terminal "github.com/lightninglabs/lightning-terminal"
2324
"github.com/lightninglabs/lightning-terminal/litrpc"
2425
"github.com/lightninglabs/lightning-terminal/perms"
2526
"github.com/lightninglabs/lightning-terminal/session"
27+
"github.com/lightninglabs/lightning-terminal/subservers"
28+
loop "github.com/lightninglabs/loop/loopd/perms"
2629
"github.com/lightninglabs/loop/looprpc"
30+
pool "github.com/lightninglabs/pool/perms"
2731
"github.com/lightninglabs/pool/poolrpc"
2832
"github.com/lightningnetwork/lnd/keychain"
2933
"github.com/lightningnetwork/lnd/lnrpc"
@@ -1090,6 +1094,12 @@ func bakeSuperMacaroon(cfg *LitNodeConfig, readOnly bool) (string, error) {
10901094
return "", err
10911095
}
10921096

1097+
permsMgr.RegisterSubServer(subservers.LOOP, loop.RequiredPermissions)
1098+
permsMgr.RegisterSubServer(subservers.POOL, pool.RequiredPermissions)
1099+
permsMgr.RegisterSubServer(
1100+
subservers.FARADAY, faraday.RequiredPermissions,
1101+
)
1102+
10931103
superMacPermissions := permsMgr.ActivePermissions(readOnly)
10941104
nullID := [4]byte{}
10951105
superMacHex, err := terminal.BakeSuperMacaroon(

perms/manager.go

Lines changed: 25 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
package perms
22

33
import (
4-
"fmt"
54
"regexp"
65
"strings"
76
"sync"
87

9-
faraday "github.com/lightninglabs/faraday/frdrpcserver/perms"
10-
loop "github.com/lightninglabs/loop/loopd/perms"
11-
pool "github.com/lightninglabs/pool/perms"
128
"github.com/lightningnetwork/lnd"
139
"github.com/lightningnetwork/lnd/lnrpc"
1410
"gopkg.in/macaroon-bakery.v2/bakery"
1511
)
1612

1713
const (
18-
poolPerms string = "pool"
19-
loopPerms string = "loop"
20-
faradayPerms string = "faraday"
21-
litPerms string = "lit"
22-
lndPerms string = "lnd"
14+
litPerms string = "lit"
15+
lndPerms string = "lnd"
2316
)
2417

2518
// Manager manages the permission lists that Lit requires.
@@ -54,9 +47,6 @@ type Manager struct {
5447
// was compiled with and then only the corresponding permissions will be added.
5548
func NewManager(withAllSubServers bool) (*Manager, error) {
5649
permissions := make(map[string]map[string][]bakery.Op)
57-
permissions[faradayPerms] = faraday.RequiredPermissions
58-
permissions[loopPerms] = loop.RequiredPermissions
59-
permissions[poolPerms] = pool.RequiredPermissions
6050
permissions[litPerms] = RequiredPermissions
6151
permissions[lndPerms] = lnd.MainRPCServerPermissions()
6252
for k, v := range whiteListedLNDMethods {
@@ -106,6 +96,21 @@ func NewManager(withAllSubServers bool) (*Manager, error) {
10696
}, nil
10797
}
10898

99+
// RegisterSubServer adds the permissions of a given sub-server to the set
100+
// managed by the Manager.
101+
func (pm *Manager) RegisterSubServer(name string,
102+
permissions map[string][]bakery.Op) {
103+
104+
pm.permsMu.Lock()
105+
defer pm.permsMu.Unlock()
106+
107+
pm.fixedPerms[name] = permissions
108+
109+
for uri, ops := range permissions {
110+
pm.perms[uri] = ops
111+
}
112+
}
113+
109114
// OnLNDBuildTags should be called once a list of LND build tags has been
110115
// obtained. It then uses those build tags to decide which of the LND sub-server
111116
// permissions to add to the main permissions list. This method should only
@@ -225,50 +230,19 @@ func (pm *Manager) ActivePermissions(readOnly bool) []bakery.Op {
225230
// _except_ for any LND permissions. In other words, this returns permissions
226231
// for which the external validator of Lit is responsible.
227232
func (pm *Manager) GetLitPerms() map[string][]bakery.Op {
228-
mapSize := len(pm.fixedPerms[litPerms]) +
229-
len(pm.fixedPerms[faradayPerms]) +
230-
len(pm.fixedPerms[loopPerms]) + len(pm.fixedPerms[poolPerms])
233+
result := make(map[string][]bakery.Op)
234+
for subserver, ops := range pm.fixedPerms {
235+
if subserver == lndPerms {
236+
continue
237+
}
231238

232-
result := make(map[string][]bakery.Op, mapSize)
233-
for key, value := range pm.fixedPerms[faradayPerms] {
234-
result[key] = value
235-
}
236-
for key, value := range pm.fixedPerms[loopPerms] {
237-
result[key] = value
238-
}
239-
for key, value := range pm.fixedPerms[poolPerms] {
240-
result[key] = value
241-
}
242-
for key, value := range pm.fixedPerms[litPerms] {
243-
result[key] = value
239+
for key, value := range ops {
240+
result[key] = value
241+
}
244242
}
245243
return result
246244
}
247245

248-
// SubServerHandler returns the name of the subserver that should handle the
249-
// given URI.
250-
func (pm *Manager) SubServerHandler(uri string) (string, error) {
251-
switch {
252-
case pm.IsSubServerURI(lndPerms, uri):
253-
return lndPerms, nil
254-
255-
case pm.IsSubServerURI(faradayPerms, uri):
256-
return faradayPerms, nil
257-
258-
case pm.IsSubServerURI(loopPerms, uri):
259-
return loopPerms, nil
260-
261-
case pm.IsSubServerURI(poolPerms, uri):
262-
return poolPerms, nil
263-
264-
case pm.IsSubServerURI(litPerms, uri):
265-
return litPerms, nil
266-
267-
default:
268-
return "", fmt.Errorf("unknown gRPC web request: %v", uri)
269-
}
270-
}
271-
272246
// IsSubServerURI if the given URI belongs to the RPC of the given server.
273247
func (pm *Manager) IsSubServerURI(name string, uri string) bool {
274248
if name == lndPerms {
@@ -292,27 +266,3 @@ func (pm *Manager) isLndURI(uri string) bool {
292266
_, lndCall := pm.fixedPerms[lndPerms][uri]
293267
return lndCall || lndSubServerCall
294268
}
295-
296-
// IsLoopURI returns true if the given URI belongs to an RPC of loopd.
297-
func (pm *Manager) IsLoopURI(uri string) bool {
298-
_, ok := pm.fixedPerms[loopPerms][uri]
299-
return ok
300-
}
301-
302-
// IsFaradayURI returns true if the given URI belongs to an RPC of faraday.
303-
func (pm *Manager) IsFaradayURI(uri string) bool {
304-
_, ok := pm.fixedPerms[faradayPerms][uri]
305-
return ok
306-
}
307-
308-
// IsPoolURI returns true if the given URI belongs to an RPC of poold.
309-
func (pm *Manager) IsPoolURI(uri string) bool {
310-
_, ok := pm.fixedPerms[poolPerms][uri]
311-
return ok
312-
}
313-
314-
// IsLitURI returns true if the given URI belongs to an RPC of LiT.
315-
func (pm *Manager) IsLitURI(uri string) bool {
316-
_, ok := pm.fixedPerms[litPerms][uri]
317-
return ok
318-
}

0 commit comments

Comments
 (0)