mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
apache: Use privileged decorator for actions
Tests: - Initial setup works when a new container is created - When transmission is enabled/disabled, the web configuration for it is enabled/disabled. - When radicale is enabled/disabled, the uwsgi configuration for it is enabled/disabled. - Sharing web configuration is disabled during backup and re-enabled. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
fdbe537529
commit
3e2900b48b
6 files changed
+66
-120
No files matched your search
@@ -1,12 +1,10 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
FreedomBox app for Apache server.
|
||||
"""
|
||||
"""FreedomBox app for Apache server."""
|
||||
|
||||
import os
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import app as app_module
|
||||
from plinth import cfg
|
||||
from plinth.daemon import Daemon, RelatedDaemon
|
||||
@@ -15,6 +13,8 @@ from plinth.modules.letsencrypt.components import LetsEncrypt
|
||||
from plinth.package import Packages
|
||||
from plinth.utils import format_lazy, is_valid_user_name
|
||||
|
||||
from . import privileged
|
||||
|
||||
|
||||
class ApacheApp(app_module.App):
|
||||
"""FreedomBox app for Apache web server."""
|
||||
@@ -60,9 +60,7 @@ class ApacheApp(app_module.App):
|
||||
def setup(self, old_version):
|
||||
"""Install and configure the app."""
|
||||
super().setup(old_version)
|
||||
actions.superuser_run('apache',
|
||||
['setup', '--old-version',
|
||||
str(old_version)])
|
||||
privileged.setup(old_version)
|
||||
self.enable()
|
||||
|
||||
|
||||
@@ -70,17 +68,17 @@ class ApacheApp(app_module.App):
|
||||
|
||||
|
||||
def uws_directory_of_user(user):
|
||||
"""Returns the directory of the given user's website."""
|
||||
"""Return the directory of the given user's website."""
|
||||
return '/home/{}/public_html'.format(user)
|
||||
|
||||
|
||||
def uws_url_of_user(user):
|
||||
"""Returns the url path of the given user's website."""
|
||||
"""Return the url path of the given user's website."""
|
||||
return '/~{}/'.format(user)
|
||||
|
||||
|
||||
def user_of_uws_directory(directory):
|
||||
"""Returns the user of a given user website directory."""
|
||||
"""Return the user of a given user website directory."""
|
||||
if directory.startswith('/home/'):
|
||||
pos_ini = 6
|
||||
elif directory.startswith('home/'):
|
||||
@@ -97,7 +95,7 @@ def user_of_uws_directory(directory):
|
||||
|
||||
|
||||
def user_of_uws_url(url):
|
||||
"""Returns the user of a given user website url path."""
|
||||
"""Return the user of a given user website url path."""
|
||||
MISSING = -1
|
||||
|
||||
pos_ini = url.find('~')
|
||||
@@ -113,7 +111,7 @@ def user_of_uws_url(url):
|
||||
|
||||
|
||||
def uws_directory_of_url(url):
|
||||
"""Returns the directory of the user's website for the given url path.
|
||||
"""Return the directory of the user's website for the given url path.
|
||||
|
||||
Note: It doesn't return the full OS file path to the url path!
|
||||
"""
|
||||
@@ -121,7 +119,7 @@ def uws_directory_of_url(url):
|
||||
|
||||
|
||||
def uws_url_of_directory(directory):
|
||||
"""Returns the url base path of the user's website for the given OS path.
|
||||
"""Return the url base path of the user's website for the given OS path.
|
||||
|
||||
Note: It doesn't return the url path for the file!
|
||||
"""
|
||||
@@ -129,10 +127,10 @@ def uws_url_of_directory(directory):
|
||||
|
||||
|
||||
def get_users_with_website():
|
||||
"""Returns a dictionary of users with actual website subdirectory."""
|
||||
"""Return a dictionary of users with actual website subdirectory."""
|
||||
|
||||
def lst_sub_dirs(directory):
|
||||
"""Returns the list of subdirectories of the given directory."""
|
||||
"""Return the list of subdirectories of the given directory."""
|
||||
return [
|
||||
name for name in os.listdir(directory)
|
||||
if os.path.isdir(os.path.join(directory, name))
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
App component for other apps to use Apache configuration functionality.
|
||||
"""
|
||||
"""App component for other apps to use Apache configuration functionality."""
|
||||
|
||||
import re
|
||||
import subprocess
|
||||
@@ -9,7 +7,9 @@ import subprocess
|
||||
from django.utils.text import format_lazy
|
||||
from django.utils.translation import gettext_lazy
|
||||
|
||||
from plinth import action_utils, actions, app
|
||||
from plinth import action_utils, app
|
||||
|
||||
from . import privileged
|
||||
|
||||
|
||||
class Webserver(app.LeaderComponent):
|
||||
@@ -47,14 +47,11 @@ class Webserver(app.LeaderComponent):
|
||||
|
||||
def enable(self):
|
||||
"""Enable the Apache configuration."""
|
||||
actions.superuser_run(
|
||||
'apache', ['enable', '--name', self.web_name, '--kind', self.kind])
|
||||
privileged.enable(self.web_name, self.kind)
|
||||
|
||||
def disable(self):
|
||||
"""Disable the Apache configuration."""
|
||||
actions.superuser_run(
|
||||
'apache',
|
||||
['disable', '--name', self.web_name, '--kind', self.kind])
|
||||
privileged.disable(self.web_name, self.kind)
|
||||
|
||||
def diagnose(self):
|
||||
"""Check if the web path is accessible by clients.
|
||||
@@ -99,13 +96,11 @@ class Uwsgi(app.LeaderComponent):
|
||||
|
||||
def enable(self):
|
||||
"""Enable the uWSGI configuration."""
|
||||
actions.superuser_run('apache',
|
||||
['uwsgi-enable', '--name', self.uwsgi_name])
|
||||
privileged.uwsgi_enable(self.uwsgi_name)
|
||||
|
||||
def disable(self):
|
||||
"""Disable the uWSGI configuration."""
|
||||
actions.superuser_run('apache',
|
||||
['uwsgi-disable', '--name', self.uwsgi_name])
|
||||
privileged.uwsgi_disable(self.uwsgi_name)
|
||||
|
||||
def is_running(self):
|
||||
"""Return whether the uWSGI daemon is running with configuration."""
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Configure Apache web server."""
|
||||
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from plinth import action_utils
|
||||
from plinth.actions import privileged
|
||||
|
||||
|
||||
def _get_sort_key_of_version(version):
|
||||
"""Return the sort key for a given version string.
|
||||
|
||||
Simple implementation hoping that PHP Apache module version numbers will be
|
||||
simple.
|
||||
|
||||
"""
|
||||
parts = []
|
||||
for part in version.split('.'):
|
||||
try:
|
||||
parts.append(int(part))
|
||||
except ValueError:
|
||||
parts.append(part)
|
||||
|
||||
return parts
|
||||
|
||||
|
||||
def _sort_versions(versions):
|
||||
"""Return a list of sorted version strings."""
|
||||
return sorted(versions, key=_get_sort_key_of_version, reverse=True)
|
||||
|
||||
|
||||
def _disable_mod_php(webserver):
|
||||
"""Disable all mod_php versions.
|
||||
|
||||
Idempotent and harmless if all or no PHP modules are identified.
|
||||
Problematic if only some modules are found.
|
||||
|
||||
"""
|
||||
paths = glob.glob('/etc/apache2/mods-available/php*.conf')
|
||||
versions = []
|
||||
for path in paths:
|
||||
match = re.search(r'\/php(.*)\.conf$', path)
|
||||
if match:
|
||||
versions.append(match[1])
|
||||
|
||||
versions = _sort_versions(versions)
|
||||
|
||||
for version in versions:
|
||||
webserver.disable('php' + version, kind='module')
|
||||
|
||||
|
||||
@privileged
|
||||
def setup(old_version: int):
|
||||
"""Setup Apache configuration."""
|
||||
# Regenerate the snakeoil self-signed SSL certificate. This is so that
|
||||
# FreedomBox images don't all have the same certificate. When FreedomBox
|
||||
# package is installed via apt, don't regenerate. When upgrading to newer
|
||||
# version of Apache FreedomBox app and setting up for the first time don't
|
||||
# regenerate.
|
||||
if action_utils.is_disk_image() and old_version == 0:
|
||||
subprocess.run([
|
||||
'make-ssl-cert', 'generate-default-snakeoil', '--force-overwrite'
|
||||
], check=True)
|
||||
# In case the certificate has been removed after ssl-cert is installed
|
||||
# on a fresh Debian machine.
|
||||
elif not os.path.exists('/etc/ssl/certs/ssl-cert-snakeoil.pem'):
|
||||
subprocess.run(['make-ssl-cert', 'generate-default-snakeoil'],
|
||||
check=True)
|
||||
|
||||
with action_utils.WebserverChange() as webserver:
|
||||
# Disable mod_php as we have switched to mod_fcgi + php-fpm. Disable
|
||||
# before switching away from mpm_prefork otherwise switching fails due
|
||||
# dependency.
|
||||
_disable_mod_php(webserver)
|
||||
|
||||
# set the prefork worker model
|
||||
webserver.disable('mpm_worker', kind='module')
|
||||
webserver.disable('mpm_prefork', kind='module')
|
||||
webserver.enable('mpm_event', kind='module')
|
||||
|
||||
# enable miscellaneous modules.
|
||||
webserver.enable('proxy', kind='module')
|
||||
webserver.enable('proxy_http', kind='module')
|
||||
webserver.enable('proxy_fcgi', kind='module')
|
||||
webserver.enable('proxy_html', kind='module')
|
||||
webserver.enable('rewrite', kind='module')
|
||||
webserver.enable('macro', kind='module')
|
||||
|
||||
# Disable /server-status page to avoid leaking private info.
|
||||
webserver.disable('status', kind='module')
|
||||
|
||||
# Enable HTTP/2 protocol
|
||||
webserver.enable('http2', kind='module')
|
||||
|
||||
# Enable shared object cache needed for OSCP stapling. Needed by
|
||||
# mod_ssl.
|
||||
webserver.enable('socache_shmcb', kind='module')
|
||||
|
||||
# switch to mod_ssl from mod_gnutls
|
||||
webserver.disable('gnutls', kind='module')
|
||||
webserver.enable('ssl', kind='module')
|
||||
|
||||
# enable mod_alias for RedirectMatch
|
||||
webserver.enable('alias', kind='module')
|
||||
|
||||
# enable mod_headers for HSTS
|
||||
webserver.enable('headers', kind='module')
|
||||
|
||||
# Various modules for authentication/authorization
|
||||
webserver.enable('authnz_ldap', kind='module')
|
||||
webserver.enable('auth_pubtkt', kind='module')
|
||||
|
||||
# enable some critical modules to avoid restart while installing
|
||||
# FreedomBox applications.
|
||||
webserver.disable('cgi', kind='module') # For process MPMs
|
||||
webserver.enable('cgid', kind='module') # For threaded MPMs
|
||||
webserver.enable('proxy_uwsgi', kind='module')
|
||||
webserver.enable('proxy_wstunnel', kind='module')
|
||||
|
||||
# enable configuration for PHP-FPM
|
||||
webserver.enable('php-fpm-freedombox', kind='config')
|
||||
|
||||
# enable users to share files uploaded to ~/public_html
|
||||
webserver.enable('userdir', kind='module')
|
||||
|
||||
# setup freedombox site
|
||||
webserver.enable('freedombox', kind='config')
|
||||
webserver.enable('freedombox-tls', kind='config')
|
||||
|
||||
# enable serving Debian javascript libraries
|
||||
webserver.enable('javascript-common', kind='config')
|
||||
|
||||
# default sites
|
||||
webserver.disable('000-default', kind='site')
|
||||
webserver.disable('default-tls', kind='site')
|
||||
webserver.disable('default-ssl', kind='site')
|
||||
webserver.disable('plinth', kind='site')
|
||||
webserver.disable('plinth-ssl', kind='site')
|
||||
webserver.enable('freedombox-default', kind='site')
|
||||
|
||||
|
||||
# TODO: Check that the (name, kind) is a managed by FreedomBox before
|
||||
# performing operation.
|
||||
@privileged
|
||||
def enable(name: str, kind: str):
|
||||
"""Enable an Apache site/config/module."""
|
||||
_assert_kind(kind)
|
||||
action_utils.webserver_enable(name, kind)
|
||||
|
||||
|
||||
@privileged
|
||||
def disable(name: str, kind: str):
|
||||
"""Disable an Apache site/config/module."""
|
||||
_assert_kind(kind)
|
||||
action_utils.webserver_disable(name, kind)
|
||||
|
||||
|
||||
def _assert_kind(kind: str):
|
||||
"""Raise and exception if kind parameter has an unexpected value."""
|
||||
if kind not in ('site', 'config', 'module'):
|
||||
raise ValueError('Invalid value for parameter kind')
|
||||
|
||||
|
||||
@privileged
|
||||
def uwsgi_enable(name: str):
|
||||
"""Enable uWSGI configuration and reload."""
|
||||
action_utils.uwsgi_enable(name)
|
||||
|
||||
|
||||
@privileged
|
||||
def uwsgi_disable(name: str):
|
||||
"""Disable uWSGI configuration and reload."""
|
||||
action_utils.uwsgi_disable(name)
|
||||
@@ -47,27 +47,22 @@ def test_webserver_is_enabled(webserver_is_enabled):
|
||||
webserver_is_enabled.assert_has_calls([call('test-config', kind='module')])
|
||||
|
||||
|
||||
@patch('plinth.actions.superuser_run')
|
||||
def test_webserver_enable(superuser_run):
|
||||
@patch('plinth.modules.apache.privileged.enable')
|
||||
def test_webserver_enable(enable):
|
||||
"""Test that enabling webserver configuration works."""
|
||||
webserver = Webserver('test-webserver', 'test-config', kind='module')
|
||||
|
||||
webserver.enable()
|
||||
superuser_run.assert_has_calls([
|
||||
call('apache', ['enable', '--name', 'test-config', '--kind', 'module'])
|
||||
])
|
||||
enable.assert_has_calls([call('test-config', 'module')])
|
||||
|
||||
|
||||
@patch('plinth.actions.superuser_run')
|
||||
def test_webserver_disable(superuser_run):
|
||||
@patch('plinth.modules.apache.privileged.disable')
|
||||
def test_webserver_disable(disable):
|
||||
"""Test that disabling webserver configuration works."""
|
||||
webserver = Webserver('test-webserver', 'test-config', kind='module')
|
||||
|
||||
webserver.disable()
|
||||
superuser_run.assert_has_calls([
|
||||
call('apache',
|
||||
['disable', '--name', 'test-config', '--kind', 'module'])
|
||||
])
|
||||
disable.assert_has_calls([call('test-config', 'module')])
|
||||
|
||||
|
||||
@patch('plinth.modules.apache.components.diagnose_url')
|
||||
@@ -132,24 +127,22 @@ def test_uwsgi_is_enabled(uwsgi_is_enabled, service_is_enabled):
|
||||
assert not uwsgi.is_enabled()
|
||||
|
||||
|
||||
@patch('plinth.actions.superuser_run')
|
||||
def test_uwsgi_enable(superuser_run):
|
||||
@patch('plinth.modules.apache.privileged.uwsgi_enable')
|
||||
def test_uwsgi_enable(enable):
|
||||
"""Test that enabling uwsgi configuration works."""
|
||||
uwsgi = Uwsgi('test-uwsgi', 'test-config')
|
||||
|
||||
uwsgi.enable()
|
||||
superuser_run.assert_has_calls(
|
||||
[call('apache', ['uwsgi-enable', '--name', 'test-config'])])
|
||||
enable.assert_has_calls([call('test-config')])
|
||||
|
||||
|
||||
@patch('plinth.actions.superuser_run')
|
||||
def test_uwsgi_disable(superuser_run):
|
||||
@patch('plinth.modules.apache.privileged.uwsgi_disable')
|
||||
def test_uwsgi_disable(disable):
|
||||
"""Test that disabling uwsgi configuration works."""
|
||||
uwsgi = Uwsgi('test-uwsgi', 'test-config')
|
||||
|
||||
uwsgi.disable()
|
||||
superuser_run.assert_has_calls(
|
||||
[call('apache', ['uwsgi-disable', '--name', 'test-config'])])
|
||||
disable.assert_has_calls([call('test-config')])
|
||||
|
||||
|
||||
@patch('plinth.action_utils.service_is_running')
|
||||
|
||||
Reference in new issue
Block a user