mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
searx: Use privileged decorator for actions
Tests: - Functional tests work - Initial setup works - UWSGI configuration is created and daemon is running. - Enabling and disabling public access works. Public access file is created/removed. App page shows current value. If exception is raised, error is shown properly. - Setting safe search to all three values works. Configuration file is updated properly. App page shows current value properly. If exception is raised, error is shown properly. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
8db063b59b
commit
52f42a4f74
4 files changed
+33
-86
No files matched your search
@@ -1,13 +1,10 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
FreedomBox app to configure Searx.
|
||||
"""
|
||||
"""FreedomBox app to configure Searx."""
|
||||
|
||||
import os
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import app as app_module
|
||||
from plinth import frontpage, menu
|
||||
from plinth.modules.apache.components import Uwsgi, Webserver
|
||||
@@ -16,7 +13,7 @@ from plinth.modules.firewall.components import Firewall
|
||||
from plinth.modules.users.components import UsersAndGroups
|
||||
from plinth.package import Packages
|
||||
|
||||
from . import manifest
|
||||
from . import manifest, privileged
|
||||
|
||||
_description = [
|
||||
_('Searx is a privacy-respecting Internet metasearch engine. '
|
||||
@@ -95,9 +92,9 @@ class SearxApp(app_module.App):
|
||||
def setup(self, old_version):
|
||||
"""Install and configure the app."""
|
||||
super().setup(old_version)
|
||||
actions.superuser_run('searx', ['setup'])
|
||||
privileged.setup()
|
||||
if not old_version or old_version < 3:
|
||||
actions.superuser_run('searx', ['disable-public-access'])
|
||||
privileged.disable_public_access()
|
||||
self.enable()
|
||||
self.set_shortcut_login_required(True)
|
||||
|
||||
@@ -115,12 +112,6 @@ class SearxWebserverAuth(Webserver):
|
||||
super().enable()
|
||||
|
||||
|
||||
def get_safe_search_setting():
|
||||
"""Get the current value of the safe search setting for Searx."""
|
||||
value = actions.superuser_run('searx', ['get-safe-search'])
|
||||
return int(value.strip())
|
||||
|
||||
|
||||
def is_public_access_enabled():
|
||||
"""Check whether public access is enabled for Searx."""
|
||||
return os.path.exists(manifest.PUBLIC_ACCESS_SETTING_FILE)
|
||||
@@ -128,7 +119,7 @@ def is_public_access_enabled():
|
||||
|
||||
def enable_public_access():
|
||||
"""Allow Searx app to be accessed by anyone with access."""
|
||||
actions.superuser_run('searx', ['enable-public-access'])
|
||||
privileged.enable_public_access()
|
||||
app = app_module.App.get('searx')
|
||||
app.get_component('webserver-searx-auth').disable()
|
||||
app.set_shortcut_login_required(False)
|
||||
@@ -136,7 +127,7 @@ def enable_public_access():
|
||||
|
||||
def disable_public_access():
|
||||
"""Allow Searx app to be accessed by logged-in users only."""
|
||||
actions.superuser_run('searx', ['disable-public-access'])
|
||||
privileged.disable_public_access()
|
||||
app = app_module.App.get('searx')
|
||||
app.get_component('webserver-searx-auth').enable()
|
||||
app.set_shortcut_login_required(True)
|
||||
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Django form for configuring Searx.
|
||||
"""
|
||||
"""Django form for configuring Searx."""
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -9,6 +7,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
|
||||
class SearxForm(forms.Form):
|
||||
"""Searx configuration form."""
|
||||
|
||||
safe_search = forms.ChoiceField(
|
||||
label=_('Safe Search'), help_text=_(
|
||||
'Select the default family filter to apply to your search results.'
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Configure searx."""
|
||||
|
||||
import gzip
|
||||
import os
|
||||
import pathlib
|
||||
import secrets
|
||||
import shutil
|
||||
|
||||
import augeas
|
||||
import yaml
|
||||
|
||||
from plinth import action_utils
|
||||
from plinth.actions import privileged
|
||||
from plinth.modules.searx.manifest import PUBLIC_ACCESS_SETTING_FILE
|
||||
from plinth.utils import gunzip
|
||||
|
||||
SETTINGS_FILE = '/etc/searx/settings.yml'
|
||||
|
||||
UWSGI_FILE = '/etc/uwsgi/apps-available/searx.ini'
|
||||
|
||||
|
||||
def _copy_uwsgi_configuration():
|
||||
"""Copy example uwsgi configuration
|
||||
|
||||
Copy the example uwsgi configuration shipped with Searx documentation to
|
||||
the appropriate uwsgi directory.
|
||||
"""
|
||||
example_config = ('/usr/share/doc/searx/examples/'
|
||||
'uwsgi/apps-available/searx.ini')
|
||||
if not os.path.exists(UWSGI_FILE):
|
||||
shutil.copy(example_config, os.path.dirname(UWSGI_FILE))
|
||||
|
||||
|
||||
def _update_uwsgi_configuration():
|
||||
"""Fix uwsgi configuration.
|
||||
|
||||
uwsgi 2.0.15-debian crashes when trying to autoload.
|
||||
"""
|
||||
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
||||
augeas.Augeas.NO_MODL_AUTOLOAD)
|
||||
aug.set('/augeas/load/inifile/lens', 'Puppet.lns')
|
||||
aug.set('/augeas/load/inifile/incl[last() + 1]', UWSGI_FILE)
|
||||
aug.load()
|
||||
aug.set('/files/etc/uwsgi/apps-available/searx.ini/uwsgi/autoload',
|
||||
'false')
|
||||
aug.save()
|
||||
|
||||
|
||||
def _generate_secret_key(settings):
|
||||
"""Generate a secret key for the Searx installation."""
|
||||
secret_key = secrets.token_hex(64)
|
||||
settings['server']['secret_key'] = secret_key
|
||||
|
||||
|
||||
def _set_title(settings):
|
||||
"""Set the page title to '{box_name} Web Search'."""
|
||||
title = 'FreedomBox Web Search'
|
||||
settings['general']['instance_name'] = title
|
||||
|
||||
|
||||
def _set_timeout(settings):
|
||||
"""Set timeout to 20 seconds."""
|
||||
settings['outgoing']['request_timeout'] = 20.0
|
||||
|
||||
|
||||
def _set_safe_search(settings):
|
||||
"""Set safe search to Moderate."""
|
||||
settings['search']['safe_search'] = 1
|
||||
|
||||
|
||||
@privileged
|
||||
def set_safe_search(filter_: int):
|
||||
"""Set safe search filter for search results."""
|
||||
settings = read_settings()
|
||||
settings['search']['safe_search'] = filter_
|
||||
write_settings(settings)
|
||||
|
||||
|
||||
@privileged
|
||||
def get_safe_search() -> int:
|
||||
"""Return the value of the safe search setting."""
|
||||
if os.path.exists(SETTINGS_FILE):
|
||||
settings = read_settings()
|
||||
return int(settings['search']['safe_search'])
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def read_settings():
|
||||
"""Load settings as dictionary from YAML config file."""
|
||||
with open(SETTINGS_FILE, 'rb') as settings_file:
|
||||
return yaml.safe_load(settings_file)
|
||||
|
||||
|
||||
def write_settings(settings):
|
||||
"""Write settings from dictionary to YAML config file."""
|
||||
with open(SETTINGS_FILE, 'w', encoding='utf-8') as settings_file:
|
||||
yaml.dump(settings, settings_file)
|
||||
|
||||
|
||||
def _get_example_settings_file():
|
||||
searx_doc_dir = pathlib.Path('/usr/share/doc/searx/examples/')
|
||||
if (searx_doc_dir / 'settings.yml').exists():
|
||||
return searx_doc_dir / 'settings.yml'
|
||||
|
||||
return searx_doc_dir / 'settings.yml.gz'
|
||||
|
||||
|
||||
def _update_search_engines(settings):
|
||||
"""Update settings with the latest supported search engines."""
|
||||
example_settings_file = _get_example_settings_file()
|
||||
open_func = gzip.open if example_settings_file.suffix == '.gz' else open
|
||||
with open_func(example_settings_file, 'rb') as example_settings:
|
||||
settings['engines'] = yaml.safe_load(example_settings)['engines']
|
||||
|
||||
|
||||
@privileged
|
||||
def setup():
|
||||
"""Post installation actions for Searx."""
|
||||
_copy_uwsgi_configuration()
|
||||
_update_uwsgi_configuration()
|
||||
|
||||
if not os.path.exists(SETTINGS_FILE):
|
||||
example_settings_file = _get_example_settings_file()
|
||||
if example_settings_file.suffix == '.gz':
|
||||
gunzip(str(example_settings_file), SETTINGS_FILE)
|
||||
else:
|
||||
pathlib.Path(SETTINGS_FILE).parent.mkdir(mode=0o755)
|
||||
shutil.copy(example_settings_file, SETTINGS_FILE)
|
||||
|
||||
settings = read_settings()
|
||||
_generate_secret_key(settings)
|
||||
_set_title(settings)
|
||||
_set_timeout(settings)
|
||||
_set_safe_search(settings)
|
||||
_update_search_engines(settings)
|
||||
write_settings(settings)
|
||||
|
||||
action_utils.service_restart('uwsgi')
|
||||
|
||||
|
||||
@privileged
|
||||
def enable_public_access():
|
||||
"""Enable public access to the SearX application."""
|
||||
open(PUBLIC_ACCESS_SETTING_FILE, 'w', encoding='utf-8').close()
|
||||
|
||||
|
||||
@privileged
|
||||
def disable_public_access():
|
||||
"""Disable public access to the SearX application."""
|
||||
if os.path.exists(PUBLIC_ACCESS_SETTING_FILE):
|
||||
os.remove(PUBLIC_ACCESS_SETTING_FILE)
|
||||
@@ -1,29 +1,27 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Django views for Searx.
|
||||
"""
|
||||
"""Django views for Searx."""
|
||||
|
||||
from django.contrib import messages
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import app as app_module
|
||||
from plinth import views
|
||||
from plinth.errors import ActionError
|
||||
from plinth.modules import searx
|
||||
|
||||
from . import privileged
|
||||
from .forms import SearxForm
|
||||
|
||||
|
||||
class SearxAppView(views.AppView):
|
||||
"""Serve configuration page."""
|
||||
|
||||
app_id = 'searx'
|
||||
form_class = SearxForm
|
||||
|
||||
def get_initial(self):
|
||||
"""Return the status of the service to fill in the form."""
|
||||
initial = super().get_initial()
|
||||
initial['safe_search'] = searx.get_safe_search_setting()
|
||||
initial['safe_search'] = privileged.get_safe_search()
|
||||
initial['public_access'] = searx.is_public_access_enabled() and \
|
||||
app_module.App.get('searx').is_enabled()
|
||||
return initial
|
||||
@@ -35,10 +33,9 @@ class SearxAppView(views.AppView):
|
||||
|
||||
if str(old_data['safe_search']) != form_data['safe_search']:
|
||||
try:
|
||||
actions.superuser_run(
|
||||
'searx', ['set-safe-search', form_data['safe_search']])
|
||||
privileged.set_safe_search(int(form_data['safe_search']))
|
||||
messages.success(self.request, _('Configuration updated.'))
|
||||
except ActionError:
|
||||
except Exception:
|
||||
messages.error(self.request,
|
||||
_('An error occurred during configuration.'))
|
||||
|
||||
@@ -49,7 +46,7 @@ class SearxAppView(views.AppView):
|
||||
else:
|
||||
searx.disable_public_access()
|
||||
messages.success(self.request, _('Configuration updated.'))
|
||||
except ActionError:
|
||||
except Exception:
|
||||
messages.error(self.request,
|
||||
_('An error occurred during configuration.'))
|
||||
|
||||
|
||||
Reference in new issue
Block a user