Compare commits
9 Commits
develop
...
ea325b0686
Author | SHA1 | Date | |
---|---|---|---|
ea325b0686
|
|||
62d92d3c91
|
|||
4bf881f03b
|
|||
610ccec47a
|
|||
5f7a0a4abd
|
|||
87252b6c5d
|
|||
e39ffc14e1
|
|||
a64d17b2e8
|
|||
9c6208f5c0
|
@ -88,28 +88,19 @@ def create_app():
|
||||
name = untaint(PAT_NAME, name)
|
||||
app.logger.info(f'Flushing table f2b-{name}'
|
||||
f' in anchor f2b-jail/{remote_user}')
|
||||
reply = {'anchor': f'f2b-jail/{remote_user}',
|
||||
'table': f'f2b-{name}',
|
||||
'operation': 'list'}
|
||||
try:
|
||||
res = pfctl_table_op('f2b-jail/{remote_user}',
|
||||
table='f2b-{name}',
|
||||
operation='show',
|
||||
verbose=True)
|
||||
except CalledProcessError as err:
|
||||
if err.stderr.find(b'pfctl: Table does not exist.') > 0:
|
||||
res = []
|
||||
reply.update({'error': f'\'{name}\' is not a known fail2ban jail'})
|
||||
else:
|
||||
raise err
|
||||
|
||||
res = pfctl_table_op('f2b-jail/{remote_user}',
|
||||
table='f2b-{name}',
|
||||
operation='show',
|
||||
verbose=True)
|
||||
result = [entry.groupdict() for entry in
|
||||
re.finditer(_PFCTL_TABLE_PAT,
|
||||
'\n'.join([x.decode('ascii') for x in res]),
|
||||
'\n'.join(res),
|
||||
re.MULTILINE|re.VERBOSE)]
|
||||
reply.update({'result': result})
|
||||
|
||||
return jsonify(reply), 200 if len(res) else 404
|
||||
return jsonify({'anchor': f'f2b-jail/{remote_user}',
|
||||
'table': f'f2b-{name}',
|
||||
'operation': 'list',
|
||||
'result': result })
|
||||
|
||||
@app.route("/register", methods=['PUT', 'DELETE'])
|
||||
@auth.login_required
|
||||
@ -191,8 +182,6 @@ def create_app():
|
||||
Show a json parsable error if the value is illegal
|
||||
'''
|
||||
app.logger.fatal(error)
|
||||
app.logger.fatal('stdout: %s', error.stderr)
|
||||
app.logger.fatal('stderr: %s', error.stderr)
|
||||
return jsonify({'error': str(error)}), 500
|
||||
|
||||
@app.errorhandler(FileNotFoundError)
|
||||
|
@ -2,7 +2,6 @@
|
||||
Tests for /list route
|
||||
'''
|
||||
from types import SimpleNamespace
|
||||
from subprocess import CalledProcessError
|
||||
|
||||
|
||||
_PF_TABLE_LIST = b''' 192.0.2.66
|
||||
@ -76,7 +75,7 @@ def test_list_single_table(client, mocker, valid_credentials):
|
||||
headers={"Authorization":
|
||||
"Basic " + valid_credentials})
|
||||
|
||||
assert response.json['table'] == 'f2b-sshd'
|
||||
assert response.json['anchor'] == 'f2b-sshd/test.example.com'
|
||||
assert response.json['result'] == _LIST_RESULT
|
||||
|
||||
|
||||
@ -89,18 +88,11 @@ def test_list_nonexistent_table(client, mocker, valid_credentials):
|
||||
|
||||
run_res = SimpleNamespace()
|
||||
run_res.stdout = b''
|
||||
run_res.stderr = b'No ALTQ support in kernel\nALTQ related functions disabled\n' \
|
||||
b'pfctl: Table does not exist.\n'
|
||||
run_res.stderr = b'No ALTQ support in kernel\nALTQ related functions disabled\n'
|
||||
run_res.returncode = 255
|
||||
run_res.check_returncode = noop
|
||||
|
||||
mocker.patch('jail2ban.pfctl.run',
|
||||
return_value=run_res,
|
||||
side_effect=CalledProcessError(run_res.returncode,
|
||||
'foobar',
|
||||
output=run_res.stdout,
|
||||
stderr=run_res.stderr)
|
||||
)
|
||||
mocker.patch('jail2ban.pfctl.run', return_value=run_res)
|
||||
|
||||
response = client.get("/list/nonexistent",
|
||||
headers={"Authorization":
|
||||
@ -109,32 +101,3 @@ def test_list_nonexistent_table(client, mocker, valid_credentials):
|
||||
assert response.status_code == 404
|
||||
assert response.json['error'] == "'nonexistent' is not " \
|
||||
"a known fail2ban jail"
|
||||
|
||||
def test_list_wrong_table_name(client, mocker, valid_credentials):
|
||||
'''
|
||||
Test for an wrong table name that lets pfctl fail. should result in a 500
|
||||
'''
|
||||
def noop():
|
||||
pass
|
||||
|
||||
run_res = SimpleNamespace()
|
||||
run_res.stdout = b''
|
||||
run_res.stderr = b'No ALTQ support in kernel\nALTQ related functions disabled\n' \
|
||||
b'pfctl: Invalid argument.\n'
|
||||
run_res.returncode = 255
|
||||
run_res.check_returncode = noop
|
||||
|
||||
mocker.patch('jail2ban.pfctl.run',
|
||||
return_value=run_res,
|
||||
side_effect=CalledProcessError(run_res.returncode,
|
||||
'foobar',
|
||||
output=run_res.stdout,
|
||||
stderr=run_res.stderr)
|
||||
)
|
||||
|
||||
response = client.get("/list/notanerrorbuttestneedstofail",
|
||||
headers={"Authorization":
|
||||
"Basic " + valid_credentials})
|
||||
|
||||
assert response.status_code == 500
|
||||
assert response.json['error'] == "Command 'foobar' returned non-zero exit status 255."
|
||||
|
Reference in New Issue
Block a user