-
Notifications
You must be signed in to change notification settings - Fork 17
/
schema_role.py
208 lines (171 loc) · 6.58 KB
/
schema_role.py
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
from snowddl.blueprint import (
DatabaseIdent,
SchemaRoleBlueprint,
SchemaBlueprint,
SchemaObjectIdent,
Grant,
FutureGrant,
build_role_ident,
)
from snowddl.resolver.abc_role_resolver import AbstractRoleResolver, ObjectType
class SchemaRoleResolver(AbstractRoleResolver):
def get_role_suffix(self):
return self.config.SCHEMA_ROLE_SUFFIX
def get_blueprints(self):
blueprints = []
for schema_bp in self.config.get_blueprints_by_type(SchemaBlueprint).values():
schema_permission_model = self.config.get_permission_model(schema_bp.permission_model)
if schema_permission_model.ruleset.create_schema_owner_role:
blueprints.append(self.get_blueprint_owner_role(schema_bp))
if schema_permission_model.ruleset.create_schema_write_role:
blueprints.append(self.get_blueprint_write_role(schema_bp))
if schema_permission_model.ruleset.create_schema_read_role:
blueprints.append(self.get_blueprint_read_role(schema_bp))
return {str(bp.full_name): bp for bp in blueprints}
def get_blueprint_owner_role(self, schema_bp: SchemaBlueprint):
grants = []
account_grants = []
future_grants = []
schema_permission_model = self.config.get_permission_model(schema_bp.permission_model)
grants.append(
Grant(
privilege="USAGE",
on=ObjectType.DATABASE,
name=DatabaseIdent(schema_bp.full_name.env_prefix, schema_bp.full_name.database),
)
)
grants.append(
Grant(
privilege="USAGE",
on=ObjectType.SCHEMA,
name=schema_bp.full_name,
)
)
for model_create_grant in schema_permission_model.owner_create_grants:
grants.append(
Grant(
privilege=f"CREATE {model_create_grant.on.singular}",
on=ObjectType.SCHEMA,
name=schema_bp.full_name,
)
)
for model_future_grant in schema_permission_model.owner_future_grants:
future_grants.append(
FutureGrant(
privilege=model_future_grant.privilege,
on_future=model_future_grant.on,
in_parent=ObjectType.SCHEMA,
name=schema_bp.full_name,
)
)
depends_on = set()
for additional_grant in schema_bp.owner_additional_grants:
grants.append(additional_grant)
# Dependency on another schema role
if additional_grant.on == ObjectType.ROLE and str(additional_grant.name).endswith(self.get_role_suffix()):
depends_on.add(additional_grant.name)
for additional_account_grant in schema_bp.owner_additional_account_grants:
account_grants.append(additional_account_grant)
bp = SchemaRoleBlueprint(
full_name=build_role_ident(
self.config.env_prefix,
schema_bp.full_name.database,
schema_bp.full_name.schema,
self.config.OWNER_ROLE_TYPE,
self.get_role_suffix(),
),
grants=grants,
account_grants=account_grants,
future_grants=future_grants,
depends_on=depends_on,
)
return bp
def get_blueprint_read_role(self, schema_bp: SchemaBlueprint):
grants = []
future_grants = []
schema_permission_model = self.config.get_permission_model(schema_bp.permission_model)
grants.append(
Grant(
privilege="USAGE",
on=ObjectType.DATABASE,
name=DatabaseIdent(schema_bp.full_name.env_prefix, schema_bp.full_name.database),
)
)
grants.append(
Grant(
privilege="USAGE",
on=ObjectType.SCHEMA,
name=schema_bp.full_name,
)
)
for model_future_grant in schema_permission_model.read_future_grants:
future_grants.append(
FutureGrant(
privilege=model_future_grant.privilege,
on_future=model_future_grant.on,
in_parent=ObjectType.SCHEMA,
name=schema_bp.full_name,
)
)
bp = SchemaRoleBlueprint(
full_name=build_role_ident(
self.config.env_prefix,
schema_bp.full_name.database,
schema_bp.full_name.schema,
self.config.READ_ROLE_TYPE,
self.get_role_suffix(),
),
grants=grants,
future_grants=future_grants,
)
return bp
def get_blueprint_write_role(self, schema_bp: SchemaBlueprint):
grants = []
future_grants = []
schema_permission_model = self.config.get_permission_model(schema_bp.permission_model)
grants.append(
Grant(
privilege="USAGE",
on=ObjectType.DATABASE,
name=DatabaseIdent(schema_bp.full_name.env_prefix, schema_bp.full_name.database),
)
)
grants.append(
Grant(
privilege="USAGE",
on=ObjectType.SCHEMA,
name=schema_bp.full_name,
)
)
for model_future_grant in schema_permission_model.write_future_grants:
future_grants.append(
FutureGrant(
privilege=model_future_grant.privilege,
on_future=model_future_grant.on,
in_parent=ObjectType.SCHEMA,
name=schema_bp.full_name,
)
)
bp = SchemaRoleBlueprint(
full_name=build_role_ident(
self.config.env_prefix,
schema_bp.full_name.database,
schema_bp.full_name.schema,
self.config.WRITE_ROLE_TYPE,
self.get_role_suffix(),
),
grants=grants,
future_grants=future_grants,
)
return bp
def grant_to_future_grant(self, grant: Grant):
if not grant.on.is_future_grant_supported:
return None
if isinstance(grant.name, SchemaObjectIdent):
return FutureGrant(
privilege=grant.privilege,
on_future=grant.on,
in_parent=ObjectType.SCHEMA,
name=grant.name.schema_full_name,
)
return None