Skip to content

Commit 1a9015d

Browse files
authored
Merge pull request #8 from MSP-Greg/ruby-desc
RUBY_DESCRIPTION, minor formatting, width
2 parents 472640b + 1e84796 commit 1a9015d

File tree

1 file changed

+88
-51
lines changed

1 file changed

+88
-51
lines changed

check.rb

+88-51
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
begin
2424
require 'openssl'
2525
rescue LoadError
26-
abort "Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to #{host}. " \
27-
"You'll need to recompile or reinstall Ruby with OpenSSL support and try again."
26+
puts "Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to #{host}.",
27+
"You'll need to recompile or reinstall Ruby with OpenSSL support and try again."
28+
exit 1
2829
end
2930

3031
begin
@@ -39,27 +40,48 @@
3940
end
4041

4142
uri = URI("https://#{host}")
42-
ssl_version = ARGV.shift
43+
tls_version = ARGV.shift
4344
verify_mode = ARGV.any? ? OpenSSL::SSL.const_get(ARGV.shift) : OpenSSL::SSL::VERIFY_PEER
4445

45-
ruby_version = RUBY_VERSION.dup
46-
ruby_version << "p#{RUBY_PATCHLEVEL}" if defined?(RUBY_PATCHLEVEL)
47-
ruby_version << " (#{RUBY_RELEASE_DATE} revision #{RUBY_REVISION})"
48-
ruby_version << " [#{RUBY_PLATFORM}]"
46+
if defined?(RUBY_DESCRIPTION)
47+
ruby_version = RUBY_DESCRIPTION
48+
else
49+
ruby_version = RUBY_VERSION.dup
50+
ruby_version << "p#{RUBY_PATCHLEVEL}" if defined?(RUBY_PATCHLEVEL)
51+
ruby_version << " (#{RUBY_RELEASE_DATE} revision #{RUBY_REVISION})"
52+
ruby_version << " [#{RUBY_PLATFORM}]"
53+
end
4954

50-
puts "Here's your Ruby and OpenSSL environment:"
51-
puts
52-
puts "Ruby: %s" % ruby_version
53-
puts "RubyGems: %s" % Gem::VERSION if defined?(Gem::VERSION)
54-
puts "Bundler: %s" % Bundler::VERSION if defined?(Bundler::VERSION)
55-
puts "Compiled with: %s" % OpenSSL::OPENSSL_VERSION
56-
puts "Loaded version: %s" % OpenSSL::OPENSSL_LIBRARY_VERSION if defined?(OpenSSL::OPENSSL_LIBRARY_VERSION)
57-
puts "SSL_CERT_FILE: %s" % OpenSSL::X509::DEFAULT_CERT_FILE
58-
puts "SSL_CERT_DIR: %s" % OpenSSL::X509::DEFAULT_CERT_DIR
55+
puts "", "Here's your Ruby and OpenSSL environment:"
5956
puts
60-
puts "With that out of the way, let's see if you can connect to #{host}..."
57+
puts "Ruby: %s" % ruby_version
58+
puts "RubyGems: %s" % Gem::VERSION if defined?(Gem::VERSION)
59+
puts "Bundler: %s" % Bundler::VERSION if defined?(Bundler::VERSION)
60+
puts "OpenSSL: %s" % OpenSSL::VERSION if defined?(OpenSSL::VERSION)
61+
puts "Compiled with: %s" % OpenSSL::OPENSSL_VERSION
62+
puts "Loaded with: %s" % OpenSSL::OPENSSL_LIBRARY_VERSION if defined?(OpenSSL::OPENSSL_LIBRARY_VERSION)
6163
puts
6264

65+
def show_ssl_certs
66+
puts "", "Below affect only Ruby net/http connections:"
67+
puts
68+
t = ENV['SSL_CERT_FILE'] || OpenSSL::X509::DEFAULT_CERT_FILE
69+
ssl_file = if Dir.exist? t
70+
"✅ exists #{t}"
71+
elsif RUBY_PLATFORM.end_with? 'linux'
72+
t = '/etc/ssl/certs/ca-certificates.crt'
73+
Dir.exist?(t) ? "✅ exists #{t}" : "❌ is missing #{t}"
74+
else
75+
"❌ is missing #{t}"
76+
end
77+
puts "SSL_CERT_FILE: %s" % ssl_file
78+
79+
t = ENV['SSL_CERT_DIR'] || OpenSSL::X509::DEFAULT_CERT_DIR
80+
ssl_dir = Dir.exist?(t) ? "✅ exists #{t}" : "❌ is missing #{t}"
81+
puts "SSL_CERT_DIR: %s" % ssl_dir
82+
puts
83+
end
84+
6385
def error_reason(error)
6486
case error.message
6587
when /certificate verify failed/
@@ -73,85 +95,101 @@ def error_reason(error)
7395
end
7496
end
7597

98+
puts "Trying connections to #{uri.to_s}:"
99+
puts
76100
begin
77101
b_uri = defined?(Bundler::URI) ? Bundler::URI(uri.to_s) : uri
78102
Bundler::Fetcher.new(Bundler::Source::Rubygems::Remote.new(b_uri)).send(:connection).request(b_uri)
79-
bundler_status = "success ✅"
103+
bundler_status = "✅ success"
80104
rescue => error
81-
bundler_status = "failed (#{error_reason(error)})"
105+
bundler_status = "failed (#{error_reason(error)})"
82106
end
83-
puts "Bundler connection to #{host}: #{bundler_status}"
107+
puts "Bundler: #{bundler_status}"
84108

85109
begin
86110
require 'rubygems/remote_fetcher'
87111
Gem::RemoteFetcher.fetcher.fetch_path(uri)
88-
rubygems_status = "success ✅"
112+
rubygems_status = "✅ success"
89113
rescue => error
90-
rubygems_status = "failed (#{error_reason(error)})"
114+
rubygems_status = "failed (#{error_reason(error)})"
91115
end
92-
puts "RubyGems connection to #{host}: #{rubygems_status}"
116+
puts "RubyGems: #{rubygems_status}"
93117

94118
begin
95119
# Try to connect using HTTPS
96120
Net::HTTP.new(uri.host, uri.port).tap do |http|
97121
http.use_ssl = true
98-
if ssl_version
122+
if tls_version
99123
if http.respond_to? :min_version=
100-
vers = ssl_version.sub("v", "").to_sym
124+
vers = tls_version.sub("v", "").to_sym
101125
http.min_version = vers
102126
http.max_version = vers
103127
else
104-
http.ssl_version = ssl_version.to_sym
128+
http.ssl_version = tls_version.to_sym
105129
end
106130
end
107131
http.verify_mode = verify_mode
108132
end.start
109133

110-
puts "Ruby net/http connection to #{host}: success ✅"
134+
puts "Ruby net/http: ✅ success"
111135
puts
112136
rescue => error
113-
puts "Ruby net/http connection to #{host}: failed ❌"
137+
puts "Ruby net/http: ❌ failed"
114138
puts
115139
puts "Unfortunately, this Ruby can't connect to #{host}. 😡"
116140

117141
case error.message
118142
# Check for certificate errors
119143
when /certificate verify failed/
120-
abort "Your Ruby can't connect to #{host} because you are missing the certificate " \
121-
"files OpenSSL needs to verify you are connecting to the genuine #{host} servers."
144+
show_ssl_certs
145+
puts "\nYour Ruby can't connect to #{host} because you are missing the certificate",
146+
"files OpenSSL needs to verify you are connecting to the genuine #{host} servers.", ""
122147
# Check for TLS version errors
123148
when /read server hello A/, /tlsv1 alert protocol version/
124-
if ssl_version == "TLSv1_3"
125-
abort "Your Ruby can't connect to #{host} because #{ssl_version} isn't supported yet."
149+
if tls_version == "TLSv1_3"
150+
puts "\nYour Ruby can't connect to #{host} because #{tls_version} isn't supported yet.\n\n"
126151
else
127-
abort "Your Ruby can't connect to #{host} because your version of OpenSSL is too old. " \
128-
"You'll need to upgrade your OpenSSL install and/or recompile Ruby to use a newer OpenSSL."
152+
puts "\nYour Ruby can't connect to #{host} because your version of OpenSSL is too old.",
153+
"You'll need to upgrade your OpenSSL install and/or recompile Ruby to use a newer OpenSSL.", ""
129154
end
155+
# OpenSSL doesn't support TLS version specified by argument
156+
when /unknown SSL method/
157+
puts "\nYour Ruby can't connect because #{tls_version} isn't supported by your version of OpenSSL.\n\n"
130158
else
131-
puts "Even worse, we're not sure why. 😕"
159+
puts "\nEven worse, we're not sure why. 😕"
132160
puts
133-
puts "Here's the full error information:"
134-
puts "#{error.class}: #{error.message}"
135-
puts " " << error.backtrace.join("\n ")
161+
puts "Here's the full error information:",
162+
"#{error.class}: #{error.message}",
163+
" #{error.backtrace.join("\n ")}"
136164
puts
137-
puts "You might have more luck using Mislav's SSL doctor.rb script. You can get it here:"
138-
puts "https://github.com/mislav/ssl-tools/blob/8b3dec4/doctor.rb"
139-
puts "Read more about the script and how to use it in this blog post:"
140-
puts "https://mislav.net/2013/07/ruby-openssl/"
141-
abort
165+
puts "You might have more luck using Mislav's SSL doctor.rb script. You can get it here:",
166+
"https://github.com/mislav/ssl-tools/blob/8b3dec4/doctor.rb",
167+
"Read more about the script and how to use it in this blog post:",
168+
"https://mislav.net/2013/07/ruby-openssl/", ""
142169
end
170+
exit 1
143171
end
144172

145173
guide_url = "http://ruby.to/ssl-check-failed"
146174
if bundler_status =~ /success/ && rubygems_status =~ /success/
147175
# Whoa, it seems like it's working!
148-
puts "Hooray! This Ruby can connect to #{host}. You are all set to use Bundler and RubyGems. 👌"
176+
puts "Hooray! This Ruby can connect to #{host}.",
177+
"You are all set to use Bundler and RubyGems. 👌", ""
149178
elsif rubygems_status !~ /success/
150-
puts "It looks like Ruby and Bundler can connect to #{host}, but RubyGems itself cannot. You can likely solve this by manually downloading and installing a RubyGems update. Visit #{guide_url} for instructions on how to manually upgrade RubyGems. 💎"
179+
puts "It looks like Ruby and Bundler can connect to #{host}, but RubyGems itself",
180+
"cannot. You can likely solve this by manually downloading and installing a",
181+
"RubyGems update. Visit #{guide_url} for instructions on how to manually upgrade RubyGems. 💎"
151182
elsif bundler_status !~ /success/
152-
puts "Although your Ruby installation and RubyGems can both connect to #{host}, Bundler is having trouble. The most likely way to fix this is to upgrade Bundler by running `gem install bundler`. Run this script again after doing that to make sure everything is all set. If you're still having trouble, check out the troubleshooting guide at #{guide_url} 📦"
183+
puts "Although your Ruby installation and RubyGems can both connect to #{host},",
184+
"Bundler is having trouble. The most likely way to fix this is to upgrade",
185+
"Bundler by running `gem install bundler`. Run this script again after doing",
186+
"that to make sure everything is all set. If you're still having trouble,",
187+
"check out the troubleshooting guide at #{guide_url} 📦"
153188
else
154-
puts "For some reason, your Ruby installation can connect to #{host}, but neither RubyGems nor Bundler can. The most likely fix is to manually upgrade RubyGems by following the instructions at #{guide_url}. After you've done that, run `gem install bundler` to upgrade Bundler, and then run this script again to make sure everything worked. ❣️"
189+
puts "For some reason, your Ruby installation can connect to #{host}, but neither",
190+
"RubyGems nor Bundler can. The most likely fix is to manually upgrade RubyGems by",
191+
"following the instructions at #{guide_url}. After you've done that, run `gem install",
192+
"bundler` to upgrade Bundler, and then run this script again to make sure everything worked. ❣️"
155193
end
156194

157195
def tls12_supported?
@@ -167,10 +205,9 @@ def tls12_supported?
167205

168206
# We were able to connect, but perhaps this Ruby will have trouble when we require TLSv1.2
169207
unless tls12_supported?
170-
puts
171-
puts "WARNING: Although your Ruby can connect to #{host} today, your OpenSSL is very old! 👴"
172-
puts "WARNING: You will need to upgrade OpenSSL before January 2018 in order to keep using #{host}."
173-
abort
208+
puts "\nWARNING: Although your Ruby can connect to #{host} today, your OpenSSL is very old! 👴",
209+
"WARNING: You will need to upgrade OpenSSL to use #{host}."
210+
exit 1
174211
end
175212

176213
exit 0

0 commit comments

Comments
 (0)