Skip to content

Commit

Permalink
log: add tidb_log_desensitization global variable to control whethe…
Browse files Browse the repository at this point in the history
…r do desensitization when log query (#18578)

Signed-off-by: crazycs520 <[email protected]>
  • Loading branch information
crazycs520 authored Jul 15, 2020
1 parent 761a961 commit 297acf7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 20 deletions.
25 changes: 18 additions & 7 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (a *recordSet) Next(ctx context.Context, req *chunk.Chunk) (err error) {
return
}
err = errors.Errorf("%v", r)
logutil.Logger(ctx).Error("execute sql panic", zap.String("sql", a.stmt.Text), zap.Stack("stack"))
logutil.Logger(ctx).Error("execute sql panic", zap.String("sql", a.stmt.GetTextToLog()), zap.Stack("stack"))
}()

err = Next(ctx, a.executor, req)
Expand Down Expand Up @@ -286,7 +286,7 @@ func (a *ExecStmt) Exec(ctx context.Context) (_ sqlexec.RecordSet, err error) {
panic(r)
}
err = errors.Errorf("%v", r)
logutil.Logger(ctx).Error("execute sql panic", zap.String("sql", a.Text), zap.Stack("stack"))
logutil.Logger(ctx).Error("execute sql panic", zap.String("sql", a.GetTextToLog()), zap.Stack("stack"))
}()

sctx := a.Ctx
Expand Down Expand Up @@ -814,7 +814,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
}
var sql stringutil.StringerFunc
normalizedSQL, digest := sessVars.StmtCtx.SQLDigest()
if sessVars.EnableSlowLogMasking {
if sessVars.EnableLogDesensitization {
sql = FormatSQL(normalizedSQL, nil)
} else if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
sql = FormatSQL(sensitiveStmt.SecureText(), nil)
Expand Down Expand Up @@ -977,10 +977,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
copTaskInfo := stmtCtx.CopTasksDetails()
memMax := stmtCtx.MemTracker.MaxConsumed()
diskMax := stmtCtx.DiskTracker.MaxConsumed()
sql := a.Text
if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
sql = sensitiveStmt.SecureText()
}
sql := a.GetTextToLog()
stmtsummary.StmtSummaryByDigestMap.AddStatement(&stmtsummary.StmtExecInfo{
SchemaName: strings.ToLower(sessVars.CurrentDB),
OriginalSQL: sql,
Expand All @@ -1006,3 +1003,17 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
PlanInCache: sessVars.FoundInPlanCache,
})
}

// GetTextToLog return the query text to log.
func (a *ExecStmt) GetTextToLog() string {
var sql string
sessVars := a.Ctx.GetSessionVars()
if sessVars.EnableLogDesensitization {
sql, _ = sessVars.StmtCtx.SQLDigest()
} else if sensitiveStmt, ok := a.StmtNode.(ast.SensitiveStmtNode); ok {
sql = sensitiveStmt.SecureText()
} else {
sql = a.Text
}
return sql
}
6 changes: 5 additions & 1 deletion server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,11 @@ func (cc getLastStmtInConn) String() string {
case mysql.ComFieldList:
return "ListFields " + string(data)
case mysql.ComQuery, mysql.ComStmtPrepare:
return queryStrForLog(string(hack.String(data)))
sql := string(hack.String(data))
if cc.ctx.GetSessionVars().EnableLogDesensitization {
sql, _ = parser.NormalizeDigest(sql)
}
return queryStrForLog(sql)
case mysql.ComStmtExecute, mysql.ComStmtFetch:
stmtID := binary.LittleEndian.Uint32(data[0:4])
return queryStrForLog(cc.preparedStmt2String(stmtID))
Expand Down
3 changes: 3 additions & 0 deletions server/conn_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,9 @@ func (cc *clientConn) preparedStmt2String(stmtID uint32) string {
if sv == nil {
return ""
}
if sv.EnableLogDesensitization {
return cc.preparedStmt2StringNoArgs(stmtID)
}
return cc.preparedStmt2StringNoArgs(stmtID) + sv.PreparedParams.String()
}

Expand Down
18 changes: 11 additions & 7 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex
s.currentPlan = stmt.Plan

// Execute the physical plan.
logStmt(stmtNode, s.sessionVars)
logStmt(stmt, s.sessionVars)
recordSet, err := runStmt(ctx, s, stmt)
if err != nil {
if !kv.ErrKeyExists.Equal(err) {
Expand Down Expand Up @@ -1330,7 +1330,7 @@ func (s *session) cachedPlanExec(ctx context.Context,
stmtCtx.OriginalSQL = stmt.Text
stmtCtx.InitSQLDigest(prepareStmt.NormalizedSQL, prepareStmt.SQLDigest)
stmtCtx.SetPlanDigest(prepareStmt.NormalizedPlan, prepareStmt.PlanDigest)
logQuery(stmt.OriginText(), s.sessionVars)
logQuery(stmt.GetTextToLog(), s.sessionVars)

// run ExecStmt
var resultSet sqlexec.RecordSet
Expand Down Expand Up @@ -2061,6 +2061,7 @@ var builtinGlobalVariable = []string{
variable.TiDBAllowAutoRandExplicitInsert,
variable.TiDBEnableClusteredIndex,
variable.TiDBSlowLogMasking,
variable.TiDBLogDesensitization,
variable.TiDBEnableTelemetry,
variable.TiDBShardAllocateStep,
}
Expand Down Expand Up @@ -2211,14 +2212,14 @@ func (s *session) ShowProcess() *util.ProcessInfo {

// logStmt logs some crucial SQL including: CREATE USER/GRANT PRIVILEGE/CHANGE PASSWORD/DDL etc and normal SQL
// if variable.ProcessGeneralLog is set.
func logStmt(node ast.StmtNode, vars *variable.SessionVars) {
switch stmt := node.(type) {
func logStmt(execStmt *executor.ExecStmt, vars *variable.SessionVars) {
switch stmt := execStmt.StmtNode.(type) {
case *ast.CreateUserStmt, *ast.DropUserStmt, *ast.AlterUserStmt, *ast.SetPwdStmt, *ast.GrantStmt,
*ast.RevokeStmt, *ast.AlterTableStmt, *ast.CreateDatabaseStmt, *ast.CreateIndexStmt, *ast.CreateTableStmt,
*ast.DropDatabaseStmt, *ast.DropIndexStmt, *ast.DropTableStmt, *ast.RenameTableStmt, *ast.TruncateTableStmt:
user := vars.User
schemaVersion := vars.TxnCtx.SchemaVersion
if ss, ok := node.(ast.SensitiveStmtNode); ok {
if ss, ok := execStmt.StmtNode.(ast.SensitiveStmtNode); ok {
logutil.BgLogger().Info("CRUCIAL OPERATION",
zap.Uint64("conn", vars.ConnectionID),
zap.Int64("schemaVersion", schemaVersion),
Expand All @@ -2233,13 +2234,16 @@ func logStmt(node ast.StmtNode, vars *variable.SessionVars) {
zap.Stringer("user", user))
}
default:
logQuery(node.Text(), vars)
logQuery(execStmt.GetTextToLog(), vars)
}
}

func logQuery(query string, vars *variable.SessionVars) {
if atomic.LoadUint32(&variable.ProcessGeneralLog) != 0 && !vars.InRestrictedSQL {
query = executor.QueryReplacer.Replace(query)
if !vars.EnableLogDesensitization {
query = query + vars.PreparedParams.String()
}
logutil.BgLogger().Info("GENERAL_LOG",
zap.Uint64("conn", vars.ConnectionID),
zap.Stringer("user", vars.User),
Expand All @@ -2249,7 +2253,7 @@ func logQuery(query string, vars *variable.SessionVars) {
zap.Bool("isReadConsistency", vars.IsReadConsistencyTxn()),
zap.String("current_db", vars.CurrentDB),
zap.String("txn_mode", vars.GetReadableTxnMode()),
zap.String("sql", query+vars.PreparedParams.String()))
zap.String("sql", query))
}
}

Expand Down
10 changes: 5 additions & 5 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,8 @@ type SessionVars struct {
// EnableClusteredIndex indicates whether to enable clustered index when creating a new table.
EnableClusteredIndex bool

// EnableSlowLogMasking indicates that whether masking the query data when log slow query.
EnableSlowLogMasking bool
// EnableLogDesensitization indicates that whether desensitization when log query.
EnableLogDesensitization bool

// PresumeKeyNotExists indicates lazy existence checking is enabled.
PresumeKeyNotExists bool
Expand Down Expand Up @@ -786,7 +786,7 @@ func NewSessionVars() *SessionVars {
SelectLimit: math.MaxUint64,
AllowAutoRandExplicitInsert: DefTiDBAllowAutoRandExplicitInsert,
EnableClusteredIndex: DefTiDBEnableClusteredIndex,
EnableSlowLogMasking: DefTiDBSlowLogMasking,
EnableLogDesensitization: DefTiDBLogDesensitization,
ShardAllocateStep: DefTiDBShardAllocateStep,
}
vars.KVVars = kv.NewVariables(&vars.Killed)
Expand Down Expand Up @@ -1397,8 +1397,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.AllowAutoRandExplicitInsert = TiDBOptOn(val)
case TiDBEnableClusteredIndex:
s.EnableClusteredIndex = TiDBOptOn(val)
case TiDBSlowLogMasking:
s.EnableSlowLogMasking = TiDBOptOn(val)
case TiDBSlowLogMasking, TiDBLogDesensitization:
s.EnableLogDesensitization = TiDBOptOn(val)
case TiDBShardAllocateStep:
s.ShardAllocateStep = tidbOptInt64(val, DefTiDBShardAllocateStep)
}
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBAllowAutoRandExplicitInsert, boolToOnOff(DefTiDBAllowAutoRandExplicitInsert)},
{ScopeGlobal | ScopeSession, TiDBEnableClusteredIndex, BoolToIntStr(DefTiDBEnableClusteredIndex)},
{ScopeGlobal, TiDBSlowLogMasking, BoolToIntStr(DefTiDBSlowLogMasking)},
{ScopeGlobal, TiDBLogDesensitization, BoolToIntStr(DefTiDBLogDesensitization)},
{ScopeGlobal | ScopeSession, TiDBShardAllocateStep, strconv.Itoa(DefTiDBShardAllocateStep)},
{ScopeGlobal, TiDBEnableTelemetry, BoolToIntStr(DefTiDBEnableTelemetry)},
}
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,12 @@ const (
TiDBEnableClusteredIndex = "tidb_enable_clustered_index"

// TiDBSlowLogMasking indicates that whether masking the query data when log slow query.
// Deprecated: use TiDBLogDesensitization instead.
TiDBSlowLogMasking = "tidb_slow_log_masking"

// TiDBLogDesensitization indicates that whether desensitization when log query.
TiDBLogDesensitization = "tidb_log_desensitization"

// TiDBShardAllocateStep indicates the max size of continuous rowid shard in one transaction.
TiDBShardAllocateStep = "tidb_shard_allocate_step"
// TiDBEnableTelemetry indicates that whether usage data report to PingCAP is enabled.
Expand Down Expand Up @@ -523,6 +527,7 @@ const (
DefTiDBAllowAutoRandExplicitInsert = false
DefTiDBEnableClusteredIndex = false
DefTiDBSlowLogMasking = false
DefTiDBLogDesensitization = false
DefTiDBShardAllocateStep = math.MaxInt64
DefTiDBEnableTelemetry = true
)
Expand Down

0 comments on commit 297acf7

Please sign in to comment.