From d8cc171cb28a427cd8f20f848bcefa12f9296013 Mon Sep 17 00:00:00 2001 From: Ahmad Farhat <ahmad.af.farhat@gmail.com> Date: Tue, 14 Feb 2023 09:51:56 -0500 Subject: [PATCH] Added configuration:check rake task that checks a configuration (#4785) * Added configuration:check rake task that checks a configuration * CR --- config/environments/development.rb | 6 +-- config/environments/production.rb | 8 ++-- lib/tasks/configuration.rake | 72 ++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 lib/tasks/configuration.rake diff --git a/config/environments/development.rb b/config/environments/development.rb index 05b73a56..60ca2b38 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -48,9 +48,9 @@ Rails.application.configure do user_name: ENV.fetch('SMTP_USERNAME', nil), password: ENV.fetch('SMTP_PASSWORD', nil), authentication: ENV.fetch('SMTP_AUTH', nil), - enable_starttls_auto: ENV.fetch('SMTP_STARTTLS_AUTO', true), - enable_starttls: ENV.fetch('SMTP_STARTTLS', false), - tls: ENV.fetch('SMTP_TLS', 'false') != 'false', + enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', 'true')), + enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', 'false')), + tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', 'false')), openssl_verify_mode: ENV.fetch('SMTP_SSL_VERIFY', 'true') == 'false' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER } config.action_mailer.default_options = { diff --git a/config/environments/production.rb b/config/environments/production.rb index b513454c..36e814f9 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -58,9 +58,9 @@ Rails.application.configure do user_name: ENV.fetch('SMTP_USERNAME'), password: ENV.fetch('SMTP_PASSWORD'), authentication: ENV.fetch('SMTP_AUTH'), - enable_starttls_auto: ENV.fetch('SMTP_STARTTLS_AUTO', true), - enable_starttls: ENV.fetch('SMTP_STARTTLS', false), - tls: ENV.fetch('SMTP_TLS', 'false') != 'false', + enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', 'true')), + enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', 'false')), + tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', 'false')), openssl_verify_mode: ENV.fetch('SMTP_SSL_VERIFY', 'true') == 'false' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER } config.action_mailer.default_options = { @@ -70,7 +70,7 @@ Rails.application.configure do config.action_mailer.perform_deliveries = false end - config.action_mailer.raise_delivery_errors = false + config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_caching = false config.action_mailer.deliver_later_queue_name = 'mailing' diff --git a/lib/tasks/configuration.rake b/lib/tasks/configuration.rake new file mode 100644 index 00000000..89334909 --- /dev/null +++ b/lib/tasks/configuration.rake @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require_relative 'task_helpers' + +namespace :configuration do + desc 'Checks that the application was configured correctly' + task check: :environment do + required_env_vars = %w[SECRET_KEY_BASE BIGBLUEBUTTON_ENDPOINT BIGBLUEBUTTON_SECRET DATABASE_URL REDIS_URL].freeze + + # Initial check that variables are set + info 'Checking required environment variables:' + required_env_vars.each do |var| + failed("#{var} not set correctly") if ENV[var].blank? + end + passed + + info 'Checking connection to Postgres Database:' + begin + ActiveRecord::Base.establish_connection # Establishes connection + ActiveRecord::Base.connection # Calls connection object + failed('Unable to connect to Database') unless ActiveRecord::Base.connected? + rescue StandardError => e + failed("Unable to connect to Database - #{e}") + end + passed + + info 'Checking connection to Redis Cache:' + begin + Redis.new.ping + rescue StandardError => e + failed("Unable to connect to Redis - #{e}") + end + passed + + info 'Checking connection to BigBlueButton:' + test_request(Rails.configuration.bigbluebutton_endpoint) + checksum = Digest::SHA1.hexdigest("getMeetings#{Rails.configuration.bigbluebutton_secret}") + test_request("#{Rails.configuration.bigbluebutton_endpoint}getMeetings?checksum=#{checksum}") + passed + + if ENV['SMTP_SERVER'].present? + info 'Checking connection to SMTP Server' + begin + UserMailer.with(to: ENV.fetch('SMTP_SENDER_EMAIL', nil), subject: ENV.fetch('SMTP_SENDER_EMAIL', nil)).test_email.deliver_now + rescue StandardError => e + failed("Unable to connect to SMTP Server - #{e}") + end + passed + end + + exit 0 + end +end + +# Takes the full URL including the protocol +def test_request(url) + uri = URI(url) + res = Net::HTTP.get(uri) + + doc = Nokogiri::XML(res) + failed("Could not get a valid response from BigBlueButton server - #{res}") if doc.css('returncode').text != 'SUCCESS' +rescue StandardError => e + failed("Error connecting to BigBlueButton server - #{e}") +end + +def passed + success 'Passed' +end + +def failed(msg) + err "Failed - #{msg}" +end -- GitLab