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

fix(dbm-services): 修复spider工具体验bugs #1083

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
11 changes: 6 additions & 5 deletions dbm-services/mysql/db-simulation/app/service/kubernets.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (k *DbPodSets) CreateClusterPod() (err error) {
Command: []string{"/bin/bash", "-c", fmt.Sprintf("mysql -uroot -p%s -e 'select 1'", k.BaseInfo.RootPwd)},
},
},
InitialDelaySeconds: 2,
InitialDelaySeconds: 3,
PeriodSeconds: 5,
},
}, {
Expand All @@ -156,7 +156,7 @@ func (k *DbPodSets) CreateClusterPod() (err error) {
Command: []string{"/bin/bash", "-c", fmt.Sprintf("mysql -uroot -p%s -e 'select 1'", k.BaseInfo.RootPwd)},
},
},
InitialDelaySeconds: 2,
InitialDelaySeconds: 3,
PeriodSeconds: 5,
},
},
Expand All @@ -179,7 +179,7 @@ func (k *DbPodSets) CreateClusterPod() (err error) {
Command: []string{"/bin/bash", "-c", fmt.Sprintf("mysql -uroot -p%s -e 'select 1'", k.BaseInfo.RootPwd)},
},
},
InitialDelaySeconds: 2,
InitialDelaySeconds: 3,
PeriodSeconds: 5,
},
},
Expand All @@ -193,6 +193,7 @@ func (k *DbPodSets) CreateClusterPod() (err error) {
logger.Info("connect tdbctl success ~")
// create cluster relation
for _, ql := range k.getCreateClusterSqls() {
logger.Info("exec init cluster sql %s", ql)
if _, err = k.DbWork.Db.Exec(ql); err != nil {
return err
}
Expand Down Expand Up @@ -289,8 +290,8 @@ func (k *DbPodSets) DeletePod() (err error) {
// GetLoadSchemaSQLCmd TODO
func (k *DbPodSets) GetLoadSchemaSQLCmd(bkpath, file string) (cmd string) {
cmd = fmt.Sprintf(
"curl -o %s %s && mysql --defaults-file=/etc/my.cnf -uroot -p%s --default-character-set=%s -vvv < %s",
file, getdownloadUrl(bkpath, file), k.BaseInfo.RootPwd, k.BaseInfo.Charset, file)
`curl -o %s %s && sed -i '/50720 SET tc_admin=0/d' %s && mysql -uroot -p%s --default-character-set=%s -vvv < %s`,
file, getdownloadUrl(bkpath, file), file, k.BaseInfo.RootPwd, k.BaseInfo.Charset, file)
return
}

Expand Down
9 changes: 7 additions & 2 deletions dbm-services/mysql/db-simulation/app/syntax/builtin_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const (
// SpecialCharRegex = `[¥$!@#%^&*()+={}\[\];:'"<>,.?/\\| ]`

// AllowWordRegex TODO
AllowWordRegex = `^[a-zA-Z0-9][a-zA-Z0-9_-]+[a-zA-Z0-9]$`
AllowWordRegex = `^[a-zA-Z0-9]([a-zA-Z0-9_-])*[a-zA-Z0-9]$`
// SysReservesPrefixName TODO

)

var reAllowWord *regexp.Regexp
var reAllowOneWord *regexp.Regexp

// var mysql55WordMap map[string]struct{}
var mysql56WordMap map[string]struct{}
Expand All @@ -39,6 +40,7 @@ var sysReservesPrefixNames []string
func init() {
sysReservesPrefixNames = []string{"stage_truncate"}
reAllowWord = regexp.MustCompile(AllowWordRegex)
reAllowOneWord = regexp.MustCompile(`^[a-zA-Z0-9]$`)
mysql56WordMap = sliceToMap(keyworld.MySQL56_KEYWORD)
mysql57WordMap = sliceToMap(keyworld.MySQL57_KEYWORD)
mysql80WordMap = sliceToMap(keyworld.MySQL80_KEYWORD)
Expand Down Expand Up @@ -66,8 +68,11 @@ func KeyWordValidator(ver, name string) (matched bool, msg string) {

// SpecialCharValidator TODO
func SpecialCharValidator(name string) (matched bool, msg string) {
if reAllowOneWord.MatchString(name) {
return false, ""
}
if !reAllowWord.MatchString(name) {
return true, "Only allowed " + AllowWordRegex + " characters "
return true, name + " : Must match the regexp " + AllowWordRegex + " characters "
}
for _, sysPrefix := range sysReservesPrefixNames {
re := regexp.MustCompile(fmt.Sprintf("^%s", sysPrefix))
Expand Down
16 changes: 15 additions & 1 deletion dbm-services/mysql/db-simulation/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

// Package handler TODO
package handler

Expand Down Expand Up @@ -80,6 +90,10 @@ func SpiderClusterSimulation(r *gin.Context) {
DbPodSets: service.NewDbPodSets(),
BaseParam: &param.BaseParam,
}
rootPwd := cmutil.RandStr(10)
if !service.DelPod {
logger.Info("the pwd %s", rootPwd)
}
tsk.DbImage = img
tsk.SpiderImage = param.GetSpiderImg()
tsk.TdbCtlImage = param.GetTdbctlImg()
Expand All @@ -88,7 +102,7 @@ func SpiderClusterSimulation(r *gin.Context) {
replaceUnderSource(param.TaskId)),
Lables: map[string]string{"task_id": replaceUnderSource(param.TaskId),
"request_id": requestId},
RootPwd: cmutil.RandStr(10),
RootPwd: rootPwd,
Charset: param.MySQLCharSet,
}
service.SpiderTaskChan <- tsk
Expand Down
109 changes: 76 additions & 33 deletions dbm-services/mysql/db-simulation/rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,103 @@ BuiltInRule:

CommandRule:
HighRiskCommandRule:
expr: ' Val in Item '
desc: '高危命令'
item: [ "drop_table", "drop_index", "lock_tables", "analyze","rename_table", "drop_procedure", "drop_view", "drop_trigger","drop_function", "drop_server", "drop_event", "drop_compression_dictionary","optimize", "alter_tablespace"]

expr: " Val in Item "
desc: "高危命令"
item:
[
"drop_table",
"drop_index",
"lock_tables",
"analyze",
"rename_table",
"drop_procedure",
"drop_view",
"drop_trigger",
"drop_function",
"drop_server",
"drop_event",
"drop_compression_dictionary",
"optimize",
"alter_tablespace",
]

BanCommandRule:
expr: ' Val in Item '
desc: '禁用命令'
expr: " Val in Item "
desc: "禁用命令"
ban: true
item: ["truncate", "revoke", "kill", "reset", "drop_db","drop_user", "grant","create_user", "revoke_all", "shutdown", "lock_tables_for_backup","reset", "purge", "lock_binlog_for_backup","lock_tables_for_backup","install_plugin", "uninstall_plugin","alter_user"]

item:
[
"truncate",
"revoke",
"kill",
"reset",
"drop_db",
"drop_user",
"grant",
"create_user",
"revoke_all",
"shutdown",
"lock_tables_for_backup",
"reset",
"purge",
"lock_binlog_for_backup",
"lock_tables_for_backup",
"install_plugin",
"uninstall_plugin",
"alter_user",
]

CreateTableRule:
SuggestBlobColumCount:
expr: ' Val >= Item '
item: 10
desc: "建议单表Blob字段不要过多"
expr: " Val >= Item "
item: 10
desc: "建议单表Blob字段不要过多"
SuggestEngine:
expr: ' not (Val contains Item) and ( len(Val) != 0 ) '
item: 'innodb'
desc: "建议使用Innodb表"
expr: " not (Val contains Item) and ( len(Val) != 0 ) "
item: "innodb"
desc: "建议使用Innodb表"
NeedPrimaryKey:
expr: ' Val == Item '
item: 1
desc: "建议包含主键"
expr: " Val == Item "
item: 1
desc: "建议包含主键"
DefinerRule:
expr: ' Val in Item '
desc: '不允许指定definer'
ban: true
item: ["create_function","create_trigger","create_event","create_procedure","create_view"]
expr: " Val in Item "
desc: "不允许指定definer"
ban: true
item:
[
"create_function",
"create_trigger",
"create_event",
"create_procedure",
"create_view",
]
NormalizedName:
expr: ' Val in Item '
desc: '规范化命名'
item: ["first_char_exception", "special_char", "Keyword_exception"]
expr: " Val in Item "
desc: "规范化命名"
item: ["first_char_exception", "special_char", "Keyword_exception"]

AlterTableRule:
HighRiskType:
expr: ' Val in Item '
item: ["drop_column", "drop_key","change_column","rename_table", "rename_key"]
expr: " Val in Item "
item:
["drop_column", "drop_key", "change_column", "rename_table", "rename_key"]
desc: "高危变更类型"
HighRiskPkAlterType:
expr: ' Val in Item '
expr: " Val in Item "
item: ["add_column", "add_key", "change_column"]
desc: "高危主键变更类型"
AlterUseAfter:
expr: ' Val != Item '
expr: " Val != Item "
item: ""
desc: "变更表时使用了after"
AddColumnMixed:
expr: ' ( Item in Val ) && ( len(Val) > 1 ) '
item: 'add_column'
AddColumnMixed:
expr: " ( Item in Val ) && ( len(Val) > 1 ) "
item: "add_column"
desc: "加字段和其它alter table 类型混用,可能导致非在线加字段"

DmlRule:
DmlNotHasWhere:
# expr: ' Val != Item '
item: true
# expr: ' Val != Item '
item: true
desc: "没有使用WHERE或者LIMIT,可能会导致全表数据更改"
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/db-simulation/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.32
appVersion: 0.0.1-alpha.33
ymakedaq marked this conversation as resolved.
Show resolved Hide resolved
description: A Helm chart for Kubernetes
name: db-simulation
type: application
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/templates/dbsimulation-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data:
- version: "8.0"
image: "{{ .Values.global.imageRegistry | default "mirrors.tencent.com" }}/build/blueking/tendb-8.0.18:4.1_v2"
- version: "tdbctl"
image: "{{ .Values.global.imageRegistry | default "mirrors.tencent.com" }}/build/blueking/tdbctl:2.4.1_v3"
image: "{{ .Values.global.imageRegistry | default "mirrors.tencent.com" }}/build/blueking/tdbctl:2.4.3_v1"
- version: "spider"
image: "{{ .Values.global.imageRegistry | default "mirrors.tencent.com" }}/build/blueking/tspider3:3.7.8_v1"
dbconf:
Expand Down
Loading