Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实例增加3个字段:是否验证服务端SSL证书、显示的数据库列表、隐藏的数据库列表 #2781

Merged
merged 28 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
01d4643
添加favicon图片
May 9, 2024
ccbb373
firset
Jun 20, 2024
3b84684
修改
Jun 20, 2024
9f66a6c
撤销
Jun 20, 2024
d1424f1
增加忽略证书错误
feiazifeiazi Sep 2, 2024
7e7f87d
支持-允许显示的数据库列表
feiazifeiazi Sep 2, 2024
407e8e4
筛选数据库列表
feiazifeiazi Sep 2, 2024
f9347e8
单元测试X
feiazifeiazi Sep 2, 2024
de90db4
增加过滤数据库选项
feiazifeiazi Sep 5, 2024
219bac4
show_db_name_regex
feiazifeiazi Sep 5, 2024
9a29c48
增加数据库过滤
feiazifeiazi Sep 5, 2024
6c9eeca
是否验证服务端SSL证书
feiazifeiazi Sep 5, 2024
9945b24
是否验证服务端SSL证书
feiazifeiazi Sep 5, 2024
0e87771
是否验证服务端SSL证书
feiazifeiazi Sep 5, 2024
ab8d940
是否验证服务端SSL证书
feiazifeiazi Sep 5, 2024
7ddab99
使用self.instance属性
feiazifeiazi Sep 5, 2024
0828a9c
使用ins属性
feiazifeiazi Sep 5, 2024
99daa5f
使用Instance属性
feiazifeiazi Sep 5, 2024
3753992
合并数据库过滤方法
feiazifeiazi Sep 5, 2024
e05ec68
filter_db_list 修改为统一出口的地方
feiazifeiazi Sep 19, 2024
9e3168d
过滤数据库改为统一出口地方
feiazifeiazi Sep 19, 2024
1d47729
修改filter_db_list
feiazifeiazi Sep 19, 2024
7f98347
添加单元测试
feiazifeiazi Sep 19, 2024
7ca276b
sql_utils
feiazifeiazi Sep 19, 2024
1b7cd73
resource.rows
feiazifeiazi Sep 19, 2024
978b85a
回滚mysql.py
feiazifeiazi Sep 19, 2024
a03674e
格式问题
feiazifeiazi Sep 19, 2024
5e69682
改为命名参数
feiazifeiazi Sep 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sql/engines/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""engine base库, 包含一个``EngineBase`` class和一个get_engine函数"""

import importlib
import re
from sql.engines.models import ResultSet, ReviewSet
from sql.models import Instance
from sql.utils.ssh_tunnel import SSHConnection
from django.conf import settings

Expand All @@ -14,11 +16,11 @@ class EngineBase:
name = "Base"
info = "base engine"

def __init__(self, instance=None):
def __init__(self, instance: Instance = None):
self.conn = None
self.thread_id = None
if instance:
self.instance = instance
self.instance = instance # type: Instance
self.instance_name = instance.instance_name
self.host = instance.host
self.port = int(instance.port)
Expand Down
5 changes: 3 additions & 2 deletions sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,9 +848,10 @@ def get_all_databases(self):
result = ResultSet()
conn = self.get_connection()
try:
result.rows = conn.list_database_names()
db_list = conn.list_database_names()
except OperationFailure:
result.rows = [self.db_name]
db_list = [self.db_name]
result.rows = db_list
return result

def get_all_tables(self, db_name, **kwargs):
Expand Down
11 changes: 11 additions & 0 deletions sql/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from common.utils.convert import Convert
from sql.engines import get_engine
from sql.plugins.schemasync import SchemaSync
from sql.utils.sql_utils import filter_db_list
from .models import Instance, ParamTemplate, ParamHistory


Expand Down Expand Up @@ -332,6 +333,16 @@ def instance_resource(request):
tb_name = query_engine.escape_string(tb_name)
if resource_type == "database":
resource = query_engine.get_all_databases()
resource.rows = filter_db_list(
db_list=resource.rows,
db_name_regex=query_engine.instance.show_db_name_regex,
is_match_regex=True,
)
resource.rows = filter_db_list(
db_list=resource.rows,
db_name_regex=query_engine.instance.denied_db_name_regex,
is_match_regex=False,
)
elif resource_type == "schema" and db_name:
resource = query_engine.get_all_schemas(db_name=db_name)
elif resource_type == "table" and db_name:
Expand Down
16 changes: 16 additions & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,23 @@ class Instance(models.Model):
verbose_name="密码", max_length=300, default="", blank=True
)
is_ssl = models.BooleanField("是否启用SSL", default=False)
verify_ssl = models.BooleanField("是否验证服务端SSL证书", default=True)
db_name = models.CharField("数据库", max_length=64, default="", blank=True)
show_db_name_regex = models.CharField(
"显示的数据库列表正则",
max_length=1024,
default="",
blank=True,
help_text="正则表达式。示例:^(test_db|dmp_db|za.*)$。Redis示例: ^(0|4|6|11|12|13)$",
)
denied_db_name_regex = models.CharField(
"隐藏的数据库列表正则",
max_length=1024,
default="",
blank=True,
help_text="正则表达式。隐藏大于显示,此规则优先。",
)

charset = models.CharField("字符集", max_length=20, default="", blank=True)
service_name = models.CharField(
"Oracle service name", max_length=50, null=True, blank=True
Expand Down
34 changes: 34 additions & 0 deletions sql/utils/sql_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,37 @@ def get_exec_sqlitem_list(reviewResult, db_name):
)
)
return list


def filter_db_list(db_list, db_name_regex: str, is_match_regex: bool, key="value"):
"""
根据配置的数据库列表过滤数据库名称列表。

:param db_list: 待过滤的数据库名称列表,可能是字符串列表或字典列表。
示例数据:
1. db_list=[{"value": 0, "text": 0, "value": 1, "text": 1}]
2. db_list=["a_db","b_db"]
:param db_name_regex: 配置的数据库正则。
:param is_match_regex 是匹配还是不匹配的正则
:param key: 当 db_list 包含字典时,指定用于匹配的键。默认值为 'value'。
:return: 过滤后的数据库名称列表或字典列表。
"""
if not db_name_regex:
return db_list # 如果没有指定 db_name_regex,返回原始 db_list

try:
db_regex = re.compile(db_name_regex) # 编译正则表达式
except re.error:
raise ValueError(f"正则表达式解析异常: {db_name_regex}")

filtered_list = []

# 根据类型处理 db_list
for db in db_list:
# 确定要检查的值(字符串或字典中的特定键)
db_value = str(db[key]) if isinstance(db, dict) else db
is_match = bool(db_regex.match(db_value))
# 根据 is_match_regex 参数过滤
if (is_match_regex and is_match) or (not is_match_regex and not is_match):
filtered_list.append(db)
return filtered_list
Loading
Loading