Skip to content

Instantly share code, notes, and snippets.

@smostovoy
Last active August 29, 2015 14:05
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 smostovoy/d21950aaa0b3cfb2027a to your computer and use it in GitHub Desktop.
Save smostovoy/d21950aaa0b3cfb2027a to your computer and use it in GitHub Desktop.
Keychain - store service keys encrypted in rails config
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
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
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