1
0
Fork 0
mirror of https://github.com/borgbase/vorta synced 2025-01-03 13:45:49 +00:00

Handle DBusException in NetworkManagerMonitor (#636)

* Catch DBus permission errors
* Better DBusException handling in NetworkManagerMonitor
This commit is contained in:
Tomasz Kontusz 2020-09-09 02:32:26 +02:00 committed by GitHub
parent 23374876d6
commit 262c91a180
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 20 deletions

View file

@ -16,32 +16,50 @@ def __init__(self, nm_adapter: 'NetworkManagerDBusAdapter' = None):
self._nm = nm_adapter or NetworkManagerDBusAdapter.get_system_nm_adapter() self._nm = nm_adapter or NetworkManagerDBusAdapter.get_system_nm_adapter()
def is_network_metered(self) -> bool: def is_network_metered(self) -> bool:
return self._nm.get_global_metered_status() in (NMMetered.YES, NMMetered.GUESS_YES) try:
return self._nm.get_global_metered_status() in (NMMetered.YES, NMMetered.GUESS_YES)
except DBusException:
logger.exception("Failed to check if network is metered, assuming it isn't")
return False
def get_current_wifi(self) -> Optional[str]: def get_current_wifi(self) -> Optional[str]:
# Only check the primary connection. VPN over WiFi will still show the WiFi as Primary Connection. # Only check the primary connection. VPN over WiFi will still show the WiFi as Primary Connection.
# We don't check all active connections, as NM won't disable WiFi when connecting a cable. # We don't check all active connections, as NM won't disable WiFi when connecting a cable.
active_connection_path = self._nm.get_primary_connection_path() try:
if not active_connection_path: active_connection_path = self._nm.get_primary_connection_path()
return if not active_connection_path:
active_connection = self._nm.get_active_connection_info(active_connection_path) return
if active_connection.type == '802-11-wireless': active_connection = self._nm.get_active_connection_info(active_connection_path)
settings = self._nm.get_settings(active_connection.connection) if active_connection.type == '802-11-wireless':
ssid = self._get_ssid_from_settings(settings) settings = self._nm.get_settings(active_connection.connection)
if ssid: ssid = self._get_ssid_from_settings(settings)
return ssid if ssid:
return ssid
except DBusException:
logger.exception("Failed to get currently connected WiFi network, assuming none")
return None
def get_known_wifis(self) -> Optional[List[SystemWifiInfo]]: def get_known_wifis(self) -> Optional[List[SystemWifiInfo]]:
try:
connections_paths = self._nm.get_connections_paths()
except DBusException:
logger.exception("Failed to list connections")
return None
wifis = [] wifis = []
for connection_path in self._nm.get_connections_paths(): for connection_path in connections_paths:
settings = self._nm.get_settings(connection_path) try:
ssid = self._get_ssid_from_settings(settings) settings = self._nm.get_settings(connection_path)
if ssid: except DBusException:
timestamp = settings['connection'].get('timestamp') logger.warning("Couldn't load settings for %s", connection_path, exc_info=True)
wifis.append(SystemWifiInfo( else:
ssid=ssid, ssid = self._get_ssid_from_settings(settings)
last_connected=timestamp and datetime.utcfromtimestamp(timestamp), if ssid:
)) timestamp = settings['connection'].get('timestamp')
wifis.append(SystemWifiInfo(
ssid=ssid,
last_connected=timestamp and datetime.utcfromtimestamp(timestamp),
))
return wifis return wifis
def _get_ssid_from_settings(self, settings): def _get_ssid_from_settings(self, settings):

View file

@ -5,7 +5,7 @@
from vorta.network_status.abc import SystemWifiInfo from vorta.network_status.abc import SystemWifiInfo
from vorta.network_status.network_manager import NetworkManagerMonitor, NMMetered, NetworkManagerDBusAdapter, \ from vorta.network_status.network_manager import NetworkManagerMonitor, NMMetered, NetworkManagerDBusAdapter, \
ActiveConnectionInfo, decode_ssid ActiveConnectionInfo, decode_ssid, DBusException
@pytest.fixture @pytest.fixture
@ -95,6 +95,27 @@ def test_get_known_wifis_with_never_used_connection(nm_monitor):
)] )]
def test_get_known_wifis_partial_failure(nm_monitor):
nm_monitor._nm.get_connections_paths.return_value = [
'/org/freedesktop/NetworkManager/Settings/12',
'/org/freedesktop/NetworkManager/Settings/42',
]
nm_monitor._nm.get_settings.side_effect = [
DBusException("Test"),
{
'connection': {},
'802-11-wireless': {'ssid': [84, 69, 83, 84]},
},
]
result = nm_monitor.get_known_wifis()
assert result == [SystemWifiInfo(
ssid='TEST',
last_connected=None,
)]
def test_get_known_wifis_with_no_wifi_connections(nm_monitor): def test_get_known_wifis_with_no_wifi_connections(nm_monitor):
nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12'] nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
nm_monitor._nm.get_settings.return_value = { nm_monitor._nm.get_settings.return_value = {