Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OpenSSL::PKey.generate_pkey instead of OpenSSL::PKey::Algo #220

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
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 @@ -23,25 +22,25 @@
end

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
end

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,28 +49,28 @@
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

context 'when setting size' do
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
end

context 'when setting password' do
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 @@ -80,28 +79,31 @@
end

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

context 'when setting size' do
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
end

context 'when setting password' do
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
Loading