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

Fix set_keychain errors #964

Merged
merged 8 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions features/encryption.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,27 @@
Then the config for journal "simple" should have "encrypt" set to "bool:True"
When we run "jrnl simple -n 1"
Then the output should contain "2013-06-10 15:40 Life is good"

Scenario: Encrypt journal with no keyring backend and do not store in keyring
Given we use the config "basic.yaml"
When we disable the keychain
and we run "jrnl test entry"
and we run "jrnl --encrypt" and enter
"""
password
password
n
"""
Then we should get no error

Scenario: Encrypt journal with no keyring backend and do store in keyring
Given we use the config "basic.yaml"
When we disable the keychain
and we run "jrnl test entry"
and we run "jrnl --encrypt" and enter
"""
password
password
y
"""
Then we should get no error
21 changes: 21 additions & 0 deletions features/steps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def delete_password(self, servicename, username):
self.keys[servicename][username] = None


class NoKeyring(keyring.backend.KeyringBackend):
"""A keyring that simulated an environment with no keyring backend."""

priority = 2
keys = defaultdict(dict)

def set_password(self, servicename, username, password):
raise keyring.errors.NoKeyringError

def get_password(self, servicename, username):
raise keyring.errors.NoKeyringError

def delete_password(self, servicename, username):
raise keyring.errors.NoKeyringError


# set the keyring for keyring lib
keyring.set_keyring(TestKeyring())

Expand Down Expand Up @@ -201,6 +217,11 @@ def set_keychain(context, journal, password):
keyring.set_password("jrnl", journal, password)


@when("we disable the keychain")
def disable_keychain(context):
keyring.core.set_keyring(NoKeyring())


@then("we should get an error")
def has_error(context):
assert context.exit_status != 0, context.exit_status
Expand Down
11 changes: 7 additions & 4 deletions jrnl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ def create_password(

if yesno("Do you want to store the password in your keychain?", default=True):
set_keychain(journal_name, pw)
else:
set_keychain(journal_name, None)

return pw


Expand Down Expand Up @@ -108,7 +105,13 @@ def set_keychain(journal_name, password):
except keyring.errors.PasswordDeleteError:
pass
else:
keyring.set_password("jrnl", journal_name, password)
try:
keyring.set_password("jrnl", journal_name, password)
except keyring.errors.NoKeyringError:
print(
"Keyring backend not found. Please install one of the supported backends by visiting: https://pypi.org/project/keyring/",
file=sys.stderr,
)


def yesno(prompt, default=True):
Expand Down