module NewRelic::Agent::Database::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
MYSQL_COMPONENTS_REGEX
ORACLE_COMPONENTS_REGEX
PLACEHOLDER
POSTGRES_COMPONENTS_REGEX
SQLITE_COMPONENTS_REGEX

Public Class Methods

generate_regex(dialect) click to toggle source
# File lib/new_relic/agent/database/obfuscation_helpers.rb, line 60
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/new_relic/agent/database/obfuscation_helpers.rb, line 92
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/new_relic/agent/database/obfuscation_helpers.rb, line 72
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_single_quote_literals(sql) click to toggle source
# File lib/new_relic/agent/database/obfuscation_helpers.rb, line 54
def obfuscate_single_quote_literals(sql)
  return sql unless sql&.match?(COMPONENTS_REGEX_MAP[:single_quotes])

  sql.gsub(COMPONENTS_REGEX_MAP[:single_quotes], PLACEHOLDER)
end