Commit 8583c8d7 by John Donnal

added url verfication to catch localhost IP addresses showing up in a…

added url verfication to catch localhost IP addresses showing up in a containerized version of Lumen
parent 3ca1ebf2
......@@ -108,7 +108,7 @@ module Joule
# === BEGIN EVENTS ===
def read_events(stream, max_events, start_time, end_time, filter)
result = @backend.read_events(stream.joule_id, max_events, start_time, end_time, filter)
{id: stream.id, valid: true, count: result[:count], events: result[:events]}
{id: stream.id, valid: true, count: result[:count], events: result[:events], type: result[:type]}
end
def node_type
......
......@@ -4,6 +4,7 @@ require 'resolv'
class AddNilmByKey
include ServiceStatus
include VerifyUrl
attr_reader :nilm
def run(request_params, remote_ip)
......@@ -43,6 +44,8 @@ class AddNilmByKey
url.port = joule_params[:port]
url.scheme = joule_params[:scheme]
url.path = joule_params[:base_uri]
# check to see if this is a valid URL for Joule
url = verify_url(url,request_params[:api_key])
#3 Create the Nilm
adapter = Joule::Adapter.new(url, joule_params[:api_key])
service = CreateNilm.new(adapter)
......
......@@ -4,6 +4,7 @@ require 'resolv'
class AddNilmByUser
include ServiceStatus
include VerifyUrl
attr_reader :nilm
def run(request_params, remote_ip)
......@@ -46,6 +47,8 @@ class AddNilmByUser
url.port = request_params[:port]
url.scheme = request_params[:scheme]
url.path = request_params[:base_uri]
# check to see if this is a valid URL for Joule
url = verify_url(url,request_params[:api_key])
#3 Create the Nilm
adapter = Joule::Adapter.new(url, request_params[:api_key])
service = CreateNilm.new(adapter)
......
module VerifyUrl
def verify_url(orig_url, key)
url = orig_url.dup
# only verify when the host is 127.0.0.1
if orig_url.host!='127.0.0.1'
return orig_url
end
begin
resp = HTTParty.get(url, verify: false,
headers: {'X-API-KEY': key})
if resp.parsed_response.downcase == 'joule server'
return orig_url # orig url works, no need to change it
end
rescue StandardError # ignore exceptions
end
# orig_url doesn't work, check to see if the Docker IP address works instead
url.host='172.17.0.1'
begin
resp = HTTParty.get(url, verify: false,
headers: {'X-API-KEY': key})
# successful modification, return the new url
return url if resp.parsed_response.downcase == 'joule server'
rescue StandardError #ignore exceptions
end
return orig_url # unsuccessful modification, return the original url
end
end
\ No newline at end of file
......@@ -2,6 +2,7 @@ json.data do
json.array! @service.data.each do |event_stream|
json.id event_stream[:id]
json.valid event_stream[:valid]
json.type event_stream[:type]
json.count event_stream[:count]
json.events event_stream[:events]
json.tag event_stream[:tag]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment