@azure/identity 4.5.1-alpha.20241030.1 → 4.5.1-alpha.20241111.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. package/dist/index.js +67 -121
  2. package/dist/index.js.map +1 -1
  3. package/dist-esm/src/credentials/managedIdentityCredential/imdsMsi.js +12 -34
  4. package/dist-esm/src/credentials/managedIdentityCredential/imdsMsi.js.map +1 -1
  5. package/dist-esm/src/credentials/managedIdentityCredential/index.browser.js.map +1 -1
  6. package/dist-esm/src/credentials/managedIdentityCredential/index.js +188 -8
  7. package/dist-esm/src/credentials/managedIdentityCredential/index.js.map +1 -1
  8. package/dist-esm/src/credentials/managedIdentityCredential/models.js.map +1 -1
  9. package/dist-esm/src/credentials/managedIdentityCredential/tokenExchangeMsi.js +4 -1
  10. package/dist-esm/src/credentials/managedIdentityCredential/tokenExchangeMsi.js.map +1 -1
  11. package/dist-esm/src/credentials/managedIdentityCredential/utils.js +1 -1
  12. package/dist-esm/src/credentials/managedIdentityCredential/utils.js.map +1 -1
  13. package/package.json +3 -4
  14. package/types/identity.d.ts +11 -1
  15. package/dist-esm/src/credentials/managedIdentityCredential/appServiceMsi2017.js +0 -71
  16. package/dist-esm/src/credentials/managedIdentityCredential/appServiceMsi2017.js.map +0 -1
  17. package/dist-esm/src/credentials/managedIdentityCredential/appServiceMsi2019.js +0 -71
  18. package/dist-esm/src/credentials/managedIdentityCredential/appServiceMsi2019.js.map +0 -1
  19. package/dist-esm/src/credentials/managedIdentityCredential/arcMsi.js +0 -140
  20. package/dist-esm/src/credentials/managedIdentityCredential/arcMsi.js.map +0 -1
  21. package/dist-esm/src/credentials/managedIdentityCredential/cloudShellMsi.js +0 -75
  22. package/dist-esm/src/credentials/managedIdentityCredential/cloudShellMsi.js.map +0 -1
  23. package/dist-esm/src/credentials/managedIdentityCredential/constants.js +0 -9
  24. package/dist-esm/src/credentials/managedIdentityCredential/constants.js.map +0 -1
  25. package/dist-esm/src/credentials/managedIdentityCredential/fabricMsi.js +0 -95
  26. package/dist-esm/src/credentials/managedIdentityCredential/fabricMsi.js.map +0 -1
  27. package/dist-esm/src/credentials/managedIdentityCredential/legacyMsiProvider.js +0 -309
  28. package/dist-esm/src/credentials/managedIdentityCredential/legacyMsiProvider.js.map +0 -1
  29. package/dist-esm/src/credentials/managedIdentityCredential/msalMsiProvider.js +0 -212
  30. package/dist-esm/src/credentials/managedIdentityCredential/msalMsiProvider.js.map +0 -1
@@ -1,309 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
- import { ConfidentialClientApplication } from "@azure/msal-node";
4
- import { AuthenticationError, AuthenticationRequiredError, CredentialUnavailableError, } from "../../errors";
5
- import { cloudShellMsi } from "./cloudShellMsi";
6
- import { credentialLogger, formatError, formatSuccess } from "../../util/logging";
7
- import { DeveloperSignOnClientId } from "../../constants";
8
- import { IdentityClient } from "../../client/identityClient";
9
- import { appServiceMsi2017 } from "./appServiceMsi2017";
10
- import { appServiceMsi2019 } from "./appServiceMsi2019";
11
- import { arcMsi } from "./arcMsi";
12
- import { fabricMsi } from "./fabricMsi";
13
- import { getLogLevel } from "@azure/logger";
14
- import { getMSALLogLevel } from "../../msal/utils";
15
- import { imdsMsi } from "./imdsMsi";
16
- import { tokenExchangeMsi } from "./tokenExchangeMsi";
17
- import { tracingClient } from "../../util/tracing";
18
- const logger = credentialLogger("ManagedIdentityCredential");
19
- export class LegacyMsiProvider {
20
- constructor(clientIdOrOptions, options) {
21
- var _a, _b;
22
- this.isEndpointUnavailable = null;
23
- this.isAppTokenProviderInitialized = false;
24
- this.msiRetryConfig = {
25
- maxRetries: 5,
26
- startDelayInMs: 800,
27
- intervalIncrement: 2,
28
- };
29
- let _options;
30
- if (typeof clientIdOrOptions === "string") {
31
- this.clientId = clientIdOrOptions;
32
- _options = options;
33
- }
34
- else {
35
- this.clientId = clientIdOrOptions === null || clientIdOrOptions === void 0 ? void 0 : clientIdOrOptions.clientId;
36
- _options = clientIdOrOptions;
37
- }
38
- this.resourceId = _options === null || _options === void 0 ? void 0 : _options.resourceId;
39
- // For JavaScript users.
40
- if (this.clientId && this.resourceId) {
41
- throw new Error(`ManagedIdentityCredential - Client Id and Resource Id can't be provided at the same time.`);
42
- }
43
- if (((_a = _options === null || _options === void 0 ? void 0 : _options.retryOptions) === null || _a === void 0 ? void 0 : _a.maxRetries) !== undefined) {
44
- this.msiRetryConfig.maxRetries = _options.retryOptions.maxRetries;
45
- }
46
- this.identityClient = new IdentityClient(_options);
47
- this.isAvailableIdentityClient = new IdentityClient(Object.assign(Object.assign({}, _options), { retryOptions: {
48
- maxRetries: 0,
49
- } }));
50
- /** authority host validation and metadata discovery to be skipped in managed identity
51
- * since this wasn't done previously before adding token cache support
52
- */
53
- this.confidentialApp = new ConfidentialClientApplication({
54
- auth: {
55
- authority: "https://login.microsoftonline.com/managed_identity",
56
- clientId: (_b = this.clientId) !== null && _b !== void 0 ? _b : DeveloperSignOnClientId,
57
- clientSecret: "dummy-secret",
58
- cloudDiscoveryMetadata: '{"tenant_discovery_endpoint":"https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration","api-version":"1.1","metadata":[{"preferred_network":"login.microsoftonline.com","preferred_cache":"login.windows.net","aliases":["login.microsoftonline.com","login.windows.net","login.microsoft.com","sts.windows.net"]},{"preferred_network":"login.partner.microsoftonline.cn","preferred_cache":"login.partner.microsoftonline.cn","aliases":["login.partner.microsoftonline.cn","login.chinacloudapi.cn"]},{"preferred_network":"login.microsoftonline.de","preferred_cache":"login.microsoftonline.de","aliases":["login.microsoftonline.de"]},{"preferred_network":"login.microsoftonline.us","preferred_cache":"login.microsoftonline.us","aliases":["login.microsoftonline.us","login.usgovcloudapi.net"]},{"preferred_network":"login-us.microsoftonline.com","preferred_cache":"login-us.microsoftonline.com","aliases":["login-us.microsoftonline.com"]}]}',
59
- authorityMetadata: '{"token_endpoint":"https://login.microsoftonline.com/common/oauth2/v2.0/token","token_endpoint_auth_methods_supported":["client_secret_post","private_key_jwt","client_secret_basic"],"jwks_uri":"https://login.microsoftonline.com/common/discovery/v2.0/keys","response_modes_supported":["query","fragment","form_post"],"subject_types_supported":["pairwise"],"id_token_signing_alg_values_supported":["RS256"],"response_types_supported":["code","id_token","code id_token","id_token token"],"scopes_supported":["openid","profile","email","offline_access"],"issuer":"https://login.microsoftonline.com/{tenantid}/v2.0","request_uri_parameter_supported":false,"userinfo_endpoint":"https://graph.microsoft.com/oidc/userinfo","authorization_endpoint":"https://login.microsoftonline.com/common/oauth2/v2.0/authorize","device_authorization_endpoint":"https://login.microsoftonline.com/common/oauth2/v2.0/devicecode","http_logout_supported":true,"frontchannel_logout_supported":true,"end_session_endpoint":"https://login.microsoftonline.com/common/oauth2/v2.0/logout","claims_supported":["sub","iss","cloud_instance_name","cloud_instance_host_name","cloud_graph_host_name","msgraph_host","aud","exp","iat","auth_time","acr","nonce","preferred_username","name","tid","ver","at_hash","c_hash","email"],"kerberos_endpoint":"https://login.microsoftonline.com/common/kerberos","tenant_region_scope":null,"cloud_instance_name":"microsoftonline.com","cloud_graph_host_name":"graph.windows.net","msgraph_host":"graph.microsoft.com","rbac_url":"https://pas.windows.net"}',
60
- clientCapabilities: [],
61
- },
62
- system: {
63
- loggerOptions: {
64
- logLevel: getMSALLogLevel(getLogLevel()),
65
- },
66
- },
67
- });
68
- }
69
- async cachedAvailableMSI(scopes, getTokenOptions) {
70
- if (this.cachedMSI) {
71
- return this.cachedMSI;
72
- }
73
- const MSIs = [
74
- arcMsi,
75
- fabricMsi,
76
- appServiceMsi2019,
77
- appServiceMsi2017,
78
- cloudShellMsi,
79
- tokenExchangeMsi,
80
- imdsMsi,
81
- ];
82
- for (const msi of MSIs) {
83
- if (await msi.isAvailable({
84
- scopes,
85
- identityClient: this.isAvailableIdentityClient,
86
- clientId: this.clientId,
87
- resourceId: this.resourceId,
88
- getTokenOptions,
89
- })) {
90
- this.cachedMSI = msi;
91
- return msi;
92
- }
93
- }
94
- throw new CredentialUnavailableError(`ManagedIdentityCredential - No MSI credential available`);
95
- }
96
- async authenticateManagedIdentity(scopes, getTokenOptions) {
97
- const { span, updatedOptions } = tracingClient.startSpan(`ManagedIdentityCredential.authenticateManagedIdentity`, getTokenOptions);
98
- try {
99
- // Determining the available MSI, and avoiding checking for other MSIs while the program is running.
100
- const availableMSI = await this.cachedAvailableMSI(scopes, updatedOptions);
101
- return availableMSI.getToken({
102
- identityClient: this.identityClient,
103
- scopes,
104
- clientId: this.clientId,
105
- resourceId: this.resourceId,
106
- retryConfig: this.msiRetryConfig,
107
- }, updatedOptions);
108
- }
109
- catch (err) {
110
- span.setStatus({
111
- status: "error",
112
- error: err,
113
- });
114
- throw err;
115
- }
116
- finally {
117
- span.end();
118
- }
119
- }
120
- /**
121
- * Authenticates with Microsoft Entra ID and returns an access token if successful.
122
- * If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.
123
- * If an unexpected error occurs, an {@link AuthenticationError} will be thrown with the details of the failure.
124
- *
125
- * @param scopes - The list of scopes for which the token will have access.
126
- * @param options - The options used to configure any requests this
127
- * TokenCredential implementation might make.
128
- */
129
- async getToken(scopes, options) {
130
- let result = null;
131
- const { span, updatedOptions } = tracingClient.startSpan(`ManagedIdentityCredential.getToken`, options);
132
- try {
133
- // isEndpointAvailable can be true, false, or null,
134
- // If it's null, it means we don't yet know whether
135
- // the endpoint is available and need to check for it.
136
- if (this.isEndpointUnavailable !== true) {
137
- const availableMSI = await this.cachedAvailableMSI(scopes, updatedOptions);
138
- if (availableMSI.name === "tokenExchangeMsi") {
139
- result = await this.authenticateManagedIdentity(scopes, updatedOptions);
140
- }
141
- else {
142
- const appTokenParameters = {
143
- correlationId: this.identityClient.getCorrelationId(),
144
- tenantId: (options === null || options === void 0 ? void 0 : options.tenantId) || "managed_identity",
145
- scopes: Array.isArray(scopes) ? scopes : [scopes],
146
- claims: options === null || options === void 0 ? void 0 : options.claims,
147
- };
148
- // Added a check to see if SetAppTokenProvider was already defined.
149
- this.initializeSetAppTokenProvider();
150
- const authenticationResult = await this.confidentialApp.acquireTokenByClientCredential(Object.assign({}, appTokenParameters));
151
- result = this.handleResult(scopes, authenticationResult || undefined);
152
- }
153
- if (result === null) {
154
- // If authenticateManagedIdentity returns null,
155
- // it means no MSI endpoints are available.
156
- // If so, we avoid trying to reach to them in future requests.
157
- this.isEndpointUnavailable = true;
158
- // It also means that the endpoint answered with either 200 or 201 (see the sendTokenRequest method),
159
- // yet we had no access token. For this reason, we'll throw once with a specific message:
160
- const error = new CredentialUnavailableError("The managed identity endpoint was reached, yet no tokens were received.");
161
- logger.getToken.info(formatError(scopes, error));
162
- throw error;
163
- }
164
- // Since `authenticateManagedIdentity` didn't throw, and the result was not null,
165
- // We will assume that this endpoint is reachable from this point forward,
166
- // and avoid pinging again to it.
167
- this.isEndpointUnavailable = false;
168
- }
169
- else {
170
- // We've previously determined that the endpoint was unavailable,
171
- // either because it was unreachable or permanently unable to authenticate.
172
- const error = new CredentialUnavailableError("The managed identity endpoint is not currently available");
173
- logger.getToken.info(formatError(scopes, error));
174
- throw error;
175
- }
176
- logger.getToken.info(formatSuccess(scopes));
177
- return result;
178
- }
179
- catch (err) {
180
- // CredentialUnavailable errors are expected to reach here.
181
- // We intend them to bubble up, so that DefaultAzureCredential can catch them.
182
- if (err.name === "AuthenticationRequiredError") {
183
- throw err;
184
- }
185
- // Expected errors to reach this point:
186
- // - Errors coming from a method unexpectedly breaking.
187
- // - When identityClient.sendTokenRequest throws, in which case
188
- // if the status code was 400, it means that the endpoint is working,
189
- // but no identity is available.
190
- span.setStatus({
191
- status: "error",
192
- error: err,
193
- });
194
- // If either the network is unreachable,
195
- // we can safely assume the credential is unavailable.
196
- if (err.code === "ENETUNREACH") {
197
- const error = new CredentialUnavailableError(`ManagedIdentityCredential: Unavailable. Network unreachable. Message: ${err.message}`);
198
- logger.getToken.info(formatError(scopes, error));
199
- throw error;
200
- }
201
- // If either the host was unreachable,
202
- // we can safely assume the credential is unavailable.
203
- if (err.code === "EHOSTUNREACH") {
204
- const error = new CredentialUnavailableError(`ManagedIdentityCredential: Unavailable. No managed identity endpoint found. Message: ${err.message}`);
205
- logger.getToken.info(formatError(scopes, error));
206
- throw error;
207
- }
208
- // If err.statusCode has a value of 400, it comes from sendTokenRequest,
209
- // and it means that the endpoint is working, but that no identity is available.
210
- if (err.statusCode === 400) {
211
- throw new CredentialUnavailableError(`ManagedIdentityCredential: The managed identity endpoint is indicating there's no available identity. Message: ${err.message}`);
212
- }
213
- // This is a special case for Docker Desktop which responds with a 403 with a message that contains "A socket operation was attempted to an unreachable network" or "A socket operation was attempted to an unreachable host"
214
- // rather than just timing out, as expected.
215
- if (err.statusCode === 403 || err.code === 403) {
216
- if (err.message.includes("unreachable")) {
217
- const error = new CredentialUnavailableError(`ManagedIdentityCredential: Unavailable. Network unreachable. Message: ${err.message}`);
218
- logger.getToken.info(formatError(scopes, error));
219
- throw error;
220
- }
221
- }
222
- // If the error has no status code, we can assume there was no available identity.
223
- // This will throw silently during any ChainedTokenCredential.
224
- if (err.statusCode === undefined) {
225
- throw new CredentialUnavailableError(`ManagedIdentityCredential: Authentication failed. Message ${err.message}`);
226
- }
227
- // Any other error should break the chain.
228
- throw new AuthenticationError(err.statusCode, {
229
- error: `ManagedIdentityCredential authentication failed.`,
230
- error_description: err.message,
231
- });
232
- }
233
- finally {
234
- // Finally is always called, both if we return and if we throw in the above try/catch.
235
- span.end();
236
- }
237
- }
238
- /**
239
- * Handles the MSAL authentication result.
240
- * If the result has an account, we update the local account reference.
241
- * If the token received is invalid, an error will be thrown depending on what's missing.
242
- */
243
- handleResult(scopes, result, getTokenOptions) {
244
- var _a;
245
- this.ensureValidMsalToken(scopes, result, getTokenOptions);
246
- logger.getToken.info(formatSuccess(scopes));
247
- return {
248
- token: result.accessToken,
249
- expiresOnTimestamp: result.expiresOn.getTime(),
250
- refreshAfterTimestamp: (_a = result.refreshOn) === null || _a === void 0 ? void 0 : _a.getTime(),
251
- tokenType: "Bearer",
252
- };
253
- }
254
- /**
255
- * Ensures the validity of the MSAL token
256
- */
257
- ensureValidMsalToken(scopes, msalToken, getTokenOptions) {
258
- const error = (message) => {
259
- logger.getToken.info(message);
260
- return new AuthenticationRequiredError({
261
- scopes: Array.isArray(scopes) ? scopes : [scopes],
262
- getTokenOptions,
263
- message,
264
- });
265
- };
266
- if (!msalToken) {
267
- throw error("No response");
268
- }
269
- if (!msalToken.expiresOn) {
270
- throw error(`Response had no "expiresOn" property.`);
271
- }
272
- if (!msalToken.accessToken) {
273
- throw error(`Response had no "accessToken" property.`);
274
- }
275
- }
276
- initializeSetAppTokenProvider() {
277
- if (!this.isAppTokenProviderInitialized) {
278
- this.confidentialApp.SetAppTokenProvider(async (appTokenProviderParameters) => {
279
- logger.info(`SetAppTokenProvider invoked with parameters- ${JSON.stringify(appTokenProviderParameters)}`);
280
- const getTokenOptions = Object.assign({}, appTokenProviderParameters);
281
- logger.info(`authenticateManagedIdentity invoked with scopes- ${JSON.stringify(appTokenProviderParameters.scopes)} and getTokenOptions - ${JSON.stringify(getTokenOptions)}`);
282
- const resultToken = await this.authenticateManagedIdentity(appTokenProviderParameters.scopes, getTokenOptions);
283
- if (resultToken) {
284
- logger.info(`SetAppTokenProvider will save the token in cache`);
285
- const expiresInSeconds = (resultToken === null || resultToken === void 0 ? void 0 : resultToken.expiresOnTimestamp)
286
- ? Math.floor((resultToken.expiresOnTimestamp - Date.now()) / 1000)
287
- : 0;
288
- const refreshInSeconds = (resultToken === null || resultToken === void 0 ? void 0 : resultToken.refreshAfterTimestamp)
289
- ? Math.floor((resultToken.refreshAfterTimestamp - Date.now()) / 1000)
290
- : 0;
291
- return {
292
- accessToken: resultToken === null || resultToken === void 0 ? void 0 : resultToken.token,
293
- expiresInSeconds,
294
- refreshInSeconds,
295
- };
296
- }
297
- else {
298
- logger.info(`SetAppTokenProvider token has "no_access_token_returned" as the saved token`);
299
- return {
300
- accessToken: "no_access_token_returned",
301
- expiresInSeconds: 0,
302
- };
303
- }
304
- });
305
- this.isAppTokenProviderInitialized = true;
306
- }
307
- }
308
- }
309
- //# sourceMappingURL=legacyMsiProvider.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"legacyMsiProvider.js","sourceRoot":"","sources":["../../../../src/credentials/managedIdentityCredential/legacyMsiProvider.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,0BAA0B,GAC3B,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAElF,OAAO,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,MAAM,MAAM,GAAG,gBAAgB,CAAC,2BAA2B,CAAC,CAAC;AA2B7D,MAAM,OAAO,iBAAiB;IAc5B,YACE,iBAA6D,EAC7D,OAAgC;;QAZ1B,0BAAqB,GAAmB,IAAI,CAAC;QAG7C,kCAA6B,GAAY,KAAK,CAAC;QAC/C,mBAAc,GAAoC;YACxD,U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G,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,kBAAkB;wBACtD,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,kBAAkB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC;wBAClE,CAAC,CAAC,CAAC,CAAC;oBACN,MAAM,gBAAgB,GAAG,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,qBAAqB;wBACzD,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,qBAAqB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC;wBACrE,CAAC,CAAC,CAAC,CAAC;oBACN,OAAO;wBACL,WAAW,EAAE,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,KAAK;wBAC/B,gBAAgB;wBAChB,gBAAgB;qBACjB,CAAC;gBACJ,CAAC;qBAAM,CAAC;oBACN,MAAM,CAAC,IAAI,CACT,6EAA6E,CAC9E,CAAC;oBACF,OAAO;wBACL,WAAW,EAAE,0BAA0B;wBACvC,gBAAgB,EAAE,CAAC;qBACpB,CAAC;gBACJ,CAAC;YACH,CAAC,CAAC,CAAC;YACH,IAAI,CAAC,6BAA6B,GAAG,IAAI,CAAC;QAC5C,CAAC;IACH,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AccessToken, GetTokenOptions } from \"@azure/core-auth\";\nimport type { AppTokenProviderParameters } from \"@azure/msal-node\";\nimport { ConfidentialClientApplication } from \"@azure/msal-node\";\nimport {\n AuthenticationError,\n AuthenticationRequiredError,\n CredentialUnavailableError,\n} from \"../../errors\";\nimport type { MSI, MSIConfiguration, MSIToken } from \"./models\";\nimport type { MsalResult, MsalToken, ValidMsalToken } from \"../../msal/types\";\nimport { cloudShellMsi } from \"./cloudShellMsi\";\nimport { credentialLogger, formatError, formatSuccess } from \"../../util/logging\";\n\nimport { DeveloperSignOnClientId } from \"../../constants\";\nimport { IdentityClient } from \"../../client/identityClient\";\nimport type { TokenCredentialOptions } from \"../../tokenCredentialOptions\";\nimport { appServiceMsi2017 } from \"./appServiceMsi2017\";\nimport { appServiceMsi2019 } from \"./appServiceMsi2019\";\nimport { arcMsi } from \"./arcMsi\";\nimport { fabricMsi } from \"./fabricMsi\";\nimport { getLogLevel } from \"@azure/logger\";\nimport { getMSALLogLevel } from \"../../msal/utils\";\nimport { imdsMsi } from \"./imdsMsi\";\nimport { tokenExchangeMsi } from \"./tokenExchangeMsi\";\nimport { tracingClient } from \"../../util/tracing\";\n\nconst logger = credentialLogger(\"ManagedIdentityCredential\");\n\n// As part of the migration of Managed Identity to MSAL, this legacy provider captures the existing behavior\n// ported over from the ManagedIdentityCredential verbatim. This is to ensure that the existing behavior\n// is maintained while the new implementation is being tested and validated.\n// https://github.com/Azure/azure-sdk-for-js/issues/30189 tracks deleting this provider once it is no longer needed.\n\n/**\n * Options to send on the {@link ManagedIdentityCredential} constructor.\n * Since this is an internal implementation, uses a looser interface than the public one.\n */\ninterface ManagedIdentityCredentialOptions extends TokenCredentialOptions {\n /**\n * The client ID of the user - assigned identity, or app registration(when working with AKS pod - identity).\n */\n clientId?: string;\n\n /**\n * Allows specifying a custom resource Id.\n * In scenarios such as when user assigned identities are created using an ARM template,\n * where the resource Id of the identity is known but the client Id can't be known ahead of time,\n * this parameter allows programs to use these user assigned identities\n * without having to first determine the client Id of the created identity.\n */\n resourceId?: string;\n}\n\nexport class LegacyMsiProvider {\n private identityClient: IdentityClient;\n private clientId: string | undefined;\n private resourceId: string | undefined;\n private isEndpointUnavailable: boolean | null = null;\n private isAvailableIdentityClient: IdentityClient;\n private confidentialApp: ConfidentialClientApplication;\n private isAppTokenProviderInitialized: boolean = false;\n private msiRetryConfig: MSIConfiguration[\"retryConfig\"] = {\n maxRetries: 5,\n startDelayInMs: 800,\n intervalIncrement: 2,\n };\n\n constructor(\n clientIdOrOptions?: string | ManagedIdentityCredentialOptions,\n options?: TokenCredentialOptions,\n ) {\n let _options: TokenCredentialOptions | undefined;\n if (typeof clientIdOrOptions === \"string\") {\n this.clientId = clientIdOrOptions;\n _options = options;\n } else {\n this.clientId = (clientIdOrOptions as ManagedIdentityCredentialOptions)?.clientId;\n _options = clientIdOrOptions;\n }\n this.resourceId = (_options as ManagedIdentityCredentialOptions)?.resourceId;\n // For JavaScript users.\n if (this.clientId && this.resourceId) {\n throw new Error(\n `ManagedIdentityCredential - Client Id and Resource Id can't be provided at the same time.`,\n );\n }\n if (_options?.retryOptions?.maxRetries !== undefined) {\n this.msiRetryConfig.maxRetries = _options.retryOptions.maxRetries;\n }\n this.identityClient = new IdentityClient(_options);\n this.isAvailableIdentityClient = new IdentityClient({\n ..._options,\n retryOptions: {\n maxRetries: 0,\n },\n });\n\n /** authority host validation and metadata discovery to be skipped in managed identity\n * since this wasn't done previously before adding token cache support\n */\n this.confidentialApp = new ConfidentialClientApplication({\n auth: {\n authority: \"https://login.microsoftonline.com/managed_identity\",\n clientId: this.clientId ?? DeveloperSignOnClientId,\n clientSecret: \"dummy-secret\",\n cloudDiscoveryMetadata:\n '{\"tenant_discovery_endpoint\":\"https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration\",\"api-version\":\"1.1\",\"metadata\":[{\"preferred_network\":\"login.microsoftonline.com\",\"preferred_cache\":\"login.windows.net\",\"aliases\":[\"login.microsoftonline.com\",\"login.windows.net\",\"login.microsoft.com\",\"sts.windows.net\"]},{\"preferred_network\":\"login.partner.microsoftonline.cn\",\"preferred_cache\":\"login.partner.microsoftonline.cn\",\"aliases\":[\"login.partner.microsoftonline.cn\",\"login.chinacloudapi.cn\"]},{\"preferred_network\":\"login.microsoftonline.de\",\"preferred_cache\":\"login.microsoftonline.de\",\"aliases\":[\"login.microsoftonline.de\"]},{\"preferred_network\":\"login.microsoftonline.us\",\"preferred_cache\":\"login.microsoftonline.us\",\"aliases\":[\"login.microsoftonline.us\",\"login.usgovcloudapi.net\"]},{\"preferred_network\":\"login-us.microsoftonline.com\",\"preferred_cache\":\"login-us.microsoftonline.com\",\"aliases\":[\"login-us.microsoftonline.com\"]}]}',\n authorityMetadata:\n '{\"token_endpoint\":\"https://login.microsoftonline.com/common/oauth2/v2.0/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_post\",\"private_key_jwt\",\"client_secret_basic\"],\"jwks_uri\":\"https://login.microsoftonline.com/common/discovery/v2.0/keys\",\"response_modes_supported\":[\"query\",\"fragment\",\"form_post\"],\"subject_types_supported\":[\"pairwise\"],\"id_token_signing_alg_values_supported\":[\"RS256\"],\"response_types_supported\":[\"code\",\"id_token\",\"code id_token\",\"id_token token\"],\"scopes_supported\":[\"openid\",\"profile\",\"email\",\"offline_access\"],\"issuer\":\"https://login.microsoftonline.com/{tenantid}/v2.0\",\"request_uri_parameter_supported\":false,\"userinfo_endpoint\":\"https://graph.microsoft.com/oidc/userinfo\",\"authorization_endpoint\":\"https://login.microsoftonline.com/common/oauth2/v2.0/authorize\",\"device_authorization_endpoint\":\"https://login.microsoftonline.com/common/oauth2/v2.0/devicecode\",\"http_logout_supported\":true,\"frontchannel_logout_supported\":true,\"end_session_endpoint\":\"https://login.microsoftonline.com/common/oauth2/v2.0/logout\",\"claims_supported\":[\"sub\",\"iss\",\"cloud_instance_name\",\"cloud_instance_host_name\",\"cloud_graph_host_name\",\"msgraph_host\",\"aud\",\"exp\",\"iat\",\"auth_time\",\"acr\",\"nonce\",\"preferred_username\",\"name\",\"tid\",\"ver\",\"at_hash\",\"c_hash\",\"email\"],\"kerberos_endpoint\":\"https://login.microsoftonline.com/common/kerberos\",\"tenant_region_scope\":null,\"cloud_instance_name\":\"microsoftonline.com\",\"cloud_graph_host_name\":\"graph.windows.net\",\"msgraph_host\":\"graph.microsoft.com\",\"rbac_url\":\"https://pas.windows.net\"}',\n clientCapabilities: [],\n },\n system: {\n loggerOptions: {\n logLevel: getMSALLogLevel(getLogLevel()),\n },\n },\n });\n }\n\n private cachedMSI: MSI | undefined;\n\n private async cachedAvailableMSI(\n scopes: string | string[],\n getTokenOptions?: GetTokenOptions,\n ): Promise<MSI> {\n if (this.cachedMSI) {\n return this.cachedMSI;\n }\n\n const MSIs = [\n arcMsi,\n fabricMsi,\n appServiceMsi2019,\n appServiceMsi2017,\n cloudShellMsi,\n tokenExchangeMsi,\n imdsMsi,\n ];\n\n for (const msi of MSIs) {\n if (\n await msi.isAvailable({\n scopes,\n identityClient: this.isAvailableIdentityClient,\n clientId: this.clientId,\n resourceId: this.resourceId,\n getTokenOptions,\n })\n ) {\n this.cachedMSI = msi;\n return msi;\n }\n }\n\n throw new CredentialUnavailableError(`ManagedIdentityCredential - No MSI credential available`);\n }\n\n private async authenticateManagedIdentity(\n scopes: string | string[],\n getTokenOptions?: GetTokenOptions,\n ): Promise<MSIToken | null> {\n const { span, updatedOptions } = tracingClient.startSpan(\n `ManagedIdentityCredential.authenticateManagedIdentity`,\n getTokenOptions,\n );\n\n try {\n // Determining the available MSI, and avoiding checking for other MSIs while the program is running.\n const availableMSI = await this.cachedAvailableMSI(scopes, updatedOptions);\n return availableMSI.getToken(\n {\n identityClient: this.identityClient,\n scopes,\n clientId: this.clientId,\n resourceId: this.resourceId,\n retryConfig: this.msiRetryConfig,\n },\n updatedOptions,\n );\n } catch (err: any) {\n span.setStatus({\n status: \"error\",\n error: err,\n });\n throw err;\n } finally {\n span.end();\n }\n }\n\n /**\n * Authenticates with Microsoft Entra ID and returns an access token if successful.\n * If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.\n * If an unexpected error occurs, an {@link AuthenticationError} will be thrown with the details of the failure.\n *\n * @param scopes - The list of scopes for which the token will have access.\n * @param options - The options used to configure any requests this\n * TokenCredential implementation might make.\n */\n public async getToken(\n scopes: string | string[],\n options?: GetTokenOptions,\n ): Promise<AccessToken> {\n let result: AccessToken | null = null;\n const { span, updatedOptions } = tracingClient.startSpan(\n `ManagedIdentityCredential.getToken`,\n options,\n );\n try {\n // isEndpointAvailable can be true, false, or null,\n // If it's null, it means we don't yet know whether\n // the endpoint is available and need to check for it.\n if (this.isEndpointUnavailable !== true) {\n const availableMSI = await this.cachedAvailableMSI(scopes, updatedOptions);\n if (availableMSI.name === \"tokenExchangeMsi\") {\n result = await this.authenticateManagedIdentity(scopes, updatedOptions);\n } else {\n const appTokenParameters: AppTokenProviderParameters = {\n correlationId: this.identityClient.getCorrelationId(),\n tenantId: options?.tenantId || \"managed_identity\",\n scopes: Array.isArray(scopes) ? scopes : [scopes],\n claims: options?.claims,\n };\n\n // Added a check to see if SetAppTokenProvider was already defined.\n this.initializeSetAppTokenProvider();\n const authenticationResult = await this.confidentialApp.acquireTokenByClientCredential({\n ...appTokenParameters,\n });\n result = this.handleResult(scopes, authenticationResult || undefined);\n }\n if (result === null) {\n // If authenticateManagedIdentity returns null,\n // it means no MSI endpoints are available.\n // If so, we avoid trying to reach to them in future requests.\n this.isEndpointUnavailable = true;\n\n // It also means that the endpoint answered with either 200 or 201 (see the sendTokenRequest method),\n // yet we had no access token. For this reason, we'll throw once with a specific message:\n const error = new CredentialUnavailableError(\n \"The managed identity endpoint was reached, yet no tokens were received.\",\n );\n logger.getToken.info(formatError(scopes, error));\n throw error;\n }\n\n // Since `authenticateManagedIdentity` didn't throw, and the result was not null,\n // We will assume that this endpoint is reachable from this point forward,\n // and avoid pinging again to it.\n this.isEndpointUnavailable = false;\n } else {\n // We've previously determined that the endpoint was unavailable,\n // either because it was unreachable or permanently unable to authenticate.\n const error = new CredentialUnavailableError(\n \"The managed identity endpoint is not currently available\",\n );\n logger.getToken.info(formatError(scopes, error));\n throw error;\n }\n\n logger.getToken.info(formatSuccess(scopes));\n return result;\n } catch (err: any) {\n // CredentialUnavailable errors are expected to reach here.\n // We intend them to bubble up, so that DefaultAzureCredential can catch them.\n if (err.name === \"AuthenticationRequiredError\") {\n throw err;\n }\n\n // Expected errors to reach this point:\n // - Errors coming from a method unexpectedly breaking.\n // - When identityClient.sendTokenRequest throws, in which case\n // if the status code was 400, it means that the endpoint is working,\n // but no identity is available.\n\n span.setStatus({\n status: \"error\",\n error: err,\n });\n\n // If either the network is unreachable,\n // we can safely assume the credential is unavailable.\n if (err.code === \"ENETUNREACH\") {\n const error = new CredentialUnavailableError(\n `ManagedIdentityCredential: Unavailable. Network unreachable. Message: ${err.message}`,\n );\n\n logger.getToken.info(formatError(scopes, error));\n throw error;\n }\n\n // If either the host was unreachable,\n // we can safely assume the credential is unavailable.\n if (err.code === \"EHOSTUNREACH\") {\n const error = new CredentialUnavailableError(\n `ManagedIdentityCredential: Unavailable. No managed identity endpoint found. Message: ${err.message}`,\n );\n\n logger.getToken.info(formatError(scopes, error));\n throw error;\n }\n // If err.statusCode has a value of 400, it comes from sendTokenRequest,\n // and it means that the endpoint is working, but that no identity is available.\n if (err.statusCode === 400) {\n throw new CredentialUnavailableError(\n `ManagedIdentityCredential: The managed identity endpoint is indicating there's no available identity. Message: ${err.message}`,\n );\n }\n\n // This is a special case for Docker Desktop which responds with a 403 with a message that contains \"A socket operation was attempted to an unreachable network\" or \"A socket operation was attempted to an unreachable host\"\n // rather than just timing out, as expected.\n if (err.statusCode === 403 || err.code === 403) {\n if (err.message.includes(\"unreachable\")) {\n const error = new CredentialUnavailableError(\n `ManagedIdentityCredential: Unavailable. Network unreachable. Message: ${err.message}`,\n );\n\n logger.getToken.info(formatError(scopes, error));\n throw error;\n }\n }\n\n // If the error has no status code, we can assume there was no available identity.\n // This will throw silently during any ChainedTokenCredential.\n if (err.statusCode === undefined) {\n throw new CredentialUnavailableError(\n `ManagedIdentityCredential: Authentication failed. Message ${err.message}`,\n );\n }\n\n // Any other error should break the chain.\n throw new AuthenticationError(err.statusCode, {\n error: `ManagedIdentityCredential authentication failed.`,\n error_description: err.message,\n });\n } finally {\n // Finally is always called, both if we return and if we throw in the above try/catch.\n span.end();\n }\n }\n\n /**\n * Handles the MSAL authentication result.\n * If the result has an account, we update the local account reference.\n * If the token received is invalid, an error will be thrown depending on what's missing.\n */\n private handleResult(\n scopes: string | string[],\n result?: MsalResult,\n getTokenOptions?: GetTokenOptions,\n ): AccessToken {\n this.ensureValidMsalToken(scopes, result, getTokenOptions);\n logger.getToken.info(formatSuccess(scopes));\n return {\n token: result.accessToken,\n expiresOnTimestamp: result.expiresOn.getTime(),\n refreshAfterTimestamp: result.refreshOn?.getTime(),\n tokenType: \"Bearer\",\n } as AccessToken;\n }\n\n /**\n * Ensures the validity of the MSAL token\n */\n private ensureValidMsalToken(\n scopes: string | string[],\n msalToken?: MsalToken,\n getTokenOptions?: GetTokenOptions,\n ): asserts msalToken is ValidMsalToken {\n const error = (message: string): Error => {\n logger.getToken.info(message);\n return new AuthenticationRequiredError({\n scopes: Array.isArray(scopes) ? scopes : [scopes],\n getTokenOptions,\n message,\n });\n };\n if (!msalToken) {\n throw error(\"No response\");\n }\n if (!msalToken.expiresOn) {\n throw error(`Response had no \"expiresOn\" property.`);\n }\n if (!msalToken.accessToken) {\n throw error(`Response had no \"accessToken\" property.`);\n }\n }\n\n private initializeSetAppTokenProvider(): void {\n if (!this.isAppTokenProviderInitialized) {\n this.confidentialApp.SetAppTokenProvider(async (appTokenProviderParameters) => {\n logger.info(\n `SetAppTokenProvider invoked with parameters- ${JSON.stringify(\n appTokenProviderParameters,\n )}`,\n );\n const getTokenOptions: GetTokenOptions = {\n ...appTokenProviderParameters,\n };\n logger.info(\n `authenticateManagedIdentity invoked with scopes- ${JSON.stringify(\n appTokenProviderParameters.scopes,\n )} and getTokenOptions - ${JSON.stringify(getTokenOptions)}`,\n );\n const resultToken = await this.authenticateManagedIdentity(\n appTokenProviderParameters.scopes,\n getTokenOptions,\n );\n\n if (resultToken) {\n logger.info(`SetAppTokenProvider will save the token in cache`);\n\n const expiresInSeconds = resultToken?.expiresOnTimestamp\n ? Math.floor((resultToken.expiresOnTimestamp - Date.now()) / 1000)\n : 0;\n const refreshInSeconds = resultToken?.refreshAfterTimestamp\n ? Math.floor((resultToken.refreshAfterTimestamp - Date.now()) / 1000)\n : 0;\n return {\n accessToken: resultToken?.token,\n expiresInSeconds,\n refreshInSeconds,\n };\n } else {\n logger.info(\n `SetAppTokenProvider token has \"no_access_token_returned\" as the saved token`,\n );\n return {\n accessToken: \"no_access_token_returned\",\n expiresInSeconds: 0,\n };\n }\n });\n this.isAppTokenProviderInitialized = true;\n }\n }\n}\n"]}
@@ -1,212 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
- import { AuthenticationRequiredError, CredentialUnavailableError } from "../../errors";
4
- import { credentialLogger, formatError, formatSuccess } from "../../util/logging";
5
- import { defaultLoggerCallback, getMSALLogLevel } from "../../msal/utils";
6
- import { IdentityClient } from "../../client/identityClient";
7
- import { ManagedIdentityApplication } from "@azure/msal-node";
8
- import { getLogLevel } from "@azure/logger";
9
- import { imdsMsi } from "./imdsMsi";
10
- import { imdsRetryPolicy } from "./imdsRetryPolicy";
11
- import { mapScopesToResource } from "./utils";
12
- import { tokenExchangeMsi } from "./tokenExchangeMsi";
13
- import { tracingClient } from "../../util/tracing";
14
- const logger = credentialLogger("ManagedIdentityCredential(MSAL)");
15
- export class MsalMsiProvider {
16
- constructor(clientIdOrOptions, options = {}) {
17
- var _a, _b;
18
- this.msiRetryConfig = {
19
- maxRetries: 5,
20
- startDelayInMs: 800,
21
- intervalIncrement: 2,
22
- };
23
- let _options = {};
24
- if (typeof clientIdOrOptions === "string") {
25
- this.clientId = clientIdOrOptions;
26
- _options = options;
27
- }
28
- else {
29
- this.clientId = clientIdOrOptions === null || clientIdOrOptions === void 0 ? void 0 : clientIdOrOptions.clientId;
30
- _options = clientIdOrOptions !== null && clientIdOrOptions !== void 0 ? clientIdOrOptions : {};
31
- }
32
- this.resourceId = _options === null || _options === void 0 ? void 0 : _options.resourceId;
33
- this.objectId = _options === null || _options === void 0 ? void 0 : _options.objectId;
34
- // For JavaScript users.
35
- const providedIds = [this.clientId, this.resourceId, this.objectId].filter(Boolean);
36
- if (providedIds.length > 1) {
37
- throw new Error(`ManagedIdentityCredential: only one of 'clientId', 'resourceId', or 'objectId' can be provided. Received values: ${JSON.stringify({ clientId: this.clientId, resourceId: this.resourceId, objectId: this.objectId })}`);
38
- }
39
- // ManagedIdentity uses http for local requests
40
- _options.allowInsecureConnection = true;
41
- if (((_a = _options === null || _options === void 0 ? void 0 : _options.retryOptions) === null || _a === void 0 ? void 0 : _a.maxRetries) !== undefined) {
42
- this.msiRetryConfig.maxRetries = _options.retryOptions.maxRetries;
43
- }
44
- this.identityClient = new IdentityClient(Object.assign(Object.assign({}, _options), { additionalPolicies: [{ policy: imdsRetryPolicy(this.msiRetryConfig), position: "perCall" }] }));
45
- this.managedIdentityApp = new ManagedIdentityApplication({
46
- managedIdentityIdParams: {
47
- userAssignedClientId: this.clientId,
48
- userAssignedResourceId: this.resourceId,
49
- userAssignedObjectId: this.objectId,
50
- },
51
- system: {
52
- // todo: proxyUrl?
53
- disableInternalRetries: true,
54
- networkClient: this.identityClient,
55
- loggerOptions: {
56
- logLevel: getMSALLogLevel(getLogLevel()),
57
- piiLoggingEnabled: (_b = options.loggingOptions) === null || _b === void 0 ? void 0 : _b.enableUnsafeSupportLogging,
58
- loggerCallback: defaultLoggerCallback(logger),
59
- },
60
- },
61
- });
62
- this.isAvailableIdentityClient = new IdentityClient(Object.assign(Object.assign({}, _options), { retryOptions: {
63
- maxRetries: 0,
64
- } }));
65
- // CloudShell MSI will ignore any user-assigned identity passed as parameters. To avoid confusion, we prevent this from happening as early as possible.
66
- if (this.managedIdentityApp.getManagedIdentitySource() === "CloudShell") {
67
- if (this.clientId || this.resourceId || this.objectId) {
68
- logger.warning(`CloudShell MSI detected with user-provided IDs - throwing. Received values: ${JSON.stringify({
69
- clientId: this.clientId,
70
- resourceId: this.resourceId,
71
- objectId: this.objectId,
72
- })}.`);
73
- throw new CredentialUnavailableError("ManagedIdentityCredential: Specifying a user-assigned managed identity is not supported for CloudShell at runtime. When using Managed Identity in CloudShell, omit the clientId, resourceId, and objectId parameters.");
74
- }
75
- }
76
- }
77
- /**
78
- * Authenticates with Microsoft Entra ID and returns an access token if successful.
79
- * If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.
80
- * If an unexpected error occurs, an {@link AuthenticationError} will be thrown with the details of the failure.
81
- *
82
- * @param scopes - The list of scopes for which the token will have access.
83
- * @param options - The options used to configure any requests this
84
- * TokenCredential implementation might make.
85
- */
86
- async getToken(scopes, options = {}) {
87
- logger.getToken.info("Using the MSAL provider for Managed Identity.");
88
- const resource = mapScopesToResource(scopes);
89
- if (!resource) {
90
- throw new CredentialUnavailableError(`ManagedIdentityCredential: Multiple scopes are not supported. Scopes: ${JSON.stringify(scopes)}`);
91
- }
92
- return tracingClient.withSpan("ManagedIdentityCredential.getToken", options, async () => {
93
- var _a;
94
- try {
95
- const isTokenExchangeMsi = await tokenExchangeMsi.isAvailable({
96
- scopes,
97
- clientId: this.clientId,
98
- getTokenOptions: options,
99
- identityClient: this.identityClient,
100
- resourceId: this.resourceId,
101
- });
102
- // Most scenarios are handled by MSAL except for two:
103
- // AKS pod identity - MSAL does not implement the token exchange flow.
104
- // IMDS Endpoint probing - MSAL does not do any probing before trying to get a token.
105
- // As a DefaultAzureCredential optimization we probe the IMDS endpoint with a short timeout and no retries before actually trying to get a token
106
- // We will continue to implement these features in the Identity library.
107
- const identitySource = this.managedIdentityApp.getManagedIdentitySource();
108
- const isImdsMsi = identitySource === "DefaultToImds" || identitySource === "Imds"; // Neither actually checks that IMDS endpoint is available, just that it's the source the MSAL _would_ try to use.
109
- logger.getToken.info(`MSAL Identity source: ${identitySource}`);
110
- if (isTokenExchangeMsi) {
111
- // In the AKS scenario we will use the existing tokenExchangeMsi indefinitely.
112
- logger.getToken.info("Using the token exchange managed identity.");
113
- const result = await tokenExchangeMsi.getToken({
114
- scopes,
115
- clientId: this.clientId,
116
- identityClient: this.identityClient,
117
- retryConfig: this.msiRetryConfig,
118
- resourceId: this.resourceId,
119
- });
120
- if (result === null) {
121
- throw new CredentialUnavailableError("Attempted to use the token exchange managed identity, but received a null response.");
122
- }
123
- return result;
124
- }
125
- else if (isImdsMsi) {
126
- // In the IMDS scenario we will probe the IMDS endpoint to ensure it's available before trying to get a token.
127
- // If the IMDS endpoint is not available and this is the source that MSAL will use, we will fail-fast with an error that tells DAC to move to the next credential.
128
- logger.getToken.info("Using the IMDS endpoint to probe for availability.");
129
- const isAvailable = await imdsMsi.isAvailable({
130
- scopes,
131
- clientId: this.clientId,
132
- getTokenOptions: options,
133
- identityClient: this.isAvailableIdentityClient,
134
- resourceId: this.resourceId,
135
- });
136
- if (!isAvailable) {
137
- throw new CredentialUnavailableError(`Attempted to use the IMDS endpoint, but it is not available.`);
138
- }
139
- }
140
- // If we got this far, it means:
141
- // - This is not a tokenExchangeMsi,
142
- // - We already probed for IMDS endpoint availability and failed-fast if it's unreachable.
143
- // We can proceed normally by calling MSAL for a token.
144
- logger.getToken.info("Calling into MSAL for managed identity token.");
145
- const token = await this.managedIdentityApp.acquireToken({
146
- resource,
147
- });
148
- this.ensureValidMsalToken(scopes, token, options);
149
- logger.getToken.info(formatSuccess(scopes));
150
- return {
151
- expiresOnTimestamp: token.expiresOn.getTime(),
152
- token: token.accessToken,
153
- refreshAfterTimestamp: (_a = token.refreshOn) === null || _a === void 0 ? void 0 : _a.getTime(),
154
- tokenType: "Bearer",
155
- };
156
- }
157
- catch (err) {
158
- logger.getToken.error(formatError(scopes, err));
159
- // AuthenticationRequiredError described as Error to enforce authentication after trying to retrieve a token silently.
160
- // TODO: why would this _ever_ happen considering we're not trying the silent request in this flow?
161
- if (err.name === "AuthenticationRequiredError") {
162
- throw err;
163
- }
164
- if (isNetworkError(err)) {
165
- throw new CredentialUnavailableError(`ManagedIdentityCredential: Network unreachable. Message: ${err.message}`, { cause: err });
166
- }
167
- throw new CredentialUnavailableError(`ManagedIdentityCredential: Authentication failed. Message ${err.message}`, { cause: err });
168
- }
169
- });
170
- }
171
- /**
172
- * Ensures the validity of the MSAL token
173
- */
174
- ensureValidMsalToken(scopes, msalToken, getTokenOptions) {
175
- const createError = (message) => {
176
- logger.getToken.info(message);
177
- return new AuthenticationRequiredError({
178
- scopes: Array.isArray(scopes) ? scopes : [scopes],
179
- getTokenOptions,
180
- message,
181
- });
182
- };
183
- if (!msalToken) {
184
- throw createError("No response.");
185
- }
186
- if (!msalToken.expiresOn) {
187
- throw createError(`Response had no "expiresOn" property.`);
188
- }
189
- if (!msalToken.accessToken) {
190
- throw createError(`Response had no "accessToken" property.`);
191
- }
192
- }
193
- }
194
- function isNetworkError(err) {
195
- // MSAL error
196
- if (err.errorCode === "network_error") {
197
- return true;
198
- }
199
- // Probe errors
200
- if (err.code === "ENETUNREACH" || err.code === "EHOSTUNREACH") {
201
- return true;
202
- }
203
- // This is a special case for Docker Desktop which responds with a 403 with a message that contains "A socket operation was attempted to an unreachable network" or "A socket operation was attempted to an unreachable host"
204
- // rather than just timing out, as expected.
205
- if (err.statusCode === 403 || err.code === 403) {
206
- if (err.message.includes("unreachable")) {
207
- return true;
208
- }
209
- }
210
- return false;
211
- }
212
- //# sourceMappingURL=msalMsiProvider.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"msalMsiProvider.js","sourceRoot":"","sources":["../../../../src/credentials/managedIdentityCredential/msalMsiProvider.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAAE,2BAA2B,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAEvF,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAClF,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAE1E,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,OAAO,EAAE,0BAA0B,EAAE,MAAM,kBAAkB,CAAC;AAE9D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO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soft Corporation.\n// Licensed under the MIT License.\n\nimport type { AccessToken, GetTokenOptions } from \"@azure/core-auth\";\nimport { AuthenticationRequiredError, CredentialUnavailableError } from \"../../errors\";\nimport type { MsalToken, ValidMsalToken } from \"../../msal/types\";\nimport { credentialLogger, formatError, formatSuccess } from \"../../util/logging\";\nimport { defaultLoggerCallback, getMSALLogLevel } from \"../../msal/utils\";\n\nimport { IdentityClient } from \"../../client/identityClient\";\nimport type { MSIConfiguration } from \"./models\";\nimport { ManagedIdentityApplication } from \"@azure/msal-node\";\nimport type { TokenCredentialOptions } from \"../../tokenCredentialOptions\";\nimport { getLogLevel } from \"@azure/logger\";\nimport { imdsMsi } from \"./imdsMsi\";\nimport { imdsRetryPolicy } from \"./imdsRetryPolicy\";\nimport { mapScopesToResource } from \"./utils\";\nimport { tokenExchangeMsi } from \"./tokenExchangeMsi\";\nimport { tracingClient } from \"../../util/tracing\";\n\nconst logger = credentialLogger(\"ManagedIdentityCredential(MSAL)\");\n\n/**\n * Options to send on the {@link ManagedIdentityCredential} constructor.\n * Since this is an internal implementation, uses a looser interface than the public one.\n */\ninterface ManagedIdentityCredentialOptions extends TokenCredentialOptions {\n /**\n * The client ID of the user - assigned identity, or app registration(when working with AKS pod - identity).\n */\n clientId?: string;\n\n /**\n * Allows specifying a custom resource Id.\n * In scenarios such as when user assigned identities are created using an ARM template,\n * where the resource Id of the identity is known but the client Id can't be known ahead of time,\n * this parameter allows programs to use these user assigned identities\n * without having to first determine the client Id of the created identity.\n */\n resourceId?: string;\n\n /**\n * Allows specifying the object ID of the underlying service principal used to authenticate a user-assigned managed identity.\n * This is an alternative to providing a client ID and is not required for system-assigned managed identities.\n */\n objectId?: string;\n}\n\nexport class MsalMsiProvider {\n private managedIdentityApp: ManagedIdentityApplication;\n private identityClient: IdentityClient;\n private clientId?: string;\n private resourceId?: string;\n private objectId?: string;\n private msiRetryConfig: MSIConfiguration[\"retryConfig\"] = {\n maxRetries: 5,\n startDelayInMs: 800,\n intervalIncrement: 2,\n };\n private isAvailableIdentityClient: IdentityClient;\n\n constructor(\n clientIdOrOptions?: string | ManagedIdentityCredentialOptions,\n options: ManagedIdentityCredentialOptions = {},\n ) {\n let _options: ManagedIdentityCredentialOptions = {};\n if (typeof clientIdOrOptions === \"string\") {\n this.clientId = clientIdOrOptions;\n _options = options;\n } else {\n this.clientId = clientIdOrOptions?.clientId;\n _options = clientIdOrOptions ?? {};\n }\n this.resourceId = _options?.resourceId;\n this.objectId = _options?.objectId;\n\n // For JavaScript users.\n const providedIds = [this.clientId, this.resourceId, this.objectId].filter(Boolean);\n if (providedIds.length > 1) {\n throw new Error(\n `ManagedIdentityCredential: only one of 'clientId', 'resourceId', or 'objectId' can be provided. Received values: ${JSON.stringify(\n { clientId: this.clientId, resourceId: this.resourceId, objectId: this.objectId },\n )}`,\n );\n }\n\n // ManagedIdentity uses http for local requests\n _options.allowInsecureConnection = true;\n\n if (_options?.retryOptions?.maxRetries !== undefined) {\n this.msiRetryConfig.maxRetries = _options.retryOptions.maxRetries;\n }\n\n this.identityClient = new IdentityClient({\n ..._options,\n additionalPolicies: [{ policy: imdsRetryPolicy(this.msiRetryConfig), position: \"perCall\" }],\n });\n\n this.managedIdentityApp = new ManagedIdentityApplication({\n managedIdentityIdParams: {\n userAssignedClientId: this.clientId,\n userAssignedResourceId: this.resourceId,\n userAssignedObjectId: this.objectId,\n },\n system: {\n // todo: proxyUrl?\n disableInternalRetries: true,\n networkClient: this.identityClient,\n loggerOptions: {\n logLevel: getMSALLogLevel(getLogLevel()),\n piiLoggingEnabled: options.loggingOptions?.enableUnsafeSupportLogging,\n loggerCallback: defaultLoggerCallback(logger),\n },\n },\n });\n\n this.isAvailableIdentityClient = new IdentityClient({\n ..._options,\n retryOptions: {\n maxRetries: 0,\n },\n });\n\n // CloudShell MSI will ignore any user-assigned identity passed as parameters. To avoid confusion, we prevent this from happening as early as possible.\n if (this.managedIdentityApp.getManagedIdentitySource() === \"CloudShell\") {\n if (this.clientId || this.resourceId || this.objectId) {\n logger.warning(\n `CloudShell MSI detected with user-provided IDs - throwing. Received values: ${JSON.stringify(\n {\n clientId: this.clientId,\n resourceId: this.resourceId,\n objectId: this.objectId,\n },\n )}.`,\n );\n throw new CredentialUnavailableError(\n \"ManagedIdentityCredential: Specifying a user-assigned managed identity is not supported for CloudShell at runtime. When using Managed Identity in CloudShell, omit the clientId, resourceId, and objectId parameters.\",\n );\n }\n }\n }\n\n /**\n * Authenticates with Microsoft Entra ID and returns an access token if successful.\n * If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.\n * If an unexpected error occurs, an {@link AuthenticationError} will be thrown with the details of the failure.\n *\n * @param scopes - The list of scopes for which the token will have access.\n * @param options - The options used to configure any requests this\n * TokenCredential implementation might make.\n */\n public async getToken(\n scopes: string | string[],\n options: GetTokenOptions = {},\n ): Promise<AccessToken> {\n logger.getToken.info(\"Using the MSAL provider for Managed Identity.\");\n const resource = mapScopesToResource(scopes);\n if (!resource) {\n throw new CredentialUnavailableError(\n `ManagedIdentityCredential: Multiple scopes are not supported. Scopes: ${JSON.stringify(\n scopes,\n )}`,\n );\n }\n\n return tracingClient.withSpan(\"ManagedIdentityCredential.getToken\", options, async () => {\n try {\n const isTokenExchangeMsi = await tokenExchangeMsi.isAvailable({\n scopes,\n clientId: this.clientId,\n getTokenOptions: options,\n identityClient: this.identityClient,\n resourceId: this.resourceId,\n });\n\n // Most scenarios are handled by MSAL except for two:\n // AKS pod identity - MSAL does not implement the token exchange flow.\n // IMDS Endpoint probing - MSAL does not do any probing before trying to get a token.\n // As a DefaultAzureCredential optimization we probe the IMDS endpoint with a short timeout and no retries before actually trying to get a token\n // We will continue to implement these features in the Identity library.\n\n const identitySource = this.managedIdentityApp.getManagedIdentitySource();\n const isImdsMsi = identitySource === \"DefaultToImds\" || identitySource === \"Imds\"; // Neither actually checks that IMDS endpoint is available, just that it's the source the MSAL _would_ try to use.\n\n logger.getToken.info(`MSAL Identity source: ${identitySource}`);\n\n if (isTokenExchangeMsi) {\n // In the AKS scenario we will use the existing tokenExchangeMsi indefinitely.\n logger.getToken.info(\"Using the token exchange managed identity.\");\n const result = await tokenExchangeMsi.getToken({\n scopes,\n clientId: this.clientId,\n identityClient: this.identityClient,\n retryConfig: this.msiRetryConfig,\n resourceId: this.resourceId,\n });\n\n if (result === null) {\n throw new CredentialUnavailableError(\n \"Attempted to use the token exchange managed identity, but received a null response.\",\n );\n }\n\n return result;\n } else if (isImdsMsi) {\n // In the IMDS scenario we will probe the IMDS endpoint to ensure it's available before trying to get a token.\n // If the IMDS endpoint is not available and this is the source that MSAL will use, we will fail-fast with an error that tells DAC to move to the next credential.\n logger.getToken.info(\"Using the IMDS endpoint to probe for availability.\");\n const isAvailable = await imdsMsi.isAvailable({\n scopes,\n clientId: this.clientId,\n getTokenOptions: options,\n identityClient: this.isAvailableIdentityClient,\n resourceId: this.resourceId,\n });\n\n if (!isAvailable) {\n throw new CredentialUnavailableError(\n `Attempted to use the IMDS endpoint, but it is not available.`,\n );\n }\n }\n\n // If we got this far, it means:\n // - This is not a tokenExchangeMsi,\n // - We already probed for IMDS endpoint availability and failed-fast if it's unreachable.\n // We can proceed normally by calling MSAL for a token.\n logger.getToken.info(\"Calling into MSAL for managed identity token.\");\n const token = await this.managedIdentityApp.acquireToken({\n resource,\n });\n\n this.ensureValidMsalToken(scopes, token, options);\n logger.getToken.info(formatSuccess(scopes));\n\n return {\n expiresOnTimestamp: token.expiresOn.getTime(),\n token: token.accessToken,\n refreshAfterTimestamp: token.refreshOn?.getTime(),\n tokenType: \"Bearer\",\n } as AccessToken;\n } catch (err: any) {\n logger.getToken.error(formatError(scopes, err));\n\n // AuthenticationRequiredError described as Error to enforce authentication after trying to retrieve a token silently.\n // TODO: why would this _ever_ happen considering we're not trying the silent request in this flow?\n if (err.name === \"AuthenticationRequiredError\") {\n throw err;\n }\n\n if (isNetworkError(err)) {\n throw new CredentialUnavailableError(\n `ManagedIdentityCredential: Network unreachable. Message: ${err.message}`,\n { cause: err },\n );\n }\n\n throw new CredentialUnavailableError(\n `ManagedIdentityCredential: Authentication failed. Message ${err.message}`,\n { cause: err },\n );\n }\n });\n }\n\n /**\n * Ensures the validity of the MSAL token\n */\n private ensureValidMsalToken(\n scopes: string | string[],\n msalToken?: MsalToken,\n getTokenOptions?: GetTokenOptions,\n ): asserts msalToken is ValidMsalToken {\n const createError = (message: string): Error => {\n logger.getToken.info(message);\n return new AuthenticationRequiredError({\n scopes: Array.isArray(scopes) ? scopes : [scopes],\n getTokenOptions,\n message,\n });\n };\n if (!msalToken) {\n throw createError(\"No response.\");\n }\n if (!msalToken.expiresOn) {\n throw createError(`Response had no \"expiresOn\" property.`);\n }\n if (!msalToken.accessToken) {\n throw createError(`Response had no \"accessToken\" property.`);\n }\n }\n}\n\nfunction isNetworkError(err: any): boolean {\n // MSAL error\n if (err.errorCode === \"network_error\") {\n return true;\n }\n\n // Probe errors\n if (err.code === \"ENETUNREACH\" || err.code === \"EHOSTUNREACH\") {\n return true;\n }\n\n // This is a special case for Docker Desktop which responds with a 403 with a message that contains \"A socket operation was attempted to an unreachable network\" or \"A socket operation was attempted to an unreachable host\"\n // rather than just timing out, as expected.\n if (err.statusCode === 403 || err.code === 403) {\n if (err.message.includes(\"unreachable\")) {\n return true;\n }\n }\n\n return false;\n}\n"]}