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 authored and James Valleroy committed 2022-10-08 18:53:21 -04:00
1 parent 6ea08fb93f
commit 317e83c38f
2 files changed
+11 -41

No files matched your search

+4 -7
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()
+82
View File
@@ -0,0 +1,82 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configure Syncthing."""
import grp
import os
import pwd
import shutil
import subprocess
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 augeas_load():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.add_transform('Xml.lns', CONF_FILE)
aug.load()
return aug
@privileged
def setup():
"""Perform post-install actions for Syncthing."""
# Create syncthing group if needed.
try:
grp.getgrnam('syncthing')
except KeyError:
subprocess.run(['addgroup', '--system', 'syncthing'], check=True)
# Create syncthing user if needed.
try:
pwd.getpwnam('syncthing')
except KeyError:
subprocess.run([
'adduser', '--system', '--ingroup', 'syncthing', '--home',
DATA_DIR, '--gecos', 'Syncthing file synchronization server',
'syncthing'
], check=True)
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR, mode=0o750)
shutil.chown(DATA_DIR, user='syncthing', group='syncthing')
@privileged
def setup_config():
"""Make configuration changes."""
# wait until the configuration file is created by the syncthing daemon
timeout = 300
while timeout > 0:
if os.path.exists(CONF_FILE):
break
timeout = timeout - 1
time.sleep(1)
aug = augeas_load()
# disable authentication missing notification as FreedomBox itself
# provides authentication
auth_conf = ('/configuration/options/unackedNotificationID'
'[#text="authenticationUserAndPassword"]')
conf_changed = bool(aug.remove('/files' + CONF_FILE + auth_conf))
# disable usage reporting notification by declining reporting
# if the user has not made a choice yet
usage_conf = '/configuration/options/urAccepted/#text'
if aug.get('/files' + CONF_FILE + usage_conf) == '0':
aug.set('/files' + CONF_FILE + usage_conf, '-1')
conf_changed = True
aug.save()
if conf_changed:
action_utils.service_try_restart('syncthing@syncthing')