coturn: Use privileged decorator for actions

Tests:

- Initial setup of coturn succeeds
  - Configuration file is created and required configuration is set.
  - Coturn is restarted
- Coturn configuration is shown on app page.
- Changing the domain succeeds and coturn configuration reflects the new domain.

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:51:42 -04:00
1 parent 7ff050511c
commit 013caa28bc
3 files changed
+18 -54

No files matched your search

+5 -10
View File
@@ -1,16 +1,12 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app to configure Coturn server.
"""
"""FreedomBox app to configure Coturn server."""
import json
import logging
import pathlib
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from plinth import actions
from plinth import app as app_module
from plinth import menu
from plinth.daemon import Daemon
@@ -23,7 +19,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 = [
_('Coturn is a server to facilitate audio/video calls and conferences by '
@@ -109,7 +105,7 @@ class CoturnApp(app_module.App):
def setup(self, old_version):
"""Install and configure the app."""
super().setup(old_version)
actions.superuser_run('coturn', ['setup'])
privileged.setup()
if old_version == 0:
self.enable()
@@ -151,14 +147,13 @@ def get_domains():
def set_domain(domain):
"""Set the TLS domain by writing a file to data directory."""
if domain:
actions.superuser_run('coturn', ['set-domain', domain])
privileged.set_domain(domain)
notify_configuration_change()
def get_config():
"""Return the coturn server configuration."""
output = actions.superuser_run('coturn', ['get-config'])
config = json.loads(output)
config = privileged.get_config()
return TurnConfiguration(config['realm'], [], config['static_auth_secret'])
+91
View File
@@ -0,0 +1,91 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configuration helper for Coturn daemon."""
import pathlib
import random
import shutil
import string
import augeas
from plinth import action_utils
from plinth.actions import privileged
CONFIG_FILE = pathlib.Path('/etc/coturn/freedombox.conf')
def _key_path(key):
"""Return the augeas path for a key."""
return '/files' + str(CONFIG_FILE) + '/' + key
@privileged
def setup():
"""Setup Coturn server."""
CONFIG_FILE.parent.mkdir(exist_ok=True)
if not CONFIG_FILE.exists():
CONFIG_FILE.touch(0o640)
shutil.chown(CONFIG_FILE, group='turnserver')
action_utils.service_daemon_reload()
aug = augeas_load()
# XXX: Should we set external-ip
aug.set(_key_path('min-port'), '49152')
aug.set(_key_path('max-port'), '50175')
aug.set(_key_path('use-auth-secret'), 'true')
if not aug.get(_key_path('static-auth-secret')):
secret = ''.join(
random.choice(string.ascii_letters + string.digits)
for _ in range(64))
aug.set(_key_path('static-auth-secret'), secret)
aug.set(_key_path('cert'), '/etc/coturn/certs/cert.pem')
aug.set(_key_path('pkey'), '/etc/coturn/certs/pkey.pem')
aug.set(_key_path('no-tlsv1'), 'true')
aug.set(_key_path('no-tlsv1_1'), 'true')
aug.set(_key_path('no-cli'), 'true')
aug.set(_key_path('listening-ip[1]'), '::')
# Keep ::1 because at least two IP addresses of same class are needed for
# enabling alternate port (port + 1). This is in turn needed for NAT
# Behavior Discovery (RFC 5780).
aug.set(_key_path('listening-ip[2]'), '::1')
aug.save()
action_utils.service_try_restart('coturn')
@privileged
def get_config() -> dict[str, str]:
"""Return the current configuration."""
aug = augeas_load()
config = {
'static_auth_secret': aug.get(_key_path('static-auth-secret')),
'realm': aug.get(_key_path('realm')),
}
return config
@privileged
def set_domain(domain_name: str):
"""Set the TLS domain.
This value is usually not stored. So, set realm value even though it is not
needed to set realm for REST API based authentication.
"""
aug = augeas_load()
aug.set(_key_path('realm'), domain_name)
aug.save()
def augeas_load():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Simplevars/lens', 'Simplevars.lns')
aug.set('/augeas/load/Simplevars/incl[last() + 1]', str(CONFIG_FILE))
aug.load()
return aug
+2 -3
View File
@@ -1,7 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Views for Coturn app.
"""
"""Views for Coturn app."""
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
@@ -15,6 +13,7 @@ from . import forms
class CoturnAppView(views.AppView):
"""Serve configuration page."""
app_id = 'coturn'
template_name = 'coturn.html'
form_class = forms.CoturnForm