Skip to content

Commit b098269

Browse files
Merge pull request #1136 from redis/DOC-4815-amr-python
DOC-4815 added candidate AMR connection page
2 parents bffa642 + 946ff04 commit b098269

File tree

1 file changed

+157
-0
lines changed
  • content/develop/clients/redis-py

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Learn how to authenticate to an Azure Managed Redis (AMR) database
13+
linkTitle: Connect to AMR
14+
title: Connect to Azure Managed Redis
15+
weight: 2
16+
---
17+
18+
The [`redis-entra-id`](https://github.com/redis/redis-py-entraid) package
19+
lets you authenticate your app to
20+
[Azure Managed Redis (AMR)](https://azure.microsoft.com/en-us/products/managed-redis)
21+
using [Microsoft Entra ID](https://learn.microsoft.com/en-us/entra/identity/).
22+
You can authenticate using a system-assigned or user-assigned
23+
[managed identity](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview)
24+
or a [service principal](https://learn.microsoft.com/en-us/entra/identity-platform/app-objects-and-service-principals),
25+
letting `redis-entra-id` fetch and renew the authentication tokens for you automatically.
26+
27+
## Install
28+
29+
Install [`redis-py`]({{< relref "/develop/clients/redis-py#install" >}}) first,
30+
if you have not already done so. Then, install `redis-entra-id` with the
31+
following command:
32+
33+
```bash
34+
pip install redis-entra-id
35+
```
36+
37+
## Create a `CredentialProvider` instance
38+
39+
A `CredentialProvider` object obtains the authentication credentials you
40+
need when you connect to Redis. See the sections below to learn how
41+
to create the `CredentialProvider` instances for AMR
42+
using the factory functions that `redis-entra-id` provides.
43+
44+
45+
### `CredentialProvider` for a service principal
46+
47+
Use the `create_from_service_principal()` factory function to create a
48+
`CredentialProvider` that authenticates to AMR using a
49+
service principal (see the
50+
[Microsoft documentation](https://learn.microsoft.com/en-us/entra/identity-platform/app-objects-and-service-principals) to learn more about service principals).
51+
52+
You will need the following details of your service principal to make the connection:
53+
54+
- Client ID
55+
- Client secret
56+
- Tenant ID
57+
58+
The example below shows how to import the required modules and call
59+
`create_from_service_principal()`:
60+
61+
```python
62+
from redis import Redis
63+
from redis_entraid.cred_provider import *
64+
65+
credential_provider = create_from_service_principal(
66+
<CLIENT_ID>,
67+
<CLIENT_SECRET>,
68+
<TENANT_ID>
69+
)
70+
```
71+
72+
This uses a default configuration but you can also provide a custom
73+
configuration using the `token_manager_config` parameter:
74+
75+
```python
76+
credential_provider = create_from_service_principal(
77+
<CLIENT_ID>,
78+
<CLIENT_SECRET>,
79+
<TENANT_ID>,
80+
token_manager_config=TokenManagerConfig(
81+
expiration_refresh_ratio=0.9,
82+
lower_refresh_bound_millis=DEFAULT_LOWER_REFRESH_BOUND_MILLIS,
83+
token_request_execution_timeout_in_ms=DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS,
84+
retry_policy=RetryPolicy(
85+
max_attempts=5,
86+
delay_in_ms=50
87+
)
88+
)
89+
)
90+
```
91+
92+
### `CredentialProvider` for a managed identity
93+
94+
Use the `create_from_managed_identity()` factory function to create a
95+
`CredentialProvider` that authenticates to AMR using a
96+
managed identity (see the
97+
[Microsoft documentation](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview) to learn more about managed identities).
98+
99+
The example below shows how to import the required modules and call
100+
`create_from_managed_identity()`.
101+
Pass `ManagedIdentityType.USER_ASSIGNED` or `ManagedIdentityType.SYSTEM_ASSIGNED`
102+
as the `identity_type` parameter.
103+
104+
```python
105+
from redis import Redis
106+
from redis_entraid.cred_provider import *
107+
108+
credential_provider = create_from_managed_identity(
109+
identity_type=ManagedIdentityType.SYSTEM_ASSIGNED,
110+
)
111+
```
112+
113+
This uses a default configuration but you can also provide a custom
114+
configuration using the `token_manager_config` parameter:
115+
116+
```python
117+
credential_provider = create_from_managed_identity(
118+
identity_type=ManagedIdentityType.SYSTEM_ASSIGNED,
119+
...
120+
121+
token_manager_config=TokenManagerConfig(
122+
expiration_refresh_ratio=0.9,
123+
lower_refresh_bound_millis=DEFAULT_LOWER_REFRESH_BOUND_MILLIS,
124+
token_request_execution_timeout_in_ms=DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS,
125+
retry_policy=RetryPolicy(
126+
max_attempts=5,
127+
delay_in_ms=50
128+
)
129+
)
130+
)
131+
```
132+
133+
## Connect
134+
135+
When you have created your `CredentialProvider` instance, you are ready to
136+
connect to AMR.
137+
The example below shows how to pass the instance as a parameter to the standard
138+
`Redis()` connection method.
139+
{{< note >}} Azure requires you to use
140+
[Transport Layer Security (TLS)](https://en.wikipedia.org/wiki/Transport_Layer_Security)
141+
when you connect (see
142+
[Connect with TLS]({{< relref "/develop/clients/redis-py/connect#connect-to-your-production-redis-with-tls" >}}) for more information).
143+
{{< /note >}}
144+
145+
```python
146+
r = Redis(
147+
host=<HOST>, port=<PORT>,
148+
credential_provider=credential_provider,
149+
ssl=True,
150+
ssl_certfile="./redis_user.crt",
151+
ssl_keyfile="./redis_user_private.key",
152+
ssl_ca_certs="./redis_ca.pem"
153+
)
154+
155+
// Test the connection.
156+
print("The database size is: {}".format(client.dbsize()))
157+
```

0 commit comments

Comments
 (0)