From af1fef189c060524deacd01d7b0d7169d0d2bdb5 Mon Sep 17 00:00:00 2001 From: Ruben van Staveren Date: Thu, 10 Mar 2022 23:29:14 +0100 Subject: [PATCH] Handle pfctl_cfg_write output as expected --- jail2ban/__init__.py | 5 ++--- jail2ban/pfctl.py | 2 +- tests/test_register.py | 10 +++++----- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/jail2ban/__init__.py b/jail2ban/__init__.py index a4b14e9..b56d970 100644 --- a/jail2ban/__init__.py +++ b/jail2ban/__init__.py @@ -54,7 +54,7 @@ def create_app(): return jsonify({'anchor': f'f2b-jail/{remote_user}', 'table': f'f2b-{name}', 'operation': 'flush', - 'result': res}) + 'result': [x.decode('ascii') for x in res]}) @app.route("/register", methods=['PUT', 'DELETE']) @auth.login_required @@ -88,12 +88,11 @@ def create_app(): table=f'f2b-{name}', operation='kill') app.logger.info(f'pfctl -a f2b-jail/{remote_user} -f-') - return jsonify({'remote_user': remote_user, 'data': data}) return jsonify({'anchor': f'f2b-jail/{remote_user}', 'table': f'f2b-{name}', 'action': 'start' if request.method == 'PUT' else 'stop', - 'result': res}) + 'result': [x.decode('ascii') for x in res]}) @app.route("/ban", methods=['PUT', 'DELETE']) @auth.login_required diff --git a/jail2ban/pfctl.py b/jail2ban/pfctl.py index 4a079e7..24f4448 100644 --- a/jail2ban/pfctl.py +++ b/jail2ban/pfctl.py @@ -29,7 +29,7 @@ def pfctl_cfg_write(anchor, cfg): if res: logging.info('Result: %s', res) res.check_returncode() - return res + return res.stdout.splitlines() def pfctl_table_op(anchor, **kwargs): diff --git a/tests/test_register.py b/tests/test_register.py index 9c4f8d1..a5bcf74 100644 --- a/tests/test_register.py +++ b/tests/test_register.py @@ -1,5 +1,5 @@ import base64 -from types import SimpleNamespace +from subprocess import CompletedProcess pfctl_stdout_lines = b''' block drop quick proto tcp from to any port = submission @@ -22,7 +22,7 @@ def test_register_unauth(client): def test_register_valid(client, mocker): def noop(): pass - run_res = SimpleNamespace() + run_res = CompletedProcess(args=['true'], returncode=0) run_res.stdout = pfctl_stdout_lines run_res.check_returncode = noop @@ -37,13 +37,13 @@ def test_register_valid(client, mocker): json=json_payload, headers={"Authorization": "Basic " + valid_credentials}) - assert response.json['action'] == 'delete' + assert response.json['action'] == 'stop' def test_unregister_valid(client, mocker): def noop(): pass - run_res = SimpleNamespace() + run_res = CompletedProcess(args=['true'], returncode=0) run_res.stdout = pfctl_stdout_lines run_res.check_returncode = noop @@ -64,7 +64,7 @@ def test_unregister_valid(client, mocker): def test_register_invalid(client, mocker): def noop(): pass - run_res = SimpleNamespace() + run_res = CompletedProcess(args=['true'], returncode=0) run_res.stdout = pfctl_stdout_lines run_res.check_returncode = noop