-
Notifications
You must be signed in to change notification settings - Fork 47
Feature/pure ruby ssl implementation for root certificate issuer check #71
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5ab24cc
Add option to treat anchor as a regexp. Fix parsing of openssl client…
jspaleta 4d8627c
updates to make travis and rubocop happy
jspaleta 1d125b5
Add pure ruby implementation of check-ssl-root-issuer.rb as alternati…
jspaleta 1cf0f8a
make rubocop happy
jspaleta 9d7bf61
add test for check-ssl-root-issuer
jspaleta 13bb102
update changelog and README with new plugin information
jspaleta af1c8d6
changes from pr review
jspaleta e5f3dc1
remove files changed in PR #70, unrelated to this new feature
jspaleta e142b7f
make rubocop happy
jspaleta fc4e6cd
Merge branch 'master' into feature/pure_ruby_ssl
jspaleta 0f02083
Update logic for validating issuer name format options. Using mixin l…
jspaleta c03b86d
make rubocop happy.
jspaleta 5d2266a
clean up changelog
jspaleta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#! /usr/bin/env ruby | ||
# | ||
# check-ssl-root-issuer | ||
# | ||
# DESCRIPTION: | ||
# Check that a certificate is chained to a specific root certificate issuer | ||
# | ||
# OUTPUT: | ||
# plain text | ||
# | ||
# PLATFORMS: | ||
# Linux | ||
# | ||
# DEPENDENCIES: | ||
# gem: sensu-plugin | ||
# | ||
# USAGE: | ||
# | ||
# Check that a specific website is chained to a specific root certificate | ||
# ./check-ssl-root-issuer.rb \ | ||
# -u https://example.com \ | ||
# -i "CN=DST Root CA X3,O=Digital Signature Trust Co." | ||
# | ||
# LICENSE: | ||
# Copyright Jef Spaleta (jspaleta@gmail.com) 2020 | ||
# Released under the same terms as Sensu (the MIT license); see LICENSE | ||
# for details. | ||
# | ||
|
||
require 'sensu-plugin/check/cli' | ||
require 'openssl' | ||
require 'uri' | ||
require 'net/http' | ||
require 'net/https' | ||
|
||
# | ||
# Check root certificate has specified issuer name | ||
# | ||
class CheckSSLRootIssuer < Sensu::Plugin::Check::CLI | ||
option :url, | ||
description: 'Url to check: Ex "https://google.com"', | ||
short: '-u', | ||
long: '--url URL', | ||
required: true | ||
|
||
option :issuer, | ||
description: 'An X509 certificate issuer name, RFC2253 format Ex: "CN=DST Root CA X3,O=Digital Signature Trust Co."', | ||
short: '-i', | ||
long: '--issuer ISSUER_NAME', | ||
required: true | ||
|
||
option :regexp, | ||
description: 'Treat the issuer name as a regexp', | ||
short: '-r', | ||
long: '--regexp', | ||
default: false, | ||
boolean: true, | ||
required: false | ||
|
||
option :format, | ||
description: 'optional issuer name format. Defaults to RFC2253. Allowed values: RFC2253, ONELINE, COMPAT', | ||
short: '-f', | ||
long: '--format FORMAT_VAL', | ||
default: 'RFC2253', | ||
required: false | ||
|
||
def validate_opts | ||
config[:issuer_format] = OpenSSL::X509::Name::RFC2253 | ||
config[:issuer_format] = OpenSSL::X509::Name::ONELINE if config[:format] == 'ONELINE' | ||
config[:issuer_format] = OpenSSL::X509::Name::COMPAT if config[:format] == 'COMPAT' | ||
end | ||
|
||
def validate_issuer(cert) | ||
issuer = cert.issuer.to_s(config[:issuer_format]) | ||
if config[:regexp] | ||
issuer_regexp = Regexp.new(config[:issuer].to_s) | ||
issuer =~ issuer_regexp | ||
else | ||
issuer == config[:issuer].to_s | ||
end | ||
end | ||
|
||
def find_root_cert(uri) | ||
root_cert = nil | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.open_timeout = 10 | ||
http.read_timeout = 10 | ||
http.use_ssl = true | ||
http.cert_store = OpenSSL::X509::Store.new | ||
http.cert_store.set_default_paths | ||
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | ||
|
||
http.verify_callback = lambda { |verify_ok, store_context| | ||
root_cert = store_context.current_cert unless root_cert | ||
unless verify_ok | ||
@failed_cert = store_context.current_cert | ||
@failed_cert_reason = [store_context.error, store_context.error_string] if store_context.error != 0 | ||
end | ||
verify_ok | ||
} | ||
http.start {} | ||
root_cert | ||
end | ||
|
||
# Do the actual work and massage some data | ||
|
||
def run | ||
@fail_cert = nil | ||
@failed_cert_reason = 'Unknown' | ||
validate_opts | ||
uri = URI.parse(config[:url]) | ||
critical "url protocol must be https, you specified #{url}" if uri.scheme != 'https' | ||
root_cert = find_root_cert(uri) | ||
if @failed_cert | ||
msg = "Certificate verification failed.\n Reason: #{@failed_cert_reason}" | ||
critical msg | ||
end | ||
|
||
if validate_issuer(root_cert) | ||
msg = 'Root certificate in chain has expected issuer name' | ||
ok msg | ||
else | ||
msg = "Root certificate issuer did not match expected name.\nFound: \"#{root_cert.issuer.to_s(config[:issuer_format])}\"" | ||
critical msg | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require_relative '../bin/check-ssl-anchor.rb' | ||
|
||
describe CheckSSLRootIssuer do | ||
before(:all) do | ||
# Ensure the check isn't run when exiting (which is the default) | ||
CheckSSLRootIssuer.class_variable_set(:@@autorun, nil) | ||
end | ||
|
||
let(:check) do | ||
CheckSSLRootIssuer.new ['-u', 'https://philporada.com', '-i', '"CN=DST Root CA X3,O=Digital Signature Trust Co."'] | ||
end | ||
|
||
it 'should pass check if the root issuer matches what the users -i flag' do | ||
expect(check).to receive(:ok).and_raise SystemExit | ||
expect { check.run }.to raise_error SystemExit | ||
end | ||
|
||
it 'should pass check if the root issuer matches what the users -i flag' do | ||
check.config[:anchor] = 'testdata' | ||
check.config[:regexp] = false | ||
expect(check).to receive(:critical).and_raise SystemExit | ||
expect { check.run }.to raise_error SystemExit | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple thoughts:
RFC2253
regardless of what is passed inMaybe that should be something we pass in and can try something like this? (not sure if this is valid)
if we cant specify it like that then we could always use if, elif, else or case statements to handle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I've updated the logic to use mixin's cli option value validation method.