1
0
Fork 0

Allow to set password

This commit is contained in:
Alex Kotov 2019-09-14 02:28:55 +05:00
parent f354cdbe27
commit 2979c7a38c
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 123 additions and 20 deletions

View file

@ -16,20 +16,47 @@ class CreateRSAKeys
private private
def attributes def pkey
pkey = OpenSSL::PKey::RSA.new BITS @pkey ||= OpenSSL::PKey::RSA.new BITS
end
def attributes
{ {
account: context.account, account: context.account,
public_key_pem: public_key_pem,
public_key_der: public_key_der,
private_key_pem: private_key_pem,
has_password: context.password.present?,
bits: BITS, bits: BITS,
sha1: sha1,
sha1: Digest::SHA1.hexdigest(pkey.public_key.to_der), sha256: sha256,
sha256: Digest::SHA256.hexdigest(pkey.public_key.to_der),
public_key_pem: pkey.public_key.to_pem.freeze,
public_key_der: pkey.public_key.to_der.freeze,
private_key_pem: pkey.to_pem.freeze,
} }
end end
def sha1
@sha1 ||= Digest::SHA1.hexdigest(pkey.public_key.to_der).freeze
end
def sha256
@sha256 ||= Digest::SHA256.hexdigest(pkey.public_key.to_der).freeze
end
def public_key_pem
@public_key_pem ||= pkey.public_key.to_pem.freeze
end
def public_key_der
@public_key_der ||= pkey.public_key.to_der.freeze
end
def private_key_pem
@private_key_pem ||=
if context.password.present?
pkey.to_pem(OpenSSL::Cipher::AES256.new, context.password).freeze
else
pkey.to_pem.freeze
end
end
end end

View file

@ -22,13 +22,17 @@ class CreateX509SelfSignedCertificate
private private
def private_key_pkey def private_key_pkey
@private_key_pkey ||= @private_key_pkey ||= OpenSSL::PKey::RSA.new(
OpenSSL::PKey::RSA.new context.asymmetric_key.private_key_pem context.asymmetric_key.private_key_pem,
String(context.password),
)
end end
def public_key_pkey def public_key_pkey
@public_key_pkey ||= @public_key_pkey ||= OpenSSL::PKey::RSA.new(
OpenSSL::PKey::RSA.new context.asymmetric_key.public_key_pem context.asymmetric_key.public_key_pem,
String(context.password),
)
end end
def subject def subject

View file

@ -6,6 +6,7 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
subject do subject do
described_class.call( described_class.call(
account: account, account: account,
password: password,
distinguished_name: distinguished_name, distinguished_name: distinguished_name,
not_before: not_before, not_before: not_before,
not_after: not_after, not_after: not_after,
@ -13,6 +14,7 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
end end
let(:account) { create :initial_account } let(:account) { create :initial_account }
let(:password) { Faker::Internet.password }
let(:distinguished_name) { "CN=#{Faker::Internet.domain_name}" } let(:distinguished_name) { "CN=#{Faker::Internet.domain_name}" }
let(:not_before) { Faker::Time.backward.utc } let(:not_before) { Faker::Time.backward.utc }
let(:not_after) { Faker::Time.forward.utc } let(:not_after) { Faker::Time.forward.utc }
@ -42,6 +44,10 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
expect(subject.asymmetric_key).to be_instance_of RSAKey expect(subject.asymmetric_key).to be_instance_of RSAKey
end end
specify do
expect(subject.asymmetric_key.has_password).to equal true
end
specify do specify do
expect(subject.certificate).to be_instance_of X509Certificate expect(subject.certificate).to be_instance_of X509Certificate
end end
@ -65,4 +71,20 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
expect(subject.asymmetric_key.account).to equal nil expect(subject.asymmetric_key.account).to equal nil
end end
end end
context 'when password is nil' do
let(:password) { nil }
specify do
expect(subject.asymmetric_key.has_password).to equal false
end
end
context 'when password is blank' do
let(:password) { ' ' * rand(1..3) }
specify do
expect(subject.asymmetric_key.has_password).to equal false
end
end
end end

View file

@ -3,9 +3,10 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe CreateRSAKeys do RSpec.describe CreateRSAKeys do
subject { described_class.call account: account } subject { described_class.call account: account, password: password }
let(:account) { create :initial_account } let(:account) { create :initial_account }
let(:password) { Faker::Internet.password }
specify do specify do
expect { subject }.to change(AsymmetricKey, :count).by(1) expect { subject }.to change(AsymmetricKey, :count).by(1)
@ -28,6 +29,10 @@ RSpec.describe CreateRSAKeys do
expect(subject.asymmetric_key).to be_instance_of RSAKey expect(subject.asymmetric_key).to be_instance_of RSAKey
end end
specify do
expect(subject.asymmetric_key.has_password).to equal true
end
specify do specify do
expect(subject.asymmetric_key.sha1).not_to be_blank expect(subject.asymmetric_key.sha1).not_to be_blank
end end
@ -50,13 +55,19 @@ RSpec.describe CreateRSAKeys do
specify do specify do
expect do expect do
OpenSSL::PKey::RSA.new subject.asymmetric_key.private_key_pem OpenSSL::PKey::RSA.new(
subject.asymmetric_key.private_key_pem,
String(password),
)
end.not_to raise_error end.not_to raise_error
end end
specify do specify do
expect do expect do
OpenSSL::PKey::RSA.new subject.asymmetric_key.public_key_pem OpenSSL::PKey::RSA.new(
subject.asymmetric_key.public_key_pem,
String(password),
)
end.not_to \ end.not_to \
raise_error raise_error
end end
@ -64,7 +75,10 @@ RSpec.describe CreateRSAKeys do
specify do specify do
expect(subject.asymmetric_key.sha1).to eq( expect(subject.asymmetric_key.sha1).to eq(
Digest::SHA1.hexdigest( Digest::SHA1.hexdigest(
OpenSSL::PKey::RSA.new(subject.asymmetric_key.public_key_pem).to_der, OpenSSL::PKey::RSA.new(
subject.asymmetric_key.public_key_pem,
String(password),
).to_der,
), ),
) )
end end
@ -72,14 +86,20 @@ RSpec.describe CreateRSAKeys do
specify do specify do
expect(subject.asymmetric_key.sha256).to eq( expect(subject.asymmetric_key.sha256).to eq(
Digest::SHA256.hexdigest( Digest::SHA256.hexdigest(
OpenSSL::PKey::RSA.new(subject.asymmetric_key.public_key_pem).to_der, OpenSSL::PKey::RSA.new(
subject.asymmetric_key.public_key_pem,
String(password),
).to_der,
), ),
) )
end end
specify do specify do
expect(subject.asymmetric_key.public_key_pem).to eq( expect(subject.asymmetric_key.public_key_pem).to eq(
OpenSSL::PKey::RSA.new(subject.asymmetric_key.private_key_pem) OpenSSL::PKey::RSA.new(
subject.asymmetric_key.private_key_pem,
String(password),
)
.public_key.to_pem, .public_key.to_pem,
) )
end end
@ -113,4 +133,34 @@ RSpec.describe CreateRSAKeys do
expect(subject.asymmetric_key.account).to equal nil expect(subject.asymmetric_key.account).to equal nil
end end
end end
context 'when password is nil' do
let(:password) { nil }
specify do
expect(subject.asymmetric_key.has_password).to equal false
end
end
context 'when password is blank' do
let(:password) { ' ' * rand(1..3) }
specify do
expect(subject.asymmetric_key.has_password).to equal false
end
end
context 'when password.to_s returns nil' do
let :password do
Class.new do
def to_s
nil
end
end.new
end
specify do
expect { subject }.to raise_error TypeError
end
end
end end