Skip to content

Commit af90cd9

Browse files
committed
multi: add macroon service to sessionRpcServer
Add a macaroonService to the sessionRPCServer so that its methods can be protected by macaroon authentication.
1 parent 38ae937 commit af90cd9

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ const (
7373
// certificate. The value corresponds to 14 months
7474
// (14 months * 30 days * 24 hours).
7575
DefaultAutogenValidity = 14 * 30 * 24 * time.Hour
76+
77+
// DefaultMacaroonFilename is the default file name for the
78+
// autogenerated lit macaroon.
79+
DefaultMacaroonFilename = "lit.macaroon"
7680
)
7781

7882
var (
@@ -119,6 +123,12 @@ var (
119123
lndDefaultConfig.DataDir, defaultLndChainSubDir,
120124
defaultLndChain, DefaultNetwork, defaultLndMacaroon,
121125
)
126+
127+
// DefaultMacaroonPath is the default full path of the base lit
128+
// macaroon.
129+
DefaultMacaroonPath = filepath.Join(
130+
DefaultLitDir, DefaultNetwork, DefaultMacaroonFilename,
131+
)
122132
)
123133

124134
// Config is the main configuration struct of lightning-terminal. It contains
@@ -141,6 +151,8 @@ type Config struct {
141151
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."`
142152
ConfigFile string `long:"configfile" description:"Path to LiT's configuration file."`
143153

154+
MacaroonPath string `long:"macaroonpath" description:"Path to write the macaroon for litd's RPC and REST services if it doesn't exist."`
155+
144156
// Network is the Bitcoin network we're running on. This will be parsed
145157
// before the configuration is loaded and will set the correct flag on
146158
// `lnd.bitcoin.mainnet|testnet|regtest` and also for the other daemons.
@@ -296,6 +308,7 @@ func defaultConfig() *Config {
296308
LitDir: DefaultLitDir,
297309
LetsEncryptListen: defaultLetsEncryptListen,
298310
LetsEncryptDir: defaultLetsEncryptDir,
311+
MacaroonPath: DefaultMacaroonPath,
299312
ConfigFile: defaultConfigFile,
300313
FaradayMode: defaultFaradayMode,
301314
Faraday: &faradayDefaultConfig,
@@ -394,6 +407,14 @@ func loadAndValidateConfig(interceptor signal.Interceptor) (*Config, error) {
394407
"UI, at least %d characters long", uiPasswordMinLength)
395408
}
396409

410+
if cfg.Network != DefaultNetwork {
411+
if cfg.MacaroonPath == DefaultMacaroonPath {
412+
cfg.MacaroonPath = filepath.Join(
413+
litDir, cfg.Network, DefaultMacaroonFilename,
414+
)
415+
}
416+
}
417+
397418
// Initiate our listeners. For now, we only support listening on one
398419
// port at a time because we can only pass in one pre-configured RPC
399420
// listener into lnd.

session_rpcserver.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import (
1111
"github.com/lightninglabs/lightning-node-connect/mailbox"
1212
"github.com/lightninglabs/lightning-terminal/litrpc"
1313
"github.com/lightninglabs/lightning-terminal/session"
14+
"github.com/lightninglabs/lndclient"
1415
"google.golang.org/grpc"
1516
)
1617

1718
// sessionRpcServer is the gRPC server for the Session RPC interface.
1819
type sessionRpcServer struct {
1920
litrpc.UnimplementedSessionsServer
2021

21-
cfg *sessionRpcServerConfig
22-
db *session.DB
23-
sessionServer *session.Server
22+
cfg *sessionRpcServerConfig
23+
db *session.DB
24+
sessionServer *session.Server
25+
macaroonService *lndclient.MacaroonService
2426

2527
quit chan struct{}
2628
wg sync.WaitGroup
@@ -32,6 +34,7 @@ type sessionRpcServer struct {
3234
type sessionRpcServerConfig struct {
3335
basicAuth string
3436
dbDir string
37+
macaroonPath string
3538
grpcOptions []grpc.ServerOption
3639
registerGrpcServers func(server *grpc.Server)
3740
superMacBaker func(ctx context.Context, rootKeyID uint64,
@@ -73,7 +76,32 @@ func newSessionRPCServer(cfg *sessionRpcServerConfig) (*sessionRpcServer,
7376
// start all the components necessary for the sessionRpcServer to start serving
7477
// requests. This includes starting the macaroon service and resuming all
7578
// non-revoked sessions.
76-
func (s *sessionRpcServer) start() error {
79+
func (s *sessionRpcServer) start(stateless bool,
80+
lndClient *lndclient.LndServices) error {
81+
82+
var err error
83+
s.macaroonService, err = lndclient.NewMacaroonService(
84+
&lndclient.MacaroonServiceConfig{
85+
DBPath: s.cfg.dbDir,
86+
MacaroonLocation: "litd",
87+
StatelessInit: stateless,
88+
RequiredPerms: litPermissions,
89+
LndClient: lndClient,
90+
EphemeralKey: lndclient.SharedKeyNUMS,
91+
KeyLocator: lndclient.SharedKeyLocator,
92+
MacaroonPath: s.cfg.macaroonPath,
93+
},
94+
)
95+
if err != nil {
96+
log.Errorf("Could not create a new macaroon service: %v", err)
97+
return err
98+
}
99+
100+
if err := s.macaroonService.Start(); err != nil {
101+
log.Errorf("Could not start macaroon service: %v", err)
102+
return err
103+
}
104+
77105
// Start up all previously created sessions.
78106
sessions, err := s.db.ListSessions()
79107
if err != nil {
@@ -98,6 +126,11 @@ func (s *sessionRpcServer) stop() error {
98126
}
99127
s.sessionServer.Stop()
100128

129+
if err := s.macaroonService.Stop(); err != nil {
130+
log.Errorf("Error stopping macaroon service: %v", err)
131+
returnErr = err
132+
}
133+
101134
close(s.quit)
102135
s.wg.Wait()
103136
})

terminal.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ func (g *LightningTerminal) Run() error {
200200
bufRpcListener,
201201
)
202202
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
203-
basicAuth: g.rpcProxy.basicAuth,
204-
dbDir: path.Join(g.cfg.LitDir, g.cfg.Network),
203+
basicAuth: g.rpcProxy.basicAuth,
204+
macaroonPath: g.cfg.MacaroonPath,
205+
dbDir: path.Join(g.cfg.LitDir, g.cfg.Network),
205206
grpcOptions: []grpc.ServerOption{
206207
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
207208
grpc.ChainStreamInterceptor(
@@ -544,7 +545,9 @@ func (g *LightningTerminal) startSubservers() error {
544545
g.poolStarted = true
545546
}
546547

547-
if err = g.sessionRpcServer.start(); err != nil {
548+
if err = g.sessionRpcServer.start(
549+
!createDefaultMacaroons, &g.lndClient.LndServices,
550+
); err != nil {
548551
return err
549552
}
550553
g.sessionRpcServerStarted = true

0 commit comments

Comments
 (0)