Skip to content
This repository was archived by the owner on Jun 25, 2024. It is now read-only.

Commit debf9d0

Browse files
committed
Pass in macaroon string to sidecar acceptor
1 parent dbb1fbb commit debf9d0

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

config_builder.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type WalletConfigBuilder interface {
9797
BuildWalletConfig(context.Context, *DatabaseInstances,
9898
*rpcperms.InterceptorChain,
9999
[]*ListenerWithSignal) (*chainreg.PartialChainControl,
100-
*btcwallet.Config, func(), error)
100+
*btcwallet.Config, func(), []byte, error)
101101
}
102102

103103
// ChainControlBuilder is an interface that must be satisfied by a custom wallet
@@ -220,7 +220,7 @@ func (d *DefaultWalletImpl) Permissions() map[string][]bakery.Op {
220220
func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
221221
dbs *DatabaseInstances, interceptorChain *rpcperms.InterceptorChain,
222222
grpcListeners []*ListenerWithSignal) (*chainreg.PartialChainControl,
223-
*btcwallet.Config, func(), error) {
223+
*btcwallet.Config, func(), []byte, error) {
224224

225225
// Keep track of our various cleanup functions. We use a defer function
226226
// as well to not repeat ourselves with every return statement.
@@ -262,7 +262,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
262262
err := fmt.Errorf("unable to initialize neutrino "+
263263
"backend: %v", err)
264264
d.logger.Error(err)
265-
return nil, nil, nil, err
265+
return nil, nil, nil, nil, err
266266
}
267267
cleanUpTasks = append(cleanUpTasks, neutrinoCleanUp)
268268
neutrinoCS = neutrinoBackend
@@ -287,7 +287,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
287287
d.pwService.SetMacaroonDB(dbs.MacaroonDB)
288288
walletExists, err := d.pwService.WalletExists()
289289
if err != nil {
290-
return nil, nil, nil, err
290+
return nil, nil, nil, nil, err
291291
}
292292

293293
if !walletExists {
@@ -304,7 +304,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
304304
if d.cfg.WalletUnlockPasswordFile != "" && !walletExists &&
305305
!d.cfg.WalletUnlockAllowCreate {
306306

307-
return nil, nil, nil, fmt.Errorf("wallet unlock password file " +
307+
return nil, nil, nil, nil, fmt.Errorf("wallet unlock password file " +
308308
"was specified but wallet does not exist; initialize " +
309309
"the wallet before using auto unlocking")
310310
}
@@ -323,7 +323,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
323323
"password provided in file")
324324
pwBytes, err := ioutil.ReadFile(d.cfg.WalletUnlockPasswordFile)
325325
if err != nil {
326-
return nil, nil, nil, fmt.Errorf("error reading "+
326+
return nil, nil, nil, nil, fmt.Errorf("error reading "+
327327
"password from file %s: %v",
328328
d.cfg.WalletUnlockPasswordFile, err)
329329
}
@@ -339,7 +339,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
339339
pwBytes, 0,
340340
)
341341
if err != nil {
342-
return nil, nil, nil, fmt.Errorf("error unlocking "+
342+
return nil, nil, nil, nil, fmt.Errorf("error unlocking "+
343343
"wallet with password from file: %v", err)
344344
}
345345

@@ -360,7 +360,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
360360
// over RPC.
361361
default:
362362
if err := d.interceptor.Notifier.NotifyReady(false); err != nil {
363-
return nil, nil, nil, err
363+
return nil, nil, nil, nil, err
364364
}
365365

366366
params, err := waitForWalletPassword(
@@ -371,7 +371,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
371371
err := fmt.Errorf("unable to set up wallet password "+
372372
"listeners: %v", err)
373373
d.logger.Error(err)
374-
return nil, nil, nil, err
374+
return nil, nil, nil, nil, err
375375
}
376376

377377
walletInitParams = *params
@@ -391,7 +391,10 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
391391
}
392392
}
393393

394-
var macaroonService *macaroons.Service
394+
var (
395+
macaroonService *macaroons.Service
396+
adminMacBytes []byte
397+
)
395398
if !d.cfg.NoMacaroons {
396399
// Create the macaroon authentication/authorization service.
397400
macaroonService, err = macaroons.NewService(
@@ -403,7 +406,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
403406
err := fmt.Errorf("unable to set up macaroon "+
404407
"authentication: %v", err)
405408
d.logger.Error(err)
406-
return nil, nil, nil, err
409+
return nil, nil, nil, nil, err
407410
}
408411
cleanUpTasks = append(cleanUpTasks, func() {
409412
if err := macaroonService.Close(); err != nil {
@@ -419,7 +422,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
419422
if err != nil && err != macaroons.ErrAlreadyUnlocked {
420423
err := fmt.Errorf("unable to unlock macaroons: %v", err)
421424
d.logger.Error(err)
422-
return nil, nil, nil, err
425+
return nil, nil, nil, nil, err
423426
}
424427

425428
// In case we actually needed to unlock the wallet, we now need
@@ -428,11 +431,11 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
428431
// backup mode, there's nobody listening on the channel and we'd
429432
// block here forever.
430433
if !d.cfg.NoSeedBackup {
431-
adminMacBytes, err := bakeMacaroon(
434+
adminMacBytes, err = bakeMacaroon(
432435
ctx, macaroonService, adminPermissions(),
433436
)
434437
if err != nil {
435-
return nil, nil, nil, err
438+
return nil, nil, nil, nil, err
436439
}
437440

438441
// The channel is buffered by one element so writing
@@ -463,7 +466,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
463466
err := fmt.Errorf("unable to create macaroons "+
464467
"%v", err)
465468
d.logger.Error(err)
466-
return nil, nil, nil, err
469+
return nil, nil, nil, nil, err
467470
}
468471
}
469472

@@ -555,7 +558,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
555558
err := fmt.Errorf("unable to create partial chain control: %v",
556559
err)
557560
d.logger.Error(err)
558-
return nil, nil, nil, err
561+
return nil, nil, nil, nil, err
559562
}
560563

561564
walletConfig := &btcwallet.Config{
@@ -580,12 +583,12 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
580583
walletConfig.CoinSelectionStrategy = wallet.CoinSelectionRandom
581584

582585
default:
583-
return nil, nil, nil, fmt.Errorf("unknown coin selection "+
586+
return nil, nil, nil, nil, fmt.Errorf("unknown coin selection "+
584587
"strategy %v", d.cfg.CoinSelectionStrategy)
585588
}
586589

587590
earlyExit = false
588-
return partialChainControl, walletConfig, cleanUp, nil
591+
return partialChainControl, walletConfig, cleanUp, adminMacBytes, nil
589592
}
590593

591594
// BuildChainControl is responsible for creating a fully populated chain

lnd.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ const (
7373
//
7474
// NOTE: This should only be called after the RPCListener has signaled it is
7575
// ready.
76-
func AdminAuthOptions(cfg *Config, skipMacaroons, insecure bool) ([]grpc.DialOption, error) {
76+
func AdminAuthOptions(cfg *Config, skipMacaroons, insecure bool,
77+
macBytes []byte) ([]grpc.DialOption, error) {
78+
7779
var (
7880
creds credentials.TransportCredentials
7981
err error
@@ -98,11 +100,14 @@ func AdminAuthOptions(cfg *Config, skipMacaroons, insecure bool) ([]grpc.DialOpt
98100

99101
// Get the admin macaroon if macaroons are active.
100102
if !skipMacaroons && !cfg.NoMacaroons {
101-
// Load the adming macaroon file.
102-
macBytes, err := ioutil.ReadFile(cfg.AdminMacPath)
103-
if err != nil {
104-
return nil, fmt.Errorf("unable to read macaroon "+
105-
"path (check the network setting!): %v", err)
103+
// If we sent the macaroon bytes, don't read it from disk.
104+
if macBytes == nil {
105+
// Load the adming macaroon file.
106+
macBytes, err = ioutil.ReadFile(cfg.AdminMacPath)
107+
if err != nil {
108+
return nil, fmt.Errorf("unable to read macaroon "+
109+
"path (check the network setting!): %v", err)
110+
}
106111
}
107112

108113
mac := &macaroon.Macaroon{}
@@ -386,7 +391,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
386391

387392
defer cleanUp()
388393

389-
partialChainControl, walletConfig, cleanUp, err := implCfg.BuildWalletConfig(
394+
partialChainControl, walletConfig, cleanUp, mac, err := implCfg.BuildWalletConfig(
390395
ctx, dbs, interceptorChain, grpcListeners,
391396
)
392397
if err != nil {
@@ -671,7 +676,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
671676
bestHeight)
672677

673678
if cfg.SidecarAcceptor {
674-
acceptor, err := StartSidecarAcceptor(cfg)
679+
acceptor, err := StartSidecarAcceptor(cfg, mac)
675680
if err != nil {
676681
ltndLog.Error(err)
677682
return err

start_sidecar.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lnd
22

33
import (
44
"context"
5+
"encoding/hex"
56
"errors"
67
"fmt"
78
"time"
@@ -18,13 +19,13 @@ import (
1819
"google.golang.org/grpc"
1920
)
2021

21-
func StartSidecarAcceptor(cfg *Config) (*acceptor.SidecarAcceptor, error) {
22-
opts, err := AdminAuthOptions(cfg, false, true)
22+
func StartSidecarAcceptor(cfg *Config, macBytes []byte) (*acceptor.SidecarAcceptor, error) {
23+
opts, err := AdminAuthOptions(cfg, false, true, macBytes)
2324
if err != nil {
2425
return nil, err
2526
}
2627

27-
host := cfg.RPCListeners[0].String()
28+
host := "127.0.0.1:10009"
2829
conn, err := grpc.Dial(host, opts...)
2930
if err != nil {
3031
return nil, fmt.Errorf("unable to connect to RPC server: %v", err)
@@ -42,7 +43,8 @@ func StartSidecarAcceptor(cfg *Config) (*acceptor.SidecarAcceptor, error) {
4243
LndAddress: host,
4344
Network: network,
4445
TLSPath: cfg.TLSCertPath,
45-
CustomMacaroonPath: cfg.AdminMacPath,
46+
Insecure: true,
47+
CustomMacaroonHex: hex.EncodeToString(macBytes),
4648
BlockUntilChainSynced: false,
4749
BlockUntilUnlocked: true,
4850
CallerCtx: ctxc,

0 commit comments

Comments
 (0)