1
0
Fork 0

Add virtual attribute RSAPublicKey#private_key_pem

This commit is contained in:
Alex Kotov 2019-09-12 07:10:50 +05:00
parent 3df7acd095
commit 4f41d2d0e4
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
8 changed files with 16 additions and 26 deletions

View file

@ -9,11 +9,10 @@ class CreateRSAKeys
before :set_ciphertext
def call
context.private_key_pem = @pkey.to_pem.freeze
context.public_key = RSAPublicKey.create!(
bits: BITS,
public_key_pem: @pkey.public_key.to_pem.freeze,
private_key_pem: @pkey.to_pem.freeze,
private_key_pem_iv: @iv,
private_key_pem_secret: @key,
private_key_pem_ciphertext: @ciphertext,

View file

@ -14,7 +14,8 @@ class CreateX509CertificateRequest
private
def private_key_pkey
@private_key_pkey ||= OpenSSL::PKey::RSA.new context.private_key_pem
@private_key_pkey ||=
OpenSSL::PKey::RSA.new context.public_key.private_key_pem
end
def public_key_pkey

View file

@ -22,7 +22,8 @@ class CreateX509SelfSignedCertificate
private
def private_key_pkey
@private_key_pkey ||= OpenSSL::PKey::RSA.new context.private_key_pem
@private_key_pkey ||=
OpenSSL::PKey::RSA.new context.public_key.private_key_pem
end
def public_key_pkey

View file

@ -6,7 +6,7 @@ class DecryptRSAPrivateKey
before :set_cipher
def call
context.private_key_pem_cleartext = [
context.public_key.private_key_pem = [
@cipher.update(context.public_key.private_key_pem_ciphertext),
@cipher.final,
].join.freeze

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class RSAPublicKey < ApplicationRecord
attr_accessor :private_key_pem_secret
attr_accessor :private_key_pem, :private_key_pem_secret
###############
# Validations #

View file

@ -23,10 +23,6 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
expect { subject }.to change(X509Certificate, :count).by(1)
end
specify do
expect(subject.private_key_pem).to be_instance_of String
end
specify do
expect(subject.public_key).to be_instance_of RSAPublicKey
end
@ -36,7 +32,7 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
end
specify do
expect(subject.private_key_pem).not_to be_blank
expect(subject.public_key.private_key_pem).not_to be_blank
end
specify do

View file

@ -9,20 +9,12 @@ RSpec.describe CreateRSAKeys do
expect { subject }.to change(RSAPublicKey, :count).by(1)
end
specify do
expect(subject.private_key_pem).to be_instance_of String
end
specify do
expect(subject.public_key).to be_instance_of RSAPublicKey
end
specify do
expect(subject.private_key_pem).to be_frozen
end
specify do
expect(subject.private_key_pem).not_to be_blank
expect(subject.public_key.private_key_pem).not_to be_blank
end
specify do
@ -30,7 +22,9 @@ RSpec.describe CreateRSAKeys do
end
specify do
expect { OpenSSL::PKey::RSA.new subject.private_key_pem }.not_to raise_error
expect do
OpenSSL::PKey::RSA.new subject.public_key.private_key_pem
end.not_to raise_error
end
specify do
@ -39,8 +33,9 @@ RSpec.describe CreateRSAKeys do
end
specify do
expect(subject.public_key.public_key_pem).to \
eq OpenSSL::PKey::RSA.new(subject.private_key_pem).public_key.to_pem
expect(subject.public_key.public_key_pem).to eq(
OpenSSL::PKey::RSA.new(subject.public_key.private_key_pem).public_key.to_pem,
)
end
specify do
@ -62,6 +57,6 @@ RSpec.describe CreateRSAKeys do
cipher.final,
].join.freeze
expect(cleartext).to eq subject.private_key_pem
expect(cleartext).to eq subject.public_key.private_key_pem
end
end

View file

@ -5,14 +5,12 @@ require 'rails_helper'
RSpec.describe CreateX509CertificateRequest do
subject do
described_class.call(
private_key_pem: private_key_pem,
public_key: public_key,
distinguished_name: distinguished_name,
)
end
let(:rsa_keys) { CreateRSAKeys.call }
let(:private_key_pem) { rsa_keys.private_key_pem }
let(:public_key) { rsa_keys.public_key }
let(:distinguished_name) { "CN=#{Faker::Internet.domain_name}" }