Skip to content

Commit

Permalink
feat: Make Sanitizer.sanitize a class method to avoid creating new ob…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
estolfo committed Mar 1, 2023
1 parent 8651580 commit 9855ca7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def perform_request(method, path, *args, &block)
omit = config[:db_statement] == :omit
obfuscate = config[:db_statement] == :obfuscate
unless omit
# TODO cache Sanitizer instead of creating a new one each time
body = Sanitizer.new(config[:sanitize_field_names]).sanitize(body, obfuscate)
body = Sanitizer.sanitize(body, obfuscate, config[:sanitize_field_names])
attributes['db.statement'] = body.to_json if body
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,42 @@ module Instrumentation
module Elasticsearch
module Patches
class Sanitizer
FILTERED = '?'
DEFAULT_KEY_PATTERNS =
%w[password passwd pwd secret *key *token* *session* *credit* *card* *auth* set-cookie].map! do |p|
Regexp.new(p.gsub('*', '.*'))
end

def initialize(key_patterns = [])
@key_patterns = DEFAULT_KEY_PATTERNS
@key_patterns += key_patterns if key_patterns
end
class << self

def sanitize(query, obfuscate)
sanitize!(DeepDup.dup(query), obfuscate)
end
FILTERED = '?'
DEFAULT_KEY_PATTERNS =
%w[password passwd pwd secret *key *token* *session* *credit* *card* *auth* set-cookie].map! do |p|
Regexp.new(p.gsub('*', '.*'))
end

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

private

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

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

obj[k] = FILTERED
obj[k] = FILTERED
end
end
end
end

def filter_key?(key)
@key_patterns.any? { |regex| regex.match(key) }
def filter_key?(key_patterns, key)
key_patterns.any? { |regex| regex.match(key) }
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
require_relative '../../../../lib/opentelemetry/instrumentation/elasticsearch/patches/sanitizer'

describe OpenTelemetry::Instrumentation::Elasticsearch::Patches::Sanitizer do
let(:sanitizer) { OpenTelemetry::Instrumentation::Elasticsearch::Patches::Sanitizer }

describe '#sanitize with default key patterns' do
let(:sanitizer) { OpenTelemetry::Instrumentation::Elasticsearch::Patches::Sanitizer.new }
let(:obfuscate) { true }
let(:obj) {
{
Expand All @@ -32,9 +33,7 @@

describe '#sanitize with custom key patterns' do
let(:obfuscate) { true }
let(:sanitizer) do
OpenTelemetry::Instrumentation::Elasticsearch::Patches::Sanitizer.new([/.*sensitive.*/])
end
let(:key_patterns) { [/.*sensitive.*/] }

let(:obj) {
{
Expand All @@ -43,8 +42,8 @@
}
}

it 'sanitizes default key patterns' do
_(sanitizer.sanitize(obj, obfuscate)).must_equal(
it 'sanitizes custom key patterns' do
_(sanitizer.sanitize(obj, obfuscate, key_patterns)).must_equal(
{
query: 'a query',
some_sensitive_field: '?'
Expand All @@ -55,9 +54,7 @@

describe '#sanitize with no matching key patterns' do
let(:obfuscate) { true }
let(:sanitizer) do
OpenTelemetry::Instrumentation::Elasticsearch::Patches::Sanitizer.new([/.*sensitive.*/])
end
let(:key_patterns) { [/.*sensitive.*/] }

let(:obj) {
{
Expand All @@ -67,7 +64,7 @@
}

it 'does not sanitize fields' do
_(sanitizer.sanitize(obj, obfuscate)).must_equal(
_(sanitizer.sanitize(obj, obfuscate, key_patterns)).must_equal(
{
query: 'a query',
a_normal_field: 'normal data'
Expand All @@ -77,7 +74,6 @@
end

describe '#sanitize with obfuscate set to false' do
let(:sanitizer) { OpenTelemetry::Instrumentation::Elasticsearch::Patches::Sanitizer.new }
let(:obfuscate) { false }
let(:obj) {
{
Expand All @@ -86,7 +82,7 @@
}
}

it 'sanitizes default key patterns' do
it 'does not obfuscate values' do
_(sanitizer.sanitize(obj, obfuscate)).must_equal(
{
query: 'a query',
Expand Down

0 comments on commit 9855ca7

Please sign in to comment.