Commit c8f23bd2 authored by DJ Mountney's avatar DJ Mountney

Support token header for health check token, and general cleanup of the health_check feature.

parent 0e0caf4d
class HealthCheckController < HealthCheck::HealthCheckController class HealthCheckController < HealthCheck::HealthCheckController
before_action :validate_health_check_access! before_action :validate_health_check_access!
protected private
def validate_health_check_access! def validate_health_check_access!
return render_404 unless params[:token].presence && params[:token] == current_application_settings.health_check_access_token render_404 unless token_valid?
end
def token_valid?
token = params[:token].presence || request.headers['TOKEN']
token.present? &&
ActiveSupport::SecurityUtils.variable_size_secure_compare(
token,
current_application_settings.health_check_access_token
)
end end
def render_404 def render_404
render file: Rails.root.join("public", "404"), layout: false, status: "404" render file: Rails.root.join('public', '404'), layout: false, status: '404'
end end
end end
...@@ -2,29 +2,35 @@ ...@@ -2,29 +2,35 @@
%h3.page-title %h3.page-title
Health Check Health Check
%p.light .bs-callout.clearfix
.pull-left
%p
Access token is Access token is
%code{ id:'health-check-token' }= "#{current_application_settings.health_check_access_token}" %code#health-check-token= current_application_settings.health_check_access_token
= button_to reset_health_check_token_admin_application_settings_path,
method: :put, class: 'btn btn-default',
data: { confirm: 'Are you sure you want to reset the health check token?' } do
= icon('refresh')
Reset health check access token
%p.light %p.light
Health information can be reteived as plain text, json, or xml using: Health information can be reteived as plain text, json, or xml using:
%ul %ul
%li %li
%code= "/health_check?token=#{current_application_settings.health_check_access_token}" %code= health_check_url(token:current_application_settings.health_check_access_token)
%li %li
%code= "/health_check.json?token=#{current_application_settings.health_check_access_token}" %code= health_check_url(token:current_application_settings.health_check_access_token, format: :json)
%li %li
%code= "/health_check.xml?token=#{current_application_settings.health_check_access_token}" %code= health_check_url(token:current_application_settings.health_check_access_token, format: :xml)
.bs-callout.clearfix %p.light
.pull-left You can also ask for the status of specific services:
%p %ul
You can reset the health check access token by pressing the button below. %li
%p %code= health_check_url(token:current_application_settings.health_check_access_token, checks: :cache)
= button_to reset_health_check_token_admin_application_settings_path, %li
method: :put, class: 'btn btn-default', %code= health_check_url(token:current_application_settings.health_check_access_token, checks: :database)
data: { confirm: 'Are you sure you want to reset the health check token?' } do %li
= icon('refresh') %code= health_check_url(token:current_application_settings.health_check_access_token, checks: :migrations)
Reset health check access token
%hr %hr
.panel.panel-default .panel.panel-default
......
HealthCheck.setup do |config| HealthCheck.setup do |config|
config.standard_checks = [ 'database', 'migrations', 'cache' ] config.standard_checks = ['database', 'migrations', 'cache']
end end
...@@ -74,7 +74,7 @@ Rails.application.routes.draw do ...@@ -74,7 +74,7 @@ Rails.application.routes.draw do
end end
# Health check # Health check
get 'health_check(/:checks)(.:format)' => 'health_check#index' get 'health_check(/:checks)' => 'health_check#index', as: :health_check
# Enable Grack support # Enable Grack support
mount Grack::AuthSpawner, at: '/', constraints: lambda { |request| /[-\/\w\.]+\.git\//.match(request.path_info) }, via: [:get, :post, :put] mount Grack::AuthSpawner, at: '/', constraints: lambda { |request| /[-\/\w\.]+\.git\//.match(request.path_info) }, via: [:get, :post, :put]
......
...@@ -14,6 +14,13 @@ describe HealthCheckController do ...@@ -14,6 +14,13 @@ describe HealthCheckController do
end end
context 'when services are up and an access token is provided' do context 'when services are up and an access token is provided' do
it 'supports passing the token in the header' do
request.headers['TOKEN'] = token
get :index
expect(response).to be_success
expect(response.content_type).to eq 'text/plain'
end
it 'supports successful plaintest response' do it 'supports successful plaintest response' do
get :index, token: token get :index, token: token
expect(response).to be_success expect(response).to be_success
...@@ -55,6 +62,14 @@ describe HealthCheckController do ...@@ -55,6 +62,14 @@ describe HealthCheckController do
allow(HealthCheck::Utils).to receive(:process_checks).with('email').and_return('Email is on fire') allow(HealthCheck::Utils).to receive(:process_checks).with('email').and_return('Email is on fire')
end end
it 'supports passing the token in the header' do
request.headers['TOKEN'] = token
get :index
expect(response.status).to eq(500)
expect(response.content_type).to eq 'text/plain'
expect(response.body).to include('The server is on fire')
end
it 'supports failure plaintest response' do it 'supports failure plaintest response' do
get :index, token: token get :index, token: token
expect(response.status).to eq(500) expect(response.status).to eq(500)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment