Externalize userlist

This commit is contained in:
Ruben van Staveren 2022-03-07 18:03:59 +01:00
parent 438f8eaf63
commit da83d716d7
Signed by: ruben
GPG Key ID: 886F6BECD477A93F
2 changed files with 25 additions and 5 deletions

View File

@ -3,14 +3,12 @@ from flask_httpauth import HTTPBasicAuth
from werkzeug.security import check_password_hash from werkzeug.security import check_password_hash
from ipaddress import ip_address from ipaddress import ip_address
import re import re
from pfctl import pfctl_table_op, pfctl_cfg_read, pfctl_cfg_write from jail2ban.pfctl import pfctl_table_op, pfctl_cfg_read, pfctl_cfg_write
from jail2ban.auth import get_users
auth = HTTPBasicAuth() auth = HTTPBasicAuth()
users = {
"erg.verweg.com": 'pbkdf2:sha256:260000$leXVKkMYNu60eQZR$0893397beb241931d33d2c996e66447a375d3b7923aa32fc4af6b80eec716fbe'
}
PAT_PORT = r'^any(?:\s+port\s+{\w+(?:,\w+)*})?$' PAT_PORT = r'^any(?:\s+port\s+{\w+(?:,\w+)*})?$'
PAT_PROT = r'^(?:tcp|udp)$' PAT_PROT = r'^(?:tcp|udp)$'
@ -28,11 +26,19 @@ def untaint(pattern, string):
raise ValueError(f'"{string}" is tainted') raise ValueError(f'"{string}" is tainted')
def create_app(): def create_app(config=None):
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
if config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=False)
else:
# load the test config if passed in
app.config.from_pyfile(config, silent=True)
@auth.verify_password @auth.verify_password
def verify_password(username, password): def verify_password(username, password):
users = get_users()
if username in users and \ if username in users and \
check_password_hash(users.get(username), password): check_password_hash(users.get(username), password):
return username return username

14
jail2ban/auth.py Normal file
View File

@ -0,0 +1,14 @@
from flask import current_app, g
import os
def get_users():
if 'users' not in g:
users = {}
authfile = current_app.config['AUTHFILE']
with current_app.open_resource(os.path.join(current_app.instance_path,
authfile)) as f:
for entry in f:
users.update({tuple(entry.split(b':', 1))})
g.users = users
return g.users