Skip to content

Commit

Permalink
Use OpenSSL::PKey.generate_key instead of OpenSSL::PKey::Algo
Browse files Browse the repository at this point in the history
This was needed on Fedora 40 with OpenSSL 40 to make the test suite
pass.
  • Loading branch information
ekohl committed Jul 18, 2024
1 parent cac0733 commit fd2cd0e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
8 changes: 5 additions & 3 deletions lib/puppet/provider/ssl_pkey/openssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ def self.dirname(resource)
resource[:path].dirname
end

# @see man openssl genpkey
def self.generate_key(resource)
case resource[:authentication]
when :dsa
OpenSSL::PKey::DSA.new(resource[:size])
params = OpenSSL::PKey.generate_parameters('DSA', 'dsa_paramgen_bits' => resource[:size])
OpenSSL::PKey.generate_key(params)
when :rsa
OpenSSL::PKey::RSA.new(resource[:size])
OpenSSL::PKey.generate_key('RSA', 'rsa_keygen_bits' => resource[:size])
when :ec
OpenSSL::PKey::EC.new(resource[:curve]).generate_key
OpenSSL::PKey.generate_key('EC', 'ec_paramgen_curve' => resource[:curve])
else
raise Puppet::Error,
"Unknown authentication type '#{resource[:authentication]}'"
Expand Down
35 changes: 18 additions & 17 deletions spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
let(:path) { '/tmp/foo.key' }
let(:pathname) { Pathname.new(path) }
let(:resource) { Puppet::Type::Ssl_pkey.new(path: path) }
let(:key) { OpenSSL::PKey::RSA.new }

it 'exists? should return true if key exists' do
expect(Pathname).to receive(:new).twice.with(path).and_return(pathname)
Expand All @@ -24,15 +23,15 @@

context 'when creating a key with defaults' do
it 'creates an rsa key' do

Check failure on line 25 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when creating a key with defaults creates an rsa key Failure/Error: OpenSSL::PKey.generate_key('RSA', 'rsa_keygen_bits' => resource[:size]) NameError: undefined local variable or method `generate_key' for OpenSSL::PKey:Module
allow(OpenSSL::PKey::RSA).to receive(:new).with(2048).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('RSA', 'rsa_keygen_bits' => 2048).and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end

context 'when setting size' do
it 'creates with given size' do

Check failure on line 32 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when creating a key with defaults when setting size creates with given size Failure/Error: OpenSSL::PKey.generate_key('RSA', 'rsa_keygen_bits' => resource[:size]) NameError: undefined local variable or method `generate_key' for OpenSSL::PKey:Module
resource[:size] = 1024
allow(OpenSSL::PKey::RSA).to receive(:new).with(1024).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('RSA', 'rsa_keygen_bits' => 1024).and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end
Expand All @@ -41,7 +40,7 @@
context 'when setting password' do
it 'creates with given password' do

Check failure on line 41 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when creating a key with defaults when setting password creates with given password Failure/Error: OpenSSL::PKey.generate_key('RSA', 'rsa_keygen_bits' => resource[:size]) NameError: undefined local variable or method `generate_key' for OpenSSL::PKey:Module
resource[:password] = '2x$5{'
allow(OpenSSL::PKey::RSA).to receive(:new).with(2048).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('RSA', 'rsa_keygen_bits' => 2048).and_call_original
allow(OpenSSL::Cipher).to receive(:new).with('des3')
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
Expand All @@ -50,9 +49,9 @@
end

context 'when setting authentication to rsa' do
it 'creates a dsa key' do
it 'creates an rsa key' do

Check failure on line 52 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when setting authentication to rsa creates an rsa key Failure/Error: OpenSSL::PKey.generate_key('RSA', 'rsa_keygen_bits' => resource[:size]) NameError: undefined local variable or method `generate_key' for OpenSSL::PKey:Module
resource[:authentication] = :rsa
allow(OpenSSL::PKey::RSA).to receive(:new).with(2048).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('RSA', 'rsa_keygen_bits' => 2048).and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end
Expand All @@ -61,7 +60,7 @@
it 'creates with given size' do

Check failure on line 60 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when setting authentication to rsa when setting size creates with given size Failure/Error: OpenSSL::PKey.generate_key('RSA', 'rsa_keygen_bits' => resource[:size]) NameError: undefined local variable or method `generate_key' for OpenSSL::PKey:Module
resource[:authentication] = :rsa
resource[:size] = 1024
allow(OpenSSL::PKey::RSA).to receive(:new).with(1024).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('RSA', 'rsa_keygen_bits' => 1024).and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end
Expand All @@ -71,7 +70,7 @@
it 'creates with given password' do

Check failure on line 70 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when setting authentication to rsa when setting password creates with given password Failure/Error: OpenSSL::PKey.generate_key('RSA', 'rsa_keygen_bits' => resource[:size]) NameError: undefined local variable or method `generate_key' for OpenSSL::PKey:Module
resource[:authentication] = :rsa
resource[:password] = '2x$5{'
allow(OpenSSL::PKey::RSA).to receive(:new).with(2048).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('RSA', 'rsa_keygen_bits' => 2048).and_call_original
allow(OpenSSL::Cipher).to receive(:new).with('des3')
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
Expand All @@ -82,7 +81,8 @@
context 'when setting authentication to dsa' do
it 'creates a dsa key' do

Check failure on line 82 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when setting authentication to dsa creates a dsa key Failure/Error: params = OpenSSL::PKey.generate_parameters('DSA', 'dsa_paramgen_bits' => resource[:size]) NameError: undefined local variable or method `generate_parameters' for OpenSSL::PKey:Module
resource[:authentication] = :dsa
allow(OpenSSL::PKey::DSA).to receive(:new).with(2048).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_parameters).with('DSA', 'dsa_paramgen_bits' => 2048).and_call_original
expect(OpenSSL::PKey).to receive(:generate_key).with(kind_of(OpenSSL::PKey::DSA)).and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end
Expand All @@ -91,7 +91,8 @@
it 'creates with given size' do

Check failure on line 91 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when setting authentication to dsa when setting size creates with given size Failure/Error: params = OpenSSL::PKey.generate_parameters('DSA', 'dsa_paramgen_bits' => resource[:size]) NameError: undefined local variable or method `generate_parameters' for OpenSSL::PKey:Module
resource[:authentication] = :dsa
resource[:size] = 1024
allow(OpenSSL::PKey::DSA).to receive(:new).with(1024).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_parameters).with('DSA', 'dsa_paramgen_bits' => 1024).and_call_original
expect(OpenSSL::PKey).to receive(:generate_key).with(kind_of(OpenSSL::PKey::DSA)).and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end
Expand All @@ -101,7 +102,8 @@
it 'creates with given password' do

Check failure on line 102 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when setting authentication to dsa when setting password creates with given password Failure/Error: params = OpenSSL::PKey.generate_parameters('DSA', 'dsa_paramgen_bits' => resource[:size]) NameError: undefined local variable or method `generate_parameters' for OpenSSL::PKey:Module
resource[:authentication] = :dsa
resource[:password] = '2x$5{'
allow(OpenSSL::PKey::DSA).to receive(:new).with(2048).and_return(key)
expect(OpenSSL::PKey).to receive(:generate_parameters).with('DSA', 'dsa_paramgen_bits' => 2048).and_call_original
expect(OpenSSL::PKey).to receive(:generate_key).with(kind_of(OpenSSL::PKey::DSA)).and_call_original
allow(OpenSSL::Cipher).to receive(:new).with('des3')
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
Expand All @@ -110,20 +112,19 @@
end

context 'when setting authentication to ec' do
key = OpenSSL::PKey::EC.new('secp384r1').generate_key # For mocking

it 'creates an ec key' do

Check failure on line 115 in spec/unit/puppet/provider/ssl_pkey/openssl_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

The openssl provider for the ssl_pkey type when setting authentication to ec creates an ec key Failure/Error: OpenSSL::PKey.generate_key('EC', 'ec_paramgen_curve' => resource[:curve]) NameError: undefined local variable or method `generate_key' for OpenSSL::PKey:Module
resource[:authentication] = :ec
allow(OpenSSL::PKey::EC).to receive(:new).with('secp384r1').and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('EC', 'ec_paramgen_curve' => 'secp384r1').and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end

context 'when setting curve' do
it 'creates with given curve' do
resource[:authentication] = :ec
resource[:curve] = 'prime239v1'
allow(OpenSSL::PKey::EC).to receive(:new).with('prime239v1').and_return(key)
# See: openssl ecparam -list_curves
resource[:curve] = 'prime256v1'
expect(OpenSSL::PKey).to receive(:generate_key).with('EC', 'ec_paramgen_curve' => 'prime256v1').and_call_original
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
end
Expand All @@ -133,7 +134,7 @@
it 'creates with given password' do
resource[:authentication] = :ec
resource[:password] = '2x$5{'
allow(OpenSSL::PKey::EC).to receive(:new).with('secp384r1').and_return(key)
expect(OpenSSL::PKey).to receive(:generate_key).with('EC', 'ec_paramgen_curve' => 'secp384r1').and_call_original
allow(OpenSSL::Cipher).to receive(:new).with('des3')
expect(File).to receive(:write).with('/tmp/foo.key', kind_of(String))
resource.provider.create
Expand Down

0 comments on commit fd2cd0e

Please sign in to comment.