@@ -12,6 +12,7 @@ import (
12
12
"github.com/lightningnetwork/lnd/lnrpc"
13
13
"github.com/lightningnetwork/lnd/lntest"
14
14
"github.com/lightningnetwork/lnd/lntest/node"
15
+ "github.com/lightningnetwork/lnd/lntest/wait"
15
16
"github.com/lightningnetwork/lnd/macaroons"
16
17
"github.com/stretchr/testify/assert"
17
18
"github.com/stretchr/testify/require"
@@ -316,6 +317,77 @@ func testMacaroonAuthentication(ht *lntest.HarnessTest) {
316
317
require .Error (t , err )
317
318
require .Contains (t , err .Error (), "permission denied" )
318
319
},
320
+ }, {
321
+ // Check that with the CheckMacaroonPermissions RPC, we can
322
+ // check that a macaroon follows the permissions of a given
323
+ // method.
324
+ name : "default permissions from full method" ,
325
+ run : func (ctxt context.Context , t * testing.T ) {
326
+ // We test that the macaroon of the test client has
327
+ // all the permissions for calling the BakeMacaroon RPC.
328
+ mac , err := testNode .ReadMacaroon (
329
+ testNode .Cfg .AdminMacPath , wait .DefaultTimeout ,
330
+ )
331
+ require .NoError (t , err )
332
+
333
+ macBytes , err := mac .MarshalBinary ()
334
+ require .NoError (t , err )
335
+
336
+ rpcURI := "/lnrpc.Lightning/BakeMacaroon"
337
+ checkReq := & lnrpc.CheckMacPermRequest {
338
+ Macaroon : macBytes ,
339
+ FullMethod : rpcURI ,
340
+ CheckDefaultPermsFromFullMethod : true ,
341
+ }
342
+
343
+ // Test that CheckMacaroonPermissions accurately
344
+ // characterizes macaroon as valid, since the admin
345
+ // macaroon should have all the permissions.
346
+ checkResp , err := testClient .CheckMacaroonPermissions (
347
+ ctxt , checkReq ,
348
+ )
349
+ require .NoError (t , err )
350
+ require .Equal (t , checkResp .Valid , true )
351
+
352
+ // Check different error cases.
353
+ dummy := []* lnrpc.MacaroonPermission {{
354
+ Entity : "foo" ,
355
+ }}
356
+ _ , err = testClient .CheckMacaroonPermissions (
357
+ ctxt , & lnrpc.CheckMacPermRequest {
358
+ Permissions : dummy ,
359
+ CheckDefaultPermsFromFullMethod : true ,
360
+ },
361
+ )
362
+ require .ErrorContains (
363
+ t , err , "cannot check default permissions " +
364
+ "from full method and from provided " +
365
+ "permission list at the same time" ,
366
+ )
367
+
368
+ _ , err = testClient .CheckMacaroonPermissions (
369
+ ctxt , & lnrpc.CheckMacPermRequest {
370
+ FullMethod : "" ,
371
+ CheckDefaultPermsFromFullMethod : true ,
372
+ },
373
+ )
374
+ require .ErrorContains (
375
+ t , err , "cannot check default permissions " +
376
+ "from full method without providing " +
377
+ "the full method name" ,
378
+ )
379
+
380
+ _ , err = testClient .CheckMacaroonPermissions (
381
+ ctxt , & lnrpc.CheckMacPermRequest {
382
+ FullMethod : "baz" ,
383
+ CheckDefaultPermsFromFullMethod : true ,
384
+ },
385
+ )
386
+ require .ErrorContains (
387
+ t , err , "no permissions found for full method " +
388
+ "baz" ,
389
+ )
390
+ },
319
391
}}
320
392
321
393
for _ , tc := range testCases {
0 commit comments