18
18
# <http://www.gnu.org/licenses/>.
19
19
#
20
20
from collections import defaultdict
21
+ from typing import FrozenSet , List , Optional , Union
22
+
23
+ from ..policyrep import AnyConstraint , ConstraintRuletype , Role , Type , User
21
24
22
- from ..policyrep import ConstraintRuletype
23
25
from .descriptors import DiffResultDescriptor
24
26
from .difference import Difference , SymbolWrapper , Wrapper
25
27
from .objclass import class_wrapper_factory
28
+ from .typing import RuleList
26
29
27
30
28
31
class ConstraintsDifference (Difference ):
@@ -55,10 +58,10 @@ class ConstraintsDifference(Difference):
55
58
removed_mlsvalidatetrans = DiffResultDescriptor ("diff_mlsvalidatetrans" )
56
59
57
60
# Lists of rules for each policy
58
- _left_constraints = None
59
- _right_constraints = None
61
+ _left_constraints : RuleList [ ConstraintRuletype , AnyConstraint ] = None
62
+ _right_constraints : RuleList [ ConstraintRuletype , AnyConstraint ] = None
60
63
61
- def diff_constrains (self ):
64
+ def diff_constrains (self ) -> None :
62
65
"""Generate the difference in constraint rules between the policies."""
63
66
64
67
self .log .info ("Generating constraint differences from {0.left_policy} to {0.right_policy}" .
@@ -67,11 +70,14 @@ def diff_constrains(self):
67
70
if self ._left_constraints is None or self ._right_constraints is None :
68
71
self ._create_constrain_lists ()
69
72
73
+ assert self ._left_constraints is not None , "Left constraints didn't load, this a bug."
74
+ assert self ._right_constraints is not None , "Right constraints didn't load, this a bug."
75
+
70
76
self .added_constrains , self .removed_constrains , _ = self ._set_diff (
71
77
(ConstraintWrapper (c ) for c in self ._left_constraints [ConstraintRuletype .constrain ]),
72
78
(ConstraintWrapper (c ) for c in self ._right_constraints [ConstraintRuletype .constrain ]))
73
79
74
- def diff_mlsconstrains (self ):
80
+ def diff_mlsconstrains (self ) -> None :
75
81
"""Generate the difference in MLS constraint rules between the policies."""
76
82
77
83
self .log .info (
@@ -81,13 +87,16 @@ def diff_mlsconstrains(self):
81
87
if self ._left_constraints is None or self ._right_constraints is None :
82
88
self ._create_constrain_lists ()
83
89
90
+ assert self ._left_constraints is not None , "Left constraints didn't load, this a bug."
91
+ assert self ._right_constraints is not None , "Right constraints didn't load, this a bug."
92
+
84
93
self .added_mlsconstrains , self .removed_mlsconstrains , _ = self ._set_diff (
85
94
(ConstraintWrapper (c ) for c in self ._left_constraints [
86
95
ConstraintRuletype .mlsconstrain ]),
87
96
(ConstraintWrapper (c ) for c in self ._right_constraints [
88
97
ConstraintRuletype .mlsconstrain ]))
89
98
90
- def diff_validatetrans (self ):
99
+ def diff_validatetrans (self ) -> None :
91
100
"""Generate the difference in validatetrans rules between the policies."""
92
101
93
102
self .log .info (
@@ -97,13 +106,16 @@ def diff_validatetrans(self):
97
106
if self ._left_constraints is None or self ._right_constraints is None :
98
107
self ._create_constrain_lists ()
99
108
109
+ assert self ._left_constraints is not None , "Left constraints didn't load, this a bug."
110
+ assert self ._right_constraints is not None , "Right constraints didn't load, this a bug."
111
+
100
112
self .added_validatetrans , self .removed_validatetrans , _ = self ._set_diff (
101
113
(ConstraintWrapper (c ) for c in self ._left_constraints [
102
114
ConstraintRuletype .validatetrans ]),
103
115
(ConstraintWrapper (c ) for c in self ._right_constraints [
104
116
ConstraintRuletype .validatetrans ]))
105
117
106
- def diff_mlsvalidatetrans (self ):
118
+ def diff_mlsvalidatetrans (self ) -> None :
107
119
"""Generate the difference in MLS validatetrans rules between the policies."""
108
120
109
121
self .log .info (
@@ -113,6 +125,9 @@ def diff_mlsvalidatetrans(self):
113
125
if self ._left_constraints is None or self ._right_constraints is None :
114
126
self ._create_constrain_lists ()
115
127
128
+ assert self ._left_constraints is not None , "Left constraints didn't load, this a bug."
129
+ assert self ._right_constraints is not None , "Right constraints didn't load, this a bug."
130
+
116
131
self .added_mlsvalidatetrans , self .removed_mlsvalidatetrans , _ = self ._set_diff (
117
132
(ConstraintWrapper (c ) for c in self ._left_constraints [
118
133
ConstraintRuletype .mlsvalidatetrans ]),
@@ -122,7 +137,7 @@ def diff_mlsvalidatetrans(self):
122
137
#
123
138
# Internal functions
124
139
#
125
- def _create_constrain_lists (self ):
140
+ def _create_constrain_lists (self ) -> None :
126
141
"""Create rule lists for both policies."""
127
142
self ._left_constraints = defaultdict (list )
128
143
self .log .debug ("Building constraint lists from {0.left_policy}" .format (self ))
@@ -142,7 +157,7 @@ def _create_constrain_lists(self):
142
157
143
158
self .log .debug ("Completed building constraint rule lists." )
144
159
145
- def _reset_diff (self ):
160
+ def _reset_diff (self ) -> None :
146
161
"""Reset diff results on policy changes."""
147
162
self .log .debug ("Resetting all constraints differences" )
148
163
self .added_constrains = None
@@ -159,26 +174,28 @@ def _reset_diff(self):
159
174
self ._right_constraints = None
160
175
161
176
162
- class ConstraintWrapper (Wrapper ):
177
+ # Pylint bug: https://github.com/PyCQA/pylint/issues/2822
178
+ class ConstraintWrapper (Wrapper [AnyConstraint ]): # pylint: disable=unsubscriptable-object
163
179
164
180
"""Wrap constraints for diff purposes."""
165
181
166
182
__slots__ = ("ruletype" , "tclass" , "perms" , "expr" )
167
183
168
- def __init__ (self , rule ) :
184
+ def __init__ (self , rule : AnyConstraint ) -> None :
169
185
self .origin = rule
170
186
self .ruletype = rule .ruletype
171
187
self .tclass = class_wrapper_factory (rule .tclass )
172
188
173
189
try :
174
- self .perms = rule .perms
190
+ self .perms : Optional [ FrozenSet [ str ]] = rule .perms
175
191
except AttributeError :
176
192
# (mls)validatetrans
177
193
self .perms = None
178
194
179
195
self .key = hash (rule )
180
196
181
- self .expr = []
197
+ self .expr : List [Union [FrozenSet [SymbolWrapper [Union [Role , Type , User ]]], str ]] = []
198
+
182
199
for op in rule .expression :
183
200
if isinstance (op , frozenset ):
184
201
# lists of types/users/roles
0 commit comments