-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathpolicy.py
More file actions
287 lines (219 loc) · 8.32 KB
/
policy.py
File metadata and controls
287 lines (219 loc) · 8.32 KB
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
import logging
DEFAULT_SEP = ","
class Policy:
def __init__(self):
self.logger = logging.getLogger(__name__)
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[ptype]
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 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(
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:
assertion.policy[i] = assertion.policy[i - 1]
else:
break
assertion.policy[i] = rule
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:
self[sec][ptype].policy.append(rule)
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
if "p_priority" in ast.tokens:
priority_index = ast.tokens.index("p_priority")
if old_rule[priority_index] == new_rule[priority_index]:
ast.policy[rule_index] = new_rule
else:
raise Exception("New rule should have the same priority with old rule.")
else:
ast.policy[rule_index] = new_rule
return True
def update_policies(self, sec, ptype, old_rules, new_rules):
"""update policy rules from the model."""
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]
old_rules_index = []
for old_rule in old_rules:
if old_rule in ast.policy:
old_rules_index.append(ast.policy.index(old_rule))
else:
return False
if "p_priority" in ast.tokens:
priority_index = ast.tokens.index("p_priority")
for idx, old_rule, new_rule in zip(old_rules_index, old_rules, new_rules):
if old_rule[priority_index] == new_rule[priority_index]:
ast.policy[idx] = new_rule
else:
raise Exception(
"New rule should have the same priority with old rule."
)
else:
for idx, old_rule, new_rule in zip(old_rules_index, old_rules, new_rules):
ast.policy[idx] = new_rule
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
self[sec][ptype].policy.remove(rule)
return rule not in self[sec][ptype].policy
def remove_policies(self, sec, ptype, rules):
"""RemovePolicies removes policy rules from the model."""
for rule in rules:
if not self.has_policy(sec, ptype, rule):
return False
self[sec][ptype].policy.remove(rule)
if rule in self[sec][ptype].policy:
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.
"""
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)
self[sec][ptype].policy = tmp
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)
self[sec][ptype].policy = tmp
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