summaryrefslogtreecommitdiff
path: root/spec/bundler/commands/ssl_spec.rb
blob: b4aca55194e23f591ea44eb037d742b80559d7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# frozen_string_literal: true

require "bundler/cli"
require "bundler/cli/doctor"
require "bundler/cli/doctor/ssl"
require_relative "../support/artifice/helpers/artifice"
require "bundler/vendored_persistent.rb"

RSpec.describe "bundle doctor ssl" do
  before(:each) do
    require_rack_test
    require_relative "../support/artifice/helpers/endpoint"

    @dummy_endpoint = Class.new(Endpoint) do
      get "/" do
      end
    end

    @previous_level = Bundler.ui.level
    Bundler.ui.instance_variable_get(:@warning_history).clear
    @previous_client = Gem::Request::ConnectionPools.client
    Bundler.ui.level = "info"
    Artifice.activate_with(@dummy_endpoint)
    Gem::Request::ConnectionPools.client = Gem::Net::HTTP
  end

  after(:each) do
    Bundler.ui.level = @previous_level
    Artifice.deactivate
    Gem::Request::ConnectionPools.client = @previous_client
  end

  context "when a diagnostic fails" do
    it "prints the diagnostic when openssl can't be loaded" do
      subject = Bundler::CLI::Doctor::SSL.new({})
      allow(subject).to receive(:require).with("openssl").and_raise(LoadError)

      expected_err = <<~MSG
        Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to rubygems.org.
        You'll need to recompile or reinstall Ruby with OpenSSL support and try again.
      MSG

      expect { subject.run }.to output("").to_stdout.and output(expected_err).to_stderr
    end

    it "fails due to certificate verification", :ruby_repo do
      net_http = Class.new(Artifice::Net::HTTP) do
        def connect
          raise OpenSSL::SSL::SSLError, "certificate verify failed"
        end
      end

      Artifice.replace_net_http(net_http)
      Gem::Request::ConnectionPools.client = net_http
      Gem::RemoteFetcher.fetcher.close_all

      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
      MSG

      expected_err = <<~MSG
        Bundler:       failed     (certificate verification)
        RubyGems:      failed     (certificate verification)
        Ruby net/http: failed

        Unfortunately, this Ruby can't connect to rubygems.org.

        Below affect only Ruby net/http connections:
        SSL_CERT_FILE: exists     #{OpenSSL::X509::DEFAULT_CERT_FILE}
        SSL_CERT_DIR:  exists     #{OpenSSL::X509::DEFAULT_CERT_DIR}

        Your Ruby can't connect to rubygems.org because you are missing the certificate files OpenSSL needs to verify you are connecting to the genuine rubygems.org servers.

      MSG

      subject = Bundler::CLI::Doctor::SSL.new({})
      expect { subject.run }.to output(expected_out).to_stdout.and output(expected_err).to_stderr
    end

    it "fails due to a too old tls version" do
      subject = Bundler::CLI::Doctor::SSL.new({})

      net_http = Class.new(Artifice::Net::HTTP) do
        def connect
          raise OpenSSL::SSL::SSLError, "read server hello A"
        end
      end

      Artifice.replace_net_http(net_http)
      Gem::Request::ConnectionPools.client = Gem::Net::HTTP
      Gem::RemoteFetcher.fetcher.close_all

      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
      MSG

      expected_err = <<~MSG
        Bundler:       failed     (SSL/TLS protocol version mismatch)
        RubyGems:      failed     (SSL/TLS protocol version mismatch)
        Ruby net/http: failed

        Unfortunately, this Ruby can't connect to rubygems.org.

        Your Ruby can't connect to rubygems.org because your version of OpenSSL is too old.
        You'll need to upgrade your OpenSSL install and/or recompile Ruby to use a newer OpenSSL.

      MSG

      expect { subject.run }.to output(expected_out).to_stdout.and output(expected_err).to_stderr
    end

    it "fails due to unsupported tls 1.3 version" do
      net_http = Class.new(Artifice::Net::HTTP) do
        def connect
          raise OpenSSL::SSL::SSLError, "read server hello A"
        end
      end

      Artifice.replace_net_http(net_http)
      Gem::Request::ConnectionPools.client = net_http
      Gem::RemoteFetcher.fetcher.close_all

      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
      MSG

      expected_err = <<~MSG
        Bundler:       failed     (SSL/TLS protocol version mismatch)
        RubyGems:      failed     (SSL/TLS protocol version mismatch)
        Ruby net/http: failed

        Unfortunately, this Ruby can't connect to rubygems.org.

        Your Ruby can't connect to rubygems.org because TLS1_3 isn't supported yet.

      MSG

      subject = Bundler::CLI::Doctor::SSL.new("tls-version": "1.3")
      expect { subject.run }.to output(expected_out).to_stdout.and output(expected_err).to_stderr
    end

    it "fails due to a bundler and rubygems connection error" do
      endpoint = Class.new(Endpoint) do
        get "/" do
          raise OpenSSL::SSL::SSLError, "read server hello A"
        end
      end

      Artifice.activate_with(endpoint)
      Gem::Request::ConnectionPools.client = Gem::Net::HTTP

      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
        Ruby net/http: success

        For some reason, your Ruby installation can connect to rubygems.org, but neither RubyGems nor Bundler can.
        The most likely fix is to manually upgrade RubyGems by following the instructions at http://ruby.to/ssl-check-failed.
        After you've done that, run `gem install bundler` to upgrade Bundler, and then run this script again to make sure everything worked. ❣

      MSG

      expected_err = <<~MSG
        Bundler:       failed     (SSL/TLS protocol version mismatch)
        RubyGems:      failed     (SSL/TLS protocol version mismatch)
      MSG

      subject = Bundler::CLI::Doctor::SSL.new({})
      expect { subject.run }.to output(expected_out).to_stdout.and output(expected_err).to_stderr
    end

    it "fails due to a bundler connection error" do
      endpoint = Class.new(Endpoint) do
        get "/" do
          if request.user_agent.include?("bundler")
            raise OpenSSL::SSL::SSLError, "read server hello A"
          end
        end
      end

      Artifice.activate_with(endpoint)
      Gem::Request::ConnectionPools.client = Gem::Net::HTTP

      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
        RubyGems:      success
        Ruby net/http: success

        Although your Ruby installation and RubyGems can both connect to rubygems.org, 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 http://ruby.to/ssl-check-failed.

      MSG

      expected_err = <<~MSG
        Bundler:       failed     (SSL/TLS protocol version mismatch)
      MSG

      subject = Bundler::CLI::Doctor::SSL.new({})
      expect { subject.run }.to output(expected_out).to_stdout.and output(expected_err).to_stderr
    end

    it "fails due to a RubyGems connection error" do
      endpoint = Class.new(Endpoint) do
        get "/" do
          if request.user_agent.include?("Ruby, RubyGems")
            raise OpenSSL::SSL::SSLError, "read server hello A"
          end
        end
      end

      Artifice.activate_with(endpoint)
      Gem::Request::ConnectionPools.client = Gem::Net::HTTP

      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
        Bundler:       success
        Ruby net/http: success

        It looks like Ruby and Bundler can connect to rubygems.org, but RubyGems itself cannot.
        You can likely solve this by manually downloading and installing a RubyGems update.
        Visit http://ruby.to/ssl-check-failed for instructions on how to manually upgrade RubyGems.

      MSG

      expected_err = <<~MSG
        RubyGems:      failed     (SSL/TLS protocol version mismatch)
      MSG

      subject = Bundler::CLI::Doctor::SSL.new({})
      expect { subject.run }.to output(expected_out).to_stdout.and output(expected_err).to_stderr
    end
  end

  context "when no diagnostic fails" do
    it "prints the SSL environment" do
      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
        Bundler:       success
        RubyGems:      success
        Ruby net/http: success

        Hooray! This Ruby can connect to rubygems.org.
        You are all set to use Bundler and RubyGems.

      MSG

      subject = Bundler::CLI::Doctor::SSL.new({})
      expect { subject.run }.to output(expected_out).to_stdout.and output("").to_stderr
    end

    it "uses the tls_version verify mode and host when given as option" do
      net_http = Class.new(Artifice::Net::HTTP) do
        class << self
          attr_accessor :verify_mode, :min_version, :max_version
        end

        def connect
          self.class.verify_mode = verify_mode
          self.class.min_version = min_version
          self.class.max_version = max_version

          super
        end
      end

      net_http.endpoint = @dummy_endpoint
      Artifice.replace_net_http(net_http)
      Gem::Request::ConnectionPools.client = net_http
      Gem::RemoteFetcher.fetcher.close_all

      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://example.org:
        Bundler:       success
        RubyGems:      success
        Ruby net/http: success

        Hooray! This Ruby can connect to example.org.
        You are all set to use Bundler and RubyGems.

      MSG

      subject = Bundler::CLI::Doctor::SSL.new("tls-version": "1.3", "verify-mode": :none, host: "example.org")
      expect { subject.run }.to output(expected_out).to_stdout.and output("").to_stderr
      expect(net_http.verify_mode).to eq(0)
      expect(net_http.min_version.to_s).to eq("TLS1_3")
      expect(net_http.max_version.to_s).to eq("TLS1_3")
    end

    it "warns when TLS1.2 is not supported" do
      expected_out = <<~MSG
        Here's your OpenSSL environment:

        OpenSSL:       #{OpenSSL::VERSION}
        Compiled with: #{OpenSSL::OPENSSL_VERSION}
        Loaded with:   #{OpenSSL::OPENSSL_LIBRARY_VERSION}

        Trying connections to https://rubygems.org:
        Bundler:       success
        RubyGems:      success
        Ruby net/http: success

        Hooray! This Ruby can connect to rubygems.org.
        You are all set to use Bundler and RubyGems.

      MSG

      expected_err = <<~MSG

        WARNING: Although your Ruby can connect to rubygems.org today, your OpenSSL is very old!
        WARNING: You will need to upgrade OpenSSL to use rubygems.org.

      MSG

      previous_version = OpenSSL::SSL::TLS1_2_VERSION
      OpenSSL::SSL.send(:remove_const, :TLS1_2_VERSION)

      subject = Bundler::CLI::Doctor::SSL.new({})
      expect { subject.run }.to output(expected_out).to_stdout.and output(expected_err).to_stderr
    ensure
      OpenSSL::SSL.const_set(:TLS1_2_VERSION, previous_version)
    end
  end
end