From 24af41e6a8e3a6690657523cd4c7556ee2e9d5e9 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 25 Aug 2022 11:49:00 -0700 Subject: [PATCH] i2p: Use privileged decorator for actions Tests: - Functional tests work. - Initial setup works - Sometimes fails to write tunnel configuration (See #2127). - Favorites are created as listed in FAVORITES in resources.py - Tunnels are created: I2P HTTP Proxy, I2P HTTPS Proxy, Irc2P Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- actions/i2p | 82 -------------------------------- plinth/modules/i2p/__init__.py | 29 +++-------- plinth/modules/i2p/privileged.py | 26 ++++++++++ 3 files changed, 32 insertions(+), 105 deletions(-) delete mode 100755 actions/i2p create mode 100644 plinth/modules/i2p/privileged.py diff --git a/actions/i2p b/actions/i2p deleted file mode 100755 index 0f5e9731b..000000000 --- a/actions/i2p +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/python3 -# SPDX-License-Identifier: AGPL-3.0-or-later -""" -Wrapper to list and handle system services -""" - -import argparse -import os - -from plinth import cfg -from plinth.modules.i2p.helpers import RouterEditor, TunnelEditor - -cfg.read() -module_config_path = os.path.join(cfg.config_dir, 'modules-enabled') - -I2P_CONF_DIR = '/var/lib/i2p/i2p-config' - - -def parse_arguments(): - """Return parsed command line arguments as dictionary.""" - parser = argparse.ArgumentParser() - subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') - - subparser = subparsers.add_parser( - 'add-favorite', help='Add an eepsite to the list of favorites') - subparser.add_argument('--name', help='Name of the entry', required=True) - subparser.add_argument('--url', help='URL of the entry', required=True) - subparser.add_argument('--description', help='Short description', - required=False) - subparser.add_argument('--icon', help='URL to icon', required=False) - - subparser = subparsers.add_parser('set-tunnel-property', - help='Modify configuration of a tunnel') - subparser.add_argument('--name', help='Name of the tunnel', required=True) - subparser.add_argument('--property', help='Property to modify', - required=True) - subparser.add_argument('--value', help='Value to assign', required=True) - - subparsers.required = True - return parser.parse_args() - - -def subcommand_set_tunnel_property(arguments): - """Modify the configuration file for a certain tunnel.""" - editor = TunnelEditor() - editor \ - .read_conf() \ - .set_tunnel_idx(arguments.name) \ - .set_tunnel_prop(arguments.property, arguments.value) \ - .write_conf() - print('Updated "{property}" of {filename} to {value}'.format( - property=editor.calc_prop_path(arguments.property), - filename=editor.conf_filename, value=arguments.value)) - - -def subcommand_add_favorite(arguments): - """ - Adds a favorite to the router.config - - :param arguments: - :type arguments: - """ - url = arguments.url - - editor = RouterEditor() - editor.read_conf().add_favorite(arguments.name, url, arguments.description, - arguments.icon).write_conf() - - print('Added {} to favorites'.format(url)) - - -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() diff --git a/plinth/modules/i2p/__init__.py b/plinth/modules/i2p/__init__.py index aad01f1e1..b0e1458a2 100644 --- a/plinth/modules/i2p/__init__.py +++ b/plinth/modules/i2p/__init__.py @@ -1,11 +1,8 @@ # SPDX-License-Identifier: AGPL-3.0-or-later -""" -FreedomBox app to configure I2P. -""" +"""FreedomBox app to configure I2P.""" from django.utils.translation import gettext_lazy as _ -from plinth import actions from plinth import app as app_module from plinth import frontpage, menu from plinth.daemon import Daemon @@ -16,7 +13,7 @@ from plinth.modules.i2p.resources import FAVORITES from plinth.modules.users.components import UsersAndGroups from plinth.package import Packages -from . import manifest +from . import manifest, privileged _description = [ _('The Invisible Internet Project is an anonymous network layer intended ' @@ -103,25 +100,11 @@ class I2PApp(app_module.App): self.disable() # Add favorites to the configuration for fav in FAVORITES: - args = [ - 'add-favorite', - '--name', - fav.get('name'), - '--url', - fav.get('url'), - ] - if 'icon' in fav: - args.extend(['--icon', fav.get('icon')]) - - if 'description' in fav: - args.extend(['--description', fav.get('description')]) - - actions.superuser_run('i2p', args) + privileged.add_favorite(fav['name'], fav['url'], + fav.get('description'), fav.get('icon')) # Tunnels to all interfaces for tunnel in tunnels_to_manage: - actions.superuser_run('i2p', [ - 'set-tunnel-property', '--name', tunnel, '--property', - 'interface', '--value', '0.0.0.0' - ]) + privileged.set_tunnel_property(tunnel, 'interface', '0.0.0.0') + self.enable() diff --git a/plinth/modules/i2p/privileged.py b/plinth/modules/i2p/privileged.py new file mode 100644 index 000000000..bd3c50395 --- /dev/null +++ b/plinth/modules/i2p/privileged.py @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +"""Configure I2P.""" + +from typing import Optional + +from plinth.actions import privileged +from plinth.modules.i2p.helpers import RouterEditor, TunnelEditor + + +@privileged +def set_tunnel_property(name: str, property_: str, value: str): + """Modify the configuration file for a certain tunnel.""" + editor = TunnelEditor() + editor \ + .read_conf() \ + .set_tunnel_idx(name) \ + .set_tunnel_prop(property_, value) \ + .write_conf() + + +@privileged +def add_favorite(name: str, url: str, description: Optional[str], + icon: Optional[str]): + """Add a favorite to router.config.""" + editor = RouterEditor() + editor.read_conf().add_favorite(name, url, description, icon).write_conf()