-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
With this commit, a user can now specify a regex when specifying custom permissions for an LNC session. This regex will be used to select permissions for URIs that match the regex.
- Loading branch information
1 parent
d41f796
commit 15cd1bd
Showing
4 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package perms | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/macaroon-bakery.v2/bakery" | ||
) | ||
|
||
// TestMatchRegexURI tests the behaviour of the MatchRegexURI method of the | ||
// Manager. | ||
func TestMatchRegexURI(t *testing.T) { | ||
// Construct a new Manager with a predefined list of perms. | ||
m := &Manager{ | ||
perms: map[string][]bakery.Op{ | ||
"/lnrpc.WalletUnlocker/GenSeed": {}, | ||
"/lnrpc.WalletUnlocker/InitWallet": {}, | ||
"/lnrpc.Lightning/SendCoins": {{ | ||
Entity: "onchain", | ||
Action: "write", | ||
}}, | ||
"/litrpc.Sessions/AddSession": {{ | ||
Entity: "sessions", | ||
Action: "write", | ||
}}, | ||
"/litrpc.Sessions/ListSessions": {{ | ||
Entity: "sessions", | ||
Action: "read", | ||
}}, | ||
"/litrpc.Sessions/RevokeSession": {{ | ||
Entity: "sessions", | ||
Action: "write", | ||
}}, | ||
}, | ||
} | ||
|
||
// Assert that a full URI is not considered a wild card. | ||
uris, isRegex := m.MatchRegexURI("/litrpc.Sessions/RevokeSession") | ||
require.False(t, isRegex) | ||
require.Empty(t, uris) | ||
|
||
// Assert that an invalid URI is also caught as such. | ||
uris, isRegex = m.MatchRegexURI("***") | ||
require.False(t, isRegex) | ||
require.Nil(t, uris) | ||
|
||
// Assert that the function correctly matches on a valid wild card for | ||
// litrpc URIs. | ||
uris, isRegex = m.MatchRegexURI("/litrpc.Sessions/.*") | ||
require.True(t, isRegex) | ||
require.ElementsMatch(t, uris, []string{ | ||
"/litrpc.Sessions/AddSession", | ||
"/litrpc.Sessions/ListSessions", | ||
"/litrpc.Sessions/RevokeSession", | ||
}) | ||
|
||
// Assert that the function correctly matches on a valid wild card for | ||
// lnd URIs. First we check that we can specify that only the | ||
// "WalletUnlocker" methods should be included. | ||
uris, isRegex = m.MatchRegexURI("/lnrpc.WalletUnlocker/.*") | ||
require.True(t, isRegex) | ||
require.ElementsMatch(t, uris, []string{ | ||
"/lnrpc.WalletUnlocker/GenSeed", | ||
"/lnrpc.WalletUnlocker/InitWallet", | ||
}) | ||
|
||
// Now we check that we can include all the `lnrpc` methods. | ||
uris, isRegex = m.MatchRegexURI("/lnrpc\\..*") | ||
require.True(t, isRegex) | ||
require.ElementsMatch(t, uris, []string{ | ||
"/lnrpc.WalletUnlocker/GenSeed", | ||
"/lnrpc.WalletUnlocker/InitWallet", | ||
"/lnrpc.Lightning/SendCoins", | ||
}) | ||
|
||
// Assert that the function does not return any URIs for a wild card | ||
// URI that does not match on any of its perms. | ||
uris, isRegex = m.MatchRegexURI("/poolrpc.Trader/.*") | ||
require.True(t, isRegex) | ||
require.Empty(t, uris) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters