@@ -39,10 +39,8 @@ def items(self):
39
39
40
40
def build_role_links (self , rm_map ):
41
41
"""initializes the roles in RBAC."""
42
-
43
42
if "g" not in self .keys ():
44
43
return
45
-
46
44
for ptype , ast in self ["g" ].items ():
47
45
rm = rm_map .get (ptype )
48
46
if rm :
@@ -68,28 +66,23 @@ def build_conditional_role_links(self, cond_rm_map):
68
66
69
67
def print_policy (self ):
70
68
"""Log using info"""
71
-
72
69
self .logger .info ("Policy:" )
73
70
for sec in ["p" , "g" ]:
74
71
if sec not in self .keys ():
75
72
continue
76
-
77
73
for key , ast in self [sec ].items ():
78
74
self .logger .info ("{} : {} : {}" .format (key , ast .value , ast .policy ))
79
75
80
76
def clear_policy (self ):
81
77
"""clears all current policy."""
82
-
83
78
for sec in ["p" , "g" ]:
84
79
if sec not in self .keys ():
85
80
continue
86
-
87
81
for key in self [sec ].keys ():
88
82
self [sec ][key ].policy = []
89
83
90
84
def get_policy (self , sec , ptype ):
91
85
"""gets all rules in a policy."""
92
-
93
86
return self [sec ][ptype ].policy
94
87
95
88
def get_filtered_policy (self , sec , ptype , field_index , * field_values ):
@@ -109,7 +102,6 @@ def has_policy(self, sec, ptype, rule):
109
102
return False
110
103
if ptype not in self [sec ]:
111
104
return False
112
-
113
105
return rule in self [sec ][ptype ].policy
114
106
115
107
def add_policy (self , sec , ptype , rule ):
@@ -123,23 +115,19 @@ def add_policy(self, sec, ptype, rule):
123
115
if sec == "p" and assertion .priority_index >= 0 :
124
116
try :
125
117
idx_insert = int (rule [assertion .priority_index ])
126
-
127
118
i = len (assertion .policy ) - 1
128
119
for i in range (i , 0 , - 1 ):
129
120
try :
130
121
idx = int (assertion .policy [i - 1 ][assertion .priority_index ])
131
122
except Exception as e :
132
123
print (e )
133
-
134
124
if idx > idx_insert :
135
125
tmp = assertion .policy [i ]
136
126
assertion .policy [i ] = assertion .policy [i - 1 ]
137
127
assertion .policy [i - 1 ] = tmp
138
128
else :
139
129
break
140
-
141
130
assertion .policy_map [DEFAULT_SEP .join (rule )] = i
142
-
143
131
except Exception as e :
144
132
print (e )
145
133
@@ -148,19 +136,16 @@ def add_policy(self, sec, ptype, rule):
148
136
149
137
def add_policies (self , sec , ptype , rules ):
150
138
"""adds policy rules to the model."""
151
-
152
139
for rule in rules :
153
140
if self .has_policy (sec , ptype , rule ):
154
141
return False
155
-
156
142
for rule in rules :
157
- self [ sec ][ ptype ]. policy . append ( rule )
158
-
143
+ if not self . add_policy ( sec , ptype , rule ):
144
+ return False
159
145
return True
160
146
161
147
def update_policy (self , sec , ptype , old_rule , new_rule ):
162
148
"""update a policy rule from the model."""
163
-
164
149
if sec not in self .keys ():
165
150
return False
166
151
if ptype not in self [sec ]:
@@ -175,18 +160,21 @@ def update_policy(self, sec, ptype, old_rule, new_rule):
175
160
176
161
if "p_priority" in ast .tokens :
177
162
priority_index = ast .tokens .index ("p_priority" )
178
- if old_rule [priority_index ] == new_rule [priority_index ]:
179
- ast .policy [rule_index ] = new_rule
180
- else :
163
+ if old_rule [priority_index ] != new_rule [priority_index ]:
181
164
raise Exception ("New rule should have the same priority with old rule." )
182
- else :
183
- ast .policy [rule_index ] = new_rule
165
+ # 替换列表中的规则
166
+ ast .policy [rule_index ] = new_rule
167
+ # 更新映射:删除旧键,添加新键
168
+ old_key = DEFAULT_SEP .join (old_rule )
169
+ new_key = DEFAULT_SEP .join (new_rule )
170
+ if old_key in ast .policy_map :
171
+ del ast .policy_map [old_key ]
172
+ ast .policy_map [new_key ] = rule_index
184
173
185
174
return True
186
175
187
176
def update_policies (self , sec , ptype , old_rules , new_rules ):
188
177
"""update policy rules from the model."""
189
-
190
178
if sec not in self .keys ():
191
179
return False
192
180
if ptype not in self [sec ]:
@@ -206,13 +194,22 @@ def update_policies(self, sec, ptype, old_rules, new_rules):
206
194
if "p_priority" in ast .tokens :
207
195
priority_index = ast .tokens .index ("p_priority" )
208
196
for idx , old_rule , new_rule in zip (old_rules_index , old_rules , new_rules ):
209
- if old_rule [priority_index ] == new_rule [priority_index ]:
210
- ast .policy [idx ] = new_rule
211
- else :
197
+ if old_rule [priority_index ] != new_rule [priority_index ]:
212
198
raise Exception ("New rule should have the same priority with old rule." )
199
+ ast .policy [idx ] = new_rule
200
+ old_key = DEFAULT_SEP .join (old_rule )
201
+ new_key = DEFAULT_SEP .join (new_rule )
202
+ if old_key in ast .policy_map :
203
+ del ast .policy_map [old_key ]
204
+ ast .policy_map [new_key ] = idx
213
205
else :
214
206
for idx , old_rule , new_rule in zip (old_rules_index , old_rules , new_rules ):
215
207
ast .policy [idx ] = new_rule
208
+ old_key = DEFAULT_SEP .join (old_rule )
209
+ new_key = DEFAULT_SEP .join (new_rule )
210
+ if old_key in ast .policy_map :
211
+ del ast .policy_map [old_key ]
212
+ ast .policy_map [new_key ] = idx
216
213
217
214
return True
218
215
@@ -221,19 +218,30 @@ def remove_policy(self, sec, ptype, rule):
221
218
if not self .has_policy (sec , ptype , rule ):
222
219
return False
223
220
224
- self [sec ][ptype ].policy .remove (rule )
221
+ assertion = self [sec ][ptype ]
222
+ assertion .policy .remove (rule )
223
+ # 重新构建映射
224
+ new_map = {}
225
+ for idx , r in enumerate (assertion .policy ):
226
+ new_map [DEFAULT_SEP .join (r )] = idx
227
+ assertion .policy_map = new_map
225
228
226
- return rule not in self [ sec ][ ptype ] .policy
229
+ return rule not in assertion .policy
227
230
228
231
def remove_policies (self , sec , ptype , rules ):
229
232
"""RemovePolicies removes policy rules from the model."""
230
-
233
+ assertion = self [ sec ][ ptype ]
231
234
for rule in rules :
232
235
if not self .has_policy (sec , ptype , rule ):
233
236
return False
234
- self [ sec ][ ptype ] .policy .remove (rule )
235
- if rule in self [ sec ][ ptype ] .policy :
237
+ assertion .policy .remove (rule )
238
+ if rule in assertion .policy :
236
239
return False
240
+ # 重新构建映射
241
+ new_map = {}
242
+ for idx , r in enumerate (assertion .policy ):
243
+ new_map [DEFAULT_SEP .join (r )] = idx
244
+ assertion .policy_map = new_map
237
245
238
246
return True
239
247
@@ -243,7 +251,6 @@ def remove_policies_with_effected(self, sec, ptype, rules):
243
251
if self .has_policy (sec , ptype , rule ):
244
252
effected .append (rule )
245
253
self .remove_policy (sec , ptype , rule )
246
-
247
254
return effected
248
255
249
256
def remove_filtered_policy_returns_effects (self , sec , ptype , field_index , * field_values ):
@@ -266,7 +273,13 @@ def remove_filtered_policy_returns_effects(self, sec, ptype, field_index, *field
266
273
else :
267
274
tmp .append (rule )
268
275
269
- self [sec ][ptype ].policy = tmp
276
+ assertion = self [sec ][ptype ]
277
+ assertion .policy = tmp
278
+ # 重新构建映射
279
+ new_map = {}
280
+ for idx , r in enumerate (assertion .policy ):
281
+ new_map [DEFAULT_SEP .join (r )] = idx
282
+ assertion .policy_map = new_map
270
283
271
284
return effects
272
285
@@ -286,7 +299,13 @@ def remove_filtered_policy(self, sec, ptype, field_index, *field_values):
286
299
else :
287
300
tmp .append (rule )
288
301
289
- self [sec ][ptype ].policy = tmp
302
+ assertion = self [sec ][ptype ]
303
+ assertion .policy = tmp
304
+ # 重新构建映射
305
+ new_map = {}
306
+ for idx , r in enumerate (assertion .policy ):
307
+ new_map [DEFAULT_SEP .join (r )] = idx
308
+ assertion .policy_map = new_map
290
309
291
310
return res
292
311
@@ -297,10 +316,8 @@ def get_values_for_field_in_policy(self, sec, ptype, field_index):
297
316
return values
298
317
if ptype not in self [sec ]:
299
318
return values
300
-
301
319
for rule in self [sec ][ptype ].policy :
302
320
value = rule [field_index ]
303
321
if value not in values :
304
322
values .append (value )
305
-
306
323
return values
0 commit comments