1
0
Fork 0
mirror of https://github.com/borgbase/vorta synced 2024-12-22 07:43:09 +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()
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]:
# 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.
active_connection_path = self._nm.get_primary_connection_path()
if not active_connection_path:
return
active_connection = self._nm.get_active_connection_info(active_connection_path)
if active_connection.type == '802-11-wireless':
settings = self._nm.get_settings(active_connection.connection)
ssid = self._get_ssid_from_settings(settings)
if ssid:
return ssid
try:
active_connection_path = self._nm.get_primary_connection_path()
if not active_connection_path:
return
active_connection = self._nm.get_active_connection_info(active_connection_path)
if active_connection.type == '802-11-wireless':
settings = self._nm.get_settings(active_connection.connection)
ssid = self._get_ssid_from_settings(settings)
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]]:
try:
connections_paths = self._nm.get_connections_paths()
except DBusException:
logger.exception("Failed to list connections")
return None
wifis = []
for connection_path in self._nm.get_connections_paths():
settings = self._nm.get_settings(connection_path)
ssid = self._get_ssid_from_settings(settings)
if ssid:
timestamp = settings['connection'].get('timestamp')
wifis.append(SystemWifiInfo(
ssid=ssid,
last_connected=timestamp and datetime.utcfromtimestamp(timestamp),
))
for connection_path in connections_paths:
try:
settings = self._nm.get_settings(connection_path)
except DBusException:
logger.warning("Couldn't load settings for %s", connection_path, exc_info=True)
else:
ssid = self._get_ssid_from_settings(settings)
if ssid:
timestamp = settings['connection'].get('timestamp')
wifis.append(SystemWifiInfo(
ssid=ssid,
last_connected=timestamp and datetime.utcfromtimestamp(timestamp),
))
return wifis
def _get_ssid_from_settings(self, settings):

View file

@ -5,7 +5,7 @@
from vorta.network_status.abc import SystemWifiInfo
from vorta.network_status.network_manager import NetworkManagerMonitor, NMMetered, NetworkManagerDBusAdapter, \
ActiveConnectionInfo, decode_ssid
ActiveConnectionInfo, decode_ssid, DBusException
@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):
nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
nm_monitor._nm.get_settings.return_value = {