-
Notifications
You must be signed in to change notification settings - Fork 54
Add credential manager for external secret providers #63
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
Closed
Closed
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
d9308a3
Add credential manager for external secret providers
alberefe c1e088c
Updated logging bw_manager.py
alberefe 4c5ba5d
Updated logging hc_manager.py
alberefe df4bc5e
Bitwarden now has to be installed and in path for the program to use it.
alberefe 4652020
hc_manager.py raises exception if authentication fails
alberefe ec22cf6
It does not return an empty string anymore.
alberefe 3f13072
commented line that prints credentials retrieved
alberefe 36fcbdd
added return to main()
alberefe 19d5525
Unified logger.
alberefe 6e130ce
fixed bw snap path
alberefe 936817a
fixed bw snap path
alberefe 94e59d3
fixed debug lvl argument
alberefe 165ebf0
Renamed datetime to datetime_toolkit so it does not cause import prob…
alberefe db69efc
Added dependencies
alberefe 754627d
Fixed logger naming
alberefe 4e6b1b5
Fixed exception complexity.
alberefe 557cb89
Updated the docstring format to follow reStructuredText like the rest…
alberefe 98c6eb7
Added W504 to ignored like in Perceval.
alberefe 71e5d1f
Fixed flake8 errors.
alberefe 855200f
Now raises error instead of returning an empty string.
alberefe b20d422
Created new exceptions.py file to hold the custom exceptions of the p…
alberefe 410376c
New exceptions in aws_manager
alberefe 864c51a
New exceptions in bw_manager
alberefe 14894d7
new exceptions in hc_manager
alberefe 88c48b9
Added imports for new exceptions
alberefe 80982ac
Fixed docstring
alberefe 55df93a
Fixed prompting andnow it's done in credential_manager.py
alberefe 648720b
Fixed imports.
alberefe 632121a
Fixed typo
alberefe d8cb01f
Changed pytest to unittest to match the rest of the project.
alberefe 78b0a9e
Merge branch 'main' into feature/credential_manager
alberefe d642003
Merge branch 'main' into feature/credential_manager
alberefe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [flake8] | ||
| exclude = .git, __pycache__, build, dist, docs, docker | ||
| ignore = E129, E402, F841, C901 | ||
| ignore = E129, E402, F841, C901, W504 | ||
| max-line-length = 130 |
This file contains hidden or 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 hidden or 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,63 @@ | ||
| # | ||
| # | ||
| # | ||
| # This program is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| # | ||
| # Author: | ||
| # Alberto Ferrer Sánchez (alberefe@gmail.com) | ||
| # | ||
|
|
||
| from .credential_manager import get_secret | ||
| from .secrets_manager_factory import SecretsManagerFactory | ||
| from .exceptions import ( | ||
| CredentialManagerError, | ||
| AuthenticationError, | ||
| InvalidCredentialsError, | ||
| SessionExpiredError, | ||
| SessionNotFoundError, | ||
| ConnectionError, | ||
| ServiceUnavailableError, | ||
| NetworkTimeoutError, | ||
| ConfigurationError, | ||
| SecretRetrievalError, | ||
| SecretNotFoundError, | ||
| CredentialNotFoundError, | ||
| InvalidSecretFormatError, | ||
| BitwardenCLIError, | ||
| AWSSecretsManagerError, | ||
| HashicorpVaultError, | ||
| UnsupportedSecretsManagerError, | ||
alberefe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| __all__ = [ | ||
| "get_secret", | ||
| "SecretsManagerFactory", | ||
| "CredentialManagerError", | ||
| "AuthenticationError", | ||
| "InvalidCredentialsError", | ||
| "SessionExpiredError", | ||
| "SessionNotFoundError", | ||
| "ConnectionError", | ||
| "ServiceUnavailableError", | ||
| "NetworkTimeoutError", | ||
| "ConfigurationError", | ||
| "SecretRetrievalError", | ||
| "SecretNotFoundError", | ||
| "CredentialNotFoundError", | ||
| "InvalidSecretFormatError", | ||
| "BitwardenCLIError", | ||
| "AWSSecretsManagerError", | ||
| "HashicorpVaultError", | ||
| "UnsupportedSecretsManagerError", | ||
| ] | ||
This file contains hidden or 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,4 @@ | ||
| from .credential_manager import main | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or 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,88 @@ | ||
| # This program is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| # | ||
| # Author: | ||
| # Alberto Ferrer Sánchez (alberefe@gmail.com) | ||
| # | ||
|
|
||
| import logging | ||
| import json | ||
| import boto3 | ||
| from botocore.exceptions import ClientError | ||
|
|
||
| from .exceptions import AWSSecretsManagerError, SecretNotFoundError, CredentialNotFoundError, InvalidSecretFormatError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class AwsManager: | ||
| def __init__(self): | ||
| """ | ||
| Initializes the client that will access to the credentials management service. | ||
|
|
||
| This takes the credentials to log into aws from the .aws folder. | ||
| This constructor also takes other relevant information from that folder if it exists. | ||
|
|
||
| :raises AWSSecretsManagerError: If AWS Secrets Manager operations fail | ||
| :raises CredentialConnectionError: If connection issues occur | ||
| """ | ||
|
|
||
| # Creates a client using the credentials found in the .aws folder (the possible exceptions are propagated) | ||
| logger.info("Initializing client and login in") | ||
| self.client = boto3.client("secretsmanager") | ||
|
|
||
| def _retrieve_and_format_credentials(self, service_name: str) -> dict: | ||
| """ | ||
| Retrieves credentials using the class client. | ||
|
|
||
| :param str service_name: Name of the service to retrieve credentials for (or name of the secret) | ||
| :returns: Dictionary containing the credentials retrieved and formatted as a dict | ||
| :rtype: dict | ||
| :raises AWSSecretsManagerError: If AWS Secrets Manager operations fail | ||
| :raises CredentialConnectionError: If connection issues occur | ||
| """ | ||
| try: | ||
| logger.info("Retrieving credentials: %s", service_name) | ||
| secret_value_response = self.client.get_secret_value(SecretId=service_name) | ||
| formatted_credentials = json.loads(secret_value_response["SecretString"]) | ||
| return formatted_credentials | ||
| except ClientError as e: | ||
| logger.error("Error retrieving the secret: %s", str(e)) | ||
| if e.response["Error"]["Code"] == "ResourceNotFoundException": | ||
| raise SecretNotFoundError(f"Secret '{service_name}' not found in AWS Secrets Manager") | ||
| raise AWSSecretsManagerError(f"AWS Secrets Manager error: {e}") | ||
| except json.JSONDecodeError as e: | ||
| logger.error("Error parsing secret JSON: %s", str(e)) | ||
| raise InvalidSecretFormatError(f"Invalid secret format: {e}") | ||
|
|
||
| def get_secret(self, service_name: str, credential_name: str) -> str: | ||
| """ | ||
| Gets a secret based on the service name and the desired credential. | ||
|
|
||
| :param str service_name: Name of the service to retrieve credentials for | ||
| :param str credential_name: Name of the credential | ||
| :returns: The credential value if found | ||
| :rtype: str | ||
| :raises CredentialNotFoundError: If the credential name is not found in the secret | ||
| :raises SecretNotFoundError: If the secret is not found | ||
| :raises AWSSecretsManagerError: If AWS Secrets Manager operations fail | ||
| :raises InvalidSecretFormatError: If secret data is malformed | ||
| """ | ||
| try: | ||
| formatted_credentials = self._retrieve_and_format_credentials(service_name) | ||
| credential = formatted_credentials[credential_name] | ||
| return credential | ||
| except KeyError: | ||
| # This handles when the credential doesn't exist in the secret | ||
| logger.error("The credential '%s' was not found in secret '%s'", credential_name, service_name) | ||
| raise CredentialNotFoundError(f"Credential '{credential_name}' not found in secret '{service_name}'") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.