@@ -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+ }
0 commit comments