wireguard: Us privileged decorator for actions

Tests:

- Functional tests work (uninstall test fails to no backup component,
  intermittent failure)
- Showing status information works
  - In the main app page for server and clients
  - When showing server details
  - When showing client details

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-08-26 15:14:20 -07:00 committed by James Valleroy
parent 66c1ddc404
commit 0c936512c4
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 13 additions and 47 deletions

View File

@ -1,40 +1,27 @@
#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Configuration helper for WireGuard.
"""
"""Configuration helper for WireGuard."""
import argparse
import json
import subprocess
from plinth.actions import privileged
SERVER_INTERFACE = 'wg0'
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('get-info',
help='Get info for each configured interface')
subparsers.required = True
return parser.parse_args()
def _get_info():
@privileged
def get_info() -> dict[str, dict]:
"""Return info for each configured interface."""
output = subprocess.check_output(['wg', 'show', 'all',
'dump']).decode().strip()
lines = output.split('\n')
interfaces = {}
interfaces: dict[str, dict] = {}
for line in lines:
if not line:
continue
fields = line.split()
fields = [field if field != '(none)' else None for field in fields]
fields = [
field if field != '(none)' else None for field in line.split()
]
interface_name = fields[0]
if interface_name in interfaces:
latest_handshake = int(fields[5]) if int(fields[5]) else None
@ -61,21 +48,3 @@ def _get_info():
}
return interfaces
def subcommand_get_info(_):
"""Print info for each configured interface."""
print(json.dumps(_get_info()))
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()

View File

@ -1,19 +1,17 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Utilities for managing WireGuard.
"""
"""Utilities for managing WireGuard."""
import datetime
import json
import logging
import subprocess
import time
from plinth import actions
from plinth import app as app_module
from plinth import network
from plinth.utils import import_from_gi
from . import privileged
nm = import_from_gi('NM', '1.0')
IP_TEMPLATE = '10.84.0.{}'
@ -70,8 +68,7 @@ def get_nm_info():
def get_info():
"""Return server and clients info."""
output = actions.superuser_run('wireguard', ['get-info'])
status = json.loads(output)
status = privileged.get_info()
nm_info = get_nm_info()