From a099bd9d4e5f0606f7c33003231a002446ed1e62 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 11 Sep 2019 17:03:14 +0500 Subject: [PATCH] Add columns X509Certificate#subject, #issuer --- app/interactors/create_x509_self_signed_certificate.rb | 2 ++ app/models/x509_certificate.rb | 4 ++++ db/migrate/20190911081459_create_x509_tables.rb | 2 ++ db/structure.sql | 2 ++ factories/x509_certificates.rb | 2 ++ .../create_x509_self_signed_certificate_spec.rb | 8 ++++++++ spec/models/x509_certificate_spec.rb | 8 ++++++++ 7 files changed, 28 insertions(+) diff --git a/app/interactors/create_x509_self_signed_certificate.rb b/app/interactors/create_x509_self_signed_certificate.rb index b9b1eef..a05dee8 100644 --- a/app/interactors/create_x509_self_signed_certificate.rb +++ b/app/interactors/create_x509_self_signed_certificate.rb @@ -11,6 +11,8 @@ class CreateX509SelfSignedCertificate def call context.certificate = X509Certificate.create!( pem: cert.to_pem.freeze, + subject: cert.subject.to_s, + issuer: cert.issuer.to_s, not_before: context.not_before, not_after: context.not_after, ) diff --git a/app/models/x509_certificate.rb b/app/models/x509_certificate.rb index fdcad50..4368992 100644 --- a/app/models/x509_certificate.rb +++ b/app/models/x509_certificate.rb @@ -13,6 +13,10 @@ class X509Certificate < ApplicationRecord validates :pem, presence: true + validates :subject, presence: true + + validates :issuer, presence: true + validates :not_before, presence: true validates :not_after, presence: true diff --git a/db/migrate/20190911081459_create_x509_tables.rb b/db/migrate/20190911081459_create_x509_tables.rb index 3deefd2..982f988 100644 --- a/db/migrate/20190911081459_create_x509_tables.rb +++ b/db/migrate/20190911081459_create_x509_tables.rb @@ -36,6 +36,8 @@ class CreateX509Tables < ActiveRecord::Migration[6.0] t.references :x509_certificate_request, null: true, foreign_key: true t.text :pem, null: false + t.string :subject, null: false + t.string :issuer, null: false t.datetime :not_before, null: false t.datetime :not_after, null: false end diff --git a/db/structure.sql b/db/structure.sql index cc90574..c79da79 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -925,6 +925,8 @@ CREATE TABLE public.x509_certificates ( updated_at timestamp(6) without time zone NOT NULL, x509_certificate_request_id bigint, pem text NOT NULL, + subject character varying NOT NULL, + issuer character varying NOT NULL, not_before timestamp without time zone NOT NULL, not_after timestamp without time zone NOT NULL ); diff --git a/factories/x509_certificates.rb b/factories/x509_certificates.rb index 5d37749..9fde9e6 100644 --- a/factories/x509_certificates.rb +++ b/factories/x509_certificates.rb @@ -3,6 +3,8 @@ FactoryBot.define do factory :self_signed_x509_certificate, class: X509Certificate do pem { File.read Rails.root.join 'fixtures', 'ca.crt' } + subject { '/CN=example.com' } + issuer { subject } not_before { Faker::Time.backward.utc } not_after { Faker::Time.forward.utc } end diff --git a/spec/interactors/create_x509_self_signed_certificate_spec.rb b/spec/interactors/create_x509_self_signed_certificate_spec.rb index 353ca40..825e7f7 100644 --- a/spec/interactors/create_x509_self_signed_certificate_spec.rb +++ b/spec/interactors/create_x509_self_signed_certificate_spec.rb @@ -37,6 +37,14 @@ RSpec.describe CreateX509SelfSignedCertificate do be_start_with "-----BEGIN CERTIFICATE-----\n" end + specify do + expect(subject.certificate.subject).to eq "/#{distinguished_name}" + end + + specify do + expect(subject.certificate.issuer).to eq "/#{distinguished_name}" + end + specify do expect(subject.certificate.not_before).to eq not_before end diff --git a/spec/models/x509_certificate_spec.rb b/spec/models/x509_certificate_spec.rb index b432b5c..9f3e6cf 100644 --- a/spec/models/x509_certificate_spec.rb +++ b/spec/models/x509_certificate_spec.rb @@ -25,6 +25,14 @@ RSpec.describe X509Certificate do end end + describe '#subject' do + it { is_expected.to validate_presence_of :subject } + end + + describe '#issuer' do + it { is_expected.to validate_presence_of :issuer } + end + describe '#not_before' do it { is_expected.to validate_presence_of :not_before } end