-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mongo.py
1385 lines (1286 loc) · 57.4 KB
/
mongo.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: UTF-8 -*-
import re, time
import pymongo
import logging
import traceback
import subprocess
import simplejson as json
import datetime
import tempfile
from bson.son import SON
from bson import json_util
from pymongo.errors import OperationFailure
from dateutil.parser import parse
from bson.objectid import ObjectId
from bson.int64 import Int64
from . import EngineBase
from .models import ResultSet, ReviewSet, ReviewResult
from common.config import SysConfig
logger = logging.getLogger("default")
# mongo客户端安装在本机的位置
mongo = "mongo"
# 自定义异常
class mongo_error(Exception):
def __init__(self, error_info):
super().__init__(self)
self.error_info = error_info
def __str__(self):
return self.error_info
class JsonDecoder:
"""处理传入mongodb语句中的条件,并转换成pymongo可识别的字典格式"""
def __init__(self):
pass
def __json_object(self, tokener):
# obj = collections.OrderedDict()
obj = {}
if tokener.cur_token() != "{":
raise Exception('Json must start with "{"')
while True:
tokener.next()
tk_temp = tokener.cur_token()
if tk_temp == "}":
return {}
# 限制key的格式
if not isinstance(
tk_temp, str
): # or (not tk_temp.isidentifier() and not tk_temp.startswith("$"))
raise Exception("invalid key %s" % tk_temp)
key = tk_temp.strip()
tokener.next()
if tokener.cur_token() != ":":
raise Exception('expect ":" after "%s"' % key)
tokener.next()
val = tokener.cur_token()
if val == "[":
val = self.__json_array(tokener)
elif val == "{":
val = self.__json_object(tokener)
obj[key] = val
tokener.next()
tk_split = tokener.cur_token()
if tk_split == ",":
continue
elif tk_split == "}":
break
else:
if tk_split is None:
raise Exception('missing "}" at at the end of object')
raise Exception('unexpected token "%s" at key "%s"' % (tk_split, key))
return obj
def __json_array(self, tokener):
if tokener.cur_token() != "[":
raise Exception('Json array must start with "["')
arr = []
while True:
tokener.next()
tk_temp = tokener.cur_token()
if tk_temp == "]":
return []
if tk_temp == "{":
val = self.__json_object(tokener)
elif tk_temp == "[":
val = self.__json_array(tokener)
elif tk_temp in (",", ":", "}"):
raise Exception('unexpected token "%s"' % tk_temp)
else:
val = tk_temp
arr.append(val)
tokener.next()
tk_end = tokener.cur_token()
if tk_end == ",":
continue
if tk_end == "]":
break
else:
if tk_end is None:
raise Exception('missing "]" at the end of array')
return arr
def decode(self, json_str):
tokener = JsonDecoder.__Tokener(json_str)
if not tokener.next():
return None
first_token = tokener.cur_token()
if first_token == "{":
decode_val = self.__json_object(tokener)
elif first_token == "[":
decode_val = self.__json_array(tokener)
else:
raise Exception('Json must start with "{"')
if tokener.next():
raise Exception('unexpected token "%s"' % tokener.cur_token())
return decode_val
class __Tokener: # Tokener 作为一个内部类
def __init__(self, json_str):
self.__str = json_str
self.__i = 0
self.__cur_token = None
def __cur_char(self):
if self.__i < len(self.__str):
return self.__str[self.__i]
return ""
def __previous_char(self):
if self.__i < len(self.__str):
return self.__str[self.__i - 1]
def __remain_str(self):
if self.__i < len(self.__str):
return self.__str[self.__i :]
def __move_i(self, step=1):
if self.__i < len(self.__str):
self.__i += step
def __next_string(self):
"""当出现了"和'后就进入这个方法解析,直到出现与之对应的结束字符"""
outstr = ""
trans_flag = False
start_ch = ""
self.__move_i()
while self.__cur_char() != "":
ch = self.__cur_char()
if start_ch == "":
start_ch = self.__previous_char()
if ch == '\\"': # 判断是否是转义
trans_flag = True
else:
if not trans_flag:
if (ch == '"' and start_ch == '"') or (
ch == "'" and start_ch == "'"
):
break
else:
trans_flag = False
outstr += ch
self.__move_i()
return outstr
def __next_number(self):
expr = ""
while self.__cur_char().isdigit() or self.__cur_char() in (".", "+", "-"):
expr += self.__cur_char()
self.__move_i()
self.__move_i(-1)
if "." in expr:
return float(expr)
else:
return int(expr)
def __next_const(self):
"""处理没有被''和""包含的字符,如true和ObjectId"""
outstr = ""
data_type = ""
while self.__cur_char().isalpha() or self.__cur_char() in ("$", "_", " "):
outstr += self.__cur_char()
self.__move_i()
if outstr.replace(" ", "") in (
"ObjectId",
"newDate",
"ISODate",
"newISODate",
"NumberLong",
): # ======类似的类型比较多还需单独处理,如int()等
data_type = outstr
for c in self.__remain_str():
outstr += c
self.__move_i()
if c == ")":
break
self.__move_i(-1)
if outstr in ("true", "false", "null"):
return {"true": True, "false": False, "null": None}[outstr]
elif data_type == "ObjectId":
ojStr = re.findall(r"ObjectId\(.*?\)", outstr) # 单独处理ObjectId
if len(ojStr) > 0:
# return eval(ojStr[0])
id_str = re.findall(r"\(.*?\)", ojStr[0])
oid = id_str[0].replace(" ", "")[2:-2]
return ObjectId(oid)
elif data_type.replace(" ", "") in (
"newDate",
"ISODate",
"newISODate",
): # 处理时间格式
tmp_type = "%s()" % data_type
if outstr.replace(" ", "") == tmp_type.replace(" ", ""):
return datetime.datetime.now() + datetime.timedelta(
hours=-8
) # mongodb默认时区为utc
date_regex = re.compile(r'%s\("(.*)"\)' % data_type, re.IGNORECASE)
date_content = date_regex.findall(outstr)
if len(date_content) > 0:
return parse(date_content[0], yearfirst=True)
elif data_type.replace(" ", "") in ("NumberLong",):
nuStr = re.findall(r"NumberLong\(.*?\)", outstr) # 单独处理NumberLong
if len(nuStr) > 0:
id_str = re.findall(r"\(.*?\)", nuStr[0])
nlong = id_str[0].replace(" ", "")[2:-2]
return Int64(nlong)
elif outstr:
return outstr
raise Exception('Invalid symbol "%s"' % outstr)
def next(self):
is_white_space = lambda a_char: a_char in (
"\x20",
"\n",
"\r",
"\t",
) # 定义一个匿名函数
while is_white_space(self.__cur_char()):
self.__move_i()
ch = self.__cur_char()
if ch == "":
cur_token = None
elif ch in ("{", "}", "[", "]", ",", ":"):
cur_token = ch
elif ch in ('"', "'"): # 当字符为" '
cur_token = self.__next_string()
elif ch.isalpha() or ch in ("$", "_"): # 字符串是否只由字母和"$","_"组成
cur_token = self.__next_const()
elif ch.isdigit() or ch in (".", "-", "+"): # 检测字符串是否只由数字组成
cur_token = self.__next_number()
else:
raise Exception('Invalid symbol "%s"' % ch)
self.__move_i()
self.__cur_token = cur_token
return cur_token is not None
def cur_token(self):
return self.__cur_token
class MongoEngine(EngineBase):
error = None
warning = None
methodStr = None
def test_connection(self):
return self.get_all_databases()
def exec_cmd(self, sql, db_name=None, slave_ok=""):
"""审核时执行的语句"""
if self.user and self.password and self.port and self.host:
msg = ""
auth_db = self.instance.db_name or "admin"
sql_len = len(sql)
is_load = False # 默认不使用load方法执行mongodb sql语句
try:
if (
not sql.startswith("var host=") and sql_len > 4000
): # 在master节点执行的情况,如果sql长度大于4000,就采取load js的方法
# 因为用mongo load方法执行js脚本,所以需要重新改写一下sql,以便回显js执行结果
sql = "var result = " + sql + "\nprintjson(result);"
# 因为要知道具体的临时文件位置,所以用了NamedTemporaryFile模块
fp = tempfile.NamedTemporaryFile(
suffix=".js", prefix="mongo_", dir="/tmp/", delete=True
)
fp.write(sql.encode("utf-8"))
fp.seek(0) # 把文件指针指向开始,这样写的sql内容才能落到磁盘文件上
cmd = "{mongo} --quiet -u {uname} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\ndb=db.getSiblingDB(\"{db_name}\");{slave_ok}load('{tempfile_}')\nEOF".format(
mongo=mongo,
uname=self.user,
password=self.password,
host=self.host,
port=self.port,
db_name=db_name,
sql=sql,
auth_db=auth_db,
slave_ok=slave_ok,
tempfile_=fp.name,
)
is_load = True # 标记使用了load方法,用来在finally里面判断是否需要强制删除临时文件
elif (
not sql.startswith("var host=") and sql_len < 4000
): # 在master节点执行的情况, 如果sql长度小于4000,就直接用mongo shell执行,减少磁盘交换,节省性能
cmd = "{mongo} --quiet -u {uname} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\ndb=db.getSiblingDB(\"{db_name}\");{slave_ok}printjson({sql})\nEOF".format(
mongo=mongo,
uname=self.user,
password=self.password,
host=self.host,
port=self.port,
db_name=db_name,
sql=sql,
auth_db=auth_db,
slave_ok=slave_ok,
)
else:
cmd = "{mongo} --quiet -u {user} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\nrs.slaveOk();{sql}\nEOF".format(
mongo=mongo,
user=self.user,
password=self.password,
host=self.host,
port=self.port,
db_name=db_name,
sql=sql,
auth_db=auth_db,
)
p = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
re_msg = []
for line in iter(p.stdout.read, ""):
re_msg.append(line)
# 因为返回的line中也有可能带有换行符,因此需要先全部转换成字符串
__msg = "\n".join(re_msg)
_re_msg = []
for _line in __msg.split("\n"):
if not _re_msg and re.match("WARNING.*", _line):
# 第一行可能是WARNING语句,因此跳过
continue
_re_msg.append(_line)
msg = "\n".join(_re_msg)
msg = msg.replace("true\n", "")
except Exception as e:
logger.warning(f"mongo语句执行报错,语句:{sql},{e}错误信息{traceback.format_exc()}")
finally:
if is_load:
fp.close()
return msg
def get_master(self):
"""获得主节点的port和host"""
sql = "rs.isMaster().primary"
master = self.exec_cmd(sql)
if master != "undefined" and master.find("TypeError") >= 0:
sp_host = master.replace('"', "").split(":")
self.host = sp_host[0]
self.port = int(sp_host[1])
# return master
def get_slave(self):
"""获得从节点的port和host"""
sql = """var host=""; rs.status().members.forEach(function(item) {i=1; if (item.stateStr =="SECONDARY") \
{host=item.name } }); print(host);"""
slave_msg = self.exec_cmd(sql)
# 如果是阿里云的云mongodb,会获取不到备节点真实的ip和端口,那就干脆不获取,直接用主节点来执行sql
# 如果是自建mongodb,获取到备节点的ip是192.168.1.33:27019这样的值;但如果是阿里云mongodb,获取到的备节点ip是SECONDARY、hiddenNode这样的值
# 所以,为了使代码更加通用,通过有无冒号来判断自建Mongod还是阿里云mongdb;没有冒号就判定为阿里云mongodb,直接返回false;
if ":" not in slave_msg:
return False
if slave_msg.lower().find("undefined") < 0:
sp_host = slave_msg.replace('"', "").split(":")
self.host = sp_host[0]
self.port = int(sp_host[1])
return True
else:
return False
def get_table_conut(self, table_name, db_name):
try:
count_sql = f"db.{table_name}.count()"
status = self.get_slave() # 查询总数据要求在slave节点执行
if self.host and self.port and status:
count = int(self.exec_cmd(count_sql, db_name, slave_ok="rs.slaveOk();"))
else:
count = int(self.exec_cmd(count_sql, db_name))
return count
except Exception as e:
logger.debug("get_table_conut:" + str(e))
return 0
def execute_workflow(self, workflow):
"""执行上线单,返回Review set"""
return self.execute(
db_name=workflow.db_name, sql=workflow.sqlworkflowcontent.sql_content
)
def execute(self, db_name=None, sql=""):
"""mongo命令执行语句"""
self.get_master()
execute_result = ReviewSet(full_sql=sql)
sql = sql.strip()
# 以;切分语句,逐句执行
sp_sql = sql.split(";")
line = 0
for exec_sql in sp_sql:
if not exec_sql == "":
exec_sql = exec_sql.strip()
try:
# DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
start = time.perf_counter()
r = self.exec_cmd(exec_sql, db_name)
end = time.perf_counter()
line += 1
logger.debug("执行结果:" + r)
# 如果执行中有错误
rz = r.replace(" ", "").replace('"', "")
tr = 1
if (
r.lower().find("syntaxerror") >= 0
or rz.find("ok:0") >= 0
or rz.find("error:invalid") >= 0
or rz.find("ReferenceError") >= 0
or rz.find("getErrorWithCode") >= 0
or rz.find("failedtoconnect") >= 0
or rz.find("Error:") >= 0
):
tr = 0
if (rz.find("errmsg") >= 0 or tr == 0) and (
r.lower().find("already exist") < 0
):
execute_result.error = r
result = ReviewResult(
id=line,
stage="Execute failed",
errlevel=2,
stagestatus="异常终止",
errormessage=f"mongo语句执行报错: {r}",
sql=exec_sql,
)
else:
try:
r = json.loads(r)
except Exception as e:
logger.info(str(e))
finally:
methodStr = exec_sql.split(").")[-1].split("(")[0].strip()
if "." in methodStr:
methodStr = methodStr.split(".")[-1]
if methodStr == "insert":
actual_affected_rows = r.get("nInserted", 0)
elif methodStr in ("insertOne", "insertMany"):
actual_affected_rows = r.count("ObjectId")
elif methodStr == "update":
actual_affected_rows = r.get("nModified", 0)
elif methodStr in ("updateOne", "updateMany"):
actual_affected_rows = r.get("modifiedCount", 0)
elif methodStr in ("deleteOne", "deleteMany"):
actual_affected_rows = r.get("deletedCount", 0)
elif methodStr == "remove":
actual_affected_rows = r.get("nRemoved", 0)
else:
actual_affected_rows = 0
# 把结果转换为ReviewSet
result = ReviewResult(
id=line,
errlevel=0,
stagestatus="执行结束",
errormessage=str(r),
execute_time=round(end - start, 6),
affected_rows=actual_affected_rows,
sql=exec_sql,
)
execute_result.rows += [result]
except Exception as e:
logger.warning(
f"mongo语句执行报错,语句:{exec_sql},错误信息{traceback.format_exc()}"
)
execute_result.error = str(e)
# result_set.column_list = [i[0] for i in fields] if fields else []
return execute_result
def execute_check(self, db_name=None, sql=""):
"""上线单执行前的检查, 返回Review set"""
line = 1
count = 0
check_result = ReviewSet(full_sql=sql)
# 获取real_row_count参数选项
real_row_count = SysConfig().get("real_row_count", False)
sql = sql.strip()
# sql 检查过滤注释语句
sql = re.sub(r"^\s*//.*$", "", sql, flags=re.MULTILINE)
if sql.find(";") < 0:
raise Exception("提交的语句请以分号结尾")
# 以;切分语句,逐句执行
sp_sql = sql.split(";")
# 执行语句
for check_sql in sp_sql:
alert = "" # 警告信息
if not check_sql == "" and check_sql != "\n":
check_sql = check_sql.strip()
# check_sql = f'''{check_sql}'''
# check_sql = check_sql.replace('\n', '') #处理成一行
# 支持的命令列表
supportMethodList = [
"explain",
"bulkWrite",
"convertToCapped",
"createIndex",
"createIndexes",
"deleteOne",
"deleteMany",
"drop",
"dropIndex",
"dropIndexes",
"ensureIndex",
"insert",
"insertOne",
"insertMany",
"remove",
"replaceOne",
"renameCollection",
"update",
"updateOne",
"updateMany",
"createCollection",
"renameCollection",
]
# 需要有表存在为前提的操作
is_exist_premise_method = [
"convertToCapped",
"deleteOne",
"deleteMany",
"drop",
"dropIndex",
"dropIndexes",
"remove",
"replaceOne",
"renameCollection",
"update",
"updateOne",
"updateMany",
"renameCollection",
]
pattern = re.compile(
r"""^db\.createCollection\(([\s\S]*)\)$|^db\.([\w\.-]+)\.(?:[A-Za-z]+)(?:\([\s\S]*\)$)|^db\.getCollection\((?:\s*)(?:'|")([\w\.-]+)('|")(\s*)\)\.([A-Za-z]+)(\([\s\S]*\)$)"""
)
m = pattern.match(check_sql)
if (
m is not None
and (re.search(re.compile(r"}(?:\s*){"), check_sql) is None)
and check_sql.count("{") == check_sql.count("}")
and check_sql.count("(") == check_sql.count(")")
):
sql_str = m.group()
table_name = (
m.group(1) or m.group(2) or m.group(3)
).strip() # 通过正则的组拿到表名
table_name = table_name.replace('"', "").replace("'", "")
table_names = self.get_all_tables(db_name).rows
is_in = table_name in table_names # 检查表是否存在
if not is_in:
alert = f"\n提示:{table_name}文档不存在!"
if sql_str:
count = 0
if (
sql_str.find("createCollection") > 0
): # 如果是db.createCollection()
methodStr = "createCollection"
alert = ""
if is_in:
check_result.error = "文档已经存在"
result = ReviewResult(
id=line,
errlevel=2,
stagestatus="文档已经存在",
errormessage="文档已经存在!",
affected_rows=count,
sql=check_sql,
)
check_result.rows += [result]
continue
else:
methodStr = sql_str.split(").")[-1].split("(")[0].strip()
if "." in methodStr:
methodStr = methodStr.split(".")[-1]
if methodStr in is_exist_premise_method and not is_in:
check_result.error = "文档不存在"
result = ReviewResult(
id=line,
errlevel=2,
stagestatus="文档不存在",
errormessage=f"文档不存在,不能进行{methodStr}操作!",
sql=check_sql,
)
check_result.rows += [result]
continue
if methodStr in supportMethodList: # 检查方法是否支持
if (
methodStr == "createIndex"
or methodStr == "createIndexes"
or methodStr == "ensureIndex"
): # 判断是否创建索引,如果大于500万,提醒不能在高峰期创建
p_back = re.compile(
r"""(['"])(?:(?!\1)background)\1(?:\s*):(?:\s*)true|background\s*:\s*true|(['"])(?:(?!\1)background)\1(?:\s*):(?:\s*)(['"])(?:(?!\2)true)\2""",
re.M,
)
m_back = re.search(p_back, check_sql)
if m_back is None:
count = 5555555
check_result.warning = "创建索引请加background:true"
check_result.warning_count += 1
result = ReviewResult(
id=line,
errlevel=2,
stagestatus="后台创建索引",
errormessage="创建索引没有加 background:true" + alert,
sql=check_sql,
)
elif not is_in:
count = 0
else:
count = self.get_table_conut(
table_name, db_name
) # 获得表的总条数
if count >= 5000000:
check_result.warning = (
alert + "大于500万条,请在业务低谷期创建索引"
)
check_result.warning_count += 1
result = ReviewResult(
id=line,
errlevel=1,
stagestatus="大表创建索引",
errormessage="大于500万条,请在业务低谷期创建索引!",
affected_rows=count,
sql=check_sql,
)
if count < 5000000:
# 检测通过
affected_all_row_method = [
"drop",
"dropIndex",
"dropIndexes",
"createIndex",
"createIndexes",
"ensureIndex",
]
if methodStr not in affected_all_row_method:
count = 0
else:
count = self.get_table_conut(
table_name, db_name
) # 获得表的总条数
result = ReviewResult(
id=line,
errlevel=0,
stagestatus="Audit completed",
errormessage="检测通过",
affected_rows=count,
sql=check_sql,
execute_time=0,
)
if real_row_count:
if methodStr == "insertOne":
count = 1
elif methodStr in ("insert", "insertMany"):
insert_str = re.search(
rf"{methodStr}\((.*)\)", sql_str, re.S
).group(1)
first_char = insert_str.replace(" ", "").replace(
"\n", ""
)[0]
if first_char == "{":
count = 1
elif first_char == "[":
insert_values = re.search(
r"\[(.*?)\]", insert_str, re.S
).group(0)
de = JsonDecoder()
insert_values = de.decode(insert_values)
count = len(insert_values)
else:
count = 0
elif methodStr in (
"update",
"updateOne",
"updateMany",
"deleteOne",
"deleteMany",
"remove",
):
if sql_str.find("find(") > 0:
count_sql = sql_str.replace(methodStr, "count")
else:
count_sql = (
sql_str.replace(methodStr, "find")
+ ".count()"
)
query_dict = self.parse_query_sentence(count_sql)
count_sql = f"""db.getCollection("{query_dict["collection"]}").find({query_dict["condition"]}).count()"""
query_result = self.query(db_name, count_sql)
count = json.loads(query_result.rows[0][0]).get(
"count", 0
)
if (
methodStr == "update"
and "multi:true"
not in sql_str.replace(" ", "")
.replace('"', "")
.replace("'", "")
.replace("\n", "")
) or methodStr in ("deleteOne", "updateOne"):
count = 1 if count > 0 else 0
if methodStr in (
"insertOne",
"insert",
"insertMany",
"update",
"updateOne",
"updateMany",
"deleteOne",
"deleteMany",
"remove",
):
result = ReviewResult(
id=line,
errlevel=0,
stagestatus="Audit completed",
errormessage="检测通过",
affected_rows=count,
sql=check_sql,
execute_time=0,
)
else:
result = ReviewResult(
id=line,
errlevel=2,
stagestatus="驳回不支持语句",
errormessage="仅支持DML和DDL语句,如需查询请使用数据库查询功能!",
sql=check_sql,
)
else:
check_result.error = "语法错误"
result = ReviewResult(
id=line,
errlevel=2,
stagestatus="语法错误",
errormessage="请检查语句的正确性或(){} },{是否正确匹配!",
sql=check_sql,
)
check_result.rows += [result]
line += 1
count = 0
check_result.column_list = ["Result"] # 审核结果的列名
check_result.checked = True
check_result.warning = self.warning
# 统计警告和错误数量
for r in check_result.rows:
if r.errlevel == 1:
check_result.warning_count += 1
if r.errlevel == 2:
check_result.error_count += 1
return check_result
def get_connection(self, db_name=None):
self.db_name = db_name or self.instance.db_name or "admin"
auth_db = self.instance.db_name or "admin"
self.conn = pymongo.MongoClient(
self.host,
self.port,
authSource=auth_db,
connect=True,
connectTimeoutMS=10000,
)
if self.user and self.password:
self.conn[self.db_name].authenticate(self.user, self.password, auth_db)
return self.conn
def close(self):
if self.conn:
self.conn.close()
self.conn = None
name = "Mongo"
info = "Mongo engine"
def get_roles(self):
sql_get_roles = "db.system.roles.find({},{_id:1})"
result_set = self.query("admin", sql_get_roles)
rows = ["read", "readWrite", "userAdminAnyDatabase"]
for row in result_set.rows:
rows.append(row[1])
result_set.rows = rows
return result_set
def get_all_databases(self):
result = ResultSet()
conn = self.get_connection()
try:
result.rows = conn.list_database_names()
except OperationFailure:
result.rows = [self.db_name]
return result
def get_all_tables(self, db_name, **kwargs):
result = ResultSet()
conn = self.get_connection()
db = conn[db_name]
result.rows = db.list_collection_names()
return result
def get_all_columns_by_tb(self, db_name, tb_name, **kwargs):
"""获取所有字段, 返回一个ResultSet"""
# https://github.com/getredash/redash/blob/master/redash/query_runner/mongodb.py
result = ResultSet()
db = self.get_connection()[db_name]
collection_name = tb_name
documents_sample = []
if "viewOn" in db[collection_name].options():
for d in db[collection_name].find().limit(2):
documents_sample.append(d)
else:
for d in db[collection_name].find().sort([("_id", 1)]).limit(1):
documents_sample.append(d)
for d in db[collection_name].find().sort([("_id", -1)]).limit(1):
documents_sample.append(d)
columns = []
# _merge_property_names
for document in documents_sample:
for prop in document:
if prop not in columns:
columns.append(prop)
result.column_list = ["COLUMN_NAME"]
result.rows = columns
return result
def describe_table(self, db_name, tb_name, **kwargs):
"""return ResultSet 类似查询"""
result = self.get_all_columns_by_tb(db_name=db_name, tb_name=tb_name)
result.rows = [
[
[r],
]
for r in result.rows
]
return result
@staticmethod
def dispose_str(parse_sql, start_flag, index):
"""解析处理字符串"""
stop_flag = ""
while index < len(parse_sql):
if parse_sql[index] == stop_flag and parse_sql[index - 1] != "\\":
return index
index += 1
stop_flag = start_flag
raise Exception("near column %s,' or \" has no close" % index)
def dispose_pair(self, parse_sql, index, begin, end):
"""解析处理需要配对的字符{}[]() 检索一个左括号计数器加1,右括号计数器减1"""
start_pos = -1
stop_pos = 0
count = 0
while index < len(parse_sql):
char = parse_sql[index]
if char == begin:
count += 1
if start_pos == -1:
start_pos = index
if char == end:
count -= 1
if count == 0:
stop_pos = index + 1
break
if char in ("'", '"'): # 避免字符串中带括号的情况,如{key:"{dd"}
index = self.dispose_str(parse_sql, char, index)
index += 1
if count > 0:
raise Exception(
"near column %s, The symbol %s has no closed" % (index, begin)
)
re_char = parse_sql[start_pos:stop_pos] # 截取
return index, re_char
def parse_query_sentence(self, parse_sql):
"""解析mongodb的查询语句,返回一个字典"""
index = 0
query_dict = {}
# 开始解析查询语句
while index < len(parse_sql):
char = parse_sql[index]
if char == "(":
# 获得语句中的方法名
head_sql = parse_sql[:index]
method = parse_sql[:index].split(".")[-1].strip()
index, re_char = self.dispose_pair(parse_sql, index, "(", ")")
re_char = re_char.lstrip("(").rstrip(")")
# 获得表名
if method and "collection" not in query_dict:
collection = head_sql.replace("." + method, "").replace("db.", "")
query_dict["collection"] = collection
# 分割查询条件和投影(返回字段)
if method == "find":
p_index, condition = self.dispose_pair(re_char, 0, "{", "}")
query_dict["condition"] = condition
query_dict["method"] = method
# 获取查询返回字段
projection = re_char[p_index:].strip()[2:]
if projection:
query_dict["projection"] = projection
# 聚合查询
elif method == "aggregate":
pipeline = []
agg_index = 0
while agg_index < len(re_char):
p_index, condition = self.dispose_pair(
re_char, agg_index, "{", "}"
)
agg_index = p_index + 1
if condition:
de = JsonDecoder()
step = de.decode(condition)
if "$sort" in step:
sort_list = []
for name, direction in step["$sort"].items():
sort_list.append((name, direction))
step["$sort"] = SON(sort_list)
pipeline.append(step)
query_dict["condition"] = pipeline
query_dict["method"] = method
elif method.lower() == "getcollection": # 获得表名
collection = re_char.strip().replace("'", "").replace('"', "")
query_dict["collection"] = collection
elif method.lower() == "getindexes":
query_dict["method"] = "index_information"
else:
query_dict[method] = re_char
index += 1
logger.debug(query_dict)
if query_dict:
return query_dict
def filter_sql(self, sql="", limit_num=0):
"""给查询语句改写语句, 返回修改后的语句"""
sql = sql.split(";")[0].strip()
# 执行计划
if sql.startswith("explain"):
sql = sql.replace("explain", "") + ".explain()"
return sql.strip()
def query_check(self, db_name=None, sql=""):
"""提交查询前的检查"""
sql = sql.strip()
if sql.startswith("explain"):
sql = sql[7:] + ".explain()"
sql = re.sub("[;\s]*.explain\(\)$", ".explain()", sql).strip()
result = {"msg": "", "bad_query": False, "filtered_sql": sql, "has_star": False}
pattern = re.compile(
r"""^db\.(\w+\.?)+(?:\([\s\S]*\)(\s*;*)$)|^db\.getCollection\((?:\s*)(?:'|")(\w+\.?)+('|")(\s*)\)\.([A-Za-z]+)(\([\s\S]*\)(\s*;*)$)"""
)
m = pattern.match(sql)
if m is not None:
logger.debug(sql)
query_dict = self.parse_query_sentence(sql)
if "method" not in query_dict: