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

Support ActiveSupport::RedisCacheStore #340

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- Adds support for ActiveSupport::RedisCacheStore

## [5.2.0] - 2018-03-29

### Added
Expand Down
25 changes: 13 additions & 12 deletions lib/rack/attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ class Rack::Attack
class MisconfiguredStoreError < StandardError; end
class MissingStoreError < StandardError; end

autoload :Cache, 'rack/attack/cache'
autoload :Check, 'rack/attack/check'
autoload :Throttle, 'rack/attack/throttle'
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 :MemCacheProxy, 'rack/attack/store_proxy/mem_cache_proxy'
autoload :RedisStoreProxy, 'rack/attack/store_proxy/redis_store_proxy'
autoload :Fail2Ban, 'rack/attack/fail2ban'
autoload :Allow2Ban, 'rack/attack/allow2ban'
autoload :Cache, 'rack/attack/cache'
autoload :Check, 'rack/attack/check'
autoload :Throttle, 'rack/attack/throttle'
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 :MemCacheProxy, 'rack/attack/store_proxy/mem_cache_proxy'
autoload :RedisStoreProxy, 'rack/attack/store_proxy/redis_store_proxy'
autoload :RedisCacheStoreProxy, 'rack/attack/store_proxy/redis_cache_store_proxy'
autoload :Fail2Ban, 'rack/attack/fail2ban'
autoload :Allow2Ban, 'rack/attack/allow2ban'

class << self
attr_accessor :notifier, :blocklisted_response, :throttled_response
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/attack/store_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Rack
class Attack
module StoreProxy
PROXIES = [DalliProxy, MemCacheProxy, RedisStoreProxy].freeze
PROXIES = [DalliProxy, MemCacheProxy, RedisStoreProxy, RedisCacheStoreProxy].freeze

ACTIVE_SUPPORT_WRAPPER_CLASSES = Set.new(['ActiveSupport::Cache::MemCacheStore', 'ActiveSupport::Cache::RedisStore']).freeze
ACTIVE_SUPPORT_WRAPPER_CLASSES = Set.new(['ActiveSupport::Cache::MemCacheStore', 'ActiveSupport::Cache::RedisStore', 'ActiveSupport::Cache::RedisCacheStore']).freeze
ACTIVE_SUPPORT_CLIENTS = Set.new(['Redis::Store', 'Dalli::Client', 'MemCache']).freeze

def self.build(store)
Expand Down
30 changes: 30 additions & 0 deletions lib/rack/attack/store_proxy/redis_cache_store_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'delegate'

module Rack
class Attack
module StoreProxy
class RedisCacheStoreProxy < SimpleDelegator
def self.handle?(store)
defined?(::ActiveSupport::Cache::RedisCacheStore) && store.is_a?(::ActiveSupport::Cache::RedisCacheStore)
end

def increment(name, amount, options = {})
# Redis doesn't check expiration on the INCRBY command. See https://redis.io/commands/expire
count = redis.pipelined do
redis.incrby(name, amount)
redis.expire(name, options[:expires_in]) if options[:expires_in]
end
count.first
end

def read(name, options = {})
super(name, options.merge!({ raw: true }))
end

def write(name, value, options = {})
super(name, value, options.merge!({ raw: true }))
end
end
end
end
end
5 changes: 4 additions & 1 deletion spec/integration/rack_attack_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Rack::Attack::Cache do
# A convenience method for deleting a key from cache.
# Slightly differnet than @cache.delete, which adds a prefix.
# Slightly different than @cache.delete, which adds a prefix.
def delete(key)
if @cache.store.respond_to?(:delete)
@cache.store.delete(key)
Expand All @@ -18,6 +18,7 @@ def sleep_until_expired
require 'active_support/cache/dalli_store'
require 'active_support/cache/mem_cache_store'
require 'active_support/cache/redis_store'
require 'active_support/cache/redis_cache_store' if ActiveSupport.version.to_s.to_f >= 5.2
require 'connection_pool'

cache_stores = [
Expand All @@ -30,6 +31,8 @@ def sleep_until_expired
Redis::Store.new
]

cache_stores << ActiveSupport::Cache::RedisCacheStore.new if defined?(ActiveSupport::Cache::RedisCacheStore)

cache_stores.each do |store|
store = Rack::Attack::StoreProxy.build(store)

Expand Down