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

executor: remove sensitive information in slow-log and statement (#18107) #18129

Merged
merged 5 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 26 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,27 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
cfg := config.GetGlobalConfig()
costTime := time.Since(sessVars.StartTime) + sessVars.DurationParse
threshold := time.Duration(atomic.LoadUint64(&cfg.Log.SlowThreshold)) * time.Millisecond
<<<<<<< HEAD
if costTime < threshold && level > zapcore.DebugLevel {
return
}
sql := FormatSQL(a.Text, sessVars.PreparedParams)
=======
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve the conflict @crazycs520

enable := cfg.Log.EnableSlowLog
// if the level is Debug, print slow logs anyway
if (!enable || costTime < threshold) && level > zapcore.DebugLevel {
return
}
var sql stringutil.StringerFunc
normalizedSQL, digest := sessVars.StmtCtx.SQLDigest()
if sessVars.EnableSlowLogMasking {
sql = FormatSQL(normalizedSQL, nil)
} else if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
sql = FormatSQL(sensitiveStmt.SecureText(), nil)
} else {
sql = FormatSQL(a.Text, sessVars.PreparedParams)
}
>>>>>>> dfca52c... executor: remove sensitive information in slow-log and statement (#18107)

var tableIDs, indexNames string
if len(sessVars.StmtCtx.TableIDs) > 0 {
Expand Down Expand Up @@ -833,14 +850,22 @@ func (a *ExecStmt) SummaryStmt() {
execDetail := stmtCtx.GetExecDetails()
copTaskInfo := stmtCtx.CopTasksDetails()
memMax := stmtCtx.MemTracker.MaxConsumed()
<<<<<<< HEAD
var userString string
if sessVars.User != nil {
userString = sessVars.User.Username
}

=======
diskMax := stmtCtx.DiskTracker.MaxConsumed()
sql := a.Text
if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
sql = sensitiveStmt.SecureText()
}
>>>>>>> dfca52c... executor: remove sensitive information in slow-log and statement (#18107)
stmtsummary.StmtSummaryByDigestMap.AddStatement(&stmtsummary.StmtExecInfo{
SchemaName: strings.ToLower(sessVars.CurrentDB),
OriginalSQL: a.Text,
OriginalSQL: sql,
NormalizedSQL: normalizedSQL,
Digest: digest,
PrevSQL: prevSQL,
Expand Down
30 changes: 30 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"math"
"os"
"path"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -4521,3 +4522,32 @@ func (s *testSuite1) TestIssue15718(c *C) {
tk.MustExec("INSERT INTO t0(c0) VALUES (NULL);")
tk.MustQuery("SELECT * FROM t0 WHERE NOT(0 OR t0.c0);").Check(testkit.Rows())
}

func (s *testSuite) TestSlowQuerySensitiveQuery(c *C) {
tk := testkit.NewTestKit(c, s.store)
originCfg := config.GetGlobalConfig()
newCfg := *originCfg
newCfg.Log.SlowQueryFile = path.Join(os.TempDir(), "tidb-slow.log")
config.StoreGlobalConfig(&newCfg)
defer func() {
tk.MustExec("set tidb_slow_log_threshold=300;")
config.StoreGlobalConfig(originCfg)
os.Remove(newCfg.Log.SlowQueryFile)
}()
err := logutil.InitLogger(newCfg.Log.ToLogConfig())
c.Assert(err, IsNil)

tk.MustExec("set tidb_slow_log_threshold=0;")
tk.MustExec("drop user if exists user_sensitive;")
tk.MustExec("create user user_sensitive identified by '123456789';")
tk.MustExec("alter user 'user_sensitive'@'%' identified by 'abcdefg';")
tk.MustExec("set password for 'user_sensitive'@'%' = 'xyzuvw';")
tk.MustQuery("select query from `information_schema`.`slow_query` " +
"where (query like 'set password%' or query like 'create user%' or query like 'alter user%') " +
"and query like '%user_sensitive%' order by query;").
Check(testkit.Rows(
"alter user {user_sensitive@% password = ***};",
"create user {user_sensitive@% password = ***};",
"set password for user user_sensitive@%;",
))
}
28 changes: 28 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,39 @@ func (s *testTableSuite) TestPartitionsTable(c *C) {
tk.MustExec(`CREATE TABLE test_partitions (a int, b int, c varchar(5), primary key(a), index idx(c)) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6), PARTITION p1 VALUES LESS THAN (11), PARTITION p2 VALUES LESS THAN (16));`)
tk.MustExec(`insert into test_partitions(a, b, c) values(1, 2, "c"), (7, 3, "d"), (12, 4, "e");`)

<<<<<<< HEAD
tk.MustQuery("select PARTITION_NAME, PARTITION_DESCRIPTION from information_schema.PARTITIONS where table_name='test_partitions';").Check(
testkit.Rows("" +
"p0 6]\n" +
"[p1 11]\n" +
"[p2 16"))
=======
func (s *testTableSuite) TestStmtSummarySensitiveQuery(c *C) {
tk := s.newTestKitWithRoot(c)
tk.MustExec("set global tidb_enable_stmt_summary = 0")
tk.MustExec("set global tidb_enable_stmt_summary = 1")
tk.MustExec("drop user if exists user_sensitive;")
tk.MustExec("create user user_sensitive identified by '123456789';")
tk.MustExec("alter user 'user_sensitive'@'%' identified by 'abcdefg';")
tk.MustExec("set password for 'user_sensitive'@'%' = 'xyzuvw';")
tk.MustQuery("select query_sample_text from `information_schema`.`STATEMENTS_SUMMARY` " +
"where query_sample_text like '%user_sensitive%' and " +
"(query_sample_text like 'set password%' or query_sample_text like 'create user%' or query_sample_text like 'alter user%') " +
"order by query_sample_text;").
Check(testkit.Rows(
"alter user {user_sensitive@% password = ***}",
"create user {user_sensitive@% password = ***}",
"set password for user user_sensitive@%",
))
}

func (s *testTableSuite) TestPerformanceSchemaforPlanCache(c *C) {
orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
}()
plannercore.SetPreparedPlanCache(true)
>>>>>>> dfca52c... executor: remove sensitive information in slow-log and statement (#18107)

tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.PARTITIONS where table_name='test_partitions';").Check(
testkit.Rows("" +
Expand Down