Sunil Mohan Adapa b4e6c03bd7
coturn: New app to manage Coturn TURN/STUN server
- Shows URLs and shared secret that communication servers like matrix-synapse
should be configured to. Later we will implement auto-configuring those servers.

- Allow selecting domain for the sake of TLS/DTLS certificate installation.

- Simplify systemd service file options. Drop log file and pid file support as
they are not needed with systemd. Add security options.

- Set custom configuration file by overriding systemd service file options so
that we don't have a problem with conffile prompts.

- Implement functional tests (and automatic diagnostics).

- Custom icon selected from the Noun project as Coturn project does not have
one.

- Backup/restore configuration file and certificates.

- Document some questions regarding configuration options.

Tests performed:

- App is not listed in the app page if 'advanced' flag is disabled.

- App name, icon and short description shows up correctly in apps page.

- App name, icon, short description, description, manual link, enable/disable
button and diagnostics link show up currently in app page.

- Verify that configuration used by coturn server is the FreedomBox
configuration by checking the cert path in the log output.

- PID file is not created in /var/run/turnserver/. It goes into /dev/null
according to the log output.

- No log file is created other than what is collected by systemd from command
line.

- systemctl show coturn.service shows all the intended restrictions such as
NoNewPrivileges, Protect* options.

- Run functional tests.

- Ensure that backup of configuration file works by taking backup, changing the
secret and restoring. During backup and restore coturn should be stopped and
started as per logs.

- Build Debian package. No warnings about the copyright file.

- Enabling the app enables the service and runs it.

- Disabling the app disables the service and stop it.

- All diagnostics tests pass.

- Diagnostic tests show firewall port coturn-freedombox for internal and
external networks, service coturn, and each listening port for udp4, udp6, tcp4
and tcp6.

- Information in the firewall page shows up properly. Enabling the app opens
firewall ports, and disabling it closes them.

- When the app is installed, if a cert domain is available, it will be used.
When multiple domains are available, one of them is picked.

- Status shows 4 URLs with the currently selected domain and secret key.

- Changing domain to another domain succeeds and reflects in the status
information.

- When no domain is configured. Installing the app succeeds. No domain is shown
in the list of domains.

- When domain is changed, the certificates files in /etc/coturn/certs are
overwritten.

- Certificates have the ownership turnserver:turnserver. Public key is cert.pem
has 644 permissions. Private is pkey.pem has 600 permissions. /etc/coturn/certs
is owned by root:root.

- Let's encrypt certificates are setup immediately after install.

- Port forwarding information shows all ports except for relay ports.

- Trying to create a user with username 'turnserver' throws an error. This
happens even when coturn is not installed yet.

- After installing coturn, the configuration file /etc/coturn/freedombox.conf is
created with ownership root:turnserver and permissions 640. The directory
/etc/coturn is created with ownership root:root and permissions 755.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
[jvalleroy: Fix copied form_valid comment]
Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-05-02 18:51:23 -04:00

157 lines
4.8 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app to configure Coturn server.
"""
import json
import pathlib
from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth import app as app_module
from plinth import menu
from plinth.daemon import Daemon
from plinth.modules import names
from plinth.modules.firewall.components import Firewall
from plinth.modules.letsencrypt.components import LetsEncrypt
from plinth.modules.users.components import UsersAndGroups
from .manifest import backup # noqa, pylint: disable=unused-import
version = 1
managed_services = ['coturn']
managed_packages = ['coturn']
managed_paths = [pathlib.Path('/etc/coturn/')]
_description = [
_('Coturn is a server to facilitate audio/video calls and conferences by '
'providing an implementation of TURN and STUN protocols. WebRTC, SIP '
'and other communication servers can use it to establish a call between '
'parties who are otherwise unable connect to each other.'),
_('It is not meant to be used directly by users. Servers such as '
'matrix-synapse need to be configured with the details provided here.'),
]
port_forwarding_info = [
('UDP', 3478),
('TCP', 3478),
('UDP', 3479),
('TCP', 3479),
('UDP', 5349),
('TCP', 5349),
('UDP', 5350),
('TCP', 5350),
# XXX: Add relay ports here
]
app = None
class CoturnApp(app_module.App):
"""FreedomBox app for Coturn."""
app_id = 'coturn'
def __init__(self):
"""Create components for the app."""
super().__init__()
info = app_module.Info(app_id=self.app_id, version=version,
name=_('Coturn'), icon_filename='coturn',
short_description=_('VoIP Helper'),
description=_description, manual_page='Coturn')
self.add(info)
menu_item = menu.Menu('menu-coturn', info.name, info.short_description,
info.icon_filename, 'coturn:index',
parent_url_name='apps', advanced=True)
self.add(menu_item)
firewall = Firewall('firewall-coturn', info.name,
ports=['coturn-freedombox'], is_external=True)
self.add(firewall)
letsencrypt = LetsEncrypt(
'letsencrypt-coturn', domains=get_domains,
daemons=managed_services, should_copy_certificates=True,
private_key_path='/etc/coturn/certs/pkey.pem',
certificate_path='/etc/coturn/certs/cert.pem',
user_owner='turnserver', group_owner='turnserver',
managing_app='coturn')
self.add(letsencrypt)
daemon = Daemon(
'daemon-coturn', managed_services[0],
listen_ports=[(3478, 'udp4'), (3478, 'udp6'), (3478, 'tcp4'),
(3478, 'tcp6'), (3479, 'udp4'), (3479, 'udp6'),
(3479, 'tcp4'), (3479, 'tcp6'), (5349, 'udp4'),
(5349, 'udp6'), (5349, 'tcp4'), (5349, 'tcp6'),
(5350, 'udp4'), (5350, 'udp6'), (5350, 'tcp4'),
(5350, 'tcp6')])
self.add(daemon)
users_and_groups = UsersAndGroups('users-and-groups-coturn',
reserved_usernames=['turnserver'])
self.add(users_and_groups)
def init():
"""Initialize the Coturn module."""
global app
app = CoturnApp()
setup_helper = globals()['setup_helper']
if setup_helper.get_state() != 'needs-setup' and app.is_enabled():
app.set_enabled(True)
def setup(helper, old_version=None):
"""Install and configure the module."""
helper.install(managed_packages)
helper.call('post', actions.superuser_run, 'coturn', ['setup'])
helper.call('post', app.enable)
app.get_component('letsencrypt-coturn').setup_certificates()
def get_available_domains():
"""Return an iterator with all domains able to have a certificate."""
return (domain.name for domain in names.components.DomainName.list()
if domain.domain_type.can_have_certificate)
def get_domain():
"""Read TLS domain from config file select first available if none."""
config = get_config()
if config['realm']:
return get_config()['realm']
domain = next(get_available_domains(), None)
set_domain(domain)
return domain
def get_domains():
"""Return a list with the configured domains."""
domain = get_domain()
if domain:
return [domain]
return []
def set_domain(domain):
"""Set the TLS domain by writing a file to data directory."""
if domain:
actions.superuser_run('coturn', ['set-domain', domain])
def get_config():
"""Return the coturn server configuration."""
output = actions.superuser_run('coturn', ['get-config'])
return json.loads(output)