Class: MyObfuscate

Inherits:
Object
  • Object
show all
Defined in:
lib/my_obfuscate.rb,
lib/my_obfuscate/mysql.rb,
lib/my_obfuscate/version.rb,
lib/my_obfuscate/postgres.rb,
lib/my_obfuscate/sql_server.rb,
lib/my_obfuscate/config_applicator.rb,
lib/my_obfuscate/copy_statement_parser.rb,
lib/my_obfuscate/insert_statement_parser.rb

Overview

Class for obfuscating MySQL dumps. This can parse mysqldump outputs when using the -c option, which includes column names in the insert statements.

Defined Under Namespace

Modules: CopyStatementParser, InsertStatementParser Classes: ConfigApplicator, Mysql, Postgres, SqlServer

Constant Summary collapse

NUMBER_CHARS =
"1234567890"
USERNAME_CHARS =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" + NUMBER_CHARS
SENSIBLE_CHARS =
USERNAME_CHARS + '+-=[{]}/?|!@#$%^&*()`~'
VERSION =
"0.5.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ MyObfuscate

Make a new MyObfuscate object. Pass in a configuration structure to define how the obfuscation should be performed. See the README.rdoc file for more information.



17
18
19
# File 'lib/my_obfuscate.rb', line 17

def initialize(configuration = {})
  @config = configuration
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/my_obfuscate.rb', line 9

def config
  @config
end

#database_typeObject

Returns the value of attribute database_type.



9
10
11
# File 'lib/my_obfuscate.rb', line 9

def database_type
  @database_type
end

#fail_on_unspecified_columnsObject

Returns the value of attribute fail_on_unspecified_columns.



9
10
11
# File 'lib/my_obfuscate.rb', line 9

def fail_on_unspecified_columns
  @fail_on_unspecified_columns
end

#globally_kept_columnsObject

Returns the value of attribute globally_kept_columns.



9
10
11
# File 'lib/my_obfuscate.rb', line 9

def globally_kept_columns
  @globally_kept_columns
end

Instance Method Details

#check_for_defined_columns_not_in_table(table_name, columns) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/my_obfuscate.rb', line 55

def check_for_defined_columns_not_in_table(table_name, columns)
  return unless config[table_name]
  missing_columns = config[table_name].keys - columns
  unless missing_columns.length == 0
    error_message = missing_columns.map do |missing_column|
      "Column '#{missing_column}' could not be found in table '#{table_name}', please fix your obfuscator config."
    end.join("\n")
    raise RuntimeError.new(error_message)
  end
end

#check_for_table_columns_not_in_definition(table_name, columns) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/my_obfuscate.rb', line 66

def check_for_table_columns_not_in_definition(table_name, columns)
  missing_columns = columns - (config[table_name].keys + (globally_kept_columns || []).map {|i| i.to_sym}).uniq
  unless missing_columns.length == 0
    error_message = missing_columns.map do |missing_column|
      "Column '#{missing_column}' defined in table '#{table_name}', but not found in table definition, please fix your obfuscator config."
    end.join("\n")
    raise RuntimeError.new(error_message)
  end
end

#database_helperObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/my_obfuscate.rb', line 25

def database_helper
  if @database_helper.nil?
    if @database_type == :sql_server
      @database_helper = SqlServer.new
    elsif @database_type == :postgres
      @database_helper = Postgres.new
    else
      @database_helper = Mysql.new
    end
  end

  @database_helper
end

#fail_on_unspecified_columns?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/my_obfuscate.rb', line 21

def fail_on_unspecified_columns?
  @fail_on_unspecified_columns
end

#obfuscate(input_io, output_io) ⇒ Object

Read an input stream and dump out an obfuscated output stream. These streams could be StringIO objects, Files, or STDIN and STDOUT.



41
42
43
# File 'lib/my_obfuscate.rb', line 41

def obfuscate(input_io, output_io)
  database_helper.parse(self, config, input_io, output_io)
end

#obfuscate_bulk_insert_line(line, table_name, columns) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/my_obfuscate.rb', line 76

def obfuscate_bulk_insert_line(line, table_name, columns)
  table_config = config[table_name]
  if table_config == :truncate
    ""
  elsif table_config == :keep
    line
  else
    check_for_defined_columns_not_in_table(table_name, columns)
    check_for_table_columns_not_in_definition(table_name, columns) if fail_on_unspecified_columns?
    # Note: Remember to SQL escape strings in what you pass back.
    reassembling_each_insert(line, table_name, columns) do |row|
      ConfigApplicator.apply_table_config(row, table_config, columns)
    end
  end
end

#reassembling_each_insert(line, table_name, columns) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/my_obfuscate.rb', line 45

def reassembling_each_insert(line, table_name, columns)
  output = database_helper.rows_to_be_inserted(line).map do |sub_insert|
    result = yield(sub_insert)
    result = result.map do |i|
      database_helper.make_valid_value_string(i)
    end
  end
  database_helper.make_insert_statement(table_name, columns, output)
end