-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_adapter.py
253 lines (214 loc) · 8.6 KB
/
test_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Tests for `adapter.py` module."""
from unittest import TestCase
from casbin import Enforcer, Model
from pathlib import Path
from pymongo import MongoClient
from typing import List, Tuple
from foca.security.access_control.foca_casbin_adapter.adapter import Adapter
# Constants
DIR = Path(__file__).parent / "test_files"
MODEL_CONF_FILE = str(DIR / "rbac_model.conf")
MODEL_ROLES_CONF_FILE = str(DIR / "rbac_with_resources_roles.conf")
TEST_POLICIES_MODEL_CONF = [
("p", "p", ["alice", "data1", "read"]),
("p", "p", ["bob", "data2", "write"]),
("p", "p", ["data2_admin", "data2", "read"]),
("p", "p", ["data2_admin", "data2", "write"]),
("g", "g", ["alice", "data2_admin"])
]
TEST_POLICIES_MODEL_ROLES_CONF = [
("p", "p", ["alice", "data1", "write"]),
("p", "p", ["alice", "data1", "read"]),
("p", "p", ["bob", "data2", "read"]),
("p", "p", ["data_group_admin", "data_group", "write"]),
("g", "g", ["alice", "data_group_admin"]),
("g", "g2", ["data2", "data_group"])
]
class TestAdapter(TestCase):
"""Class to test adapter configuration."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.db_name = "casbin_test"
self.db_port = 12345
def setUp(self):
self.clear_db()
def tearDown(self):
self.clear_db()
def save_policies(
self,
adapter: Adapter,
model: Model,
policy: Tuple
) -> None:
"""Helper function for adding policy to a given model.
Args:
adapter: Casbin mongodb adapter.
model: Casbin model configuration.
policy: New policy to be added.
Attributes:
adapter: Casbin mongodb adapter.
model: Casbin model configuration.
policy: New policy to be added.
"""
model.clear_policy()
model.add_policy(*policy)
adapter.save_policy(model)
def get_enforcer(
self,
conf_file: str,
policies: List
) -> Enforcer:
"""Helper function to register policy enforcer.
Args:
conf_file: Policy model configuration file path.
policies: List of policies to be registered.
Attributes:
conf_file: Policy model configuration file path.
policies: List of policies to be registered.
Returns:
Casbin enforcer object that validates against the registered
policy model.
"""
adapter = Adapter(f"mongodb://localhost:{self.db_port}", self.db_name)
e = Enforcer(conf_file, adapter)
model = e.get_model()
for _policy in policies:
self.save_policies(adapter=adapter, model=model, policy=_policy)
return Enforcer(conf_file, adapter)
def clear_db(self):
"""Helper to clear db after each test."""
client = MongoClient(f"mongodb://localhost:{self.db_port}")
client.drop_database(self.db_name)
def test_enforcer(self):
"""Test policy enforcer."""
e = self.get_enforcer(
conf_file=MODEL_CONF_FILE, policies=TEST_POLICIES_MODEL_CONF
)
assert e.enforce("alice", "data1", "read") is True
assert e.enforce("alice", "data1", "write") is False
assert e.enforce("bob", "data2", "read") is False
assert e.enforce("bob", "data2", "write") is True
assert e.enforce("alice", "data2", "read") is True
assert e.enforce("alice", "data2", "write") is True
def test_add_policy(self):
"""Test for adding new policy."""
e = self.get_enforcer(
conf_file=MODEL_CONF_FILE, policies=TEST_POLICIES_MODEL_CONF
)
adapter = e.get_adapter()
assert e.enforce("alice", "data1", "write") is False
assert e.enforce("bob", "data2", "read") is False
policy1 = adapter.add_policy(
sec="p", ptype="p", rule=("alice", "data1", "write")
)
policy2 = adapter.add_policy(
sec="p", ptype="p", rule=("bob", "data2", "read")
)
e.load_policy()
assert policy1 is True
assert policy2 is True
assert e.enforce("alice", "data1", "read") is True
assert e.enforce("bob", "data2", "read") is True
def test_remove_policy(self):
"""Test for removing policy."""
e = self.get_enforcer(
conf_file=MODEL_CONF_FILE, policies=TEST_POLICIES_MODEL_CONF
)
adapter = e.get_adapter()
assert e.enforce("alice", "data2", "read") is True
assert e.enforce("alice", "data2", "write") is True
remove_policy = adapter.remove_policy(
sec="g", ptype="g", rule=("alice", "data2_admin")
)
e.load_policy()
assert remove_policy is True
assert e.enforce("alice", "data2", "read") is False
assert e.enforce("alice", "data2", "write") is False
def test_remove_policy_with_incomplete_rule(self):
"""Test to remove policy with incomplete rule. (Policy should not be
removed)
"""
adapter = Adapter(f"mongodb://localhost:{self.db_port}", self.db_name)
e = Enforcer(MODEL_ROLES_CONF_FILE, adapter)
e = self.get_enforcer(
conf_file=MODEL_ROLES_CONF_FILE,
policies=TEST_POLICIES_MODEL_ROLES_CONF
)
e.load_policy()
assert e.enforce("alice", "data1", "write") is True
assert e.enforce("alice", "data1", "read") is True
assert e.enforce("bob", "data2", "read") is True
assert e.enforce("alice", "data2", "write") is True
# test remove_policy doesn't remove when given an incomplete policy
remove_policy = adapter.remove_policy(
sec="p", ptype="p", rule=("alice", "data1")
)
e.load_policy()
assert remove_policy is False
assert e.enforce("alice", "data1", "write") is True
assert e.enforce("alice", "data1", "read") is True
assert e.enforce("bob", "data2", "read") is True
assert e.enforce("alice", "data2", "write") is True
def test_remove_policy_with_empty_rule(self):
"""Test to remove policy with empty rule. (Policy should not be
removed)
"""
adapter = Adapter(f"mongodb://localhost:{self.db_port}", self.db_name)
e = Enforcer(MODEL_ROLES_CONF_FILE, adapter)
e = self.get_enforcer(
conf_file=MODEL_ROLES_CONF_FILE,
policies=TEST_POLICIES_MODEL_ROLES_CONF
)
e.load_policy()
assert e.enforce("alice", "data1", "write") is True
remove_policy = adapter.remove_policy(
sec="p", ptype=None, rule=()
)
e.load_policy()
assert remove_policy is False
assert e.enforce("alice", "data1", "write") is True
def test_save_policy(self):
"""Test to save policy."""
e = self.get_enforcer(
conf_file=MODEL_CONF_FILE, policies=TEST_POLICIES_MODEL_CONF
)
assert e.enforce("alice", "data4", "read") is False
model = e.get_model()
model.clear_policy()
model.add_policy("p", "p", ("alice", "data4", "read"))
adapter = e.get_adapter()
adapter.save_policy(model)
assert e.enforce("alice", "data4", "read") is True
def test_remove_filtered_policy(self):
"""Test to remove filtered policy definitions."""
e = self.get_enforcer(
conf_file=MODEL_CONF_FILE, policies=TEST_POLICIES_MODEL_CONF
)
adapter = e.get_adapter()
assert e.enforce("alice", "data1", "read") is True
assert e.enforce("alice", "data1", "write") is False
assert e.enforce("bob", "data2", "read") is False
assert e.enforce("bob", "data2", "write") is True
assert e.enforce("alice", "data2", "read") is True
assert e.enforce("alice", "data2", "write") is True
result = adapter.remove_filtered_policy(
"g", "g", 6, "alice", "data2_admin"
)
e.load_policy()
assert result is False
result = adapter.remove_filtered_policy(
"g", "g", 0, *[f"v{i}" for i in range(7)]
)
e.load_policy()
assert result is False
result = adapter.remove_filtered_policy(
"g", "g", 0, "alice", "data2_admin"
)
e.load_policy()
assert result is True
assert e.enforce("alice", "data1", "read") is True
assert e.enforce("alice", "data1", "write") is False
assert e.enforce("bob", "data2", "read") is False
assert e.enforce("bob", "data2", "write") is True
assert e.enforce("alice", "data2", "read") is False
assert e.enforce("alice", "data2", "write") is False