-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from conda-incubator/bugfix-better-keyring-err…
…or-message Adds better error handling when keyring is not supported
- Loading branch information
Showing
14 changed files
with
152 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,3 @@ | |
""" | ||
|
||
PLUGIN_NAME = "conda-auth" | ||
|
||
LOGOUT_ERROR_MESSAGE = "Unable to logout." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# flake8: noqa: F401 | ||
from keyring import get_keyring | ||
from keyring.errors import NoKeyringError | ||
|
||
from .base import Storage | ||
from .keyring import KeyringStorage | ||
from ..exceptions import CondaAuthError | ||
|
||
|
||
def get_storage_backend() -> Storage: | ||
""" | ||
Determine the correct storage backend to use, raise CondaAuthError if none found. | ||
TODO: Add future support for another storage backend when keyring cannot be used. | ||
""" | ||
try: | ||
keyring_tester = get_keyring() | ||
# Retrieve a dummy password to try to trigger NoKeyringError | ||
keyring_tester.get_password("conda_auth", "test") | ||
except NoKeyringError: | ||
raise CondaAuthError( | ||
"Unable to find a credential storage backend, which means this operating system" | ||
" is likely unsupported. One way to overcome this is by installing a third party" | ||
" keyring storage backend. You can find more information about that here: " | ||
"https://pypi.org/project/keyring" | ||
) | ||
|
||
return KeyringStorage() | ||
|
||
|
||
storage = get_storage_backend() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Storage(ABC): | ||
"""ABC class for all credential storage backends""" | ||
|
||
@abstractmethod | ||
def set_password(self, key_id: str, username: str, password: str) -> None: | ||
""" | ||
Sets the password for a specific ``key_id`` | ||
""" | ||
|
||
@abstractmethod | ||
def get_password(self, key_id: str, username: str) -> str | None: | ||
""" | ||
Gets the password for a specific ``key_id`` | ||
""" | ||
|
||
@abstractmethod | ||
def delete_password(self, key_id: str, username: str) -> None: | ||
""" | ||
Deletes the password for a specific ``key_id`` | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from __future__ import annotations | ||
|
||
import keyring | ||
from keyring.errors import PasswordDeleteError | ||
|
||
from .base import Storage | ||
from ..exceptions import CondaAuthError | ||
|
||
|
||
class KeyringStorage(Storage): | ||
""" | ||
Storage implementation for keyring library | ||
""" | ||
|
||
def get_password(self, key_id: str, username: str) -> str | None: | ||
return keyring.get_password(key_id, username) | ||
|
||
def set_password(self, key_id: str, username: str, password: str) -> None: | ||
return keyring.set_password(key_id, username, password) | ||
|
||
def delete_password(self, key_id: str, username: str) -> None: | ||
try: | ||
return keyring.delete_password(key_id, username) | ||
except PasswordDeleteError as exc: | ||
raise CondaAuthError(f"Unable to remove secret: {exc}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.