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

Commit

Permalink
Pass in macaroon string to sidecar acceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Dec 15, 2021
1 parent dbb1fbb commit debf9d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
39 changes: 21 additions & 18 deletions config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type WalletConfigBuilder interface {
BuildWalletConfig(context.Context, *DatabaseInstances,
*rpcperms.InterceptorChain,
[]*ListenerWithSignal) (*chainreg.PartialChainControl,
*btcwallet.Config, func(), error)
*btcwallet.Config, func(), []byte, error)
}

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

// Keep track of our various cleanup functions. We use a defer function
// as well to not repeat ourselves with every return statement.
Expand Down Expand Up @@ -262,7 +262,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
err := fmt.Errorf("unable to initialize neutrino "+
"backend: %v", err)
d.logger.Error(err)
return nil, nil, nil, err
return nil, nil, nil, nil, err
}
cleanUpTasks = append(cleanUpTasks, neutrinoCleanUp)
neutrinoCS = neutrinoBackend
Expand All @@ -287,7 +287,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
d.pwService.SetMacaroonDB(dbs.MacaroonDB)
walletExists, err := d.pwService.WalletExists()
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, nil, err
}

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

return nil, nil, nil, fmt.Errorf("wallet unlock password file " +
return nil, nil, nil, nil, fmt.Errorf("wallet unlock password file " +
"was specified but wallet does not exist; initialize " +
"the wallet before using auto unlocking")
}
Expand All @@ -323,7 +323,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
"password provided in file")
pwBytes, err := ioutil.ReadFile(d.cfg.WalletUnlockPasswordFile)
if err != nil {
return nil, nil, nil, fmt.Errorf("error reading "+
return nil, nil, nil, nil, fmt.Errorf("error reading "+
"password from file %s: %v",
d.cfg.WalletUnlockPasswordFile, err)
}
Expand All @@ -339,7 +339,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
pwBytes, 0,
)
if err != nil {
return nil, nil, nil, fmt.Errorf("error unlocking "+
return nil, nil, nil, nil, fmt.Errorf("error unlocking "+
"wallet with password from file: %v", err)
}

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

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

walletInitParams = *params
Expand All @@ -391,7 +391,10 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
}
}

var macaroonService *macaroons.Service
var (
macaroonService *macaroons.Service
adminMacBytes []byte
)
if !d.cfg.NoMacaroons {
// Create the macaroon authentication/authorization service.
macaroonService, err = macaroons.NewService(
Expand All @@ -403,7 +406,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
err := fmt.Errorf("unable to set up macaroon "+
"authentication: %v", err)
d.logger.Error(err)
return nil, nil, nil, err
return nil, nil, nil, nil, err
}
cleanUpTasks = append(cleanUpTasks, func() {
if err := macaroonService.Close(); err != nil {
Expand All @@ -419,7 +422,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
if err != nil && err != macaroons.ErrAlreadyUnlocked {
err := fmt.Errorf("unable to unlock macaroons: %v", err)
d.logger.Error(err)
return nil, nil, nil, err
return nil, nil, nil, nil, err
}

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

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

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

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

default:
return nil, nil, nil, fmt.Errorf("unknown coin selection "+
return nil, nil, nil, nil, fmt.Errorf("unknown coin selection "+
"strategy %v", d.cfg.CoinSelectionStrategy)
}

earlyExit = false
return partialChainControl, walletConfig, cleanUp, nil
return partialChainControl, walletConfig, cleanUp, adminMacBytes, nil
}

// BuildChainControl is responsible for creating a fully populated chain
Expand Down
21 changes: 13 additions & 8 deletions lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ const (
//
// NOTE: This should only be called after the RPCListener has signaled it is
// ready.
func AdminAuthOptions(cfg *Config, skipMacaroons, insecure bool) ([]grpc.DialOption, error) {
func AdminAuthOptions(cfg *Config, skipMacaroons, insecure bool,
macBytes []byte) ([]grpc.DialOption, error) {

var (
creds credentials.TransportCredentials
err error
Expand All @@ -98,11 +100,14 @@ func AdminAuthOptions(cfg *Config, skipMacaroons, insecure bool) ([]grpc.DialOpt

// Get the admin macaroon if macaroons are active.
if !skipMacaroons && !cfg.NoMacaroons {
// Load the adming macaroon file.
macBytes, err := ioutil.ReadFile(cfg.AdminMacPath)
if err != nil {
return nil, fmt.Errorf("unable to read macaroon "+
"path (check the network setting!): %v", err)
// If we sent the macaroon bytes, don't read it from disk.
if macBytes == nil {
// Load the adming macaroon file.
macBytes, err = ioutil.ReadFile(cfg.AdminMacPath)
if err != nil {
return nil, fmt.Errorf("unable to read macaroon "+
"path (check the network setting!): %v", err)
}
}

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

defer cleanUp()

partialChainControl, walletConfig, cleanUp, err := implCfg.BuildWalletConfig(
partialChainControl, walletConfig, cleanUp, mac, err := implCfg.BuildWalletConfig(
ctx, dbs, interceptorChain, grpcListeners,
)
if err != nil {
Expand Down Expand Up @@ -671,7 +676,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
bestHeight)

if cfg.SidecarAcceptor {
acceptor, err := StartSidecarAcceptor(cfg)
acceptor, err := StartSidecarAcceptor(cfg, mac)
if err != nil {
ltndLog.Error(err)
return err
Expand Down
10 changes: 6 additions & 4 deletions start_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lnd

import (
"context"
"encoding/hex"
"errors"
"fmt"
"time"
Expand All @@ -18,13 +19,13 @@ import (
"google.golang.org/grpc"
)

func StartSidecarAcceptor(cfg *Config) (*acceptor.SidecarAcceptor, error) {
opts, err := AdminAuthOptions(cfg, false, true)
func StartSidecarAcceptor(cfg *Config, macBytes []byte) (*acceptor.SidecarAcceptor, error) {
opts, err := AdminAuthOptions(cfg, false, true, macBytes)
if err != nil {
return nil, err
}

host := cfg.RPCListeners[0].String()
host := "127.0.0.1:10009"
conn, err := grpc.Dial(host, opts...)
if err != nil {
return nil, fmt.Errorf("unable to connect to RPC server: %v", err)
Expand All @@ -42,7 +43,8 @@ func StartSidecarAcceptor(cfg *Config) (*acceptor.SidecarAcceptor, error) {
LndAddress: host,
Network: network,
TLSPath: cfg.TLSCertPath,
CustomMacaroonPath: cfg.AdminMacPath,
Insecure: true,
CustomMacaroonHex: hex.EncodeToString(macBytes),
BlockUntilChainSynced: false,
BlockUntilUnlocked: true,
CallerCtx: ctxc,
Expand Down

0 comments on commit debf9d0

Please sign in to comment.