scout_apm 6.3.3 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'erb'
3
+ require 'openssl'
3
4
 
4
5
  require 'scout_apm/environment'
5
6
 
@@ -353,6 +354,19 @@ module ScoutApm
353
354
  "Resolved Setting Values:\n" +
354
355
  all_settings.map{|hsh| "#{hsh[:source]} - #{hsh[:key]}: #{hsh[:value]}"}.join("\n")
355
356
  )
357
+ logger.debug(ssl_trust_store_message)
358
+ end
359
+
360
+ def ssl_trust_store_message
361
+ cert_file = value('ssl_cert_file')
362
+ if cert_file.nil?
363
+ "HTTPS reporting verifies against OpenSSL's default trust store " \
364
+ "(#{OpenSSL::X509::DEFAULT_CERT_FILE}, #{OpenSSL::X509::DEFAULT_CERT_DIR})"
365
+ elsif cert_file == ConfigDefaults::BUNDLED_SSL_CERT_FILE
366
+ "HTTPS reporting verifies against the bundled CA file #{cert_file}"
367
+ else
368
+ "HTTPS reporting verifies against ssl_cert_file #{cert_file}"
369
+ end
356
370
  end
357
371
 
358
372
  def logger
@@ -360,6 +374,40 @@ module ScoutApm
360
374
  end
361
375
 
362
376
  class ConfigDefaults
377
+ # Mozilla's CA bundle as published by curl (https://curl.se/docs/caextract.html).
378
+ # To refresh it, download https://curl.se/ca/cacert.pem, check it against
379
+ # https://curl.se/ca/cacert.pem.sha256, and replace data/cacert.pem unmodified.
380
+ BUNDLED_SSL_CERT_FILE = File.join(File.dirname(__FILE__), *%w[.. .. data cacert.pem])
381
+
382
+ def self.default_ssl_cert_file(cert_file=OpenSSL::X509::DEFAULT_CERT_FILE,
383
+ cert_dir=OpenSSL::X509::DEFAULT_CERT_DIR,
384
+ env=ENV)
385
+ # SSL_CERT_FILE and SSL_CERT_DIR replace OpenSSL's default store, often
386
+ # with a bundle meant only for internal services. The bundled file has
387
+ # always taken precedence over them, so keep it when either is set.
388
+ # SCOUT_SSL_CERT_FILE still selects any other file explicitly.
389
+ return BUNDLED_SSL_CERT_FILE if env.key?(OpenSSL::X509::DEFAULT_CERT_FILE_ENV) ||
390
+ env.key?(OpenSSL::X509::DEFAULT_CERT_DIR_ENV)
391
+
392
+ # nil leaves Net::HTTP on OpenSSL's default trust store.
393
+ return nil if (File.file?(cert_file) && File.readable?(cert_file)) ||
394
+ hashed_cert_dir?(cert_dir)
395
+
396
+ BUNDLED_SSL_CERT_FILE
397
+ end
398
+
399
+ # OpenSSL only finds CAs in a directory through hash links such as
400
+ # "5ad8a5d6.0". A directory without any, like the empty /etc/ssl/certs
401
+ # left by installing openssl without ca-certificates, has nothing to trust.
402
+ # Names are matched as binary because this runs at require time, and a
403
+ # name that is invalid in the locale's encoding would raise ArgumentError.
404
+ def self.hashed_cert_dir?(cert_dir)
405
+ File.directory?(cert_dir) &&
406
+ Dir.entries(cert_dir).any? { |name| name.b =~ /\A\h{8}\.\d+\z/n }
407
+ rescue SystemCallError
408
+ false
409
+ end
410
+
363
411
  DEFAULTS = {
364
412
  'compress_payload' => true,
365
413
  'detailed_middleware' => false,
@@ -400,7 +448,7 @@ module ScoutApm
400
448
  'use_prepend' => false,
401
449
  'alias_method_instruments' => [],
402
450
  'prepend_instruments' => [],
403
- 'ssl_cert_file' => File.join( File.dirname(__FILE__), *%w[.. .. data cacert.pem] ),
451
+ 'ssl_cert_file' => default_ssl_cert_file,
404
452
  'errors_enabled' => false,
405
453
  'errors_ignored_exceptions' => %w(ActiveRecord::RecordNotFound ActionController::RoutingError),
406
454
  'errors_filtered_params' => %w(password s3-key),
@@ -126,7 +126,8 @@ module ScoutApm
126
126
  proxy_uri.password).new(url.host, url.port)
127
127
  if url.is_a?(URI::HTTPS)
128
128
  http.use_ssl = true
129
- http.ca_file = config.value("ssl_cert_file")
129
+ cert_file = config.value("ssl_cert_file")
130
+ http.ca_file = cert_file unless cert_file.nil?
130
131
  http.verify_mode = VERIFY_MODE
131
132
  end
132
133
  http
@@ -1,3 +1,3 @@
1
1
  module ScoutApm
2
- VERSION = "6.3.3"
2
+ VERSION = "6.4.0"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  require 'scout_apm/config'
4
+ require 'tmpdir'
4
5
 
5
6
  class ConfigTest < Minitest::Test
6
7
  def setup
@@ -21,6 +22,109 @@ class ConfigTest < Minitest::Test
21
22
  assert_equal 'testval', conf.value('config_test_key')
22
23
  end
23
24
 
25
+ def test_ssl_cert_file_defaults_to_os_store_when_cert_file_exists
26
+ Dir.mktmpdir do |dir|
27
+ cert_file = File.join(dir, 'cert.pem')
28
+ File.write(cert_file, 'certificate')
29
+
30
+ assert_nil ScoutApm::Config::ConfigDefaults.default_ssl_cert_file(cert_file, File.join(dir, 'missing'), {})
31
+ end
32
+ end
33
+
34
+ def test_ssl_cert_file_defaults_to_os_store_when_only_cert_dir_has_hashed_certs
35
+ Dir.mktmpdir do |dir|
36
+ File.write(File.join(dir, '5ad8a5d6.0'), 'certificate')
37
+
38
+ assert_nil ScoutApm::Config::ConfigDefaults.default_ssl_cert_file(File.join(dir, 'missing'), dir, {})
39
+ end
40
+ end
41
+
42
+ def test_ssl_cert_file_falls_back_to_bundled_file_when_cert_dir_is_empty
43
+ Dir.mktmpdir do |dir|
44
+ defaults = ScoutApm::Config::ConfigDefaults
45
+ assert_equal defaults::BUNDLED_SSL_CERT_FILE,
46
+ defaults.default_ssl_cert_file(File.join(dir, 'missing'), dir, {})
47
+ end
48
+ end
49
+
50
+ def test_ssl_cert_file_falls_back_to_bundled_file_when_cert_dir_has_no_hashed_certs
51
+ Dir.mktmpdir do |dir|
52
+ File.write(File.join(dir, 'README'), 'not a certificate')
53
+
54
+ defaults = ScoutApm::Config::ConfigDefaults
55
+ assert_equal defaults::BUNDLED_SSL_CERT_FILE,
56
+ defaults.default_ssl_cert_file(File.join(dir, 'missing'), dir, {})
57
+ end
58
+ end
59
+
60
+ def test_ssl_cert_file_ignores_cert_dir_names_invalid_in_the_locale_encoding
61
+ # An existing directory that needs no cleanup: Dir.mktmpdir's cleanup calls
62
+ # Dir.entries too, which the stub would intercept.
63
+ dir = File.dirname(__FILE__)
64
+ invalid_name = "\xFFcert".dup.force_encoding('UTF-8')
65
+ Dir.stubs(:entries).with(dir).returns(['.', '..', invalid_name, '5ad8a5d6.0'])
66
+
67
+ assert_nil ScoutApm::Config::ConfigDefaults.default_ssl_cert_file(File.join(dir, 'missing'), dir, {})
68
+ end
69
+
70
+ def test_bundled_ssl_cert_file_loads_into_a_cert_store
71
+ bundled = ScoutApm::Config::ConfigDefaults::BUNDLED_SSL_CERT_FILE
72
+ OpenSSL::X509::Store.new.add_file(bundled)
73
+
74
+ assert_operator File.binread(bundled).scan('-----BEGIN CERTIFICATE-----').size, :>, 100
75
+ end
76
+
77
+ def test_ssl_cert_file_falls_back_to_bundled_file_without_os_store
78
+ Dir.mktmpdir do |dir|
79
+ defaults = ScoutApm::Config::ConfigDefaults
80
+ assert_equal defaults::BUNDLED_SSL_CERT_FILE,
81
+ defaults.default_ssl_cert_file(File.join(dir, 'missing.pem'), File.join(dir, 'missing-certs'), {})
82
+ end
83
+ end
84
+
85
+ def test_ssl_cert_file_keeps_bundled_file_when_ssl_cert_file_env_is_set
86
+ Dir.mktmpdir do |dir|
87
+ cert_file = File.join(dir, 'cert.pem')
88
+ File.write(cert_file, 'certificate')
89
+
90
+ defaults = ScoutApm::Config::ConfigDefaults
91
+ assert_equal defaults::BUNDLED_SSL_CERT_FILE,
92
+ defaults.default_ssl_cert_file(cert_file, dir, 'SSL_CERT_FILE' => '/etc/internal-ca.pem')
93
+ end
94
+ end
95
+
96
+ def test_ssl_cert_file_keeps_bundled_file_when_ssl_cert_dir_env_is_set
97
+ Dir.mktmpdir do |dir|
98
+ File.write(File.join(dir, '5ad8a5d6.0'), 'certificate')
99
+
100
+ defaults = ScoutApm::Config::ConfigDefaults
101
+ assert_equal defaults::BUNDLED_SSL_CERT_FILE,
102
+ defaults.default_ssl_cert_file(File.join(dir, 'missing'), dir, 'SSL_CERT_DIR' => '/etc/internal-certs')
103
+ end
104
+ end
105
+
106
+ def test_ssl_trust_store_message_for_os_store
107
+ conf = make_fake_config('ssl_cert_file' => nil)
108
+ assert_match(/OpenSSL's default trust store/, conf.ssl_trust_store_message)
109
+ end
110
+
111
+ def test_ssl_trust_store_message_for_bundled_file
112
+ conf = make_fake_config('ssl_cert_file' => ScoutApm::Config::ConfigDefaults::BUNDLED_SSL_CERT_FILE)
113
+ assert_match(/bundled CA file/, conf.ssl_trust_store_message)
114
+ end
115
+
116
+ def test_ssl_trust_store_message_for_explicit_file
117
+ conf = make_fake_config('ssl_cert_file' => '/tmp/custom-ca.pem')
118
+ assert_equal 'HTTPS reporting verifies against ssl_cert_file /tmp/custom-ca.pem', conf.ssl_trust_store_message
119
+ end
120
+
121
+ def test_explicit_ssl_cert_file_overrides_default
122
+ ENV['SCOUT_SSL_CERT_FILE'] = '/tmp/custom-ca.pem'
123
+ assert_equal '/tmp/custom-ca.pem', ScoutApm::Config.without_file(@context).value('ssl_cert_file')
124
+ ensure
125
+ ENV.delete('SCOUT_SSL_CERT_FILE')
126
+ end
127
+
24
128
  def test_loading_a_file
25
129
  set_rack_env('production')
26
130
 
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class ReporterTest < Minitest::Test
4
+ FakeContext = Struct.new(:config)
5
+
6
+ def test_http_does_not_set_ca_file_when_default_is_nil
7
+ config = FakeConfigOverlay.new('ssl_cert_file' => nil)
8
+ reporter = ScoutApm::Reporter.new(FakeContext.new(config), :checkin)
9
+ Net::HTTP.any_instance.expects(:ca_file=).never
10
+
11
+ http = reporter.send(:http, URI.parse('https://example.com'))
12
+
13
+ assert http.use_ssl?
14
+ assert_equal ScoutApm::Reporter::VERIFY_MODE, http.verify_mode
15
+ end
16
+
17
+ def test_http_uses_explicit_ca_file
18
+ config = FakeConfigOverlay.new('ssl_cert_file' => '/tmp/custom-ca.pem')
19
+ reporter = ScoutApm::Reporter.new(FakeContext.new(config), :checkin)
20
+ Net::HTTP.any_instance.expects(:ca_file=).with('/tmp/custom-ca.pem')
21
+
22
+ reporter.send(:http, URI.parse('https://example.com'))
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scout_apm
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.3.3
4
+ version: 6.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Haynes
@@ -486,6 +486,7 @@ files:
486
486
  - test/unit/remote/message_test.rb
487
487
  - test/unit/remote/route_test.rb
488
488
  - test/unit/remote/server_test.rb
489
+ - test/unit/reporter_test.rb
489
490
  - test/unit/reporting_test.rb
490
491
  - test/unit/request_histograms_test.rb
491
492
  - test/unit/request_manager_test.rb