From dfee0e29ca6b9a049748ac5358c9a6b9b2652605 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 13 Sep 2019 17:52:03 +0500 Subject: [PATCH] Add column RSAPublicKey#public_key_der --- app/interactors/create_rsa_keys.rb | 1 + app/models/rsa_public_key.rb | 2 ++ db/migrate/20190911081459_create_x509_tables.rb | 4 +++- db/structure.sql | 8 ++++++++ factories/rsa_public_keys.rb | 1 + spec/models/rsa_public_key_spec.rb | 4 ++++ 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/interactors/create_rsa_keys.rb b/app/interactors/create_rsa_keys.rb index e3a06bc..5f77987 100644 --- a/app/interactors/create_rsa_keys.rb +++ b/app/interactors/create_rsa_keys.rb @@ -22,6 +22,7 @@ private 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 diff --git a/app/models/rsa_public_key.rb b/app/models/rsa_public_key.rb index a4a6489..4c7f79d 100644 --- a/app/models/rsa_public_key.rb +++ b/app/models/rsa_public_key.rb @@ -9,6 +9,8 @@ class RSAPublicKey < ApplicationRecord validates :public_key_pem, presence: true + validates :public_key_der, presence: true + validates :bits, inclusion: { in: [2048, 4096] } validates :sha1, diff --git a/db/migrate/20190911081459_create_x509_tables.rb b/db/migrate/20190911081459_create_x509_tables.rb index 23b44fa..e857b13 100644 --- a/db/migrate/20190911081459_create_x509_tables.rb +++ b/db/migrate/20190911081459_create_x509_tables.rb @@ -7,7 +7,8 @@ class CreateX509Tables < ActiveRecord::Migration[6.0] create_table :rsa_public_keys do |t| t.timestamps null: false - t.text :public_key_pem, null: false + t.text :public_key_pem, null: false + t.binary :public_key_der, null: false t.binary :private_key_pem_iv t.binary :private_key_pem_ciphertext @@ -17,6 +18,7 @@ class CreateX509Tables < ActiveRecord::Migration[6.0] t.string :sha256, null: false t.index :public_key_pem, unique: true + t.index :public_key_der, unique: true t.index :sha1, unique: true t.index :sha256, unique: true end diff --git a/db/structure.sql b/db/structure.sql index 7976abe..03b4e6b 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -730,6 +730,7 @@ CREATE TABLE public.rsa_public_keys ( created_at timestamp(6) without time zone NOT NULL, updated_at timestamp(6) without time zone NOT NULL, public_key_pem text NOT NULL, + public_key_der bytea NOT NULL, private_key_pem_iv bytea, private_key_pem_ciphertext bytea, bits integer NOT NULL, @@ -1459,6 +1460,13 @@ CREATE INDEX index_relationships_on_role ON public.relationships USING btree (ro CREATE INDEX index_relationships_on_status ON public.relationships USING btree (status); +-- +-- Name: index_rsa_public_keys_on_public_key_der; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX index_rsa_public_keys_on_public_key_der ON public.rsa_public_keys USING btree (public_key_der); + + -- -- Name: index_rsa_public_keys_on_public_key_pem; Type: INDEX; Schema: public; Owner: - -- diff --git a/factories/rsa_public_keys.rb b/factories/rsa_public_keys.rb index 5eddf1f..9140130 100644 --- a/factories/rsa_public_keys.rb +++ b/factories/rsa_public_keys.rb @@ -3,6 +3,7 @@ FactoryBot.define do factory :rsa_public_key do public_key_pem { OpenSSL::PKey::RSA.new(bits).public_key.to_pem } + public_key_der { OpenSSL::PKey::RSA.new(bits).public_key.to_der } bits { [2048, 4096].sample } sha1 { Digest::SHA1.hexdigest SecureRandom.hex } sha256 { Digest::SHA256.hexdigest SecureRandom.hex } diff --git a/spec/models/rsa_public_key_spec.rb b/spec/models/rsa_public_key_spec.rb index e83c8a0..20c7014 100644 --- a/spec/models/rsa_public_key_spec.rb +++ b/spec/models/rsa_public_key_spec.rb @@ -9,6 +9,10 @@ RSpec.describe RSAPublicKey do it { is_expected.to validate_presence_of :public_key_pem } end + describe '#public_key_der' do + it { is_expected.to validate_presence_of :public_key_der } + end + describe '#bits' do it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) } end