Entra ID credentials provider implementation for Redis-py client
Project description
The redis-entra-id Python package helps simplifying the authentication with Azure Managed Redis and Azure Cache for Redis using Microsoft Entra ID (formerly Azure Active Directory). It enables seamless integration with Azure's Redis services by fetching authentication tokens and managing the token renewal in the background. This package builds on top of redis-py and provides a structured way to authenticate by using a:
- System-assigned managed identity
- User-assigned managed identity
- Service principal
You can learn more about managed identities in the Microsoft Entra ID documentation.
Preparation
Create a service principal in Azure
In this quick start guide, you will register an application and create a service principal in Azure. Then the following credentials are used to authenticate via Entra ID:
- Tenant id
- Client id
- Client secret
Create cache and grant access
Create a Redis cache in Azure and grant your service principal access:
- Create a cache resource and wait until it was created successfully
- Navigate to
Settings/Authentication - If needed, enable Entra ID authentication
- Assign your previously created service principal to the cache
Further details are available in the AMR or ACR documentation.
Install the Entra ID package
You need to install the redis-py Entra ID package via the following command:
pip install redis-entra-id
The package depends on redis-py.
Usage
Step 1 - Import the dependencies
After having installed the package, you can import its modules:
from redis import Redis
from redis_entraid.cred_provider import *
Step 2 - Create the credential provider via the factory method
credential_provider = create_from_service_principal(
CLIENT_ID,
CLIENT_SECRET,
TENANT_ID
)
Step 3 - Provide optional token renewal configuration
The default configuration would be applied, but you're able to customise it.
credential_provider = create_from_service_principal(
CLIENT_ID,
CLIENT_SECRET,
TENANT_ID,
token_manager_config=TokenManagerConfig(
expiration_refresh_ratio=0.9,
lower_refresh_bound_millis=DEFAULT_LOWER_REFRESH_BOUND_MILLIS,
token_request_execution_timeout_in_ms=DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS,
retry_policy=RetryPolicy(
max_attempts=5,
delay_in_ms=50
)
)
)
You can test the credentials provider by obtaining a token. The following example demonstrates both, a synchronous and an asynchronous approach:
# Synchronous
credential_provider.get_credentials()
# Asynchronous
await credential_provider.get_credentials_async()
Step 4 - Connect to Redis
When using Entra ID, Azure enforces TLS on your Redis connection. Here is an example that shows how to test the connection in an insecure way:
client = Redis(host=HOST, port=PORT, ssl=True, ssl_cert_reqs=None, credential_provider=credential_provider)
print("The database size is: {}".format(client.dbsize()))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file redis_entraid-0.3.0b1.tar.gz.
File metadata
- Download URL: redis_entraid-0.3.0b1.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
127daf25b3126082f2c1f13ea3543a3eec7efba6d9ce0c3c38cb775f4546e136
|
|
| MD5 |
c530c8f570a94e556dbc779eb4b09e1c
|
|
| BLAKE2b-256 |
b02a21b2dff4f1942a91c60f474cd1ee1b615a5b0f08b793665f4f7323e7c057
|
File details
Details for the file redis_entraid-0.3.0b1-py3-none-any.whl.
File metadata
- Download URL: redis_entraid-0.3.0b1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfd63ddca8baf61a090f3b0b170a9df56f50e5244a8d9be54e220cb8e9a73e72
|
|
| MD5 |
51ae58c5fa0b80430207582a64dad2ff
|
|
| BLAKE2b-256 |
f210443e584571b1eaa142597ddc042b06e6db18d65fb11a427bc20346169515
|