-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
sql.go
344 lines (313 loc) · 10.6 KB
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
package obfuscate
import (
"bytes"
"errors"
"fmt"
"strings"
"unicode"
"unicode/utf8"
"github.com/DataDog/datadog-agent/pkg/trace/config"
"github.com/DataDog/datadog-agent/pkg/trace/metrics"
"github.com/DataDog/datadog-agent/pkg/trace/pb"
"github.com/DataDog/datadog-agent/pkg/trace/traceutil"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const sqlQueryTag = "sql.query"
const nonParsableResource = "Non-parsable SQL query"
// tokenFilter is a generic interface that a sqlObfuscator expects. It defines
// the Filter() function used to filter or replace given tokens.
// A filter can be stateful and keep an internal state to apply the filter later;
// this can be useful to prevent backtracking in some cases.
// tokenFilter implementations process tokens in the order that they are found by the tokenizer
// and respond as to how they should be interpreted in the result. For example: one token filter
// may decide that a token should be hidden in the result, or that it should be transformed in
// some way.
type tokenFilter interface {
// Filter takes the current token kind, the last token kind and the token itself,
// returning the new token kind and the value that should be stored in the final query,
// along with an error.
Filter(token, lastToken TokenKind, buffer []byte) (TokenKind, []byte, error)
// Reset resets the filter.
Reset()
}
// discardFilter is a token filter which discards certain elements from a query, such as
// comments and AS aliases by returning a nil buffer.
type discardFilter struct{}
// Filter the given token so that a `nil` slice is returned if the token is in the token filtered list.
func (f *discardFilter) Filter(token, lastToken TokenKind, buffer []byte) (TokenKind, []byte, error) {
// filters based on previous token
switch lastToken {
case FilteredBracketedIdentifier:
if token != ']' {
// we haven't found the closing bracket yet, keep going
if token != ID {
// the token between the brackets *must* be an identifier,
// otherwise the query is invalid.
return LexError, nil, fmt.Errorf("expected identifier in bracketed filter, got %d", token)
}
return FilteredBracketedIdentifier, nil, nil
}
fallthrough
case As:
if token == '[' {
// the identifier followed by AS is an MSSQL bracketed identifier
// and will continue to be discarded until we find the corresponding
// closing bracket counter-part. See GitHub issue DataDog/datadog-trace-agent#475.
return FilteredBracketedIdentifier, nil, nil
}
return Filtered, nil, nil
}
// filters based on the current token; if the next token should be ignored,
// return the same token value (not FilteredGroupable) and nil
switch token {
case As:
return As, nil, nil
case Comment, ';':
return FilteredGroupable, nil, nil
default:
return token, buffer, nil
}
}
// Reset implements tokenFilter.
func (f *discardFilter) Reset() {}
// replaceFilter is a token filter which obfuscates strings and numbers in queries by replacing them
// with the "?" character.
type replaceFilter struct{}
// Filter the given token so that it will be replaced if in the token replacement list
func (f *replaceFilter) Filter(token, lastToken TokenKind, buffer []byte) (tokenType TokenKind, tokenBytes []byte, err error) {
switch lastToken {
case Savepoint:
return FilteredGroupable, []byte("?"), nil
case '=':
switch token {
case DoubleQuotedString:
// double-quoted strings after assignments are eligible for obfuscation
return FilteredGroupable, []byte("?"), nil
}
}
switch token {
case String, Number, Null, Variable, PreparedStatement, BooleanLiteral, EscapeSequence:
return FilteredGroupable, []byte("?"), nil
default:
return token, buffer, nil
}
}
// Reset implements tokenFilter.
func (f *replaceFilter) Reset() {}
// groupingFilter is a token filter which groups together items replaced by the replaceFilter. It is meant
// to run immediately after it.
type groupingFilter struct {
groupFilter int
groupMulti int
}
// Filter the given token so that it will be discarded if a grouping pattern
// has been recognized. A grouping is composed by items like:
// * '( ?, ?, ? )'
// * '( ?, ? ), ( ?, ? )'
func (f *groupingFilter) Filter(token, lastToken TokenKind, buffer []byte) (tokenType TokenKind, tokenBytes []byte, err error) {
// increasing the number of groups means that we're filtering an entire group
// because it can be represented with a single '( ? )'
if (lastToken == '(' && token == FilteredGroupable) || (token == '(' && f.groupMulti > 0) {
f.groupMulti++
}
switch {
case token == FilteredGroupable:
// the previous filter has dropped this token so we should start
// counting the group filter so that we accept only one '?' for
// the same group
f.groupFilter++
if f.groupFilter > 1 {
return FilteredGroupable, nil, nil
}
case f.groupFilter > 0 && (token == ',' || token == '?'):
// if we are in a group drop all commas
return FilteredGroupable, nil, nil
case f.groupMulti > 1:
// drop all tokens since we're in a counting group
// and they're duplicated
return FilteredGroupable, nil, nil
case token != ',' && token != '(' && token != ')' && token != FilteredGroupable:
// when we're out of a group reset the filter state
f.Reset()
}
return token, buffer, nil
}
// Reset resets the groupingFilter so that it may be used again.
func (f *groupingFilter) Reset() {
f.groupFilter = 0
f.groupMulti = 0
}
// obfuscateSQLString quantizes and obfuscates the given input SQL query string. Quantization removes
// some elements such as comments and aliases and obfuscation attempts to hide sensitive information
// in strings and numbers by redacting them.
func (o *Obfuscator) obfuscateSQLString(in string) (*obfuscatedQuery, error) {
lesc := o.SQLLiteralEscapes()
tok := NewSQLTokenizer(in, lesc)
out, err := attemptObfuscation(tok)
if err != nil && tok.SeenEscape() {
// If the tokenizer failed, but saw an escape character in the process,
// try again treating escapes differently
tok = NewSQLTokenizer(in, !lesc)
if out, err2 := attemptObfuscation(tok); err2 == nil {
// If the second attempt succeeded, change the default behavior so that
// on the next run we get it right in the first run.
o.SetSQLLiteralEscapes(!lesc)
return out, nil
}
}
return out, err
}
// tableFinderFilter is a filter which attempts to identify the table name as it goes through each
// token in a query.
type tableFinderFilter struct {
// seen keeps track of unique table names encountered by the filter.
seen map[string]struct{}
// csv specifies a comma-separated list of tables
csv strings.Builder
}
// Filter implements tokenFilter.
func (f *tableFinderFilter) Filter(token, lastToken TokenKind, buffer []byte) (TokenKind, []byte, error) {
switch lastToken {
case From:
// SELECT ... FROM [tableName]
// DELETE FROM [tableName]
if r, _ := utf8.DecodeRune(buffer); !unicode.IsLetter(r) {
// first character in buffer is not a letter; we might have a nested
// query like SELECT * FROM (SELECT ...)
break
}
fallthrough
case Update, Into, Join:
// UPDATE [tableName]
// INSERT INTO [tableName]
// ... JOIN [tableName]
f.storeName(string(buffer))
}
return token, buffer, nil
}
// storeName marks the given table name as seen in the internal storage.
func (f *tableFinderFilter) storeName(name string) {
if _, ok := f.seen[name]; ok {
return
}
if f.seen == nil {
f.seen = make(map[string]struct{}, 1)
}
f.seen[name] = struct{}{}
if f.csv.Len() > 0 {
f.csv.WriteByte(',')
}
f.csv.WriteString(name)
}
// CSV returns a comma-separated list of the tables seen by the filter.
func (f *tableFinderFilter) CSV() string { return f.csv.String() }
// Reset implements tokenFilter.
func (f *tableFinderFilter) Reset() {
for k := range f.seen {
delete(f.seen, k)
}
f.csv.Reset()
}
// obfuscatedQuery specifies information about an obfuscated SQL query.
type obfuscatedQuery struct {
query string // the obfuscated SQL query
tablesCSV string // comma-separated list of tables that the query addresses
}
// attemptObfuscation attempts to obfuscate the SQL query loaded into the tokenizer, using the
// given set of filters.
func attemptObfuscation(tokenizer *SQLTokenizer) (*obfuscatedQuery, error) {
filters := []tokenFilter{
&discardFilter{},
&replaceFilter{},
&groupingFilter{},
}
tableFinder := &tableFinderFilter{}
if config.HasFeature("table_names") {
filters = append(filters, tableFinder)
}
var (
out bytes.Buffer
err error
lastToken TokenKind
)
// call Scan() function until tokens are available or if a LEX_ERROR is raised. After
// retrieving a token, send it to the tokenFilter chains so that the token is discarded
// or replaced.
for {
token, buff := tokenizer.Scan()
if token == EOFChar {
break
}
if token == LexError {
return nil, fmt.Errorf("%v", tokenizer.Err())
}
for _, f := range filters {
if token, buff, err = f.Filter(token, lastToken, buff); err != nil {
return nil, err
}
}
if buff != nil {
if out.Len() != 0 {
switch token {
case ',':
case '=':
if lastToken == ':' {
// do not add a space before an equals if a colon was
// present before it.
break
}
fallthrough
default:
out.WriteRune(' ')
}
}
out.Write(buff)
}
lastToken = token
}
if out.Len() == 0 {
return nil, errors.New("result is empty")
}
return &obfuscatedQuery{
query: out.String(),
tablesCSV: tableFinder.CSV(),
}, nil
}
func (o *Obfuscator) obfuscateSQL(span *pb.Span) {
tags := []string{"type:sql"}
defer func() {
metrics.Count("datadog.trace_agent.obfuscations", 1, tags, 1)
}()
if span.Resource == "" {
tags = append(tags, "outcome:empty-resource")
return
}
oq, err := o.obfuscateSQLString(span.Resource)
if err != nil {
// we have an error, discard the SQL to avoid polluting user resources.
log.Debugf("Error parsing SQL query: %v. Resource: %q", err, span.Resource)
if span.Meta == nil {
span.Meta = make(map[string]string, 1)
}
if _, ok := span.Meta[sqlQueryTag]; !ok {
span.Meta[sqlQueryTag] = span.Resource
}
span.Resource = nonParsableResource
tags = append(tags, "outcome:error")
return
}
tags = append(tags, "outcome:success")
span.Resource = oq.query
if len(oq.tablesCSV) > 0 {
traceutil.SetMeta(span, "sql.tables", oq.tablesCSV)
}
if span.Meta != nil && span.Meta[sqlQueryTag] != "" {
// "sql.query" tag already set by user, do not change it.
return
}
traceutil.SetMeta(span, sqlQueryTag, oq.query)
}