Skip to content

Commit 23d3992

Browse files
authored
Add policy support for warrants (#13)
* Add support for context in access checks * Add policy support for creating / deleting warrants * Add tests for creating, checking, and deleting a warrant with a policy * Fix lint errors * Only pass policy if not empty when creating / deleting warrants
1 parent 55544fe commit 23d3992

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

examples/example.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@
167167
result = warrant.Authz.check("permission", "permission1", "member", user1_subject)
168168
print(f"Does [{user1.id}] have the [permission1] permission? (should be true) -> {result}")
169169

170+
# Create, check, and delete warrant with a policy
171+
test_user_subject = warrant.Subject("user", "test-user")
172+
warrant.Warrant.create("permission", "test-permission", "member", test_user_subject, "geo == 'us'")
173+
print("Manually assigned [test-permission] permission to test-user with the context [geo == 'us']")
174+
result = warrant.Authz.check("permission", "test-permission", "member", test_user_subject, {"geo": "us"})
175+
print(f"Does test-user have the [test-permission] permission with the following context [geo == 'us']? (should be true) -> {result}")
176+
result = warrant.Authz.check("permission", "test-permission", "member", test_user_subject, {"geo": "eu"})
177+
print(f"Does test-user have the [test-permission] permission with the following context [geo == 'eu']? (should be false) -> {result}")
178+
warrant.Warrant.delete("permission", "test-permission", "member", test_user_subject, "geo == 'us'")
179+
print("Manually removed [test-permission] permission from test-user with the context [geo == 'us']")
180+
170181
# Query warrants
171182
# warrants = warrant.Warrant.query(select="explicit warrants", for_clause="subject=user:"+user1.id, where="relation=member")
172183
# print("Query warrants results:")

warrant/authz.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Authz(APIResource):
66

77
@classmethod
8-
def check(cls, object_type, object_id, relation, subject):
8+
def check(cls, object_type, object_id, relation, subject, context={}):
99
warrantToCheck = {
1010
"objectType": object_type,
1111
"objectId": object_id,
@@ -14,7 +14,8 @@ def check(cls, object_type, object_id, relation, subject):
1414
"objectType": subject.object_type,
1515
"objectId": subject.object_id,
1616
"relation": subject.relation
17-
}
17+
},
18+
"context": context
1819
}
1920
payload = {
2021
"op": "anyOf",

warrant/warrant.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, obj):
2727
self.subject = obj["subject"]
2828

2929
@classmethod
30-
def create(cls, object_type, object_id, relation, subject):
30+
def create(cls, object_type, object_id, relation, subject, policy=""):
3131
payload = {
3232
"objectType": object_type,
3333
"objectId": object_id,
@@ -41,6 +41,8 @@ def create(cls, object_type, object_id, relation, subject):
4141
}
4242
else:
4343
raise WarrantException(msg="Invalid type for \'subject\'. Must be of type Subject")
44+
if policy != "":
45+
payload["policy"] = policy
4446
cls._post(uri="/v1/warrants", json=payload)
4547

4648
@classmethod
@@ -53,7 +55,7 @@ def query(cls, select, for_clause, where):
5355
return cls._get(uri="/v1/query", params=params, object_hook=Warrant.from_json)
5456

5557
@classmethod
56-
def delete(cls, object_type, object_id, relation, subject):
58+
def delete(cls, object_type, object_id, relation, subject, policy=""):
5759
payload = {
5860
"objectType": object_type,
5961
"objectId": object_id,
@@ -67,6 +69,8 @@ def delete(cls, object_type, object_id, relation, subject):
6769
}
6870
else:
6971
raise WarrantException(msg="Invalid type for \'subject\'. Must be of type Subject")
72+
if policy != "":
73+
payload["policy"] = policy
7074
cls._delete(uri="/v1/warrants", json=payload)
7175

7276
"""

0 commit comments

Comments
 (0)