11package perms
22
33import (
4- "fmt"
54 "regexp"
65 "strings"
76 "sync"
87
9- faraday "github.com/lightninglabs/faraday/frdrpcserver/perms"
10- loop "github.com/lightninglabs/loop/loopd/perms"
11- pool "github.com/lightninglabs/pool/perms"
128 "github.com/lightningnetwork/lnd"
139 "github.com/lightningnetwork/lnd/lnrpc"
1410 "gopkg.in/macaroon-bakery.v2/bakery"
1511)
1612
1713const (
18- poolPerms string = "pool"
19- loopPerms string = "loop"
20- faradayPerms string = "faraday"
21- litPerms string = "lit"
22- lndPerms string = "lnd"
14+ litPerms string = "lit"
15+ lndPerms string = "lnd"
2316)
2417
2518// Manager manages the permission lists that Lit requires.
@@ -54,9 +47,6 @@ type Manager struct {
5447// was compiled with and then only the corresponding permissions will be added.
5548func NewManager (withAllSubServers bool ) (* Manager , error ) {
5649 permissions := make (map [string ]map [string ][]bakery.Op )
57- permissions [faradayPerms ] = faraday .RequiredPermissions
58- permissions [loopPerms ] = loop .RequiredPermissions
59- permissions [poolPerms ] = pool .RequiredPermissions
6050 permissions [litPerms ] = RequiredPermissions
6151 permissions [lndPerms ] = lnd .MainRPCServerPermissions ()
6252 for k , v := range whiteListedLNDMethods {
@@ -106,6 +96,21 @@ func NewManager(withAllSubServers bool) (*Manager, error) {
10696 }, nil
10797}
10898
99+ // RegisterSubServer adds the permissions of a given sub-server to the set
100+ // managed by the Manager.
101+ func (pm * Manager ) RegisterSubServer (name string ,
102+ permissions map [string ][]bakery.Op ) {
103+
104+ pm .permsMu .Lock ()
105+ defer pm .permsMu .Unlock ()
106+
107+ pm .fixedPerms [name ] = permissions
108+
109+ for uri , ops := range permissions {
110+ pm .perms [uri ] = ops
111+ }
112+ }
113+
109114// OnLNDBuildTags should be called once a list of LND build tags has been
110115// obtained. It then uses those build tags to decide which of the LND sub-server
111116// permissions to add to the main permissions list. This method should only
@@ -225,50 +230,19 @@ func (pm *Manager) ActivePermissions(readOnly bool) []bakery.Op {
225230// _except_ for any LND permissions. In other words, this returns permissions
226231// for which the external validator of Lit is responsible.
227232func (pm * Manager ) GetLitPerms () map [string ][]bakery.Op {
228- mapSize := len (pm .fixedPerms [litPerms ]) +
229- len (pm .fixedPerms [faradayPerms ]) +
230- len (pm .fixedPerms [loopPerms ]) + len (pm .fixedPerms [poolPerms ])
233+ result := make (map [string ][]bakery.Op )
234+ for subserver , ops := range pm .fixedPerms {
235+ if subserver == lndPerms {
236+ continue
237+ }
231238
232- result := make (map [string ][]bakery.Op , mapSize )
233- for key , value := range pm .fixedPerms [faradayPerms ] {
234- result [key ] = value
235- }
236- for key , value := range pm .fixedPerms [loopPerms ] {
237- result [key ] = value
238- }
239- for key , value := range pm .fixedPerms [poolPerms ] {
240- result [key ] = value
241- }
242- for key , value := range pm .fixedPerms [litPerms ] {
243- result [key ] = value
239+ for key , value := range ops {
240+ result [key ] = value
241+ }
244242 }
245243 return result
246244}
247245
248- // SubServerHandler returns the name of the subserver that should handle the
249- // given URI.
250- func (pm * Manager ) SubServerHandler (uri string ) (string , error ) {
251- switch {
252- case pm .IsSubServerURI (lndPerms , uri ):
253- return lndPerms , nil
254-
255- case pm .IsSubServerURI (faradayPerms , uri ):
256- return faradayPerms , nil
257-
258- case pm .IsSubServerURI (loopPerms , uri ):
259- return loopPerms , nil
260-
261- case pm .IsSubServerURI (poolPerms , uri ):
262- return poolPerms , nil
263-
264- case pm .IsSubServerURI (litPerms , uri ):
265- return litPerms , nil
266-
267- default :
268- return "" , fmt .Errorf ("unknown gRPC web request: %v" , uri )
269- }
270- }
271-
272246// IsSubServerURI if the given URI belongs to the RPC of the given server.
273247func (pm * Manager ) IsSubServerURI (name string , uri string ) bool {
274248 if name == lndPerms {
@@ -292,27 +266,3 @@ func (pm *Manager) isLndURI(uri string) bool {
292266 _ , lndCall := pm.fixedPerms [lndPerms ][uri ]
293267 return lndCall || lndSubServerCall
294268}
295-
296- // IsLoopURI returns true if the given URI belongs to an RPC of loopd.
297- func (pm * Manager ) IsLoopURI (uri string ) bool {
298- _ , ok := pm.fixedPerms [loopPerms ][uri ]
299- return ok
300- }
301-
302- // IsFaradayURI returns true if the given URI belongs to an RPC of faraday.
303- func (pm * Manager ) IsFaradayURI (uri string ) bool {
304- _ , ok := pm.fixedPerms [faradayPerms ][uri ]
305- return ok
306- }
307-
308- // IsPoolURI returns true if the given URI belongs to an RPC of poold.
309- func (pm * Manager ) IsPoolURI (uri string ) bool {
310- _ , ok := pm.fixedPerms [poolPerms ][uri ]
311- return ok
312- }
313-
314- // IsLitURI returns true if the given URI belongs to an RPC of LiT.
315- func (pm * Manager ) IsLitURI (uri string ) bool {
316- _ , ok := pm.fixedPerms [litPerms ][uri ]
317- return ok
318- }
0 commit comments