require 'patron' require 'net/http' require 'json' require 'base64' class Registry def getKeyValue (key) uri = URI.parse(ENV["CONSUL_URL"] + "/v1/kv/#{key}") http = Net::HTTP.new(uri.host, uri.port) if (Canzea::config[:consul_tls]) pemCert = File.read(Canzea::config[:consul_tls_cert_file]) pemKey = File.read(Canzea::config[:consul_tls_key_file]) http.use_ssl = true http.ca_file = Canzea::config[:consul_tls_ca_file] http.cert = OpenSSL::X509::Certificate.new(pemCert) http.key = OpenSSL::PKey::RSA.new(pemKey) http.verify_mode = OpenSSL::SSL::VERIFY_PEER # http.set_debug_output($stdout) http.ssl_version = :SSLv23 end request = Net::HTTP::Get.new(uri.request_uri) resp = http.request(request) if Integer(resp.code) != 200 puts "-- KEY VALUE NOT FOUND! " + key abort("Problem, response code #{resp.code}") end result = JSON.parse(resp.body) return Base64.decode64(result[0]["Value"]) end def setKeyValue (root, key, value) uri = URI.parse(ENV["CONSUL_URL"] + "/v1/kv/#{root}/#{key}") http = Net::HTTP.new(uri.host, uri.port) if (Canzea::config[:consul_tls]) pemCert = File.read(Canzea::config[:consul_tls_cert_file]) pemKey = File.read(Canzea::config[:consul_tls_key_file]) http.use_ssl = true http.ca_file = Canzea::config[:consul_tls_ca_file] http.cert = OpenSSL::X509::Certificate.new(pemCert) http.key = OpenSSL::PKey::RSA.new(pemKey) http.verify_mode = OpenSSL::SSL::VERIFY_PEER # http.set_debug_output($stdout) http.ssl_version = :SSLv23 end request = Net::HTTP::Put.new(uri.request_uri) request.body = value; resp = http.request(request) if Integer(resp.code) != 200 puts "-- KEY VALUE NOT SAVED! #{root} #{key}" abort("Problem, response code #{resp.code}") end result = JSON.parse(resp.body) return Base64.decode64(result[0]["Value"]) end def getValue (root, key) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' # patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end #sess.enable_debug resp = sess.get("/#{root}/#{key}") # puts resp.body if resp.status != 200 abort("problem #{resp.status_line}") end result = JSON.parse(resp.body) return Base64.decode64(result[0]["Value"]) end def exists (root) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end # sess.enable_debug resp = sess.get("/#{root}") puts resp.body if resp.status == 200 abort("problem #{resp.status_line}") end end def deleteDirectory (root) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end # sess.enable_debug resp = sess.delete("/#{root}?&recurse=true") puts resp.body if resp.status != 200 abort("problem #{resp.status_line}") end end def registerDirectory (root) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end # sess.enable_debug resp = sess.put("/#{root}?dir=true", "") puts resp.body if resp.status > 202 abort("problem #{resp.status_line}") end end def register (root, key, value) sess = Patron::Session.new do |patron| patron.timeout = 10 patron.base_url = ENV["CONSUL_URL"] + '/v1/kv' patron.headers = {'User-Agent' => 'IKE-Registry/1.0'} end #sess.enable_debug resp = sess.put("/#{root}/#{key}", "#{value}") puts resp.body if resp.status > 202 abort("problem #{resp.status_line}") end end end