Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSL configuration and authentication for the HTTP check #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/easymon/checks/http_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module Easymon
class HttpCheck
attr_accessor :url

def initialize(url)
self.url = url
end
end

def check
check_status = http_up?(url)
if check_status
Expand All @@ -17,7 +17,7 @@ def check
end
[check_status, message]
end

private
def http_up?(url)
http_head(url).is_a?(Net::HTTPSuccess)
Expand All @@ -27,14 +27,18 @@ def http_up?(url)

def http_head(url)
uri = URI.parse(url)
is_https = uri.is_a?(URI::HTTPS)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.is_a?(URI::HTTPS)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = is_https
http.verify_mode = is_https ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
http.open_timeout = 5
http.read_timeout = 5

http.request Net::HTTP::Head.new(uri.request_uri)
head = Net::HTTP::Head.new(uri.request_uri)
head.basic_auth(uri.user, uri.password) unless uri.userinfo.nil?

http.request head
end
end
end
18 changes: 18 additions & 0 deletions test/unit/checks/http_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ class HttpCheckTest < ActiveSupport::TestCase
assert_equal(false, results[0])
end

test "uses the right ssl configuration" do
Net::HTTP.any_instance.expects(:use_ssl=).with(true)
Net::HTTP.any_instance.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
Easymon::HttpCheck.new("https://localhost:9200").check

Net::HTTP.any_instance.expects(:use_ssl=).with(false)
Net::HTTP.any_instance.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
Easymon::HttpCheck.new("http://localhost:9200").check
end

test "sends request with basic auth if present" do
Net::HTTP::Head.any_instance.expects(:basic_auth).with("user", "password")
Easymon::HttpCheck.new("https://user:password@localhost:9200").check

Net::HTTP::Head.any_instance.expects(:basic_auth).never
Easymon::HttpCheck.new("https://localhost:9200").check
end

private
def create_check
# Fake URL
Expand Down