-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpolicy.py
More file actions
345 lines (272 loc) · 11 KB
/
policy.py
File metadata and controls
345 lines (272 loc) · 11 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from abc import ABC, abstractmethod
from typing import Any
from feast.permissions.user import User
from feast.protos.feast.core.Policy_pb2 import (
CombinedGroupNamespacePolicy as CombinedGroupNamespacePolicyProto,
)
from feast.protos.feast.core.Policy_pb2 import GroupBasedPolicy as GroupBasedPolicyProto
from feast.protos.feast.core.Policy_pb2 import (
NamespaceBasedPolicy as NamespaceBasedPolicyProto,
)
from feast.protos.feast.core.Policy_pb2 import Policy as PolicyProto
from feast.protos.feast.core.Policy_pb2 import RoleBasedPolicy as RoleBasedPolicyProto
class Policy(ABC):
"""
An abstract class to ensure that the current user matches the configured security policies.
"""
@abstractmethod
def validate_user(self, user: User) -> tuple[bool, str]:
"""
Validate the given user against the configured policy.
Args:
user: The current user.
Returns:
bool: `True` if the user matches the policy criteria, `False` otherwise.
str: A possibly empty explanation of the reason for not matching the configured policy.
"""
raise NotImplementedError
@staticmethod
def from_proto(policy_proto: PolicyProto) -> Any:
"""
Converts policy config in protobuf spec to a Policy class object.
Args:
policy_proto: A protobuf representation of a Policy.
Returns:
A Policy class object.
"""
policy_type = policy_proto.WhichOneof("policy_type")
if policy_type == "role_based_policy":
return RoleBasedPolicy.from_proto(policy_proto)
elif policy_type == "group_based_policy":
return GroupBasedPolicy.from_proto(policy_proto)
elif policy_type == "namespace_based_policy":
return NamespaceBasedPolicy.from_proto(policy_proto)
elif policy_type == "combined_group_namespace_policy":
return CombinedGroupNamespacePolicy.from_proto(policy_proto)
if policy_type is None:
return None
raise NotImplementedError(f"policy_type is unsupported: {policy_type}")
@abstractmethod
def to_proto(self) -> PolicyProto:
"""
Converts a PolicyProto object to its protobuf representation.
"""
raise NotImplementedError
class RoleBasedPolicy(Policy):
"""
A `Policy` implementation where the user roles must be enforced to grant access to the requested action.
At least one of the configured roles must be granted to the current user in order to allow the execution of the secured operation.
E.g., if the policy enforces roles `a` and `b`, the user must have at least one of them in order to satisfy the policy.
"""
def __init__(
self,
roles: list[str],
):
self.roles = roles
def __eq__(self, other):
if not isinstance(other, RoleBasedPolicy):
raise TypeError(
"Comparisons should only involve RoleBasedPolicy class objects."
)
if sorted(self.roles) != sorted(other.roles):
return False
return True
def get_roles(self) -> list[str]:
return self.roles
def validate_user(self, user: User) -> tuple[bool, str]:
"""
Validate the given `user` against the configured roles.
"""
result = user.has_matching_role(self.roles)
explain = "" if result else f"Requires roles {self.roles}"
return (result, explain)
@staticmethod
def from_proto(policy_proto: PolicyProto) -> Any:
"""
Converts policy config in protobuf spec to a Policy class object.
Args:
policy_proto: A protobuf representation of a Policy.
Returns:
A RoleBasedPolicy class object.
"""
return RoleBasedPolicy(roles=list(policy_proto.role_based_policy.roles))
def to_proto(self) -> PolicyProto:
"""
Converts a PolicyProto object to its protobuf representation.
"""
role_based_policy_proto = RoleBasedPolicyProto(roles=self.roles)
policy_proto = PolicyProto(role_based_policy=role_based_policy_proto)
return policy_proto
def allow_all(self, user: User) -> tuple[bool, str]:
return True, ""
def empty_policy(self) -> PolicyProto:
return PolicyProto()
class GroupBasedPolicy(Policy):
"""
A `Policy` implementation where the user groups must be enforced to grant access to the requested action.
At least one of the configured groups must be granted to the current user in order to allow the execution of the secured operation.
E.g., if the policy enforces groups `a` and `b`, the user must be added in one of them in order to satisfy the policy.
"""
def __init__(
self,
groups: list[str],
):
self.groups = groups
def __eq__(self, other):
if not isinstance(other, GroupBasedPolicy):
raise TypeError(
"Comparisons should only involve GroupBasedPolicy class objects."
)
if sorted(self.groups) != sorted(other.groups):
return False
return True
def get_groups(self) -> list[str]:
return self.groups
def validate_user(self, user: User) -> tuple[bool, str]:
"""
Validate the given `user` against the configured groups.
"""
result = user.has_matching_group(self.groups)
explain = "User is not added into the permitted groups" if not result else ""
return (result, explain)
@staticmethod
def from_proto(policy_proto: PolicyProto) -> Any:
"""
Converts policy config in protobuf spec to a Policy class object.
Args:
policy_proto: A protobuf representation of a Policy.
Returns:
A GroupBasedPolicy class object.
"""
return GroupBasedPolicy(groups=list(policy_proto.group_based_policy.groups))
def to_proto(self) -> PolicyProto:
"""
Converts a GroupBasedPolicy object to its protobuf representation.
"""
group_based_policy_proto = GroupBasedPolicyProto(groups=self.groups)
policy_proto = PolicyProto(group_based_policy=group_based_policy_proto)
return policy_proto
class NamespaceBasedPolicy(Policy):
"""
A `Policy` implementation where the user must be added to the namespaces must be enforced to grant access to the requested action.
User must be added to at least one of the permitted namespaces in order to allow the execution of the secured operation.
E.g., if the policy enforces namespaces `a` and `b`, the user must have at least one of them in order to satisfy the policy.
"""
def __init__(
self,
namespaces: list[str],
):
self.namespaces = namespaces
def __eq__(self, other):
if not isinstance(other, NamespaceBasedPolicy):
raise TypeError(
"Comparisons should only involve NamespaceBasedPolicy class objects."
)
if sorted(self.namespaces) != sorted(other.namespaces):
return False
return True
def get_namespaces(self) -> list[str]:
return self.namespaces
def validate_user(self, user: User) -> tuple[bool, str]:
"""
Validate the given `user` against the configured namespaces.
"""
result = user.has_matching_namespace(self.namespaces)
explain = (
"User is not added into the permitted namespaces" if not result else ""
)
return (result, explain)
@staticmethod
def from_proto(policy_proto: PolicyProto) -> Any:
"""
Converts policy config in protobuf spec to a Policy class object.
Args:
policy_proto: A protobuf representation of a Policy.
Returns:
A NamespaceBasedPolicy class object.
"""
return NamespaceBasedPolicy(
namespaces=list(policy_proto.namespace_based_policy.namespaces)
)
def to_proto(self) -> PolicyProto:
"""
Converts a NamespaceBasedPolicy object to its protobuf representation.
"""
namespace_based_policy_proto = NamespaceBasedPolicyProto(
namespaces=self.namespaces
)
policy_proto = PolicyProto(namespace_based_policy=namespace_based_policy_proto)
return policy_proto
class CombinedGroupNamespacePolicy(Policy):
"""
A `Policy` implementation that combines group-based and namespace-based authorization.
The user must be in at least one of the permitted groups OR namespaces to satisfy the policy.
"""
def __init__(
self,
groups: list[str],
namespaces: list[str],
):
self.groups = groups
self.namespaces = namespaces
def __eq__(self, other):
if not isinstance(other, CombinedGroupNamespacePolicy):
raise TypeError(
"Comparisons should only involve CombinedGroupNamespacePolicy class objects."
)
if sorted(self.groups) != sorted(other.groups) or sorted(
self.namespaces
) != sorted(other.namespaces):
return False
return True
def get_groups(self) -> list[str]:
return self.groups
def get_namespaces(self) -> list[str]:
return self.namespaces
def validate_user(self, user: User) -> tuple[bool, str]:
"""
Validate the given `user` against the permitted groups and namespaces.
User must be added to one of the matching group or namespace.
"""
has_matching_group = user.has_matching_group(self.groups)
has_matching_namespace = user.has_matching_namespace(self.namespaces)
result = has_matching_group or has_matching_namespace
if not result:
explain = (
"User must be in at least one of the permitted groups or namespaces"
)
else:
explain = ""
return (result, explain)
@staticmethod
def from_proto(policy_proto: PolicyProto) -> Any:
"""
Converts policy config in protobuf spec to a Policy class object.
Args:
policy_proto: A protobuf representation of a Policy.
Returns:
A CombinedGroupNamespacePolicy class object.
"""
return CombinedGroupNamespacePolicy(
groups=list(policy_proto.combined_group_namespace_policy.groups),
namespaces=list(policy_proto.combined_group_namespace_policy.namespaces),
)
def to_proto(self) -> PolicyProto:
"""
Converts a CombinedGroupNamespacePolicy object to its protobuf representation.
"""
combined_policy_proto = CombinedGroupNamespacePolicyProto(
groups=self.groups, namespaces=self.namespaces
)
policy_proto = PolicyProto(
combined_group_namespace_policy=combined_policy_proto
)
return policy_proto
"""
A `Policy` instance to allow execution of any action to each user
"""
AllowAll = type(
"AllowAll",
(Policy,),
{Policy.validate_user.__name__: allow_all, Policy.to_proto.__name__: empty_policy},
)()