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

feature: 添加enable_any_statement选项以支持所有语法 #301

Merged
merged 2 commits into from
Feb 9, 2021
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
9 changes: 6 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ type Inc struct {
CheckIdentifierUpper bool `toml:"check_identifier_upper" json:"check_identifier_upper"`

// 连接服务器的默认字符集,默认值为utf8mb4
DefaultCharset string `toml:"default_charset" json:"default_charset"`
EnableAlterDatabase bool `toml:"enable_alter_database" json:"enable_alter_database"`
EnableAutoIncrementUnsigned bool `toml:"enable_autoincrement_unsigned" json:"enable_autoincrement_unsigned"`
DefaultCharset string `toml:"default_charset" json:"default_charset"`
EnableAlterDatabase bool `toml:"enable_alter_database" json:"enable_alter_database"`
// 允许执行任意语法类型.该设置有安全要求,仅支持配置文件方式设置
EnableAnyStatement bool `toml:"enable_any_statement" json:"enable_any_statement"`
EnableAutoIncrementUnsigned bool `toml:"enable_autoincrement_unsigned" json:"enable_autoincrement_unsigned"`
// 允许blob,text,json列设置为NOT NULL
EnableBlobNotNull bool `toml:"enable_blob_not_null" json:"enable_blob_not_null"`
EnableBlobType bool `toml:"enable_blob_type" json:"enable_blob_type"`
Expand Down Expand Up @@ -740,6 +742,7 @@ var defaultConf = Config{
EnableSetEngine: true,
CheckTableComment: false,
CheckColumnComment: false,
EnableAnyStatement: false,
EnableChangeColumn: true,
CheckTimestampCount: true,
EnableTimeStampType: true,
Expand Down
12 changes: 12 additions & 0 deletions session/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ func (s *session) init() {
s.osc = config.GetGlobalConfig().Osc
s.ghost = config.GetGlobalConfig().Ghost

// 在开启goinception鉴权时.非root用户禁止启用EnableAnyStatement功能
// if !s.inc.SkipGrantTable {}
if s.inc.EnableAnyStatement {
if tmp := s.processInfo.Load(); tmp != nil {
if pi, ok := tmp.(util.ProcessInfo); ok {
if pi.User != "root" {
log.Warnf("Insufficient permissions to enable any statement! user: %s", pi.User)
s.inc.EnableAnyStatement = false
}
}
}
}
s.inc.Lang = strings.Replace(strings.ToLower(s.inc.Lang), "-", "_", 1)

s.sqlFingerprint = nil
Expand Down
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte) {
Info: sql,
OperState: "INIT",
}
log.Errorf("s.sessionVars.User: %#v", s.sessionVars.User)
if s.sessionVars.User != nil {
pi.User = s.sessionVars.User.Username
pi.Host = s.sessionVars.User.Hostname
Expand Down
32 changes: 21 additions & 11 deletions session/session_inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,10 @@ func (s *session) processCommand(ctx context.Context, stmtNode ast.StmtNode,
s.checkSetStmt(node)

default:
log.Infof("无匹配类型:%T\n", stmtNode)
s.appendErrorNo(ER_NOT_SUPPORTED_YET)
log.Warnf("无匹配类型:%T\n", stmtNode)
if !s.inc.EnableAnyStatement {
s.appendErrorNo(ER_NOT_SUPPORTED_YET)
}
}

s.mysqlComputeSqlSha1(s.myRecord)
Expand Down Expand Up @@ -1208,8 +1210,12 @@ func (s *session) executeRemoteCommand(record *Record, isTran bool) int {
s.executeRemoteStatement(record, isTran)

default:
log.Infof("无匹配类型: %T\n", node)
s.appendErrorNo(ER_NOT_SUPPORTED_YET)
log.Warnf("无匹配类型: %T\n", node)
if s.inc.EnableAnyStatement {
s.executeRemoteStatement(record, isTran)
} else {
s.appendErrorNo(ER_NOT_SUPPORTED_YET)
}
}

return int(record.ErrLevel)
Expand Down Expand Up @@ -5462,9 +5468,10 @@ func (s *session) executeInceptionSet(node *ast.InceptionSetStmt, sql string) ([

// t := reflect.TypeOf(cnf.Inc)
// values := reflect.ValueOf(&cnf.Inc).Elem()
prefix := strings.ToLower(v.Name)
if strings.Contains(prefix, "_") {
prefix = strings.Split(prefix, "_")[0]
variableName := strings.ToLower(v.Name)
var prefix string
if strings.Contains(variableName, "_") {
prefix = strings.Split(variableName, "_")[0]
}

var err error
Expand Down Expand Up @@ -5494,8 +5501,9 @@ func (s *session) executeInceptionSet(node *ast.InceptionSetStmt, sql string) ([
}

default:
if prefix == "version" {
return nil, errors.New("只读变量")
if prefix == "version" || variableName == "enable_any_statement" {
return nil, errors.New(
fmt.Sprintf("Variable '%s' is a read only variable", v.Name))
}
var object *config.Inc
if v.IsGlobal {
Expand Down Expand Up @@ -5759,7 +5767,7 @@ func (s *session) executeLocalShowProcesslist(node *ast.ShowStmt) ([]sqlexec.Rec
pi.DestUser,
pi.DestHost,
pi.DestPort,
pi.Host,
fmt.Sprintf("%s@%s", pi.User, pi.Host),
pi.Command,
pi.OperState,
int64(time.Since(pi.Time) / time.Second),
Expand Down Expand Up @@ -7861,7 +7869,9 @@ func (s *session) checkSetStmt(node *ast.SetStmt) {
s.appendErrorNo(ErrCharsetNotSupport, "utf8,utf8mb4")
}
} else {
s.appendErrorNo(ER_NOT_SUPPORTED_YET)
if !s.inc.EnableAnyStatement {
s.appendErrorNo(ER_NOT_SUPPORTED_YET)
}
continue
}
}
Expand Down
12 changes: 12 additions & 0 deletions session/session_inception_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ import (
"strconv"
"strings"
"testing"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/hanchuanchuan/goInception/ast"
"github.com/hanchuanchuan/goInception/config"
"github.com/hanchuanchuan/goInception/domain"
"github.com/hanchuanchuan/goInception/kv"
"github.com/hanchuanchuan/goInception/mysql"
"github.com/hanchuanchuan/goInception/parser"
"github.com/hanchuanchuan/goInception/server"
"github.com/hanchuanchuan/goInception/session"
"github.com/hanchuanchuan/goInception/store/mockstore"
"github.com/hanchuanchuan/goInception/store/mockstore/mocktikv"
"github.com/hanchuanchuan/goInception/util/auth"
"github.com/hanchuanchuan/goInception/util/logutil"
"github.com/hanchuanchuan/goInception/util/testkit"
"github.com/hanchuanchuan/goInception/util/testleak"
Expand Down Expand Up @@ -151,6 +154,15 @@ func (s *testCommon) initSetUp(c *C) {
s.tk.Se.SetSessionManager(server)

s.session = s.tk.Se
vars := s.session.GetSessionVars()
if vars.User == nil {
vars.User = &auth.UserIdentity{
Username: "root",
Hostname: "127.0.0.1",
}
}
// 重新设置进程信息,写入user@host
s.session.SetProcessInfo("", time.Now(), mysql.ComQuery)

cfg := config.GetGlobalConfig()
_, localFile, _, _ := runtime.Caller(0)
Expand Down
14 changes: 14 additions & 0 deletions session/session_inception_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,3 +1026,17 @@ func (s *testSessionIncExecSuite) TestWhereCondition(c *C) {
`
s.mustRunExec(c, sql)
}

func (s *testSessionIncExecSuite) TestExecAnyStatement(c *C) {
sql := ""
config.GetGlobalConfig().Inc.EnableAnyStatement = true
config.GetGlobalConfig().Inc.EnableDropTable = false

sql = "drop table if exists t1;create table t1(id int);drop table t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_DROP_TABLE, "t1"))

s.mustRunExec(c, "set global binlog_format = ROW;")
s.mustRunExec(c, "create user test1@'127.0.0.1' identified by '123';")
s.mustRunExec(c, "drop user test1@'127.0.0.1';")
}