-
Notifications
You must be signed in to change notification settings - Fork 220
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
Info on how to retrieve secrets using google secret manager #1505
Changes from 2 commits
087a7ad
dc65951
13c95bd
74ec761
68e3242
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,72 @@ DESTINATION__BIGQUERY__CREDENTIALS__PRIVATE_KEY | |
DESTINATION__BIGQUERY__CREDENTIALS__CLIENT_EMAIL | ||
DESTINATION__BIGQUERY__LOCATION | ||
``` | ||
|
||
## Retrieving credentials from Google Cloud Secret Manager | ||
To retrieve secrets from Google Cloud Secret Manager using Python, and convert them into a dictionary format, you'll need to follow these steps. First, ensure that you have the necessary permissions to access the secrets on Google Cloud, and have the `google-cloud-secret-manager` library installed. If not, you can install it using pip: | ||
|
||
```sh | ||
pip install google-cloud-secret-manager | ||
``` | ||
[Google Docs](https://cloud.google.com/secret-manager/docs/reference/libraries) | ||
|
||
Here's how you can retrieve secrets and convert them into a dictionary: | ||
|
||
1. **Set up the Secret Manager client**: Create a client that will interact with the Secret Manager API. | ||
2. **Access the secret**: Use the client to access the secret's latest version. | ||
3. **Convert to a dictionary**: If the secret is stored in a structured format (like JSON), parse it into a Python dictionary. | ||
|
||
Assume we store secrets in JSON format: | ||
```json | ||
{"api_token": "ghp_Kskdgf98dugjf98ghd...."} | ||
``` | ||
|
||
In the script `dlt_with_google_secrets_pipeline.py` you can find an example how to use Google Secrets in `dlt` pipelines. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of this lone put the code snippet from the bottom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bottom code shifted here. |
||
|
||
### Points to Note: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should be at the end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved |
||
|
||
- **Permissions**: Ensure the service account or user credentials you are using have the necessary permissions to access the Secret Manager and the specific secrets. | ||
- **Secret Format**: This example assumes that the secret is stored in a JSON string format. If your secret is in a different format, you will need to adjust the parsing method accordingly. | ||
- **Google Cloud Authentication**: Make sure your environment is authenticated with Google Cloud. This can typically be done by setting credentials in `.dlt/secrets.toml` or setting the `GOOGLE_SECRETS__CREDENTIALS` environment variable to the path of your service account key file or the dict of credentials as a string. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add an example of credentials also
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
|
||
With this setup, you can effectively retrieve secrets stored in Google Cloud Secret Manager and use them in your `dlt` pipelines as dictionaries. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can delete this sentence at all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deleted |
||
|
||
You can use this function to retrieve the secrets stored in the secret manager: | ||
|
||
```py | ||
import json as json_lib # Rename the json import to avoid name conflict | ||
|
||
import dlt | ||
from dlt.sources.helpers import requests | ||
from dlt.common.configuration.inject import with_config | ||
from dlt.common.configuration.specs import GcpServiceAccountCredentials | ||
from google.cloud import secretmanager | ||
|
||
|
||
@with_config(sections=("google_secrets",)) | ||
def get_secret_dict(secret_id: str, credentials: GcpServiceAccountCredentials = dlt.secrets.value) -> dict: | ||
""" | ||
Retrieve a secret from Google Cloud Secret Manager and convert it to a dictionary. | ||
""" | ||
# Create the Secret Manager client with provided credentials | ||
client = secretmanager.SecretManagerServiceClient(credentials=credentials.to_native_credentials()) | ||
|
||
# Build the resource name of the secret version | ||
name = f"projects/{credentials.project_id}/secrets/{secret_id}/versions/latest" | ||
|
||
# Access the secret version | ||
response = client.access_secret_version(request={"name": name}) | ||
|
||
# Decode the payload to a string and convert it to a dictionary | ||
secret_string = response.payload.data.decode("UTF-8") | ||
secret_dict = json_lib.loads(secret_string) | ||
|
||
return secret_dict | ||
|
||
# Retrieve secret data as a dictionary for use in other functions. | ||
secret_data = get_secret_dict("temp-secret") | ||
|
||
# Fetch the list of repositories from the specified organization using the provided API token. | ||
# The API token is retrieved from the secret data dictionary. | ||
data = get_repositories(api_token=secret_data["api_token"], organization="dlt-hub") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mentioning function which you don;t provide in a code snippet is not cool. let's make it clearer:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, added it to the code. |
||
``` |
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.
describe what kind of doc it is.
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.
done