-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathpolicy.py
309 lines (240 loc) · 9.52 KB
/
policy.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Copyright 2021 The casbin Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
DEFAULT_SEP = ","
class Policy:
def __init__(self):
self.logger = logging.getLogger("casbin.policy")
self.model = {}
def __getitem__(self, item):
return self.model.get(item)
def __setitem__(self, key, value):
self.model[key] = value
def keys(self):
return self.model.keys()
def values(self):
return self.model.values()
def items(self):
return self.model.items()
def build_role_links(self, rm_map):
"""initializes the roles in RBAC."""
if "g" not in self.keys():
return
for ptype, ast in self["g"].items():
rm = rm_map.get(ptype)
if rm:
ast.build_role_links(rm)
def build_incremental_role_links(self, rm, op, sec, ptype, rules):
if sec == "g":
self[sec].get(ptype).build_incremental_role_links(rm, op, rules)
def build_incremental_conditional_role_links(self, cond_rm, op, sec, ptype, rules):
if sec == "g":
return self[sec].get(ptype).build_incremental_conditional_role_links(cond_rm, op, rules)
return None
def build_conditional_role_links(self, cond_rm_map):
if "g" not in self.keys():
return
self.print_policy()
for ptype, ast in self["g"].items():
cond_rm = cond_rm_map.get(ptype)
if cond_rm:
ast.build_conditional_role_links(cond_rm)
def print_policy(self):
"""Log using info"""
self.logger.info("Policy:")
for sec in ["p", "g"]:
if sec not in self.keys():
continue
for key, ast in self[sec].items():
self.logger.info("{} : {} : {}".format(key, ast.value, ast.policy))
def clear_policy(self):
"""clears all current policy."""
for sec in ["p", "g"]:
if sec not in self.keys():
continue
for key in self[sec].keys():
self[sec][key].policy = []
def get_policy(self, sec, ptype):
"""gets all rules in a policy."""
return self[sec][ptype].policy
def get_filtered_policy(self, sec, ptype, field_index, *field_values):
"""gets rules based on field filters from a policy."""
return [
rule
for rule in self[sec][ptype].policy
if all(
(callable(value) and value(rule[field_index + i])) or (value == "" or rule[field_index + i] == value)
for i, value in enumerate(field_values)
)
]
def has_policy(self, sec, ptype, rule):
"""determines whether a model has the specified policy rule."""
if sec not in self.keys():
return False
if ptype not in self[sec]:
return False
return rule in self[sec][ptype].policy
def add_policy(self, sec, ptype, rule):
"""adds a policy rule to the model."""
assertion = self[sec][ptype]
if not self.has_policy(sec, ptype, rule):
assertion.policy.append(rule)
else:
return False
if sec == "p" and assertion.priority_index >= 0:
try:
idx_insert = int(rule[assertion.priority_index])
i = len(assertion.policy) - 1
for i in range(i, 0, -1):
try:
idx = int(assertion.policy[i - 1][assertion.priority_index])
except Exception as e:
print(e)
if idx > idx_insert:
tmp = assertion.policy[i]
assertion.policy[i] = assertion.policy[i - 1]
assertion.policy[i - 1] = tmp
else:
break
assertion.policy_map[DEFAULT_SEP.join(rule)] = i
except Exception as e:
print(e)
assertion.policy_map[DEFAULT_SEP.join(rule)] = len(assertion.policy) - 1
return True
def add_policies(self, sec, ptype, rules):
"""adds policy rules to the model."""
for rule in rules:
if self.has_policy(sec, ptype, rule):
return False
for rule in rules:
if not self.add_policy(sec, ptype, rule):
return False
return True
def update_policy(self, sec, ptype, old_rule, new_rule):
"""update a policy rule from the model."""
if sec not in self.keys():
return False
if ptype not in self[sec]:
return False
ast = self[sec][ptype]
if old_rule in ast.policy:
rule_index = ast.policy.index(old_rule)
else:
return False
ast.policy[rule_index] = new_rule
old_key = DEFAULT_SEP.join(old_rule)
new_key = DEFAULT_SEP.join(new_rule)
if old_key in ast.policy_map:
del ast.policy_map[old_key]
ast.policy_map[new_key] = rule_index
return True
def update_policies(self, sec, ptype, old_rules, new_rules):
"""update policy rules from the model using update_policy for each rule.
If any update fails, roll back all changes."""
if sec not in self.keys():
return False
if ptype not in self[sec]:
return False
if len(old_rules) != len(new_rules):
return False
ast = self[sec][ptype]
original_policy = [rule[:] for rule in ast.policy]
original_policy_map = ast.policy_map.copy()
for old_rule, new_rule in zip(old_rules, new_rules):
if not self.update_policy(sec, ptype, old_rule, new_rule):
ast.policy = original_policy
ast.policy_map = original_policy_map
return False
return True
def remove_policy(self, sec, ptype, rule):
"""removes a policy rule from the model."""
if not self.has_policy(sec, ptype, rule):
return False
assertion = self[sec][ptype]
assertion.policy.remove(rule)
new_map = {}
for idx, r in enumerate(assertion.policy):
new_map[DEFAULT_SEP.join(r)] = idx
assertion.policy_map = new_map
return rule not in assertion.policy
def remove_policies(self, sec, ptype, rules):
"""Remove multiple policy rules by sequentially calling remove_policy."""
for rule in rules:
if not self.remove_policy(sec, ptype, rule):
return False
return True
def remove_policies_with_effected(self, sec, ptype, rules):
effected = []
for rule in rules:
if self.has_policy(sec, ptype, rule):
effected.append(rule)
self.remove_policy(sec, ptype, rule)
return effected
def remove_filtered_policy_returns_effects(self, sec, ptype, field_index, *field_values):
"""
remove_filtered_policy_returns_effects removes policy rules based on field filters from the model.
Returns a list of rules that were removed.
"""
tmp = []
effects = []
if len(field_values) == 0:
return []
if sec not in self.keys():
return []
if ptype not in self[sec]:
return []
for rule in self[sec][ptype].policy:
if all(value == "" or rule[field_index + i] == value for i, value in enumerate(field_values)):
effects.append(rule)
else:
tmp.append(rule)
assertion = self[sec][ptype]
assertion.policy = tmp
new_map = {}
for idx, r in enumerate(assertion.policy):
new_map[DEFAULT_SEP.join(r)] = idx
assertion.policy_map = new_map
return effects
def remove_filtered_policy(self, sec, ptype, field_index, *field_values):
"""removes policy rules based on field filters from the model."""
tmp = []
res = False
if sec not in self.keys():
return res
if ptype not in self[sec]:
return res
for rule in self[sec][ptype].policy:
if all(value == "" or rule[field_index + i] == value for i, value in enumerate(field_values)):
res = True
else:
tmp.append(rule)
assertion = self[sec][ptype]
assertion.policy = tmp
new_map = {}
for idx, r in enumerate(assertion.policy):
new_map[DEFAULT_SEP.join(r)] = idx
assertion.policy_map = new_map
return res
def get_values_for_field_in_policy(self, sec, ptype, field_index):
"""gets all values for a field for all rules in a policy, duplicated values are removed."""
values = []
if sec not in self.keys():
return values
if ptype not in self[sec]:
return values
for rule in self[sec][ptype].policy:
value = rule[field_index]
if value not in values:
values.append(value)
return values