-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathobfuscate.go
114 lines (98 loc) · 2.9 KB
/
obfuscate.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// source(Apache 2.0): https://github.com/DataDog/datadog-agent/blob/main/pkg/collector/python/datadog_agent.go
// 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-present Datadog, Inc.
package sqlserverreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver"
import (
"bytes"
"encoding/xml"
"fmt"
"strings"
"sync"
"github.com/DataDog/datadog-agent/pkg/obfuscate"
)
var (
obfuscator *obfuscate.Obfuscator
obfuscatorLoader sync.Once
)
// lazyInitObfuscator initializes the obfuscator the first time it is used.
func lazyInitObfuscator() *obfuscate.Obfuscator {
obfuscatorLoader.Do(func() { obfuscator = obfuscate.NewObfuscator(obfuscate.Config{}) })
return obfuscator
}
// ObfuscateSQL obfuscates & normalizes the provided SQL query, writing the error into errResult if the operation fails.
func obfuscateSQL(rawQuery string) (string, error) {
obfuscatedQuery, err := lazyInitObfuscator().ObfuscateSQLStringWithOptions(rawQuery, &obfuscate.SQLConfig{})
if err != nil {
return "", err
}
return obfuscatedQuery.Query, nil
}
// Ending source(Apache 2.0): https://github.com/DataDog/datadog-agent/blob/main/pkg/collector/python/datadog_agent.go
var xmlPlanObfuscationAttrs = []string{
"StatementText",
"ConstValue",
"ScalarString",
"ParameterCompiledValue",
}
// obfuscateXMLPlan obfuscates SQL text & parameters from the provided SQL Server XML Plan
func obfuscateXMLPlan(rawPlan string) (string, error) {
decoder := xml.NewDecoder(strings.NewReader(rawPlan))
var buffer bytes.Buffer
encoder := xml.NewEncoder(&buffer)
for {
token, err := decoder.Token()
if err != nil {
if err.Error() == "EOF" {
break
}
return "", err
}
switch elem := token.(type) {
case xml.StartElement:
for i := range elem.Attr {
for _, attrName := range xmlPlanObfuscationAttrs {
if elem.Attr[i].Name.Local == attrName {
if elem.Attr[i].Value == "" {
continue
}
val, err := obfuscateSQL(elem.Attr[i].Value)
if err != nil {
fmt.Println("Unable to obfuscated sql statement, skipping: " + elem.Attr[i].Value)
continue
}
elem.Attr[i].Value = val
}
}
}
err := encoder.EncodeToken(elem)
if err != nil {
return "", err
}
case xml.CharData:
elem = bytes.TrimSpace(elem)
err := encoder.EncodeToken(elem)
if err != nil {
return "", err
}
case xml.EndElement:
err := encoder.EncodeToken(elem)
if err != nil {
return "", err
}
default:
err := encoder.EncodeToken(token)
if err != nil {
return "", err
}
}
}
err := encoder.Flush()
if err != nil {
return "", err
}
return buffer.String(), nil
}