From f354cdbe27db6651898783af4f563d1b7d505166 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 14 Sep 2019 01:48:33 +0500 Subject: [PATCH] Add attribute AsymmetricKey#has_password --- app/models/rsa_key.rb | 2 ++ db/migrate/20190911081459_create_x509_tables.rb | 7 ++++--- db/structure.sql | 1 + factories/rsa_keys.rb | 2 ++ spec/models/rsa_key_spec.rb | 4 ++++ 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models/rsa_key.rb b/app/models/rsa_key.rb index ddd4e37..cb9ea26 100644 --- a/app/models/rsa_key.rb +++ b/app/models/rsa_key.rb @@ -21,6 +21,8 @@ class RSAKey < AsymmetricKey presence: true, uniqueness: true + validates :has_password, exclusion: { in: [nil] } + 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 03e57f0..ae09752 100644 --- a/db/migrate/20190911081459_create_x509_tables.rb +++ b/db/migrate/20190911081459_create_x509_tables.rb @@ -17,9 +17,10 @@ class CreateX509Tables < ActiveRecord::Migration[6.0] t.binary :private_key_pem_iv t.binary :private_key_pem_ciphertext - t.integer :bits, null: false - t.string :sha1, null: false - t.string :sha256, null: false + t.boolean :has_password, null: false + t.integer :bits, null: false + t.string :sha1, null: false + t.string :sha256, null: false t.index :public_key_pem, unique: true t.index :public_key_der, unique: true diff --git a/db/structure.sql b/db/structure.sql index e6cd275..b071baa 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -378,6 +378,7 @@ CREATE TABLE public.asymmetric_keys ( public_key_der bytea NOT NULL, private_key_pem_iv bytea, private_key_pem_ciphertext bytea, + has_password boolean NOT NULL, bits integer NOT NULL, sha1 character varying NOT NULL, sha256 character varying NOT NULL, diff --git a/factories/rsa_keys.rb b/factories/rsa_keys.rb index b3fdb83..7fe53ab 100644 --- a/factories/rsa_keys.rb +++ b/factories/rsa_keys.rb @@ -6,6 +6,8 @@ FactoryBot.define 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 } + + has_password { [false, true].sample } bits { [2048, 4096].sample } sha1 { Digest::SHA1.hexdigest SecureRandom.hex } sha256 { Digest::SHA256.hexdigest SecureRandom.hex } diff --git a/spec/models/rsa_key_spec.rb b/spec/models/rsa_key_spec.rb index 87fcc68..2a4056d 100644 --- a/spec/models/rsa_key_spec.rb +++ b/spec/models/rsa_key_spec.rb @@ -22,6 +22,10 @@ RSpec.describe RSAKey do it { is_expected.to validate_uniqueness_of :public_key_der } end + describe '#has_password' do + it { is_expected.to validate_exclusion_of(:has_password).in_array([nil]) } + end + describe '#bits' do it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) } end