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 all 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
4 changes: 2 additions & 2 deletions dbm-services/go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ github.com/cenkalti/backoff/v4 v4.1.2 h1:6Yo7N8UP2K6LWZnW94DLVSSrbobcWdVzAYOisuD
github.com/census-instrumentation/opencensus-proto v0.3.0 h1:t/LhUZLVitR1Ow2YOnduCsavhwFUklBMoGVYUCqmCqk=
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 h1:uH66TXeswKn5PW5zdZ39xEwfS9an067BirqA+P4QaLI=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/checkpoint-restore/go-criu/v4 v4.1.0 h1:WW2B2uxx9KWF6bGlHqhm8Okiafwwx7Y2kcpn8lCpjgo=
github.com/checkpoint-restore/go-criu/v5 v5.3.0 h1:wpFFOoomK3389ue2lAb0Boag6XPht5QYpipxmSNL4d8=
Expand Down Expand Up @@ -856,8 +857,6 @@ github.com/sclevine/agouti v3.0.0+incompatible h1:8IBJS6PWz3uTlMP3YBIR5f+KAldcGu
github.com/sclevine/spec v1.2.0 h1:1Jwdf9jSfDl9NVmt8ndHqbTZ7XCCPbh1jI3hkDBHVYA=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921 h1:58EBmR2dMNL2n/FnbQewK3D14nXr0V9CObDSvMJLq+Y=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371 h1:SWV2fHctRpRrp49VXJ6UZja7gU9QLHwRpIPBN89SKEo=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/vfsgen v0.0.0-20181020040650-a97a25d856ca h1:3fECS8atRjByijiI8yYiuwLwQ2ZxXobW7ua/8GRB3pI=
Expand Down Expand Up @@ -1024,6 +1023,7 @@ golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
Expand Down
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,可能会导致全表数据更改"
114 changes: 57 additions & 57 deletions helm-charts/bk-dbm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
apiVersion: v2
dependencies:
- name: common
repository: https://charts.bitnami.com/bitnami
version: 1.13.0
#- condition: mysql.enabled
# name: mysql
# version: 8.x.x
# repository: https://charts.bitnami.com/bitnami
- condition: redis.enabled
name: redis
version: 16.x.x
repository: https://charts.bitnami.com/bitnami
- name: grafana
repository: file://./charts/grafana
version: 7.x.x
condition: grafana.enabled
- condition: dbm.enabled
name: dbm
repository: file://charts/dbm
version: 0.1.20
- condition: dbconfig.enabled
name: dbconfig
repository: file://charts/dbconfig
version: 0.1.6
- condition: dbpriv.enabled
name: dbpriv
repository: file://charts/dbpriv
version: 0.1.28
- condition: dbpartition.enabled
name: dbpartition
repository: file://charts/dbpartition
version: 0.1.3
- condition: db-simulation.enabled
name: db-simulation
repository: file://charts/db-simulation
version: 0.1.7
- condition: dbRemoteService.enabled
name: db-remote-service
repository: file://charts/db-remote-service
version: 0.9.5
- condition: db-dns-api.enabled
name: db-dns-api
repository: file://charts/db-dns-api
version: 0.1.3
- condition: hadb-api.enabled
name: hadb-api
repository: file://charts/hadb-api
version: 0.0.10
- condition: dbResource.enabled
name: db-resource
repository: file://charts/db-resource
version: 0.0.7
- condition: db-celery-service.enabled
name: db-celery-service
repository: file://charts/db-celery-service
version: 0.0.4
- name: common
repository: https://charts.bitnami.com/bitnami
version: 1.13.0
#- condition: mysql.enabled
# name: mysql
# version: 8.x.x
# repository: https://charts.bitnami.com/bitnami
- condition: redis.enabled
name: redis
version: 16.x.x
repository: https://charts.bitnami.com/bitnami
- name: grafana
repository: file://./charts/grafana
version: 7.x.x
condition: grafana.enabled
- condition: dbm.enabled
name: dbm
repository: file://charts/dbm
version: 0.1.20
- condition: dbconfig.enabled
name: dbconfig
repository: file://charts/dbconfig
version: 0.1.6
- condition: dbpriv.enabled
name: dbpriv
repository: file://charts/dbpriv
version: 0.1.28
- condition: dbpartition.enabled
name: dbpartition
repository: file://charts/dbpartition
version: 0.1.3
- condition: db-simulation.enabled
name: db-simulation
repository: file://charts/db-simulation
version: 0.1.7
- condition: dbRemoteService.enabled
name: db-remote-service
repository: file://charts/db-remote-service
version: 0.9.5
- condition: db-dns-api.enabled
name: db-dns-api
repository: file://charts/db-dns-api
version: 0.1.3
- condition: hadb-api.enabled
name: hadb-api
repository: file://charts/hadb-api
version: 0.0.10
- condition: dbResource.enabled
name: db-resource
repository: file://charts/db-resource
version: 0.0.7
- condition: db-celery-service.enabled
name: db-celery-service
repository: file://charts/db-celery-service
version: 0.0.4
description: A Helm chart for bkdbm
name: bk-dbm
type: application
version: 1.2.0-alpha.26
appVersion: 1.2.0-alpha.26
version: 1.2.0-alpha.27
appVersion: 1.2.0-alpha.27
Loading
Loading