class MuchSecrets::Secret

Public Class Methods

new(options = {}) click to toggle source
# File lib/muchsecrets/secret.rb, line 6
def initialize(options = {})
  @private_key = options[:private_key]
  @public_key  = options[:public_key]
  @base_url    = options[:base_url] || "http://consul:8500/v1/kv" 
  @cipher      = OpenSSL::Cipher.new(options[:cipher] || "AES-256-CFB")
end

Public Instance Methods

encrypt_string(val) click to toggle source
# File lib/muchsecrets/secret.rb, line 23
def encrypt_string(val)
  cert = OpenSSL::X509::Certificate.new(File.read(@public_key))
  return OpenSSL::PKCS7::encrypt([cert], val, @cipher, OpenSSL::PKCS7::BINARY)
end
get_consul_secret(uri) click to toggle source
# File lib/muchsecrets/secret.rb, line 18
def get_consul_secret(uri)
  uri = uri + '?raw'
  return get_http_secret(uri)
end
get_http_secret(uri) click to toggle source
# File lib/muchsecrets/secret.rb, line 13
def get_http_secret(uri)
  encrypted_secret = fetch_encrypted_http_secret(uri)
  return decrypt_string(encrypted_secret)
end

Private Instance Methods

decrypt_string(val) click to toggle source
# File lib/muchsecrets/secret.rb, line 36
def decrypt_string(val)
  cert = OpenSSL::X509::Certificate.new(File.read(@public_key))
  key  = OpenSSL::PKey::RSA.new(File.read(@private_key))
  return OpenSSL::PKCS7.new(val).decrypt(key, cert)
end
fetch_encrypted_http_secret(uri) click to toggle source
# File lib/muchsecrets/secret.rb, line 30
def fetch_encrypted_http_secret(uri)
  # get the secret from the http endpoint
  uri = File.join(@base_url, uri)
  return Net::HTTP.get(URI(uri)).chomp
end