Skip to content

Commit

Permalink
oracle脱敏更换函数 (#1306)
Browse files Browse the repository at this point in the history
* oracle脱敏更换函数

* ci测试Oracle语法问题

* 删除了注释备份

* 修改了测试sql
  • Loading branch information
unknowissue authored Dec 31, 2021
1 parent 3f6b517 commit f588e8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
27 changes: 13 additions & 14 deletions sql/engines/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from . import EngineBase
import cx_Oracle
from .models import ResultSet, ReviewSet, ReviewResult
from sql.utils.data_masking import brute_mask
from sql.utils.data_masking import simple_column_mask

logger = logging.getLogger('default')

Expand Down Expand Up @@ -361,12 +361,11 @@ def query(self, db_name=None, sql='', limit_num=0, close_conn=True, **kwargs):
self.close()
return result_set

def query_masking(self, schema_name=None, sql='', resultset=None):
"""传入 sql语句, db名, 结果集,
返回一个脱敏后的结果集"""
# 仅对select语句脱敏
if re.match(r"^select|^with", sql, re.I):
filtered_result = brute_mask(self.instance, resultset)

def query_masking(self, db_name=None, sql='', resultset=None):
"""简单字段脱敏规则, 仅对select有效"""
if re.match(r"^select", sql, re.I):
filtered_result = simple_column_mask(self.instance, resultset)
filtered_result.is_masked = True
else:
filtered_result = resultset
Expand Down Expand Up @@ -637,9 +636,9 @@ def execute_workflow(self, workflow, close_conn=True):
rowcount = cursor.rowcount
stagestatus = "Execute Successfully"
if sqlitem.stmt_type == "PLSQL" and sqlitem.object_name and sqlitem.object_name != 'ANONYMOUS' and sqlitem.object_name != '':
query_obj_sql = f"""SELECT OBJECT_NAME, STATUS, TO_CHAR(LAST_DDL_TIME, 'YYYY-MM-DD HH24:MI:SS') FROM ALL_OBJECTS
WHERE OWNER = '{sqlitem.object_owner}'
AND OBJECT_NAME = '{sqlitem.object_name}'
query_obj_sql = f"""SELECT OBJECT_NAME, STATUS, TO_CHAR(LAST_DDL_TIME, 'YYYY-MM-DD HH24:MI:SS') FROM ALL_OBJECTS
WHERE OWNER = '{sqlitem.object_owner}'
AND OBJECT_NAME = '{sqlitem.object_name}'
"""
cursor.execute(query_obj_sql)
row = cursor.fetchone()
Expand Down Expand Up @@ -738,7 +737,7 @@ def backup(self, workflow, cursor, begin_time, end_time):
endtime=>to_date('{end_time}','yyyy/mm/dd hh24:mi:ss'),
options=>dbms_logmnr.dict_from_online_catalog + dbms_logmnr.continuous_mine);
end;'''
undo_sql = f'''select sql_redo,sql_undo from v$logmnr_contents
undo_sql = f'''select sql_redo,sql_undo from v$logmnr_contents
where SEG_OWNER not in ('SYS','SYSTEM')
and session# = (select sid from v$mystat where rownum = 1)
and serial# = (select serial# from v$session s where s.sid = (select sid from v$mystat where rownum = 1 )) order by scn desc'''
Expand Down Expand Up @@ -822,7 +821,7 @@ def sqltuningadvisor(self, db_name=None, sql='', close_conn=True, **kwargs):
my_sqltext := '{sql}';
my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(
sql_text => my_sqltext,
user_name => '{db_name}',
user_name => '{db_name}',
scope => 'COMPREHENSIVE',
time_limit => 30,
task_name => '{task_name}',
Expand All @@ -848,8 +847,8 @@ def sqltuningadvisor(self, db_name=None, sql='', close_conn=True, **kwargs):
finally:
# 结束分析任务
if task_begin == 1:
end_sql = f'''DECLARE
begin
end_sql = f'''DECLARE
begin
dbms_sqltune.drop_tuning_task('{task_name}');
end;'''
cursor.execute(end_sql)
Expand Down
2 changes: 1 addition & 1 deletion sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ def test_filter_sql_with_limit(self):
def test_query_masking(self):
query_result = ResultSet()
new_engine = OracleEngine(instance=self.ins)
masking_result = new_engine.query_masking(schema_name='', sql='select 1', resultset=query_result)
masking_result = new_engine.query_masking(sql='select 1 from dual', resultset=query_result)
self.assertEqual(masking_result, query_result)

def test_execute_check_select_sql(self):
Expand Down

0 comments on commit f588e8b

Please sign in to comment.