-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Azure-Samples/nodejs_msi_azure_vm
Added MSI-NodeJS Azure VM Sample
- Loading branch information
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,3 +396,6 @@ FodyWeavers.xsd | |
|
||
# JetBrains Rider | ||
*.sln.iml | ||
|
||
# TypeScript build folder | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. */ | ||
} | ||
} |