Skip to content

Instantly share code, notes, and snippets.

@corny
Created March 17, 2012 11:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corny/2057660 to your computer and use it in GitHub Desktop.
Save corny/2057660 to your computer and use it in GitHub Desktop.
Rake task to replace the secret token in your config/initializers/secret_token.rb
#
# License: WTFPL (http://sam.zoy.org/wtfpl/)
#
namespace :secret do
desc 'Replace the secure secret key in your secret_token.rb'
task :replace do
pattern = /(\.secret_token *= *')\w+(')/
secret = SecureRandom.hex(64)
filepath = "#{Rails.root}/config/initializers/secret_token.rb"
content = File.read(filepath)
unless pattern
STDERR.puts "no secret token found in #{filepath}"
exit 1
end
# replace the secret token
content.gsub!(pattern,"\\1#{secret}\\2")
# write the new configuration
File.open(filepath, 'w') {|f| f.write(content) }
puts "Secret token succesfully replaced"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment