-
-
Save anonymous/c260b94a464dc092ee36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'gibberish' | |
task rotate_db_keys: :environment do | |
desc "rotates the keys for usernames and passwords" | |
new_salt = Gibberish::SHA512(Time.now.to_s) | |
new_key = Gibberish::AES.new(new_salt) | |
old_key = $cipher | |
Rake::Task['maintenance:start'].execute | |
Login.skip_callback(:save, :before, :encrypt_record) | |
Login.skip_callback(:save, :after, :decrypt_record) | |
Login.skip_callback(:find, :after, :decrypt_record) | |
count = Login.count | |
current_count = 0 | |
start = Time.now | |
Login.transaction do | |
Login.find_in_batches(:batch_size => 100) do |group| | |
group.each do |login| | |
login.send(:decrypt_record) | |
login.send(:encrypt_record, new_key) | |
login.save | |
end | |
current_count+= 100 | |
elapsed = Time.now - start | |
percentage = (current_count.to_f / count.to_f) * 100 | |
remaining_time = (elapsed / current_count.to_f) * (count - current_count) | |
if (((current_count % 1000) == 0) or ) | |
print "\r%-100s" % "#{current_count}/#{count}: #{ "%.2f" % percentage}% done: #{"%.2f" % elapsed}s Elapsed, #{"%.0f" % remaining_time}s remaining." | |
end | |
end | |
end | |
File.open("./config/initializers/encryption.rb", 'w') {|f| f.write("$cipher = Gibberish::AES.new(\'#{new_salt}\')") } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment