feat: provide discriminator in throttle_data

This commit is contained in:
Gonzalo Rodriguez 2019-07-10 17:21:48 -03:00
parent d1b71da182
commit 58ba650e6b
No known key found for this signature in database
GPG key ID: 5DB8B81B049B8AB1
3 changed files with 35 additions and 5 deletions

View file

@ -354,7 +354,7 @@ end
For responses that did not exceed a throttle limit, Rack::Attack annotates the env with match data: For responses that did not exceed a throttle limit, Rack::Attack annotates the env with match data:
```ruby ```ruby
request.env['rack.attack.throttle_data'][name] # => { :count => n, :period => p, :limit => l, :epoch_time => t } request.env['rack.attack.throttle_data'][name] # => { discriminator: d, count: n, period: p, limit: l, epoch_time: t }
``` ```
## Logging & Instrumentation ## Logging & Instrumentation

View file

@ -31,6 +31,7 @@ module Rack
epoch_time = cache.last_epoch_time epoch_time = cache.last_epoch_time
data = { data = {
discriminator: discriminator,
count: count, count: count,
period: current_period, period: current_period,
limit: current_limit, limit: current_limit,

View file

@ -22,7 +22,14 @@ describe 'Rack::Attack.throttle' do
end end
it 'should populate throttle data' do it 'should populate throttle data' do
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } data = {
count: 1,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
}
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
end end
end end
@ -39,7 +46,15 @@ describe 'Rack::Attack.throttle' do
it 'should tag the env' do it 'should tag the env' do
last_request.env['rack.attack.matched'].must_equal 'ip/sec' last_request.env['rack.attack.matched'].must_equal 'ip/sec'
last_request.env['rack.attack.match_type'].must_equal :throttle last_request.env['rack.attack.match_type'].must_equal :throttle
last_request.env['rack.attack.match_data'].must_equal(count: 2, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i)
last_request.env['rack.attack.match_data'].must_equal(
count: 2,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
)
last_request.env['rack.attack.match_discriminator'].must_equal('1.2.3.4') last_request.env['rack.attack.match_discriminator'].must_equal('1.2.3.4')
end end
@ -67,7 +82,14 @@ describe 'Rack::Attack.throttle with limit as proc' do
end end
it 'should populate throttle data' do it 'should populate throttle data' do
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } data = {
count: 1,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
}
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
end end
end end
@ -91,7 +113,14 @@ describe 'Rack::Attack.throttle with period as proc' do
end end
it 'should populate throttle data' do it 'should populate throttle data' do
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } data = {
count: 1,
limit: 1,
period: @period,
epoch_time: Rack::Attack.cache.last_epoch_time.to_i,
discriminator: "1.2.3.4"
}
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
end end
end end