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

Updated Managed Identity IMDS Sample #7205

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const getSecretFromKeyVault = (
keyVaultUri: string,
secretName: string
): Promise<string> => {
const customOptions: https.RequestOptions = {
const options: https.RequestOptions = {
headers: {
Authorization: `Bearer ${accessToken}`,
},
Expand All @@ -15,22 +15,41 @@ export const getSecretFromKeyVault = (
https
.get(
`${keyVaultUri}secrets/${secretName}?api-version=7.2`,
customOptions,
options,
(response) => {
const data: Buffer[] = [];
response.on("data", (chunk) => {
data.push(chunk);
});

response.on("end", () => {
// combine all received buffer streams into one buffer, and then into a string
const parsedData = Buffer.concat([...data]).toString();
resolve(JSON.parse(parsedData).value);
// combine all received buffer streams into one buffer, convert it to a string,
// then parse it as a JSON object
let parsedData;
try {
parsedData = JSON.parse(
Buffer.concat([...data]).toString()
);
} catch (error) {
return reject(
new Error(
"Unable to parse response from the Key Vault"
)
);
}

if (parsedData.error) {
return reject(
new Error(`${parsedData.error.message}`)
);
}

return resolve(parsedData.value);
});
}
)
.on("error", (error) => {
reject(new Error(`Error: ${error.message}`));
return reject(new Error(`${error.message}`));
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "Managed Identity for Imds",
"scripts": {
"build": "npx tsc",
"start:app": "npm run build && node build/index.js"
"start:app": "npm run build && node dist/index.js"
},
"dependencies": {
"@azure/msal-node": "^2.7.0",
"@azure/msal-node": "^2.11.0",
"dotenv": "^16.4.5"
},
"devDependencies": {
Expand Down
Loading