-
Notifications
You must be signed in to change notification settings - Fork 55
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
Open
alberefe
wants to merge
13
commits into
chaoss:main
Choose a base branch
from
alberefe:feature/credential_manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,463
−0
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7a14bf3
Add credential manager for external secret providers
alberefe 90e439d
Updated logging bw_manager.py
alberefe a611632
Updated logging hc_manager.py
alberefe 7838824
Bitwarden now has to be installed and in path for the program to use it.
alberefe e3612ea
hc_manager.py raises exception if authentication fails
alberefe 0bfaecd
It does not return an empty string anymore.
alberefe 9673050
commented line that prints credentials retrieved
alberefe efdc245
added return to main()
alberefe 2d4306b
Unified logger.
alberefe 308697c
fixed bw snap path
alberefe 972b5ac
fixed bw snap path
alberefe 08386d5
fixed debug lvl argument
alberefe 6bb93b5
Renamed datetime to datetime_toolkit so it does not cause import prob…
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
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,25 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# | ||
# | ||
# 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 ([email protected]) | ||
# | ||
|
||
from .credential_manager import get_secret | ||
from .secrets_manager_factory import SecretsManagerFactory | ||
|
||
__all__ = ['get_secret', 'SecretsManagerFactory'] |
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,110 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# | ||
# | ||
# 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 ([email protected]) | ||
# | ||
|
||
import logging | ||
import json | ||
import boto3 | ||
from botocore.exceptions import EndpointConnectionError, SSLError, ClientError | ||
|
||
_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: | ||
Exception: If there's a connection error. | ||
""" | ||
|
||
# Creates a client using the credentials found in the .aws folder | ||
try: | ||
_logger.info("Initializing client and login in") | ||
self.client = boto3.client("secretsmanager") | ||
|
||
except (EndpointConnectionError, SSLError, ClientError, Exception) as e: | ||
_logger.error("Problem starting the client: %s", e) | ||
raise e | ||
|
||
def _retrieve_and_format_credentials(self, service_name: str) -> dict: | ||
""" | ||
Retrieves credentials using the class client. | ||
|
||
Args: | ||
service_name (str): Name of the service to retrieve credentials for.(or name of the secret) | ||
|
||
Returns: | ||
formatted_credentials (dict): Dictionary containing the credentials retrieved and formatted as a dict | ||
|
||
Raises: | ||
Exception: If there's a connection error. | ||
""" | ||
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, json.JSONDecodeError) as e: | ||
_logger.error("Error retrieving the secret: %s", str(e)) | ||
raise e | ||
|
||
def get_secret(self, service_name: str, credential_name: str) -> str: | ||
""" | ||
Gets a secret based on the service name and the desired credential. | ||
|
||
Args: | ||
service_name (str): Name of the service to retrieve credentials for | ||
credential_name (str): Name of the credential | ||
|
||
Returns: | ||
str: The credential value if found, empty string if not found | ||
|
||
Raises: | ||
Exception: If there's a connection error. | ||
""" | ||
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 secret %s:%s, was not found.", service_name, credential_name) | ||
_logger.error( | ||
"Please check the secret name and the credential name. For now here you have an empty string.") | ||
return "" | ||
except ClientError as e: | ||
# This handles AWS-specific errors like ResourceNotFoundException | ||
if e.response['Error']['Code'] == 'ResourceNotFoundException': | ||
_logger.error("The secret %s:%s, was not found.", service_name, credential_name) | ||
_logger.error(e) | ||
_logger.error( | ||
"Please check the secret name and the credential name. For now here you have an empty string.") | ||
return "" | ||
_logger.error("There was a problem getting the secret") | ||
raise e | ||
except Exception as e: | ||
_logger.error("There was a problem getting the secret") | ||
raise e |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the
pyproject
file using Poetry to include these requirements.