Rpcserver: Add GetAllPermissions function for retrieving permissions for external macaroon baking

This commit is contained in:
Turtle 2021-09-22 14:32:02 -05:00
parent 57fc32fe6a
commit 218fa1e43e
No known key found for this signature in database
GPG Key ID: 3E325201FFD68EC0
2 changed files with 50 additions and 0 deletions

View File

@ -258,6 +258,42 @@ func calculateFeeRate(satPerByte, satPerVByte uint64, targetConf uint32,
}
// GetAllPermissions returns all the permissions required to interact with lnd.
func GetAllPermissions() []bakery.Op {
allPerms := make([]bakery.Op, 0)
// The map will help keep track of which specific permission pairs have
// already been added to the slice.
allPermsMap := make(map[string]map[string]struct{})
for _, perms := range MainRPCServerPermissions() {
for _, perm := range perms {
entity := perm.Entity
action := perm.Action
// If this specific entity-action permission pair isn't
// in the map yet. Add it to map, and the permission
// slice.
if acts, ok := allPermsMap[entity]; ok {
if _, ok := acts[action]; !ok {
allPermsMap[entity][action] = struct{}{}
allPerms = append(
allPerms, perm,
)
}
} else {
allPermsMap[entity] = make(map[string]struct{})
allPermsMap[entity][action] = struct{}{}
allPerms = append(allPerms, perm)
}
}
}
return allPerms
}
// MainRPCServerPermissions returns a mapping of the main RPC server calls to
// the permissions they require.
func MainRPCServerPermissions() map[string][]bakery.Op {

14
rpcserver_test.go Normal file
View File

@ -0,0 +1,14 @@
package lnd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetAllPermissions(t *testing.T) {
perms := GetAllPermissions()
// Currently there are there are 16 entity:action pairs in use.
assert.Equal(t, len(perms), 16)
}