|
| 1 | +# |
| 2 | +# |
| 3 | +# |
| 4 | +# This program is free software; you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation; either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | +# Author: |
| 18 | +# Alberto Ferrer Sánchez ([email protected]) |
| 19 | +# |
| 20 | + |
| 21 | +import subprocess |
| 22 | +import logging |
| 23 | +import shutil |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +from .exceptions import ( |
| 27 | + BitwardenCLIError, |
| 28 | + InvalidCredentialsError, |
| 29 | + CredentialNotFoundError, |
| 30 | +) |
| 31 | + |
| 32 | +logger = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +class BitwardenManager: |
| 36 | + """Retrieve credentials from Bitwarden. |
| 37 | +
|
| 38 | + This class defines functions to log in, retrieve secrets |
| 39 | + and log out of Bitwarden using the Bitwarden CLI. The |
| 40 | + workflow is: |
| 41 | +
|
| 42 | + manager = BitwardenManager(client_id, client_secret) |
| 43 | + manager.login() |
| 44 | + manager.get_secret("github") |
| 45 | + manager.get_secret("elasticsearch") |
| 46 | + manager.logout() |
| 47 | +
|
| 48 | + The manager logs in using the client_id and client_secret |
| 49 | + given as arguments when creating the instance, so the object |
| 50 | + is reusable along the program. |
| 51 | +
|
| 52 | + The path of Bitwarden CLI (bw) is retrieved using shutil. |
| 53 | + """ |
| 54 | + |
| 55 | + def __init__(self, client_id: str, client_secret: str): |
| 56 | + """ |
| 57 | + Creates BitwardenManager object using API key authentication |
| 58 | + """ |
| 59 | + # Session key of the bw session |
| 60 | + self.session_key = None |
| 61 | + |
| 62 | + # API credentials |
| 63 | + self.client_id = client_id |
| 64 | + self.client_secret = client_secret |
| 65 | + |
| 66 | + # Get the absolute path to the bw executable |
| 67 | + self.bw_path = shutil.which("bw") |
| 68 | + if not self.bw_path: |
| 69 | + raise BitwardenCLIError("Bitwarden CLI (bw) not found in PATH") |
| 70 | + |
| 71 | + # Set up environment variables for consistent execution context |
| 72 | + self.env = {"LANG": "C", "BW_CLIENTID": client_id, "BW_CLIENTSECRET": client_secret} |
| 73 | + |
| 74 | + def login(self) -> str | None: |
| 75 | + """Log into Bitwarden. |
| 76 | +
|
| 77 | + Use the API authentication key to log in and unlock the vault. After it, |
| 78 | + it will obtain a session key that will be used by to access the vault. |
| 79 | +
|
| 80 | + :returns: The session key for the current Bitwarden session. |
| 81 | +
|
| 82 | + :raises InvalidCredentialsError: If invalid credentials are provided |
| 83 | + :raises BitwardenCLIError: If Bitwarden CLI operations fail |
| 84 | + """ |
| 85 | + # Log in using API key |
| 86 | + login_result = subprocess.run( |
| 87 | + [self.bw_path, "login", "--apikey"], |
| 88 | + input=f"{self.client_id}\n{self.client_secret}\n", |
| 89 | + capture_output=True, |
| 90 | + text=True, |
| 91 | + env=self.env, |
| 92 | + ) |
| 93 | + |
| 94 | + if login_result.returncode != 0: |
| 95 | + error_msg = login_result.stderr.strip() if login_result.stderr else "Unknown error" |
| 96 | + logger.error("Error logging in with API key: %s", error_msg) |
| 97 | + raise InvalidCredentialsError("Invalid API credentials provided for Bitwarden") |
| 98 | + |
| 99 | + # After login, we need to unlock the vault to get a session key |
| 100 | + self.session_key = self._unlock_vault() |
| 101 | + |
| 102 | + return self.session_key |
| 103 | + |
| 104 | + def _unlock_vault(self) -> str: |
| 105 | + """Unlock the vault after authentication. |
| 106 | +
|
| 107 | + Executes the Bitwarden unlock command to obtain a session key |
| 108 | + for an already authenticated user but locked vault. |
| 109 | +
|
| 110 | + :returns: Session key for the unlocked vault |
| 111 | + :raises BitwardenCLIError: If unlock operation fails or returns empty session key |
| 112 | + """ |
| 113 | + unlock_result = subprocess.run( |
| 114 | + [self.bw_path, "unlock", "--raw"], |
| 115 | + capture_output=True, |
| 116 | + text=True, |
| 117 | + env=self.env, |
| 118 | + ) |
| 119 | + |
| 120 | + if unlock_result.returncode != 0: |
| 121 | + error_msg = unlock_result.stderr.strip() if unlock_result.stderr else "Unknown error" |
| 122 | + logger.error("Error unlocking vault: %s", error_msg) |
| 123 | + raise BitwardenCLIError(f"Failed to unlock vault: {error_msg}") |
| 124 | + |
| 125 | + session_key = unlock_result.stdout.strip() if unlock_result.stdout else "" |
| 126 | + if not session_key: |
| 127 | + raise BitwardenCLIError("Empty session key received from unlock command") |
| 128 | + |
| 129 | + return session_key |
| 130 | + |
| 131 | + def get_secret(self, item_name: str) -> Any | None: |
| 132 | + """ |
| 133 | + Retrieves an item by name from the Bitwarden vault. This |
| 134 | + retrieves all the fields stored in json format for an item |
| 135 | + with the name provided as an argument. |
| 136 | +
|
| 137 | + This json can be later parsed to retrieve the values of the |
| 138 | + desired fields. (see docs) |
| 139 | +
|
| 140 | + :param str item_name: The name of the item to retrieve. |
| 141 | + :raises CredentialNotFoundError: If the specific credential is not found |
| 142 | + :raises BitwardenCLIError: If Bitwarden CLI operations fail |
| 143 | + """ |
| 144 | + secret = subprocess.run( |
| 145 | + [self.bw_path, "get", "item", item_name], |
| 146 | + capture_output=True, |
| 147 | + text=True, |
| 148 | + env=self.env, |
| 149 | + ) |
| 150 | + |
| 151 | + if secret.returncode != 0: |
| 152 | + raise CredentialNotFoundError(f"Credential not found: '{item_name}'") |
| 153 | + |
| 154 | + return secret |
| 155 | + |
| 156 | + def logout(self) -> None: |
| 157 | + """Log out from Bitwarden and invalidate the session. |
| 158 | +
|
| 159 | + This method ends the current session and clears the session key. |
| 160 | + """ |
| 161 | + logger.info("Logging out from Bitwarden") |
| 162 | + |
| 163 | + # Execute logout command |
| 164 | + result = subprocess.run( |
| 165 | + [self.bw_path, "logout"], |
| 166 | + capture_output=True, |
| 167 | + text=True, |
| 168 | + env=self.env, |
| 169 | + ) |
| 170 | + |
| 171 | + if result.returncode != 0: |
| 172 | + error_msg = result.stderr.strip() if result.stderr else "Unknown error" |
| 173 | + logger.error("Error during logout: %s", error_msg) |
| 174 | + |
| 175 | + # Clear session key for security |
| 176 | + self.session_key = None |
| 177 | + |
| 178 | + logger.info("Successfully logged out from Bitwarden") |
0 commit comments