2019-05-23 20:45:02 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
2016-11-18 13:45:52 -05:00
|
|
|
# Adds logging for all Rack Attack blocks and throttling events.
|
|
|
|
|
2019-10-27 05:05:56 -04:00
|
|
|
ActiveSupport::Notifications.subscribe(/rack_attack/) do |name, start, finish, request_id, payload|
|
|
|
|
req = payload[:request]
|
|
|
|
|
|
|
|
if [:throttle, :blocklist].include? req.env['rack.attack.match_type']
|
2019-07-02 15:48:06 -04:00
|
|
|
rack_attack_info = {
|
2019-05-23 20:45:02 -04:00
|
|
|
message: 'Rack_Attack',
|
|
|
|
env: req.env['rack.attack.match_type'],
|
2019-08-20 14:12:28 -04:00
|
|
|
remote_ip: req.ip,
|
2019-05-23 20:45:02 -04:00
|
|
|
request_method: req.request_method,
|
2019-08-20 14:12:28 -04:00
|
|
|
path: req.fullpath
|
2019-07-02 15:48:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-26 17:06:29 -04:00
|
|
|
throttles_with_user_information = [
|
|
|
|
:throttle_authenticated_api,
|
|
|
|
:throttle_authenticated_web,
|
|
|
|
:throttle_authenticated_protected_paths_api,
|
|
|
|
:throttle_authenticated_protected_paths_web
|
|
|
|
]
|
|
|
|
|
|
|
|
if throttles_with_user_information.include? req.env['rack.attack.matched'].to_sym
|
2019-07-02 15:48:06 -04:00
|
|
|
user_id = req.env['rack.attack.match_discriminator']
|
|
|
|
user = User.find_by(id: user_id)
|
|
|
|
|
2019-09-26 17:06:29 -04:00
|
|
|
rack_attack_info[:throttle_type] = req.env['rack.attack.matched']
|
2019-07-02 15:48:06 -04:00
|
|
|
rack_attack_info[:user_id] = user_id
|
|
|
|
rack_attack_info[:username] = user.username unless user.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::AuthLogger.error(rack_attack_info)
|
2016-11-18 13:45:52 -05:00
|
|
|
end
|
|
|
|
end
|