Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Identity] update redis samples for public preview scope #26007

Merged
merged 24 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
349ec69
update redis samples to public scope
KarishmaGhiya May 25, 2023
ff8c2b8
update the instructions for public preview
KarishmaGhiya May 25, 2023
768b35d
update for timeout
KarishmaGhiya May 27, 2023
8ee092a
fixing what messed up
KarishmaGhiya May 30, 2023
9b8a7ff
update node redis
KarishmaGhiya May 30, 2023
6cb5fd9
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 15, 2023
d56b84c
Update sdk/identity/identity/samples/AzureCacheForRedis/ioredis.md
KarishmaGhiya Jun 16, 2023
cfc7aec
update the samples
KarishmaGhiya Jun 16, 2023
f062c38
Update sdk/identity/identity/samples/AzureCacheForRedis/ioredis.md
KarishmaGhiya Jun 19, 2023
baaf9c1
Update sdk/identity/identity/samples/AzureCacheForRedis/ioredis.md
KarishmaGhiya Jun 19, 2023
436a5b9
Update sdk/identity/identity/samples/AzureCacheForRedis/ioredis.md
KarishmaGhiya Jun 19, 2023
cfac612
Update sdk/identity/identity/samples/AzureCacheForRedis/ioredis.md
KarishmaGhiya Jun 19, 2023
93568c1
Update sdk/identity/identity/samples/AzureCacheForRedis/ioredis.md
KarishmaGhiya Jun 19, 2023
4b9c103
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
80d5f43
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
47fe0eb
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
3d3fd6f
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
a8a24e5
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
3e68a29
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
4832d2e
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
810cae1
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
778c6fa
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
68bc930
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
2ba294e
Update sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
KarishmaGhiya Jun 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions sdk/identity/identity/samples/AzureCacheForRedis/ioredis.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

```
"dependencies": {
"@azure/identity": "^2.0.5",
"ioredis": "^5.0.4",
"@azure/identity": "^3.2.1",
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
"ioredis": "^5.3.2"
}
```
- Familiarity with the [ioredis](https://github.com/luin/ioredis) and [Azure Identity for JavaScript](https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest) client libraries is assumed.
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -50,9 +51,7 @@ dotenv.config();
async function main() {
// Construct a Token Credential from Identity library, e.g. ClientSecretCredential / ClientCertificateCredential / ManagedIdentityCredential, etc.
const credential = new DefaultAzureCredential();

// The scope will be changed for Azure AD Public Preview
const redisScope = "https://*.cacheinfra.windows.net:10225/appid/.default"
const redisScope = "acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default"
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved

// Fetch an Azure AD token to be used for authentication. This token will be used as the password.
let accessToken = await credential.getToken(
Expand Down Expand Up @@ -111,8 +110,7 @@ import * as dotenv from "dotenv";
dotenv.config();

async function returnPassword(credential: TokenCredential) {
// The scope will be changed for Azure AD Public Preview
const redisScope = "https://*.cacheinfra.windows.net:10225/appid/.default"
const redisScope = "acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default"
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved

// Fetch an Azure AD token to be used for authentication. This token will be used as the password.
return credential.getToken(redisScope);
Expand Down Expand Up @@ -180,9 +178,14 @@ import { AccessToken, DefaultAzureCredential, TokenCredential } from "@azure/ide
import * as dotenv from "dotenv";
dotenv.config();

function randomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

async function returnPassword(credential: TokenCredential) {
// The scope will be changed for Azure AD Public Preview
const redisScope = "https://*.cacheinfra.windows.net:10225/appid/.default"
const redisScope = "acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default"
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved

// Fetch an Azure AD token to be used for authentication. This token will be used as the password.
let accessToken = await credential.getToken(redisScope);
Expand All @@ -197,7 +200,11 @@ async function main() {

async function updateToken() {
accessTokenCache = await returnPassword(credential);
id = setTimeout(updateToken, ((accessTokenCache.expiresOnTimestamp- 120*1000)) - Date.now());
let randomTimestamp = randomNumber(120000,300000);
id = setTimeout(updateToken, ((accessTokenCache.expiresOnTimestamp- randomTimestamp)) - Date.now());
if(redis){
await redis.auth(process.env.REDIS_SERVICE_PRINCIPAL_NAME, accessTokenCache.token);
}
}

await updateToken();
Expand Down Expand Up @@ -253,12 +260,12 @@ main().catch((err) => {

In this error scenario, the username provided and the access token used as password are not compatible. To mitigate this error, navigate to your Azure Cache for Redis resource in the Azure portal. Confirm that:

- In **RBAC Rules**, you've assigned the required role to your user/service principal identity.
- In **Data Access Configuration**, you've assigned the required role to your user/service principal identity.
- In **Advanced settings**, the **Azure AD access authorization** box is selected. If not, select it and select the **Save** button.

##### Permissions not granted / NOPERM Error

In this error scenario, the authentication was successful, but your registered user/service principal is not granted the RBAC permission to perform the action. To mitigate this error, navigate to your Azure Cache for Redis resource in the Azure portal. Confirm that:

- In **RBAC Rules**, you've assigned the appropriate role (Owner, Contributor, Reader) to your user/service principal identity.
- In **Data Access Configuration**, you've assigned the appropriate role (Owner, Contributor, Reader) to your user/service principal identity.
- In the event you are using a custom role, ensure the permissions granted under your custom role include the one required for your target action.
46 changes: 29 additions & 17 deletions sdk/identity/identity/samples/AzureCacheForRedis/node-redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

```
"dependencies": {
"@azure/identity": "^2.0.5",
"redis": "^4.1.0"
"@azure/identity": "^3.2.1",
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
"redis": "^4.6.6",
}
```
- Familiarity with the [node-redis](https://github.com/redis/node-redis) and [Azure Identity for JavaScript](https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest) client libraries is assumed.
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -50,9 +50,7 @@ dotenv.config();
async function main() {
// Construct a Token Credential from Identity library, e.g. ClientSecretCredential / ClientCertificateCredential / ManagedIdentityCredential, etc.
const credential = new DefaultAzureCredential();

// The scope will be changed for Azure AD Public Preview
const redisScope = "https://*.cacheinfra.windows.net:10225/appid/.default"
const redisScope = "acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default"
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved

// Fetch an Azure AD token to be used for authentication. This token will be used as the password.
let accessToken = await credential.getToken(redisScope);
Expand All @@ -63,6 +61,7 @@ async function main() {
username: process.env.REDIS_SERVICE_PRINCIPAL_NAME,
password: accessToken.token,
url: `redis://${process.env.REDIS_HOSTNAME}:6380`,
pingInterval:100000,
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
socket: {
tls: true,
keepAlive: 0
Expand Down Expand Up @@ -111,8 +110,7 @@ import * as dotenv from "dotenv";
dotenv.config();

async function returnPassword(credential: TokenCredential) {
// The scope will be changed for Azure AD Public Preview
const redisScope = "https://*.cacheinfra.windows.net:10225/appid/.default"
const redisScope = "acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default"
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved

// Fetch an Azure AD token to be used for authentication. This token will be used as the password.
return credential.getToken(redisScope);
Expand All @@ -127,6 +125,7 @@ async function main() {
username: process.env.REDIS_SERVICE_PRINCIPAL_NAME,
password: accessToken.token,
url: `redis://${process.env.REDIS_HOSTNAME}:6380`,
pingInterval:100000,
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
socket: {
tls: true,
keepAlive:0
Expand All @@ -150,6 +149,7 @@ async function main() {
username: process.env.REDIS_SERVICE_PRINCIPAL_NAME,
password: accessToken.token,
url: `redis://${process.env.REDIS_HOSTNAME}:6380`,
pingInterval:100000,
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
socket: {
tls: true,
keepAlive: 0
Expand Down Expand Up @@ -183,37 +183,49 @@ import * as dotenv from "dotenv";
dotenv.config();

async function returnPassword(credential: TokenCredential) {
// The scope will be changed for Azure AD Public Preview
const redisScope = "https://*.cacheinfra.windows.net:10225/appid/.default"
const redisScope = "acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default"
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved

// Fetch an Azure AD token to be used for authentication. This token will be used as the password.
return credential.getToken(redisScope);
}
function randomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

async function main() {
// Construct a Token Credential from Azure Identity library, e.g. ClientSecretCredential / ClientCertificateCredential / ManagedIdentityCredential, etc.
const credential = new DefaultAzureCredential();
let accessTokenCache: AccessToken | undefined = undefined;
let id;
let redisClient;

async function updateToken() {
accessTokenCache = await returnPassword(credential);
id = setTimeout(updateToken, ((accessTokenCache.expiresOnTimestamp- 120*1000)) - Date.now());
let randomTimestamp = randomNumber(120000,300000);
id = setTimeout(updateToken, ((accessTokenCache.expiresOnTimestamp- randomTimestamp)) - Date.now());
if(redisClient){
console.log("Auth called...")
await redisClient.auth({username: process.env.REDIS_SERVICE_PRINCIPAL_NAME,
password: accessToken.token})
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
}
}

await updateToken();
let accessToken: AccessToken | undefined = {...accessTokenCache};
// Create node-redis client and connect to the Azure Cache for Redis over the TLS port using the access token as password.
let redisClient = createClient({
redisClient = createClient({
username: process.env.REDIS_SERVICE_PRINCIPAL_NAME,
password: accessToken.token,
url: `redis://${process.env.REDIS_HOSTNAME}:6380`,
pingInterval:100000,
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
socket: {
tls: true,
keepAlive:0
},
});
await redisClient.connect();
await redisClient.connect();

for (let i = 0; i < 3; i++) {
try {
Expand All @@ -224,16 +236,16 @@ async function main() {
break;
} catch (e) {
console.log("error during redis get", e.toString());
if ((accessToken.expiresOnTimestamp <= Date.now())|| (redis.status === "end" || "close") ) {
await redis.disconnect();
if ((accessToken.expiresOnTimestamp <= Date.now())) {
await redisClient.disconnect();
accessToken = {...accessTokenCache};
redisClient = createClient({
username: process.env.REDIS_SERVICE_PRINCIPAL_NAME,
password: accessToken.token,
url: `redis://${process.env.REDIS_HOSTNAME}:6380`,
pingInterval:100000,
KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
socket: {
tls: true,
keepAlive: 0
},
});
}
Expand All @@ -254,12 +266,12 @@ main().catch((err) => {

In this error scenario, the username provided and the access token used as password are not compatible. To mitigate this error, navigate to your Azure Cache for Redis resource in the Azure portal. Confirm that:

* In **RBAC Rules**, you've assigned the required role to your user/service principal identity.
* In **Data Access Configuration**, you've assigned the required role to your user/service principal identity.
* In **Advanced settings**, the **Azure AD access authorization** box is selected. If not, select it and select the **Save** button.

##### Permissions not granted / NOPERM Error

In this error scenario, the authentication was successful, but your registered user/service principal is not granted the RBAC permission to perform the action. To mitigate this error, navigate to your Azure Cache for Redis resource in the Azure portal. Confirm that:

* In **RBAC Rules**, you've assigned the appropriate role (Owner, Contributor, Reader) to your user/service principal identity.
* In **Data Access Configuration**, you've assigned the appropriate role (Owner, Contributor, Reader) to your user/service principal identity.
* In the event you are using a custom role, ensure the permissions granted under your custom role include the one required for your target action.