Ruby和OpenSSL证书验证失败

时间:2014-08-20 02:43:20

标签: ruby ssl openssl emc

我很确定我已经在阳光下尝试了一切。这是我正在尝试运行的代码

require 'rubygems'
require 'rest-client'
require 'json'

def vipr_session(viprurl, username, password)
  vipr_session_link = RestClient::Resource.new(viprurl + '/login', username, password)
  vipr_session_response = vipr_session_link.get
  myvar = 'x_sds_auth_token'
  @mysession = vipr_session_link.headers[myvar.to_sym]
end

@username = 'root'
@password = 'mypw'
@viprurl = 'https://192.168.50.141:4443'

print " Logging into ViPR..."
  vipr_session(@viprurl, @username, @password)
print "Success! \n\n\n"
puts @mysession
storagesystems = JSON.parse(RestClient.get(@viprurl + '/vdc/storage-systems', :x_sds_auth_token => @mysession, :content_type => :json, :accept => :json))
puts storagesystems

这是错误

kcoleman-mbp:vipr_scripts kcoleman$ ruby storage_systems.rb
 Logging into ViPR.../Users/kcoleman/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:445:in `rescue in transmit': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (RestClient::SSLCertificateNotVerified)
    from /Users/kcoleman/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:350:in `transmit'
    from /Users/kcoleman/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:176:in `execute'
    from /Users/kcoleman/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/request.rb:41:in `execute'
    from /Users/kcoleman/.rvm/gems/ruby-2.1.2/gems/rest-client-1.7.2/lib/restclient/resource.rb:51:in `get'
    from storage_systems.rb:8:in `vipr_session'
    from storage_systems.rb:18:in `<main>'

我可以通过在RestClient中设置 verify_ssl:false 来完成这项工作,但此代码以前一直在运行,而且突然间它已经不再存在了。

以下是我尝试修复的内容:

rvm osx-ssl-certs update all
brew install openssl
brew link openssl --force
brew tap raggi/ale
brew install openssl-osx-ca
rvm pkg install openssl
curl http://curl.haxx.se/ca/cacert.pem -o /usr/local/etc/openssl/cert.pem

以下是我目前的配置

kcoleman$ which openssl
/usr/local/bin/openssl
kcoleman$ openssl version
OpenSSL 1.0.1i 6 Aug 2014

我遇到了这篇文章SSLError and Rubyist, sitting in a tree,这是doctor.rb脚本的输出。

kcoleman-mbp:ssl-tools kcoleman$ ruby doctor.rb 192.168.50.141:4443
/Users/kcoleman/.rvm/rubies/ruby-2.1.2/bin/ruby (2.1.2-p95)
OpenSSL 1.0.1g 7 Apr 2014: /etc/openssl
SSL_CERT_DIR=""
SSL_CERT_FILE=""

HEAD https://192.168.50.141:4443
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

The server presented a certificate that could not be verified:
  subject: /CN=192.168.50.140
  issuer: /CN=192.168.50.140
  error code 18: self signed certificate

看起来ruby正在使用与RVM打包在一起的不同版本的OpenSSL。通过在rvm上运行openssl install来验证。

kcoleman-mbp:vipr_scripts kcoleman$ rvm pkg install openssl

Beware, 'rvm pkg ...' is deprecated, read about the new autolibs feature: 'rvm help autolibs'.

Checking requirements for osx.
Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
Requirements installation successful.
Fetching openssl-1.0.1g.tar.gz to /Users/kcoleman/.rvm/archives
Extracting openssl to /Users/kcoleman/.rvm/src/openssl-1.0.1g....
Configuring openssl in /Users/kcoleman/.rvm/src/openssl-1.0.1g.......................
Compiling openssl in /Users/kcoleman/.rvm/src/openssl-1.0.1g...........................................................................................-
Installing openssl to /Users/kcoleman/.rvm/usr

如果您对尝试什么有任何想法,我们非常感激。

1 个答案:

答案 0 :(得分:2)

HEAD https://192.168.50.141:4443
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

The server presented a certificate that could not be verified:
  subject: /CN=192.168.50.140
  issuer: /CN=192.168.50.140
  error code 18: self signed certificate

您的证书有几个问题导致验证失败:

  • 证书是自签名的,因此无法针对本地信任锚进行检查。接受这样的证书相当于接受某人为自己创建的护照,而不仅仅是受信任政府签发的护照。
  • 证书主题与您用于连接它的名称不匹配。证书适用于192.168.50.140,但您可以访问主机192.168.50.141(可能仍然是证书中有更多IP作为主题备用名称,此处未显示)。不检查证书中的名称相当于不检查护照中的照片与护照的主持人。