-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathoracle.py
318 lines (292 loc) · 12.6 KB
/
oracle.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding: UTF-8 -*-
# https://stackoverflow.com/questions/7942520/relationship-between-catalog-schema-user-and-database-instance
import logging
import traceback
import re
import sqlparse
from common.config import SysConfig
from common.utils.timer import FuncTimer
from sql.utils.sql_utils import get_syntax_type
from . import EngineBase
import cx_Oracle
from .models import ResultSet, ReviewSet, ReviewResult
from sql.utils.data_masking import brute_mask
logger = logging.getLogger('default')
class OracleEngine(EngineBase):
def __init__(self, instance=None):
super(OracleEngine, self).__init__(instance=instance)
self.service_name = instance.service_name
self.sid = instance.sid
def get_connection(self, db_name=None):
if self.conn:
return self.conn
if self.sid:
dsn = cx_Oracle.makedsn(self.host, self.port, self.sid)
self.conn = cx_Oracle.connect(self.user, self.password, dsn=dsn, encoding="UTF-8", nencoding="UTF-8")
elif self.service_name:
dsn = cx_Oracle.makedsn(self.host, self.port, service_name=self.service_name)
self.conn = cx_Oracle.connect(self.user, self.password, dsn=dsn, encoding="UTF-8", nencoding="UTF-8")
else:
raise ValueError('sid 和 dsn 均未填写, 请联系管理页补充该实例配置.')
return self.conn
@property
def name(self):
return 'Oracle'
@property
def info(self):
return 'Oracle engine'
@property
def server_version(self):
conn = self.get_connection()
version = conn.version
return tuple([n for n in version.split('.')[:3]])
def get_all_databases(self):
"""获取数据库列表, 返回resultSet 供上层调用, 底层实际上是获取oracle的schema列表"""
return self._get_all_schemas()
def _get_all_databases(self):
"""获取数据库列表, 返回一个ResultSet"""
sql = "select name from v$database"
result = self.query(sql=sql)
db_list = [row[0] for row in result.rows]
result.rows = db_list
return result
def _get_all_instances(self):
"""获取实例列表, 返回一个ResultSet"""
sql = "select instance_name from v$instance"
result = self.query(sql=sql)
instance_list = [row[0] for row in result.rows]
result.rows = instance_list
return result
def _get_all_schemas(self):
"""
获取模式列表
:return:
"""
result = self.query(sql="select username from sys.dba_users")
sysschema = (
'AUD_SYS', 'ANONYMOUS', 'APEX_030200', 'APEX_PUBLIC_USER', 'APPQOSSYS', 'BI USERS', 'CTXSYS', 'DBSNMP',
'DIP USERS', 'EXFSYS', 'FLOWS_FILES', 'HR USERS', 'IX USERS', 'MDDATA', 'MDSYS', 'MGMT_VIEW', 'OE USERS',
'OLAPSYS', 'ORACLE_OCM', 'ORDDATA', 'ORDPLUGINS', 'ORDSYS', 'OUTLN', 'OWBSYS', 'OWBSYS_AUDIT', 'PM USERS',
'SCOTT', 'SH USERS', 'SI_INFORMTN_SCHEMA', 'SPATIAL_CSW_ADMIN_USR', 'SPATIAL_WFS_ADMIN_USR', 'SYS',
'SYSMAN', 'SYSTEM', 'WMSYS', 'XDB', 'XS$NULL')
schema_list = [row[0] for row in result.rows if row[0] not in sysschema]
result.rows = schema_list
return result
def get_all_tables(self, db_name):
"""获取table 列表, 返回一个ResultSet"""
sql = f"""select
TABLE_NAME
from dba_tab_privs
where grantee in ('{db_name}')
union
select
OBJECT_NAME
from dba_objects
WHERE OWNER IN ('{db_name}') and object_type in ('TABLE')
"""
result = self.query(sql=sql)
tb_list = [row[0] for row in result.rows if row[0] not in ['test']]
result.rows = tb_list
return result
def get_all_columns_by_tb(self, db_name, tb_name):
"""获取所有字段, 返回一个ResultSet"""
result = self.describe_table(db_name, tb_name)
column_list = [row[0] for row in result.rows]
result.rows = column_list
return result
def describe_table(self, db_name, tb_name):
"""return ResultSet"""
# https://www.thepolyglotdeveloper.com/2015/01/find-tables-oracle-database-column-name/
sql = f"""SELECT
column_name,
data_type,
data_length,
nullable,
data_default
FROM all_tab_cols
WHERE table_name = '{tb_name}'
"""
result = self.query(sql=sql)
return result
def query_check(self, db_name=None, sql=''):
# 查询语句的检查、注释去除、切分
result = {'msg': '', 'bad_query': False, 'filtered_sql': sql, 'has_star': False}
keyword_warning = ''
star_patter = r"(^|,| )\*( |\(|$)"
# 删除注释语句,进行语法判断,执行第一条有效sql
try:
sql = sqlparse.split(sql)[0]
result['filtered_sql'] = re.sub(r';$', '', sql.strip())
sql = sqlparse.format(sql, strip_comments=True)
sql_lower = sql.lower()
except IndexError:
result['bad_query'] = True
result['msg'] = '没有有效的SQL语句'
return result
if re.match(r"^select", sql_lower) is None:
result['bad_query'] = True
result['msg'] = '仅支持^select语法!'
return result
if re.search(star_patter, sql_lower) is not None:
keyword_warning += '禁止使用 * 关键词\n'
result['has_star'] = True
if '+' in sql_lower:
keyword_warning += '禁止使用 + 关键词\n'
result['bad_query'] = True
if result.get('bad_query') or result.get('has_star'):
result['msg'] = keyword_warning
return result
def filter_sql(self, sql='', limit_num=0):
sql_lower = sql.lower()
# 对查询sql增加limit限制
if re.match(r"^select", sql_lower):
if sql_lower.find(' rownum ') == -1:
if sql_lower.find(' where ') == -1:
return f"{sql.rstrip(';')} WHERE ROWNUM <= {limit_num}"
else:
return f"{sql.rstrip(';')} AND ROWNUM <= {limit_num}"
return sql.strip()
def query(self, db_name=None, sql='', limit_num=0, close_conn=True):
"""返回 ResultSet """
result_set = ResultSet(full_sql=sql)
try:
conn = self.get_connection()
cursor = conn.cursor()
if db_name:
cursor.execute(f"ALTER SESSION SET CURRENT_SCHEMA = {db_name}")
cursor.execute(sql)
fields = cursor.description
if any(x[1] == cx_Oracle.CLOB for x in fields):
rows = [tuple([(c.read() if type(c) == cx_Oracle.LOB else c) for c in r]) for r in cursor]
if int(limit_num) > 0:
rows = rows[0:int(limit_num)]
else:
if int(limit_num) > 0:
rows = cursor.fetchmany(int(limit_num))
else:
rows = cursor.fetchall()
result_set.column_list = [i[0] for i in fields] if fields else []
result_set.rows = [tuple(x) for x in rows]
result_set.affected_rows = len(result_set.rows)
except Exception as e:
logger.error(f"Oracle 语句执行报错,语句:{sql},错误信息{traceback.format_exc()}")
result_set.error = str(e)
finally:
if close_conn:
self.close()
return result_set
def query_masking(self, schema_name=None, sql='', resultset=None):
"""传入 sql语句, db名, 结果集,
返回一个脱敏后的结果集"""
# 仅对select语句脱敏
if re.match(r"^select", sql, re.I):
filtered_result = brute_mask(resultset)
filtered_result.is_masked = True
else:
filtered_result = resultset
return filtered_result
def execute_check(self, db_name=None, sql=''):
"""上线单执行前的检查, 返回Review set"""
config = SysConfig()
check_result = ReviewSet(full_sql=sql)
# 禁用/高危语句检查
line = 1
critical_ddl_regex = config.get('critical_ddl_regex', '')
p = re.compile(critical_ddl_regex)
check_result.syntax_type = 2 # TODO 工单类型 0、其他 1、DDL,2、DML
for statement in sqlparse.split(sql):
statement = sqlparse.format(statement, strip_comments=True)
# 禁用语句
if re.match(r"^select", statement.lower()):
check_result.is_critical = True
result = ReviewResult(id=line, errlevel=2,
stagestatus='驳回不支持语句',
errormessage='仅支持DML和DDL语句,查询语句请使用SQL查询功能!',
sql=statement)
# 高危语句
elif critical_ddl_regex and p.match(statement.strip().lower()):
check_result.is_critical = True
result = ReviewResult(id=line, errlevel=2,
stagestatus='驳回高危SQL',
errormessage='禁止提交匹配' + critical_ddl_regex + '条件的语句!',
sql=statement)
# 正常语句
else:
result = ReviewResult(id=line, errlevel=0,
stagestatus='Audit completed',
errormessage='None',
sql=statement,
affected_rows=0,
execute_time=0, )
# 判断工单类型
if get_syntax_type(statement) == 'DDL':
check_result.syntax_type = 1
check_result.rows += [result]
# 遇到禁用和高危语句直接返回,提高效率
if check_result.is_critical:
check_result.error_count += 1
return check_result
line += 1
return check_result
def execute_workflow(self, workflow, close_conn=True):
"""执行上线单,返回Review set"""
sql = workflow.sqlworkflowcontent.sql_content
execute_result = ReviewSet(full_sql=sql)
# 删除注释语句,切分语句,将切换CURRENT_SCHEMA语句增加到切分结果中
sql = sqlparse.format(sql, strip_comments=True)
split_sql = [f"ALTER SESSION SET CURRENT_SCHEMA = {workflow.db_name};"] + sqlparse.split(sql)
line = 1
statement = None
try:
conn = self.get_connection()
cursor = conn.cursor()
# 逐条执行切分语句,追加到执行结果中
for statement in split_sql:
statement = statement.rstrip(';')
with FuncTimer() as t:
cursor.execute(statement)
conn.commit()
execute_result.rows.append(ReviewResult(
id=line,
errlevel=0,
stagestatus='Execute Successfully',
errormessage='None',
sql=statement,
affected_rows=cursor.rowcount,
execute_time=t.cost,
))
line += 1
except Exception as e:
logger.error(f"Oracle命令执行报错,语句:{statement or sql}, 错误信息:{traceback.format_exc()}")
execute_result.error = str(e)
# 追加当前报错语句信息到执行结果中
execute_result.rows.append(ReviewResult(
id=line,
errlevel=2,
stagestatus='Execute Failed',
errormessage=f'异常信息:{e}',
sql=statement or sql,
affected_rows=0,
execute_time=0,
))
line += 1
# 报错语句后面的语句标记为审核通过、未执行,追加到执行结果中
for statement in split_sql[line - 1:]:
execute_result.rows.append(ReviewResult(
id=line,
errlevel=0,
stagestatus='Audit completed',
errormessage=f'前序语句失败, 未执行',
sql=statement,
affected_rows=0,
execute_time=0,
))
line += 1
finally:
if close_conn:
self.close()
return execute_result
def close(self):
if self.conn:
self.conn.close()
self.conn = None