From 0d0d4e7c75a6e198bcb176db1f78dff6f75d8943 Mon Sep 17 00:00:00 2001 From: Jarrett Lusso Date: Fri, 6 Sep 2024 12:11:46 -0400 Subject: [PATCH] Add Mongoid Support This is a first attempt at adding mongoid support. It currently breaks AR support. --- Appraisals | 2 ++ .../lets_encrypt/verifications_controller.rb | 2 +- app/models/lets_encrypt/certificate.rb | 19 ++++++++++++++----- gemfiles/rails6.gemfile | 1 + gemfiles/rails6.gemfile.lock | 16 +++++++++++++--- gemfiles/rails7.gemfile | 1 + gemfiles/rails7.gemfile.lock | 16 +++++++++++++--- rails-letsencrypt.gemspec | 1 + spec/dummy/config/application.rb | 1 + spec/dummy/config/mongoid.yml | 10 ++++++++++ spec/spec_helper.rb | 4 ++++ 11 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 spec/dummy/config/mongoid.yml diff --git a/Appraisals b/Appraisals index 8312aee..888367f 100644 --- a/Appraisals +++ b/Appraisals @@ -8,6 +8,7 @@ appraise 'rails6' do gem 'activemodel', '~> 6.1' gem 'activerecord', '~> 6.1' gem 'activesupport', '~> 6.1' + gem 'mongoid', '~> 8' end appraise 'rails7' do @@ -18,4 +19,5 @@ appraise 'rails7' do gem 'activemodel', '~> 7' gem 'activerecord', '~> 7' gem 'activesupport', '~> 7' + gem 'mongoid', '~> 8' end diff --git a/app/controllers/lets_encrypt/verifications_controller.rb b/app/controllers/lets_encrypt/verifications_controller.rb index fe09304..e957a00 100644 --- a/app/controllers/lets_encrypt/verifications_controller.rb +++ b/app/controllers/lets_encrypt/verifications_controller.rb @@ -18,7 +18,7 @@ def render_verification_string end def certificate - LetsEncrypt.certificate_model.find_by(verification_path: filename) + LetsEncrypt.certificate_model.where(verification_path: filename).first end def filename diff --git a/app/models/lets_encrypt/certificate.rb b/app/models/lets_encrypt/certificate.rb index d634a98..f0c29b9 100644 --- a/app/models/lets_encrypt/certificate.rb +++ b/app/models/lets_encrypt/certificate.rb @@ -22,17 +22,26 @@ module LetsEncrypt # index_letsencrypt_certificates_on_domain (domain) # index_letsencrypt_certificates_on_renew_after (renew_after) # - class Certificate < ApplicationRecord + class Certificate + include Mongoid::Document + include Mongoid::Timestamps include CertificateVerifiable include CertificateIssuable - self.table_name = 'letsencrypt_certificates' + field :domain, type: String + field :certificate, type: String + field :intermediaries, type: String + field :key, type: String + field :expires_at, type: DateTime + field :renew_after, type: DateTime + field :verification_path, type: String + field :verification_string, type: String validates :domain, presence: true, uniqueness: true - scope :active, -> { where('certificate IS NOT NULL AND expires_at > ?', Time.zone.now) } - scope :renewable, -> { where('renew_after IS NULL OR renew_after <= ?', Time.zone.now) } - scope :expired, -> { where('expires_at <= ?', Time.zone.now) } + scope :active, -> { where(:certificate.ne => nil, :expires_at.gt => Time.zone.now) } + scope :renewable, -> { self.or({ :renew_after => nil }, { :renew_after.lte => Time.zone.now }) } + scope :expired, -> { where(:expires_at.lte => Time.zone.now) } before_create -> { self.key = OpenSSL::PKey::RSA.new(4096).to_s } after_destroy -> { delete_from_redis }, if: -> { LetsEncrypt.config.use_redis? && active? } diff --git a/gemfiles/rails6.gemfile b/gemfiles/rails6.gemfile index da92a6e..f907aa2 100644 --- a/gemfiles/rails6.gemfile +++ b/gemfiles/rails6.gemfile @@ -15,5 +15,6 @@ gem "actionview", "~> 6.1" gem "activemodel", "~> 6.1" gem "activerecord", "~> 6.1" gem "activesupport", "~> 6.1" +gem "mongoid", "~> 8" gemspec path: "../" diff --git a/gemfiles/rails6.gemfile.lock b/gemfiles/rails6.gemfile.lock index 0f0c899..16ea13d 100644 --- a/gemfiles/rails6.gemfile.lock +++ b/gemfiles/rails6.gemfile.lock @@ -9,6 +9,7 @@ PATH activemodel (>= 6.1) activerecord (>= 6.1) activesupport (>= 6.1) + mongoid (>= 8) railties (>= 6.1) redis @@ -58,6 +59,7 @@ GEM thor (>= 0.14.0) ast (2.4.2) base64 (0.2.0) + bson (5.0.1) builder (3.2.4) concurrent-ruby (1.2.2) connection_pool (2.4.1) @@ -91,14 +93,21 @@ GEM mini_mime (1.1.5) mini_portile2 (2.8.5) minitest (5.20.0) - net-imap (0.4.4) + mongo (2.20.1) + bson (>= 4.14.1, < 6.0.0) + mongoid (8.1.5) + activemodel (>= 5.1, < 7.2, != 7.0.0) + concurrent-ruby (>= 1.0.5, < 2.0) + mongo (>= 2.18.0, < 3.0.0) + ruby2_keywords (~> 0.0.5) + net-imap (0.4.16) date net-protocol net-pop (0.1.2) net-protocol net-protocol (0.2.2) timeout - net-smtp (0.4.0) + net-smtp (0.5.0) net-protocol nokogiri (1.15.4) mini_portile2 (~> 2.8.2) @@ -205,6 +214,7 @@ DEPENDENCIES activerecord (~> 6.1) activesupport (~> 6.1) appraisal + mongoid (~> 8) rails-letsencrypt! railties (~> 6.1) rspec-rails @@ -214,4 +224,4 @@ DEPENDENCIES sqlite3 BUNDLED WITH - 2.4.13 + 2.3.18 diff --git a/gemfiles/rails7.gemfile b/gemfiles/rails7.gemfile index 7d4b25d..3e84930 100644 --- a/gemfiles/rails7.gemfile +++ b/gemfiles/rails7.gemfile @@ -15,5 +15,6 @@ gem "actionview", "~> 7" gem "activemodel", "~> 7" gem "activerecord", "~> 7" gem "activesupport", "~> 7" +gem "mongoid", "~> 8" gemspec path: "../" diff --git a/gemfiles/rails7.gemfile.lock b/gemfiles/rails7.gemfile.lock index 3182a76..bd05141 100644 --- a/gemfiles/rails7.gemfile.lock +++ b/gemfiles/rails7.gemfile.lock @@ -9,6 +9,7 @@ PATH activemodel (>= 6.1) activerecord (>= 6.1) activesupport (>= 6.1) + mongoid (>= 8) railties (>= 6.1) redis @@ -70,6 +71,7 @@ GEM ast (2.4.2) base64 (0.2.0) bigdecimal (3.1.6) + bson (5.0.1) builder (3.2.4) concurrent-ruby (1.2.3) connection_pool (2.4.1) @@ -108,15 +110,22 @@ GEM mini_mime (1.1.5) mini_portile2 (2.8.5) minitest (5.22.2) + mongo (2.20.1) + bson (>= 4.14.1, < 6.0.0) + mongoid (8.1.5) + activemodel (>= 5.1, < 7.2, != 7.0.0) + concurrent-ruby (>= 1.0.5, < 2.0) + mongo (>= 2.18.0, < 3.0.0) + ruby2_keywords (~> 0.0.5) mutex_m (0.2.0) - net-imap (0.4.10) + net-imap (0.4.16) date net-protocol net-pop (0.1.2) net-protocol net-protocol (0.2.2) timeout - net-smtp (0.4.0.1) + net-smtp (0.5.0) net-protocol nokogiri (1.15.4) mini_portile2 (~> 2.8.2) @@ -238,6 +247,7 @@ DEPENDENCIES activerecord (~> 7) activesupport (~> 7) appraisal + mongoid (~> 8) rails-letsencrypt! railties (~> 7) rspec-rails @@ -247,4 +257,4 @@ DEPENDENCIES sqlite3 BUNDLED WITH - 2.4.13 + 2.3.18 diff --git a/rails-letsencrypt.gemspec b/rails-letsencrypt.gemspec index fd7ac85..245e114 100644 --- a/rails-letsencrypt.gemspec +++ b/rails-letsencrypt.gemspec @@ -27,6 +27,7 @@ Gem::Specification.new do |s| s.add_dependency 'activemodel', '>= 6.1' s.add_dependency 'activerecord', '>= 6.1' s.add_dependency 'activesupport', '>= 6.1' + s.add_dependency 'mongoid', '>= 8' s.add_dependency 'railties', '>= 6.1' s.add_dependency 'redis' s.metadata['rubygems_mfa_required'] = 'true' diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index 0b13b70..507adfc 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -5,6 +5,7 @@ require_relative 'boot' require 'rails/all' +require 'mongoid' Bundler.require(*Rails.groups) require 'rails-letsencrypt' diff --git a/spec/dummy/config/mongoid.yml b/spec/dummy/config/mongoid.yml new file mode 100644 index 0000000..4467a74 --- /dev/null +++ b/spec/dummy/config/mongoid.yml @@ -0,0 +1,10 @@ +test: + clients: + default: + database: rails_letsencrypt_test + hosts: + - localhost:27017 + options: + read: + mode: :primary + max_pool_size: 1 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f8a6674..e9e420b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -39,6 +39,10 @@ end RSpec.configure do |config| + config.before(:each) do + Mongoid.purge! + end + # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer.