|
1 | | -require "logstash/devutils/rspec/spec_helper" |
2 | | -require "logstash/outputs/http" |
3 | | -require "logstash/codecs/plain" |
4 | | -require "thread" |
5 | | -require "sinatra" |
6 | | -require "webrick" |
7 | | -require "webrick/https" |
8 | | -require 'openssl' |
9 | | -require_relative "../supports/compressed_requests" |
10 | | - |
11 | | -PORT = rand(65535-1024) + 1025 |
12 | | - |
13 | | -class LogStash::Outputs::Http |
14 | | - attr_writer :agent |
15 | | - attr_reader :request_tokens |
16 | | -end |
17 | | - |
18 | | -# note that Sinatra startup and shutdown messages are directly logged to stderr so |
19 | | -# it is not really possible to disable them without reopening stderr which is not advisable. |
20 | | -# |
21 | | -# == Sinatra (v1.4.6) has taken the stage on 51572 for development with backup from WEBrick |
22 | | -# == Sinatra has ended his set (crowd applauds) |
23 | | -# |
24 | | -class TestApp < Sinatra::Base |
25 | | - # on the fly uncompress gzip content |
26 | | - use CompressedRequests |
27 | | - |
28 | | - set :environment, :production |
29 | | - set :sessions, false |
30 | | - |
31 | | - @@server_settings = { |
32 | | - :AccessLog => [], # disable WEBrick logging |
33 | | - :Logger => WEBrick::BasicLog::new(nil, WEBrick::BasicLog::FATAL) |
34 | | - } |
35 | | - |
36 | | - def self.server_settings |
37 | | - @@server_settings |
38 | | - end |
39 | | - |
40 | | - def self.server_settings=(settings) |
41 | | - @@server_settings = settings |
42 | | - end |
43 | | - |
44 | | - def self.multiroute(methods, path, &block) |
45 | | - methods.each do |method| |
46 | | - method.to_sym |
47 | | - self.send method, path, &block |
48 | | - end |
49 | | - end |
50 | | - |
51 | | - def self.last_request=(request) |
52 | | - @last_request = request |
53 | | - end |
54 | | - |
55 | | - def self.last_request |
56 | | - @last_request |
57 | | - end |
58 | | - |
59 | | - def self.retry_fail_count=(count) |
60 | | - @retry_fail_count = count |
61 | | - end |
62 | | - |
63 | | - def self.retry_fail_count() |
64 | | - @retry_fail_count || 2 |
65 | | - end |
66 | | - |
67 | | - multiroute(%w(get post put patch delete), "/good") do |
68 | | - self.class.last_request = request |
69 | | - [200, "YUP"] |
70 | | - end |
71 | | - |
72 | | - multiroute(%w(get post put patch delete), "/bad") do |
73 | | - self.class.last_request = request |
74 | | - [400, "YUP"] |
75 | | - end |
76 | | - |
77 | | - multiroute(%w(get post put patch delete), "/retry") do |
78 | | - self.class.last_request = request |
79 | | - |
80 | | - if self.class.retry_fail_count > 0 |
81 | | - self.class.retry_fail_count -= 1 |
82 | | - [429, "Will succeed in #{self.class.retry_fail_count}"] |
83 | | - else |
84 | | - [200, "Done Retrying"] |
85 | | - end |
86 | | - end |
87 | | -end |
88 | | - |
89 | | -RSpec.configure do |
90 | | - #http://stackoverflow.com/questions/6557079/start-and-call-ruby-http-server-in-the-same-script |
91 | | - def start_app_and_wait(app, opts = {}) |
92 | | - queue = Queue.new |
93 | | - |
94 | | - Thread.start do |
95 | | - begin |
96 | | - app.start!({ server: 'WEBrick', port: PORT }.merge opts) do |server| |
97 | | - queue.push(server) |
98 | | - end |
99 | | - rescue => e |
100 | | - warn "Error starting app: #{e.inspect}" # ignore |
101 | | - end |
102 | | - end |
103 | | - |
104 | | - queue.pop # blocks until the start! callback runs |
105 | | - end |
106 | | -end |
| 1 | +require File.expand_path('../spec_helper.rb', File.dirname(__FILE__)) |
107 | 2 |
|
108 | 3 | describe LogStash::Outputs::Http do |
109 | 4 | # Wait for the async request to finish in this spinlock |
@@ -520,24 +415,28 @@ def start_app_and_wait(app, opts = {}) |
520 | 415 | end |
521 | 416 | end |
522 | 417 |
|
523 | | -describe LogStash::Outputs::Http do # different block as we're starting web server with TLS |
| 418 | +RSpec.describe LogStash::Outputs::Http do # different block as we're starting web server with TLS |
524 | 419 |
|
525 | 420 | @@default_server_settings = TestApp.server_settings.dup |
526 | 421 |
|
527 | 422 | before do |
528 | | - cert, key = WEBrick::Utils.create_self_signed_cert 2048, [["CN", ssl_cert_host]], "Logstash testing" |
529 | | - TestApp.server_settings = @@default_server_settings.merge({ |
530 | | - :SSLEnable => true, |
531 | | - :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, |
532 | | - :SSLCertificate => cert, |
533 | | - :SSLPrivateKey => key |
534 | | - }) |
| 423 | + TestApp.server_settings = @@default_server_settings.merge(webrick_config) |
535 | 424 |
|
536 | 425 | TestApp.last_request = nil |
537 | 426 |
|
538 | 427 | @server = start_app_and_wait(TestApp) |
539 | 428 | end |
540 | 429 |
|
| 430 | + let(:webrick_config) do |
| 431 | + cert, key = WEBrick::Utils.create_self_signed_cert 2048, [["CN", ssl_cert_host]], "Logstash testing" |
| 432 | + { |
| 433 | + SSLEnable: true, |
| 434 | + SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE, |
| 435 | + SSLCertificate: cert, |
| 436 | + SSLPrivateKey: key |
| 437 | + } |
| 438 | + end |
| 439 | + |
541 | 440 | after do |
542 | 441 | @server.shutdown # WEBrick::HTTPServer |
543 | 442 |
|
@@ -590,4 +489,44 @@ def start_app_and_wait(app, opts = {}) |
590 | 489 |
|
591 | 490 | end |
592 | 491 |
|
| 492 | + context 'with supported_protocols set to (disabled) 1.1' do |
| 493 | + |
| 494 | + let(:config) { super().merge 'ssl_supported_protocols' => ['TLSv1.1'], 'ssl_verification_mode' => 'none' } |
| 495 | + |
| 496 | + it "keeps retrying due a protocol exception" do # TLSv1.1 not enabled by default |
| 497 | + expect(subject).to receive(:log_failure). |
| 498 | + with('Could not fetch URL', hash_including(message: 'No appropriate protocol (protocol is disabled or cipher suites are inappropriate)')). |
| 499 | + at_least(:once) |
| 500 | + Thread.start { subject.multi_receive [ event ] } |
| 501 | + sleep 1.0 |
| 502 | + end |
| 503 | + |
| 504 | + end unless tls_version_enabled_by_default?('TLSv1.1') |
| 505 | + |
| 506 | + context 'with supported_protocols set to 1.2/1.3' do |
| 507 | + |
| 508 | + let(:config) { super().merge 'ssl_supported_protocols' => ['TLSv1.2', 'TLSv1.3'], 'ssl_verification_mode' => 'none' } |
| 509 | + |
| 510 | + let(:webrick_config) { super().merge SSLVersion: 'TLSv1.2' } |
| 511 | + |
| 512 | + it "should process the request" do |
| 513 | + subject.multi_receive [ event ] |
| 514 | + expect(last_request_body).to include '"message":"hello!"' |
| 515 | + end |
| 516 | + |
| 517 | + end |
| 518 | + |
| 519 | + context 'with supported_protocols set to 1.3' do |
| 520 | + |
| 521 | + let(:config) { super().merge 'ssl_supported_protocols' => ['TLSv1.3'], 'ssl_verification_mode' => 'none' } |
| 522 | + |
| 523 | + let(:webrick_config) { super().merge SSLVersion: 'TLSv1.3' } |
| 524 | + |
| 525 | + it "should process the request" do |
| 526 | + subject.multi_receive [ event ] |
| 527 | + expect(last_request_body).to include '"message":"hello!"' |
| 528 | + end |
| 529 | + |
| 530 | + end if tls_version_enabled_by_default?('TLSv1.3') && JOpenSSL::VERSION > '0.12' # due WEBrick uses OpenSSL |
| 531 | + |
593 | 532 | end |
0 commit comments