Skip to content

Commit

Permalink
Redact bind var value only
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Feb 16, 2024
1 parent fe608ba commit 8f63ad5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
8 changes: 6 additions & 2 deletions go/streamlog/bind_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ func (bv *BindVariable) UnmarshalJSON(b []byte) error {
// {"Type":"VARBINARY","Value":"FmtAtEq6S9Y="}
func (bv BindVariable) MarshalJSON() ([]byte, error) {
// convert querypb.Type integer to string and pass along Value.
return json.Marshal(map[string]interface{}{
out := map[string]interface{}{
"Type": bv.Type.String(),
"Value": bv.Value,
})
}
if GetRedactDebugUIQueries() {
out["Value"] = nil
}
return json.Marshal(out)
}

// BindVariablesToProto converts a string-map of BindVariable to a string-map of *querypb.BindVariable.
Expand Down
4 changes: 1 addition & 3 deletions go/vt/vtgate/logstats/logstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,13 @@ func (stats *LogStats) Logf(w io.Writer, params url.Values) error {
}()

formattedBindVars := "\"[REDACTED]\""
if !streamlog.GetRedactDebugUIQueries() {
if !streamlog.GetRedactDebugUIQueries() && !streamlog.UseQueryLogJSONV2() {
_, fullBindParams := params["full"]
formattedBindVars = sqltypes.FormatBindVariables(
streamlog.BindVariablesToProto(stats.BindVariables),
fullBindParams,
streamlog.GetQueryLogFormat() == streamlog.QueryLogFormatJSON,
)
} else {
stats.BindVariables = nil
}

// TODO: remove username here we fully enforce immediate caller id
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/logstats/logstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestLogStatsFormatJSONV2(t *testing.T) {
streamlog.SetRedactDebugUIQueries(true)
var buf bytes.Buffer
assert.Nil(t, logStats.Logf(&buf, nil))
assert.Equal(t, `{"RemoteAddr":"","Username":"","ImmediateCaller":"","EffectiveCaller":"","Method":"test","TabletType":"PRIMARY","StmtType":"select","SQL":"select * from testtable where name = :strVal and message = :bytesVal","StartTime":"2017-01-01T01:02:03Z","EndTime":"2017-01-01T01:02:04.000001234Z","ShardQueries":0,"RowsAffected":0,"RowsReturned":0,"PlanTime":0,"ExecuteTime":0,"CommitTime":0,"TablesUsed":["ks1.tbl1","ks2.tbl2"],"SessionUUID":"suuid","CachedPlan":false,"ActiveKeyspace":"db"}`, strings.TrimSpace(buf.String()))
assert.Equal(t, `{"RemoteAddr":"","Username":"","ImmediateCaller":"","EffectiveCaller":"","Method":"test","TabletType":"PRIMARY","StmtType":"select","SQL":"select * from testtable where name = :strVal and message = :bytesVal","BindVariables":{"bytesVal":{"Type":"VARBINARY","Value":null},"strVal":{"Type":"VARCHAR","Value":null}},"StartTime":"2017-01-01T01:02:03Z","EndTime":"2017-01-01T01:02:04.000001234Z","ShardQueries":0,"RowsAffected":0,"RowsReturned":0,"PlanTime":0,"ExecuteTime":0,"CommitTime":0,"TablesUsed":["ks1.tbl1","ks2.tbl2"],"SessionUUID":"suuid","CachedPlan":false,"ActiveKeyspace":"db"}`, strings.TrimSpace(buf.String()))
assert.Nil(t, json.Unmarshal(buf.Bytes(), &cmpStats))
}
}
Expand Down
15 changes: 6 additions & 9 deletions go/vt/vttablet/tabletserver/tabletenv/logstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (stats *LogStats) TotalTime() time.Duration {
// RewrittenSQL returns a semicolon separated list of SQL statements
// that were executed.
func (stats *LogStats) RewrittenSQL() string {
if streamlog.GetRedactDebugUIQueries() {
return "[REDACTED]"
}
return strings.Join(stats.rewrittenSqls, "; ")
}

Expand Down Expand Up @@ -193,20 +196,14 @@ func (stats *LogStats) Logf(w io.Writer, params url.Values) error {
return nil
}

rewrittenSQL := "[REDACTED]"
formattedBindVars := "\"[REDACTED]\""

if !streamlog.GetRedactDebugUIQueries() {
rewrittenSQL = stats.RewrittenSQL()

if !streamlog.GetRedactDebugUIQueries() && !streamlog.UseQueryLogJSONV2() {
_, fullBindParams := params["full"]
formattedBindVars = sqltypes.FormatBindVariables(
streamlog.BindVariablesToProto(stats.BindVariables),
fullBindParams,
streamlog.GetQueryLogFormat() == streamlog.QueryLogFormatJSON,
)
} else {
stats.BindVariables = nil
}

// TODO: remove username here we fully enforce immediate caller id
Expand All @@ -226,7 +223,7 @@ func (stats *LogStats) Logf(w io.Writer, params url.Values) error {
LogStats: *stats,
Username: username,
CallInfo: callInfo,
RewrittenSQL: rewrittenSQL,
RewrittenSQL: stats.RewrittenSQL(),
ResponseSize: stats.SizeOfResponse(),
TotalTime: stats.TotalTime(),
})
Expand All @@ -249,7 +246,7 @@ func (stats *LogStats) Logf(w io.Writer, params url.Values) error {
stats.OriginalSQL,
formattedBindVars,
stats.NumberOfQueries,
rewrittenSQL,
stats.RewrittenSQL(),
stats.FmtQuerySources(),
stats.MysqlResponseTime.Seconds(),
stats.WaitingForConnection.Seconds(),
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vttablet/tabletserver/tabletenv/logstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestLogStatsFormatJSONV2(t *testing.T) {
streamlog.SetRedactDebugUIQueries(true)
var buf bytes.Buffer
assert.Nil(t, logStats.Logf(&buf, nil))
assert.Equal(t, `{"CallInfo":"","Username":"","ImmediateCaller":"","EffectiveCaller":"","RewrittenSQL":"[REDACTED]","TotalTime":1000001234,"ResponseSize":1,"Method":"test","PlanType":"","OriginalSQL":"sql","RowsAffected":0,"NumberOfQueries":1,"StartTime":"2017-01-01T01:02:03Z","EndTime":"2017-01-01T01:02:04.000001234Z","MysqlResponseTime":0,"WaitingForConnection":0,"QuerySources":2,"TransactionID":12345,"ReservedID":0,"CachedPlan":false}`, strings.TrimSpace(buf.String()))
assert.Equal(t, `{"CallInfo":"","Username":"","ImmediateCaller":"","EffectiveCaller":"","RewrittenSQL":"[REDACTED]","TotalTime":1000001234,"ResponseSize":1,"Method":"test","PlanType":"","OriginalSQL":"sql","BindVariables":{"bytesVal":{"Type":"VARBINARY","Value":null},"intVal":{"Type":"INT64","Value":null}},"RowsAffected":0,"NumberOfQueries":1,"StartTime":"2017-01-01T01:02:03Z","EndTime":"2017-01-01T01:02:04.000001234Z","MysqlResponseTime":0,"WaitingForConnection":0,"QuerySources":2,"TransactionID":12345,"ReservedID":0,"CachedPlan":false}`, strings.TrimSpace(buf.String()))
assert.Nil(t, json.Unmarshal(buf.Bytes(), &cmpStats))
}
}
Expand Down

0 comments on commit 8f63ad5

Please sign in to comment.