mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
diagnostics: Add DiagnosticCheck dataclass
- Set unique check_id for each diagnostic check. - Result is a string-based enumeration. The default value (NOT_DONE) can be used for diagnostic checks that have not been completed yet. - Result is StrEnum so that the return value of check_url can still be used directly as a diagnostic result. Closes: #2375 Signed-off-by: James Valleroy <jvalleroy@mailbox.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
19 files changed
+284
-183
No files matched your search
@@ -8,6 +8,7 @@ from django.utils.text import format_lazy
|
||||
from django.utils.translation import gettext_lazy
|
||||
|
||||
from plinth import action_utils, app
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck
|
||||
from plinth.privileged import service as service_privileged
|
||||
|
||||
from . import privileged
|
||||
@@ -145,13 +146,15 @@ def diagnose_url(url, kind=None, env=None, check_certificate=True,
|
||||
wrapper, expected_output)
|
||||
|
||||
if kind:
|
||||
check_id = f'apache-url-{url}-{kind}'
|
||||
template = gettext_lazy('Access URL {url} on tcp{kind}')
|
||||
testname = format_lazy(template, url=url, kind=kind)
|
||||
description = format_lazy(template, url=url, kind=kind)
|
||||
else:
|
||||
check_id = f'apache-url-{url}'
|
||||
template = gettext_lazy('Access URL {url}')
|
||||
testname = format_lazy(template, url=url)
|
||||
description = format_lazy(template, url=url)
|
||||
|
||||
return [testname, result]
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
|
||||
|
||||
def diagnose_url_on_all(url, expect_redirects=False, **kwargs):
|
||||
|
||||
@@ -243,11 +243,13 @@ def test_diagnose_url(get_addresses, check):
|
||||
}
|
||||
check.return_value = 'passed'
|
||||
result = diagnose_url(**args)
|
||||
assert result == ['Access URL https://localhost/test on tcp4', 'passed']
|
||||
assert result.description == 'Access URL https://localhost/test on tcp4'
|
||||
assert result.result == 'passed'
|
||||
|
||||
check.return_value = 'failed'
|
||||
result = diagnose_url(**args)
|
||||
assert result == ['Access URL https://localhost/test on tcp4', 'failed']
|
||||
assert result.description == 'Access URL https://localhost/test on tcp4'
|
||||
assert result.result == 'failed'
|
||||
|
||||
del args['kind']
|
||||
args['url'] = 'https://{host}/test'
|
||||
@@ -264,10 +266,12 @@ def test_diagnose_url(get_addresses, check):
|
||||
'url_address': 'test-host-2'
|
||||
}]
|
||||
result = diagnose_url_on_all(**args)
|
||||
assert result == [
|
||||
['Access URL https://test-host-1/test on tcp4', 'passed'],
|
||||
['Access URL https://test-host-2/test on tcp6', 'passed'],
|
||||
]
|
||||
assert result[
|
||||
0].description == 'Access URL https://test-host-1/test on tcp4'
|
||||
assert result[0].result == 'passed'
|
||||
assert result[
|
||||
1].description == 'Access URL https://test-host-2/test on tcp6'
|
||||
assert result[1].result == 'passed'
|
||||
|
||||
|
||||
@patch('subprocess.run')
|
||||
|
||||
@@ -19,6 +19,7 @@ from plinth.modules.apache.components import diagnose_url_on_all
|
||||
from plinth.modules.backups.components import BackupRestore
|
||||
|
||||
from . import manifest
|
||||
from .check import Result
|
||||
|
||||
_description = [
|
||||
_('The system diagnostic test will run a number of checks on your '
|
||||
@@ -299,13 +300,14 @@ def _run_background_diagnostics():
|
||||
for _app_id, app_data in results.items():
|
||||
if app_data['exception']:
|
||||
exception_count += 1
|
||||
continue
|
||||
|
||||
for _test, result in app_data['diagnosis']:
|
||||
if result == 'error':
|
||||
for check in app_data['diagnosis']:
|
||||
if check.result == Result.ERROR:
|
||||
error_count += 1
|
||||
elif result == 'failed':
|
||||
elif check.result == Result.FAILED:
|
||||
failure_count += 1
|
||||
elif cfg.develop and result == 'warning':
|
||||
elif check.result == Result.WARNING:
|
||||
warning_count += 1
|
||||
|
||||
notification_id = 'diagnostics-background'
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Diagnostic check data type
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
|
||||
|
||||
class Result(StrEnum):
|
||||
"""The result of a diagnostic check."""
|
||||
NOT_DONE = 'not_done'
|
||||
PASSED = 'passed'
|
||||
WARNING = 'warning'
|
||||
FAILED = 'failed'
|
||||
ERROR = 'error'
|
||||
|
||||
|
||||
# TODO: Add parameters for test.
|
||||
# TODO: Description should not be translated until we need to display it.
|
||||
|
||||
|
||||
@dataclass
|
||||
class DiagnosticCheck:
|
||||
"""A diagnostic check and optional result."""
|
||||
check_id: str
|
||||
description: str
|
||||
result: Result = Result.NOT_DONE
|
||||
@@ -13,18 +13,18 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for test, result in results %}
|
||||
{% for result in results %}
|
||||
<tr>
|
||||
<td>{{ test }}</td>
|
||||
<td>{{ result.description }}</td>
|
||||
<td class="diagnostics-result">
|
||||
{% if result == 'passed' %}
|
||||
<span class="badge badge-success">{% trans result %}</span>
|
||||
{% elif result == 'failed' %}
|
||||
<span class="badge badge-danger">{% trans result %}</span>
|
||||
{% elif result == 'error' or result == 'warning' %}
|
||||
<span class="badge badge-warning">{% trans result %}</span>
|
||||
{% if result.result == 'passed' %}
|
||||
<span class="badge badge-success">{% trans result.result %}</span>
|
||||
{% elif result.result == 'failed' %}
|
||||
<span class="badge badge-danger">{% trans result.result %}</span>
|
||||
{% elif result.result == 'error' or result.result == 'warning' %}
|
||||
<span class="badge badge-warning">{% trans result.result %}</span>
|
||||
{% else %}
|
||||
{{ result }}
|
||||
{{ result.result }}
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -11,6 +11,7 @@ from plinth import app as app_module
|
||||
from plinth import cfg, menu
|
||||
from plinth.daemon import Daemon
|
||||
from plinth.modules.backups.components import BackupRestore
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
from plinth.package import Packages, install
|
||||
from plinth.utils import Version, format_lazy, import_from_gi
|
||||
|
||||
@@ -266,16 +267,20 @@ def remove_passthrough(ipv, *args):
|
||||
|
||||
def _diagnose_default_zone(config):
|
||||
"""Diagnose whether the default zone is external."""
|
||||
testname = gettext('Default zone is external')
|
||||
result = 'passed' if config['default_zone'] == 'external' else 'failed'
|
||||
return [testname, result]
|
||||
check_id = 'firewall-default-zone'
|
||||
description = gettext('Default zone is external')
|
||||
result = Result.PASSED if config[
|
||||
'default_zone'] == 'external' else Result.FAILED
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
|
||||
|
||||
def _diagnose_firewall_backend(config):
|
||||
"""Diagnose whether the firewall backend is nftables."""
|
||||
testname = gettext('Firewall backend is nftables')
|
||||
result = 'passed' if config['backend'] == 'nftables' else 'failed'
|
||||
return [testname, result]
|
||||
check_id = 'firewall-backend'
|
||||
description = gettext('Firewall backend is nftables')
|
||||
result = Result.PASSED if config['backend'] == 'nftables' \
|
||||
else Result.FAILED
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
|
||||
|
||||
def _diagnose_direct_passthroughs(config):
|
||||
@@ -284,6 +289,8 @@ def _diagnose_direct_passthroughs(config):
|
||||
Currently, we just check that the number of passthroughs is at least 12,
|
||||
which are the number that are added by firewall's setup.
|
||||
"""
|
||||
testname = gettext('Direct passthrough rules exist')
|
||||
result = 'passed' if len(config['passthroughs']) >= 12 else 'failed'
|
||||
return [testname, result]
|
||||
check_id = 'firewall-direct-passthroughs'
|
||||
description = gettext('Direct passthrough rules exist')
|
||||
result = Result.PASSED if len(
|
||||
config['passthroughs']) >= 12 else Result.FAILED
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
@@ -12,6 +12,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from plinth import app
|
||||
from plinth.modules import firewall
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -130,25 +131,29 @@ class Firewall(app.FollowerComponent):
|
||||
for port_number, protocol in port_detail['details']))
|
||||
|
||||
# Internal zone
|
||||
result = 'passed' if port in internal_ports else 'failed'
|
||||
check_id = f'firewall-port-{port}-internal'
|
||||
result = Result.PASSED if port in internal_ports else Result.FAILED
|
||||
template = _(
|
||||
'Port {name} ({details}) available for internal networks')
|
||||
testname = format_lazy(template, name=port, details=details)
|
||||
results.append([testname, result])
|
||||
description = format_lazy(template, name=port, details=details)
|
||||
results.append(DiagnosticCheck(check_id, description, result))
|
||||
|
||||
# External zone
|
||||
check_id = f'firewall-port-{port}-external'
|
||||
if self.is_external:
|
||||
result = 'passed' if port in external_ports else 'failed'
|
||||
result = Result.PASSED \
|
||||
if port in external_ports else Result.FAILED
|
||||
template = _(
|
||||
'Port {name} ({details}) available for external networks')
|
||||
testname = format_lazy(template, name=port, details=details)
|
||||
description = format_lazy(template, name=port, details=details)
|
||||
else:
|
||||
result = 'passed' if port not in external_ports else 'failed'
|
||||
result = Result.PASSED \
|
||||
if port not in external_ports else Result.FAILED
|
||||
template = _(
|
||||
'Port {name} ({details}) unavailable for external networks'
|
||||
)
|
||||
testname = format_lazy(template, name=port, details=details)
|
||||
results.append([testname, result])
|
||||
description = format_lazy(template, name=port, details=details)
|
||||
results.append(DiagnosticCheck(check_id, description, result))
|
||||
|
||||
return results
|
||||
|
||||
|
||||
@@ -153,37 +153,34 @@ def test_diagnose(get_enabled_services, get_port_details):
|
||||
firewall = Firewall('test-firewall-1', ports=['test-port1', 'test-port2'],
|
||||
is_external=False)
|
||||
results = firewall.diagnose()
|
||||
assert results == [
|
||||
[
|
||||
'Port test-port1 (1234/tcp, 1234/udp) available for internal '
|
||||
'networks', 'passed'
|
||||
],
|
||||
[
|
||||
'Port test-port1 (1234/tcp, 1234/udp) unavailable for external '
|
||||
'networks', 'passed'
|
||||
],
|
||||
[
|
||||
'Port test-port2 (2345/udp) available for internal networks',
|
||||
'failed'
|
||||
],
|
||||
[
|
||||
'Port test-port2 (2345/udp) unavailable for external networks',
|
||||
'failed'
|
||||
]
|
||||
]
|
||||
assert (results[0].description == 'Port test-port1 (1234/tcp, 1234/udp) '
|
||||
'available for internal networks')
|
||||
assert results[0].result == 'passed'
|
||||
assert (results[1].description == 'Port test-port1 (1234/tcp, 1234/udp) '
|
||||
'unavailable for external networks')
|
||||
assert results[1].result == 'passed'
|
||||
assert (results[2].description == 'Port test-port2 (2345/udp) available '
|
||||
'for internal networks')
|
||||
assert results[2].result == 'failed'
|
||||
assert (results[3].description == 'Port test-port2 (2345/udp) unavailable '
|
||||
'for external networks')
|
||||
assert results[3].result == 'failed'
|
||||
|
||||
firewall = Firewall('test-firewall-1', ports=['test-port3', 'test-port4'],
|
||||
is_external=True)
|
||||
results = firewall.diagnose()
|
||||
assert results == [[
|
||||
'Port test-port3 (3456/tcp) available for internal networks', 'passed'
|
||||
], [
|
||||
'Port test-port3 (3456/tcp) available for external networks', 'passed'
|
||||
], [
|
||||
'Port test-port4 (4567/udp) available for internal networks', 'failed'
|
||||
], [
|
||||
'Port test-port4 (4567/udp) available for external networks', 'failed'
|
||||
]]
|
||||
assert (results[0].description == 'Port test-port3 (3456/tcp) available '
|
||||
'for internal networks')
|
||||
assert results[0].result == 'passed'
|
||||
assert (results[1].description == 'Port test-port3 (3456/tcp) available '
|
||||
'for external networks')
|
||||
assert results[1].result == 'passed'
|
||||
assert (results[2].description == 'Port test-port4 (4567/udp) available '
|
||||
'for internal networks')
|
||||
assert results[2].result == 'failed'
|
||||
assert (results[3].description == 'Port test-port4 (4567/udp) available '
|
||||
'for external networks')
|
||||
assert results[3].result == 'failed'
|
||||
|
||||
|
||||
def test_local_protection_init():
|
||||
|
||||
@@ -111,7 +111,8 @@ def diagnose_url_with_proxy():
|
||||
env = {'https_proxy': proxy}
|
||||
|
||||
result = diagnose_url(url, kind=address['kind'], env=env)
|
||||
result[0] = _('Access {url} with proxy {proxy} on tcp{kind}') \
|
||||
result.description = _(
|
||||
'Access {url} with proxy {proxy} on tcp{kind}') \
|
||||
.format(url=url, proxy=proxy, kind=address['kind'])
|
||||
results.append(result)
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ from plinth.daemon import (Daemon, app_is_running, diagnose_netcat,
|
||||
from plinth.modules import torproxy
|
||||
from plinth.modules.apache.components import Webserver
|
||||
from plinth.modules.backups.components import BackupRestore
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
from plinth.modules.firewall.components import Firewall
|
||||
from plinth.modules.names.components import DomainType
|
||||
from plinth.modules.torproxy.utils import is_apt_transport_tor_enabled
|
||||
@@ -131,10 +132,10 @@ class TorApp(app_module.App):
|
||||
ports = status['ports']
|
||||
|
||||
if status['relay_enabled']:
|
||||
results.append([
|
||||
_('Tor relay port available'),
|
||||
'passed' if 'orport' in ports else 'failed'
|
||||
])
|
||||
results.append(
|
||||
DiagnosticCheck(
|
||||
'tor-port-relay', _('Tor relay port available'),
|
||||
Result.PASSED if 'orport' in ports else Result.FAILED))
|
||||
if 'orport' in ports:
|
||||
results.append(
|
||||
diagnose_port_listening(int(ports['orport']), 'tcp4'))
|
||||
@@ -142,20 +143,20 @@ class TorApp(app_module.App):
|
||||
diagnose_port_listening(int(ports['orport']), 'tcp6'))
|
||||
|
||||
if status['bridge_relay_enabled']:
|
||||
results.append([
|
||||
_('Obfs3 transport registered'),
|
||||
'passed' if 'obfs3' in ports else 'failed'
|
||||
])
|
||||
results.append(
|
||||
DiagnosticCheck(
|
||||
'tor-port-obfs3', _('Obfs3 transport registered'),
|
||||
Result.PASSED if 'obfs3' in ports else Result.FAILED))
|
||||
if 'obfs3' in ports:
|
||||
results.append(
|
||||
diagnose_port_listening(int(ports['obfs3']), 'tcp4'))
|
||||
results.append(
|
||||
diagnose_port_listening(int(ports['obfs3']), 'tcp6'))
|
||||
|
||||
results.append([
|
||||
_('Obfs4 transport registered'),
|
||||
'passed' if 'obfs4' in ports else 'failed'
|
||||
])
|
||||
results.append(
|
||||
DiagnosticCheck(
|
||||
'tor-port-obfs4', _('Obfs4 transport registered'),
|
||||
Result.PASSED if 'obfs4' in ports else Result.FAILED))
|
||||
if 'obfs4' in ports:
|
||||
results.append(
|
||||
diagnose_port_listening(int(ports['obfs4']), 'tcp4'))
|
||||
@@ -164,10 +165,11 @@ class TorApp(app_module.App):
|
||||
|
||||
if status['hs_enabled']:
|
||||
hs_hostname = status['hs_hostname'].split('.onion')[0]
|
||||
results.append([
|
||||
_('Onion service is version 3'),
|
||||
'passed' if len(hs_hostname) == 56 else 'failed'
|
||||
])
|
||||
results.append(
|
||||
DiagnosticCheck(
|
||||
'tor-onion-version', _('Onion service is version 3'),
|
||||
Result.PASSED
|
||||
if len(hs_hostname) == 56 else Result.FAILED))
|
||||
|
||||
return results
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ class TorProxyApp(app_module.App):
|
||||
def _diagnose_url_via_tor(url, kind=None):
|
||||
"""Diagnose whether a URL is reachable via Tor."""
|
||||
result = diagnose_url(url, kind=kind, wrapper='torsocks')
|
||||
result[0] = _('Access URL {url} on tcp{kind} via Tor') \
|
||||
result.description = _('Access URL {url} on tcp{kind} via Tor') \
|
||||
.format(url=url, kind=kind)
|
||||
|
||||
return result
|
||||
@@ -146,7 +146,7 @@ def _diagnose_tor_use(url, kind=None):
|
||||
expected_output = 'Congratulations. This browser is configured to use Tor.'
|
||||
result = diagnose_url(url, kind=kind, wrapper='torsocks',
|
||||
expected_output=expected_output)
|
||||
result[0] = _('Confirm Tor usage at {url} on tcp{kind}') \
|
||||
result.description = _('Confirm Tor usage at {url} on tcp{kind}') \
|
||||
.format(url=url, kind=kind)
|
||||
|
||||
return result
|
||||
@@ -12,6 +12,7 @@ from plinth import app as app_module
|
||||
from plinth import cfg, menu
|
||||
from plinth.config import DropinConfigs
|
||||
from plinth.daemon import Daemon
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
from plinth.package import Packages
|
||||
from plinth.privileged import service as service_privileged
|
||||
|
||||
@@ -112,32 +113,34 @@ class UsersApp(app_module.App):
|
||||
|
||||
def _diagnose_ldap_entry(search_item):
|
||||
"""Diagnose that an LDAP entry exists."""
|
||||
result = 'failed'
|
||||
check_id = f'users-ldap-entry-{search_item}'
|
||||
result = Result.FAILED
|
||||
|
||||
try:
|
||||
subprocess.check_output(
|
||||
['ldapsearch', '-x', '-b', 'dc=thisbox', search_item])
|
||||
result = 'passed'
|
||||
result = Result.PASSED
|
||||
except subprocess.CalledProcessError:
|
||||
pass
|
||||
|
||||
template = _('Check LDAP entry "{search_item}"')
|
||||
testname = format_lazy(template, search_item=search_item)
|
||||
description = format_lazy(template, search_item=search_item)
|
||||
|
||||
return [testname, result]
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
|
||||
|
||||
def _diagnose_nslcd_config(config, key, value):
|
||||
"""Diagnose that nslcd has a configuration."""
|
||||
check_id = f'users-nslcd-config-{key}'
|
||||
try:
|
||||
result = 'passed' if config[key] == value else 'failed'
|
||||
result = Result.PASSED if config[key] == value else Result.FAILED
|
||||
except KeyError:
|
||||
result = 'failed'
|
||||
result = Result.FAILED
|
||||
|
||||
template = _('Check nslcd config "{key} {value}"')
|
||||
testname = format_lazy(template, key=key, value=value)
|
||||
description = format_lazy(template, key=key, value=value)
|
||||
|
||||
return [testname, result]
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
|
||||
|
||||
def _diagnose_nsswitch_config():
|
||||
@@ -151,22 +154,23 @@ def _diagnose_nsswitch_config():
|
||||
|
||||
results = []
|
||||
for database in ['passwd', 'group', 'shadow']:
|
||||
result = 'failed'
|
||||
check_id = f'users-nsswitch-config-{database}'
|
||||
result = Result.FAILED
|
||||
for match in aug.match('database'):
|
||||
if aug.get(match) != database:
|
||||
continue
|
||||
|
||||
for service_match in aug.match(match + '/service'):
|
||||
if 'ldap' == aug.get(service_match):
|
||||
result = 'passed'
|
||||
result = Result.PASSED
|
||||
break
|
||||
|
||||
break
|
||||
|
||||
template = _('Check nsswitch config "{database}"')
|
||||
testname = format_lazy(template, database=database)
|
||||
description = format_lazy(template, database=database)
|
||||
|
||||
results.append([testname, result])
|
||||
results.append(DiagnosticCheck(check_id, description, result))
|
||||
|
||||
return results
|
||||
|
||||
|
||||
Reference in new issue
Block a user