Skip to content

Instantly share code, notes, and snippets.

@Splat
Last active November 21, 2016 16:37
Show Gist options
  • Save Splat/f61cebd1b67832c0978b269b89676ec5 to your computer and use it in GitHub Desktop.
Save Splat/f61cebd1b67832c0978b269b89676ec5 to your computer and use it in GitHub Desktop.
Loading certificate chain from a CA directory to use for validation to a certain depth.
require 'net/http'
require 'net/https'
require 'uri'
API_URI = 'some.site.com'
root_ca_path = '/etc/ssl/certs'
root_ca_depth = 5
api_user = 'username'
api_password = 'password'
url = URI.parse "https://#{API_URI}"
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
if (File.directory?(root_ca_path) && http.use_ssl?)
http.ca_path = RootCA
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.verify_depth = root_ca_depth
else
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(url.path)
request.basic_auth api_user, api_password
response = http.request(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment