Class: Aikido::Zen::Middleware::AttackWaveProtector
- Inherits:
-
Object
- Object
- Aikido::Zen::Middleware::AttackWaveProtector
- Defined in:
- lib/aikido/zen/middleware/attack_wave_protector.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #detect_attack_wave(context, status_code = nil) ⇒ Array<Aikido::Zen::AttackWave::Sample>? private
-
#initialize(app, zen: Aikido::Zen, settings: Aikido::Zen.runtime_settings) ⇒ AttackWaveProtector
constructor
A new instance of AttackWaveProtector.
- #protect(context, status_code = nil) ⇒ Object private
Constructor Details
#initialize(app, zen: Aikido::Zen, settings: Aikido::Zen.runtime_settings) ⇒ AttackWaveProtector
Returns a new instance of AttackWaveProtector.
7 8 9 10 11 |
# File 'lib/aikido/zen/middleware/attack_wave_protector.rb', line 7 def initialize(app, zen: Aikido::Zen, settings: Aikido::Zen.runtime_settings) @app = app @zen = zen @settings = settings end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/aikido/zen/middleware/attack_wave_protector.rb', line 13 def call(env) response = @app.call(env) status_code = response[0].to_i context = @zen.current_context protect(context, status_code) response end |
#detect_attack_wave(context, status_code = nil) ⇒ Array<Aikido::Zen::AttackWave::Sample>?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Note:
Visible for testing.
29 30 31 32 33 34 35 36 |
# File 'lib/aikido/zen/middleware/attack_wave_protector.rb', line 29 def detect_attack_wave(context, status_code = nil) request = context.request return nil if request.nil? return nil if @settings.bypassed_ips.include?(request.client_ip) @zen.detect_attack_wave(context, status_code) end |
#protect(context, status_code = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Note:
Visible for testing.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/aikido/zen/middleware/attack_wave_protector.rb', line 40 def protect(context, status_code = nil) samples = detect_attack_wave(context, status_code) return unless samples client_ip = context.request.client_ip request = Aikido::Zen::AttackWave::Request.new( ip_address: client_ip, user_agent: context.request.user_agent, source: context.request.framework ) attack = Aikido::Zen::AttackWave::Attack.new( samples: samples, user: context.request.actor ) attack_wave = Aikido::Zen::Events::AttackWave.new( request: request, attack: attack ) @zen.track_attack_wave(attack_wave) @zen.agent.report(attack_wave) end |