Skip to content

Commit

Permalink
Mongo返回结果支持脱敏-V0.1-beta (#2884)
Browse files Browse the repository at this point in the history
* 返回一个脱敏后的结果集

* 通过goInception获取select list

* test_query_masking

* test_query_masking

* 8889

* 18888888889
  • Loading branch information
feiazifeiazi authored Dec 27, 2024
1 parent 1940735 commit 69e29b1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
8 changes: 8 additions & 0 deletions sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from bson.objectid import ObjectId
from bson.int64 import Int64

from sql.utils.data_masking import data_masking

from . import EngineBase
from .models import ResultSet, ReviewSet, ReviewResult
from common.config import SysConfig
Expand Down Expand Up @@ -1422,3 +1424,9 @@ def reset_instance_user_pwd(self, db_name_user: str, reset_pwd: str, **kwargs):
except Exception as e:
exec_result.error = str(e)
return exec_result

def query_masking(self, db_name=None, sql="", resultset=None):
"""传入 sql语句, db名, 结果集,
返回一个脱敏后的结果集"""
mask_result = data_masking(self.instance, db_name, sql, resultset)
return mask_result
33 changes: 32 additions & 1 deletion sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sql.engines.mongo import MongoEngine
from sql.engines.clickhouse import ClickHouseEngine
from sql.engines.odps import ODPSEngine
from sql.models import Instance, SqlWorkflow, SqlWorkflowContent
from sql.models import DataMaskingColumns, Instance, SqlWorkflow, SqlWorkflowContent

User = get_user_model()

Expand Down Expand Up @@ -1665,9 +1665,19 @@ def setUp(self) -> None:
)
self.engine = MongoEngine(instance=self.ins)
self.sys_config = SysConfig()
# rule_type=100的规则不需要加,会自动创建。只需要加脱敏字段
DataMaskingColumns.objects.create(
rule_type=100,
active=True,
instance=self.ins,
table_schema="*",
table_name="*",
column_name="mobile",
)

def tearDown(self) -> None:
self.ins.delete()
DataMaskingColumns.objects.all().delete()

@patch("sql.engines.mongo.pymongo")
def test_get_connection(self, mock_pymongo):
Expand Down Expand Up @@ -2042,6 +2052,27 @@ def test_create_instance_user(self, _mock_command):
],
)

def test_query_masking(self):
query_result = ResultSet()
new_engine = MongoEngine(instance=self.ins)
query_result.column_list = ["id", "mobile"]
query_result.rows = (
("a11", "18888888888"),
("a12", ""),
("a13", None),
("a14", "18888888889"),
)
masking_result = new_engine.query_masking(
db_name="archery", sql="db.test_collection.find()", resultset=query_result
)
mask_result_rows = [
["a11", "188****8888"],
["a12", ""],
["a13", None],
["a14", "188****8889"],
]
self.assertEqual(masking_result.rows, mask_result_rows)


class TestClickHouse(TestCase):
def setUp(self):
Expand Down
23 changes: 18 additions & 5 deletions sql/utils/data_masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ def data_masking(instance, db_name, sql, sql_result):
for token in p.tokens:
if token.ttype is Keyword and token.value.upper() in ["UNION", "UNION ALL"]:
keywords_count["UNION"] = keywords_count.get("UNION", 0) + 1
# 通过goInception获取select list
inception_engine = GoInceptionEngine()
select_list = inception_engine.query_data_masking(
instance=instance, db_name=db_name, sql=sql
)
if instance.db_type == "mongo":
select_list = [
{
"index": index,
"field": field,
"type": "varchar",
"table": "*",
"schema": db_name,
"alias": field,
}
for index, field in enumerate(sql_result.column_list)
]
else:
# 通过goInception获取select list
inception_engine = GoInceptionEngine()
select_list = inception_engine.query_data_masking(
instance=instance, db_name=db_name, sql=sql
)
# 如果UNION存在,那么调用去重函数
select_list = (
del_repeat(select_list, keywords_count) if keywords_count else select_list
Expand Down

0 comments on commit 69e29b1

Please sign in to comment.