#6 Implement /list/<name> for #4

This commit is contained in:
Ruben van Staveren 2023-01-14 17:26:00 +01:00
parent 62d92d3c91
commit ea325b0686
Signed by: ruben
GPG Key ID: 886F6BECD477A93F
2 changed files with 23 additions and 1 deletions

View File

@ -81,6 +81,27 @@ def create_app():
'operation': 'flush',
'result': [x.decode('ascii') for x in res]})
@app.route("/list/<name>", methods=['GET'])
@auth.login_required
def list_table(name):
remote_user = auth.username()
name = untaint(PAT_NAME, name)
app.logger.info(f'Flushing table f2b-{name}'
f' in anchor f2b-jail/{remote_user}')
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(res),
re.MULTILINE|re.VERBOSE)]
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
def register():

View File

@ -44,7 +44,8 @@ def pfctl_table_op(anchor, **kwargs):
table = kwargs['table']
operation = kwargs['operation']
value = kwargs['value'] if 'value' in kwargs else None
cmd = [_SUDO, _PFCTL, '-a', anchor, '-t', table, '-T', operation, value]
verbose = '-v' if 'verbose' in kwargs and kwargs['verbose'] else None
cmd = [_SUDO, _PFCTL, '-a', anchor, '-t', table, verbose, '-T', operation, value]
logging.info('Running %s', cmd)