Skip to content

Instantly share code, notes, and snippets.

@cantino
Created October 15, 2015 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cantino/5376e73b0ad806dc4da4 to your computer and use it in GitHub Desktop.
Save cantino/5376e73b0ad806dc4da4 to your computer and use it in GitHub Desktop.
Example my_obfuscate configuration file that validates against the Rails schema.rb
#!/usr/bin/env ruby
require "rubygems"
require "my_obfuscate"
obfuscator = MyObfuscate.new({
:people => {
:email => { :type => :email, :skip_regexes => [/^[\w\.\_]+@my_company\.com$/i] },
:ethnicity => :keep,
:crypted_password => { :type => :fixed, :string => "SOME_FIXED_PASSWORD_FOR_EASE_OF_DEBUGGING" },
:salt => { :type => :fixed, :string => "SOME_THING" },
:remember_token => :null,
:remember_token_expires_at => :null,
:age => { :type => :null, :unless => lambda { |person| person[:email] == "hello@example.com" } },
:photo_file_name => :null,
:photo_content_type => :null,
:photo_file_size => :null,
:photo_updated_at => :null,
:postal_code => { :type => :fixed, :string => "94109", :unless => lambda {|person| person[:postal_code] == "12345"} },
:name => :name,
:full_address => :address,
:bio => { :type => :lorem, :number => 4 },
:relationship_status => { :type => :fixed, :one_of => ["Single", "Divorced", "Married", "Engaged", "In a Relationship"] },
:has_children => { :type => :integer, :between => 0..1 },
},
:invites => :truncate,
:invite_requests => :truncate,
:tags => :keep,
:relationships => {
:account_id => :keep,
:code => { :type => :string, :length => 8, :chars => MyObfuscate::USERNAME_CHARS }
}
})
obfuscator.fail_on_unspecified_columns = true # if you want it to require every column in the table to be in the above definition
obfuscator.globally_kept_columns = %w[id created_at updated_at] # if you set fail_on_unspecified_columns, you may want this as well
# Validate against schema.rb
db_schema = File.read(File.expand_path(File.join(File.dirname(__FILE__), "..", "db", "schema.rb"))) # assumes we're located in config/
table_name = var = error = nil
errors = {}
found_columns = {}
db_schema.each_line do |line|
if line =~ /create_table (["'])([\w\d_-]+)\1.*?\|(\w+)\|/
table_name = $2
var = $3
elsif table_name
if line =~ /#{Regexp::escape var}\.\w+\s+(['"])([\w\d_-]+)\1/
column = $2
next if errors[table_name]
case obfuscator.config[table_name.to_sym]
when nil
STDERR.puts "Table #{table_name} not found in the obfuscator specification."; errors[table_name] = true
when :keep, :truncate
# Looks good
when Hash
if obfuscator.config[table_name.to_sym][column.to_sym] || obfuscator.globally_kept_columns.include?(column)
# valid
found_columns[table_name.to_sym] ||= {}
found_columns[table_name.to_sym][column.to_sym] = true
else
STDERR.puts "Missing column specification for #{table_name}##{column} in the obfuscator specification."
errors[table_name + column] = true
end
else
STDERR.puts "Unknown definition for #{table_name}: #{obfuscator.config[table_name.to_sym]}"
errors[table_name] = true
end
end
end
end
found_columns.each do |table, config|
obfuscator.config[table].keys.each do |key|
if !obfuscator.globally_kept_columns.include?(key) && config[key] != true
STDERR.puts "Unknown column in obfuscator configuration: #{table}.#{key}"
errors[table] = true
end
end
end
exit 1 if errors.length > 0
exit 0 if ARGV[0] && ARGV[0] =~ /validate/
obfuscator.obfuscate(STDIN, STDOUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment