Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make store proxies lookup dynamic #453

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/rack/attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
require 'rack/attack/configuration'
require 'rack/attack/path_normalizer'
require 'rack/attack/request'
require 'rack/attack/store_proxy/dalli_proxy'
require 'rack/attack/store_proxy/mem_cache_store_proxy'
require 'rack/attack/store_proxy/redis_proxy'
require 'rack/attack/store_proxy/redis_store_proxy'
require 'rack/attack/store_proxy/redis_cache_store_proxy'
require 'rack/attack/store_proxy/active_support_redis_store_proxy'

require 'rack/attack/railtie' if defined?(::Rails)

Expand All @@ -21,13 +27,6 @@ class IncompatibleStoreError < Error; end
autoload :Safelist, 'rack/attack/safelist'
autoload :Blocklist, 'rack/attack/blocklist'
autoload :Track, 'rack/attack/track'
autoload :StoreProxy, 'rack/attack/store_proxy'
autoload :DalliProxy, 'rack/attack/store_proxy/dalli_proxy'
autoload :MemCacheStoreProxy, 'rack/attack/store_proxy/mem_cache_store_proxy'
autoload :RedisProxy, 'rack/attack/store_proxy/redis_proxy'
autoload :RedisStoreProxy, 'rack/attack/store_proxy/redis_store_proxy'
autoload :RedisCacheStoreProxy, 'rack/attack/store_proxy/redis_cache_store_proxy'
autoload :ActiveSupportRedisStoreProxy, 'rack/attack/store_proxy/active_support_redis_store_proxy'
autoload :Fail2Ban, 'rack/attack/fail2ban'
autoload :Allow2Ban, 'rack/attack/allow2ban'

Expand Down
27 changes: 27 additions & 0 deletions lib/rack/attack/base_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'delegate'

module Rack
class Attack
class BaseProxy < SimpleDelegator
class << self
def proxies
@@proxies ||= []
end

def inherited(klass)
proxies << klass
end

def lookup(store)
proxies.find { |proxy| proxy.handle?(store) }
end

def handle?(_store)
raise NotImplementedError
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/rack/attack/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ def initialize

attr_reader :store
def store=(store)
@store = StoreProxy.build(store)
@store =
if (proxy = BaseProxy.lookup(store))
proxy.new(store)
else
store
end
end

def count(unprefixed_key, period)
Expand Down
21 changes: 0 additions & 21 deletions lib/rack/attack/store_proxy.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'delegate'
require 'rack/attack/base_proxy'

module Rack
class Attack
module StoreProxy
class ActiveSupportRedisStoreProxy < SimpleDelegator
class ActiveSupportRedisStoreProxy < BaseProxy
def self.handle?(store)
defined?(::Redis) &&
defined?(::ActiveSupport::Cache::RedisStore) &&
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/attack/store_proxy/dalli_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'delegate'
require 'rack/attack/base_proxy'

module Rack
class Attack
module StoreProxy
class DalliProxy < SimpleDelegator
class DalliProxy < BaseProxy
def self.handle?(store)
return false unless defined?(::Dalli)

Expand Down
4 changes: 2 additions & 2 deletions lib/rack/attack/store_proxy/mem_cache_store_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'delegate'
require 'rack/attack/base_proxy'

module Rack
class Attack
module StoreProxy
class MemCacheStoreProxy < SimpleDelegator
class MemCacheStoreProxy < BaseProxy
def self.handle?(store)
defined?(::Dalli) &&
defined?(::ActiveSupport::Cache::MemCacheStore) &&
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/attack/store_proxy/redis_cache_store_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'delegate'
require 'rack/attack/base_proxy'

module Rack
class Attack
module StoreProxy
class RedisCacheStoreProxy < SimpleDelegator
class RedisCacheStoreProxy < BaseProxy
def self.handle?(store)
store.class.name == "ActiveSupport::Cache::RedisCacheStore"
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rack/attack/store_proxy/redis_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'delegate'
require 'rack/attack/base_proxy'

module Rack
class Attack
module StoreProxy
class RedisProxy < SimpleDelegator
class RedisProxy < BaseProxy
def initialize(*args)
if Gem::Version.new(Redis::VERSION) < Gem::Version.new("3")
warn 'RackAttack requires Redis gem >= 3.0.0.'
Expand All @@ -15,7 +15,7 @@ def initialize(*args)
end

def self.handle?(store)
defined?(::Redis) && store.is_a?(::Redis)
defined?(::Redis) && store.class == ::Redis
end

def read(key)
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/attack/store_proxy/redis_store_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'delegate'
require 'rack/attack/store_proxy/redis_proxy'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find


module Rack
class Attack
Expand Down