From 8b58c103ce3b82c532bb84bb26b0f372bb43bef4 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 17 Nov 2022 16:50:49 +0100 Subject: [PATCH] terminal: use account RPC server --- perms/permissions.go | 16 +++++++++++ terminal.go | 65 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/perms/permissions.go b/perms/permissions.go index 51d2fa12..f4565f7d 100644 --- a/perms/permissions.go +++ b/perms/permissions.go @@ -46,6 +46,22 @@ var ( Entity: "sessions", Action: "write", }}, + "/litrpc.Accounts/CreateAccount": {{ + Entity: "account", + Action: "write", + }}, + "/litrpc.Accounts/UpdateAccount": {{ + Entity: "account", + Action: "write", + }}, + "/litrpc.Accounts/ListAccounts": {{ + Entity: "account", + Action: "read", + }}, + "/litrpc.Accounts/RemoveAccount": {{ + Entity: "account", + Action: "write", + }}, } // whiteListedLNDMethods is a map of all lnd RPC methods that don't diff --git a/terminal.go b/terminal.go index ee693f88..96238c3f 100644 --- a/terminal.go +++ b/terminal.go @@ -21,6 +21,7 @@ import ( "github.com/jessevdk/go-flags" "github.com/lightninglabs/faraday/frdrpc" "github.com/lightninglabs/faraday/frdrpcserver" + "github.com/lightninglabs/lightning-terminal/accounts" "github.com/lightninglabs/lightning-terminal/litrpc" "github.com/lightninglabs/lightning-terminal/perms" "github.com/lightninglabs/lightning-terminal/queue" @@ -170,6 +171,11 @@ type LightningTerminal struct { middleware *mid.Manager middlewareStarted bool + accountService *accounts.InterceptorService + accountServiceStarted bool + + accountRpcServer *accounts.RPCServer + restHandler http.Handler restCancel func() } @@ -220,6 +226,25 @@ func (g *LightningTerminal) Run() error { g.rpcProxy = newRpcProxy( g.cfg, g, g.validateSuperMacaroon, g.permsMgr, bufRpcListener, ) + g.accountService, err = accounts.NewService( + filepath.Dir(g.cfg.MacaroonPath), g.errQueue.ChanIn(), + ) + if err != nil { + return fmt.Errorf("error creating account service: %v", err) + } + + superMacBaker := func(ctx context.Context, rootKeyID uint64, + recipe *session.MacaroonRecipe) (string, error) { + + return BakeSuperMacaroon( + ctx, g.basicClient, rootKeyID, + recipe.Permissions, recipe.Caveats, + ) + } + + g.accountRpcServer = accounts.NewRPCServer( + g.accountService, superMacBaker, + ) g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{ basicAuth: g.rpcProxy.basicAuth, dbDir: filepath.Join(g.cfg.LitDir, g.cfg.Network), @@ -241,14 +266,7 @@ func (g *LightningTerminal) Run() error { registerGrpcServers: func(server *grpc.Server) { g.registerSubDaemonGrpcServers(server, false) }, - superMacBaker: func(ctx context.Context, rootKeyID uint64, - recipe *session.MacaroonRecipe) (string, error) { - - return BakeSuperMacaroon( - ctx, g.basicClient, rootKeyID, - recipe.Permissions, recipe.Caveats, - ) - }, + superMacBaker: superMacBaker, firstConnectionDeadline: g.cfg.FirstLNCConnDeadline, permMgr: g.permsMgr, }) @@ -457,6 +475,7 @@ func (g *LightningTerminal) startSubservers() error { // basic client connection to find out if the RPC server is started yet // because that doesn't do anything else than just connect. We'll check // if lnd is also ready to be used in the next step. + log.Infof("Connecting basic lnd client") err := wait.NoError(func() error { // Create an lnd client now that we have the full configuration. // We'll need a basic client and a full client because not all @@ -496,6 +515,7 @@ func (g *LightningTerminal) startSubservers() error { } }() + log.Infof("Connecting full lnd client") g.lndClient, err = lndclient.NewLndServices( &lndclient.LndServicesConfig{ LndAddress: host, @@ -524,6 +544,7 @@ func (g *LightningTerminal) startSubservers() error { if g.cfg.LndMode == ModeIntegrated { // Create a super macaroon that can be used to control lnd, // faraday, loop, and pool, all at the same time. + log.Infof("Baking internal super macaroon") ctx := context.Background() superMacaroon, err := BakeSuperMacaroon( ctx, g.basicClient, session.NewSuperMacaroonRootKeyID( @@ -556,6 +577,7 @@ func (g *LightningTerminal) startSubservers() error { // Both connection types are ready now, let's start our subservers if // they should be started locally as an integrated service. if !g.cfg.faradayRemote { + log.Infof("Starting integrated faraday daemon") err = g.faradayServer.StartAsSubserver( g.lndClient.LndServices, createDefaultMacaroons, ) @@ -566,6 +588,7 @@ func (g *LightningTerminal) startSubservers() error { } if !g.cfg.loopRemote { + log.Infof("Starting integrated loop daemon") err = g.loopServer.StartAsSubserver( g.lndClient, createDefaultMacaroons, ) @@ -576,6 +599,7 @@ func (g *LightningTerminal) startSubservers() error { } if !g.cfg.poolRemote { + log.Infof("Starting integrated pool daemon") err = g.poolServer.StartAsSubserver( g.basicClient, g.lndClient, createDefaultMacaroons, ) @@ -585,6 +609,7 @@ func (g *LightningTerminal) startSubservers() error { g.poolStarted = true } + log.Infof("Starting LiT macaroon service") g.macaroonService, err = lndclient.NewMacaroonService( &lndclient.MacaroonServiceConfig{ DBPath: filepath.Join(g.cfg.LitDir, g.cfg.Network), @@ -607,16 +632,30 @@ func (g *LightningTerminal) startSubservers() error { } g.macaroonServiceStarted = true + log.Infof("Starting LiT session server") if err = g.sessionRpcServer.start(); err != nil { return err } g.sessionRpcServerStarted = true if !g.cfg.RPCMiddleware.Disabled { + log.Infof("Starting LiT account service") + err := g.accountService.Start( + g.lndClient.Client, g.lndClient.Router, + g.lndClient.ChainParams, + ) + if err != nil { + return fmt.Errorf("error starting account service: %v", + err) + } + g.accountServiceStarted = true + // Start the middleware manager. + log.Infof("Starting LiT middleware manager") g.middleware = mid.NewManager( g.cfg.RPCMiddleware.InterceptTimeout, g.lndClient.Client, g.errQueue.ChanIn(), + g.accountService, ) if err = g.middleware.Start(); err != nil { @@ -625,6 +664,8 @@ func (g *LightningTerminal) startSubservers() error { g.middlewareStarted = true } + log.Infof("Internal sub server startup complete") + return nil } @@ -672,6 +713,7 @@ func (g *LightningTerminal) registerSubDaemonGrpcServers(server *grpc.Server, if withLitRPC { litrpc.RegisterSessionsServer(server, g.sessionRpcServer) + litrpc.RegisterAccountsServer(server, g.accountRpcServer) } } @@ -913,6 +955,13 @@ func (g *LightningTerminal) shutdown() error { } } + if g.accountServiceStarted { + if err := g.accountService.Stop(); err != nil { + log.Errorf("Error stopping account service: %v", err) + returnErr = err + } + } + if g.middlewareStarted { g.middleware.Stop() }