Skip to content

Commit bd77c1a

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 8baec2c commit bd77c1a

File tree

5 files changed

+71
-11
lines changed

5 files changed

+71
-11
lines changed

config.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ var (
119119
lndDefaultConfig.DataDir, defaultLndChainSubDir,
120120
defaultLndChain, DefaultNetwork, defaultLndMacaroon,
121121
)
122+
123+
// DefaultMacaroonFilename is the default file name for the
124+
// autogenerated lit macaroon.
125+
DefaultMacaroonFilename = "lit.macaroon"
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.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/jessevdk/go-flags v1.4.0
1212
github.com/lightninglabs/faraday v0.2.7-alpha
1313
github.com/lightninglabs/lightning-node-connect v0.1.5-alpha
14-
github.com/lightninglabs/lndclient v0.14.0-7
14+
github.com/lightninglabs/lndclient v0.14.0-8
1515
github.com/lightninglabs/loop v0.15.1-beta
1616
github.com/lightninglabs/pool v0.5.4-alpha.0.20220114202858-525fe156d240
1717
github.com/lightninglabs/pool/auctioneerrpc v1.0.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ github.com/lightninglabs/lndclient v0.11.0-4/go.mod h1:8/cTKNwgL87NX123gmlv3Xh6p
617617
github.com/lightninglabs/lndclient v0.14.0-5/go.mod h1:2kH9vNoc29ghIkfMjxwSeK8yCxsYfR80XAJ9PU/QWWk=
618618
github.com/lightninglabs/lndclient v0.14.0-7 h1:muqPju9ixBtQNcO0SkvbZ2b2oORUMRqQ4e+aC077Qa8=
619619
github.com/lightninglabs/lndclient v0.14.0-7/go.mod h1:2kH9vNoc29ghIkfMjxwSeK8yCxsYfR80XAJ9PU/QWWk=
620+
github.com/lightninglabs/lndclient v0.14.0-8 h1:vdwV6yFU4A7BjG2V8cpI8Kqdl2M0NSfsA+RWR+JGTko=
621+
github.com/lightninglabs/lndclient v0.14.0-8/go.mod h1:YIE/Yac69hIMiq9cm/ZC2sP4F0Llv3tC4hZGfgOhdeY=
620622
github.com/lightninglabs/loop v0.15.1-beta h1:X4qth5qAdpgKarmcltO85HxMze3Wrk8FzI46Cwt9H4A=
621623
github.com/lightninglabs/loop v0.15.1-beta/go.mod h1:9TawqLzvjDP4pswZ8QkvTcBqH+wGKBffP+r6mFGBVi4=
622624
github.com/lightninglabs/neutrino v0.11.0/go.mod h1:CuhF0iuzg9Sp2HO6ZgXgayviFTn1QHdSTJlMncK80wg=

session_rpcserver.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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

@@ -20,9 +21,10 @@ type sessionRpcServer struct {
2021

2122
basicAuth string
2223

23-
cfg *sessionRpcServerConfig
24-
db *session.DB
25-
sessionServer *session.Server
24+
cfg *sessionRpcServerConfig
25+
db *session.DB
26+
sessionServer *session.Server
27+
macaroonService *lndclient.MacaroonService
2628

2729
quit chan struct{}
2830
wg sync.WaitGroup
@@ -32,9 +34,10 @@ type sessionRpcServer struct {
3234
// sessionRpcServerConfig holds the values used to configure the
3335
// sessionRpcServer.
3436
type sessionRpcServerConfig struct {
35-
basicAuth string
36-
dbDir string
37-
grpcOptions []grpc.ServerOption
37+
basicAuth string
38+
dbDir string
39+
grpcOptions []grpc.ServerOption
40+
macaroonPath string
3841
}
3942

4043
// newSessionRPCServer creates a new sessionRpcServer using the passed config.
@@ -68,7 +71,32 @@ func newSessionRPCServer(cfg *sessionRpcServerConfig) (*sessionRpcServer,
6871
// start all the components necessary for the sessionRpcServer to start serving
6972
// requests. This includes starting the macaroon service and resuming all
7073
// non-revoked sessions.
71-
func (s *sessionRpcServer) start() error {
74+
func (s *sessionRpcServer) start(stateless bool,
75+
lndClient *lndclient.LndServices) error {
76+
77+
var err error
78+
s.macaroonService, err = lndclient.NewMacaroonService(
79+
&lndclient.MacaroonServiceConfig{
80+
DBPath: s.cfg.dbDir,
81+
MacaroonLocation: "litd",
82+
StatelessInit: stateless,
83+
RequiredPerms: litPermissions,
84+
LndClient: lndClient,
85+
EphemeralKey: lndclient.SharedKeyNUMS,
86+
KeyLocator: lndclient.SharedKeyLocator,
87+
MacaroonPath: s.cfg.macaroonPath,
88+
},
89+
)
90+
if err != nil {
91+
log.Errorf("Could not create a new macaroon service: %v", err)
92+
return err
93+
}
94+
95+
if err := s.macaroonService.Start(); err != nil {
96+
log.Errorf("Could not start macaroon service: %v", err)
97+
return err
98+
}
99+
72100
// Start up all previously created sessions.
73101
sessions, err := s.db.ListSessions()
74102
if err != nil {
@@ -93,6 +121,11 @@ func (s *sessionRpcServer) stop() error {
93121
}
94122
s.sessionServer.Stop()
95123

124+
if err := s.macaroonService.Stop(); err != nil {
125+
log.Errorf("Error stopping macaroon service: %v", err)
126+
returnErr = err
127+
}
128+
96129
close(s.quit)
97130
s.wg.Wait()
98131
})

terminal.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ func (g *LightningTerminal) Run() error {
193193
g.cfg, g, getAllMethodPermissions(), bufRpcListener,
194194
)
195195
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
196-
basicAuth: g.rpcProxy.basicAuth,
197-
dbDir: path.Join(g.cfg.LitDir, g.cfg.Network),
196+
basicAuth: g.rpcProxy.basicAuth,
197+
macaroonPath: g.cfg.MacaroonPath,
198+
dbDir: path.Join(g.cfg.LitDir, g.cfg.Network),
198199
grpcOptions: []grpc.ServerOption{
199200
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
200201
grpc.UnknownServiceHandler(
@@ -529,7 +530,10 @@ func (g *LightningTerminal) startSubservers() error {
529530
g.poolStarted = true
530531
}
531532

532-
if err = g.sessionRpcServer.start(); err != nil {
533+
err = g.sessionRpcServer.start(
534+
!createDefaultMacaroons, &g.lndClient.LndServices,
535+
)
536+
if err != nil {
533537
return err
534538
}
535539
g.sessionRpcServerStarted = true

0 commit comments

Comments
 (0)