mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
radicale: Use privileged decorator for actions
Tests: - Functional tests work - When the app is enabled, if the log path does not exist, it is created /var/log/radicale. - Not tested: upgrading from older version to 3.x - Setting the access rights works. It is reflected in the app page and configuration file /etc/radicale/config. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
8478450467
commit
89a4d25909
@ -1,76 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Configuration helper for Radicale.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
|
||||
import augeas
|
||||
|
||||
from plinth import action_utils
|
||||
|
||||
CONFIG_FILE = '/etc/radicale/config'
|
||||
LOG_PATH = '/var/log/radicale'
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
"""Return parsed command line arguments as dictionary."""
|
||||
parser = argparse.ArgumentParser()
|
||||
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
|
||||
|
||||
configure = subparsers.add_parser('configure',
|
||||
help='Configure various options')
|
||||
configure.add_argument('--rights_type',
|
||||
help='Set the rights type for radicale')
|
||||
subparsers.add_parser('fix-paths', help='Ensure paths exists')
|
||||
|
||||
subparsers.required = True
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def subcommand_configure(arguments):
|
||||
"""Sets the radicale rights type to a particular value"""
|
||||
if arguments.rights_type == 'owner_only':
|
||||
# Default rights file is equivalent to owner_only.
|
||||
arguments.rights_type = 'from_file'
|
||||
|
||||
aug = load_augeas()
|
||||
aug.set('/files' + CONFIG_FILE + '/rights/type', arguments.rights_type)
|
||||
aug.save()
|
||||
|
||||
action_utils.service_try_restart('uwsgi')
|
||||
|
||||
|
||||
def subcommand_fix_paths(_):
|
||||
"""Fix log path to work around a bug."""
|
||||
# Workaround for bug in radicale's uwsgi script (#931201)
|
||||
if not os.path.exists(LOG_PATH):
|
||||
os.makedirs(LOG_PATH)
|
||||
|
||||
|
||||
def load_augeas():
|
||||
"""Initialize Augeas."""
|
||||
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
||||
augeas.Augeas.NO_MODL_AUTOLOAD)
|
||||
|
||||
# INI file lens
|
||||
aug.set('/augeas/load/Puppet/lens', 'Puppet.lns')
|
||||
aug.set('/augeas/load/Puppet/incl[last() + 1]', CONFIG_FILE)
|
||||
|
||||
aug.load()
|
||||
return aug
|
||||
|
||||
|
||||
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()
|
||||
@ -8,7 +8,6 @@ import logging
|
||||
import augeas
|
||||
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.modules.apache.components import Uwsgi, Webserver
|
||||
@ -18,7 +17,7 @@ from plinth.modules.users.components import UsersAndGroups
|
||||
from plinth.package import Packages, install
|
||||
from plinth.utils import Version, format_lazy
|
||||
|
||||
from . import manifest
|
||||
from . import manifest, privileged
|
||||
|
||||
_description = [
|
||||
format_lazy(
|
||||
@ -93,7 +92,7 @@ class RadicaleApp(app_module.App):
|
||||
|
||||
def enable(self):
|
||||
"""Fix missing directories before enabling radicale."""
|
||||
actions.superuser_run('radicale', ['fix-paths'])
|
||||
privileged.fix_paths()
|
||||
super().enable()
|
||||
|
||||
def setup(self, old_version):
|
||||
@ -113,8 +112,7 @@ class RadicaleApp(app_module.App):
|
||||
|
||||
rights = get_rights_value()
|
||||
install(['radicale'], force_configuration='new')
|
||||
actions.superuser_run('radicale',
|
||||
['configure', '--rights_type', rights])
|
||||
privileged.configure(rights)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
47
plinth/modules/radicale/privileged.py
Normal file
47
plinth/modules/radicale/privileged.py
Normal file
@ -0,0 +1,47 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Configure Radicale."""
|
||||
|
||||
import os
|
||||
|
||||
import augeas
|
||||
|
||||
from plinth import action_utils
|
||||
from plinth.actions import privileged
|
||||
|
||||
CONFIG_FILE = '/etc/radicale/config'
|
||||
LOG_PATH = '/var/log/radicale'
|
||||
|
||||
|
||||
@privileged
|
||||
def configure(rights_type: str):
|
||||
"""Set the radicale rights type to a particular value."""
|
||||
if rights_type == 'owner_only':
|
||||
# Default rights file is equivalent to owner_only.
|
||||
rights_type = 'from_file'
|
||||
|
||||
aug = load_augeas()
|
||||
aug.set('/files' + CONFIG_FILE + '/rights/type', rights_type)
|
||||
aug.save()
|
||||
|
||||
action_utils.service_try_restart('uwsgi')
|
||||
|
||||
|
||||
@privileged
|
||||
def fix_paths():
|
||||
"""Fix log path to work around a bug."""
|
||||
# Workaround for bug in radicale's uwsgi script (#931201)
|
||||
if not os.path.exists(LOG_PATH):
|
||||
os.makedirs(LOG_PATH)
|
||||
|
||||
|
||||
def load_augeas():
|
||||
"""Initialize Augeas."""
|
||||
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
||||
augeas.Augeas.NO_MODL_AUTOLOAD)
|
||||
|
||||
# INI file lens
|
||||
aug.set('/augeas/load/Puppet/lens', 'Puppet.lns')
|
||||
aug.set('/augeas/load/Puppet/incl[last() + 1]', CONFIG_FILE)
|
||||
|
||||
aug.load()
|
||||
return aug
|
||||
@ -6,10 +6,9 @@ Views for radicale module.
|
||||
from django.contrib import messages
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth.views import AppView
|
||||
|
||||
from . import get_rights_value
|
||||
from . import get_rights_value, privileged
|
||||
from .forms import RadicaleForm
|
||||
|
||||
|
||||
@ -28,9 +27,7 @@ class RadicaleAppView(AppView):
|
||||
"""Change the access control of Radicale service."""
|
||||
data = form.cleaned_data
|
||||
if get_rights_value() != data['access_rights']:
|
||||
actions.superuser_run(
|
||||
'radicale',
|
||||
['configure', '--rights_type', data['access_rights']])
|
||||
privileged.configure(data['access_rights'])
|
||||
messages.success(self.request,
|
||||
_('Access rights configuration updated'))
|
||||
return super().form_valid(form)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user