2018-07-06 20:20:02 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This middleware provides a health check that does not hit the database. Its purpose
|
|
|
|
# is to notify the prober that the application server is handling requests, but a 200
|
|
|
|
# response does not signify that the database or other services are ready.
|
|
|
|
#
|
|
|
|
# See https://thisdata.com/blog/making-a-rails-health-check-that-doesnt-hit-the-database/ for
|
|
|
|
# more details.
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Middleware
|
2018-07-27 19:19:51 +00:00
|
|
|
class BasicHealthCheck
|
2018-07-06 20:20:02 +00:00
|
|
|
# This can't be frozen because Rails::Rack::Logger wraps the body
|
|
|
|
# rubocop:disable Style/MutableConstant
|
2018-07-27 19:19:51 +00:00
|
|
|
OK_RESPONSE = [200, { 'Content-Type' => 'text/plain' }, ["GitLab OK"]]
|
2018-07-06 20:20:02 +00:00
|
|
|
EMPTY_RESPONSE = [404, { 'Content-Type' => 'text/plain' }, [""]]
|
|
|
|
# rubocop:enable Style/MutableConstant
|
2018-07-27 19:19:51 +00:00
|
|
|
HEALTH_PATH = '/-/health'
|
2018-07-06 20:20:02 +00:00
|
|
|
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2018-07-27 19:19:51 +00:00
|
|
|
return @app.call(env) unless env['PATH_INFO'] == HEALTH_PATH
|
2018-07-06 20:20:02 +00:00
|
|
|
|
|
|
|
request = Rack::Request.new(env)
|
|
|
|
|
|
|
|
return OK_RESPONSE if client_ip_whitelisted?(request)
|
|
|
|
|
|
|
|
EMPTY_RESPONSE
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_ip_whitelisted?(request)
|
|
|
|
ip_whitelist.any? { |e| e.include?(request.ip) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def ip_whitelist
|
|
|
|
@ip_whitelist ||= Settings.monitoring.ip_whitelist.map(&IPAddr.method(:new))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|