Skip to content

Commit

Permalink
Independent study. Add db anonymization (expertiza#2744)
Browse files Browse the repository at this point in the history
* Add db anonyimization

* Updated logic in scrub_database.rb
* Added Faker gem

* Remove Faker gem, fix .sh script

* Add name.yml

* Add comments

* Delete db_anonymization.sh

* Update scrub_database.rake

* Update scrub_database.rake

* Update scrub_database.rake

* Update scrub_database.rb

* Update name.yml

* Fix name generation

* Add anonymization environment

---------

Co-authored-by: Ali Qureshi <[email protected]>
  • Loading branch information
kamathprasad9 and qureshi-ali authored Apr 25, 2024
1 parent ef449f6 commit af5f58e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
58 changes: 58 additions & 0 deletions config/environments/anonymization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Expertiza::Application.configure do
# Settings specified here will take precedence over those in config/environment.rb

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
# config.serve_static_assets = false
# config.action_mailer.default_url_options = { :host => 'localhost:3000' }
host = '152.7.98.82:8080'
config.action_mailer.default_url_options = { host: '152.7.98.82:8080', protocol: 'http' }
# Do not eager load code on boot.
config.eager_load = false

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# config.active_record.whitelist_attributes = false # need protected_attributes gem

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Print development logs
config.log_level = :error

config.log_tags = %i[remote_ip uuid]

config.log_formatter = proc do |s, ts, pg, msg|
if msg.is_a?(LoggerMessage)
"TST=[#{ts}] SVT=[#{s}] PNM=[#{pg}] OIP=[#{msg.oip}] RID=[#{msg.req_id}] CTR=[#{msg.generator}] UID=[#{msg.unity_id}] MSG=[#{filter(msg.message)}]\n"
else
"TST=[#{ts}] SVT=[#{s}] PNM=[#{pg}] OIP=[] RID=[] CTR=[] UID=[] MSG=[#{filter(msg)}]\n"
end
end

def filter(msg)
msg.tr("\n", ' ')
end

config.action_view.logger = nil

# Raise an error on page load if there are pending migrations
config.active_record.migration_error = :page_load

# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
config.react.variant = :development
config.active_record.logger = nil
# Line 63-69 are for 'bullet' gem initialization.
config.after_initialize do
Bullet.enable = true
Bullet.alert = false
Bullet.bullet_logger = false
Bullet.console = false
Bullet.rails_logger = false
end
end
4 changes: 4 additions & 0 deletions config/google_auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ test:
production:
client_key: <%= ENV['GOOGLE_KEY'] %>
client_secret: <%= ENV['GOOGLE_SECRET'] %>

anonymization:
client_key: '1025716012590-2nlv84m8uo3pled9bide8442r2020gc3.apps.googleusercontent.com'
client_secret: 'kWjKq314ywRKAF5s5A0F8Wxh'
7 changes: 7 additions & 0 deletions config/name.yml

Large diffs are not rendered by default.

45 changes: 38 additions & 7 deletions db/data_migrations/scrub_database.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
require 'yaml'

class ScrubDatabase
def self.run!
# Loop through each user
User.find_each do |user|
user.name = "#{user.role.name.downcase.gsub(/[- ]/, '_')}#{user.id}"
user.fullname = "#{user.id}, #{user.role.name.downcase.gsub(/[- ]/, '_')}"
user.email = '[email protected]'
user.handle = 'handle'
user.password = 'password'
# user.password_confirmation = "password"
user.save(validate: false)
# Generate a new fake user record
loop do
fake_name = FakeNameGenerator.first_name
fake_lastname = FakeNameGenerator.last_name
num = rand(1..9)
role = user.role.name.downcase.gsub(/[- ]/, '_')
user.name = role == 'student' ? "#{fake_name}_#{fake_lastname[0..3]}#{num}" : "#{role}_#{fake_name}__#{fake_lastname[0..3]}"
user.fullname = role == 'student' ? "#{fake_lastname}, #{fake_name}" : "#{fake_name}, #{role}"
user.email = '[email protected]'
user.handle = 'handle'
user.password = 'password'
username = role == 'student' ? "#{fake_name}_#{fake_lastname[0..3]}#{num}" : "#{role}_#{fake_name}__#{fake_lastname[0..3]}"
# Checking if username already exists
if User.find_by(:name => username) == nil #no record exists of that username
# print "new user"
user.save(validate: false)
break
end
# if record exists, generate new fake record by going through the loop again
end
end

Participant.find_each do |participant|
participant.handle = 'handle'
participant.save(validate: false)
end
end
end


class FakeNameGenerator
NAMES = YAML.load_file("#{Rails.root}/config/name.yml")['name']

def self.first_name
gender = [:male_first_name, :female_first_name, :neutral_first_name].sample
NAMES["#{gender}"].sample
end

def self.last_name
NAMES["#{:last_name}"].sample
end
end

0 comments on commit af5f58e

Please sign in to comment.