Skip to content

Commit

Permalink
fix: add more example of globmatch and add unit test for it (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotoxin authored Jun 26, 2022
1 parent 4f3e71f commit 42d65d2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/globmatch_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && globMatch(r.obj, p.obj) && regexMatch(r.act, p.act)
5 changes: 5 additions & 0 deletions examples/globmatch_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
p, alice, /alice_data/*, GET
p, alice, /alice_data/???, POST

p, bob, /alice_data/[1-9], GET
p, bob, /bob_data/[!1-9], POST
13 changes: 13 additions & 0 deletions tests/test_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ def custom_function(key1, key2):
self.assertFalse(e.enforce("alice", "/alice_data2/myid", "GET"))
self.assertTrue(e.enforce("alice", "/alice_data2/myid/using/res_id", "GET"))

def test_enforce_glob_match(self):
e = self.get_enforcer(
get_examples("globmatch_model.conf"),
get_examples("globmatch_policy.csv"),
)

self.assertTrue(e.enforce("alice", "/alice_data/test_all", "GET"))
self.assertTrue(e.enforce("alice", "/alice_data/123", "POST"))
self.assertTrue(e.enforce("bob", "/alice_data/1", "GET"))
self.assertFalse(e.enforce("bob", "/alice_data/0", "GET"))
self.assertTrue(e.enforce("bob", "/bob_data/0", "POST"))
self.assertFalse(e.enforce("bob", "/bob_data/1", "POST"))

def test_enforce_priority(self):
e = self.get_enforcer(get_examples("priority_model.conf"), get_examples("priority_policy.csv"))
self.assertTrue(e.enforce("alice", "data1", "read"))
Expand Down

0 comments on commit 42d65d2

Please sign in to comment.