Skip to content

Commit

Permalink
Merge pull request #7 from Azure-Samples/nodejs_msi_azure_vm
Browse files Browse the repository at this point in the history
Added MSI-NodeJS Azure VM Sample
  • Loading branch information
localden authored Dec 14, 2023
2 parents 6b4e448 + ea0af13 commit 0603983
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,6 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

# TypeScript build folder
dist/
1 change: 1 addition & 0 deletions src/nodejs/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
52 changes: 52 additions & 0 deletions src/nodejs/managed-identity/azure-vm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Managed Identity for Azure VM Sample

This sample demonstrates how to use [managed identity via the msal-node library](/lib/msal-node/docs/managed-identity.md) to retrieve tokens for a managed identity application running on an Azure VM.

## Note

- The functionality for this sample is in preview (alpha)
- This sample is written in TypeScript and was developed with Node version 18.17.0.

## Virtual Machine Setup

Follow [this guide](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/qs-configure-portal-windows-vm) to setup an Azure VM, as well as add a system assigned and user assigned managed identity to the Azure VM.

## Project Setup

In a terminal on the Azure VM, navigate to the directory where `package.json` resides. Then type:

```console
npm install
```

Before running the sample, the userAssignedClientId value in the managedIdentityIdParams object in index.ts needs to be replaced by the client id of the user assigned managed identity that was created in the previous step:

```typescript
const managedIdentityIdParams: ManagedIdentityIdParams = {
userAssignedClientId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
};
```

## Run the app on the Azure VM

Before running the sample (and everytime changes are made to the sample), the TypeScript will need to be compiled. In the same folder, type:

```console
npx tsc
```

This will compile the TypeScript into JavaScript, and put the compiled files in the dist folder.

The sample can now be run by typing:

```console
node dist/index.js
```

An npm script has been configured in package.json, which will run both of the above npx and node commands. To compile and start the sample in one command, type:

```console
npm run start:app
```

A token will be returned from the system assigned managed identity application as well as the user assigned managed identity application, and they will both be immediately displayed in the terminal.
61 changes: 61 additions & 0 deletions src/nodejs/managed-identity/azure-vm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { LoggerOptions } from "@azure/msal-common";
import {
AuthenticationResult,
LogLevel,
ManagedIdentityApplication,
ManagedIdentityConfiguration,
ManagedIdentityIdParams,
ManagedIdentityRequestParams,
NodeSystemOptions,
} from "@azure/msal-node";

const config: ManagedIdentityConfiguration = {
system: {
loggerOptions: {
logLevel: LogLevel.Verbose,
} as LoggerOptions,
} as NodeSystemOptions,
};

const systemAssignedManagedIdentityApplication: ManagedIdentityApplication =
new ManagedIdentityApplication(config);

const managedIdentityIdParams: ManagedIdentityIdParams = {
userAssignedClientId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
};
const userAssignedClientIdManagedIdentityApplication: ManagedIdentityApplication =
new ManagedIdentityApplication({
...config,
managedIdentityIdParams,
});

const managedIdentityRequestParams: ManagedIdentityRequestParams = {
resource: "https://management.azure.com",
};

// self executing anonymous function, needed for async/await usage
(async () => {
// system assigned
try {
const tokenResponse: AuthenticationResult =
await systemAssignedManagedIdentityApplication.acquireToken(
managedIdentityRequestParams
);
console.log(tokenResponse);
} catch (error) {
console.log(error);
throw error;
}

// user assigned client id
try {
const tokenResponse: AuthenticationResult =
await userAssignedClientIdManagedIdentityApplication.acquireToken(
managedIdentityRequestParams
);
console.log(tokenResponse);
} catch (error) {
console.log(error);
throw error;
}
})();
15 changes: 15 additions & 0 deletions src/nodejs/managed-identity/azure-vm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "managed-identity",
"version": "1.0.0",
"description": "Managed Identity for Azure VM",
"scripts": {
"build": "npx tsc",
"start:app": "npm run build && node build/index.js"
},
"dependencies": {
"@azure/msal-node": "2.3.0-alpha.0"
},
"devDependencies": {
"typescript": "^5.3.3"
}
}
24 changes: 24 additions & 0 deletions src/nodejs/managed-identity/azure-vm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */

/* Emit */
"outDir": "./dist", /* Specify an output folder for all emitted files. */

/* Interop Constraints */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */

/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */

/* Completeness */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}

0 comments on commit 0603983

Please sign in to comment.