Skip to content

Commit 5e91713

Browse files
Merge pull request #265472 from diberry/diberry/storage-passwordless
Storage - Passwordless - JS
2 parents 54d1c11 + 31d79e1 commit 5e91713

File tree

1 file changed

+116
-2
lines changed

1 file changed

+116
-2
lines changed

articles/storage/common/multiple-identity-scenarios.md

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,45 @@ public class ExampleService {
228228
}
229229
```
230230

231+
#### [JavaScript](#tab/javascript)
232+
233+
1. Inside of your project, use [npm](https://docs.npmjs.com/) to add a reference to the `@azure/identity` package. This library contains all of the necessary entities to implement `DefaultAzureCredential`. Install any other [Azure SDK libraries](https://www.npmjs.com/search?q=%40azure) which are relevant to your app.
234+
235+
```bash
236+
npm install --save @azure/identity @azure/storage-blob @azure/keyvault-keys
237+
```
238+
239+
2. At the top of your `index.js` file, add the following `import` statements to import the necessary client classes for the services your app will connect to:
240+
241+
```javascript
242+
import { DefaultAzureCredential } from "@azure/identity";
243+
import { BlobServiceClient } from "@azure/storage-blob";
244+
import { KeyClient } from "@azure/keyvault-keys";
245+
```
246+
247+
3. Within the `index.js` file, create client objects for the Azure services your app will connect to. The following examples connect to Blob Storage and Key Vault using the corresponding SDK classes.
248+
249+
```javascript
250+
// Azure resource names
251+
const storageAccount = process.env.AZURE_STORAGE_ACCOUNT_NAME;
252+
const keyVaultName = process.env.AZURE_KEYVAULT_NAME;
253+
254+
// Create client for Blob Storage using managed identity
255+
const blobServiceClient = new BlobServiceClient(
256+
`https://${storageAccount}.blob.core.windows.net`,
257+
new DefaultAzureCredential()
258+
);
259+
260+
// Create client for Key Vault using managed identity
261+
const keyClient = new KeyClient(`https://${keyVaultName}.vault.azure.net`, new DefaultAzureCredential());
262+
263+
// Create a new key in Key Vault
264+
const result = await keyClient.createKey(keyVaultName, "RSA");
265+
```
266+
231267
---
232268
233-
When this application code runs locally, `DefaultAzureCredential` will search down a credential chain for the first available credentials. If the `Managed_Identity_Client_ID` is null locally, it will automatically use the credentials from your local Azure CLI or Visual Studio sign-in. You can read more about this process in the [Azure Identity library overview](/dotnet/api/overview/azure/Identity-readme#defaultazurecredential).
269+
When this application code runs locally, `DefaultAzureCredential` will search a credential chain for the first available credentials. If the `Managed_Identity_Client_ID` is null locally, it will automatically use the credentials from your local Azure CLI or Visual Studio sign-in. You can read more about this process in the [Azure Identity library overview](/dotnet/api/overview/azure/Identity-readme#defaultazurecredential).
234270
235271
When the application is deployed to Azure, `DefaultAzureCredential` will automatically retrieve the `Managed_Identity_Client_ID` variable from the app service environment. That value becomes available when a managed identity is associated with your app.
236272
@@ -251,7 +287,7 @@ To configure this setup in your code, make sure your application registers separ
251287
252288
```csharp
253289
// Get the first user-assigned managed identity ID to connect to shared storage
254-
var clientIDstorage = Environment.GetEnvironmentVariable("Managed_Identity_Client_ID_Storage");
290+
const clientIdStorage = Environment.GetEnvironmentVariable("Managed_Identity_Client_ID_Storage");
255291
256292
// First blob storage client that using a managed identity
257293
BlobServiceClient blobServiceClient = new BlobServiceClient(
@@ -475,6 +511,84 @@ public class ExampleService {
475511
}
476512
```
477513
514+
#### [JavaScript](#tab/javascript)
515+
516+
1. Inside of your project, use [npm](https://docs.npmjs.com/) to add a reference to the `@azure/identity` package. This library contains all of the necessary entities to implement `DefaultAzureCredential`. Install any other [Azure SDK libraries](https://www.npmjs.com/search?q=%40azure) which are relevant to your app.
517+
518+
```bash
519+
npm install --save @azure/identity @azure/storage-blob @azure/cosmos mssql
520+
```
521+
522+
2. At the top of your `index.js` file, add the following `import` statements to import the necessary client classes for the services your app will connect to:
523+
524+
```javascript
525+
import { DefaultAzureCredential } from "@azure/identity";
526+
import { BlobServiceClient } from "@azure/storage-blob";
527+
import { KeyClient } from "@azure/keyvault-keys";
528+
```
529+
530+
3. Within the `index.js` file, create client objects for the Azure services your app will connect to. The following examples connect to Blob Storage, Cosmos DB, and Azure SQL using the corresponding SDK classes.
531+
532+
```javascript
533+
// Get the first user-assigned managed identity ID to connect to shared storage
534+
const clientIdStorage = process.env.MANAGED_IDENTITY_CLIENT_ID_STORAGE;
535+
536+
// Storage account names
537+
const storageAccountName1 = process.env.AZURE_STORAGE_ACCOUNT_NAME_1;
538+
const storageAccountName2 = process.env.AZURE_STORAGE_ACCOUNT_NAME_2;
539+
540+
// First blob storage client that using a managed identity
541+
const blobServiceClient = new BlobServiceClient(
542+
`https://${storageAccountName1}.blob.core.windows.net`,
543+
new DefaultAzureCredential({
544+
managedIdentityClientId: clientIdStorage
545+
})
546+
);
547+
548+
// Second blob storage client that using a managed identity
549+
const blobServiceClient2 = new BlobServiceClient(
550+
`https://${storageAccountName2}.blob.core.windows.net`,
551+
new DefaultAzureCredential({
552+
managedIdentityClientId: clientIdStorage
553+
})
554+
);
555+
556+
// Get the second user-assigned managed identity ID to connect to shared databases
557+
const clientIdDatabases = process.env.MANAGED_IDENTITY_CLIENT_ID_DATABASES;
558+
559+
// Cosmos DB Account endpoint
560+
const cosmosDbAccountEndpoint = process.env.COSMOS_ENDPOINT;
561+
562+
// Create an Azure Cosmos DB client
563+
const client = new CosmosClient({
564+
endpoint: cosmosDbAccountEndpoint,
565+
credential: new DefaultAzureCredential({
566+
managedIdentityClientId: clientIdDatabases
567+
})
568+
});
569+
570+
// Open a connection to Azure SQL using a managed identity with mssql package
571+
// mssql reads the environment variables to get the managed identity
572+
const server = process.env.AZURE_SQL_SERVER;
573+
const database = process.env.AZURE_SQL_DATABASE;
574+
const port = parseInt(process.env.AZURE_SQL_PORT);
575+
const type = process.env.AZURE_SQL_AUTHENTICATIONTYPE;
576+
577+
const config = {
578+
server,
579+
port,
580+
database,
581+
authentication: {
582+
type // <---- Passwordless connection
583+
},
584+
options: {
585+
encrypt: true
586+
}
587+
};
588+
589+
await sql.connect(sqlConfig);
590+
```
591+
478592
---
479593
480594
You can also associate a user-assigned managed identity as well as a system-assigned managed identity to a resource simultaneously. This can be useful in scenarios where all of the apps require access to the same shared services, but one of the apps also has a very specific dependency on an additional service. Using a system-assigned identity also ensures that the identity tied to that specific app is deleted when the app is deleted, which can help keep your environment clean.

0 commit comments

Comments
 (0)