Skip to content

Commit 0d95296

Browse files
committed
Re-arrange get_keyring() for clarity
1 parent 6d6e0ec commit 0d95296

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

src/vorta/borg/borg_thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def prepare(cls, profile):
144144
return ret
145145

146146
# Try to fall back to DB Keyring, if we use the system keychain.
147-
if ret['password'] is None and cls.keyring.is_primary:
147+
if ret['password'] is None and cls.keyring.is_system:
148148
logger.debug('Password not found in primary keyring. Falling back to VortaDBKeyring.')
149149
ret['password'] = VortaDBKeyring().get_password('vorta-repo', profile.repo.url)
150150

src/vorta/keyring/abc.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,46 @@ class VortaKeyring:
1313
@classmethod
1414
def get_keyring(cls):
1515
"""
16-
Attempts to get secure keyring at runtime if current keyring is insecure.
17-
Once it finds a secure keyring, it wil always use that keyring
16+
Choose available Keyring or save passwords to settings database.
17+
18+
Will always use Keychain on macOS. Will try KWallet and then Secret
19+
Storage on Linux and *BSD. If non are available or usage of system keychain
20+
is disabled, fall back to saving passwords to DB.
1821
"""
1922

20-
# Using system keychain is disabled in settings
2123
from vorta.models import SettingsModel
22-
if not SettingsModel.get(key='use_system_keyring').value:
23-
from .db import VortaDBKeyring
24-
cls._keyring = VortaDBKeyring()
25-
elif cls._keyring is None or not cls._keyring.is_primary:
26-
if sys.platform == 'darwin': # Use Keychain on macOS
27-
from .darwin import VortaDarwinKeyring
28-
cls._keyring = VortaDarwinKeyring()
29-
else:
30-
# Try to use KWallet (KDE)
31-
from .kwallet import VortaKWallet5Keyring, KWalletNotAvailableException
32-
try:
33-
cls._keyring = VortaKWallet5Keyring()
34-
except KWalletNotAvailableException:
35-
# Try to use DBus and Gnome-Keyring (available on Linux and *BSD)
36-
# Put this last as gnome keyring is included by default on many distros
37-
import secretstorage
38-
from .secretstorage import VortaSecretStorageKeyring
39-
40-
# secretstorage has two different libraries based on version
41-
if parse_version(secretstorage.__version__) >= parse_version("3.0.0"):
42-
from jeepney.wrappers import DBusErrorResponse as DBusException
43-
else:
44-
from dbus.exceptions import DBusException
24+
from .db import VortaDBKeyring
4525

26+
if SettingsModel.get(key='use_system_keyring').value:
27+
if cls._keyring is None or not cls._keyring.is_system:
28+
# macOS: Only Keychain available
29+
if sys.platform == 'darwin':
30+
from .darwin import VortaDarwinKeyring
31+
cls._keyring = VortaDarwinKeyring()
32+
else:
33+
# Others: Try KWallet first
34+
from .kwallet import VortaKWallet5Keyring, KWalletNotAvailableException
4635
try:
47-
cls._keyring = VortaSecretStorageKeyring()
48-
except (secretstorage.exceptions.SecretStorageException, DBusException):
49-
# Save passwords in DB, if all else fails.
50-
from .db import VortaDBKeyring
51-
cls._keyring = VortaDBKeyring()
36+
cls._keyring = VortaKWallet5Keyring()
37+
except KWalletNotAvailableException:
38+
# Try to use DBus and Gnome Keyring (available on Linux and *BSD)
39+
# Put this last as Gnome Keyring is included by default on many distros
40+
import secretstorage
41+
from .secretstorage import VortaSecretStorageKeyring
42+
43+
# Secret Storage has two different libraries based on version
44+
if parse_version(secretstorage.__version__) >= parse_version("3.0.0"):
45+
from jeepney.wrappers import DBusErrorResponse as DBusException
46+
else:
47+
from dbus.exceptions import DBusException
48+
try:
49+
cls._keyring = VortaSecretStorageKeyring()
50+
except (secretstorage.exceptions.SecretStorageException, DBusException):
51+
# Fall back to using DB, if all else fails.
52+
cls._keyring = VortaDBKeyring()
53+
else:
54+
cls._keyring = VortaDBKeyring()
55+
5256
return cls._keyring
5357

5458
def set_password(self, service, repo_url, password):
@@ -64,7 +68,7 @@ def get_password(self, service, repo_url):
6468
raise NotImplementedError
6569

6670
@property
67-
def is_primary(self):
71+
def is_system(self):
6872
"""
6973
Return True if the current subclass is the system's primary keychain mechanism,
7074
rather than a fallback (like our own VortaDBKeyring).

src/vorta/keyring/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_password(self, service, repo_url):
2727
return None
2828

2929
@property
30-
def is_primary(self):
30+
def is_system(self):
3131
return False
3232

3333
@property

src/vorta/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def display_password_backend(encryption):
344344
# flake8: noqa E501
345345
if encryption != 'none':
346346
keyring = VortaKeyring.get_keyring()
347-
return trans_late('utils', "Storing the password in your password manager.") if keyring.is_primary else trans_late(
347+
return trans_late('utils', "Storing the password in your password manager.") if keyring.is_system else trans_late(
348348
'utils', 'Saving the password to disk. To store password more securely install a supported secret store such as KeepassXC')
349349
else:
350350
return ""

0 commit comments

Comments
 (0)