forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_utils.py
More file actions
328 lines (288 loc) · 11.2 KB
/
cli_utils.py
File metadata and controls
328 lines (288 loc) · 11.2 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
from typing import Any, Optional
from bigtree import Node
from colorama import Fore, Style
from feast import (
BatchFeatureView,
FeatureService,
FeatureStore,
FeatureView,
OnDemandFeatureView,
StreamFeatureView,
)
from feast.feast_object import FeastObject
from feast.permissions.action import ALL_ACTIONS
from feast.permissions.decision import DecisionEvaluator
from feast.permissions.permission import Permission
from feast.permissions.policy import Policy, RoleBasedPolicy
from feast.permissions.user import User
def print_permission_verbose_example():
print("")
print(
f"{Style.BRIGHT + Fore.GREEN}The structure of the {Style.BRIGHT + Fore.WHITE}feast-permissions list --verbose {Style.BRIGHT + Fore.GREEN}command will be as in the following example:"
)
print("")
print(f"{Style.DIM}For example: {Style.RESET_ALL}{Style.BRIGHT + Fore.GREEN}")
print("")
explanation_root_node = Node("permissions")
explanation_permission_node = Node(
"permission_1" + " " + str(["role names list"]),
parent=explanation_root_node,
)
Node(
FeatureView.__name__ + ": " + str(["feature view names"]),
parent=explanation_permission_node,
)
Node(FeatureService.__name__ + ": none", parent=explanation_permission_node)
Node("..", parent=explanation_permission_node)
Node(
"permission_2" + " " + str(["role names list"]),
parent=explanation_root_node,
)
Node("..", parent=explanation_root_node)
explanation_root_node.show()
print(
f"""
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------{Style.RESET_ALL}
"""
)
def handle_sd_verbose_permissions_command(
feast_type: list[FeastObject],
p: Permission,
policy_node: Node,
store: FeatureStore,
tags_filter: Optional[dict[str, str]],
):
saved_datasets = store.list_saved_datasets(tags=tags_filter)
saved_datasets_names = set()
for sd in saved_datasets:
if p.match_resource(sd):
saved_datasets_names.add(sd.name)
if len(saved_datasets_names) > 0:
Node(
feast_type.__name__ + ": " + str(list(saved_datasets_names)), # type: ignore[union-attr, attr-defined]
parent=policy_node,
)
else:
Node(feast_type.__name__ + ": none", parent=policy_node) # type: ignore[union-attr, attr-defined]
def handle_vr_verbose_permissions_command(
feast_type: list[FeastObject],
p: Permission,
policy_node: Node,
store: FeatureStore,
tags_filter: Optional[dict[str, str]],
):
validation_references = store.list_validation_references(tags=tags_filter)
validation_references_names = set()
for vr in validation_references:
if p.match_resource(vr):
validation_references_names.add(vr.name)
if len(validation_references_names) > 0:
Node(
feast_type.__name__ + ": " + str(list(validation_references_names)), # type: ignore[union-attr, attr-defined]
parent=policy_node,
)
else:
Node(feast_type.__name__ + ": none", parent=policy_node) # type: ignore[union-attr, attr-defined]
def handle_ds_verbose_permissions_command(
feast_type: list[FeastObject],
p: Permission,
policy_node: Node,
store: FeatureStore,
tags_filter: Optional[dict[str, str]],
):
data_sources = store.list_data_sources(tags=tags_filter)
data_sources_names = set()
for ds in data_sources:
if p.match_resource(ds):
data_sources_names.add(ds.name)
if len(data_sources_names) > 0:
Node(
feast_type.__name__ + ": " + str(list(data_sources_names)), # type: ignore[union-attr, attr-defined]
parent=policy_node,
)
else:
Node(feast_type.__name__ + ": none", parent=policy_node) # type: ignore[union-attr, attr-defined]
def handle_fs_verbose_permissions_command(
feast_type: list[FeastObject],
p: Permission,
policy_node: Node,
store: FeatureStore,
tags_filter: Optional[dict[str, str]],
):
feature_services = store.list_feature_services(tags=tags_filter)
feature_services_names = set()
for fs in feature_services:
if p.match_resource(fs):
feature_services_names.add(fs.name)
if len(feature_services_names) > 0:
Node(
feast_type.__name__ + ": " + str(list(feature_services_names)), # type: ignore[union-attr, attr-defined]
parent=policy_node,
)
else:
Node(feast_type.__name__ + ": none", parent=policy_node) # type: ignore[union-attr, attr-defined]
def handle_entity_verbose_permissions_command(
feast_type: list[FeastObject],
p: Permission,
policy_node: Node,
store: FeatureStore,
tags_filter: Optional[dict[str, str]],
):
entities = store.list_entities(tags=tags_filter)
entities_names = set()
for e in entities:
if p.match_resource(e):
entities_names.add(e.name)
if len(entities_names) > 0:
Node(feast_type.__name__ + ": " + str(list(entities_names)), parent=policy_node) # type: ignore[union-attr, attr-defined]
else:
Node(feast_type.__name__ + ": none", parent=policy_node) # type: ignore[union-attr, attr-defined]
def handle_fv_verbose_permissions_command(
feast_type: list[FeastObject],
p: Permission,
policy_node: Node,
store: FeatureStore,
tags_filter: Optional[dict[str, str]],
):
feature_views = []
feature_views_names = set()
if feast_type == FeatureView:
feature_views = store.list_all_feature_views(tags=tags_filter) # type: ignore[assignment]
elif feast_type == OnDemandFeatureView:
feature_views = store.list_on_demand_feature_views(
tags=tags_filter # type: ignore[assignment]
)
elif feast_type == BatchFeatureView:
feature_views = store.list_batch_feature_views(tags=tags_filter) # type: ignore[assignment]
elif feast_type == StreamFeatureView:
feature_views = store.list_stream_feature_views(
tags=tags_filter # type: ignore[assignment]
)
for fv in feature_views:
if p.match_resource(fv): # type: ignore[arg-type]
feature_views_names.add(fv.name)
if len(feature_views_names) > 0:
Node(
feast_type.__name__ + " " + str(list(feature_views_names)), # type: ignore[union-attr, attr-defined]
parent=policy_node,
)
else:
Node(feast_type.__name__ + ": none", parent=policy_node) # type: ignore[union-attr, attr-defined]
def handle_not_verbose_permissions_command(
p: Permission, policy: Policy, table: list[Any]
):
roles: set[str] = set()
if isinstance(policy, RoleBasedPolicy):
roles = set(policy.get_roles())
table.append(
[
p.name,
_to_multi_line([t.__name__ for t in p.types]), # type: ignore[union-attr, attr-defined]
p.name_pattern,
_to_multi_line([a.value.upper() for a in p.actions]),
_to_multi_line(sorted(roles)),
_dict_to_multi_line(p.required_tags),
],
)
def fetch_all_feast_objects(store: FeatureStore) -> list[FeastObject]:
objects: list[FeastObject] = []
objects.extend(store.list_entities())
objects.extend(store.list_all_feature_views()) # type: ignore[arg-type]
objects.extend(store.list_feature_services())
objects.extend(store.list_data_sources())
objects.extend(store.list_validation_references())
objects.extend(store.list_saved_datasets())
objects.extend(store.list_permissions())
return objects
def handle_permissions_check_command(
object: FeastObject, permissions: list[Permission], table: list[Any]
):
for p in permissions:
if p.match_resource(object):
return
table.append(
[
object.name,
type(object).__name__,
]
)
def handle_permissions_check_command_with_actions(
object: FeastObject, permissions: list[Permission], table: list[Any]
):
unmatched_actions = ALL_ACTIONS.copy()
for p in permissions:
if p.match_resource(object):
for action in ALL_ACTIONS:
if p.match_actions([action]) and action in unmatched_actions:
unmatched_actions.remove(action)
if unmatched_actions:
table.append(
[
object.name,
type(object).__name__,
_to_multi_line([a.value.upper() for a in unmatched_actions]),
]
)
def fetch_all_permission_roles(permissions: list[Permission]) -> list[str]:
all_roles = set()
for p in permissions:
if isinstance(p.policy, RoleBasedPolicy) and len(p.policy.get_roles()) > 0:
all_roles.update(p.policy.get_roles())
return sorted(all_roles)
def handler_list_all_permissions_roles(permissions: list[Permission], table: list[Any]):
all_roles = fetch_all_permission_roles(permissions)
for role in all_roles:
table.append(
[
role,
]
)
def handler_list_all_permissions_roles_verbose(
objects: list[FeastObject], permissions: list[Permission], table: list[Any]
):
all_roles = fetch_all_permission_roles(permissions)
for role in all_roles:
for o in objects:
permitted_actions = ALL_ACTIONS.copy()
for action in ALL_ACTIONS:
# Following code is derived from enforcer.enforce_policy but has a different return type and does not raise FeastPermissionError
matching_permissions = [
p
for p in permissions
if p.match_resource(o) and p.match_actions([action])
]
if matching_permissions:
evaluator = DecisionEvaluator(
len(matching_permissions),
)
for p in matching_permissions:
permission_grant, permission_explanation = (
p.policy.validate_user(user=User(username="", roles=[role]))
)
evaluator.add_grant(
permission_grant,
f"Permission {p.name} denied access: {permission_explanation}",
)
if evaluator.is_decided():
grant, explanations = evaluator.grant()
if not grant:
permitted_actions.remove(action)
break
else:
permitted_actions.remove(action)
table.append(
[
role,
o.name,
type(o).__name__,
_to_multi_line([a.value.upper() for a in permitted_actions]),
]
)
def _to_multi_line(values: list[str]) -> str:
if not values:
return "-"
return "\n".join(values)
def _dict_to_multi_line(values: dict[str, str]) -> str:
if not values:
return "-"
return "\n".join([f"{key} : {value}" for key, value in values.items()])