Skip to content

Commit 291d4ec

Browse files
committed
wip: completed
1 parent 3a3235b commit 291d4ec

File tree

3 files changed

+199
-225
lines changed

3 files changed

+199
-225
lines changed

itest/litd_node.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/btcsuite/btcd/chaincfg/chainhash"
2525
"github.com/btcsuite/btcd/wire"
2626
"github.com/lightninglabs/faraday/frdrpc"
27+
"github.com/lightninglabs/lightning-terminal/litrpc"
2728
"github.com/lightninglabs/loop/looprpc"
2829
"github.com/lightninglabs/pool/poolrpc"
2930
"github.com/lightningnetwork/lnd/lnrpc"
@@ -228,9 +229,13 @@ type HarnessNode struct {
228229
// methods SignMessage and VerifyMessage.
229230
SignerClient signrpc.SignerClient
230231

231-
// conn is the underlying connection to the grpc endpoint of the node.
232+
// conn is the underlying connection to the lnd grpc endpoint of the
233+
// node.
232234
conn *grpc.ClientConn
233235

236+
// litConn is the underlying connection to Lit's grpc endpoint.
237+
litConn *grpc.ClientConn
238+
234239
// RouterClient, WalletKitClient, WatchtowerClient cannot be embedded,
235240
// because a name collision would occur with LightningClient.
236241
RouterClient routerrpc.RouterClient
@@ -596,13 +601,24 @@ func (hn *HarnessNode) start(litdBinary string, litdError chan<- error,
596601
return nil
597602
}
598603

604+
// Also connect to Lit's RPC port for any Litd specific calls.
605+
litConn, err := connectLitRPC(
606+
context.Background(), hn.Cfg.LitAddr(), hn.Cfg.LitTLSCertPath,
607+
hn.Cfg.LitMacPath,
608+
)
609+
if err != nil {
610+
return err
611+
}
612+
hn.litConn = litConn
613+
599614
return hn.initLightningClient(conn)
600615
}
601616

602617
// WaitUntilStarted waits until the wallet state flips from "WAITING_TO_START".
603618
func (hn *HarnessNode) WaitUntilStarted(conn grpc.ClientConnInterface,
604619
timeout time.Duration) error {
605620

621+
// First wait for Litd state server to show that LND has started.
606622
err := hn.waitForState(conn, timeout, func(s lnrpc.WalletState) bool {
607623
return s >= lnrpc.WalletState_SERVER_ACTIVE
608624
})
@@ -1106,6 +1122,21 @@ func (hn *HarnessNode) stop() error {
11061122
}
11071123
}
11081124

1125+
// If lit is running in remote mode, then calling LNDs StopDaemon
1126+
// method will not shut down Lit, and so we need to explicitly request
1127+
// lit to shut down.
1128+
if hn.Cfg.RemoteMode {
1129+
ctx, cancel := context.WithTimeout(
1130+
context.Background(), lntest.DefaultTimeout,
1131+
)
1132+
litConn := litrpc.NewLitServiceClient(hn.litConn)
1133+
_, err := litConn.StopDaemon(ctx, &litrpc.StopDaemonRequest{})
1134+
cancel()
1135+
if err != nil {
1136+
return err
1137+
}
1138+
}
1139+
11091140
// Wait for lnd process and other goroutines to exit.
11101141
select {
11111142
case <-hn.processExit:
@@ -1800,3 +1831,40 @@ func (hn *HarnessNode) getChannelPolicies(include bool) policyUpdateMap {
18001831

18011832
return policyUpdates
18021833
}
1834+
1835+
// connectLigRPC can be used to connect to the lit rpc server.
1836+
func connectLitRPC(ctx context.Context, hostPort, tlsCertPath,
1837+
macPath string) (*grpc.ClientConn, error) {
1838+
1839+
tlsCreds, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
1840+
if err != nil {
1841+
return nil, err
1842+
}
1843+
1844+
opts := []grpc.DialOption{
1845+
grpc.WithBlock(),
1846+
grpc.WithTransportCredentials(tlsCreds),
1847+
}
1848+
1849+
if macPath != "" {
1850+
macBytes, err := ioutil.ReadFile(macPath)
1851+
if err != nil {
1852+
return nil, err
1853+
}
1854+
1855+
mac := &macaroon.Macaroon{}
1856+
if err = mac.UnmarshalBinary(macBytes); err != nil {
1857+
return nil, fmt.Errorf("error unmarshalling macaroon "+
1858+
"file: %v", err)
1859+
}
1860+
1861+
macCred, err := macaroons.NewMacaroonCredential(mac)
1862+
if err != nil {
1863+
return nil, fmt.Errorf("error cloning mac: %v", err)
1864+
}
1865+
1866+
opts = append(opts, grpc.WithPerRPCCredentials(macCred))
1867+
}
1868+
1869+
return grpc.DialContext(ctx, hostPort, opts...)
1870+
}

subservers/manager.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ var (
2727

2828
// Manager manages a set of subServer objects.
2929
type Manager struct {
30-
servers []*subServer
31-
mu sync.RWMutex
30+
servers []*subServer
31+
permsMgr *PermissionsMgr
32+
mu sync.RWMutex
3233
}
3334

3435
// NewManager constructs a new subServerMgr.
35-
func NewManager() *Manager {
36+
func NewManager(permsMgr *PermissionsMgr) *Manager {
3637
return &Manager{
37-
servers: []*subServer{},
38+
servers: []*subServer{},
39+
permsMgr: permsMgr,
3840
}
3941
}
4042

@@ -128,7 +130,9 @@ func (s *Manager) GetRemoteConn(uri string) (bool, *grpc.ClientConn) {
128130
defer s.mu.RUnlock()
129131

130132
for _, ss := range s.servers {
131-
// TODO(positiveblue): check subserver permissions.
133+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
134+
continue
135+
}
132136

133137
if !ss.Remote() {
134138
return false, nil
@@ -152,7 +156,9 @@ func (s *Manager) ValidateMacaroon(ctx context.Context,
152156
defer s.mu.RUnlock()
153157

154158
for _, ss := range s.servers {
155-
// TODO(positiveblue): check subserver permissions.
159+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
160+
continue
161+
}
156162

157163
if ss.Remote() {
158164
return true, nil
@@ -178,7 +184,13 @@ func (s *Manager) HandledBy(uri string) (bool, SubServerName) {
178184
s.mu.RLock()
179185
defer s.mu.RUnlock()
180186

181-
// TODO(positiveblue): check subserver permissions.
187+
for _, ss := range s.servers {
188+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
189+
continue
190+
}
191+
192+
return true, ss.Name()
193+
}
182194

183195
return false, ""
184196
}
@@ -190,7 +202,9 @@ func (s *Manager) MacaroonPath(uri string) (bool, string) {
190202
defer s.mu.RUnlock()
191203

192204
for _, ss := range s.servers {
193-
// TODO(positiveblue): check subserver permissions.
205+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
206+
continue
207+
}
194208

195209
if ss.Remote() {
196210
return true, ss.RemoteConfig().MacaroonPath
@@ -210,7 +224,9 @@ func (s *Manager) ReadRemoteMacaroon(uri string) (macaroonPath string) {
210224
defer s.mu.RUnlock()
211225

212226
for _, ss := range s.servers {
213-
// TODO(positiveblue): check subserver permissions.
227+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
228+
continue
229+
}
214230

215231
if !ss.Remote() {
216232
// return false, nil, nil

0 commit comments

Comments
 (0)