Skip to content

Commit 38020a8

Browse files
Merge pull request #1166 from redis/DOC-4816-amr-jedis
DOC-4816 added Jedis AMR connection page
2 parents b098269 + 4b953b9 commit 38020a8

File tree

1 file changed

+203
-0
lines changed
  • content/develop/clients/jedis

1 file changed

+203
-0
lines changed

content/develop/clients/jedis/amr.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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-authx-entraid`](https://github.com/redis/jvm-redis-authx-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-authx-entraid` fetch and renew the authentication tokens for you automatically.
26+
27+
See
28+
[Use Microsoft Entra for cache authentication](https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-azure-active-directory-for-authentication)
29+
in the Microsoft docs to learn how to configure Azure to use Entra ID authentication.
30+
31+
## Install
32+
33+
Install [`jedis`]({{< relref "/develop/clients/jedis" >}}) first,
34+
if you have not already done so.
35+
36+
If you are using Maven, add
37+
the following dependency to your `pom.xml` file:
38+
39+
```xml
40+
<dependency>
41+
<groupId>redis.clients.authentication</groupId>
42+
<artifactId>redis-authx-entraid</artifactId>
43+
<version>0.1.1-beta1</version>
44+
</dependency>
45+
```
46+
47+
If you are using Gradle, add the following dependency to your
48+
`build.gradle` file:
49+
50+
```bash
51+
implementation 'redis.clients.authentication:redis-authx-entraid:0.1.1-beta1'
52+
```
53+
54+
## Create a `TokenAuthConfig` instance
55+
56+
The `TokenAuthConfig` class contains the authentication details that you
57+
must supply when you connect to Redis. Chain the methods of the
58+
`EntraIDTokenAuthConfigBuilder` class together (starting with the `builder()`
59+
method) to include the details you need, as shown in the following example:
60+
61+
```java
62+
TokenAuthConfig authConfig = EntraIDTokenAuthConfigBuilder.builder()
63+
.secret("<secret>")
64+
.authority("<authority>")
65+
// Other options...
66+
.build();
67+
```
68+
69+
Some of the details you can supply are common to different use cases:
70+
71+
- `secret()`: A string containing the [authentication secret](https://learn.microsoft.com/en-us/purview/sit-defn-azure-ad-client-secret).
72+
- `authority()`: A string containing the [authority](https://learn.microsoft.com/en-us/entra/identity-platform/msal-client-application-configuration#authority)
73+
URL.
74+
- `scopes()`: A set of strings defining the [scopes](https://learn.microsoft.com/en-us/entra/identity-platform/scopes-oidc)
75+
you want to apply. Configure your client application to acquire a Microsoft Entra token for scope, `https://redis.azure.com/.default` or `acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default` (as detailed at [Microsoft Entra ID for authentication](https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/managed-redis/managed-redis-entra-for-authentication#microsoft-entra-client-workflow))
76+
with the
77+
[Microsoft Authentication Library (MSAL)](https://learn.microsoft.com/en-us/entra/identity-platform/msal-overview)
78+
79+
(See [Advanced configuration options](#advanced-configuration-options) below
80+
to learn more about the options for controlling token request retry and timeout
81+
behavior.)
82+
83+
You can also add configuration to authenticate with a [service principal](#serv-principal)
84+
or a [managed identity](#mgd-identity) as described in the sections below.
85+
86+
### Configuration for a service principal {#serv-principal}
87+
88+
Add `clientId()` to the `EntraIDTokenAuthConfigBuilder` chain to specify
89+
authentication via a service principal, passing the ID token string as
90+
a parameter. (See the
91+
[Microsoft EntraID docs](https://learn.microsoft.com/en-us/entra/identity-platform/app-objects-and-service-principals)
92+
for more information about service principals.)
93+
94+
```java
95+
TokenAuthConfig authConfig = EntraIDTokenAuthConfigBuilder.builder()
96+
.clientId("<CLIENT-ID>")
97+
// ...
98+
.build();
99+
```
100+
101+
### Configuration for a managed identity {#mgd-identity}
102+
103+
You can also authenticate to AMR using a managed identity (see the
104+
[Microsoft documentation](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview) to learn more about managed identities).
105+
106+
For a system assigned managed identity, simply add the `systemAssignedManagedIdentity()`
107+
method to the `EntraIDTokenAuthConfigBuilder` chain:
108+
109+
```java
110+
TokenAuthConfig authConfig = EntraIDTokenAuthConfigBuilder.builder()
111+
.systemAssignedManagedIdentity()
112+
// ...
113+
.build();
114+
```
115+
116+
For a user assigned managed identity, add `userAssignedManagedIdentity()`. This
117+
requires a member of the `UserManagedIdentityType` enum (to select a
118+
`CLIENT_ID`, `OBJECT_ID`, or `RESOURCE_ID`) as well as the `id` string itself:
119+
120+
```java
121+
TokenAuthConfig authConfig = EntraIDTokenAuthConfigBuilder.builder()
122+
.userAssignedManagedIdentity(
123+
UserManagedIdentityType.CLIENT_ID,
124+
"<ID>"
125+
)
126+
// ...
127+
.build();
128+
129+
```
130+
131+
## Connect using `DefaultJedisClientConfig`
132+
133+
When you have created your `TokenAuthConfig` instance, you are ready to
134+
connect to AMR.
135+
The example below shows how to include the `TokenAuthConfig` details in a
136+
`JedisClientConfig` instance and use it with the `UnifiedJedis` connection.
137+
The connection uses
138+
[Transport Layer Security (TLS)](https://en.wikipedia.org/wiki/Transport_Layer_Security),
139+
which is recommended and enabled by default for managed identities. See
140+
[Connect to your production Redis with TLS]({{< relref "/develop/clients/jedis/connect#connect-to-your-production-redis-with-tls" >}}) for more information about
141+
TLS connections, including the implementation of the `createSslSocketFactory()`
142+
method used in the example.
143+
144+
```java
145+
TokenAuthConfig authConfig = EntraIDTokenAuthConfigBuilder.builder()
146+
// Chain of options...
147+
.build();
148+
149+
SSLSocketFactory sslFactory = createSslSocketFactory(
150+
"./truststore.jks",
151+
"secret!", // Use the password you specified for `keytool`
152+
"./redis-user-keystore.p12",
153+
"secret!" // Use the password you specified for `openssl`
154+
);
155+
156+
JedisClientConfig config = DefaultJedisClientConfig.builder()
157+
// Include the `TokenAuthConfig` details.
158+
.authXManager(new AuthXManager(authConfig))
159+
.ssl(true).sslSocketFactory(sslFactory)
160+
.build();
161+
162+
UnifiedJedis jedis = new UnifiedJedis(
163+
new HostAndPort("<host>", <port>),
164+
config
165+
);
166+
167+
// Test the connection.
168+
System.out.println(String.format("Database size is %d", jedis.dbSize()));
169+
```
170+
171+
## Advanced configuration options
172+
173+
The `TokenAuthConfig` class has several other options that you can
174+
set with the `EntraIDTokenAuthConfigBuilder.builder()`. These give you
175+
more precise control over the way the token is renewed:
176+
177+
```java
178+
TokenAuthConfig authConfig = EntraIDTokenAuthConfigBuilder.builder()
179+
.expirationRefreshRatio(0.75)
180+
.lowerRefreshBoundMillis(100)
181+
.tokenRequestExecTimeoutInMs(100)
182+
.maxAttemptsToRetry(10)
183+
.delayInMsToRetry()
184+
// ...
185+
.build();
186+
```
187+
188+
These options are explained below:
189+
190+
- `expirationRefreshRatio`: a `float` value representing the fraction
191+
of a token's lifetime that should elapse before attempting to
192+
refresh it. For example, a value of 0.75 means that you want to
193+
refresh the token after 75% of its lifetime has passed.
194+
- `lowerRefreshBoundMillis`: the minimum amount of the token's lifetime
195+
(in milliseconds) remaining before attempting to refresh, regardless
196+
of the `expirationRefreshRatio` value. Set this to zero if you want
197+
the refresh time to depend only on `expirationRefreshRatio`.
198+
- `tokenRequestExecTimeoutInMs`: the maximum time (in milliseconds) to
199+
wait for a token request to receive a response. A timeout occurs if this limit is exceeded.
200+
- `maxAttemptsToRetry`: the maximum number of times to retry a token
201+
request before aborting.
202+
- `delayInMsToRetry`: the time (in milliseconds) to wait before
203+
retrying a token request after a failed attempt. This provides a mechanism to request throttling to prevent an excessive number of token requests.

0 commit comments

Comments
 (0)