-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.py
More file actions
34 lines (22 loc) · 1.06 KB
/
helpers.py
File metadata and controls
34 lines (22 loc) · 1.06 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
from typing import Dict, Literal
from aserto.authorizer.v2 import DecisionTreeResponse, PathSeparator
DecisionTree = Dict[str, Dict[str, bool]]
def policy_path_separator_field(
policy_path_separator: Literal["DOT", "SLASH"],
) -> PathSeparator.ValueType:
if policy_path_separator == "DOT":
return PathSeparator.PATH_SEPARATOR_DOT
if policy_path_separator == "SLASH":
return PathSeparator.PATH_SEPARATOR_SLASH
raise ValueError(f"Invalid PathSeparator: {policy_path_separator}")
def validate_decision_tree(response: DecisionTreeResponse) -> DecisionTree:
error = TypeError("Received unexpected response data")
decision_tree: DecisionTree = {}
for path, decisions in response.path.fields.items():
if decisions.WhichOneof("kind") != "struct_value":
raise error
for name, decision in decisions.struct_value.fields.items():
if decision.WhichOneof("kind") != "bool_value":
raise error
decision_tree.setdefault(path, {})[name] = decision.bool_value
return decision_tree