debian-mirror-gitlab/config/initializers/rack_attack_logging.rb

39 lines
1.2 KiB
Ruby
Raw Normal View History

2019-09-04 21:01:54 +05:30
# frozen_string_literal: true
#
2017-08-17 22:00:37 +05:30
# Adds logging for all Rack Attack blocks and throttling events.
2019-12-26 22:10:19 +05:30
ActiveSupport::Notifications.subscribe(/rack_attack/) do |name, start, finish, request_id, payload|
req = payload[:request]
2021-01-29 00:20:46 +05:30
case req.env['rack.attack.match_type']
when :throttle, :blocklist
2019-09-30 21:07:59 +05:30
rack_attack_info = {
2019-09-04 21:01:54 +05:30
message: 'Rack_Attack',
env: req.env['rack.attack.match_type'],
2019-12-04 20:38:33 +05:30
remote_ip: req.ip,
2019-09-04 21:01:54 +05:30
request_method: req.request_method,
2021-01-29 00:20:46 +05:30
path: req.fullpath,
matched: req.env['rack.attack.matched']
2019-09-30 21:07:59 +05:30
}
2019-12-21 20:55:43 +05:30
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-09-30 21:07:59 +05:30
user_id = req.env['rack.attack.match_discriminator']
user = User.find_by(id: user_id)
rack_attack_info[:user_id] = user_id
2021-01-29 00:20:46 +05:30
rack_attack_info['meta.user'] = user.username unless user.nil?
2019-09-30 21:07:59 +05:30
end
Gitlab::AuthLogger.error(rack_attack_info)
2021-01-29 00:20:46 +05:30
when :safelist
Gitlab::Instrumentation::Throttle.safelist = req.env['rack.attack.matched']
2017-08-17 22:00:37 +05:30
end
end