Class: MyObfuscate
- Inherits:
-
Object
- Object
- MyObfuscate
- 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,
lib/my_obfuscate/config_scaffold_generator.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: ConfigScaffoldGenerator, CopyStatementParser, InsertStatementParser Classes: ConfigApplicator, Mysql, Postgres, SqlServer
Constant Summary collapse
- NUMBER_CHARS =
"1234567890"- USERNAME_CHARS =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" + NUMBER_CHARS
- SENSIBLE_CHARS =
USERNAME_CHARS + '+-=[{]}/?|!@#$%^&*()`~'
- VERSION =
"0.6"
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#database_type ⇒ Object
Returns the value of attribute database_type.
-
#fail_on_unspecified_columns ⇒ Object
Returns the value of attribute fail_on_unspecified_columns.
-
#globally_kept_columns ⇒ Object
Returns the value of attribute globally_kept_columns.
-
#scaffolded_tables ⇒ Object
Returns the value of attribute scaffolded_tables.
Instance Method Summary collapse
- #check_for_defined_columns_not_in_table(table_name, columns) ⇒ Object
- #check_for_table_columns_not_in_definition(table_name, columns) ⇒ Object
- #database_helper ⇒ Object
- #extra_column_list(table_name, columns) ⇒ Object
- #fail_on_unspecified_columns? ⇒ Boolean
-
#initialize(configuration = {}) ⇒ MyObfuscate
constructor
Make a new MyObfuscate object.
- #missing_column_list(table_name, columns) ⇒ Object
-
#obfuscate(input_io, output_io) ⇒ Object
Read an input stream and dump out an obfuscated output stream.
- #obfuscate_bulk_insert_line(line, table_name, columns, ignore = nil) ⇒ Object
- #reassembling_each_insert(line, table_name, columns, ignore = nil) ⇒ Object
-
#scaffold(input_io, output_io) ⇒ Object
Read an input stream and dump out a config file scaffold.
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 20 |
# File 'lib/my_obfuscate.rb', line 17 def initialize(configuration = {}) @config = configuration @scaffolded_tables = {} end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
9 10 11 |
# File 'lib/my_obfuscate.rb', line 9 def config @config end |
#database_type ⇒ Object
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_columns ⇒ Object
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_columns ⇒ Object
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 |
#scaffolded_tables ⇒ Object
Returns the value of attribute scaffolded_tables.
9 10 11 |
# File 'lib/my_obfuscate.rb', line 9 def scaffolded_tables @scaffolded_tables end |
Instance Method Details
#check_for_defined_columns_not_in_table(table_name, columns) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/my_obfuscate.rb', line 67 def check_for_defined_columns_not_in_table(table_name, columns) missing_columns = extra_column_list(table_name, columns) unless missing_columns.length == 0 = 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() end end |
#check_for_table_columns_not_in_definition(table_name, columns) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/my_obfuscate.rb', line 82 def check_for_table_columns_not_in_definition(table_name, columns) missing_columns = missing_column_list(table_name, columns) unless missing_columns.length == 0 = 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() end end |
#database_helper ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/my_obfuscate.rb', line 26 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 |
#extra_column_list(table_name, columns) ⇒ Object
62 63 64 65 |
# File 'lib/my_obfuscate.rb', line 62 def extra_column_list(table_name, columns) config_columns = (config[table_name] || {}).keys config_columns - columns end |
#fail_on_unspecified_columns? ⇒ Boolean
22 23 24 |
# File 'lib/my_obfuscate.rb', line 22 def fail_on_unspecified_columns? @fail_on_unspecified_columns end |
#missing_column_list(table_name, columns) ⇒ Object
77 78 79 80 |
# File 'lib/my_obfuscate.rb', line 77 def missing_column_list(table_name, columns) config_columns = (config[table_name] || {}).keys columns - (config_columns + (globally_kept_columns || []).map {|i| i.to_sym}).uniq 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.
42 43 44 |
# File 'lib/my_obfuscate.rb', line 42 def obfuscate(input_io, output_io) database_helper.parse(self, config, input_io, output_io) end |
#obfuscate_bulk_insert_line(line, table_name, columns, ignore = nil) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/my_obfuscate.rb', line 92 def obfuscate_bulk_insert_line(line, table_name, columns, ignore = nil) 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, ignore) do |row| ConfigApplicator.apply_table_config(row, table_config, columns) end end end |
#reassembling_each_insert(line, table_name, columns, ignore = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/my_obfuscate.rb', line 52 def reassembling_each_insert(line, table_name, columns, ignore = nil) 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, ignore) end |
#scaffold(input_io, output_io) ⇒ Object
Read an input stream and dump out a config file scaffold. These streams could be StringIO objects, Files, or STDIN and STDOUT.
48 49 50 |
# File 'lib/my_obfuscate.rb', line 48 def scaffold(input_io, output_io) database_helper.generate_config(self, config, input_io, output_io) end |