Skip to content

Commit

Permalink
Доработка dwh-definitons библиотеки (#118)
Browse files Browse the repository at this point in the history
## Изменения
Добавил SensitiveBase(Base), переписал кастомные скрипты для алембика

## Детали реализации
см. выше

## Check-List
- [x] Вы проверили свой код перед отправкой запроса?
- [ ] Вы написали тесты к реализованным функциям?
- [x] Вы не забыли применить форматирование `black` и `isort` для
_Back-End_ или `Prettier` для _Front-End_?

---------

Co-authored-by: Тимофеев Никита Алексеевич <[email protected]>
Co-authored-by: Mikhail <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2024
1 parent 1433f9d commit 62d8c19
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
23 changes: 18 additions & 5 deletions migrations/custom_scripts/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
@comparators.dispatch_for("table")
def compare_table(autogen_context, modify_table_ops, s, tname, metadata_table_db, metadata_table_code):
if str(metadata_table_db) == 'None':
sensitive = metadata_table_code.info.get('sensitive', False)
for render_scope in ['read', 'write', 'all']:
group_name = (
f'test_dwh_{s}_{render_scope}'.lower()
f'test_{"sensitive_" if sensitive else ""}dwh_{s}_{render_scope}'.lower()
if os.getenv("ENVIRONMENT") != "production"
else f'prod_dwh_{s}_{render_scope}'.lower()
else f'prod_{"sensitive_" if sensitive else ""}dwh_{s}_{render_scope}'.lower()
)

scopes = []
match render_scope:
case 'read':
Expand All @@ -22,17 +24,23 @@ def compare_table(autogen_context, modify_table_ops, s, tname, metadata_table_db
scopes = ['SELECT', 'UPDATE', 'DELETE', 'TRUNCATE', 'INSERT']
case 'all':
scopes = ['ALL']


if sensitive:
modify_table_ops.ops.append(
CreateGroupOp(group_name=group_name)
)

modify_table_ops.ops.append(
GrantRightsOp(table_name=str(metadata_table_code), scopes=scopes, group_name=group_name)
)

elif str(metadata_table_code) == 'None':
sensitive = metadata_table_db.info.get('sensitive', False)
for render_scope in ['read', 'write', 'all']:
group_name = (
f'test_dwh_{s}_{render_scope}'.lower()
f'test_{"sensitive_" if sensitive else ""}dwh_{s}_{render_scope}'.lower()
if os.getenv("ENVIRONMENT") != "production"
else f'prod_dwh_{s}_{render_scope}'.lower()
else f'prod_{"sensitive_" if sensitive else ""}dwh_{s}_{render_scope}'.lower()
)
scopes = []
match render_scope:
Expand All @@ -46,3 +54,8 @@ def compare_table(autogen_context, modify_table_ops, s, tname, metadata_table_db
modify_table_ops.ops.append(
RevokeRightsOp(table_name=str(metadata_table_db), scopes=scopes, group_name=group_name)
)

if sensitive:
modify_table_ops.ops.append(
DeleteGroupOp(group_name=group_name)
)
20 changes: 18 additions & 2 deletions profcomff_definitions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import as_declarative
from typing import Any

from migrations.custom_scripts.schemas import add_table_schema_to_model

Expand All @@ -20,14 +21,29 @@ def __tablename__(cls) -> str:

@classmethod
@declared_attr
def __table_args__(cls) -> dict[str, str]:
def __table_args__(cls) -> dict[str, Any]:
schema = f'{cls.__module__.split(".")[-2].upper()}_{cls.__module__.split(".")[-1].upper()}'
add_table_schema_to_model(schema, Base.metadata)

return {'schema': schema, 'comment': cls.__doc__}
return {'schema': schema, 'comment': cls.__doc__, 'info': {'sensitive': False}}

def __repr__(self) -> str:
attrs = []
for c in self.__table__.columns:
attrs.append(f"{c.name}={getattr(self, c.name)}")
return "{}({})".format(self.__class__.__name__, ", ".join(attrs))


class SensitiveBase(Base):
"""Base class for all sensitive entities"""

@classmethod
@declared_attr
def __table_args__(cls) -> dict[str, Any]:
schema = f'{cls.__module__.split(".")[-2].upper()}_{cls.__module__.split(".")[-1].upper()}'
add_table_schema_to_model(schema, Base.metadata)

return {'schema': schema, 'comment': cls.__doc__, 'info': {'sensitive': True}}

__abstract__ = True
__mapper_args__ = {'concrete': True}

0 comments on commit 62d8c19

Please sign in to comment.