Class: TingYun::Agent::Database::Obfuscator

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

Overview

混淆器

Defined Under Namespace

Modules: ObfuscationHelpers

Constant Summary collapse

QUERY_TOO_LARGE_MESSAGE =
"Query too large (over 16k characters) to safely obfuscate"
FAILED_TO_OBFUSCATE_MESSAGE =
"Failed to obfuscate SQL query - quote characters remained after obfuscation"
QUOTED_STRINGS_REGEX =
/'(?:[^']|'')*'|"(?:[^"]|"")*"/
LABEL_LINE_REGEX =
/^([^:\n]*:\s+).*$/.freeze

Constants included from ObfuscationHelpers

ObfuscationHelpers::LITERAL_DOUBLE_QUOTE, ObfuscationHelpers::LITERAL_SINGLE_QUOTE, ObfuscationHelpers::NUMERICS_REGEX, ObfuscationHelpers::PLACEHOLDER, ObfuscationHelpers::REVERSE_ANY_QUOTES_REGEX, ObfuscationHelpers::REVERSE_SINGLE_QUOTES_REGEX, ObfuscationHelpers::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.



286
287
288
# File 'lib/ting_yun/agent/database.rb', line 286

def initialize
  reset
end

Instance Attribute Details

#obfuscatorObject (readonly)

Returns the value of attribute obfuscator.



284
285
286
# File 'lib/ting_yun/agent/database.rb', line 284

def obfuscator
  @obfuscator
end

Instance Method Details

#default_sql_obfuscator(sql) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/ting_yun/agent/database.rb', line 297

def default_sql_obfuscator(sql)
  stmt = sql.kind_of?(Statement) ? sql : Statement.new(sql)

  if stmt.sql[-3,3] == '...'
    return QUERY_TOO_LARGE_MESSAGE
  end

  obfuscate_double_quotes = stmt.adapter.to_s !~ /postgres|sqlite/

  obfuscated = obfuscate_numeric_literals(stmt.sql)

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


  obfuscated.to_s # return back to a regular String
end

#obfuscate_postgres_explain(explain) ⇒ Object



329
330
331
332
333
334
335
# File 'lib/ting_yun/agent/database.rb', line 329

def obfuscate_postgres_explain(explain)
  explain.gsub!(QUOTED_STRINGS_REGEX) do |match|
    match.start_with?('"') ? match : '?'
  end
  explain.gsub!(LABEL_LINE_REGEX,   '\1?')
  explain
end

#resetObject



290
291
292
# File 'lib/ting_yun/agent/database.rb', line 290

def reset
  @obfuscator = method(:default_sql_obfuscator)
end