syncthing: Use privileged decorator for actions

Tests:

- Functional tests succeed (noticed intermittent failure)
- Initial setup succeeds
  - User/group are created. /var/lib/syncthing is created with proper user/group
    ownership.
  - In configuration file, authentication notification is disabled
- Syncthing web interface is accessible
  - Authentication related notification is not shown.

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 14:34:46 -07:00 committed by James Valleroy
parent 6ea08fb93f
commit 317e83c38f
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 11 additions and 41 deletions

View File

@ -1,11 +1,8 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app to configure Syncthing.
"""
"""FreedomBox app to configure Syncthing."""
from django.utils.translation import gettext_lazy as _
from plinth import actions
from plinth import app as app_module
from plinth import cfg, frontpage, menu
from plinth.daemon import Daemon
@ -17,7 +14,7 @@ from plinth.modules.users.components import UsersAndGroups
from plinth.package import Packages
from plinth.utils import format_lazy
from . import manifest
from . import manifest, privileged
_description = [
_('Syncthing is an application to synchronize files across multiple '
@ -106,13 +103,13 @@ class SyncthingApp(app_module.App):
def setup(self, old_version):
"""Install and configure the app."""
super().setup(old_version)
actions.superuser_run('syncthing', ['setup'])
privileged.setup()
add_user_to_share_group(SYSTEM_USER, SyncthingApp.DAEMON)
if not old_version:
self.enable()
actions.superuser_run('syncthing', ['setup-config'])
privileged.setup_config()
if old_version == 1 and self.is_enabled():
self.get_component('firewall-syncthing-ports').enable()

View File

@ -1,10 +1,6 @@
#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Configuration helper for Syncthing.
"""
"""Configure Syncthing."""
import argparse
import grp
import os
import pwd
@ -15,24 +11,12 @@ import time
import augeas
from plinth import action_utils
from plinth.actions import privileged
DATA_DIR = '/var/lib/syncthing'
CONF_FILE = DATA_DIR + '/.config/syncthing/config.xml'
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('setup', help='Setup Syncthing')
subparsers.add_parser('setup-config', help='Setup Syncthing configuration')
subparsers.required = True
return parser.parse_args()
def augeas_load():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
@ -42,8 +26,9 @@ def augeas_load():
return aug
def subcommand_setup(_):
"""Actions to be performed before installing Syncthing"""
@privileged
def setup():
"""Perform post-install actions for Syncthing."""
# Create syncthing group if needed.
try:
grp.getgrnam('syncthing')
@ -65,7 +50,8 @@ def subcommand_setup(_):
shutil.chown(DATA_DIR, user='syncthing', group='syncthing')
def subcommand_setup_config(_):
@privileged
def setup_config():
"""Make configuration changes."""
# wait until the configuration file is created by the syncthing daemon
timeout = 300
@ -94,16 +80,3 @@ def subcommand_setup_config(_):
if conf_changed:
action_utils.service_try_restart('syncthing@syncthing')
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()