Skip to content
Snippets Groups Projects
Commit 0075172f authored by ale's avatar ale
Browse files

enable/disable rate limiting and blacklisting from config

parent 73af8193
Branches
No related tags found
No related merge requests found
......@@ -114,6 +114,8 @@ def ratelimit_http_request(key_fn, count=0, period=0):
def decoratorfn(fn):
@functools.wraps(fn)
def _ratelimit(*args, **kwargs):
if not current_app.config.get('ENABLE_RATELIMIT'):
return fn(*args, **kwargs)
key = _tostr(key_fn(fn.__name__, args))
if key and not rl.check(current_app.memcache, key):
current_app.logger.debug('ratelimited: %s', key)
......@@ -161,13 +163,15 @@ def blacklist_on_auth_failure(key_fn, count=0, period=0, ttl=0, check_wl=False,
def decoratorfn(fn):
@functools.wraps(fn)
def _blacklist(*args, **kwargs):
if not current_app.config.get('ENABLE_BLACKLIST'):
return fn(*args, **kwargs)
key = _tostr(key_fn(fn.__name__, args))
if not key:
return fn(*args, **kwargs)
if ((not check_wl or not whitelisted(key))
and not bl.check(current_app.memcache, key)):
current_app.logger.debug('blacklisted %s', key)
current_app.logger.info('blacklisted %s', key)
return bl_return_value
result = fn(*args, **kwargs)
if result != protocol.OK:
......@@ -175,4 +179,3 @@ def blacklist_on_auth_failure(key_fn, count=0, period=0, ttl=0, check_wl=False,
return result
return _blacklist
return decoratorfn
......@@ -22,6 +22,7 @@ class ServerTest(unittest.TestCase):
app.config.update({
'TESTING': True,
'DEBUG': True,
'ENABLE_BLACKLIST': True,
})
self.app = app.test_client()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment