Last active
August 29, 2015 14:05
-
-
Save smostovoy/d21950aaa0b3cfb2027a to your computer and use it in GitHub Desktop.
Keychain - store service keys encrypted in rails config
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
Gem::Specification.new do |s| | |
s.name = "keychain" | |
s.files = ['keychain.rb'] | |
s.require_path = '.' | |
s.version = '0.0.1' | |
s.add_runtime_dependency 'aes', '~> 0.5.0' | |
end |
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 'aes' | |
module Keychain | |
class << self | |
def get(keyname) | |
encrypted_key = keys[keyname] | |
AES.decrypt(encrypted_key, encryption_key) | |
end | |
def set(keyname, key) | |
keys[keyname] = AES.encrypt(key, encryption_key) | |
File.open("#{Rails.root}/config/keychain.yml", 'w') {|f| f.write keys(false).to_yaml } | |
end | |
def encryption_key | |
ENV['MAIN_KEY'] || 'development' | |
end | |
def keys(env=true) | |
@keys ||= YAML.load_file("#{Rails.root}/config/keychain.yml") | |
env ? @keys[Rails.env] : @keys | |
end | |
end | |
end |
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
production: | |
rollbar: kLAAHj74VmV+nRwl+XGNnQ==$BiFlxL6ze81lIgJ3ZargofpS3nqMtRLjX8Flt4m2L84YtUxl2o= | |
staging: | |
rollbar: z3mvkFv8J/6A==$LVrFWmJaCxbyzrjaeJtqdQ6QjO4uOKnMHKfa9iDYtUxl2oUHq+LjXFkVSJOu= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment