Method: Confy::Config.load

Defined in:
lib/confy/config.rb

.load(url = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/confy/config.rb', line 45

def self.load(url = {})
  url = self.match(url)

  auth = {}

  if url[:user] and url[:pass]
    auth[:username] = url[:user]
    auth[:password] = url[:pass]
  end

  client = Confy::Client.new(auth, { :base => url[:host] })

  body = client.instance_variable_get(:@http_client).get(url[:path]).body

  return body if body.is_a?(Hash)

  decryptPass = ENV['CONFY_DECRYPT_PASS']

  raise 'Invalid credential document' if !body.is_a?(String)
  raise 'No decryption password found. Fill env var CONFY_DECRYPT_PASS' if decryptPass.nil?

  # Strip quotes
  body = body[1..-2] if body[0] == '"' and body[-1] == '"'

  cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
  cipher.decrypt
  cipher.iv = Base64.decode64(body[0..23])
  cipher.key = Digest::MD5.hexdigest(decryptPass)

  decrypted = cipher.update(Base64.decode64(body[24..-1])) + cipher.final

  begin
    body = JSON.parse(decrypted)
  rescue JSON::ParserError
    raise 'Decryption password is wrong'
  end
end