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

Acceptance test store config for allow2ban/fail2ban #305

Merged
merged 4 commits into from
Mar 19, 2018
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
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ rvm:
- jruby-9.1.16.0

before_install:
- gem update --system
# For jruby we need to stick with rubygems 2.7.4 until
# https://github.com/rubygems/rubygems/issues/2188
# is fixed and released.
#
# Without this workaround, for jruby builds, rubygems
# activates jruby stdlib minitest (v5.4.1) instead of the
# bundled version (v5.11.3).
- if [ "${TRAVIS_RUBY_VERSION:0:5}" = "jruby" ]; then gem update --system 2.7.4; else gem update --system; fi
- gem install bundler

gemfile:
Expand Down
65 changes: 65 additions & 0 deletions spec/acceptance/cache_store_config_for_allow2ban_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require_relative "../spec_helper"

describe "Cache store config when using allow2ban" do
before do
Rack::Attack.blocklist("allow2ban pentesters") do |request|
Rack::Attack::Allow2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do
request.path.include?("scarce-resource")
end
end
end

it "gives error if no store was configured" do
assert_raises do
get "/"
end
end

it "gives error if incompatible store was configured" do
Rack::Attack.cache.store = Object.new

assert_raises do
get "/"
end
end

it "works with any object that responds to #read, #write and #increment" do
basic_store_class = Class.new do
attr_accessor :backend

def initialize
@backend = {}
end

def read(key)
@backend[key]
end

def write(key, value, options = {})
@backend[key] = value
end

def increment(key, count, options = {})
@backend[key] ||= 0
@backend[key] += 1
end
end

Rack::Attack.cache.store = basic_store_class.new

get "/"
assert_equal 200, last_response.status

get "/scarce-resource"
assert_equal 200, last_response.status

get "/scarce-resource"
assert_equal 200, last_response.status

get "/scarce-resource"
assert_equal 403, last_response.status

get "/"
assert_equal 403, last_response.status
end
end
62 changes: 62 additions & 0 deletions spec/acceptance/cache_store_config_for_fail2ban_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require_relative "../spec_helper"

describe "Cache store config when using fail2ban" do
before do
Rack::Attack.blocklist("fail2ban pentesters") do |request|
Rack::Attack::Fail2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do
request.path.include?("private-place")
end
end
end

it "gives error if no store was configured" do
assert_raises do
get "/"
end
end

it "gives error if incompatible store was configured" do
Rack::Attack.cache.store = Object.new

assert_raises do
get "/"
end
end

it "works with any object that responds to #read, #write and #increment" do
basic_store_class = Class.new do
attr_accessor :backend

def initialize
@backend = {}
end

def read(key)
@backend[key]
end

def write(key, value, options = {})
@backend[key] = value
end

def increment(key, count, options = {})
@backend[key] ||= 0
@backend[key] += 1
end
end

Rack::Attack.cache.store = basic_store_class.new

get "/"
assert_equal 200, last_response.status

get "/private-place"
assert_equal 403, last_response.status

get "/private-place"
assert_equal 403, last_response.status

get "/"
assert_equal 403, last_response.status
end
end