litmus_paper 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/config.ru CHANGED
@@ -1,5 +1,5 @@
1
1
  $LOAD_PATH.unshift File.expand_path('lib', File.dirname(__FILE__))
2
2
  require 'litmus_paper'
3
3
 
4
- use Rack::CommonLogger, LitmusPaper::Logger.new
4
+ use Rack::CommonLogger, LitmusPaper.logger
5
5
  run LitmusPaper::App
@@ -6,6 +6,7 @@ module LitmusPaper
6
6
  end
7
7
 
8
8
  def evaluate(file = @config_file_path)
9
+ LitmusPaper.logger.info "Loading file #{file}"
9
10
  config_contents = File.read(file)
10
11
  instance_eval(config_contents)
11
12
  @services
@@ -13,6 +14,8 @@ module LitmusPaper
13
14
 
14
15
  def include_files(glob_pattern)
15
16
  full_glob_pattern = File.expand_path(glob_pattern, File.dirname(@config_file_path))
17
+ LitmusPaper.logger.info "Searching for files matching: #{full_glob_pattern}"
18
+
16
19
  Dir.glob(full_glob_pattern).each do |file|
17
20
  evaluate(file)
18
21
  end
@@ -5,12 +5,20 @@ module LitmusPaper
5
5
  @uri = uri
6
6
  @expected_content = Regexp.new(options.fetch(:content, '.*'))
7
7
  @method = options.fetch(:method, 'GET')
8
+ @ca_file = options[:ca_file]
8
9
  end
9
10
 
10
11
  def available?
11
12
  response = _make_request
12
- _successful_response?(response) && _body_matches?(response)
13
- rescue Exception
13
+ success = _successful_response?(response)
14
+ matches = _body_matches?(response)
15
+
16
+ LitmusPaper.logger.info("Available check to #{@uri} failed with status #{response.code}") unless success
17
+ LitmusPaper.logger.info("Available check to #{@uri} did not match #{@expected_content}") unless matches
18
+
19
+ success && matches
20
+ rescue Exception => e
21
+ LitmusPaper.logger.info("Available check to #{@uri} failed with #{e.message}")
14
22
  false
15
23
  end
16
24
 
@@ -19,7 +27,15 @@ module LitmusPaper
19
27
  request = Net::HTTP.const_get(@method.capitalize).new(uri.normalize.path)
20
28
  request.set_form_data({})
21
29
 
22
- Net::HTTP.start(uri.host, uri.port) do |http|
30
+ connection = Net::HTTP.new(uri.host, uri.port)
31
+ if uri.scheme == "https"
32
+ connection.use_ssl = true
33
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
34
+ connection.ca_file = @ca_file unless @ca_file.nil?
35
+ connection.verify_callback = proc { |preverify_ok, ssl_context| _verify_ssl_certificate(preverify_ok, ssl_context) }
36
+ end
37
+
38
+ connection.start do |http|
23
39
  http.request(request)
24
40
  end
25
41
  end
@@ -32,6 +48,15 @@ module LitmusPaper
32
48
  (response.body =~ @expected_content) ? true : false
33
49
  end
34
50
 
51
+ def _verify_ssl_certificate(preverify_ok, ssl_context)
52
+ if preverify_ok != true || ssl_context.error != 0
53
+ err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
54
+ LitmusPaper.logger.info err_msg
55
+ false
56
+ end
57
+ true
58
+ end
59
+
35
60
  def to_s
36
61
  "Dependency::HTTP(#{@uri})"
37
62
  end
@@ -1,3 +1,3 @@
1
1
  module LitmusPaper
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/litmus_paper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'pathname'
2
2
  require 'net/http'
3
+ require 'net/https'
3
4
  require 'uri'
4
5
  require 'forwardable'
5
6
 
@@ -24,8 +25,11 @@ require 'litmus_paper/status_file'
24
25
  module LitmusPaper
25
26
  class << self
26
27
  attr_reader :services, :config_dir
28
+ attr_accessor :logger
27
29
  end
28
30
 
31
+ self.logger = Logger.new
32
+
29
33
  def self.configure(filename)
30
34
  @config_file = filename
31
35
 
@@ -22,6 +22,23 @@ describe LitmusPaper::Dependency::HTTP do
22
22
  end
23
23
  end
24
24
 
25
+ context "https" do
26
+ it "can make https request" do
27
+ begin
28
+ `env SSL_TEST_PORT=9295 PID_FILE=/tmp/https-test-server.pid bundle exec spec/script/https_test_server.rb`
29
+ SpecHelper.wait_for_service :host => '127.0.0.1', :port => 9295
30
+
31
+ check = LitmusPaper::Dependency::HTTP.new(
32
+ "https://localhost:9295/",
33
+ :ca_file => TEST_CA_CERT
34
+ )
35
+ check.should be_available
36
+ ensure
37
+ system "kill -9 `cat /tmp/https-test-server.pid`"
38
+ end
39
+ end
40
+ end
41
+
25
42
  it "is true when response is 200" do
26
43
  check = LitmusPaper::Dependency::HTTP.new("#{@url}/status/200")
27
44
  check.should be_available
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ require 'webrick'
3
+ require 'webrick/https'
4
+ require 'openssl'
5
+
6
+ private_key_file = File.expand_path(File.join(File.dirname(__FILE__), "..", "ssl", "server.key"))
7
+ cert_file = File.expand_path(File.join(File.dirname(__FILE__), "..", "ssl", "server.crt"))
8
+
9
+ pkey = OpenSSL::PKey::RSA.new(File.read(private_key_file))
10
+ cert = OpenSSL::X509::Certificate.new(File.read(cert_file))
11
+
12
+ pid_file = ENV["PID_FILE"]
13
+
14
+ s = WEBrick::HTTPServer.new(
15
+ :Port => (ENV['SSL_TEST_PORT'] || 8443),
16
+ :Logger => WEBrick::Log::new(nil, WEBrick::Log::ERROR),
17
+ :DocumentRoot => File.join(File.dirname(__FILE__)),
18
+ :ServerType => WEBrick::Daemon,
19
+ :SSLEnable => true,
20
+ :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
21
+ :SSLCertificate => cert,
22
+ :SSLPrivateKey => pkey,
23
+ :SSLCertName => [ [ "CN",WEBrick::Utils::getservername ] ],
24
+ :StartCallback => proc { File.open(pid_file, "w") { |f| f.write $$.to_s }}
25
+ )
26
+ s.mount_proc("/") { |req,resp| resp.body = "hello world" }
27
+ trap("INT"){ s.shutdown }
28
+ s.start
data/spec/spec_helper.rb CHANGED
@@ -44,3 +44,4 @@ LitmusPaper.config_dir = "/tmp/litmus_paper"
44
44
 
45
45
  TEST_CONFIG = File.expand_path('support/test.config', File.dirname(__FILE__))
46
46
  TEST_D_CONFIG = File.expand_path('support/test.d.config', File.dirname(__FILE__))
47
+ TEST_CA_CERT = File.expand_path('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4Hye/lzkZdKYIN2eu6X8wKgbO2hafKWxAJDFQXQOtqoRVVgGYbE1MXVQIDAQAB
5
+ AoGANyPdAYKvgoYoZgPsoKXwaNIkyF4RPAnkNOmQzQoZdMeYUmRP3ErO8FU/IKdn
6
+ WlwEWq/GyrlHo6XkAkAXqG2ZBITsQaWKRky8nuydsJTXyt3z09R17WpH6dYNSyw5
7
+ wc9UyGg+u5+hHiAOuMMVPEpePB4gBAo/XIBHe4ApqY5ZkakCQQDpI+DV2UmNLUG3
8
+ qKavyncY382Fctc/m2GbNn2+B+6tDDGFcs4XwwPGEHaP66U3Aa9AU2MS2dPSDRl1
9
+ IPvpQsTLAkEAwJFOvxKsxMwZT7vhCNkc8fKGI5wkjXRpCL5pEDUdDHExUbmaCGra
10
+ PfNMEBkIDL4wGVNLLDZtl3/AgiyGzTIwXwJBAOP/BVK841QgDSOp2BEUJDJ8M8Ys
11
+ z9nRf7vFp2Se50fuI8PIyUmtmlQpJ3mJzpGjoRp8Mehug9rompvUPOcaN88CQH9a
12
+ 2niV0jtpPEg2WlLuvCzrl3lPmAERfzGwNG3qoqaK6fJBzLRKbsHP+XsgAuniko79
13
+ PlckPACZFelBp3ln+wMCQQCvuEg+qo97LutnrUWID67PHEp+2W7SButs6yMerSac
14
+ TYokvi2xz9auzRvxHofQiBIKsMji51uLmW8CfFaC8kS4
15
+ -----END RSA PRIVATE KEY-----
@@ -0,0 +1,8 @@
1
+ class StdoutLogger
2
+ def write(message)
3
+ puts message
4
+ end
5
+
6
+ alias info write
7
+ alias debug write
8
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litmus_paper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Braintreeps
@@ -178,7 +178,10 @@ files:
178
178
  - spec/litmus_paper/service_spec.rb
179
179
  - spec/litmus_paper/status_file_spec.rb
180
180
  - spec/litmus_paper_spec.rb
181
+ - spec/script/https_test_server.rb
181
182
  - spec/spec_helper.rb
183
+ - spec/ssl/server.crt
184
+ - spec/ssl/server.key
182
185
  - spec/support/always_available_dependency.rb
183
186
  - spec/support/config.d/passing_test.config
184
187
  - spec/support/config.d/test.config
@@ -186,6 +189,7 @@ files:
186
189
  - spec/support/http_test_server.rb
187
190
  - spec/support/http_test_server_config.ru
188
191
  - spec/support/never_available_dependency.rb
192
+ - spec/support/stdout_logger.rb
189
193
  - spec/support/stub_facter.rb
190
194
  - spec/support/test.config
191
195
  - spec/support/test.d.config
@@ -236,7 +240,10 @@ test_files:
236
240
  - spec/litmus_paper/service_spec.rb
237
241
  - spec/litmus_paper/status_file_spec.rb
238
242
  - spec/litmus_paper_spec.rb
243
+ - spec/script/https_test_server.rb
239
244
  - spec/spec_helper.rb
245
+ - spec/ssl/server.crt
246
+ - spec/ssl/server.key
240
247
  - spec/support/always_available_dependency.rb
241
248
  - spec/support/config.d/passing_test.config
242
249
  - spec/support/config.d/test.config
@@ -244,6 +251,7 @@ test_files:
244
251
  - spec/support/http_test_server.rb
245
252
  - spec/support/http_test_server_config.ru
246
253
  - spec/support/never_available_dependency.rb
254
+ - spec/support/stdout_logger.rb
247
255
  - spec/support/stub_facter.rb
248
256
  - spec/support/test.config
249
257
  - spec/support/test.d.config