Skip to main content

The redis-entraid 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:

  1. Create a cache resource and wait until it was created successfully
  2. Navigate to Settings/Authentication
  3. If needed, enable Entra ID authentication
  4. 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-entraid

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

Following factory methods are offered depends on authentication type you need:

create_from_managed_identity - Creates a credential provider based on a managed identity. Managed identities allow Azure services to authenticate without needing explicit credentials, as they are automatically assigned by Azure.

create_from_service_principal - Creates a credential provider using a service principal. A service principal is typically used when you want to authenticate as an application, rather than as a user, with Azure Active Directory.

create_from_default_azure_credential - Creates a credential provider from a Default Azure Credential. This method allows automatic selection of the appropriate credential mechanism based on the environment (e.g., environment variables, managed identities, service principal, interactive browser etc.).

Examples

Managed Identity

credential_provider = create_from_managed_identity(
    identity_type=ManagedIdentityType.SYSTEM_ASSIGNED,
    resource="https://redis.azure.com/"
)

Service principal

credential_provider = create_from_service_principal(
    CLIENT_ID, 
    CLIENT_SECRET, 
    TENANT_ID
)

Default Azure Credential

credential_provider = create_from_default_azure_credential(
    ("https://redis.azure.com/.default",),
)

More examples available in examples folder.

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()))

Metadata

Release files for vivi-redis-entraid 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vivi-redis-entraid 1.0.0
File Size Uploaded Provenance
vivi_redis_entraid-1.0.0.tar.gz 9.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for vivi-redis-entraid 1.0.0
File Interpreter ABI Platform
vivi_redis_entraid-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.8 kB

Release files / vivi_redis_entraid-1.0.0.tar.gz

Download URL vivi_redis_entraid-1.0.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
dce2d6b9379398fe026d28e6cef3b29addb3ce28b63a881a31972785c2710547
BLAKE2b-256 checksum
How to use checksums
2b31c3f8f5786c8aa1627eeb2deb733c8b9a7d3bd4cdff7006abcb41f5cbc3bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / vivi_redis_entraid-1.0.0-py3-none-any.whl

Download URL vivi_redis_entraid-1.0.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df9d210f2a7bbb64ea166840c9c95f4344e892db19ae2163e36692b92801bca5
BLAKE2b-256 checksum
How to use checksums
41def0c6229ecb23797cd418911e9a23e5745e9201dd553eea0d34287530a932
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for vivi-redis-entraid 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page