Skip to content

Commit

Permalink
Merge pull request #25 from chooper/fix24-check-host-match
Browse files Browse the repository at this point in the history
Fix24 check host match
  • Loading branch information
Charles‮Hooper committed Sep 5, 2014
2 parents 5195e45 + 43dcd22 commit a18b163
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/elbping/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def self.main
break if not run

status = ElbPing::HttpPinger.ping_node(node,
elb_uri.host,
elb_uri.port,
(elb_uri.path == "") ? "/" : elb_uri.path,
(elb_uri.scheme == 'https'),
Expand Down
3 changes: 2 additions & 1 deletion lib/elbping/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ def self.response(status)
exc = status[:exception]
sslSubject = status[:sslSubject].join(',') if status[:sslSubject]
sslExpires = status[:sslExpires]
sslHostMatch = status[:sslHostMatch]

exc_display = exc ? "exception=#{exc}" : ''
ssl_display = (sslSubject and sslExpires) ? "ssl cn=#{sslSubject} expires=#{sslExpires}" : ''
ssl_display = (sslSubject and sslExpires) ? "ssl cn=#{sslSubject} match=#{sslHostMatch} expires=#{sslExpires}" : ''

self.out "Response from: #{node.rjust(15)}: code=#{code.to_s} time=#{duration}ms #{ssl_display} #{exc_display}"
end
Expand Down
20 changes: 17 additions & 3 deletions lib/elbping/pinger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@ def self.cert_name(x509_subject)
cn_bucket
end

# Check if a given host matches a cert's pattern
#
# Arguments:
# * cert: (object) of X.509 certificate
# * host: (string) of a hostname to compare

def self.cert_matches?(cert, host)
File.fnmatch(cert_name(cert.subject).first, host)
end

# Make HTTP request to given node using custom request method and measure response time
#
# Arguments:
# * node: (string) of node IP
# * host: (string) of hostname, used for checking SSL cert match
# * port: (string || Fixnum) of positive integer [1, 65535]
# * path: (string) of path to request, e.g. "/"
# * use_ssl: (boolean) Whether or not this is HTTPS
# * verb_len: (Fixnum) of positive integer, how long the custom HTTP verb should be
# * timeout: (Fixnum) of positive integer, how many _seconds_ for connect and read timeouts

def self.ping_node(node, port, path, use_ssl, verb_len, timeout)
def self.ping_node(node, host, port, path, use_ssl, verb_len, timeout)
##
# Build request class
ping_request = Class.new(Net::HTTPRequest) do
Expand Down Expand Up @@ -86,8 +97,11 @@ def self.ping_node(node, port, path, use_ssl, verb_len, timeout)
ssl_status = {}
if use_ssl
raise "No cert when SSL enabled?!" unless cert
ssl_status = {:sslSubject => cert_name(cert.subject),
:sslExpires => cert.not_after}
ssl_status = {
:sslSubject => cert_name(cert.subject),
:sslExpires => cert.not_after,
:sslHostMatch => cert_matches?(cert, host)
}
end

{:code => error || response.code,
Expand Down
21 changes: 21 additions & 0 deletions test/test_pinger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'elbping/pinger.rb'

DEFAULT_NODE = ENV['TEST_NODE'] || '127.0.0.1'
DEFAULT_HOST = ENV['TEST_HOST'] || 'localhost'
DEFAULT_PORT = ENV['TEST_PORT'] || '80'
DEFAULT_PATH = ENV['TEST_PATH'] || '/'
DEFAULT_SSL = ENV['TEST_SSL'] || false
Expand All @@ -16,6 +17,7 @@ def test_ping_node
assert_nothing_raised do
resp = ElbPing::HttpPinger.ping_node(
DEFAULT_NODE,
DEFAULT_HOST,
DEFAULT_PORT,
DEFAULT_PATH,
DEFAULT_SSL,
Expand All @@ -33,3 +35,22 @@ def test_ping_node
end
end

require 'openssl'
class TestCertMatches
def test_wildcard
cert = OpenSSL::X509::Certificate.new
cert.subject = OpenSSL::X509::Name.parse "/CN=*.example.com"

assert ElbPing::HttpPinger.cert_matches?(cert, "www.example.com")
assert_false ElbPing::HttpPinger.cert_matches?(cert, "www.example.org")
end

def test_static
cert = OpenSSL::X509::Certificate.new
cert.subject = OpenSSL::X509::Name.parse "/CN=www.example.com"

assert ElbPing::HttpPinger.cert_matches?(cert, "www.example.com")
assert_false ElbPing::HttpPinger.cert_matches?(cert, "www.example.org")
end
end

0 comments on commit a18b163

Please sign in to comment.