-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CIOPS-1084] Bearer Token Support (#3)
### Description This pull request adds support for Basic Auth on the endpoint. This allows projects implementing this gem to secure their endpoint when they are public facing. That way the Rails stack is safe from being hammered with requests and prevents the application from being taken down. ### Changes * Moved the actual endpoint to a dedicated controller * Added support for Bearer tokens * Updated the documentation * Added minitest for the token functionality ### Ticket [CIOPS-1084](https://customink.atlassian.net/browse/CIOPS-1084)
- Loading branch information
Showing
11 changed files
with
103 additions
and
13 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
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module IsItReady | ||
class HealthCheckController < ::IsItReady::ApplicationController | ||
# Disable the CSRF security checks, since this controller will receive calls from external | ||
# services that do not have the ability to generate the required CSRF token | ||
skip_before_action :verify_authenticity_token | ||
|
||
# Ensure that all incoming requests supply a valid Bearer token in their Authorization header. | ||
# The function will determine whether the check is required or not based upon the configuration. | ||
before_action :authenticate! | ||
|
||
AUTHORIZATION_HEADER = 'HTTP_AUTHORIZATION' | ||
|
||
# GET /is_it_ready | ||
# | ||
# Returns the desired output, running through the entire Ruby on Rails stack to indicate that | ||
# this application is able to serve requests. The routing is controlled through the Engine, | ||
# but we might be mounted under a specific endpoint or with a custom path. | ||
def is_it_ready | ||
render :json => { :status => 'ok', :code => 200 } | ||
end | ||
|
||
private | ||
|
||
# This action will look up the HTTP Authorization header when the configuration has a Bearer token set. | ||
# When the token is set, the incoming requests must provide this as a Bearer token, otherwise the request | ||
# will be refused with an HTTP UNAUTHORIZED response. | ||
def authenticate! | ||
return unless ::IsItReady.bearer_token.present? | ||
|
||
authenticate_or_request_with_http_token do |token, _options| | ||
::IsItReady.bearer_token == token | ||
end | ||
end | ||
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module IsItReady | ||
VERSION = '0.0.3' | ||
VERSION = '0.0.4' | ||
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
# This initializer configures SQLite to silence warnings and make sure the "database" is usable | ||
# across all Rails versions the way a normal database is used during testing. | ||
|
||
# Enforce booleans to represented as integers in the database. | ||
# This was an old SQLite feature that we do not want to support anymore. | ||
# Note: This is only for older versions of Sqlite3, the configuration option was dropped at some point | ||
if ::Rails.application.config.active_record.sqlite3.respond_to?(:represent_boolean_as_integer) | ||
::Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'test_helper' | ||
require 'securerandom' | ||
|
||
module IsItReady | ||
class NavigationWithTokenTest < ActionDispatch::IntegrationTest | ||
include Engine.routes.url_helpers | ||
|
||
setup do | ||
::IsItReady.bearer_token = ::SecureRandom.hex(15) | ||
end | ||
|
||
teardown do | ||
::IsItReady.bearer_token = nil | ||
end | ||
|
||
test('it returns the correct response status on the root') do | ||
get root_url, headers: { 'HTTP_AUTHORIZATION' => "Bearer token=#{::IsItReady.bearer_token}" } | ||
|
||
assert_response :success | ||
end | ||
|
||
test('it returns the correct output on the root') do | ||
get root_url, headers: { 'HTTP_AUTHORIZATION' => "Bearer token=#{::IsItReady.bearer_token}" } | ||
|
||
response = ::JSON.parse(@response.body, symbolize_names: true) | ||
|
||
assert_equal({ :status => "ok", :code => 200 }, response) | ||
end | ||
|
||
test('it returns the correct response status on the root without token') do | ||
get root_url | ||
|
||
assert_response :unauthorized | ||
end | ||
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