Class: Provider::CDB

Inherits:
Object
  • Object
show all
Defined in:
lib/depengine/provider/cdb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject

Returns the value of attribute context.



5
6
7
# File 'lib/depengine/provider/cdb.rb', line 5

def context
  @context
end

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/depengine/provider/cdb.rb', line 6

def env
  @env
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/depengine/provider/cdb.rb', line 4

def host
  @host
end

#protocolObject

Returns the value of attribute protocol.



3
4
5
# File 'lib/depengine/provider/cdb.rb', line 3

def protocol
  @protocol
end

Instance Method Details

#decrypt(password, transferrable) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/depengine/provider/cdb.rb', line 194

def decrypt(password,transferrable)
  alg           = transferrable.split("}")[0].split("{")[1]
  hex_encrypted = transferrable.split(":")[0].split("}")[1]
  hex_iv        = transferrable.split(":")[1]
  text          = Base64.decode64(hex_encrypted)
  iv            = Base64.decode64(hex_iv)
  c             = OpenSSL::Cipher::Cipher.new(alg)
  c.decrypt

  c.key = Digest::SHA1.hexdigest(password)
  c.iv  = iv
  d     = c.update(text)
  d << c.final

  return d
end

#get_parameter(path, version) ⇒ Object



8
9
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
44
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
# File 'lib/depengine/provider/cdb.rb', line 8

def get_parameter(path, version)
  result = "@@ERROR@@"
  version = "" if version == 0

  Helper.validates_presence_of host
  Helper.validates_presence_of context
  Helper.validates_presence_of version
  Helper.validates_presence_of env
  Helper.validates_presence_of path

  begin
    url = protocol + '://' + File.join(host, context, \
                                           version.to_s, env, path)
    result = Net::HTTP.get(URI.parse(url))
    if path =~ /.json$/
      begin
        result = JSON.parse( result )
      rescue Exception => e
        $log.writer.error "Error: got invalid json from cdb"
        $log.writer.error "Request was: #{url.to_s}"
        $log.writer.error "Answer was: #{Net::HTTP.get(URI.parse(url))}"
        $log.writer.error e.message
        exit 1
      end
    end

    # decrypt encrypted entries automagically
    password      = ""
    password_file = (File.join(File.dirname($exec_file_path), '../etc/crypt.yaml'))

    if File.file? password_file
      password = YAML::load_file(password_file)['password']
    else
      $log.writer.warn "Can not open etc/crypt.yaml, no decryption possible"
    end

    if result.class.name == "Hash"
      result.each do |key,value|
        if value.class.name != "Fixnum" and \
          ((value =~ /^\{[\w-]+\}.+/ ) != nil)
          result[key] = decrypt(password, value)
        end
     end
    else
      if result.class.name != "Fixnum" and \
          ((result =~ /^\{[\w-]+\}.+/ ) != nil)
        result = decrypt(password, result)
      end
    end

  rescue Exception => e
    $log.writer.error "Error: while connecting to configuration database"
    $log.writer.error "Request was: #{url.to_s}"
    $log.writer.error e.message
    exit 1
  end

  if result == "@@ERROR@@"
    $log.writer.error "Error while talking to configuration database, giving up!"
    $log.writer.error "Request was: #{url.to_s}"
    exit 1
  end

  result
end

#get_parameters(path, version) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/depengine/provider/cdb.rb', line 74

def get_parameters(path, version)
  result  = "@@ERROR@@"
  version = "" if version == 0
  url     = ""


  Helper.validates_presence_of host
  Helper.validates_presence_of context
  Helper.validates_presence_of version
  Helper.validates_presence_of env
  Helper.validates_presence_of path

  begin
    if true
      begin
        cdb_filesystem = Provider::CDB_FILESYSTEM.new
        result = JSON.parse(cdb_filesystem.read_data(path))
        #puts result
      rescue Exception => e
        $log.writer.error "Error: got invalid json from cdb"
        $log.writer.error "Request was: #{path.to_s}"
        $log.writer.error e.message
        exit 1
      end
    else
      url    = protocol + '://' + \
               File.join(host, context, env, path, "showall.json")

      begin
        result = JSON.parse(Net::HTTP.get(URI.parse(url)))
      rescue Exception => e
        $log.writer.error "Error: got invalid json from cdb"
        $log.writer.error "Request was: #{url.to_s}"
        $log.writer.error "Answer was: #{Net::HTTP.get(URI.parse(url))}"
        $log.writer.error e.message
        exit 1
      end
    end

    # decrypt encrypted entries automagically
    password      = ""
    if ENV['USE_DEPENGINE_AS_GEM']
      password_file = \
        ( File.join(File.dirname($exec_file_path), '../../etc/crypt.yaml') )
    else
      password_file = \
        ( File.join(File.dirname($exec_file_path), '../etc/crypt.yaml') )
    end
    if File.file? password_file
      password = YAML::load_file(password_file)['password']
    else
      $log.writer.warn "Can not open etc/crypt.yaml, no decryption possible"
    end

    result.each do |key,value|
      if value.class.name == "Hash"
        value.each do |subkey,subvalue|
          if subvalue.class.name != "Fixnum" and \
              ((subvalue =~ /^\{[\w-]+\}.+/ ) != nil)
            result[key][subkey] = decrypt(password, subvalue)
          end
        end
      else
        if value.class.name != "Fixnum" and
            ((value =~ /^\{[\w-]+\}.+/ ) != nil)
          result[key] = decrypt(password, value)
        end
      end
    end

  rescue Exception => e
    $log.writer.error "Error: while talking to configuration database, giving up!"
    $log.writer.error "Request was: #{url.to_s}"
    $log.writer.error e.message
    exit 1
  end

  if result == "@@ERROR@@"
    $log.writer.error "Error while talking to configuration database, giving up!"
    $log.writer.error "Request was: #{url.to_s}"
    exit 1
  end

  result
end

#set_parameter(path, key, value) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/depengine/provider/cdb.rb', line 160

def set_parameter(path, key, value)
  result = "@@ERROR@@"

  Helper.validates_presence_of host
  Helper.validates_presence_of context
  Helper.validates_presence_of env
  Helper.validates_presence_of path
  Helper.validates_presence_of key
  Helper.validates_presence_of value

  begin
    json   = {key => value.to_s}.to_json
    url    = URI.parse(protocol + '://' + \
             File.join(host, context, env, path))
    header = {'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'}
    http   = Net::HTTP.new(url.host, url.port)
    result = http.request_post(url.path, 'json=' + json.to_s, header)
    $log.writer.debug "Writing #{key} with #{value} to cdb"
  rescue Exception => e
    $log.writer.error "Error: while connecting to configuration database"
    $log.writer.error "Request was: #{url.to_s}"
    $log.writer.error e.message
    exit 1
  end

  if result == "@@ERROR@@"
    $log.writer.error "Error while talking to configuration database, giving up!"
    $log.writer.error "Request was: #{url.to_s}"
    exit 1
  end

  result
end