mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
*: Add type hints for diagnose method
Helps: #2410. - Ensure that diagnostics methods and parameters are type checked so that we can catch any potential issues. - Move plinth/modules/diagnostics/check.py to plinth/diagnostic_check.py to avoid many circular dependencies created. This is due to plinth.modules.diagnostics automatically imported when plinth.modules.diagnostics.check is imported. Also app.py is already (type) dependent on diagnostic_check due to diagnose() method. To make the Check classes independent of diagnostic module is okay. Tests: - Run make check-type. - Run full diagnostics with following apps installed: torproxy, tor. - Test to netcat to 9051 in tor works. - Test 'port available for internal/external networks' in firewall works. - Test 'Package is latest' works. - Test 'Access url with proxy' in privoxy works. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org> [jvalleroy: Also move tests for diagnostic_check] Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
f9b186e14f
commit
4b09d91f93
28 files changed
+161
-117
No files matched your search
@@ -10,8 +10,8 @@ from django.utils.translation import gettext_noop
|
||||
from plinth import app as app_module
|
||||
from plinth import cfg, menu
|
||||
from plinth.daemon import Daemon
|
||||
from plinth.diagnostic_check import DiagnosticCheck, Result
|
||||
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
|
||||
|
||||
@@ -96,7 +96,7 @@ class FirewallApp(app_module.App):
|
||||
_run_setup()
|
||||
return True
|
||||
|
||||
def diagnose(self):
|
||||
def diagnose(self) -> list[DiagnosticCheck]:
|
||||
"""Run diagnostics and return the results."""
|
||||
results = super().diagnose()
|
||||
config = privileged.get_config()
|
||||
@@ -265,7 +265,8 @@ def remove_passthrough(ipv, *args):
|
||||
config_direct.removePassthrough('(sas)', ipv, args)
|
||||
|
||||
|
||||
def _diagnose_default_zone(config):
|
||||
def _diagnose_default_zone(
|
||||
config: privileged.FirewallConfig) -> DiagnosticCheck:
|
||||
"""Diagnose whether the default zone is external."""
|
||||
check_id = 'firewall-default-zone'
|
||||
description = gettext_noop('Default zone is external')
|
||||
@@ -274,7 +275,8 @@ def _diagnose_default_zone(config):
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
|
||||
|
||||
def _diagnose_firewall_backend(config):
|
||||
def _diagnose_firewall_backend(
|
||||
config: privileged.FirewallConfig) -> DiagnosticCheck:
|
||||
"""Diagnose whether the firewall backend is nftables."""
|
||||
check_id = 'firewall-backend'
|
||||
description = gettext_noop('Firewall backend is nftables')
|
||||
@@ -283,7 +285,8 @@ def _diagnose_firewall_backend(config):
|
||||
return DiagnosticCheck(check_id, description, result)
|
||||
|
||||
|
||||
def _diagnose_direct_passthroughs(config):
|
||||
def _diagnose_direct_passthroughs(
|
||||
config: privileged.FirewallConfig) -> DiagnosticCheck:
|
||||
"""Diagnose direct passthroughs for local service protection.
|
||||
|
||||
Currently, we just check that the number of passthroughs is at least 12,
|
||||
|
||||
@@ -5,16 +5,19 @@ App component for other apps to use firewall functionality.
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import ClassVar
|
||||
from typing import ClassVar, TypeAlias
|
||||
|
||||
from django.utils.translation import gettext_noop
|
||||
|
||||
from plinth import app
|
||||
from plinth.diagnostic_check import (DiagnosticCheck,
|
||||
DiagnosticCheckParameters, Result)
|
||||
from plinth.modules import firewall
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_list_type: TypeAlias = list
|
||||
|
||||
|
||||
class Firewall(app.FollowerComponent):
|
||||
"""Component to open/close firewall ports for an app."""
|
||||
@@ -114,7 +117,7 @@ class Firewall(app.FollowerComponent):
|
||||
if not re.fullmatch(r'tun\d+', interface)
|
||||
]
|
||||
|
||||
def diagnose(self):
|
||||
def diagnose(self) -> _list_type[DiagnosticCheck]:
|
||||
"""Check if the firewall ports are open and only as expected.
|
||||
|
||||
See :py:meth:`plinth.app.Component.diagnose`.
|
||||
@@ -124,7 +127,7 @@ class Firewall(app.FollowerComponent):
|
||||
internal_ports = firewall.get_enabled_services(zone='internal')
|
||||
external_ports = firewall.get_enabled_services(zone='external')
|
||||
for port_detail in self.ports_details:
|
||||
port = port_detail['name']
|
||||
port = str(port_detail['name'])
|
||||
details = ', '.join(
|
||||
(f'{port_number}/{protocol}'
|
||||
for port_number, protocol in port_detail['details']))
|
||||
@@ -134,7 +137,10 @@ class Firewall(app.FollowerComponent):
|
||||
result = Result.PASSED if port in internal_ports else Result.FAILED
|
||||
description = gettext_noop(
|
||||
'Port {name} ({details}) available for internal networks')
|
||||
parameters = {'name': port, 'details': details}
|
||||
parameters: DiagnosticCheckParameters = {
|
||||
'name': port,
|
||||
'details': details
|
||||
}
|
||||
results.append(
|
||||
DiagnosticCheck(check_id, description, result, parameters))
|
||||
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
"""Configuration helper for FreedomBox firewall interface."""
|
||||
|
||||
import subprocess
|
||||
from typing import TypeAlias
|
||||
|
||||
import augeas
|
||||
|
||||
from plinth import action_utils
|
||||
from plinth.actions import privileged
|
||||
|
||||
FirewallConfig: TypeAlias = dict[str, str | list[str]]
|
||||
|
||||
|
||||
def _flush_iptables_rules():
|
||||
"""Flush firewalld iptables rules before restarting it.
|
||||
@@ -132,9 +135,9 @@ def setup():
|
||||
|
||||
|
||||
@privileged
|
||||
def get_config():
|
||||
def get_config() -> FirewallConfig:
|
||||
"""Return firewalld configuration for diagnostics."""
|
||||
config = {}
|
||||
config: FirewallConfig = {}
|
||||
|
||||
# Get the default zone.
|
||||
output = subprocess.check_output(['firewall-cmd', '--get-default-zone'])
|
||||
|
||||
@@ -8,7 +8,7 @@ from unittest.mock import call, patch
|
||||
import pytest
|
||||
|
||||
from plinth.app import App
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
from plinth.diagnostic_check import DiagnosticCheck, Result
|
||||
from plinth.modules.firewall.components import (Firewall,
|
||||
FirewallLocalProtection)
|
||||
|
||||
|
||||
Reference in new issue
Block a user