Skip to content

Commit 24f7ea2

Browse files
DOC-4815 added candidate AMR connection page
1 parent 9809b31 commit 24f7ea2

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: 5
16+
---
17+
18+
The `redis-entra-id` package lets you authenticate your app to
19+
[Azure Managed Redis (AMR)](https://azure.microsoft.com/en-us/products/managed-redis)
20+
using [Microsoft Entra ID](https://learn.microsoft.com/en-us/entra/identity/).
21+
You can authenticate using a system-assigned or user-assigned
22+
[managed identity](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview)
23+
or a [service principal](https://learn.microsoft.com/en-us/entra/identity-platform/app-objects-and-service-principals),
24+
letting `redis-entra-id` fetch and renew the authentication tokens for you automatically.
25+
26+
## Install
27+
28+
Install [`redis-py`]({{< relref "/develop/clients/redis-py#install" >}}) first,
29+
if you have not already done so. Then, install `redis-entra-id` with the
30+
following command:
31+
32+
```bash
33+
pip install redis-entra-id
34+
```
35+
36+
## Create a `CredentialProvider` instance
37+
38+
A `CredentialProvider` object obtains the authentication credentials you
39+
need when you connect to Redis. See the sections below to learn how
40+
to create the `CredentialProvider` instances for AMR
41+
using the factory functions that `redis-entra-id` provides.
42+
43+
44+
### `CredentialProvider` for a service principal
45+
46+
Use the `create_from_service_principal()` factory function to create a
47+
`CredentialProvider` that authenticates to AMR using a
48+
service principal (see the
49+
[Microsoft documentation](https://learn.microsoft.com/en-us/entra/identity-platform/app-objects-and-service-principals) to learn more about service principals).
50+
51+
You will need the following details of your service principal to make the connection:
52+
53+
- Client ID
54+
- Client secret
55+
- Tenant ID
56+
57+
The example below shows how to import the required modules and call
58+
`create_from_service_principal()`:
59+
60+
```python
61+
from redis import Redis
62+
from redis_entraid.cred_provider import *
63+
64+
credential_provider = create_from_service_principal(
65+
<CLIENT_ID>,
66+
<CLIENT_SECRET>,
67+
<TENANT_ID>
68+
)
69+
```
70+
71+
This uses a default configuration but you can also provide a custom
72+
configuration using the `token_manager_config` parameter:
73+
74+
```python
75+
credential_provider = create_from_service_principal(
76+
<CLIENT_ID>,
77+
<CLIENT_SECRET>,
78+
<TENANT_ID>,
79+
token_manager_config=TokenManagerConfig(
80+
expiration_refresh_ratio=0.9,
81+
lower_refresh_bound_millis=DEFAULT_LOWER_REFRESH_BOUND_MILLIS,
82+
token_request_execution_timeout_in_ms=DEFAULT_TOKEN_REQUEST_EXECUTION_TIMEOUT_IN_MS,
83+
retry_policy=RetryPolicy(
84+
max_attempts=5,
85+
delay_in_ms=50
86+
)
87+
)
88+
)
89+
```
90+
91+
### `CredentialProvider` for a managed identity
92+
93+
Use the `create_from_managed_identity()` factory function to create a
94+
`CredentialProvider` that authenticates to AMR using a
95+
managed identity (see the
96+
[Microsoft documentation](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview) to learn more about managed identities).
97+
98+
The example below shows how to import the required modules and call
99+
`create_from_managed_identity()`.
100+
Pass `ManagedIdentityType.USER_ASSIGNED` or `ManagedIdentityType.SYSTEM_ASSIGNED`
101+
as the `identity_type` parameter.
102+
103+
```python
104+
from redis import Redis
105+
from redis_entraid.cred_provider import *
106+
107+
credential_provider = create_from_managed_identity(
108+
identity_type=ManagedIdentityType.USER_ASSIGNED,
109+
...
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.USER_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)