Skip to content

Commit

Permalink
feat: No reason to send obfuscate as argument to sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
estolfo committed Mar 1, 2023
1 parent 83a16e1 commit cb98936
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def perform_request(method, path, *args, &block)
# TODO is this true for all elasticsearch client requests?
'net.transport' => IP_TCP,
'elasticsearch.method' => method
# TODO set other elasticsearch custom attributes, based on spec
}
attributes['peer.service'] = config[:peer_service] if config[:peer_service]
attributes['elasticsearch.params'] = args&.[](0).to_json if args&.[](0)
Expand All @@ -38,7 +39,7 @@ def perform_request(method, path, *args, &block)
omit = config[:db_statement] == :omit
obfuscate = config[:db_statement] == :obfuscate
unless omit
body = Sanitizer.sanitize(body, obfuscate, config[:sanitize_field_names])
body = Sanitizer.sanitize(body, config[:sanitize_field_names]) if obfuscate
if body && !body.is_a?(String)
body = body.to_json
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ class << self
Regexp.new(p.gsub('*', '.*'))
end

def sanitize(query, obfuscate, key_patterns = [])
def sanitize(query, key_patterns = [])
patterns = DEFAULT_KEY_PATTERNS
patterns += key_patterns if key_patterns
sanitize!(DeepDup.dup(query), patterns, obfuscate)
sanitize!(DeepDup.dup(query), patterns)
end

private

def sanitize!(obj, key_patterns, obfuscate)
def sanitize!(obj, key_patterns)
return obj unless obj.is_a?(Hash)

obj.each_pair do |k, v|
case v
when Hash
sanitize!(v, key_patterns, obfuscate)
sanitize!(v, key_patterns)
else
next unless obfuscate
next unless filter_key?(key_patterns, k)

obj[k] = FILTERED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
let(:sanitizer) { OpenTelemetry::Instrumentation::Elasticsearch::Patches::Sanitizer }

describe '#sanitize with default key patterns' do
let(:obfuscate) { true }
let(:obj) {
{
query: 'a query',
Expand All @@ -22,7 +21,7 @@
}

it 'sanitizes default key patterns' do
_(sanitizer.sanitize(obj, obfuscate)).must_equal(
_(sanitizer.sanitize(obj)).must_equal(
{
query: 'a query',
password: '?'
Expand All @@ -32,7 +31,6 @@
end

describe '#sanitize with custom key patterns' do
let(:obfuscate) { true }
let(:key_patterns) { [/.*sensitive.*/] }

let(:obj) {
Expand All @@ -43,7 +41,7 @@
}

it 'sanitizes custom key patterns' do
_(sanitizer.sanitize(obj, obfuscate, key_patterns)).must_equal(
_(sanitizer.sanitize(obj, key_patterns)).must_equal(
{
query: 'a query',
some_sensitive_field: '?'
Expand All @@ -53,7 +51,6 @@
end

describe '#sanitize with no matching key patterns' do
let(:obfuscate) { true }
let(:key_patterns) { [/.*sensitive.*/] }

let(:obj) {
Expand All @@ -64,31 +61,12 @@
}

it 'does not sanitize fields' do
_(sanitizer.sanitize(obj, obfuscate, key_patterns)).must_equal(
_(sanitizer.sanitize(obj, key_patterns)).must_equal(
{
query: 'a query',
a_normal_field: 'normal data'
}
)
end
end

describe '#sanitize with obfuscate set to false' do
let(:obfuscate) { false }
let(:obj) {
{
query: 'a query',
password: 'top secret'
}
}

it 'does not obfuscate values' do
_(sanitizer.sanitize(obj, obfuscate)).must_equal(
{
query: 'a query',
password: 'top secret'
}
)
end
end
end

0 comments on commit cb98936

Please sign in to comment.