Skip to content

Commit

Permalink
脱敏支持区分大小写
Browse files Browse the repository at this point in the history
  • Loading branch information
woshiyanghai committed Oct 13, 2023
1 parent 6bd0fe1 commit 2efb264
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions sql/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class DataMaskingColumnsAdmin(admin.ModelAdmin):
"table_schema",
"table_name",
"column_name",
"case_sensitive",
"column_comment",
"create_time",
)
Expand Down
1 change: 1 addition & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ class DataMaskingColumns(models.Model):
table_schema = models.CharField("字段所在库名", max_length=64)
table_name = models.CharField("字段所在表名", max_length=64)
column_name = models.CharField("字段名", max_length=64)
case_sensitive = models.BooleanField('字段区分大小写', choices=((False, '不区分'), (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
20 changes: 18 additions & 2 deletions sql/utils/data_masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def analyze_query_tree(select_list, instance):
"""解析select list, 返回命中脱敏规则的列信息"""
# 获取实例全部激活的脱敏字段信息,减少循环查询,提升效率
masking_columns = {
f"{i.instance}-{i.table_schema}-{i.table_name}-{i.column_name}": model_to_dict(
f"{i.instance}-{i.table_schema}-{i.table_name}-{i.column_name}-{i.case_sensitive}": model_to_dict(
i
)
for i in DataMaskingColumns.objects.filter(instance=instance, active=True)
Expand All @@ -103,7 +103,7 @@ def analyze_query_tree(select_list, instance):
column.get("field"),
)
masking_column = masking_columns.get(
f"{instance}-{table_schema}-{table}-{field}"
f"{instance}-{table_schema}-{table}-{field}-{'True'}"
)
if masking_column:
hit_columns.append(
Expand All @@ -117,6 +117,22 @@ def analyze_query_tree(select_list, instance):
"index": column["index"],
}
)
field_0 = field.lower()
masking_column = masking_columns.get(
f"{instance}-{table_schema}-{table}-{field_0}-{'False'}"
)
if masking_column:
hit_columns.append(
{
"instance_name": instance.instance_name,
"table_schema": table_schema,
"table_name": table,
"column_name": field_0,
"rule_type": masking_column["rule_type"],
"is_hit": True,
"index": column["index"],
}
)
return hit_columns


Expand Down
5 changes: 4 additions & 1 deletion src/init_sql/v1.10.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ execute stmt;
drop prepare stmt;
alter table instance_account add unique index uidx_instanceid_user_host_dbname(`instance_id`, `user`, `host`, `db_name`);
--- 增加 ssl 支持
ALTER TABLE sql_instance ADD is_ssl tinyint(1) DEFAULT 0 COMMENT '是否启用SSL';
ALTER TABLE sql_instance ADD is_ssl tinyint(1) DEFAULT 0 COMMENT '是否启用SSL';
-- 脱敏区分大小写支持,添加唯一键,避免同时配置同库同表同列添加两条脱敏规则
ALTER TABLE data_masking_columns ADD COLUMN case_sensitive TINYINT(1) NOT NULL comment '字段是否区分大小写' AFTER column_name;
ALTER TABLE data_masking_columns ADD UNIQUE INDEX (instance_id,table_schema,table_name,column_name);

0 comments on commit 2efb264

Please sign in to comment.