Skip to main content

Entra ID credentials provider implementation for Redis-py client

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vivi_redis_entraid-0.4.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vivi_redis_entraid-0.4.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file vivi_redis_entraid-0.4.0.tar.gz.

File metadata

  • Download URL: vivi_redis_entraid-0.4.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for vivi_redis_entraid-0.4.0.tar.gz
Algorithm Hash digest
SHA256 6353aacc1da96ae327e2cbf60857942ed9f5c3190a9458f888d2725854caeede
MD5 b3f1d7471d36a8a8a461a0c94c7d19c4
BLAKE2b-256 a6df8a84795d57ce3742a8b18a02b7ee650059ab3ab1edf1ede7bdd04fba34a0

See more details on using hashes here.

File details

Details for the file vivi_redis_entraid-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for vivi_redis_entraid-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9af5a67330db591323a7471ded26c7ef660250625b947d43c6e9d9b2dfc0fbe
MD5 ef698ca1e8a38379c2de9edb77ef48c3
BLAKE2b-256 95bd07070f186523e304a94cb62a6a103b2422a32344608fd1e0180d5dea8f86

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page