|
| 1 | +import base64 |
| 2 | +import os |
| 3 | + |
| 4 | +try: |
| 5 | + import keyring |
| 6 | +except ImportError: |
| 7 | + keyring = None |
| 8 | + |
| 9 | +from upcloud_api.errors import UpCloudClientError |
| 10 | + |
| 11 | +ENV_KEY_USERNAME = "UPCLOUD_USERNAME" |
| 12 | +ENV_KEY_PASSWORD = "UPCLOUD_PASSWORD" # noqa: S105 |
| 13 | +ENV_KEY_TOKEN = "UPCLOUD_TOKEN" # noqa: S105 |
| 14 | + |
| 15 | +KEYRING_SERVICE_NAME = "UpCloud" |
| 16 | +KEYRING_TOKEN_USER = "" |
| 17 | +KEYRING_GO_PREFIX = "go-keyring-base64:" |
| 18 | + |
| 19 | + |
| 20 | +def _parse_keyring_value(value: str) -> str: |
| 21 | + if value.startswith(KEYRING_GO_PREFIX): |
| 22 | + value = value[len(KEYRING_GO_PREFIX) :] |
| 23 | + return base64.b64decode(value).decode() |
| 24 | + |
| 25 | + return value |
| 26 | + |
| 27 | + |
| 28 | +def _read_keyring_value(username: str) -> str: |
| 29 | + if keyring is None: |
| 30 | + return None |
| 31 | + |
| 32 | + value = keyring.get_password(KEYRING_SERVICE_NAME, username) |
| 33 | + try: |
| 34 | + return _parse_keyring_value(value) if value else None |
| 35 | + except Exception: |
| 36 | + raise UpCloudClientError( |
| 37 | + f"Failed to read keyring value for {username}. Ensure that the value saved to the system keyring is correct." |
| 38 | + ) from None |
| 39 | + |
| 40 | + |
| 41 | +class Credentials: |
| 42 | + """ |
| 43 | + Class for handling UpCloud API credentials. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, username: str = None, password: str = None, token: str = None): |
| 47 | + """ |
| 48 | + Initializes the Credentials object with username, password and/or token. Use `parse` method to read credentials from environment variables or keyring. |
| 49 | + """ |
| 50 | + self._username = username |
| 51 | + self._password = password |
| 52 | + self._token = token |
| 53 | + |
| 54 | + @property |
| 55 | + def authorization(self) -> str: |
| 56 | + """ |
| 57 | + Returns the authorization header value based on the provided credentials. |
| 58 | + """ |
| 59 | + if self._token: |
| 60 | + return f"Bearer {self._token}" |
| 61 | + |
| 62 | + credentials = f"{self._username}:{self._password}".encode() |
| 63 | + encoded_credentials = base64.b64encode(credentials).decode() |
| 64 | + return f"Basic {encoded_credentials}" |
| 65 | + |
| 66 | + @property |
| 67 | + def is_defined(self) -> bool: |
| 68 | + """ |
| 69 | + Checks if the credentials are defined. |
| 70 | + """ |
| 71 | + return bool(self._username and self._password or self._token) |
| 72 | + |
| 73 | + def _read_from_env(self): |
| 74 | + if not self._username: |
| 75 | + self._username = os.getenv(ENV_KEY_USERNAME) |
| 76 | + if not self._password: |
| 77 | + self._password = os.getenv(ENV_KEY_PASSWORD) |
| 78 | + if not self._token: |
| 79 | + self._token = os.getenv(ENV_KEY_TOKEN) |
| 80 | + |
| 81 | + def _read_from_keyring(self): |
| 82 | + if self._username and not self._password: |
| 83 | + self._password = _read_keyring_value(self._username) |
| 84 | + |
| 85 | + if self.is_defined: |
| 86 | + return |
| 87 | + |
| 88 | + self._token = _read_keyring_value(KEYRING_TOKEN_USER) |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def parse(cls, username: str = None, password: str = None, token: str = None): |
| 92 | + """ |
| 93 | + Parses credentials from the provided parameters, environment variables or the system keyring. |
| 94 | + """ |
| 95 | + credentials = cls(username, password, token) |
| 96 | + if credentials.is_defined: |
| 97 | + return credentials |
| 98 | + |
| 99 | + credentials._read_from_env() |
| 100 | + if credentials.is_defined: |
| 101 | + return credentials |
| 102 | + |
| 103 | + credentials._read_from_keyring() |
| 104 | + if credentials.is_defined: |
| 105 | + return credentials |
| 106 | + |
| 107 | + raise UpCloudClientError( |
| 108 | + f"Credentials not found. These must be set in configuration, via environment variables or in the system keyring ({KEYRING_SERVICE_NAME})" |
| 109 | + ) |
0 commit comments