Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoQuote committed Feb 26, 2025
1 parent 3a25c42 commit 5edd64a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 49 deletions.
53 changes: 16 additions & 37 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
password_module = importlib.import_module(file)
PasswordMixin = getattr(password_module, _class)
except (ImportError, AttributeError) as e:
logger.error(f"failed to import password minxin {settings.PASSWORD_MIXIN_PATH}, {str(e)}")
logger.error(
f"failed to import password minxin {settings.PASSWORD_MIXIN_PATH}, {str(e)}"

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This expression logs
sensitive data (password)
as clear text.
)
logger.error(f"falling back to dummy mixin")
from sql.plugins.password import DummyMixin

PasswordMixin = DummyMixin


Expand All @@ -39,9 +42,7 @@ class ResourceGroup(models.Model):
ding_webhook = models.CharField("钉钉webhook地址", max_length=255, blank=True)
feishu_webhook = models.CharField("飞书webhook地址", max_length=255, blank=True)
qywx_webhook = models.CharField("企业微信webhook地址", max_length=255, blank=True)
is_deleted = models.IntegerField(
"是否删除", choices=((0, "否"), (1, "是")), default=0
)
is_deleted = models.IntegerField("是否删除", choices=((0, "否"), (1, "是")), default=0)
create_time = models.DateTimeField(auto_now_add=True)
sys_time = models.DateTimeField(auto_now=True)

Expand All @@ -65,9 +66,7 @@ class Users(AbstractUser):
wx_user_id = models.CharField("企业微信UserID", max_length=64, blank=True)
feishu_open_id = models.CharField("飞书OpenID", max_length=64, blank=True)
failed_login_count = models.IntegerField("失败计数", default=0)
last_login_failed_at = models.DateTimeField(
"上次失败登录时间", blank=True, null=True
)
last_login_failed_at = models.DateTimeField("上次失败登录时间", blank=True, null=True)
resource_group = models.ManyToManyField(
ResourceGroup, verbose_name="资源组", blank=True
)
Expand Down Expand Up @@ -246,9 +245,7 @@ class Instance(models.Model, PasswordMixin):
resource_group = models.ManyToManyField(
ResourceGroup, verbose_name="资源组", blank=True
)
instance_tag = models.ManyToManyField(
InstanceTag, verbose_name="实例标签", blank=True
)
instance_tag = models.ManyToManyField(InstanceTag, verbose_name="实例标签", blank=True)
tunnel = models.ForeignKey(
Tunnel,
verbose_name="连接隧道",
Expand Down Expand Up @@ -344,9 +341,7 @@ class SqlWorkflow(models.Model, WorkflowAuditMixin):
run_date_end = models.DateTimeField("可执行结束时间", null=True, blank=True)
create_time = models.DateTimeField("创建时间", auto_now_add=True)
finish_time = models.DateTimeField("结束时间", null=True, blank=True)
is_manual = models.IntegerField(
"是否原生执行", choices=((0, "否"), (1, "是")), default=0
)
is_manual = models.IntegerField("是否原生执行", choices=((0, "否"), (1, "是")), default=0)

def __str__(self):
return self.workflow_name
Expand Down Expand Up @@ -390,9 +385,7 @@ class WorkflowAudit(models.Model):
workflow_id = models.BigIntegerField("关联业务id")
workflow_type = models.IntegerField("申请类型", choices=WorkflowType.choices)
workflow_title = models.CharField("申请标题", max_length=50)
workflow_remark = models.CharField(
"申请备注", default="", max_length=140, blank=True
)
workflow_remark = models.CharField("申请备注", default="", max_length=140, blank=True)
audit_auth_groups = models.CharField("审批权限组列表", max_length=255)
current_audit = models.CharField("当前审批权限组", max_length=20)
next_audit = models.CharField("下级审批权限组", max_length=20)
Expand Down Expand Up @@ -479,9 +472,7 @@ class WorkflowLog(models.Model):

id = models.AutoField(primary_key=True)
audit_id = models.IntegerField("工单审批id", db_index=True)
operation_type = models.SmallIntegerField(
"操作类型", choices=WorkflowAction.choices
)
operation_type = models.SmallIntegerField("操作类型", choices=WorkflowAction.choices)
# operation_type_desc 字段实际无意义
operation_type_desc = models.CharField("操作类型描述", max_length=10)
operation_info = models.CharField("操作信息", max_length=1000)
Expand Down Expand Up @@ -651,16 +642,12 @@ class DataMaskingColumns(models.Model):
choices=rule_type_choices,
help_text="三段式通用脱敏规则:根据字段长度自动分成三份,中间段脱敏。",
)
active = models.BooleanField(
"激活状态", choices=((False, "未激活"), (True, "激活"))
)
active = models.BooleanField("激活状态", choices=((False, "未激活"), (True, "激活")))
instance = models.ForeignKey(Instance, on_delete=models.CASCADE)
table_schema = models.CharField("字段所在库名", max_length=64)
table_name = models.CharField("字段所在表名", max_length=64)
column_name = models.CharField("字段名", max_length=64)
column_comment = models.CharField(
"字段描述", max_length=1024, default="", blank=True
)
column_comment = models.CharField("字段描述", max_length=1024, default="", blank=True)
create_time = models.DateTimeField(auto_now_add=True)
sys_time = models.DateTimeField(auto_now=True)

Expand Down Expand Up @@ -699,12 +686,8 @@ class InstanceAccount(models.Model):

instance = models.ForeignKey(Instance, on_delete=models.CASCADE)
user = fields.EncryptedCharField(verbose_name="账号", max_length=128)
host = models.CharField(
verbose_name="主机", max_length=64
) # mysql数据库存储主机信息
db_name = models.CharField(
verbose_name="数据库名称", max_length=128
) # mongo数据库存储数据库名称
host = models.CharField(verbose_name="主机", max_length=64) # mysql数据库存储主机信息
db_name = models.CharField(verbose_name="数据库名称", max_length=128) # mongo数据库存储数据库名称
password = fields.EncryptedCharField(
verbose_name="密码", max_length=128, default="", blank=True
)
Expand All @@ -727,9 +710,7 @@ class InstanceDatabase(models.Model):
instance = models.ForeignKey(Instance, on_delete=models.CASCADE)
db_name = models.CharField("数据库名", max_length=128)
owner = models.CharField("负责人", max_length=50, default="", blank=True)
owner_display = models.CharField(
"负责人中文名", max_length=50, default="", blank=True
)
owner_display = models.CharField("负责人中文名", max_length=50, default="", blank=True)
remark = models.CharField("备注", max_length=255, default="", blank=True)
sys_time = models.DateTimeField("系统修改时间", auto_now=True)

Expand Down Expand Up @@ -822,9 +803,7 @@ class ArchiveConfig(models.Model, WorkflowAuditMixin):
)
state = models.BooleanField("是否启用归档", default=True)
user_name = models.CharField("申请人", max_length=30, blank=True, default="")
user_display = models.CharField(
"申请人中文名", max_length=50, blank=True, default=""
)
user_display = models.CharField("申请人中文名", max_length=50, blank=True, default="")
create_time = models.DateTimeField("创建时间", auto_now_add=True)
last_archive_time = models.DateTimeField("最近归档时间", blank=True, null=True)
sys_time = models.DateTimeField("系统时间修改", auto_now=True)
Expand Down
32 changes: 20 additions & 12 deletions sql/plugins/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class DummyMixin:
"""mixin 模板, 用于提供一些基础的方法, 给其他 mixin 继承
默认从schema 中直接提取 username 和 password
"""

def get_username_password(self):
return self.user, self.password

Expand All @@ -22,7 +23,7 @@ def get_username_password(self):
"instance_name": {
"username": "username",
"password": "password",
"expires_at": "1740557906.15272"
"expires_at": "1740557906.15272",
}
}

Expand All @@ -34,21 +35,28 @@ class VaultMixin(DummyMixin):
不使用任何 token, 适合 vault-proxy 部署方式, 如需其他部署方式, 可继承后修改配置
使用的是 static secret, 如需其他获取方式, 可继承后修改配置
"""

vault_server = "localhost:8200"
vault_token = ""

def get_username_password(self):
if self.instance_name in password_cache:
if password_cache[self.instance_name]['expires_at'] > time.time():
return password_cache[self.instance_name]['username'], password_cache[self.instance_name]['password']

vault_role = f"{self.instance_name}-archery-rw"
response = requests.get(f"http://{self.vault_server}/v1/database/static-creds/{vault_role}",
headers={"X-Vault-Token": self.vault_token})
if password_cache[self.instance_name]["expires_at"] > time.time():
return (
password_cache[self.instance_name]["username"],
password_cache[self.instance_name]["password"],
)

vault_role = f"{self.instance_name}-archery-rw"
response = requests.get(
f"http://{self.vault_server}/v1/database/static-creds/{vault_role}",
headers={"X-Vault-Token": self.vault_token},
)
response.raise_for_status()
data = response.json()['data']
data = response.json()["data"]
password_cache[self.instance_name] = {
"username": data['username'],
"password": data['password'],
"expires_at": time.time() + data['ttl'] - 60
"username": data["username"],
"password": data["password"],
"expires_at": time.time() + data["ttl"] - 60,
}
return data['username'], data['password']
return data["username"], data["password"]

0 comments on commit 5edd64a

Please sign in to comment.