1- package terminal
1+ package perms
22
33import (
44 "net"
5+ "regexp"
56 "strings"
67 "sync"
78
@@ -30,9 +31,9 @@ import (
3031)
3132
3233var (
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
3435 // macaroon permissions to access the session service.
35- litPermissions = map [string ][]bakery.Op {
36+ LitPermissions = map [string ][]bakery.Op {
3637 "/litrpc.Sessions/AddSession" : {{
3738 Entity : "sessions" ,
3839 Action : "write" ,
@@ -93,15 +94,15 @@ const (
9394 lndPerms subServerName = "lnd"
9495)
9596
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 {
9899 // lndSubServerPerms is a map from LND subserver name to permissions
99100 // map. This is used once the manager receives a list of build tags
100101 // that LND has been compiled with so that the correct permissions can
101102 // be extracted based on subservers that LND has been compiled with.
102103 lndSubServerPerms map [string ]map [string ][]bakery.Op
103104
104- // fixedPerms is constructed once on creation of the PermissionsManager .
105+ // fixedPerms is constructed once on creation of the Manager .
105106 // It contains all the permissions that will not change throughout the
106107 // lifetime of the manager. It maps sub-server name to uri to permission
107108 // operations.
@@ -117,14 +118,14 @@ type PermissionsManager struct {
117118 permsMu sync.RWMutex
118119}
119120
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 ) {
123124 permissions := make (map [subServerName ]map [string ][]bakery.Op )
124125 permissions [faradayPerms ] = faraday .RequiredPermissions
125126 permissions [loopPerms ] = loop .RequiredPermissions
126127 permissions [poolPerms ] = pool .RequiredPermissions
127- permissions [litPerms ] = litPermissions
128+ permissions [litPerms ] = LitPermissions
128129 permissions [lndPerms ] = lnd .MainRPCServerPermissions ()
129130 for k , v := range whiteListedLNDMethods {
130131 permissions [lndPerms ][k ] = v
@@ -163,7 +164,7 @@ func NewPermissionsManager() (*PermissionsManager, error) {
163164 }
164165 }
165166
166- return & PermissionsManager {
167+ return & Manager {
167168 lndSubServerPerms : lndSubServerPerms ,
168169 fixedPerms : permissions ,
169170 perms : allPerms ,
@@ -174,7 +175,7 @@ func NewPermissionsManager() (*PermissionsManager, error) {
174175// obtained. It then uses those build tags to decide which of the LND sub-server
175176// permissions to add to the main permissions list. This method should only
176177// be called once.
177- func (pm * PermissionsManager ) OnLNDBuildTags (lndBuildTags []string ) {
178+ func (pm * Manager ) OnLNDBuildTags (lndBuildTags []string ) {
178179 pm .permsMu .Lock ()
179180 defer pm .permsMu .Unlock ()
180181
@@ -202,18 +203,52 @@ func (pm *PermissionsManager) OnLNDBuildTags(lndBuildTags []string) {
202203// URIPermissions returns a list of permission operations for the given URI if
203204// the uri is known to the manager. The second return parameter will be false
204205// 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 ) {
206207 pm .permsMu .RLock ()
207208 defer pm .permsMu .RUnlock ()
208209
209210 ops , ok := pm .perms [uri ]
210211 return ops , ok
211212}
212213
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+
213248// ActivePermissions returns all the available active permissions that the
214249// manager is aware of. Optionally, readOnly can be set to true if only the
215250// read-only permissions should be returned.
216- func (pm * PermissionsManager ) ActivePermissions (readOnly bool ) []bakery.Op {
251+ func (pm * Manager ) ActivePermissions (readOnly bool ) []bakery.Op {
217252 pm .permsMu .RLock ()
218253 defer pm .permsMu .RUnlock ()
219254
@@ -254,7 +289,7 @@ func (pm *PermissionsManager) ActivePermissions(readOnly bool) []bakery.Op {
254289// GetLitPerms returns a map of all permissions that the manager is aware of
255290// _except_ for any LND permissions. In other words, this returns permissions
256291// 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 {
258293 mapSize := len (pm .fixedPerms [litPerms ]) +
259294 len (pm .fixedPerms [faradayPerms ]) +
260295 len (pm .fixedPerms [loopPerms ]) + len (pm .fixedPerms [poolPerms ])
@@ -276,7 +311,7 @@ func (pm *PermissionsManager) GetLitPerms() map[string][]bakery.Op {
276311}
277312
278313// 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 {
280315 var lndSubServerCall bool
281316 for _ , subserverPermissions := range pm .lndSubServerPerms {
282317 _ , found := subserverPermissions [uri ]
@@ -290,25 +325,25 @@ func (pm *PermissionsManager) IsLndURI(uri string) bool {
290325}
291326
292327// 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 {
294329 _ , ok := pm.fixedPerms [loopPerms ][uri ]
295330 return ok
296331}
297332
298333// 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 {
300335 _ , ok := pm.fixedPerms [faradayPerms ][uri ]
301336 return ok
302337}
303338
304339// 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 {
306341 _ , ok := pm.fixedPerms [poolPerms ][uri ]
307342 return ok
308343}
309344
310345// 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 {
312347 _ , ok := pm.fixedPerms [litPerms ][uri ]
313348 return ok
314349}
0 commit comments