Class: OneApm::Agent::Database::Obfuscator

Inherits:
Object
  • Object
show all
Includes:
ObfuscationHelpers, Singleton
Defined in:
lib/one_apm/agent/database/obfuscator.rb

Constant Summary collapse

OA_QUERY_TOO_LARGE_MESSAGE =
"Query too large (over 16k characters) to safely obfuscate"
OA_FAILED_TO_OBFUSCATE_MESSAGE =
"Failed to obfuscate SQL query - quote characters remained after obfuscation"

Constants included from ObfuscationHelpers

OneApm::Agent::Database::ObfuscationHelpers::OA_LITERAL_DOUBLE_QUOTE, OneApm::Agent::Database::ObfuscationHelpers::OA_LITERAL_SINGLE_QUOTE, OneApm::Agent::Database::ObfuscationHelpers::OA_NUMERICS_REGEX, OneApm::Agent::Database::ObfuscationHelpers::OA_PLACEHOLDER, OneApm::Agent::Database::ObfuscationHelpers::OA_REVERSE_ANY_QUOTES_REGEX, OneApm::Agent::Database::ObfuscationHelpers::OA_REVERSE_SINGLE_QUOTES_REGEX, OneApm::Agent::Database::ObfuscationHelpers::OA_SQL_COMMENT_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ObfuscationHelpers

#contains_quotes?, #contains_single_quotes?, #obfuscate_numeric_literals, #obfuscate_quoted_literals, #obfuscate_single_quote_literals, #remove_comments

Constructor Details

#initializeObfuscator

Returns a new instance of Obfuscator.



17
18
19
# File 'lib/one_apm/agent/database/obfuscator.rb', line 17

def initialize
  reset
end

Instance Attribute Details

#obfuscatorObject (readonly)

Returns the value of attribute obfuscator.



12
13
14
# File 'lib/one_apm/agent/database/obfuscator.rb', line 12

def obfuscator
  @obfuscator
end

Instance Method Details

#default_sql_obfuscator(sql) ⇒ Object



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
# File 'lib/one_apm/agent/database/obfuscator.rb', line 48

def default_sql_obfuscator(sql)
  if sql[-3,3] == '...'
    return OA_QUERY_TOO_LARGE_MESSAGE
  end

  stmt = sql.kind_of?(Statement) ? sql : Statement.new(sql)
  obfuscate_double_quotes = stmt.adapter.to_s !~ /postgres|sqlite/

  obfuscated = obfuscate_numeric_literals(stmt)

  if obfuscate_double_quotes
    obfuscated = obfuscate_quoted_literals(obfuscated)
    obfuscated = remove_comments(obfuscated)
    if contains_quotes?(obfuscated)
      obfuscated = OA_FAILED_TO_OBFUSCATE_MESSAGE
    end
  else
    obfuscated = obfuscate_single_quote_literals(obfuscated)
    obfuscated = remove_comments(obfuscated)
    if contains_single_quotes?(obfuscated)
      obfuscated = OA_FAILED_TO_OBFUSCATE_MESSAGE
    end
  end


  obfuscated.to_s # return back to a regular String
end

#resetObject



21
22
23
# File 'lib/one_apm/agent/database/obfuscator.rb', line 21

def reset
  @obfuscator = method(:default_sql_obfuscator)
end

#set_sql_obfuscator(type, &block) ⇒ Object

Sets the sql obfuscator used to clean up sql when sending it to the server. Possible types are:

:before => sets the block to run before the existing obfuscators

:after => sets the block to run after the existing obfuscator(s)

:replace => removes the current obfuscator and replaces it with the provided block



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/one_apm/agent/database/obfuscator.rb', line 36

def set_sql_obfuscator(type, &block)
  if type == :before
    @obfuscator = OneApm::ChainedCall.new(block, @obfuscator)
  elsif type == :after
    @obfuscator = OneApm::ChainedCall.new(@obfuscator, block)
  elsif type == :replace
    @obfuscator = block
  else
    fail "unknown sql_obfuscator type #{type}"
  end
end