Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions spec/openssl_unfreeze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Loading