Skip to content

Commit 05c5268

Browse files
committed
multi: add ReadRemoteMacaroon method to subserver manager
1 parent 9eb0151 commit 05c5268

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

rpc_proxy.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -553,22 +553,9 @@ func (p *rpcProxy) convertSuperMacaroon(ctx context.Context, macHex string,
553553

554554
// Is this actually a request that goes to a daemon that is running
555555
// remotely?
556-
subserver, err := p.permsMgr.SubServerHandler(fullMethod)
557-
if err == nil {
558-
switch {
559-
case subserver == subservers.FARADAY && p.cfg.faradayRemote:
560-
return readMacaroon(lncfg.CleanAndExpandPath(
561-
p.cfg.Remote.Faraday.MacaroonPath,
562-
))
563-
case subserver == subservers.LOOP && p.cfg.loopRemote:
564-
return readMacaroon(lncfg.CleanAndExpandPath(
565-
p.cfg.Remote.Loop.MacaroonPath,
566-
))
567-
case subserver == subservers.POOL && p.cfg.poolRemote:
568-
return readMacaroon(lncfg.CleanAndExpandPath(
569-
p.cfg.Remote.Pool.MacaroonPath,
570-
))
571-
}
556+
handled, macBytes, err := p.subServerMgr.ReadRemoteMacaroon(fullMethod)
557+
if handled {
558+
return macBytes, err
572559
}
573560

574561
return nil, nil

subservers/manager.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ package subservers
33
import (
44
"context"
55
"fmt"
6+
"io/ioutil"
67
"sync"
78
"time"
89

910
"github.com/lightninglabs/lightning-terminal/perms"
1011
"github.com/lightninglabs/lndclient"
12+
"github.com/lightningnetwork/lnd/lncfg"
1113
"github.com/lightningnetwork/lnd/lnrpc"
1214
grpcProxy "github.com/mwitkow/grpc-proxy/proxy"
1315
"google.golang.org/grpc"
1416
"google.golang.org/grpc/backoff"
1517
"google.golang.org/grpc/credentials"
1618
"gopkg.in/macaroon-bakery.v2/bakery"
19+
"gopkg.in/macaroon.v2"
1720
)
1821

1922
var (
@@ -210,6 +213,32 @@ func (s *Manager) MacaroonPath(uri string) (bool, string) {
210213
return false, ""
211214
}
212215

216+
// ReadRemoteMacaroon checks if any of the manager's sub-servers running in
217+
// remote mode owns the given uri and if so, the appropriate macaroon path is
218+
// returned for that sub-server.
219+
func (s *Manager) ReadRemoteMacaroon(uri string) (bool, []byte, error) {
220+
s.mu.RLock()
221+
defer s.mu.RUnlock()
222+
223+
for _, ss := range s.servers {
224+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
225+
continue
226+
}
227+
228+
if !ss.Remote() {
229+
return false, nil, nil
230+
}
231+
232+
macBytes, err := readMacaroon(lncfg.CleanAndExpandPath(
233+
ss.RemoteConfig().MacaroonPath,
234+
))
235+
236+
return true, macBytes, err
237+
}
238+
239+
return false, nil, nil
240+
}
241+
213242
// Stop stops all the manager's sub-servers
214243
func (s *Manager) Stop() error {
215244
var returnErr error
@@ -259,3 +288,21 @@ func dialBackend(name, dialAddr, tlsCertPath string) (*grpc.ClientConn, error) {
259288
}
260289
return cc, nil
261290
}
291+
292+
// readMacaroon tries to read the macaroon file at the specified path.
293+
func readMacaroon(macPath string) ([]byte, error) {
294+
// Load the specified macaroon file.
295+
macBytes, err := ioutil.ReadFile(macPath)
296+
if err != nil {
297+
return nil, fmt.Errorf("unable to read macaroon path: %v", err)
298+
}
299+
300+
// Make sure it actually is a macaroon by parsing it.
301+
mac := &macaroon.Macaroon{}
302+
if err := mac.UnmarshalBinary(macBytes); err != nil {
303+
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
304+
}
305+
306+
// It's a macaroon alright, let's return the binary data now.
307+
return macBytes, nil
308+
}

0 commit comments

Comments
 (0)