|
| 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 [`Lettuce`]({{< relref "/develop/clients/lettuce" >}}) 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 `TokenBasedRedisCredentialsProvider` instance |
| 55 | + |
| 56 | +The `TokenBasedRedisCredentialsProvider` 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 | +TokenBasedRedisCredentialsProvider credentials; |
| 63 | + |
| 64 | +try ( EntraIDTokenAuthConfigBuilder builder = EntraIDTokenAuthConfigBuilder.builder()) { |
| 65 | + builder.clientId(CLIENT_ID) |
| 66 | + .secret(CLIENT_SECRET) |
| 67 | + .authority(AUTHORITY) // "https://login.microsoftonline.com/{YOUR_TENANT_ID}"; |
| 68 | + .scopes(SCOPES); // "https://redis.azure.com/.default"; |
| 69 | + |
| 70 | + credentials = TokenBasedRedisCredentialsProvider.create(builder.build()); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Some of the details you can supply are common to different use cases: |
| 75 | + |
| 76 | +- `secret()`: A string containing the [authentication secret](https://learn.microsoft.com/en-us/purview/sit-defn-azure-ad-client-secret). |
| 77 | +- `authority()`: A string containing the [authority](https://learn.microsoft.com/en-us/entra/identity-platform/msal-client-application-configuration#authority) |
| 78 | + URL. |
| 79 | +- `scopes()`: A set of strings defining the [scopes](https://learn.microsoft.com/en-us/entra/identity-platform/scopes-oidc) |
| 80 | + 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` |
| 81 | + with the |
| 82 | + [Microsoft Authentication Library (MSAL)](https://learn.microsoft.com/en-us/entra/identity-platform/msal-overview) |
| 83 | + |
| 84 | +You can also add configuration to authenticate with a [service principal](#serv-principal) |
| 85 | +or a [managed identity](#mgd-identity) as described in the sections below. |
| 86 | + |
| 87 | +When you have created your `TokenBasedRedisCredentialsProvider` instance, you may want to |
| 88 | +test it by obtaining a token, as shown in the folowing example: |
| 89 | + |
| 90 | +```java |
| 91 | +// Test that the Entra ID credentials provider can resolve credentials. |
| 92 | +credentials.resolveCredentials() |
| 93 | + .doOnNext(c-> System.out.println(c.getUsername())) |
| 94 | + .block(); |
| 95 | +``` |
| 96 | + |
| 97 | +### Configuration for a service principal {#serv-principal} |
| 98 | + |
| 99 | +Add `clientId()` to the `EntraIDTokenAuthConfigBuilder` chain to specify |
| 100 | +authentication via a service principal, passing the ID token string as |
| 101 | +a parameter. (See the |
| 102 | +[Microsoft EntraID docs](https://learn.microsoft.com/en-us/entra/identity-platform/app-objects-and-service-principals) |
| 103 | +for more information about service principals.) |
| 104 | + |
| 105 | +```java |
| 106 | +TokenBasedRedisCredentialsProvider credentials; |
| 107 | + |
| 108 | +try ( EntraIDTokenAuthConfigBuilder builder = EntraIDTokenAuthConfigBuilder.builder()) { |
| 109 | + builder.clientId(CLIENT_ID) |
| 110 | + .secret(CLIENT_SECRET) |
| 111 | + .authority(AUTHORITY) // "https://login.microsoftonline.com/{YOUR_TENANT_ID}"; |
| 112 | + .scopes(SCOPES); // "https://redis.azure.com/.default"; |
| 113 | + |
| 114 | + credentials = TokenBasedRedisCredentialsProvider.create(builder.build()); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Configuration for a managed identity {#mgd-identity} |
| 119 | + |
| 120 | +You can also authenticate to AMR using a managed identity (see the |
| 121 | +[Microsoft documentation](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview) to learn more about managed identities). |
| 122 | + |
| 123 | +For a system assigned managed identity, simply add the `systemAssignedManagedIdentity()` |
| 124 | +method to the `EntraIDTokenAuthConfigBuilder` chain: |
| 125 | + |
| 126 | +```java |
| 127 | +TokenBasedRedisCredentialsProvider credentials; |
| 128 | + |
| 129 | +try ( EntraIDTokenAuthConfigBuilder builder = EntraIDTokenAuthConfigBuilder.builder()) { |
| 130 | + builder.clientId(CLIENT_ID) |
| 131 | + // ... |
| 132 | + .systemAssignedManagedIdentity(); |
| 133 | + |
| 134 | + credentials = TokenBasedRedisCredentialsProvider.create(builder.build()); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +For a user assigned managed identity, add `userAssignedManagedIdentity()`. This |
| 139 | +requires a member of the `UserManagedIdentityType` enum (to select a |
| 140 | +`CLIENT_ID`, `OBJECT_ID`, or `RESOURCE_ID`) as well as the `id` string itself: |
| 141 | + |
| 142 | +```java |
| 143 | +TokenBasedRedisCredentialsProvider credentials; |
| 144 | + |
| 145 | +try ( EntraIDTokenAuthConfigBuilder builder = EntraIDTokenAuthConfigBuilder.builder()) { |
| 146 | + builder.clientId(CLIENT_ID) |
| 147 | + // ... |
| 148 | + .userAssignedManagedIdentity( |
| 149 | + UserManagedIdentityType.CLIENT_ID, |
| 150 | + "<ID>" |
| 151 | + ); |
| 152 | + |
| 153 | + credentials = TokenBasedRedisCredentialsProvider.create(builder.build()); |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Connect using the `withAuthentication()` option |
| 158 | + |
| 159 | +When you have created your `TokenBasedRedisCredentialsProvider` instance, you are ready to |
| 160 | +connect to AMR. |
| 161 | +The example below shows how to include the authentication details in a |
| 162 | +`TokenBasedRedisCredentialsProvider` instance and pass it to the `RedisURI.Builder` |
| 163 | +using the `withAuthentication()` option. It also uses a `ClientOptions` object to |
| 164 | +enable automatic re-authentication. |
| 165 | + |
| 166 | +The connection uses |
| 167 | +[Transport Layer Security (TLS)](https://en.wikipedia.org/wiki/Transport_Layer_Security), |
| 168 | +which is recommended and enabled by default for managed identities. See |
| 169 | +[TLS connection]({{< relref "/develop/clients/lettuce/connect#tls-connection" >}}) for more information. |
| 170 | + |
| 171 | +{{< note >}} The `Lettuce` client library doesn't manage the lifecycle of |
| 172 | +the `TokenBasedRedisCredentialsProvider` instance for you. You can reuse the |
| 173 | +same instance for as many clients and connections as you want. When you have |
| 174 | +finished using the credentials provider, call its `close()` method, as shown |
| 175 | +at the end of the example. |
| 176 | +{{< /note >}} |
| 177 | + |
| 178 | +```java |
| 179 | +// Entra ID credentials provider for Service Principal Identity with Client Secret. |
| 180 | +TokenBasedRedisCredentialsProvider credentialsSP; |
| 181 | +try (EntraIDTokenAuthConfigBuilder builder = EntraIDTokenAuthConfigBuilder.builder()) { |
| 182 | + builder |
| 183 | + .clientId(CLIENT_ID) |
| 184 | + .secret(CLIENT_SECRET).authority(AUTHORITY) // "https://login.microsoftonline.com/{YOUR_TENANT_ID}" |
| 185 | + .scopes(SCOPES); // "https://redis.azure.com/.default" |
| 186 | + |
| 187 | + credentialsSP = TokenBasedRedisCredentialsProvider.create(builder.build()); |
| 188 | +} |
| 189 | + |
| 190 | +// Optionally test the credentials provider. |
| 191 | +// credentialsSP.resolveCredentials().doOnNext(c -> System.out.println("SPI ID :" + c.getUsername())).block(); |
| 192 | + |
| 193 | +// Enable automatic re-authentication. |
| 194 | +ClientOptions clientOptions = ClientOptions.builder() |
| 195 | + .reauthenticateBehavior( |
| 196 | + ClientOptions.ReauthenticateBehavior.ON_NEW_CREDENTIALS |
| 197 | + ).build(); |
| 198 | + |
| 199 | +// Use the Entra ID credentials provider. |
| 200 | +RedisURI redisURI = RedisURI.builder() |
| 201 | + .withHost(HOST) |
| 202 | + .withPort(PORT) |
| 203 | + .withAuthentication(credentialsSP) |
| 204 | + .withSsl(true) |
| 205 | + .build(); |
| 206 | + |
| 207 | +// Create the RedisClient and set the re-authentication options. |
| 208 | +RedisClient redisClient = RedisClient.create(redisURI); |
| 209 | +redisClient.setOptions(clientOptions); |
| 210 | + |
| 211 | +// Connect with the credentials provider. |
| 212 | +try (StatefulRedisConnection<String, String> user1 = redisClient.connect(StringCodec.UTF8)) { |
| 213 | + System.out.println("Connected to redis as :" + user1.sync().aclWhoami()); |
| 214 | + System.out.println("Db size :" + user1.sync().dbsize()); |
| 215 | +} finally { |
| 216 | + redisClient.shutdown(); // Shutdown Redis client and close connections. |
| 217 | + credentialsSP.close(); // Shutdown Entra ID Credentials provider. |
| 218 | +} |
| 219 | +``` |
0 commit comments