Skip to content

Commit

Permalink
(maint) Simplifies SSLContext intialization
Browse files Browse the repository at this point in the history
Ruby 2.5 added the `keyword_init: true` option to Struct.new to enable
keyword arguments.

This commit updates the Puppet::SSL::SSLContext to take advantage of
this language feature and removes a workaround needed for older versions
of Ruby.

Additionally, Ruby 3.2 addeed the ability to initialize a Struct with
keyword arguments without the `keyword_init: true` option. This commit
adds a comment noting that, so it can be removed in the future when
Puppet drops support for Ruby < 3.2.
  • Loading branch information
mhashizume committed Jun 20, 2023
1 parent fd7f09f commit e84c6c0
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lib/puppet/ssl/ssl_context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require_relative '../../puppet/ssl'

module Puppet::SSL
# Struct in Ruby >= 2.5 uses the `keyword_init: true` option to allow for keyword arguments. Ruby >= 3.2 allows Struct.new to use keyword
# arguments without `keyword_init: true`
SSLContext = Struct.new(
:store,
:cacerts,
Expand All @@ -9,22 +11,16 @@ module Puppet::SSL
:client_cert,
:client_chain,
:revocation,
:verify_peer
:verify_peer,
keyword_init: true
) do
DEFAULTS = {
cacerts: [],
crls: [],
client_chain: [],
revocation: true,
verify_peer: true
}.freeze

# This is an idiom to initialize a Struct from keyword
# arguments. Ruby 2.5 introduced `keyword_init: true` for
# that purpose, but we need to support older versions.
def initialize(kwargs = {})
super({})
DEFAULTS.merge(**kwargs).each { |k,v| self[k] = v }
def initialize(**)
super
self[:cacerts] ||= []
self[:crls] ||= []
self[:client_chain] ||= []
self[:revocation] ||= true
self[:verify_peer] ||= true
end
end
end

0 comments on commit e84c6c0

Please sign in to comment.