Skip to content

Commit 6d03a8c

Browse files
committed
itest: add super macaroon test
1 parent fcb4e2a commit 6d03a8c

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

itest/litd_mode_integrated_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010
"fmt"
1111
"io/ioutil"
1212
"net/http"
13+
"os"
1314
"strings"
1415
"testing"
1516
"time"
1617

1718
"github.com/btcsuite/btcutil"
1819
"github.com/lightninglabs/faraday/frdrpc"
20+
terminal "github.com/lightninglabs/lightning-terminal"
1921
"github.com/lightninglabs/lightning-terminal/litrpc"
22+
"github.com/lightninglabs/lightning-terminal/session"
2023
"github.com/lightninglabs/loop/looprpc"
2124
"github.com/lightninglabs/pool/poolrpc"
2225
"github.com/lightningnetwork/lnd/lnrpc"
@@ -283,6 +286,46 @@ func testModeIntegrated(net *NetworkHarness, t *harnessTest) {
283286
})
284287
}
285288
})
289+
290+
t.t.Run("gRPC super macaroon auth check", func(tt *testing.T) {
291+
cfg := net.Alice.Cfg
292+
293+
superMacFile, err := bakeSuperMacaroon(cfg, true)
294+
require.NoError(tt, err)
295+
296+
defer func() {
297+
_ = os.Remove(superMacFile)
298+
}()
299+
300+
for _, endpoint := range endpoints {
301+
endpoint := endpoint
302+
tt.Run(endpoint.name+" lnd port", func(ttt *testing.T) {
303+
if !endpoint.supportsMacAuthOnLndPort {
304+
return
305+
}
306+
307+
runGRPCAuthTest(
308+
ttt, cfg.RPCAddr(), cfg.TLSCertPath,
309+
superMacFile,
310+
endpoint.requestFn,
311+
endpoint.successPattern,
312+
)
313+
})
314+
315+
tt.Run(endpoint.name+" lit port", func(ttt *testing.T) {
316+
if !endpoint.supportsMacAuthOnLitPort {
317+
return
318+
}
319+
320+
runGRPCAuthTest(
321+
ttt, cfg.LitAddr(), cfg.TLSCertPath,
322+
superMacFile,
323+
endpoint.requestFn,
324+
endpoint.successPattern,
325+
)
326+
})
327+
}
328+
})
286329
}
287330

288331
// runCertificateCheck checks that the TLS certificates presented to clients are
@@ -601,3 +644,51 @@ func connectRPC(ctx context.Context, hostPort,
601644

602645
return grpc.DialContext(ctx, hostPort, opts...)
603646
}
647+
648+
func bakeSuperMacaroon(cfg *LitNodeConfig, readOnly bool) (string, error) {
649+
lndAdminMac := lndMacaroonFn(cfg)
650+
651+
ctxb := context.Background()
652+
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
653+
defer cancel()
654+
655+
rawConn, err := connectRPC(ctxt, cfg.RPCAddr(), cfg.TLSCertPath)
656+
if err != nil {
657+
return "", err
658+
}
659+
660+
lndAdminMacBytes, err := ioutil.ReadFile(lndAdminMac)
661+
if err != nil {
662+
return "", err
663+
}
664+
lndAdminCtx := macaroonContext(ctxt, lndAdminMacBytes)
665+
lndConn := lnrpc.NewLightningClient(rawConn)
666+
667+
superMacPermissions := terminal.GetAllPermissions(readOnly)
668+
nullID := [4]byte{}
669+
superMacHex, err := terminal.BakeSuperMacaroon(
670+
lndAdminCtx, lndConn, session.NewSuperMacaroonRootKeyID(nullID),
671+
superMacPermissions, nil,
672+
)
673+
if err != nil {
674+
return "", err
675+
}
676+
677+
// The BakeSuperMacaroon function just hex encoded the macaroon, we know
678+
// it's valid.
679+
superMacBytes, _ := hex.DecodeString(superMacHex)
680+
681+
tempFile, err := ioutil.TempFile("", "lit-super-macaroon")
682+
if err != nil {
683+
_ = os.Remove(tempFile.Name())
684+
return "", err
685+
}
686+
687+
err = ioutil.WriteFile(tempFile.Name(), superMacBytes, 0644)
688+
if err != nil {
689+
_ = os.Remove(tempFile.Name())
690+
return "", err
691+
}
692+
693+
return tempFile.Name(), nil
694+
}

itest/litd_mode_remote_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package itest
22

33
import (
44
"context"
5+
"os"
56
"testing"
67

78
"github.com/btcsuite/btcutil"
@@ -90,4 +91,31 @@ func testModeRemote(net *NetworkHarness, t *harnessTest) {
9091
})
9192
}
9293
})
94+
95+
t.t.Run("gRPC super macaroon auth check", func(tt *testing.T) {
96+
cfg := net.Bob.Cfg
97+
98+
superMacFile, err := bakeSuperMacaroon(cfg, true)
99+
require.NoError(tt, err)
100+
101+
defer func() {
102+
_ = os.Remove(superMacFile)
103+
}()
104+
105+
for _, endpoint := range endpoints {
106+
endpoint := endpoint
107+
tt.Run(endpoint.name+" lit port", func(ttt *testing.T) {
108+
if !endpoint.supportsMacAuthOnLitPort {
109+
return
110+
}
111+
112+
runGRPCAuthTest(
113+
ttt, cfg.LitAddr(), cfg.LitTLSCertPath,
114+
superMacFile,
115+
endpoint.requestFn,
116+
endpoint.successPattern,
117+
)
118+
})
119+
}
120+
})
93121
}

0 commit comments

Comments
 (0)