Skip to content

Commit 218fa1e

Browse files
committed
Rpcserver: Add GetAllPermissions function for retrieving permissions for external macaroon baking
1 parent 57fc32f commit 218fa1e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

rpcserver.go

+36
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,42 @@ func calculateFeeRate(satPerByte, satPerVByte uint64, targetConf uint32,
258258

259259
}
260260

261+
// GetAllPermissions returns all the permissions required to interact with lnd.
262+
func GetAllPermissions() []bakery.Op {
263+
allPerms := make([]bakery.Op, 0)
264+
265+
// The map will help keep track of which specific permission pairs have
266+
// already been added to the slice.
267+
allPermsMap := make(map[string]map[string]struct{})
268+
269+
for _, perms := range MainRPCServerPermissions() {
270+
for _, perm := range perms {
271+
entity := perm.Entity
272+
action := perm.Action
273+
274+
// If this specific entity-action permission pair isn't
275+
// in the map yet. Add it to map, and the permission
276+
// slice.
277+
if acts, ok := allPermsMap[entity]; ok {
278+
if _, ok := acts[action]; !ok {
279+
allPermsMap[entity][action] = struct{}{}
280+
281+
allPerms = append(
282+
allPerms, perm,
283+
)
284+
}
285+
286+
} else {
287+
allPermsMap[entity] = make(map[string]struct{})
288+
allPermsMap[entity][action] = struct{}{}
289+
allPerms = append(allPerms, perm)
290+
}
291+
}
292+
}
293+
294+
return allPerms
295+
}
296+
261297
// MainRPCServerPermissions returns a mapping of the main RPC server calls to
262298
// the permissions they require.
263299
func MainRPCServerPermissions() map[string][]bakery.Op {

rpcserver_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package lnd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGetAllPermissions(t *testing.T) {
10+
perms := GetAllPermissions()
11+
12+
// Currently there are there are 16 entity:action pairs in use.
13+
assert.Equal(t, len(perms), 16)
14+
}

0 commit comments

Comments
 (0)