Skip to content

Commit

Permalink
Convert authentication generator to Rails command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rosa committed Dec 2, 2024
1 parent d54b2a4 commit 689e8dd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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`"
Expand All @@ -26,6 +30,8 @@ def init
end

private
attr_reader :environment

def credentials_accessible?
credentials.read.present?
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mission_control/jobs/tasks.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 689e8dd

Please sign in to comment.