From 689e8ddc83d0c7ce6e44dc901d693d289a059731 Mon Sep 17 00:00:00 2001 From: Rosa Gutierrez Date: Mon, 2 Dec 2024 21:24:20 +0100 Subject: [PATCH] Convert authentication generator to Rails command Generators require the whole app to be booted, which involves having access to the DB, loading all controlers, etc. which we don't need for just generating the credentials, and might not work when trying to add production credentials because many apps aren't prepared to boot for production in another environment. --- README.md | 4 ++-- .../jobs/authentication.rb} | 10 ++++++++-- lib/mission_control/jobs/tasks.rb | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) rename lib/{generators/mission_control/jobs/authentication_generator.rb => mission_control/jobs/authentication.rb} (91%) diff --git a/README.md b/README.md index 6c44f001..49fcc5e8 100644 --- a/README.md +++ b/README.md @@ -68,12 +68,12 @@ mission_control: If no credentials are configured, Mission Control won't be accessible. To set these up, you can run the generator provided like this: ``` -bin/rails mission_control:jobs:authentication:init +bin/rails mission_control:jobs:authentication:configure ``` To set them up for different environments you can use the `RAILS_ENV` environment variable, like this: ``` -RAILS_ENV=production bin/rails mission_control:jobs:authentication:init +RAILS_ENV=production bin/rails mission_control:jobs:authentication:configure ``` #### Custom authentication diff --git a/lib/generators/mission_control/jobs/authentication_generator.rb b/lib/mission_control/jobs/authentication.rb similarity index 91% rename from lib/generators/mission_control/jobs/authentication_generator.rb rename to lib/mission_control/jobs/authentication.rb index c29eb72a..3398b6dc 100644 --- a/lib/generators/mission_control/jobs/authentication_generator.rb +++ b/lib/mission_control/jobs/authentication.rb @@ -1,5 +1,9 @@ -class MissionControl::Jobs::AuthenticationGenerator < Rails::Generators::Base - def init +class MissionControl::Jobs::Authentication < Rails::Command::Base + def self.configure + new.configure + end + + def configure if credentials_accessible? if authentication_configured? say "HTTP Basic Authentication is already configured for `#{Rails.env}`. You can edit it using `credentials:edit`" @@ -26,6 +30,8 @@ def init end private + attr_reader :environment + def credentials_accessible? credentials.read.present? end diff --git a/lib/mission_control/jobs/tasks.rb b/lib/mission_control/jobs/tasks.rb index b1fd03ee..fb8edeab 100644 --- a/lib/mission_control/jobs/tasks.rb +++ b/lib/mission_control/jobs/tasks.rb @@ -1,8 +1,8 @@ namespace :mission_control do namespace :jobs do desc "Configure HTTP Basic Authentication" - task "authentication:init" do - Rails::Command.invoke :generate, [ "mission_control:jobs:authentication" ] + task "authentication:configure" => :environment do + MissionControl::Jobs::Authentication.configure end end end