1
- package terminal
1
+ package perms
2
2
3
3
import (
4
4
"net"
5
+ "regexp"
5
6
"strings"
6
7
"sync"
7
8
@@ -30,9 +31,9 @@ import (
30
31
)
31
32
32
33
var (
33
- // litPermissions is a map of all LiT RPC methods and their required
34
+ // LitPermissions is a map of all LiT RPC methods and their required
34
35
// macaroon permissions to access the session service.
35
- litPermissions = map [string ][]bakery.Op {
36
+ LitPermissions = map [string ][]bakery.Op {
36
37
"/litrpc.Sessions/AddSession" : {{
37
38
Entity : "sessions" ,
38
39
Action : "write" ,
@@ -93,15 +94,15 @@ const (
93
94
lndPerms subServerName = "lnd"
94
95
)
95
96
96
- // PermissionsManager manages the permission lists that Lit requires.
97
- type PermissionsManager struct {
97
+ // Manager manages the permission lists that Lit requires.
98
+ type Manager struct {
98
99
// lndSubServerPerms is a map from LND subserver name to permissions
99
100
// map. This is used once the manager receives a list of build tags
100
101
// that LND has been compiled with so that the correct permissions can
101
102
// be extracted based on subservers that LND has been compiled with.
102
103
lndSubServerPerms map [string ]map [string ][]bakery.Op
103
104
104
- // fixedPerms is constructed once on creation of the PermissionsManager .
105
+ // fixedPerms is constructed once on creation of the Manager .
105
106
// It contains all the permissions that will not change throughout the
106
107
// lifetime of the manager. It maps sub-server name to uri to permission
107
108
// operations.
@@ -117,14 +118,14 @@ type PermissionsManager struct {
117
118
permsMu sync.RWMutex
118
119
}
119
120
120
- // NewPermissionsManager constructs a new PermissionsManager instance and
121
- // collects any of the fixed permissions.
122
- func NewPermissionsManager () (* PermissionsManager , error ) {
121
+ // NewManager constructs a new Manager instance and collects any of the fixed
122
+ // permissions.
123
+ func NewManager () (* Manager , error ) {
123
124
permissions := make (map [subServerName ]map [string ][]bakery.Op )
124
125
permissions [faradayPerms ] = faraday .RequiredPermissions
125
126
permissions [loopPerms ] = loop .RequiredPermissions
126
127
permissions [poolPerms ] = pool .RequiredPermissions
127
- permissions [litPerms ] = litPermissions
128
+ permissions [litPerms ] = LitPermissions
128
129
permissions [lndPerms ] = lnd .MainRPCServerPermissions ()
129
130
for k , v := range whiteListedLNDMethods {
130
131
permissions [lndPerms ][k ] = v
@@ -163,7 +164,7 @@ func NewPermissionsManager() (*PermissionsManager, error) {
163
164
}
164
165
}
165
166
166
- return & PermissionsManager {
167
+ return & Manager {
167
168
lndSubServerPerms : lndSubServerPerms ,
168
169
fixedPerms : permissions ,
169
170
perms : allPerms ,
@@ -174,7 +175,7 @@ func NewPermissionsManager() (*PermissionsManager, error) {
174
175
// obtained. It then uses those build tags to decide which of the LND sub-server
175
176
// permissions to add to the main permissions list. This method should only
176
177
// be called once.
177
- func (pm * PermissionsManager ) OnLNDBuildTags (lndBuildTags []string ) {
178
+ func (pm * Manager ) OnLNDBuildTags (lndBuildTags []string ) {
178
179
pm .permsMu .Lock ()
179
180
defer pm .permsMu .Unlock ()
180
181
@@ -202,18 +203,52 @@ func (pm *PermissionsManager) OnLNDBuildTags(lndBuildTags []string) {
202
203
// URIPermissions returns a list of permission operations for the given URI if
203
204
// the uri is known to the manager. The second return parameter will be false
204
205
// if the URI is unknown to the manager.
205
- func (pm * PermissionsManager ) URIPermissions (uri string ) ([]bakery.Op , bool ) {
206
+ func (pm * Manager ) URIPermissions (uri string ) ([]bakery.Op , bool ) {
206
207
pm .permsMu .RLock ()
207
208
defer pm .permsMu .RUnlock ()
208
209
209
210
ops , ok := pm .perms [uri ]
210
211
return ops , ok
211
212
}
212
213
214
+ // MatchRegexURI first checks that the given URI is in fact a regex. If it is,
215
+ // then it is used to match on the perms that the manager has. The return values
216
+ // are a list of URIs that match the regex and the boolean represents whether
217
+ // the given uri is in fact a regex.
218
+ func (pm * Manager ) MatchRegexURI (uriRegex string ) ([]string , bool ) {
219
+ pm .permsMu .RLock ()
220
+ defer pm .permsMu .RUnlock ()
221
+
222
+ // If the given uri string is one of our permissions, then it is not
223
+ // a regex.
224
+ if _ , ok := pm .perms [uriRegex ]; ok {
225
+ return nil , false
226
+ }
227
+
228
+ // Construct the regex type from the given string.
229
+ r , err := regexp .Compile (uriRegex )
230
+ if err != nil {
231
+ return nil , false
232
+ }
233
+
234
+ // Iterate over the list of permissions and collect all permissions that
235
+ // match the given regex.
236
+ var matches []string
237
+ for uri := range pm .perms {
238
+ if ! r .MatchString (uri ) {
239
+ continue
240
+ }
241
+
242
+ matches = append (matches , uri )
243
+ }
244
+
245
+ return matches , true
246
+ }
247
+
213
248
// ActivePermissions returns all the available active permissions that the
214
249
// manager is aware of. Optionally, readOnly can be set to true if only the
215
250
// read-only permissions should be returned.
216
- func (pm * PermissionsManager ) ActivePermissions (readOnly bool ) []bakery.Op {
251
+ func (pm * Manager ) ActivePermissions (readOnly bool ) []bakery.Op {
217
252
pm .permsMu .RLock ()
218
253
defer pm .permsMu .RUnlock ()
219
254
@@ -254,7 +289,7 @@ func (pm *PermissionsManager) ActivePermissions(readOnly bool) []bakery.Op {
254
289
// GetLitPerms returns a map of all permissions that the manager is aware of
255
290
// _except_ for any LND permissions. In other words, this returns permissions
256
291
// for which the external validator of Lit is responsible.
257
- func (pm * PermissionsManager ) GetLitPerms () map [string ][]bakery.Op {
292
+ func (pm * Manager ) GetLitPerms () map [string ][]bakery.Op {
258
293
mapSize := len (pm .fixedPerms [litPerms ]) +
259
294
len (pm .fixedPerms [faradayPerms ]) +
260
295
len (pm .fixedPerms [loopPerms ]) + len (pm .fixedPerms [poolPerms ])
@@ -276,7 +311,7 @@ func (pm *PermissionsManager) GetLitPerms() map[string][]bakery.Op {
276
311
}
277
312
278
313
// IsLndURI returns true if the given URI belongs to an RPC of lnd.
279
- func (pm * PermissionsManager ) IsLndURI (uri string ) bool {
314
+ func (pm * Manager ) IsLndURI (uri string ) bool {
280
315
var lndSubServerCall bool
281
316
for _ , subserverPermissions := range pm .lndSubServerPerms {
282
317
_ , found := subserverPermissions [uri ]
@@ -290,25 +325,25 @@ func (pm *PermissionsManager) IsLndURI(uri string) bool {
290
325
}
291
326
292
327
// IsLoopURI returns true if the given URI belongs to an RPC of loopd.
293
- func (pm * PermissionsManager ) IsLoopURI (uri string ) bool {
328
+ func (pm * Manager ) IsLoopURI (uri string ) bool {
294
329
_ , ok := pm.fixedPerms [loopPerms ][uri ]
295
330
return ok
296
331
}
297
332
298
333
// IsFaradayURI returns true if the given URI belongs to an RPC of faraday.
299
- func (pm * PermissionsManager ) IsFaradayURI (uri string ) bool {
334
+ func (pm * Manager ) IsFaradayURI (uri string ) bool {
300
335
_ , ok := pm.fixedPerms [faradayPerms ][uri ]
301
336
return ok
302
337
}
303
338
304
339
// IsPoolURI returns true if the given URI belongs to an RPC of poold.
305
- func (pm * PermissionsManager ) IsPoolURI (uri string ) bool {
340
+ func (pm * Manager ) IsPoolURI (uri string ) bool {
306
341
_ , ok := pm.fixedPerms [poolPerms ][uri ]
307
342
return ok
308
343
}
309
344
310
345
// IsLitURI returns true if the given URI belongs to an RPC of LiT.
311
- func (pm * PermissionsManager ) IsLitURI (uri string ) bool {
346
+ func (pm * Manager ) IsLitURI (uri string ) bool {
312
347
_ , ok := pm.fixedPerms [litPerms ][uri ]
313
348
return ok
314
349
}
0 commit comments