Skip to content

Commit 29c1524

Browse files
[Identity] Add logging for managed identity (Azure#33144)
Closes Azure#32468 --------- Co-authored-by: Scott Addie <[email protected]>
1 parent e394986 commit 29c1524

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

sdk/identity/identity/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 4.7.1 (Unreleased)
44

55
### Features Added
6+
- `ManagedIdentityCredential` will now log the configured user-assigned managed identity ID. [#33144](https://github.com/Azure/azure-sdk-for-js/pull/33144)
67

78
### Breaking Changes
89

@@ -15,7 +16,7 @@
1516

1617
### Features Added
1718

18-
- Added `subscription` property in `AzureCliCredentialOptions`. [#31451](https://github.com/Azure/azure-sdk-for-js/pull/31451).
19+
- Added `subscription` property in `AzureCliCredentialOptions`. [#31451](https://github.com/Azure/azure-sdk-for-js/pull/31451)
1920

2021
### Bugs Fixed
2122

sdk/identity/identity/src/credentials/managedIdentityCredential/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ export class ManagedIdentityCredential implements TokenCredential {
9696
this.objectId = (_options as ManagedIdentityCredentialObjectIdOptions)?.objectId;
9797

9898
// For JavaScript users.
99-
const providedIds = [this.clientId, this.resourceId, this.objectId].filter(Boolean);
99+
const providedIds = [
100+
{ key: "clientId", value: this.clientId },
101+
{ key: "resourceId", value: this.resourceId },
102+
{ key: "objectId", value: this.objectId },
103+
].filter((id) => id.value);
100104
if (providedIds.length > 1) {
101105
throw new Error(
102106
`ManagedIdentityCredential: only one of 'clientId', 'resourceId', or 'objectId' can be provided. Received values: ${JSON.stringify(
@@ -177,6 +181,14 @@ export class ManagedIdentityCredential implements TokenCredential {
177181
);
178182
}
179183
}
184+
185+
logger.info(`Using ${managedIdentitySource} managed identity.`);
186+
187+
// Check if either clientId, resourceId or objectId was provided and log the value used
188+
if (providedIds.length === 1) {
189+
const { key, value } = providedIds[0];
190+
logger.info(`${managedIdentitySource} with ${key}: ${value}`);
191+
}
180192
}
181193

182194
/**

sdk/identity/identity/test/internal/node/managedIdentityCredential/msalMsiProvider.spec.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
import type { AuthenticationResult, ManagedIdentityRequestParams } from "@azure/msal-node";
3+
import type {
4+
AuthenticationResult,
5+
ManagedIdentityRequestParams,
6+
ManagedIdentitySourceNames,
7+
} from "@azure/msal-node";
48
import { AuthError, ManagedIdentityApplication } from "@azure/msal-node";
59
import { ManagedIdentityCredential } from "../../../../src/credentials/managedIdentityCredential/index.js";
610
import { tokenExchangeMsi } from "../../../../src/credentials/managedIdentityCredential/tokenExchangeMsi.js";
@@ -11,6 +15,7 @@ import type { AccessToken, GetTokenOptions } from "@azure/core-auth";
1115
import { describe, it, assert, expect, vi, beforeEach, afterEach, type MockInstance } from "vitest";
1216
import type { IdentityClient } from "../../../../src/client/identityClient.js";
1317
import { serviceFabricErrorMessage } from "../../../../src/credentials/managedIdentityCredential/utils.js";
18+
import { logger } from "../../../../src/index.js";
1419

1520
describe("ManagedIdentityCredential (MSAL)", function () {
1621
let acquireTokenStub: MockInstance<
@@ -115,6 +120,54 @@ describe("ManagedIdentityCredential (MSAL)", function () {
115120
`ManagedIdentityCredential: ${serviceFabricErrorMessage}`,
116121
);
117122
});
123+
124+
it("logs authentication", async function () {
125+
const logSpy = vi.spyOn(logger, "info");
126+
vi.spyOn(ManagedIdentityApplication.prototype, "getManagedIdentitySource").mockReturnValue(
127+
"ServiceFabric",
128+
);
129+
new ManagedIdentityCredential();
130+
131+
expect(logSpy).toHaveBeenCalledTimes(1);
132+
expect(logSpy).toHaveBeenCalledWith(
133+
"ManagedIdentityCredential =>",
134+
"Using ServiceFabric managed identity.",
135+
);
136+
137+
logSpy.mockRestore();
138+
});
139+
});
140+
141+
describe("log user-assigned managed Identity", function () {
142+
const testCases = [
143+
{ idType: "clientId", idValue: "fakeClientID", source: "DefaultToImds" },
144+
{ idType: "objectId", idValue: "fakeObjectID", source: "Imds" },
145+
{ idType: "resourceId", idValue: "fakeResourceID", source: "AppService" },
146+
];
147+
148+
testCases.forEach(({ idType, idValue, source }) => {
149+
it(`logs ${idType}`, async function () {
150+
const logSpy = vi.spyOn(logger, "info");
151+
vi.spyOn(
152+
ManagedIdentityApplication.prototype,
153+
"getManagedIdentitySource",
154+
).mockReturnValue(source as ManagedIdentitySourceNames);
155+
156+
new ManagedIdentityCredential({ [idType]: idValue });
157+
158+
expect(logSpy).toHaveBeenCalledTimes(2);
159+
expect(logSpy).toHaveBeenCalledWith(
160+
"ManagedIdentityCredential =>",
161+
`Using ${source} managed identity.`,
162+
);
163+
expect(logSpy).toHaveBeenCalledWith(
164+
"ManagedIdentityCredential =>",
165+
`${source} with ${idType}: ${idValue}`,
166+
);
167+
168+
logSpy.mockRestore();
169+
});
170+
});
118171
});
119172
});
120173

0 commit comments

Comments
 (0)