Skip to content

Commit c3bc991

Browse files
committed
subservers: add uri handler helper funcs
1 parent f2fe3cc commit c3bc991

File tree

3 files changed

+88
-33
lines changed

3 files changed

+88
-33
lines changed

perms/manager.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package perms
22

33
import (
4+
"fmt"
45
"regexp"
56
"strings"
67
"sync"
@@ -244,8 +245,42 @@ func (pm *Manager) GetLitPerms() map[string][]bakery.Op {
244245
return result
245246
}
246247

247-
// IsLndURI returns true if the given URI belongs to an RPC of lnd.
248-
func (pm *Manager) IsLndURI(uri string) bool {
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+
272+
// IsSubServerURI if the given URI belongs to the RPC of the given server.
273+
func (pm *Manager) IsSubServerURI(name string, uri string) bool {
274+
if name == lndPerms {
275+
return pm.isLndURI(uri)
276+
}
277+
278+
_, ok := pm.fixedPerms[name][uri]
279+
return ok
280+
}
281+
282+
// isLndURI returns true if the given URI belongs to an RPC of lnd.
283+
func (pm *Manager) isLndURI(uri string) bool {
249284
var lndSubServerCall bool
250285
for _, subserverPermissions := range pm.lndSubServerPerms {
251286
_, found := subserverPermissions[uri]

rpc_proxy.go

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/lightninglabs/lightning-terminal/litrpc"
1616
"github.com/lightninglabs/lightning-terminal/perms"
1717
"github.com/lightninglabs/lightning-terminal/session"
18+
"github.com/lightninglabs/lightning-terminal/subservers"
1819
"github.com/lightningnetwork/lnd/lncfg"
1920
"github.com/lightningnetwork/lnd/macaroons"
2021
grpcProxy "github.com/mwitkow/grpc-proxy/proxy"
@@ -361,18 +362,30 @@ func (p *rpcProxy) makeDirector(allowLitRPC bool) func(ctx context.Context,
361362
// since it must either be an lnd call or something that'll be
362363
// handled by the integrated daemons that are hooking into lnd's
363364
// gRPC server.
365+
isFaraday := p.permsMgr.IsSubServerURI(
366+
subservers.FARADAY, requestURI,
367+
)
368+
isLoop := p.permsMgr.IsSubServerURI(
369+
subservers.LOOP, requestURI,
370+
)
371+
isPool := p.permsMgr.IsSubServerURI(
372+
subservers.POOL, requestURI,
373+
)
374+
isLit := p.permsMgr.IsSubServerURI(
375+
subservers.LIT, requestURI,
376+
)
364377
switch {
365-
case p.permsMgr.IsFaradayURI(requestURI) && p.cfg.faradayRemote:
378+
case isFaraday && p.cfg.faradayRemote:
366379
return outCtx, p.faradayConn, nil
367380

368-
case p.permsMgr.IsLoopURI(requestURI) && p.cfg.loopRemote:
381+
case isLoop && p.cfg.loopRemote:
369382
return outCtx, p.loopConn, nil
370383

371-
case p.permsMgr.IsPoolURI(requestURI) && p.cfg.poolRemote:
384+
case isPool && p.cfg.poolRemote:
372385
return outCtx, p.poolConn, nil
373386

374387
// Calls to LiT session RPC aren't allowed in some cases.
375-
case p.permsMgr.IsLitURI(requestURI) && !allowLitRPC:
388+
case isLit && !allowLitRPC:
376389
return outCtx, nil, status.Errorf(
377390
codes.Unimplemented, "unknown service %s",
378391
requestURI,
@@ -533,37 +546,42 @@ func (p *rpcProxy) basicAuthToMacaroon(basicAuth, requestURI string,
533546
macPath string
534547
macData []byte
535548
)
536-
switch {
537-
case p.permsMgr.IsLndURI(requestURI):
549+
subserver, err := p.permsMgr.SubServerHandler(requestURI)
550+
if err != nil {
551+
return nil, err
552+
}
553+
554+
switch subserver {
555+
case subservers.LND:
538556
_, _, _, macPath, macData = p.cfg.lndConnectParams()
539557

540-
case p.permsMgr.IsFaradayURI(requestURI):
558+
case subservers.FARADAY:
541559
if p.cfg.faradayRemote {
542560
macPath = p.cfg.Remote.Faraday.MacaroonPath
543561
} else {
544562
macPath = p.cfg.Faraday.MacaroonPath
545563
}
546564

547-
case p.permsMgr.IsLoopURI(requestURI):
565+
case subservers.LOOP:
548566
if p.cfg.loopRemote {
549567
macPath = p.cfg.Remote.Loop.MacaroonPath
550568
} else {
551569
macPath = p.cfg.Loop.MacaroonPath
552570
}
553571

554-
case p.permsMgr.IsPoolURI(requestURI):
572+
case subservers.POOL:
555573
if p.cfg.poolRemote {
556574
macPath = p.cfg.Remote.Pool.MacaroonPath
557575
} else {
558576
macPath = p.cfg.Pool.MacaroonPath
559577
}
560578

561-
case p.permsMgr.IsLitURI(requestURI):
579+
case subservers.LIT:
562580
macPath = p.cfg.MacaroonPath
563581

564582
default:
565-
return nil, fmt.Errorf("unknown gRPC web request: %v",
566-
requestURI)
583+
return nil, fmt.Errorf("unknown subserver handler: %v",
584+
subserver)
567585
}
568586

569587
switch {
@@ -635,21 +653,22 @@ func (p *rpcProxy) convertSuperMacaroon(ctx context.Context, macHex string,
635653

636654
// Is this actually a request that goes to a daemon that is running
637655
// remotely?
638-
switch {
639-
case p.permsMgr.IsFaradayURI(fullMethod) && p.cfg.faradayRemote:
640-
return readMacaroon(lncfg.CleanAndExpandPath(
641-
p.cfg.Remote.Faraday.MacaroonPath,
642-
))
643-
644-
case p.permsMgr.IsLoopURI(fullMethod) && p.cfg.loopRemote:
645-
return readMacaroon(lncfg.CleanAndExpandPath(
646-
p.cfg.Remote.Loop.MacaroonPath,
647-
))
648-
649-
case p.permsMgr.IsPoolURI(fullMethod) && p.cfg.poolRemote:
650-
return readMacaroon(lncfg.CleanAndExpandPath(
651-
p.cfg.Remote.Pool.MacaroonPath,
652-
))
656+
subserver, err := p.permsMgr.SubServerHandler(fullMethod)
657+
if err == nil {
658+
switch {
659+
case subserver == subservers.FARADAY && p.cfg.faradayRemote:
660+
return readMacaroon(lncfg.CleanAndExpandPath(
661+
p.cfg.Remote.Faraday.MacaroonPath,
662+
))
663+
case subserver == subservers.LOOP && p.cfg.loopRemote:
664+
return readMacaroon(lncfg.CleanAndExpandPath(
665+
p.cfg.Remote.Loop.MacaroonPath,
666+
))
667+
case subserver == subservers.POOL && p.cfg.poolRemote:
668+
return readMacaroon(lncfg.CleanAndExpandPath(
669+
p.cfg.Remote.Pool.MacaroonPath,
670+
))
671+
}
653672
}
654673

655674
return nil, nil

terminal.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
3232
"github.com/lightninglabs/lightning-terminal/rules"
3333
"github.com/lightninglabs/lightning-terminal/session"
34+
"github.com/lightninglabs/lightning-terminal/subservers"
3435
"github.com/lightninglabs/lndclient"
3536
"github.com/lightninglabs/loop"
3637
"github.com/lightninglabs/loop/loopd"
@@ -999,7 +1000,7 @@ func (g *LightningTerminal) ValidateMacaroon(ctx context.Context,
9991000
// process. Calls that we proxy to a remote host don't need to be
10001001
// checked as they'll have their own interceptor.
10011002
switch {
1002-
case g.permsMgr.IsFaradayURI(fullMethod):
1003+
case g.permsMgr.IsSubServerURI(subservers.FARADAY, fullMethod):
10031004
// In remote mode we just pass through the request, the remote
10041005
// daemon will check the macaroon.
10051006
if g.cfg.faradayRemote {
@@ -1023,7 +1024,7 @@ func (g *LightningTerminal) ValidateMacaroon(ctx context.Context,
10231024
}
10241025
}
10251026

1026-
case g.permsMgr.IsLoopURI(fullMethod):
1027+
case g.permsMgr.IsSubServerURI(subservers.LOOP, fullMethod):
10271028
// In remote mode we just pass through the request, the remote
10281029
// daemon will check the macaroon.
10291030
if g.cfg.loopRemote {
@@ -1047,7 +1048,7 @@ func (g *LightningTerminal) ValidateMacaroon(ctx context.Context,
10471048
}
10481049
}
10491050

1050-
case g.permsMgr.IsPoolURI(fullMethod):
1051+
case g.permsMgr.IsSubServerURI(subservers.POOL, fullMethod):
10511052
// In remote mode we just pass through the request, the remote
10521053
// daemon will check the macaroon.
10531054
if g.cfg.poolRemote {
@@ -1071,7 +1072,7 @@ func (g *LightningTerminal) ValidateMacaroon(ctx context.Context,
10711072
}
10721073
}
10731074

1074-
case g.permsMgr.IsLitURI(fullMethod):
1075+
case g.permsMgr.IsSubServerURI(subservers.LIT, fullMethod):
10751076
if !g.macaroonServiceStarted {
10761077
return fmt.Errorf("the macaroon service has not " +
10771078
"started yet")

0 commit comments

Comments
 (0)