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
@@ -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():
|
||||
|
||||
Reference in new issue
Block a user