Class: Confy::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/confy/config.rb

Class Method Summary collapse

Class Method Details

.env(url = {}) ⇒ Object



83
84
85
# File 'lib/confy/config.rb', line 83

def self.env(url = {})
  self.path(self.load(url))
end

.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

.match(url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/confy/config.rb', line 10

def self.match(url)
   if url.is_a?(String)
    name_regex = '([a-z0-9][a-z0-9-]*[a-z0-9])'
    token_regex = '([a-f0-9]{40})'
    path_regex = 'orgs\\/' + name_regex + '(\\/projects\\/' + name_regex + '\\/envs\\/' + name_regex + '\\/config|\\/config\\/' + token_regex + ')'
    url_regex = Regexp.new('(https?:\\/\\/)((.*):(.*)@)?(.*)\\/(' + path_regex + '|heroku\\/config)', true)

    matches = url_regex.match(url)

    raise 'Invalid URL' if matches.nil?

    url = {
      :host => matches[1] + matches[5],
      :user => matches[3], :pass => matches[4],
      :org => matches[7], :project => matches[9], :env => matches[10],
      :token => matches[11],
      :heroku => (matches[6] == 'heroku/config')
    }
  end

  raise 'Invalid URL' if !url.is_a?(Hash)

  if url[:host] and url[:user] and url[:pass] and url[:heroku]
    url[:path] = '/heroku/config'
  elsif url[:host] and url[:token] and url[:org]
    url[:path] = '/orgs/' + url[:org] + '/config/' + url[:token]
  elsif url[:host] and url[:user] and url[:pass] and url[:org] and url[:project] and url[:env]
    url[:path] = '/orgs/' + url[:org] + '/projects/' + url[:project] + '/envs/' + url[:env] + '/config'
  else
    raise 'Invalid URL'
  end

  url
end

.path(config, str = '') ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/confy/config.rb', line 87

def self.path(config, str = '')
  type = config.class

  if type == Array
    config.each_with_index do |value, key|
      self.path(value, "#{str}_#{key}")
    end
  elsif type == Hash
    config.each do |key, value|
      self.path(value, "#{str}_#{key.upcase}")
    end
  elsif type == TrueClass
    ENV[str.slice(1, str.length)] = '1'
  elsif type == FalseClass
    ENV[str.slice(1, str.length)] = '0'
  else
    ENV[str.slice(1, str.length)] = config.to_s
  end
end