Skip to content

Commit 8b8290a

Browse files
authored
Merge pull request #10030 from guggero/macaroon-docs
lnrpc+rpcserver: improve docs for CheckMacaroonPermissions
2 parents 5c62e90 + 6b1c852 commit 8b8290a

File tree

6 files changed

+736
-556
lines changed

6 files changed

+736
-556
lines changed

itest/lnd_macaroons_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/lightningnetwork/lnd/lnrpc"
1313
"github.com/lightningnetwork/lnd/lntest"
1414
"github.com/lightningnetwork/lnd/lntest/node"
15+
"github.com/lightningnetwork/lnd/lntest/wait"
1516
"github.com/lightningnetwork/lnd/macaroons"
1617
"github.com/stretchr/testify/assert"
1718
"github.com/stretchr/testify/require"
@@ -316,6 +317,77 @@ func testMacaroonAuthentication(ht *lntest.HarnessTest) {
316317
require.Error(t, err)
317318
require.Contains(t, err.Error(), "permission denied")
318319
},
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+
},
319391
}}
320392

321393
for _, tc := range testCases {

0 commit comments

Comments
 (0)