Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests leave key in keyring #1639

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
abb3ec8
Adding remove password
ganeshrevadi Mar 9, 2023
527797c
remove pass method
ganeshrevadi Mar 10, 2023
6afdbbc
Kwallet
ganeshrevadi Mar 11, 2023
cfd0ebf
Implement remove password for kwallet
ganeshrevadi Mar 12, 2023
1e17d31
Merge branch 'master' into Test-leave-key-in-keyring
ganeshrevadi Mar 12, 2023
31f1048
Implement remove_password() for darwin
m3nu Mar 12, 2023
f5a5a0b
Merge branch 'Test-leave-key-in-keyring' of https://github.com/ganesh…
ganeshrevadi Mar 13, 2023
3d27600
Remove password for db
ganeshrevadi Mar 13, 2023
923b410
Remove password for db
ganeshrevadi Mar 15, 2023
afb8b09
remove password saved in test_utils
ganeshrevadi Mar 17, 2023
f8ba8b7
remove password from keyring
ganeshrevadi Mar 23, 2023
da20aac
remove_password
ganeshrevadi Mar 23, 2023
a5767fa
fixes
ganeshrevadi Mar 23, 2023
9bfbc3c
fixes2
ganeshrevadi Mar 23, 2023
870b674
pytest-fixtures
ganeshrevadi Mar 24, 2023
55e6de6
pytest-fixtures2
ganeshrevadi Mar 24, 2023
9a33bd4
Adding remove password
ganeshrevadi Mar 9, 2023
4657aec
remove pass method
ganeshrevadi Mar 10, 2023
658d791
Kwallet
ganeshrevadi Mar 11, 2023
c7137b0
Implement remove password for kwallet
ganeshrevadi Mar 12, 2023
e5791da
Implement remove_password() for darwin
m3nu Mar 12, 2023
2304a76
Remove password for db
ganeshrevadi Mar 13, 2023
e4a62d5
Remove password for db
ganeshrevadi Mar 15, 2023
650a209
remove password saved in test_utils
ganeshrevadi Mar 17, 2023
354f646
remove password from keyring
ganeshrevadi Mar 23, 2023
37c76e4
remove_password
ganeshrevadi Mar 23, 2023
020973e
fixes
ganeshrevadi Mar 23, 2023
a4c11b8
fixes2
ganeshrevadi Mar 23, 2023
9177435
pytest-fixtures
ganeshrevadi Mar 24, 2023
ac42825
pytest-fixtures2
ganeshrevadi Mar 24, 2023
9abe861
Merge branch 'Test-leave-key-in-keyring' of https://github.com/ganesh…
ganeshrevadi Mar 25, 2023
f1cc8f4
fixtures
ganeshrevadi Mar 25, 2023
c910d61
fixtures-2
ganeshrevadi Mar 25, 2023
eb2e878
fixtures-3
ganeshrevadi Mar 25, 2023
30201b1
Merge branch 'master' into Test-leave-key-in-keyring
ganeshrevadi Apr 2, 2023
099e9e4
fixing
ganeshrevadi Apr 2, 2023
1c21dd9
fix2
ganeshrevadi Apr 2, 2023
b6aad7a
fix3
ganeshrevadi Apr 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/vorta/keyring/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def get_password(self, service, repo_url):
"""
raise NotImplementedError

def remove_password(self, service, repo_url):
"""
Removes a password form the underlying store.
"""
raise NotImplementedError

@property
def is_system(self):
"""
Expand Down
26 changes: 25 additions & 1 deletion src/vorta/keyring/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@


class VortaDarwinKeyring(VortaKeyring):
"""Homemade macOS Keychain Service"""
"""
Homemade macOS Keychain Service

TODO: Could use the newer API, as done here: https://github.com/jaraco/keyring/pull/522/files
"""

login_keychain = None

Expand All @@ -42,6 +46,7 @@ def _set_keychain(self):
b'i@I*I*o^Io^^{OpaquePassBuff}o^^{OpaqueSecKeychainItemRef}',
),
('SecKeychainGetStatus', b'i^{OpaqueSecKeychainRef=}o^I'),
('SecKeychainItemDelete', b'i^{OpaqueSecKeychainItemRef=}o^I'),
]

objc.loadBundleFunctions(Security, globals(), S_functions)
Expand Down Expand Up @@ -97,6 +102,25 @@ def get_password(self, service, repo_url):
logger.debug(f"Retrieved password for repo {repo_url}")
return password

def remove_password(self, service, repo_url):
if not self.login_keychain:
self._set_keychain()

(result, password_length, password_buffer, keychain_item,) = SecKeychainFindGenericPassword(
self.login_keychain,
len(service),
service.encode(),
len(repo_url),
repo_url.encode(),
None,
None,
None,
)
password = None
if (result == 0) and (password_length != 0):
logger.debug(f"Found password for repo {repo_url}")
SecKeychainItemDelete(keychain_item, None)

@property
def is_unlocked(self):
kSecUnlockStateStatus = 1
Expand Down
14 changes: 10 additions & 4 deletions src/vorta/keyring/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import peewee
from vorta.store.models import SettingsModel
from vorta.store.models import RepoPassword, SettingsModel
from .abc import VortaKeyring

logger = logging.getLogger(__name__)
Expand All @@ -14,16 +14,13 @@ class VortaDBKeyring(VortaKeyring):
"""

def set_password(self, service, repo_url, password):
from vorta.store.models import RepoPassword

keyring_entry, created = RepoPassword.get_or_create(url=repo_url, defaults={'password': password})
keyring_entry.password = password
keyring_entry.save()

logger.debug(f"Saved password for repo {repo_url}")

def get_password(self, service, repo_url):
from vorta.store.models import RepoPassword

try:
keyring_entry = RepoPassword.get(url=repo_url)
Expand All @@ -33,6 +30,15 @@ def get_password(self, service, repo_url):
except peewee.DoesNotExist:
return None

def remove_password(self, service, repo_url):

try:
keyring_entry = RepoPassword.get(url=repo_url)
keyring_entry.delete_instance()
logger.debug(f"Removed password for repo {repo_url}")
except peewee.DoesNotExist:
pass
ganeshrevadi marked this conversation as resolved.
Show resolved Hide resolved

@property
def is_system(self):
return False
Expand Down
6 changes: 6 additions & 0 deletions src/vorta/keyring/kwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def get_password(self, service, repo_url):
logger.debug(f"Retrieved password for repo {repo_url}")
return password

def remove_password(self, service, repo_url):
entry = [self.handle, self.folder_name, repo_url, service]
if self.is_unlocked and self.get_result("hasEntry", args=entry):
self.get_result("removeEntry", args=entry)
logger.debug(f"Removed password for repo {repo_url}")

def get_result(self, method, args=[]):
if args:
result = self.iface.callWithArgumentList(QtDBus.QDBus.AutoDetect, method, args)
Expand Down
20 changes: 20 additions & 0 deletions src/vorta/keyring/secretstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ def get_password(self, service, repo_url):
return item.get_secret().decode("utf-8")
return None

def remove_password(self, service, repo_url):
"""
Remove a password from the underlying store.
"""
if self.is_unlocked:
asyncio.set_event_loop(asyncio.new_event_loop())
attributes = {
'application': 'Vorta',
'service': service,
'repo_url': repo_url,
}
items = list(self.collection.search_items(attributes))
logger.debug('Found %i passwords matching repo URL.', len(items))
for item in items:
if item.is_locked() and item.unlock():
return None
ganeshrevadi marked this conversation as resolved.
Show resolved Hide resolved
self.collection.delete(item)
ganeshrevadi marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(f"Removed password for repo {repo_url}")
return None
ganeshrevadi marked this conversation as resolved.
Show resolved Hide resolved

@property
def is_unlocked(self):
# unlock() will return True if the unlock prompt is dismissed
Expand Down