1
1
package perms
2
2
3
3
import (
4
- "fmt"
5
4
"regexp"
6
5
"strings"
7
6
"sync"
8
7
9
- faraday "github.com/lightninglabs/faraday/frdrpcserver/perms"
10
- loop "github.com/lightninglabs/loop/loopd/perms"
11
- pool "github.com/lightninglabs/pool/perms"
12
8
"github.com/lightningnetwork/lnd"
13
9
"github.com/lightningnetwork/lnd/lnrpc"
14
10
"gopkg.in/macaroon-bakery.v2/bakery"
15
11
)
16
12
17
13
const (
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"
23
16
)
24
17
25
18
// Manager manages the permission lists that Lit requires.
@@ -54,9 +47,6 @@ type Manager struct {
54
47
// was compiled with and then only the corresponding permissions will be added.
55
48
func NewManager (withAllSubServers bool ) (* Manager , error ) {
56
49
permissions := make (map [string ]map [string ][]bakery.Op )
57
- permissions [faradayPerms ] = faraday .RequiredPermissions
58
- permissions [loopPerms ] = loop .RequiredPermissions
59
- permissions [poolPerms ] = pool .RequiredPermissions
60
50
permissions [litPerms ] = RequiredPermissions
61
51
permissions [lndPerms ] = lnd .MainRPCServerPermissions ()
62
52
for k , v := range whiteListedLNDMethods {
@@ -106,6 +96,21 @@ func NewManager(withAllSubServers bool) (*Manager, error) {
106
96
}, nil
107
97
}
108
98
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
+
109
114
// OnLNDBuildTags should be called once a list of LND build tags has been
110
115
// obtained. It then uses those build tags to decide which of the LND sub-server
111
116
// permissions to add to the main permissions list. This method should only
@@ -225,50 +230,19 @@ func (pm *Manager) ActivePermissions(readOnly bool) []bakery.Op {
225
230
// _except_ for any LND permissions. In other words, this returns permissions
226
231
// for which the external validator of Lit is responsible.
227
232
func (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
+ }
231
238
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
+ }
244
242
}
245
243
return result
246
244
}
247
245
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
-
272
246
// IsSubServerURI if the given URI belongs to the RPC of the given server.
273
247
func (pm * Manager ) IsSubServerURI (name string , uri string ) bool {
274
248
if name == lndPerms {
@@ -292,27 +266,3 @@ func (pm *Manager) isLndURI(uri string) bool {
292
266
_ , lndCall := pm.fixedPerms [lndPerms ][uri ]
293
267
return lndCall || lndSubServerCall
294
268
}
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