@azure/msal-node 2.6.4 → 2.6.5-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/dist/cache/NodeStorage.cjs +1 -1
  2. package/dist/cache/NodeStorage.mjs +1 -1
  3. package/dist/cache/TokenCache.cjs +1 -1
  4. package/dist/cache/TokenCache.mjs +1 -1
  5. package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
  6. package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
  7. package/dist/cache/serializer/Deserializer.cjs +1 -1
  8. package/dist/cache/serializer/Deserializer.mjs +1 -1
  9. package/dist/cache/serializer/Serializer.cjs +1 -1
  10. package/dist/cache/serializer/Serializer.mjs +1 -1
  11. package/dist/client/ClientApplication.cjs +1 -1
  12. package/dist/client/ClientApplication.mjs +1 -1
  13. package/dist/client/ClientAssertion.cjs +1 -1
  14. package/dist/client/ClientAssertion.mjs +1 -1
  15. package/dist/client/ClientCredentialClient.cjs +27 -23
  16. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  17. package/dist/client/ClientCredentialClient.d.ts +3 -3
  18. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  19. package/dist/client/ClientCredentialClient.mjs +28 -24
  20. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  21. package/dist/client/ConfidentialClientApplication.cjs +1 -1
  22. package/dist/client/ConfidentialClientApplication.mjs +1 -1
  23. package/dist/client/DeviceCodeClient.cjs +1 -1
  24. package/dist/client/DeviceCodeClient.mjs +1 -1
  25. package/dist/client/ManagedIdentityApplication.cjs +97 -0
  26. package/dist/client/ManagedIdentityApplication.cjs.map +1 -0
  27. package/dist/client/ManagedIdentityApplication.d.ts +24 -0
  28. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -0
  29. package/dist/client/ManagedIdentityApplication.mjs +95 -0
  30. package/dist/client/ManagedIdentityApplication.mjs.map +1 -0
  31. package/dist/client/ManagedIdentityClient.cjs +53 -0
  32. package/dist/client/ManagedIdentityClient.cjs.map +1 -0
  33. package/dist/client/ManagedIdentityClient.d.ts +25 -0
  34. package/dist/client/ManagedIdentityClient.d.ts.map +1 -0
  35. package/dist/client/ManagedIdentityClient.mjs +51 -0
  36. package/dist/client/ManagedIdentityClient.mjs.map +1 -0
  37. package/dist/client/ManagedIdentitySources/AppService.cjs +52 -0
  38. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -0
  39. package/dist/client/ManagedIdentitySources/AppService.d.ts +17 -0
  40. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -0
  41. package/dist/client/ManagedIdentitySources/AppService.mjs +50 -0
  42. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -0
  43. package/dist/client/ManagedIdentitySources/AzureArc.cjs +96 -0
  44. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -0
  45. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +19 -0
  46. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -0
  47. package/dist/client/ManagedIdentitySources/AzureArc.mjs +93 -0
  48. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -0
  49. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +122 -0
  50. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -0
  51. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +31 -0
  52. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -0
  53. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +119 -0
  54. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -0
  55. package/dist/client/ManagedIdentitySources/CloudShell.cjs +47 -0
  56. package/dist/client/ManagedIdentitySources/CloudShell.cjs.map +1 -0
  57. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +16 -0
  58. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -0
  59. package/dist/client/ManagedIdentitySources/CloudShell.mjs +45 -0
  60. package/dist/client/ManagedIdentitySources/CloudShell.mjs.map +1 -0
  61. package/dist/client/ManagedIdentitySources/Imds.cjs +54 -0
  62. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -0
  63. package/dist/client/ManagedIdentitySources/Imds.d.ts +13 -0
  64. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -0
  65. package/dist/client/ManagedIdentitySources/Imds.mjs +52 -0
  66. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -0
  67. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs +61 -0
  68. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs.map +1 -0
  69. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +17 -0
  70. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -0
  71. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +59 -0
  72. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs.map +1 -0
  73. package/dist/client/OnBehalfOfClient.cjs +1 -1
  74. package/dist/client/OnBehalfOfClient.mjs +1 -1
  75. package/dist/client/PublicClientApplication.cjs +1 -1
  76. package/dist/client/PublicClientApplication.mjs +1 -1
  77. package/dist/client/UsernamePasswordClient.cjs +1 -1
  78. package/dist/client/UsernamePasswordClient.mjs +1 -1
  79. package/dist/config/Configuration.cjs +36 -2
  80. package/dist/config/Configuration.cjs.map +1 -1
  81. package/dist/config/Configuration.d.ts +16 -0
  82. package/dist/config/Configuration.d.ts.map +1 -1
  83. package/dist/config/Configuration.mjs +36 -3
  84. package/dist/config/Configuration.mjs.map +1 -1
  85. package/dist/config/ManagedIdentityId.cjs +59 -0
  86. package/dist/config/ManagedIdentityId.cjs.map +1 -0
  87. package/dist/config/ManagedIdentityId.d.ts +12 -0
  88. package/dist/config/ManagedIdentityId.d.ts.map +1 -0
  89. package/dist/config/ManagedIdentityId.mjs +57 -0
  90. package/dist/config/ManagedIdentityId.mjs.map +1 -0
  91. package/dist/config/ManagedIdentityRequestParameters.cjs +37 -0
  92. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -0
  93. package/dist/config/ManagedIdentityRequestParameters.d.ts +12 -0
  94. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -0
  95. package/dist/config/ManagedIdentityRequestParameters.mjs +35 -0
  96. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -0
  97. package/dist/crypto/CryptoProvider.cjs +1 -1
  98. package/dist/crypto/CryptoProvider.mjs +1 -1
  99. package/dist/crypto/GuidGenerator.cjs +1 -1
  100. package/dist/crypto/GuidGenerator.mjs +1 -1
  101. package/dist/crypto/HashUtils.cjs +1 -1
  102. package/dist/crypto/HashUtils.mjs +1 -1
  103. package/dist/crypto/PkceGenerator.cjs +1 -1
  104. package/dist/crypto/PkceGenerator.mjs +1 -1
  105. package/dist/error/ManagedIdentityError.cjs +51 -0
  106. package/dist/error/ManagedIdentityError.cjs.map +1 -0
  107. package/dist/error/ManagedIdentityError.d.ts +28 -0
  108. package/dist/error/ManagedIdentityError.d.ts.map +1 -0
  109. package/dist/error/ManagedIdentityError.mjs +47 -0
  110. package/dist/error/ManagedIdentityError.mjs.map +1 -0
  111. package/dist/error/ManagedIdentityErrorCodes.cjs +41 -0
  112. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -0
  113. package/dist/error/ManagedIdentityErrorCodes.d.ts +20 -0
  114. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -0
  115. package/dist/error/ManagedIdentityErrorCodes.mjs +28 -0
  116. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -0
  117. package/dist/error/NodeAuthError.cjs +1 -1
  118. package/dist/error/NodeAuthError.mjs +1 -1
  119. package/dist/index.cjs +3 -1
  120. package/dist/index.cjs.map +1 -1
  121. package/dist/index.d.ts +3 -1
  122. package/dist/index.d.ts.map +1 -1
  123. package/dist/index.mjs +2 -1
  124. package/dist/index.mjs.map +1 -1
  125. package/dist/internals.cjs +1 -1
  126. package/dist/internals.mjs +1 -1
  127. package/dist/network/HttpClient.cjs +13 -6
  128. package/dist/network/HttpClient.cjs.map +1 -1
  129. package/dist/network/HttpClient.d.ts +2 -1
  130. package/dist/network/HttpClient.d.ts.map +1 -1
  131. package/dist/network/HttpClient.mjs +13 -6
  132. package/dist/network/HttpClient.mjs.map +1 -1
  133. package/dist/network/HttpClientWithRetries.cjs +44 -0
  134. package/dist/network/HttpClientWithRetries.cjs.map +1 -0
  135. package/dist/network/HttpClientWithRetries.d.ts +12 -0
  136. package/dist/network/HttpClientWithRetries.d.ts.map +1 -0
  137. package/dist/network/HttpClientWithRetries.mjs +42 -0
  138. package/dist/network/HttpClientWithRetries.mjs.map +1 -0
  139. package/dist/network/LoopbackClient.cjs +1 -1
  140. package/dist/network/LoopbackClient.mjs +1 -1
  141. package/dist/packageMetadata.cjs +2 -2
  142. package/dist/packageMetadata.d.ts +1 -1
  143. package/dist/packageMetadata.d.ts.map +1 -1
  144. package/dist/packageMetadata.mjs +2 -2
  145. package/dist/request/ManagedIdentityRequest.d.ts +9 -0
  146. package/dist/request/ManagedIdentityRequest.d.ts.map +1 -0
  147. package/dist/request/ManagedIdentityRequestParams.d.ts +10 -0
  148. package/dist/request/ManagedIdentityRequestParams.d.ts.map +1 -0
  149. package/dist/response/ManagedIdentityTokenResponse.d.ts +25 -0
  150. package/dist/response/ManagedIdentityTokenResponse.d.ts.map +1 -0
  151. package/dist/retry/IHttpRetryPolicy.d.ts +6 -0
  152. package/dist/retry/IHttpRetryPolicy.d.ts.map +1 -0
  153. package/dist/retry/LinearRetryPolicy.cjs +47 -0
  154. package/dist/retry/LinearRetryPolicy.cjs.map +1 -0
  155. package/dist/retry/LinearRetryPolicy.d.ts +12 -0
  156. package/dist/retry/LinearRetryPolicy.d.ts.map +1 -0
  157. package/dist/retry/LinearRetryPolicy.mjs +45 -0
  158. package/dist/retry/LinearRetryPolicy.mjs.map +1 -0
  159. package/dist/utils/Constants.cjs +68 -1
  160. package/dist/utils/Constants.cjs.map +1 -1
  161. package/dist/utils/Constants.d.ts +45 -0
  162. package/dist/utils/Constants.d.ts.map +1 -1
  163. package/dist/utils/Constants.mjs +54 -2
  164. package/dist/utils/Constants.mjs.map +1 -1
  165. package/dist/utils/EncodingUtils.cjs +1 -1
  166. package/dist/utils/EncodingUtils.mjs +1 -1
  167. package/dist/utils/NetworkUtils.cjs +1 -1
  168. package/dist/utils/NetworkUtils.mjs +1 -1
  169. package/package.json +2 -2
  170. package/src/client/ClientCredentialClient.ts +65 -25
  171. package/src/client/ManagedIdentityApplication.ts +194 -0
  172. package/src/client/ManagedIdentityClient.ts +128 -0
  173. package/src/client/ManagedIdentitySources/AppService.ts +122 -0
  174. package/src/client/ManagedIdentitySources/AzureArc.ts +199 -0
  175. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +237 -0
  176. package/src/client/ManagedIdentitySources/CloudShell.ts +104 -0
  177. package/src/client/ManagedIdentitySources/Imds.ts +126 -0
  178. package/src/client/ManagedIdentitySources/ServiceFabric.ts +140 -0
  179. package/src/config/Configuration.ts +81 -1
  180. package/src/config/ManagedIdentityId.ts +73 -0
  181. package/src/config/ManagedIdentityRequestParameters.ts +48 -0
  182. package/src/error/ManagedIdentityError.ts +59 -0
  183. package/src/error/ManagedIdentityErrorCodes.ts +34 -0
  184. package/src/index.ts +4 -0
  185. package/src/network/HttpClient.ts +17 -5
  186. package/src/network/HttpClientWithRetries.ts +80 -0
  187. package/src/packageMetadata.ts +1 -1
  188. package/src/request/ManagedIdentityRequest.ts +15 -0
  189. package/src/request/ManagedIdentityRequestParams.ts +14 -0
  190. package/src/response/ManagedIdentityTokenResponse.ts +33 -0
  191. package/src/retry/IHttpRetryPolicy.ts +18 -0
  192. package/src/retry/LinearRetryPolicy.ts +71 -0
  193. package/src/utils/Constants.ts +63 -0
@@ -0,0 +1,126 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { INetworkModule, Logger } from "@azure/msal-common";
7
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
8
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
9
+ import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
10
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
11
+ import {
12
+ API_VERSION_QUERY_PARAMETER_NAME,
13
+ HttpMethod,
14
+ METADATA_HEADER_NAME,
15
+ ManagedIdentityEnvironmentVariableNames,
16
+ ManagedIdentityIdType,
17
+ ManagedIdentitySourceNames,
18
+ RESOURCE_BODY_OR_QUERY_PARAMETER_NAME,
19
+ } from "../../utils/Constants";
20
+ import { NodeStorage } from "../../cache/NodeStorage";
21
+
22
+ // IMDS constants. Docs for IMDS are available here https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-http
23
+ const IMDS_TOKEN_PATH: string = "/metadata/identity/oauth2/token";
24
+ const DEFAULT_IMDS_ENDPOINT: string = `http://169.254.169.254${IMDS_TOKEN_PATH}`;
25
+
26
+ const IMDS_API_VERSION: string = "2018-02-01";
27
+
28
+ // Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/ImdsManagedIdentitySource.cs
29
+ export class Imds extends BaseManagedIdentitySource {
30
+ private identityEndpoint: string;
31
+
32
+ constructor(
33
+ logger: Logger,
34
+ nodeStorage: NodeStorage,
35
+ networkClient: INetworkModule,
36
+ cryptoProvider: CryptoProvider,
37
+ identityEndpoint: string
38
+ ) {
39
+ super(logger, nodeStorage, networkClient, cryptoProvider);
40
+
41
+ this.identityEndpoint = identityEndpoint;
42
+ }
43
+
44
+ public static tryCreate(
45
+ logger: Logger,
46
+ nodeStorage: NodeStorage,
47
+ networkClient: INetworkModule,
48
+ cryptoProvider: CryptoProvider
49
+ ): Imds {
50
+ let validatedIdentityEndpoint: string;
51
+
52
+ if (
53
+ process.env[
54
+ ManagedIdentityEnvironmentVariableNames
55
+ .AZURE_POD_IDENTITY_AUTHORITY_HOST
56
+ ]
57
+ ) {
58
+ logger.info(
59
+ `[Managed Identity] Environment variable ${
60
+ ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST
61
+ } for ${ManagedIdentitySourceNames.IMDS} returned endpoint: ${
62
+ process.env[
63
+ ManagedIdentityEnvironmentVariableNames
64
+ .AZURE_POD_IDENTITY_AUTHORITY_HOST
65
+ ]
66
+ }`
67
+ );
68
+ validatedIdentityEndpoint = Imds.getValidatedEnvVariableUrlString(
69
+ ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST,
70
+ `${
71
+ process.env[
72
+ ManagedIdentityEnvironmentVariableNames
73
+ .AZURE_POD_IDENTITY_AUTHORITY_HOST
74
+ ]
75
+ }${IMDS_TOKEN_PATH}`,
76
+ ManagedIdentitySourceNames.IMDS,
77
+ logger
78
+ );
79
+ } else {
80
+ logger.info(
81
+ `[Managed Identity] Unable to find ${ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST} environment variable for ${ManagedIdentitySourceNames.IMDS}, using the default endpoint.`
82
+ );
83
+ validatedIdentityEndpoint = DEFAULT_IMDS_ENDPOINT;
84
+ }
85
+
86
+ return new Imds(
87
+ logger,
88
+ nodeStorage,
89
+ networkClient,
90
+ cryptoProvider,
91
+ validatedIdentityEndpoint
92
+ );
93
+ }
94
+
95
+ public createRequest(
96
+ resource: string,
97
+ managedIdentityId: ManagedIdentityId
98
+ ): ManagedIdentityRequestParameters {
99
+ const request: ManagedIdentityRequestParameters =
100
+ new ManagedIdentityRequestParameters(
101
+ HttpMethod.GET,
102
+ this.identityEndpoint
103
+ );
104
+
105
+ request.headers[METADATA_HEADER_NAME] = "true";
106
+
107
+ request.queryParameters[API_VERSION_QUERY_PARAMETER_NAME] =
108
+ IMDS_API_VERSION;
109
+ request.queryParameters[RESOURCE_BODY_OR_QUERY_PARAMETER_NAME] =
110
+ resource;
111
+
112
+ if (
113
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
114
+ ) {
115
+ request.queryParameters[
116
+ this.getManagedIdentityUserAssignedIdQueryParameterKey(
117
+ managedIdentityId.idType
118
+ )
119
+ ] = managedIdentityId.id;
120
+ }
121
+
122
+ // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
123
+
124
+ return request;
125
+ }
126
+ }
@@ -0,0 +1,140 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { INetworkModule, Logger } from "@azure/msal-common";
7
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
8
+ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
9
+ import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
10
+ import { NodeStorage } from "../../cache/NodeStorage";
11
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
12
+ import {
13
+ API_VERSION_QUERY_PARAMETER_NAME,
14
+ HttpMethod,
15
+ ManagedIdentityEnvironmentVariableNames,
16
+ ManagedIdentityIdType,
17
+ ManagedIdentitySourceNames,
18
+ RESOURCE_BODY_OR_QUERY_PARAMETER_NAME,
19
+ SERVICE_FABRIC_SECRET_HEADER_NAME,
20
+ } from "../../utils/Constants";
21
+
22
+ // MSI Constants. Docs for MSI are available here https://docs.microsoft.com/azure/app-service/overview-managed-identity
23
+ const SERVICE_FABRIC_MSI_API_VERSION: string = "2019-07-01-preview";
24
+
25
+ /**
26
+ * Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/ServiceFabricManagedIdentitySource.cs
27
+ */
28
+ export class ServiceFabric extends BaseManagedIdentitySource {
29
+ private identityEndpoint: string;
30
+ private identityHeader: string;
31
+
32
+ constructor(
33
+ logger: Logger,
34
+ nodeStorage: NodeStorage,
35
+ networkClient: INetworkModule,
36
+ cryptoProvider: CryptoProvider,
37
+ identityEndpoint: string,
38
+ identityHeader: string
39
+ ) {
40
+ super(logger, nodeStorage, networkClient, cryptoProvider);
41
+
42
+ this.identityEndpoint = identityEndpoint;
43
+ this.identityHeader = identityHeader;
44
+ }
45
+
46
+ public static tryCreate(
47
+ logger: Logger,
48
+ nodeStorage: NodeStorage,
49
+ networkClient: INetworkModule,
50
+ cryptoProvider: CryptoProvider,
51
+ managedIdentityId: ManagedIdentityId
52
+ ): ServiceFabric | null {
53
+ const identityEndpoint: string | undefined =
54
+ process.env[
55
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT
56
+ ];
57
+ const identityHeader: string | undefined =
58
+ process.env[
59
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_HEADER
60
+ ];
61
+ const identityServerThumbprint: string | undefined =
62
+ process.env[
63
+ ManagedIdentityEnvironmentVariableNames
64
+ .IDENTITY_SERVER_THUMBPRINT
65
+ ];
66
+
67
+ /*
68
+ * if either of the identity endpoint, identity header, or identity server thumbprint
69
+ * environment variables are undefined, this MSI provider is unavailable.
70
+ */
71
+ if (!identityEndpoint || !identityHeader || !identityServerThumbprint) {
72
+ logger.info(
73
+ `[Managed Identity] ${ManagedIdentitySourceNames.SERVICE_FABRIC} managed identity is unavailable because one or all of the '${ManagedIdentityEnvironmentVariableNames.IDENTITY_HEADER}', '${ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT}' or '${ManagedIdentityEnvironmentVariableNames.IDENTITY_SERVER_THUMBPRINT}' environment variables are not defined.`
74
+ );
75
+ return null;
76
+ }
77
+
78
+ const validatedIdentityEndpoint: string =
79
+ ServiceFabric.getValidatedEnvVariableUrlString(
80
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT,
81
+ identityEndpoint,
82
+ ManagedIdentitySourceNames.SERVICE_FABRIC,
83
+ logger
84
+ );
85
+
86
+ logger.info(
87
+ `[Managed Identity] Environment variables validation passed for ${ManagedIdentitySourceNames.SERVICE_FABRIC} managed identity. Endpoint URI: ${validatedIdentityEndpoint}. Creating ${ManagedIdentitySourceNames.SERVICE_FABRIC} managed identity.`
88
+ );
89
+
90
+ if (
91
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
92
+ ) {
93
+ logger.warning(
94
+ `[Managed Identity] ${ManagedIdentitySourceNames.SERVICE_FABRIC} user assigned managed identity is configured in the cluster, not during runtime. See also: https://learn.microsoft.com/en-us/azure/service-fabric/configure-existing-cluster-enable-managed-identity-token-service.`
95
+ );
96
+ }
97
+
98
+ return new ServiceFabric(
99
+ logger,
100
+ nodeStorage,
101
+ networkClient,
102
+ cryptoProvider,
103
+ identityEndpoint,
104
+ identityHeader
105
+ );
106
+ }
107
+
108
+ public createRequest(
109
+ resource: string,
110
+ managedIdentityId: ManagedIdentityId
111
+ ): ManagedIdentityRequestParameters {
112
+ const request: ManagedIdentityRequestParameters =
113
+ new ManagedIdentityRequestParameters(
114
+ HttpMethod.GET,
115
+ this.identityEndpoint
116
+ );
117
+
118
+ request.headers[SERVICE_FABRIC_SECRET_HEADER_NAME] =
119
+ this.identityHeader;
120
+
121
+ request.queryParameters[API_VERSION_QUERY_PARAMETER_NAME] =
122
+ SERVICE_FABRIC_MSI_API_VERSION;
123
+ request.queryParameters[RESOURCE_BODY_OR_QUERY_PARAMETER_NAME] =
124
+ resource;
125
+
126
+ if (
127
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
128
+ ) {
129
+ request.queryParameters[
130
+ this.getManagedIdentityUserAssignedIdQueryParameterKey(
131
+ managedIdentityId.idType
132
+ )
133
+ ] = managedIdentityId.id;
134
+ }
135
+
136
+ // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
137
+
138
+ return request;
139
+ }
140
+ }
@@ -18,6 +18,14 @@ import {
18
18
  import { HttpClient } from "../network/HttpClient.js";
19
19
  import http from "http";
20
20
  import https from "https";
21
+ import { ManagedIdentityId } from "./ManagedIdentityId.js";
22
+ import {
23
+ MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON,
24
+ MANAGED_IDENTITY_MAX_RETRIES,
25
+ MANAGED_IDENTITY_RETRY_DELAY,
26
+ } from "../utils/Constants.js";
27
+ import { LinearRetryPolicy } from "../retry/LinearRetryPolicy.js";
28
+ import { HttpClientWithRetries } from "../network/HttpClientWithRetries.js";
21
29
 
22
30
  /**
23
31
  * - clientId - Client id of the application.
@@ -83,6 +91,7 @@ export type NodeSystemOptions = {
83
91
  networkClient?: INetworkModule;
84
92
  proxyUrl?: string;
85
93
  customAgentOptions?: http.AgentOptions | https.AgentOptions;
94
+ disableInternalRetries?: boolean;
86
95
  };
87
96
 
88
97
  export type NodeTelemetryOptions = {
@@ -107,6 +116,17 @@ export type Configuration = {
107
116
  telemetry?: NodeTelemetryOptions;
108
117
  };
109
118
 
119
+ export type ManagedIdentityIdParams = {
120
+ userAssignedClientId?: string;
121
+ userAssignedResourceId?: string;
122
+ userAssignedObjectId?: string;
123
+ };
124
+
125
+ export type ManagedIdentityConfiguration = {
126
+ managedIdentityIdParams?: ManagedIdentityIdParams;
127
+ system?: NodeSystemOptions;
128
+ };
129
+
110
130
  const DEFAULT_AUTH_OPTIONS: Required<NodeAuthOptions> = {
111
131
  clientId: Constants.EMPTY_STRING,
112
132
  authority: Constants.DEFAULT_AUTHORITY,
@@ -146,6 +166,7 @@ const DEFAULT_SYSTEM_OPTIONS: Required<NodeSystemOptions> = {
146
166
  networkClient: new HttpClient(),
147
167
  proxyUrl: Constants.EMPTY_STRING,
148
168
  customAgentOptions: {} as http.AgentOptions | https.AgentOptions,
169
+ disableInternalRetries: false,
149
170
  };
150
171
 
151
172
  const DEFAULT_TELEMETRY_OPTIONS: Required<NodeTelemetryOptions> = {
@@ -186,9 +207,13 @@ export function buildAppConfiguration({
186
207
  ...DEFAULT_SYSTEM_OPTIONS,
187
208
  networkClient: new HttpClient(
188
209
  system?.proxyUrl,
189
- system?.customAgentOptions as http.AgentOptions | https.AgentOptions
210
+ system?.customAgentOptions as
211
+ | http.AgentOptions
212
+ | https.AgentOptions,
213
+ false // Managed Identity
190
214
  ),
191
215
  loggerOptions: system?.loggerOptions || DEFAULT_LOGGER_OPTIONS,
216
+ disableInternalRetries: system?.disableInternalRetries || false,
192
217
  };
193
218
 
194
219
  return {
@@ -199,3 +224,58 @@ export function buildAppConfiguration({
199
224
  telemetry: { ...DEFAULT_TELEMETRY_OPTIONS, ...telemetry },
200
225
  };
201
226
  }
227
+
228
+ export type ManagedIdentityNodeConfiguration = {
229
+ managedIdentityId: ManagedIdentityId;
230
+ system: Required<
231
+ Pick<NodeSystemOptions, "loggerOptions" | "networkClient">
232
+ >;
233
+ };
234
+
235
+ export function buildManagedIdentityConfiguration({
236
+ managedIdentityIdParams,
237
+ system,
238
+ }: ManagedIdentityConfiguration): ManagedIdentityNodeConfiguration {
239
+ const managedIdentityId: ManagedIdentityId = new ManagedIdentityId(
240
+ managedIdentityIdParams
241
+ );
242
+
243
+ const loggerOptions: LoggerOptions =
244
+ system?.loggerOptions || DEFAULT_LOGGER_OPTIONS;
245
+
246
+ let networkClient: INetworkModule;
247
+ // use developer provided network client if passed in
248
+ if (system?.networkClient) {
249
+ networkClient = system.networkClient;
250
+ // otherwise, create a new one
251
+ } else {
252
+ networkClient = new HttpClient(
253
+ system?.proxyUrl,
254
+ system?.customAgentOptions as
255
+ | http.AgentOptions
256
+ | https.AgentOptions,
257
+ true // Managed Identity
258
+ );
259
+ }
260
+
261
+ // wrap the network client with a retry policy if the developer has not disabled the option to do so
262
+ if (!system?.disableInternalRetries) {
263
+ const linearRetryPolicy: LinearRetryPolicy = new LinearRetryPolicy(
264
+ MANAGED_IDENTITY_MAX_RETRIES,
265
+ MANAGED_IDENTITY_RETRY_DELAY,
266
+ MANAGED_IDENTITY_HTTP_STATUS_CODES_TO_RETRY_ON
267
+ );
268
+ networkClient = new HttpClientWithRetries(
269
+ networkClient,
270
+ linearRetryPolicy
271
+ );
272
+ }
273
+
274
+ return {
275
+ managedIdentityId: managedIdentityId,
276
+ system: {
277
+ loggerOptions,
278
+ networkClient,
279
+ },
280
+ };
281
+ }
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ ManagedIdentityErrorCodes,
8
+ createManagedIdentityError,
9
+ } from "../error/ManagedIdentityError";
10
+ import {
11
+ DEFAULT_MANAGED_IDENTITY_ID,
12
+ ManagedIdentityIdType,
13
+ } from "../utils/Constants";
14
+ import { ManagedIdentityIdParams } from "./Configuration";
15
+
16
+ export class ManagedIdentityId {
17
+ private _id: string;
18
+ public get id(): string {
19
+ return this._id;
20
+ }
21
+ private set id(value: string) {
22
+ this._id = value;
23
+ }
24
+
25
+ private _idType: ManagedIdentityIdType;
26
+ public get idType(): ManagedIdentityIdType {
27
+ return this._idType;
28
+ }
29
+ private set idType(value: ManagedIdentityIdType) {
30
+ this._idType = value;
31
+ }
32
+
33
+ constructor(managedIdentityIdParams?: ManagedIdentityIdParams) {
34
+ const userAssignedClientId =
35
+ managedIdentityIdParams?.userAssignedClientId;
36
+ const userAssignedResourceId =
37
+ managedIdentityIdParams?.userAssignedResourceId;
38
+ const userAssignedObjectId =
39
+ managedIdentityIdParams?.userAssignedObjectId;
40
+
41
+ if (userAssignedClientId) {
42
+ if (userAssignedResourceId || userAssignedObjectId) {
43
+ throw createManagedIdentityError(
44
+ ManagedIdentityErrorCodes.invalidManagedIdentityIdType
45
+ );
46
+ }
47
+
48
+ this.id = userAssignedClientId;
49
+ this.idType = ManagedIdentityIdType.USER_ASSIGNED_CLIENT_ID;
50
+ } else if (userAssignedResourceId) {
51
+ if (userAssignedClientId || userAssignedObjectId) {
52
+ throw createManagedIdentityError(
53
+ ManagedIdentityErrorCodes.invalidManagedIdentityIdType
54
+ );
55
+ }
56
+
57
+ this.id = userAssignedResourceId;
58
+ this.idType = ManagedIdentityIdType.USER_ASSIGNED_RESOURCE_ID;
59
+ } else if (userAssignedObjectId) {
60
+ if (userAssignedClientId || userAssignedResourceId) {
61
+ throw createManagedIdentityError(
62
+ ManagedIdentityErrorCodes.invalidManagedIdentityIdType
63
+ );
64
+ }
65
+
66
+ this.id = userAssignedObjectId;
67
+ this.idType = ManagedIdentityIdType.USER_ASSIGNED_OBJECT_ID;
68
+ } else {
69
+ this.id = DEFAULT_MANAGED_IDENTITY_ID;
70
+ this.idType = ManagedIdentityIdType.SYSTEM_ASSIGNED;
71
+ }
72
+ }
73
+ }
@@ -0,0 +1,48 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { RequestParameterBuilder, UrlString } from "@azure/msal-common";
7
+ import { HttpMethod } from "../utils/Constants";
8
+
9
+ export class ManagedIdentityRequestParameters {
10
+ private _baseEndpoint: string;
11
+ public httpMethod: HttpMethod;
12
+ public headers: Record<string, string>;
13
+ public bodyParameters: Record<string, string>;
14
+ public queryParameters: Record<string, string>;
15
+
16
+ constructor(httpMethod: HttpMethod, endpoint: string) {
17
+ this.httpMethod = httpMethod;
18
+ this._baseEndpoint = endpoint;
19
+ this.headers = {} as Record<string, string>;
20
+ this.bodyParameters = {} as Record<string, string>;
21
+ this.queryParameters = {} as Record<string, string>;
22
+ }
23
+
24
+ public computeUri(): string {
25
+ const parameterBuilder = new RequestParameterBuilder();
26
+
27
+ if (this.queryParameters) {
28
+ parameterBuilder.addExtraQueryParameters(this.queryParameters);
29
+ }
30
+
31
+ const queryParametersString = parameterBuilder.createQueryString();
32
+
33
+ return UrlString.appendQueryString(
34
+ this._baseEndpoint,
35
+ queryParametersString
36
+ );
37
+ }
38
+
39
+ public computeParametersBodyString(): string {
40
+ const parameterBuilder = new RequestParameterBuilder();
41
+
42
+ if (this.bodyParameters) {
43
+ parameterBuilder.addExtraQueryParameters(this.bodyParameters);
44
+ }
45
+
46
+ return parameterBuilder.createQueryString();
47
+ }
48
+ }
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AuthError } from "@azure/msal-common";
7
+ import * as ManagedIdentityErrorCodes from "./ManagedIdentityErrorCodes";
8
+ import { ManagedIdentityEnvironmentVariableNames } from "../utils/Constants";
9
+ export { ManagedIdentityErrorCodes };
10
+
11
+ /**
12
+ * ManagedIdentityErrorMessage class containing string constants used by error codes and messages.
13
+ */
14
+ export const ManagedIdentityErrorMessages = {
15
+ [ManagedIdentityErrorCodes.invalidManagedIdentityIdType]:
16
+ "More than one ManagedIdentityIdType was provided.",
17
+ [ManagedIdentityErrorCodes.invalidResource]:
18
+ "The supplied resource is an invalid URL.",
19
+ [ManagedIdentityErrorCodes.missingId]:
20
+ "A ManagedIdentityId id was not provided.",
21
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
22
+ .AZURE_POD_IDENTITY_AUTHORITY_HOST]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST}' environment variable is malformed.`,
23
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
24
+ .IDENTITY_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT}' environment variable is malformed.`,
25
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
26
+ .IMDS_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT}' environment variable is malformed.`,
27
+ [ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
28
+ .MSI_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT}' environment variable is malformed.`,
29
+ [ManagedIdentityErrorCodes.networkUnavailable]:
30
+ "Authentication unavailable. The request to the managed identity endpoint timed out.",
31
+ [ManagedIdentityErrorCodes.unableToCreateAzureArc]:
32
+ "Azure Arc Managed Identities can only be system assigned.",
33
+ [ManagedIdentityErrorCodes.unableToCreateCloudShell]:
34
+ "Cloud Shell Managed Identities can only be system assigned.",
35
+ [ManagedIdentityErrorCodes.unableToCreateSource]:
36
+ "Unable to create a Managed Identity source based on environment variables.",
37
+ [ManagedIdentityErrorCodes.unableToReadSecretFile]:
38
+ "Unable to read the secret file.",
39
+ [ManagedIdentityErrorCodes.userAssignedNotAvailableAtRuntime]:
40
+ "Service Fabric user assigned managed identity ClientId or ResourceId is not configurable at runtime.",
41
+ [ManagedIdentityErrorCodes.wwwAuthenticateHeaderMissing]:
42
+ "A 401 response was received form the Azure Arc Managed Identity, but the www-authenticate header is missing.",
43
+ [ManagedIdentityErrorCodes.wwwAuthenticateHeaderUnsupportedFormat]:
44
+ "A 401 response was received form the Azure Arc Managed Identity, but the www-authenticate header is in an unsupported format.",
45
+ };
46
+
47
+ export class ManagedIdentityError extends AuthError {
48
+ constructor(errorCode: string) {
49
+ super(errorCode, ManagedIdentityErrorMessages[errorCode]);
50
+ this.name = "ManagedIdentityError";
51
+ Object.setPrototypeOf(this, ManagedIdentityError.prototype);
52
+ }
53
+ }
54
+
55
+ export function createManagedIdentityError(
56
+ errorCode: string
57
+ ): ManagedIdentityError {
58
+ return new ManagedIdentityError(errorCode);
59
+ }
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { ManagedIdentityEnvironmentVariableNames } from "../utils/Constants";
7
+
8
+ export const invalidManagedIdentityIdType = "invalid_managed_identity_id_type";
9
+ export const invalidResource = "invalid_resource";
10
+ export const missingId = "missing_client_id";
11
+ export const networkUnavailable = "network_unavailable";
12
+ export const unableToCreateAzureArc = "unable_to_create_azure_arc";
13
+ export const unableToCreateCloudShell = "unable_to_create_cloud_shell";
14
+ export const unableToCreateSource = "unable_to_create_source";
15
+ export const unableToReadSecretFile = "unable_to_read_secret_file";
16
+ export const urlParseError = "url_parse_error";
17
+ export const userAssignedNotAvailableAtRuntime =
18
+ "user_assigned_not_available_at_runtime";
19
+ export const wwwAuthenticateHeaderMissing = "www_authenticate_header_missing";
20
+ export const wwwAuthenticateHeaderUnsupportedFormat =
21
+ "www_authenticate_header_unsupported_format";
22
+
23
+ export const MsiEnvironmentVariableUrlMalformedErrorCodes = {
24
+ [ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST]:
25
+ "azure_pod_identity_authority_host_url_malformed",
26
+ [ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT]:
27
+ "identity_endpoint_url_malformed",
28
+ [ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT]:
29
+ "imds_endpoint_url_malformed",
30
+ [ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT]:
31
+ "msi_endpoint_url_malformed",
32
+ } as const;
33
+ export type MsiEnvironmentVariableErrorCodes =
34
+ (typeof MsiEnvironmentVariableUrlMalformedErrorCodes)[keyof typeof MsiEnvironmentVariableUrlMalformedErrorCodes];
package/src/index.ts CHANGED
@@ -31,10 +31,13 @@ export { ClientApplication } from "./client/ClientApplication.js";
31
31
  export { ClientCredentialClient } from "./client/ClientCredentialClient.js";
32
32
  export { DeviceCodeClient } from "./client/DeviceCodeClient.js";
33
33
  export { OnBehalfOfClient } from "./client/OnBehalfOfClient.js";
34
+ export { ManagedIdentityApplication } from "./client/ManagedIdentityApplication.js";
34
35
  export { UsernamePasswordClient } from "./client/UsernamePasswordClient.js";
35
36
 
36
37
  export {
37
38
  Configuration,
39
+ ManagedIdentityConfiguration,
40
+ ManagedIdentityIdParams,
38
41
  buildAppConfiguration,
39
42
  NodeAuthOptions,
40
43
  NodeSystemOptions,
@@ -73,6 +76,7 @@ export type { RefreshTokenRequest } from "./request/RefreshTokenRequest.js";
73
76
  export type { SilentFlowRequest } from "./request/SilentFlowRequest.js";
74
77
  export type { InteractiveRequest } from "./request/InteractiveRequest.js";
75
78
  export type { SignOutRequest } from "./request/SignOutRequest.js";
79
+ export type { ManagedIdentityRequestParams } from "./request/ManagedIdentityRequestParams.js";
76
80
 
77
81
  // Common Object Formats
78
82
  export {
@@ -20,13 +20,16 @@ import https from "https";
20
20
  export class HttpClient implements INetworkModule {
21
21
  private proxyUrl: string;
22
22
  private customAgentOptions: http.AgentOptions | https.AgentOptions;
23
+ private managedIdentity: boolean;
23
24
 
24
25
  constructor(
25
26
  proxyUrl?: string,
26
- customAgentOptions?: http.AgentOptions | https.AgentOptions
27
+ customAgentOptions?: http.AgentOptions | https.AgentOptions,
28
+ managedIdentity?: boolean
27
29
  ) {
28
30
  this.proxyUrl = proxyUrl || "";
29
31
  this.customAgentOptions = customAgentOptions || {};
32
+ this.managedIdentity = managedIdentity || false;
30
33
  }
31
34
 
32
35
  /**
@@ -51,7 +54,9 @@ export class HttpClient implements INetworkModule {
51
54
  url,
52
55
  HttpMethod.GET,
53
56
  options,
54
- this.customAgentOptions as https.AgentOptions
57
+ this.customAgentOptions as https.AgentOptions,
58
+ undefined,
59
+ this.managedIdentity
55
60
  );
56
61
  }
57
62
  }
@@ -81,7 +86,8 @@ export class HttpClient implements INetworkModule {
81
86
  HttpMethod.POST,
82
87
  options,
83
88
  this.customAgentOptions as https.AgentOptions,
84
- cancellationToken
89
+ cancellationToken,
90
+ this.managedIdentity
85
91
  );
86
92
  }
87
93
  }
@@ -277,7 +283,8 @@ const networkRequestViaHttps = <T>(
277
283
  httpMethod: string,
278
284
  options?: NetworkRequestOptions,
279
285
  agentOptions?: https.AgentOptions,
280
- timeout?: number
286
+ timeout?: number,
287
+ managedIdentity?: boolean
281
288
  ): Promise<NetworkResponse<T>> => {
282
289
  const isPostRequest = httpMethod === HttpMethod.POST;
283
290
  const body: string = options?.body || "";
@@ -307,7 +314,12 @@ const networkRequestViaHttps = <T>(
307
314
  }
308
315
 
309
316
  return new Promise<NetworkResponse<T>>((resolve, reject) => {
310
- const request = https.request(customOptions);
317
+ let request: http.ClientRequest;
318
+ if (managedIdentity) {
319
+ request = http.request(customOptions);
320
+ } else {
321
+ request = https.request(customOptions);
322
+ }
311
323
 
312
324
  if (timeout) {
313
325
  request.on("timeout", () => {