Skip to content

Commit 1e84796

Browse files
committed
Check ENV for ssl file & dir info, formatting
1. Remove abort, as console output is sometimes scrambled in CI. 2. Only show OpenSSL cert location info if net/http failure, as it often contains user name. 3. Misc formatting re output width.
1 parent e7cc891 commit 1e84796

File tree

1 file changed

+69
-46
lines changed

1 file changed

+69
-46
lines changed

check.rb

+69-46
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
begin
1717
require 'openssl'
1818
rescue LoadError
19-
abort "Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to #{host}. " \
20-
"You'll need to recompile or reinstall Ruby with OpenSSL support and try again."
19+
puts "Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to #{host}.",
20+
"You'll need to recompile or reinstall Ruby with OpenSSL support and try again."
21+
exit 1
2122
end
2223

2324
begin
@@ -32,7 +33,7 @@
3233
end
3334

3435
uri = URI("https://#{host}")
35-
ssl_version = ARGV.shift
36+
tls_version = ARGV.shift
3637
verify_mode = ARGV.any? ? OpenSSL::SSL.const_get(ARGV.shift) : OpenSSL::SSL::VERIFY_PEER
3738

3839
if defined?(RUBY_DESCRIPTION)
@@ -46,17 +47,34 @@
4647

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

58+
def show_ssl_certs
59+
puts "", "Below affect only Ruby net/http connections:"
60+
puts
61+
t = ENV['SSL_CERT_FILE'] || OpenSSL::X509::DEFAULT_CERT_FILE
62+
ssl_file = if Dir.exist? t
63+
"✅ exists #{t}"
64+
elsif RUBY_PLATFORM.end_with? 'linux'
65+
t = '/etc/ssl/certs/ca-certificates.crt'
66+
Dir.exist?(t) ? "✅ exists #{t}" : "❌ is missing #{t}"
67+
else
68+
"❌ is missing #{t}"
69+
end
70+
puts "SSL_CERT_FILE: %s" % ssl_file
71+
72+
t = ENV['SSL_CERT_DIR'] || OpenSSL::X509::DEFAULT_CERT_DIR
73+
ssl_dir = Dir.exist?(t) ? "✅ exists #{t}" : "❌ is missing #{t}"
74+
puts "SSL_CERT_DIR: %s" % ssl_dir
75+
puts
76+
end
77+
6078
def error_reason(error)
6179
case error.message
6280
when /certificate verify failed/
@@ -70,80 +88,86 @@ def error_reason(error)
7088
end
7189
end
7290

91+
puts "Trying connections to #{uri.to_s}:"
92+
puts
7393
begin
7494
b_uri = defined?(Bundler::URI) ? Bundler::URI(uri.to_s) : uri
7595
Bundler::Fetcher.new(Bundler::Source::Rubygems::Remote.new(b_uri)).send(:connection).request(b_uri)
76-
bundler_status = "success ✅"
96+
bundler_status = "✅ success"
7797
rescue => error
78-
bundler_status = "failed (#{error_reason(error)})"
98+
bundler_status = "failed (#{error_reason(error)})"
7999
end
80-
puts "Bundler connection to #{host}: #{bundler_status}"
100+
puts "Bundler: #{bundler_status}"
81101

82102
begin
83103
require 'rubygems/remote_fetcher'
84104
Gem::RemoteFetcher.fetcher.fetch_path(uri)
85-
rubygems_status = "success ✅"
105+
rubygems_status = "✅ success"
86106
rescue => error
87-
rubygems_status = "failed (#{error_reason(error)})"
107+
rubygems_status = "failed (#{error_reason(error)})"
88108
end
89-
puts "RubyGems connection to #{host}: #{rubygems_status}"
109+
puts "RubyGems: #{rubygems_status}"
90110

91111
begin
92112
# Try to connect using HTTPS
93113
Net::HTTP.new(uri.host, uri.port).tap do |http|
94114
http.use_ssl = true
95-
if ssl_version
115+
if tls_version
96116
if http.respond_to? :min_version=
97-
vers = ssl_version.sub("v", "").to_sym
117+
vers = tls_version.sub("v", "").to_sym
98118
http.min_version = vers
99119
http.max_version = vers
100120
else
101-
http.ssl_version = ssl_version.to_sym
121+
http.ssl_version = tls_version.to_sym
102122
end
103123
end
104124
http.verify_mode = verify_mode
105125
end.start
106126

107-
puts "Ruby net/http connection to #{host}: success ✅"
127+
puts "Ruby net/http: ✅ success"
108128
puts
109129
rescue => error
110-
puts "Ruby net/http connection to #{host}: failed ❌"
130+
puts "Ruby net/http: ❌ failed"
111131
puts
112132
puts "Unfortunately, this Ruby can't connect to #{host}. 😡"
113133

114134
case error.message
115135
# Check for certificate errors
116136
when /certificate verify failed/
117-
abort "Your Ruby can't connect to #{host} because you are missing the certificate\n" \
118-
"files OpenSSL needs to verify you are connecting to the genuine #{host} servers."
137+
show_ssl_certs
138+
puts "\nYour Ruby can't connect to #{host} because you are missing the certificate",
139+
"files OpenSSL needs to verify you are connecting to the genuine #{host} servers.", ""
119140
# Check for TLS version errors
120141
when /read server hello A/, /tlsv1 alert protocol version/
121-
if ssl_version == "TLSv1_3"
122-
abort "Your Ruby can't connect to #{host} because #{ssl_version} isn't supported yet."
142+
if tls_version == "TLSv1_3"
143+
puts "\nYour Ruby can't connect to #{host} because #{tls_version} isn't supported yet.\n\n"
123144
else
124-
abort "Your Ruby can't connect to #{host} because your version of OpenSSL is too old.\n" \
125-
"You'll need to upgrade your OpenSSL install and/or recompile Ruby to use a newer OpenSSL."
145+
puts "\nYour Ruby can't connect to #{host} because your version of OpenSSL is too old.",
146+
"You'll need to upgrade your OpenSSL install and/or recompile Ruby to use a newer OpenSSL.", ""
126147
end
148+
# OpenSSL doesn't support TLS version specified by argument
149+
when /unknown SSL method/
150+
puts "\nYour Ruby can't connect because #{tls_version} isn't supported by your version of OpenSSL.\n\n"
127151
else
128-
puts "Even worse, we're not sure why. 😕"
152+
puts "\nEven worse, we're not sure why. 😕"
129153
puts
130-
puts "Here's the full error information:"
131-
puts "#{error.class}: #{error.message}"
132-
puts " " << error.backtrace.join("\n ")
154+
puts "Here's the full error information:",
155+
"#{error.class}: #{error.message}",
156+
" #{error.backtrace.join("\n ")}"
133157
puts
134-
puts "You might have more luck using Mislav's SSL doctor.rb script. You can get it here:"
135-
puts "https://github.com/mislav/ssl-tools/blob/8b3dec4/doctor.rb"
136-
puts "Read more about the script and how to use it in this blog post:"
137-
puts "https://mislav.net/2013/07/ruby-openssl/"
138-
abort
158+
puts "You might have more luck using Mislav's SSL doctor.rb script. You can get it here:",
159+
"https://github.com/mislav/ssl-tools/blob/8b3dec4/doctor.rb",
160+
"Read more about the script and how to use it in this blog post:",
161+
"https://mislav.net/2013/07/ruby-openssl/", ""
139162
end
163+
exit 1
140164
end
141165

142166
guide_url = "http://ruby.to/ssl-check-failed"
143167
if bundler_status =~ /success/ && rubygems_status =~ /success/
144168
# Whoa, it seems like it's working!
145169
puts "Hooray! This Ruby can connect to #{host}.",
146-
"You are all set to use Bundler and RubyGems. 👌", ""
170+
"You are all set to use Bundler and RubyGems. 👌", ""
147171
elsif rubygems_status !~ /success/
148172
puts "It looks like Ruby and Bundler can connect to #{host}, but RubyGems itself",
149173
"cannot. You can likely solve this by manually downloading and installing a",
@@ -156,9 +180,9 @@ def error_reason(error)
156180
"check out the troubleshooting guide at #{guide_url} 📦"
157181
else
158182
puts "For some reason, your Ruby installation can connect to #{host}, but neither",
159-
"RubyGems nor Bundler can. The most likely fix is to manually upgrade RubyGems by",
160-
"following the instructions at #{guide_url}. After you've done that, run `gem install",
161-
"bundler` to upgrade Bundler, and then run this script again to make sure everything worked. ❣️"
183+
"RubyGems nor Bundler can. The most likely fix is to manually upgrade RubyGems by",
184+
"following the instructions at #{guide_url}. After you've done that, run `gem install",
185+
"bundler` to upgrade Bundler, and then run this script again to make sure everything worked. ❣️"
162186
end
163187

164188
def tls12_supported?
@@ -174,10 +198,9 @@ def tls12_supported?
174198

175199
# We were able to connect, but perhaps this Ruby will have trouble when we require TLSv1.2
176200
unless tls12_supported?
177-
puts
178-
puts "WARNING: Although your Ruby can connect to #{host} today, your OpenSSL is very old! 👴"
179-
puts "WARNING: You will need to upgrade OpenSSL to use #{host}."
180-
abort
201+
puts "\nWARNING: Although your Ruby can connect to #{host} today, your OpenSSL is very old! 👴",
202+
"WARNING: You will need to upgrade OpenSSL to use #{host}."
203+
exit 1
181204
end
182205

183206
exit 0

0 commit comments

Comments
 (0)