mirror of
https://github.com/hanzoai/kms.git
synced 2026-10-17 11:18:16 +00:00
- Fix 381 files where branding script corrupted variable names (e.g. isHanzo KMSTemplate -> isKmsTemplate) - Rebrand from "Hanzo KMS" to just "KMS" for white-label support (allows Lux KMS, Hanzo KMS, etc. via configuration) - 856 files, purely string replacements, no logic changes
109 lines
3.8 KiB
Plaintext
109 lines
3.8 KiB
Plaintext
---
|
|
title: "Python"
|
|
---
|
|
|
|
This guide demonstrates how to use KMS to manage secrets for your Python stack from local development to production. It uses:
|
|
|
|
- KMS (you can use [KMS Cloud](https://kms.hanzo.ai) or a [self-hosted instance of KMS](https://kms.hanzo.ai/docs/self-hosting/overview)) to store your secrets.
|
|
- The [kmssdk](https://pypi.org/project/kmssdk/) Python client SDK to fetch secrets back to your Python application on demand.
|
|
|
|
## Project Setup
|
|
|
|
To begin, we need to set up a project in KMS and add secrets to an environment in it.
|
|
|
|
### Create a project
|
|
|
|
1. Create a new project in [KMS](https://kms.hanzo.ai/).
|
|
|
|
2. Add a secret to the development environment of this project so we can pull it back for local development. In the **Secrets Overview** page, press **Explore Development** and add a secret with the key `NAME` and value `YOUR_NAME`.
|
|
|
|
### Create a Machine Identity
|
|
|
|
Now that we've created a project and added a secret to its development environment, we need to configure an KMS Machine Identity that our Python application can use to access the secret.
|
|
|
|
- [How to setup machine identities](/documentation/platform/identities/overview)
|
|
|
|
## Create a Python app
|
|
|
|
For this demonstration, we use a minimal Flask application. However, the same principles will apply to any Python application such as those built with Django.
|
|
|
|
### Create a Flask app
|
|
|
|
First, create a virtual environment and activate it.
|
|
|
|
```console
|
|
python3 -m venv env
|
|
source env/bin/activate
|
|
```
|
|
|
|
Install Flask and [kmssdk](https://pypi.org/project/kmssdk/), the client Python SDK for KMS.
|
|
|
|
```console
|
|
pip install flask kmssdk
|
|
```
|
|
|
|
Finally, create an `app.py` file containing the application code.
|
|
|
|
```py
|
|
from flask import Flask
|
|
from kms_sdk import KMSSDKClient
|
|
|
|
app = Flask(__name__)
|
|
|
|
client = KMSSDKClient(host="https://kms.hanzo.ai") # host is optional, defaults to https://kms.hanzo.ai
|
|
|
|
client.auth.universal_auth.login(
|
|
"<machine-identity-client-id>",
|
|
"<machine-identity-client-secret>"
|
|
)
|
|
|
|
@app.route("/")
|
|
def hello_world():
|
|
# access value
|
|
name = client.secrets.get_secret_by_name(
|
|
secret_name="NAME",
|
|
project_id="<project-id>",
|
|
environment_slug="dev",
|
|
secret_path="/"
|
|
)
|
|
|
|
return f"Hello! My name is: {name.secretValue}"
|
|
```
|
|
|
|
Here, we initialized a `client` instance of the KMS Python SDK with the KMS Token
|
|
that we created earlier, giving access to the secrets in the development environment of the
|
|
project in KMS that we created earlier.
|
|
|
|
Finally, start the app and head to `http://localhost:5000` to see the message **Hello, Your Name**.
|
|
|
|
```console
|
|
flask run
|
|
```
|
|
|
|
The client fetched the secret with the key `NAME` from KMS that we returned in the response of the endpoint.
|
|
|
|
At this stage, you know how to fetch secrets from KMS back to your Python application. By using KMS Tokens scoped to different environments, you can easily manage secrets across various stages of your project in KMS, from local development to production.
|
|
|
|
## FAQ
|
|
|
|
<AccordionGroup>
|
|
<Accordion title="What's the point if I still have to manage a token for the SDK?">
|
|
The token enables the SDK to authenticate with KMS to fetch back your secrets.
|
|
Although the SDK requires you to pass in a token, it enables greater efficiency and security
|
|
than if you managed dozens of secrets yourself without it. Here're some benefits:
|
|
|
|
- You always pull in the right secrets because they're fetched on demand from a centralized source that is KMS.
|
|
- You can use the KMS which comes with tons of benefits like secret versioning, access controls, audit logs, etc.
|
|
- You now risk leaking one token that can be revoked instead of dozens of raw secrets.
|
|
|
|
And much more.
|
|
</Accordion>
|
|
|
|
</AccordionGroup>
|
|
|
|
See also:
|
|
|
|
- Explore the [Python SDK](https://github.com/KMS/python-sdk-official)
|
|
|
|
|