-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Default to closed by basic auth when not configured
- Loading branch information
Showing
6 changed files
with
89 additions
and
14 deletions.
There are no files selected for viewing
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
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 |