diff --git a/spec/openssl_unfreeze.rb b/spec/openssl_unfreeze.rb index f639c311f..6a76e8e6d 100644 --- a/spec/openssl_unfreeze.rb +++ b/spec/openssl_unfreeze.rb @@ -4,26 +4,30 @@ # When loaded via RUBYOPT before bundler has activated gems we must NOT call # require 'openssl' ourselves -- that would activate the wrong default-gem version # and then bundler's setup.rb would raise Gem::LoadError (already activated X, need Y). -# Instead, if openssl is already loaded apply the fix now; otherwise hook Kernel#require -# so the fix is applied the moment openssl is loaded (by bundler, puppet, or anyone else). -if defined?(OpenSSL::SSL::SSLContext::DEFAULT_PARAMS) - if OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.frozen? - unfrozen = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.dup - OpenSSL::SSL::SSLContext.send(:remove_const, :DEFAULT_PARAMS) - OpenSSL::SSL::SSLContext.const_set(:DEFAULT_PARAMS, unfrozen) - end -else - module OpenSSLUnfreezeOnRequire - def require(name) - result = super - if defined?(OpenSSL::SSL::SSLContext::DEFAULT_PARAMS) && - OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.frozen? - unfrozen = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.dup - OpenSSL::SSL::SSLContext.send(:remove_const, :DEFAULT_PARAMS) - OpenSSL::SSL::SSLContext.const_set(:DEFAULT_PARAMS, unfrozen) +# +# Don't hook Kernel#require either: other code redefines it in ways that break +# or bypass a hook. rspec-puppet 6 aliases it (breaks a prepended hook's super) +# and Ruby's bundled_gems.rb replaces it (skips an alias-chain hook). Instead, a +# TracePoint on :class unfreezes DEFAULT_PARAMS when puppet reopens +# `class OpenSSL::SSL::SSLContext`, before the class body runs. +unless defined?(SensuOpenSSLUnfreeze) + module SensuOpenSSLUnfreeze + def self.unfreeze! + return unless defined?(OpenSSL::SSL::SSLContext::DEFAULT_PARAMS) + return unless OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.frozen? + + unfrozen = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.dup + OpenSSL::SSL::SSLContext.send(:remove_const, :DEFAULT_PARAMS) + OpenSSL::SSL::SSLContext.const_set(:DEFAULT_PARAMS, unfrozen) + end + + TRACE = TracePoint.new(:class) do |tp| + if defined?(OpenSSL::SSL::SSLContext) && tp.self.equal?(OpenSSL::SSL::SSLContext) + SensuOpenSSLUnfreeze.unfreeze! end - result end + TRACE.enable end - Kernel.prepend(OpenSSLUnfreezeOnRequire) end + +SensuOpenSSLUnfreeze.unfreeze!