Skip to content

Instantly share code, notes, and snippets.

@plexus
Created May 11, 2012 13:29
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/2659619 to your computer and use it in GitHub Desktop.
Save plexus/2659619 to your computer and use it in GitHub Desktop.
Monkey patch Ruby 1.9 net/http to read system's root certificates
# Please fork if you can improve this. (e.g. Windows support)
#
# Ruby 1.9 doesn't contain any SSL root certificates, neither does it read the ones
# installed with your operating system. This results in an error like
#
# SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
#
# This solution is based on http://martinottenwaelter.fr/2010/12/ruby19-and-the-ssl-error/
# but can be used to monkey patch 3rd party tools, e.g. Github's 'gist' command.
#
# It should work on Debian/Ubuntu Linux and Mac OS X.
require 'net/https'
class Net::HTTP
alias orig_initialize initialize
def initialize(*args,&blk)
orig_initialize(*args,&blk)
self.ca_path = '/etc/ssl/certs' if File.exists?('/etc/ssl/certs') # Ubuntu
self.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt') # Mac OS X
end
end
@keydrop-teamcity
Copy link

Thanks for this solution. This is the closest to what we needed amongst all the monkeypatch-nethttp solutions.

@clarkewd
Copy link

very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment