-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from rails/http-auth
Provide HTTP Basic authentication closed by default
- Loading branch information
Showing
13 changed files
with
213 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/controllers/concerns/mission_control/jobs/basic_authentication.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module MissionControl::Jobs::BasicAuthentication | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :authenticate_by_http_basic | ||
end | ||
|
||
private | ||
def authenticate_by_http_basic | ||
if http_basic_authentication_enabled? | ||
if http_basic_authentication_configured? | ||
http_basic_authenticate_or_request_with(**http_basic_authentication_credentials) | ||
else | ||
head :unauthorized | ||
end | ||
end | ||
end | ||
|
||
def http_basic_authentication_enabled? | ||
MissionControl::Jobs.http_basic_auth_enabled | ||
end | ||
|
||
def http_basic_authentication_configured? | ||
http_basic_authentication_credentials.values.all?(&:present?) | ||
end | ||
|
||
def http_basic_authentication_credentials | ||
{ | ||
name: MissionControl::Jobs.http_basic_auth_user, | ||
password: MissionControl::Jobs.http_basic_auth_password | ||
}.transform_values(&:presence) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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`" | ||
else | ||
say "Setting up credentials for HTTP Basic Authentication for `#{Rails.env}` environment." | ||
say "" | ||
|
||
username = ask "Enter username: " | ||
password = SecureRandom.base58(64) | ||
|
||
store_credentials(username, password) | ||
say "Username and password stored in Rails encrypted credentials." | ||
say "" | ||
say "You can now access Mission Control – Jobs with: " | ||
say "" | ||
say " - Username: #{username}" | ||
say " - password: #{password}" | ||
say "" | ||
say "You can also edit these in the future via `credentials:edit`" | ||
end | ||
else | ||
say "Rails credentials haven't been configured or aren't accessible. Configure them following the instructions in `credentials:help`" | ||
end | ||
end | ||
|
||
private | ||
attr_reader :environment | ||
|
||
def credentials_accessible? | ||
credentials.read.present? | ||
end | ||
|
||
def authentication_configured? | ||
%i[ http_basic_auth_user http_basic_auth_password ].any? do |key| | ||
credentials.dig(:mission_control, key).present? | ||
end | ||
end | ||
|
||
def store_credentials(username, password) | ||
content = credentials.read + "\n" + http_authentication_entry(username, password) + "\n" | ||
credentials.write(content) | ||
end | ||
|
||
def credentials | ||
@credentials ||= Rails.application.encrypted(config.content_path, key_path: config.key_path) | ||
end | ||
|
||
def config | ||
Rails.application.config.credentials | ||
end | ||
|
||
def http_authentication_entry(username, password) | ||
<<~ENTRY | ||
mission_control: | ||
http_basic_auth_user: #{username} | ||
http_basic_auth_password: #{password} | ||
ENTRY | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace :mission_control do | ||
namespace :jobs do | ||
desc "Configure HTTP Basic Authentication" | ||
task "authentication:configure" => :environment do | ||
MissionControl::Jobs::Authentication.configure | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
67f819f011ec672273c91cf789afb5d7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3wr+OnlAdcQJl0WURd7JXv+pleXbJVWozLH4JfPU6dGc9A0VlQ/kQosdPqDF7Yf/WrLtodre258ALf0ZHE2bQYgH3Eq0cJQ7xN8WwfGjBjXiL6uWaOHcfgcPVNg4E3Ag+YN3EOH8aquSttX7Uqyfv3tPlYQBQ7fs8lXjx3APfl3P8Vk2Yz6bhQcBgXhtFqH+--f7tDKb8EHxaT9l+Z--WIHpj/e3mEcqupnMrf5fvw== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "test_helper" | ||
|
||
class MissionControl::Jobs::BasicAuthenticationTest < ActionDispatch::IntegrationTest | ||
test "unconfigured basic auth is closed" do | ||
with_http_basic_auth do | ||
get mission_control_jobs.application_queues_url(@application), headers: auth_headers("dev", "secret") | ||
assert_response :unauthorized | ||
end | ||
end | ||
|
||
test "fail to authenticate without credentials" do | ||
with_http_basic_auth(user: "dev", password: "secret") do | ||
get mission_control_jobs.application_queues_url(@application) | ||
assert_response :unauthorized | ||
end | ||
end | ||
|
||
test "fail to authenticate with wrong credentials" do | ||
with_http_basic_auth(user: "dev", password: "secret") do | ||
get mission_control_jobs.application_queues_url(@application), headers: auth_headers("dev", "wrong") | ||
assert_response :unauthorized | ||
end | ||
end | ||
|
||
test "authenticate with correct credentials" do | ||
with_http_basic_auth(user: "dev", password: "secret") do | ||
get mission_control_jobs.application_queues_url(@application), headers: auth_headers("dev", "secret") | ||
assert_response :ok | ||
end | ||
end | ||
|
||
private | ||
def with_http_basic_auth(enabled: true, user: nil, password: nil) | ||
previous_enabled, MissionControl::Jobs.http_basic_auth_enabled = MissionControl::Jobs.http_basic_auth_enabled, enabled | ||
previous_user, MissionControl::Jobs.http_basic_auth_user = MissionControl::Jobs.http_basic_auth_user, user | ||
previous_password, MissionControl::Jobs.http_basic_auth_password = MissionControl::Jobs.http_basic_auth_password, password | ||
yield | ||
ensure | ||
MissionControl::Jobs.http_basic_auth_enabled = previous_enabled | ||
MissionControl::Jobs.http_basic_auth_user = previous_user | ||
MissionControl::Jobs.http_basic_auth_password = previous_password | ||
end | ||
|
||
def auth_headers(user, password) | ||
{ Authorization: ActionController::HttpAuthentication::Basic.encode_credentials(user, password) } | ||
end | ||
end |