multi: add macroon service to litd

Add a macaroonService to the main LightningTerminal struct.
This commit is contained in:
Elle Mouton 2022-03-30 14:28:46 +02:00
parent 1aee59426e
commit 50cfd3b823
No known key found for this signature in database
GPG key ID: D7D916376026F177
2 changed files with 53 additions and 0 deletions

View file

@ -73,6 +73,10 @@ const (
// certificate. The value corresponds to 14 months
// (14 months * 30 days * 24 hours).
DefaultAutogenValidity = 14 * 30 * 24 * time.Hour
// DefaultMacaroonFilename is the default file name for the
// autogenerated lit macaroon.
DefaultMacaroonFilename = "lit.macaroon"
)
var (
@ -119,6 +123,12 @@ var (
lndDefaultConfig.DataDir, defaultLndChainSubDir,
defaultLndChain, DefaultNetwork, defaultLndMacaroon,
)
// DefaultMacaroonPath is the default full path of the base lit
// macaroon.
DefaultMacaroonPath = filepath.Join(
DefaultLitDir, DefaultNetwork, DefaultMacaroonFilename,
)
)
// Config is the main configuration struct of lightning-terminal. It contains
@ -141,6 +151,8 @@ type Config struct {
LitDir string `long:"lit-dir" description:"The main directory where LiT looks for its configuration file. If LiT is running in 'remote' lnd mode, this is also the directory where the TLS certificates and log files are stored by default."`
ConfigFile string `long:"configfile" description:"Path to LiT's configuration file."`
MacaroonPath string `long:"macaroonpath" description:"Path to write the macaroon for litd's RPC and REST services if it doesn't exist."`
// Network is the Bitcoin network we're running on. This will be parsed
// before the configuration is loaded and will set the correct flag on
// `lnd.bitcoin.mainnet|testnet|regtest` and also for the other daemons.
@ -296,6 +308,7 @@ func defaultConfig() *Config {
LitDir: DefaultLitDir,
LetsEncryptListen: defaultLetsEncryptListen,
LetsEncryptDir: defaultLetsEncryptDir,
MacaroonPath: DefaultMacaroonPath,
ConfigFile: defaultConfigFile,
FaradayMode: defaultFaradayMode,
Faraday: &faradayDefaultConfig,
@ -394,6 +407,14 @@ func loadAndValidateConfig(interceptor signal.Interceptor) (*Config, error) {
"UI, at least %d characters long", uiPasswordMinLength)
}
if cfg.Network != DefaultNetwork {
if cfg.MacaroonPath == DefaultMacaroonPath {
cfg.MacaroonPath = filepath.Join(
litDir, cfg.Network, DefaultMacaroonFilename,
)
}
}
// Initiate our listeners. For now, we only support listening on one
// port at a time because we can only pass in one pre-configured RPC
// listener into lnd.

View file

@ -159,6 +159,9 @@ type LightningTerminal struct {
sessionRpcServer *sessionRpcServer
sessionRpcServerStarted bool
macaroonService *lndclient.MacaroonService
macaroonServiceStarted bool
restHandler http.Handler
restCancel func()
}
@ -548,6 +551,28 @@ func (g *LightningTerminal) startSubservers() error {
g.poolStarted = true
}
g.macaroonService, err = lndclient.NewMacaroonService(
&lndclient.MacaroonServiceConfig{
DBPath: path.Join(g.cfg.LitDir, g.cfg.Network),
MacaroonLocation: "litd",
StatelessInit: !createDefaultMacaroons,
RequiredPerms: litPermissions,
LndClient: &g.lndClient.LndServices,
EphemeralKey: lndclient.SharedKeyNUMS,
KeyLocator: lndclient.SharedKeyLocator,
MacaroonPath: g.cfg.MacaroonPath,
},
)
if err != nil {
log.Errorf("Could not create a new macaroon service: %v", err)
return err
}
if err := g.macaroonService.Start(); err != nil {
return fmt.Errorf("could not start macaroon service: %v", err)
}
g.macaroonServiceStarted = true
if err = g.sessionRpcServer.start(); err != nil {
return err
}
@ -829,6 +854,13 @@ func (g *LightningTerminal) shutdown() error {
}
}
if g.macaroonServiceStarted {
if err := g.macaroonService.Stop(); err != nil {
log.Errorf("Error stopping macaroon service: %v", err)
returnErr = err
}
}
if g.lndClient != nil {
g.lndClient.Close()
}