module TingYun::Agent::Database::Obfuscator::ObfuscationHelpers

Constants

CASSANDRA_COMPONENTS_REGEX
CLEANUP_REGEX

We use these to check whether the query contains any quote characters after obfuscation. If so, that's a good indication that the original query was malformed, and so our obfuscation can't reliably find literals. In such a case, we'll replace the entire query with a placeholder.

COMPONENTS_REGEX_MAP
DIALECT_COMPONENTS
FAILED_TO_OBFUSCATE_MESSAGE
FALLBACK_REGEX
LABEL_LINE_REGEX
MYSQL_COMPONENTS_REGEX
ORACLE_COMPONENTS_REGEX
PLACEHOLDER
POSTGRES_COMPONENTS_REGEX
QUOTED_STRINGS_REGEX
SQLITE_COMPONENTS_REGEX

Public Class Methods

generate_regex(dialect) click to toggle source
# File lib/ting_yun/agent/database/obfuscator.rb, line 107
def self.generate_regex(dialect)
  components = DIALECT_COMPONENTS[dialect]
  Regexp.union(components.map{|component| COMPONENTS_REGEX_MAP[component]})
end

Public Instance Methods

detect_unmatched_pairs(obfuscated, adapter) click to toggle source
# File lib/ting_yun/agent/database/obfuscator.rb, line 139
def detect_unmatched_pairs(obfuscated, adapter)
  if CLEANUP_REGEX[adapter]
    CLEANUP_REGEX[adapter].match(obfuscated)
  else
    CLEANUP_REGEX[:mysql].match(obfuscated)
  end
end
obfuscate(sql, adapter) click to toggle source
# File lib/ting_yun/agent/database/obfuscator.rb, line 119
def obfuscate(sql, adapter)
  case adapter
    when :mysql, :mysql2
      regex = MYSQL_COMPONENTS_REGEX
    when :postgres
      regex = POSTGRES_COMPONENTS_REGEX
    when :sqlite
      regex = SQLITE_COMPONENTS_REGEX
    when :oracle, :oracle_enhanced
      regex = ORACLE_COMPONENTS_REGEX
    when :cassandra
      regex = CASSANDRA_COMPONENTS_REGEX
    else
      regex = FALLBACK_REGEX
  end
  obfuscated = sql.gsub(regex, PLACEHOLDER)
  obfuscated = FAILED_TO_OBFUSCATE_MESSAGE if detect_unmatched_pairs(obfuscated, adapter)
  obfuscated
end
obfuscate_postgres_explain(sql) click to toggle source
# File lib/ting_yun/agent/database/obfuscator.rb, line 87
def obfuscate_postgres_explain(sql)
  sql.gsub!(QUOTED_STRINGS_REGEX) do |match|
    match.start_with?('"') ? match : '?'
  end

  sql.gsub!(LABEL_LINE_REGEX,   '\1?')
  sql
end
obfuscate_single_quote_literals(sql) click to toggle source
# File lib/ting_yun/agent/database/obfuscator.rb, line 102
def obfuscate_single_quote_literals(sql)
  return sql unless sql =~ COMPONENTS_REGEX_MAP[:single_quotes]
  sql.gsub(COMPONENTS_REGEX_MAP[:single_quotes], PLACEHOLDER)
end