From 62d92d3c91cc7f90713eaaf95a668b57edc9327e Mon Sep 17 00:00:00 2001 From: Ruben van Staveren Date: Sat, 14 Jan 2023 16:29:39 +0100 Subject: [PATCH] Pylint fixes --- .pylintrc | 2 ++ jail2ban/__init__.py | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 .pylintrc diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..3fdb488 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,2 @@ +[TYPECHECK] +generated-members=app.logger.* diff --git a/jail2ban/__init__.py b/jail2ban/__init__.py index 7f0adab..8d3971d 100644 --- a/jail2ban/__init__.py +++ b/jail2ban/__init__.py @@ -1,11 +1,16 @@ +''' +jail2ban, a remote fail2ban action plugin using OpenBSD pf(8) +''' +from ipaddress import ip_address +import re +from subprocess import CalledProcessError + from flask import Flask, request, jsonify, current_app from flask_httpauth import HTTPBasicAuth from werkzeug.security import check_password_hash -from ipaddress import ip_address -import re + from jail2ban.pfctl import pfctl_table_op, pfctl_cfg_read, pfctl_cfg_write from jail2ban.auth import get_users -from subprocess import CalledProcessError auth = HTTPBasicAuth() @@ -30,11 +35,13 @@ def untaint(pattern, string): match = re.match(pattern, string) if match: return match.string - else: - raise ValueError(f'"{string}" is tainted') + raise ValueError(f'"{string}" is tainted') def create_app(): + ''' + Create wsgi application instance + ''' app = Flask(__name__, instance_relative_config=True) # load the instance config, if it exists, when not testing @@ -48,6 +55,7 @@ def create_app(): if username in users and \ check_password_hash(users.get(username), password): return username + return None @app.route("/ping", methods=['GET']) @auth.login_required @@ -118,21 +126,21 @@ def create_app(): data = request.get_json() # name / ip name = untaint(PAT_NAME, data['name']) - ip = ip_address(data['ip']) + ip_addr = ip_address(data['ip']) if request.method == 'PUT': - app.logger.info(f'Add {ip} to f2b-{name}' + app.logger.info(f'Add {ip_addr} to f2b-{name}' f' in anchor f2b-jail/{remote_user}') res = pfctl_table_op(f'f2b-jail/{remote_user}', table=f'f2b-{name}', operation='add', - value=str(ip)) + value=str(ip_addr)) else: # 'DELETE': - app.logger.info(f'Remove {ip} from f2b-{name}' + app.logger.info(f'Remove {ip_addr} from f2b-{name}' f' in anchor f2b-jail/{remote_user}') res = pfctl_table_op(f'f2b-jail/{remote_user}', table=f'f2b-{name}', operation='delete', - value=str(ip)) + value=str(ip_addr)) return jsonify({'anchor': f'f2b-jail/{remote_user}', 'table': f'f2b-{name}', 'operation': 'add' if request.method == 'PUT'