44package db // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/redactionprocessor/internal/db"
55
66import (
7+ "strings"
8+
79 "github.com/DataDog/datadog-agent/pkg/obfuscate"
10+ semconv128 "go.opentelemetry.io/otel/semconv/v1.28.0"
11+ "go.uber.org/zap"
812)
913
1014type Obfuscator struct {
11- obfuscators []databaseObfuscator
12- processAttributesEnabled bool
15+ obfuscators []databaseObfuscator
16+ processAttributesEnabled bool
17+ logger * zap.Logger
18+ allowFallbackWithoutSystem bool
19+ DBSystem string
1320}
1421
1522func createAttributes (attributes []string ) map [string ]bool {
@@ -20,7 +27,10 @@ func createAttributes(attributes []string) map[string]bool {
2027 return attributesMap
2128}
2229
23- func NewObfuscator (cfg DBSanitizerConfig ) * Obfuscator {
30+ func NewObfuscator (cfg DBSanitizerConfig , logger * zap.Logger ) * Obfuscator {
31+ if logger == nil {
32+ logger = zap .NewNop ()
33+ }
2434 o := obfuscate .NewObfuscator (obfuscate.Config {
2535 SQL : obfuscate.SQLConfig {
2636 ReplaceDigits : true ,
@@ -49,83 +59,92 @@ func NewObfuscator(cfg DBSanitizerConfig) *Obfuscator {
4959 processAttributesEnabled := false
5060
5161 if cfg .SQLConfig .Enabled {
52- attributes := createAttributes (cfg .SQLConfig .Attributes )
53- processAttributesEnabled = processAttributesEnabled || len (attributes ) > 0
62+ dbAttrs := newDBAttributes (cfg .SQLConfig .Attributes , []string {
63+ semconv128 .DBSystemOtherSQL .Value .AsString (),
64+ semconv128 .DBSystemMySQL .Value .AsString (),
65+ semconv128 .DBSystemPostgreSQL .Value .AsString (),
66+ semconv128 .DBSystemMariaDB .Value .AsString (),
67+ semconv128 .DBSystemSqlite .Value .AsString (),
68+ })
69+ processAttributesEnabled = processAttributesEnabled || len (dbAttrs .attributes ) > 0
5470 obfuscators = append (obfuscators , & sqlObfuscator {
55- dbAttributes : dbAttributes {
56- attributes : attributes ,
57- },
58- obfuscator : o ,
71+ dbAttributes : dbAttrs ,
72+ obfuscator : o ,
5973 })
6074 }
6175
6276 if cfg .RedisConfig .Enabled {
63- attributes := createAttributes (cfg .RedisConfig .Attributes )
64- processAttributesEnabled = processAttributesEnabled || len (attributes ) > 0
77+ dbAttrs := newDBAttributes (cfg .RedisConfig .Attributes , []string {
78+ semconv128 .DBSystemRedis .Value .AsString (),
79+ })
80+ processAttributesEnabled = processAttributesEnabled || len (dbAttrs .attributes ) > 0
6581 obfuscators = append (obfuscators , & redisObfuscator {
66- dbAttributes : dbAttributes {
67- attributes : attributes ,
68- },
69- obfuscator : o ,
82+ dbAttributes : dbAttrs ,
83+ obfuscator : o ,
7084 })
7185 }
7286
7387 if cfg .ValkeyConfig .Enabled {
74- attributes := createAttributes (cfg .ValkeyConfig .Attributes )
75- processAttributesEnabled = processAttributesEnabled || len ( attributes ) > 0
76- obfuscators = append ( obfuscators , & valkeyObfuscator {
77- dbAttributes : dbAttributes {
78- attributes : attributes ,
79- } ,
80- obfuscator : o ,
88+ dbAttrs := newDBAttributes (cfg .ValkeyConfig .Attributes , [] string {
89+ "valkey" , // Not part of semantic conventions
90+ })
91+ processAttributesEnabled = processAttributesEnabled || len ( dbAttrs . attributes ) > 0
92+ obfuscators = append ( obfuscators , & redisObfuscator {
93+ dbAttributes : dbAttrs ,
94+ obfuscator : o ,
8195 })
8296 }
8397
8498 if cfg .MemcachedConfig .Enabled {
85- attributes := createAttributes (cfg .MemcachedConfig .Attributes )
86- processAttributesEnabled = processAttributesEnabled || len (attributes ) > 0
99+ dbAttrs := newDBAttributes (cfg .MemcachedConfig .Attributes , []string {
100+ semconv128 .DBSystemMemcached .Value .AsString (),
101+ })
102+ processAttributesEnabled = processAttributesEnabled || len (dbAttrs .attributes ) > 0
87103 obfuscators = append (obfuscators , & memcachedObfuscator {
88- dbAttributes : dbAttributes {
89- attributes : attributes ,
90- },
91- obfuscator : o ,
104+ dbAttributes : dbAttrs ,
105+ obfuscator : o ,
92106 })
93107 }
94108
95109 if cfg .MongoConfig .Enabled {
96- attributes := createAttributes (cfg .MongoConfig .Attributes )
97- processAttributesEnabled = processAttributesEnabled || len (attributes ) > 0
110+ dbAttrs := newDBAttributes (cfg .MongoConfig .Attributes , []string {
111+ semconv128 .DBSystemMongoDB .Value .AsString (),
112+ })
113+ processAttributesEnabled = processAttributesEnabled || len (dbAttrs .attributes ) > 0
98114 obfuscators = append (obfuscators , & mongoObfuscator {
99- dbAttributes : dbAttributes {
100- attributes : attributes ,
101- },
102- obfuscator : o ,
115+ dbAttributes : dbAttrs ,
116+ obfuscator : o ,
117+ logger : logger ,
103118 })
104119 }
105120
106121 if cfg .OpenSearchConfig .Enabled {
107- attributes := createAttributes ([]string {})
122+ dbAttrs := newDBAttributes ([]string {}, []string {
123+ "opensearch" , // Not part of semantic conventions
124+ })
108125 obfuscators = append (obfuscators , & opensearchObfuscator {
109- dbAttributes : dbAttributes {
110- attributes : attributes ,
111- },
112- obfuscator : o ,
126+ dbAttributes : dbAttrs ,
127+ obfuscator : o ,
128+ logger : logger ,
113129 })
114130 }
115131
116132 if cfg .ESConfig .Enabled {
117- attributes := createAttributes ([]string {})
133+ dbAttrs := newDBAttributes ([]string {}, []string {
134+ semconv128 .DBSystemElasticsearch .Value .AsString (),
135+ })
118136 obfuscators = append (obfuscators , & esObfuscator {
119- dbAttributes : dbAttributes {
120- attributes : attributes ,
121- },
122- obfuscator : o ,
137+ dbAttributes : dbAttrs ,
138+ obfuscator : o ,
139+ logger : logger ,
123140 })
124141 }
125142
126143 return & Obfuscator {
127- obfuscators : obfuscators ,
128- processAttributesEnabled : processAttributesEnabled ,
144+ obfuscators : obfuscators ,
145+ processAttributesEnabled : processAttributesEnabled ,
146+ logger : logger ,
147+ allowFallbackWithoutSystem : cfg .AllowFallbackWithoutSystem ,
129148 }
130149}
131150
@@ -144,14 +163,40 @@ func (o *Obfuscator) ObfuscateAttribute(attributeValue, attributeKey string) (st
144163 if ! o .HasSpecificAttributes () {
145164 return attributeValue , nil
146165 }
166+
167+ if o .DBSystem == "" {
168+ if o .allowFallbackWithoutSystem {
169+ return o .obfuscateSequentially (attributeValue , attributeKey )
170+ }
171+ return attributeValue , nil
172+ }
173+
147174 for _ , obfuscator := range o .obfuscators {
148- obfuscatedValue , err := obfuscator .ObfuscateAttribute (attributeValue , attributeKey )
175+ if ! obfuscator .SupportsSystem (o .DBSystem ) {
176+ continue
177+ }
178+ if ! obfuscator .ShouldProcessAttribute (attributeKey ) {
179+ continue
180+ }
181+ return obfuscator .ObfuscateAttribute (attributeValue , attributeKey )
182+ }
183+
184+ return attributeValue , nil
185+ }
186+
187+ func (o * Obfuscator ) obfuscateSequentially (attributeValue , attributeKey string ) (string , error ) {
188+ result := attributeValue
189+ for _ , obfuscator := range o .obfuscators {
190+ if ! obfuscator .ShouldProcessAttribute (attributeKey ) {
191+ continue
192+ }
193+ obfuscatedValue , err := obfuscator .ObfuscateAttribute (result , attributeKey )
149194 if err != nil {
150195 return attributeValue , err
151196 }
152- attributeValue = obfuscatedValue
197+ result = obfuscatedValue
153198 }
154- return attributeValue , nil
199+ return result , nil
155200}
156201
157202func (o * Obfuscator ) HasSpecificAttributes () bool {
@@ -161,3 +206,38 @@ func (o *Obfuscator) HasSpecificAttributes() bool {
161206func (o * Obfuscator ) HasObfuscators () bool {
162207 return len (o .obfuscators ) > 0
163208}
209+
210+ func (o * Obfuscator ) ObfuscateWithSystem (val , dbSystem string ) (string , error ) {
211+ if ! o .HasObfuscators () {
212+ return val , nil
213+ }
214+ if dbSystem == "" {
215+ return val , nil
216+ }
217+ lower := strings .ToLower (dbSystem )
218+ for _ , obfuscator := range o .obfuscators {
219+ if ! obfuscator .SupportsSystem (lower ) {
220+ continue
221+ }
222+ return obfuscator .ObfuscateWithSystem (val , lower )
223+ }
224+ return val , nil
225+ }
226+
227+ func createSystems (systems []string ) map [string ]bool {
228+ if len (systems ) == 0 {
229+ return nil
230+ }
231+ systemsMap := make (map [string ]bool , len (systems ))
232+ for _ , system := range systems {
233+ systemsMap [strings .ToLower (system )] = true
234+ }
235+ return systemsMap
236+ }
237+
238+ func newDBAttributes (attributes , systems []string ) dbAttributes {
239+ return dbAttributes {
240+ attributes : createAttributes (attributes ),
241+ dbSystems : createSystems (systems ),
242+ }
243+ }
0 commit comments