@azure/msal-node 2.7.0-alpha.0 → 2.7.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 (225) hide show
  1. package/dist/cache/ITokenCache.d.ts +15 -15
  2. package/dist/cache/NodeStorage.cjs +423 -423
  3. package/dist/cache/NodeStorage.cjs.map +1 -1
  4. package/dist/cache/NodeStorage.d.ts +193 -193
  5. package/dist/cache/NodeStorage.d.ts.map +1 -1
  6. package/dist/cache/NodeStorage.mjs +424 -424
  7. package/dist/cache/NodeStorage.mjs.map +1 -1
  8. package/dist/cache/TokenCache.cjs +265 -265
  9. package/dist/cache/TokenCache.d.ts +90 -90
  10. package/dist/cache/TokenCache.mjs +265 -265
  11. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  12. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  13. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  14. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  15. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  16. package/dist/cache/serializer/Deserializer.cjs +167 -170
  17. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  18. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  19. package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
  20. package/dist/cache/serializer/Deserializer.mjs +168 -171
  21. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  22. package/dist/cache/serializer/Serializer.cjs +137 -137
  23. package/dist/cache/serializer/Serializer.d.ts +39 -39
  24. package/dist/cache/serializer/Serializer.mjs +137 -137
  25. package/dist/cache/serializer/SerializerTypes.d.ts +102 -102
  26. package/dist/client/ClientApplication.cjs +333 -333
  27. package/dist/client/ClientApplication.cjs.map +1 -1
  28. package/dist/client/ClientApplication.d.ts +142 -142
  29. package/dist/client/ClientApplication.d.ts.map +1 -1
  30. package/dist/client/ClientApplication.mjs +333 -333
  31. package/dist/client/ClientApplication.mjs.map +1 -1
  32. package/dist/client/ClientAssertion.cjs +116 -116
  33. package/dist/client/ClientAssertion.d.ts +46 -46
  34. package/dist/client/ClientAssertion.mjs +116 -116
  35. package/dist/client/ClientCredentialClient.cjs +194 -194
  36. package/dist/client/ClientCredentialClient.d.ts +33 -33
  37. package/dist/client/ClientCredentialClient.mjs +194 -194
  38. package/dist/client/ConfidentialClientApplication.cjs +163 -163
  39. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  40. package/dist/client/ConfidentialClientApplication.mjs +163 -163
  41. package/dist/client/DeviceCodeClient.cjs +207 -207
  42. package/dist/client/DeviceCodeClient.d.ts +53 -53
  43. package/dist/client/DeviceCodeClient.mjs +207 -207
  44. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  45. package/dist/client/IPublicClientApplication.d.ts +42 -42
  46. package/dist/client/ManagedIdentityApplication.cjs +79 -78
  47. package/dist/client/ManagedIdentityApplication.cjs.map +1 -1
  48. package/dist/client/ManagedIdentityApplication.d.ts +23 -23
  49. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -1
  50. package/dist/client/ManagedIdentityApplication.mjs +79 -78
  51. package/dist/client/ManagedIdentityApplication.mjs.map +1 -1
  52. package/dist/client/ManagedIdentityClient.cjs +40 -36
  53. package/dist/client/ManagedIdentityClient.cjs.map +1 -1
  54. package/dist/client/ManagedIdentityClient.d.ts +19 -24
  55. package/dist/client/ManagedIdentityClient.d.ts.map +1 -1
  56. package/dist/client/ManagedIdentityClient.mjs +40 -36
  57. package/dist/client/ManagedIdentityClient.mjs.map +1 -1
  58. package/dist/client/ManagedIdentitySources/AppService.cjs +42 -69
  59. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -1
  60. package/dist/client/ManagedIdentitySources/AppService.d.ts +16 -16
  61. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -1
  62. package/dist/client/ManagedIdentitySources/AppService.mjs +43 -70
  63. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -1
  64. package/dist/client/ManagedIdentitySources/AzureArc.cjs +81 -92
  65. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -1
  66. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +18 -18
  67. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
  68. package/dist/client/ManagedIdentitySources/AzureArc.mjs +84 -95
  69. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -1
  70. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +112 -77
  71. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -1
  72. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +30 -18
  73. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
  74. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +114 -80
  75. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
  76. package/dist/client/ManagedIdentitySources/CloudShell.cjs +47 -0
  77. package/dist/client/ManagedIdentitySources/CloudShell.cjs.map +1 -0
  78. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +15 -6
  79. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -1
  80. package/dist/client/ManagedIdentitySources/CloudShell.mjs +45 -0
  81. package/dist/client/ManagedIdentitySources/CloudShell.mjs.map +1 -0
  82. package/dist/client/ManagedIdentitySources/Imds.cjs +43 -52
  83. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -1
  84. package/dist/client/ManagedIdentitySources/Imds.d.ts +12 -12
  85. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -1
  86. package/dist/client/ManagedIdentitySources/Imds.mjs +44 -53
  87. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -1
  88. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs +61 -0
  89. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs.map +1 -0
  90. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +16 -6
  91. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -1
  92. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +59 -0
  93. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs.map +1 -0
  94. package/dist/client/OnBehalfOfClient.cjs +202 -202
  95. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  96. package/dist/client/OnBehalfOfClient.mjs +202 -202
  97. package/dist/client/PublicClientApplication.cjs +229 -229
  98. package/dist/client/PublicClientApplication.d.ts +71 -71
  99. package/dist/client/PublicClientApplication.mjs +229 -229
  100. package/dist/client/UsernamePasswordClient.cjs +95 -95
  101. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  102. package/dist/client/UsernamePasswordClient.mjs +95 -95
  103. package/dist/config/Configuration.cjs +102 -86
  104. package/dist/config/Configuration.cjs.map +1 -1
  105. package/dist/config/Configuration.d.ts +123 -122
  106. package/dist/config/Configuration.d.ts.map +1 -1
  107. package/dist/config/Configuration.mjs +102 -86
  108. package/dist/config/Configuration.mjs.map +1 -1
  109. package/dist/config/ManagedIdentityId.cjs +48 -48
  110. package/dist/config/ManagedIdentityId.d.ts +11 -11
  111. package/dist/config/ManagedIdentityId.mjs +48 -48
  112. package/dist/config/ManagedIdentityRequestParameters.cjs +28 -29
  113. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -1
  114. package/dist/config/ManagedIdentityRequestParameters.d.ts +11 -11
  115. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -1
  116. package/dist/config/ManagedIdentityRequestParameters.mjs +28 -29
  117. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -1
  118. package/dist/crypto/CryptoProvider.cjs +75 -75
  119. package/dist/crypto/CryptoProvider.d.ts +52 -52
  120. package/dist/crypto/CryptoProvider.mjs +75 -75
  121. package/dist/crypto/GuidGenerator.cjs +22 -22
  122. package/dist/crypto/GuidGenerator.d.ts +14 -14
  123. package/dist/crypto/GuidGenerator.mjs +22 -22
  124. package/dist/crypto/HashUtils.cjs +13 -13
  125. package/dist/crypto/HashUtils.d.ts +8 -8
  126. package/dist/crypto/HashUtils.mjs +13 -13
  127. package/dist/crypto/PkceGenerator.cjs +49 -49
  128. package/dist/crypto/PkceGenerator.d.ts +22 -22
  129. package/dist/crypto/PkceGenerator.mjs +49 -49
  130. package/dist/error/ManagedIdentityError.cjs +39 -28
  131. package/dist/error/ManagedIdentityError.cjs.map +1 -1
  132. package/dist/error/ManagedIdentityError.d.ts +27 -21
  133. package/dist/error/ManagedIdentityError.d.ts.map +1 -1
  134. package/dist/error/ManagedIdentityError.mjs +40 -29
  135. package/dist/error/ManagedIdentityError.mjs.map +1 -1
  136. package/dist/error/ManagedIdentityErrorCodes.cjs +27 -14
  137. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -1
  138. package/dist/error/ManagedIdentityErrorCodes.d.ts +19 -9
  139. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -1
  140. package/dist/error/ManagedIdentityErrorCodes.mjs +24 -14
  141. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -1
  142. package/dist/error/NodeAuthError.cjs +85 -85
  143. package/dist/error/NodeAuthError.d.ts +65 -65
  144. package/dist/error/NodeAuthError.mjs +85 -85
  145. package/dist/index.cjs +1 -1
  146. package/dist/index.d.ts +44 -44
  147. package/dist/index.mjs +1 -1
  148. package/dist/internals.cjs +8 -8
  149. package/dist/internals.d.ts +6 -6
  150. package/dist/internals.mjs +8 -8
  151. package/dist/network/HttpClient.cjs +283 -283
  152. package/dist/network/HttpClient.cjs.map +1 -1
  153. package/dist/network/HttpClient.d.ts +25 -26
  154. package/dist/network/HttpClient.d.ts.map +1 -1
  155. package/dist/network/HttpClient.mjs +283 -283
  156. package/dist/network/HttpClient.mjs.map +1 -1
  157. package/dist/network/HttpClientWithRetries.cjs +44 -0
  158. package/dist/network/HttpClientWithRetries.cjs.map +1 -0
  159. package/dist/network/HttpClientWithRetries.d.ts +12 -0
  160. package/dist/network/HttpClientWithRetries.d.ts.map +1 -0
  161. package/dist/network/HttpClientWithRetries.mjs +42 -0
  162. package/dist/network/HttpClientWithRetries.mjs.map +1 -0
  163. package/dist/network/ILoopbackClient.d.ts +10 -10
  164. package/dist/network/LoopbackClient.cjs +77 -77
  165. package/dist/network/LoopbackClient.d.ts +21 -21
  166. package/dist/network/LoopbackClient.mjs +77 -77
  167. package/dist/packageMetadata.cjs +4 -4
  168. package/dist/packageMetadata.d.ts +2 -2
  169. package/dist/packageMetadata.d.ts.map +1 -1
  170. package/dist/packageMetadata.mjs +4 -4
  171. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  172. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  173. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  174. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  175. package/dist/request/InteractiveRequest.d.ts +22 -22
  176. package/dist/request/ManagedIdentityRequest.d.ts +8 -8
  177. package/dist/request/ManagedIdentityRequestParams.d.ts +9 -9
  178. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  179. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  180. package/dist/request/SignOutRequest.d.ts +5 -5
  181. package/dist/request/SilentFlowRequest.d.ts +16 -16
  182. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  183. package/dist/response/ManagedIdentityTokenResponse.d.ts +24 -24
  184. package/dist/retry/IHttpRetryPolicy.d.ts +6 -0
  185. package/dist/retry/IHttpRetryPolicy.d.ts.map +1 -0
  186. package/dist/retry/LinearRetryPolicy.cjs +47 -0
  187. package/dist/retry/LinearRetryPolicy.cjs.map +1 -0
  188. package/dist/retry/LinearRetryPolicy.d.ts +12 -0
  189. package/dist/retry/LinearRetryPolicy.d.ts.map +1 -0
  190. package/dist/retry/LinearRetryPolicy.mjs +45 -0
  191. package/dist/retry/LinearRetryPolicy.mjs.map +1 -0
  192. package/dist/utils/Constants.cjs +141 -103
  193. package/dist/utils/Constants.cjs.map +1 -1
  194. package/dist/utils/Constants.d.ts +131 -106
  195. package/dist/utils/Constants.d.ts.map +1 -1
  196. package/dist/utils/Constants.mjs +133 -100
  197. package/dist/utils/Constants.mjs.map +1 -1
  198. package/dist/utils/EncodingUtils.cjs +44 -44
  199. package/dist/utils/EncodingUtils.d.ts +26 -26
  200. package/dist/utils/EncodingUtils.mjs +44 -44
  201. package/dist/utils/NetworkUtils.cjs +38 -38
  202. package/dist/utils/NetworkUtils.d.ts +5 -5
  203. package/dist/utils/NetworkUtils.mjs +38 -38
  204. package/package.json +5 -5
  205. package/src/cache/NodeStorage.ts +4 -9
  206. package/src/cache/serializer/Deserializer.ts +1 -5
  207. package/src/client/ClientApplication.ts +5 -4
  208. package/src/client/ManagedIdentityApplication.ts +3 -3
  209. package/src/client/ManagedIdentityClient.ts +23 -11
  210. package/src/client/ManagedIdentitySources/AppService.ts +68 -97
  211. package/src/client/ManagedIdentitySources/AzureArc.ts +59 -68
  212. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +70 -2
  213. package/src/client/ManagedIdentitySources/CloudShell.ts +93 -8
  214. package/src/client/ManagedIdentitySources/Imds.ts +54 -40
  215. package/src/client/ManagedIdentitySources/ServiceFabric.ts +126 -5
  216. package/src/config/Configuration.ts +44 -16
  217. package/src/config/ManagedIdentityRequestParameters.ts +1 -6
  218. package/src/error/ManagedIdentityError.ts +15 -2
  219. package/src/error/ManagedIdentityErrorCodes.ts +19 -0
  220. package/src/network/HttpClient.ts +6 -11
  221. package/src/network/HttpClientWithRetries.ts +80 -0
  222. package/src/packageMetadata.ts +1 -1
  223. package/src/retry/IHttpRetryPolicy.ts +18 -0
  224. package/src/retry/LinearRetryPolicy.ts +71 -0
  225. package/src/utils/Constants.ts +43 -7
@@ -12,10 +12,8 @@ import {
12
12
  NetworkResponse,
13
13
  NetworkRequestOptions,
14
14
  Logger,
15
- UrlString,
16
15
  ServerAuthorizationTokenResponse,
17
16
  } from "@azure/msal-common";
18
- import { ManagedIdentityId } from "../../config/ManagedIdentityId";
19
17
  import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
20
18
  import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
21
19
  import { CryptoProvider } from "../../crypto/CryptoProvider";
@@ -24,14 +22,19 @@ import {
24
22
  createManagedIdentityError,
25
23
  } from "../../error/ManagedIdentityError";
26
24
  import {
25
+ API_VERSION_QUERY_PARAMETER_NAME,
27
26
  AUTHORIZATION_HEADER_NAME,
28
27
  HttpMethod,
29
28
  METADATA_HEADER_NAME,
29
+ ManagedIdentityEnvironmentVariableNames,
30
30
  ManagedIdentityIdType,
31
+ ManagedIdentitySourceNames,
32
+ RESOURCE_BODY_OR_QUERY_PARAMETER_NAME,
31
33
  } from "../../utils/Constants";
32
34
  import { NodeStorage } from "../../cache/NodeStorage";
33
35
  import { readFileSync } from "fs";
34
36
  import { ManagedIdentityTokenResponse } from "../../response/ManagedIdentityTokenResponse";
37
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
35
38
 
36
39
  export const ARC_API_VERSION: string = "2019-11-01";
37
40
 
@@ -39,52 +42,65 @@ export const ARC_API_VERSION: string = "2019-11-01";
39
42
  * Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/AzureArcManagedIdentitySource.cs
40
43
  */
41
44
  export class AzureArc extends BaseManagedIdentitySource {
42
- private endpoint: string;
45
+ private identityEndpoint: string;
43
46
 
44
47
  constructor(
45
48
  logger: Logger,
46
49
  nodeStorage: NodeStorage,
47
50
  networkClient: INetworkModule,
48
51
  cryptoProvider: CryptoProvider,
49
- endpoint: string
52
+ identityEndpoint: string
50
53
  ) {
51
54
  super(logger, nodeStorage, networkClient, cryptoProvider);
52
55
 
53
- this.endpoint = endpoint;
56
+ this.identityEndpoint = identityEndpoint;
54
57
  }
55
58
 
56
59
  public static tryCreate(
57
60
  logger: Logger,
58
61
  nodeStorage: NodeStorage,
59
62
  networkClient: INetworkModule,
60
- cryptoProvider: CryptoProvider
63
+ cryptoProvider: CryptoProvider,
64
+ managedIdentityId: ManagedIdentityId
61
65
  ): AzureArc | null {
62
- const imdsEndpoint: string | undefined = process.env["IMDS_ENDPOINT"];
66
+ const identityEndpoint: string | undefined =
67
+ process.env[
68
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT
69
+ ];
70
+ const imdsEndpoint: string | undefined =
71
+ process.env[ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT];
72
+
73
+ // if either of the identity or imds endpoints are undefined, this MSI provider is unavailable.
74
+ if (!identityEndpoint || !imdsEndpoint) {
75
+ logger.info(
76
+ `[Managed Identity] ${ManagedIdentitySourceNames.AZURE_ARC} managed identity is unavailable because one or both of the '${ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT}' and '${ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT}' environment variables are not defined.`
77
+ );
78
+ return null;
79
+ }
80
+
81
+ const validatedIdentityEndpoint: string =
82
+ AzureArc.getValidatedEnvVariableUrlString(
83
+ ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT,
84
+ identityEndpoint,
85
+ ManagedIdentitySourceNames.AZURE_ARC,
86
+ logger
87
+ );
88
+ // remove trailing slash
89
+ validatedIdentityEndpoint.endsWith("/")
90
+ ? validatedIdentityEndpoint.slice(0, -1)
91
+ : validatedIdentityEndpoint;
63
92
 
64
- const [areEnvironmentVariablesValidated, endpoint]: [
65
- boolean,
66
- string | undefined
67
- ] = validateEnvironmentVariables(
68
- process.env["IDENTITY_ENDPOINT"] || undefined,
93
+ AzureArc.getValidatedEnvVariableUrlString(
94
+ ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT,
69
95
  imdsEndpoint,
96
+ ManagedIdentitySourceNames.AZURE_ARC,
70
97
  logger
71
98
  );
72
99
 
73
- return areEnvironmentVariablesValidated
74
- ? new AzureArc(
75
- logger,
76
- nodeStorage,
77
- networkClient,
78
- cryptoProvider,
79
- endpoint as string
80
- )
81
- : null;
82
- }
100
+ logger.info(
101
+ `[Managed Identity] Environment variables validation passed for ${ManagedIdentitySourceNames.AZURE_ARC} managed identity. Endpoint URI: ${validatedIdentityEndpoint}. Creating ${ManagedIdentitySourceNames.AZURE_ARC} managed identity.`
102
+ );
83
103
 
84
- public createRequest(
85
- resource: string,
86
- managedIdentityId: ManagedIdentityId
87
- ): ManagedIdentityRequestParameters {
88
104
  if (
89
105
  managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
90
106
  ) {
@@ -93,15 +109,29 @@ export class AzureArc extends BaseManagedIdentitySource {
93
109
  );
94
110
  }
95
111
 
112
+ return new AzureArc(
113
+ logger,
114
+ nodeStorage,
115
+ networkClient,
116
+ cryptoProvider,
117
+ identityEndpoint
118
+ );
119
+ }
120
+
121
+ public createRequest(resource: string): ManagedIdentityRequestParameters {
96
122
  const request: ManagedIdentityRequestParameters =
97
123
  new ManagedIdentityRequestParameters(
98
124
  HttpMethod.GET,
99
- this.endpoint.replace("localhost", "127.0.0.1")
125
+ this.identityEndpoint.replace("localhost", "127.0.0.1")
100
126
  );
101
127
 
102
128
  request.headers[METADATA_HEADER_NAME] = "true";
103
- request.queryParameters["api-version"] = ARC_API_VERSION;
104
- request.queryParameters["resource"] = resource;
129
+
130
+ request.queryParameters[API_VERSION_QUERY_PARAMETER_NAME] =
131
+ ARC_API_VERSION;
132
+ request.queryParameters[RESOURCE_BODY_OR_QUERY_PARAMETER_NAME] =
133
+ resource;
134
+
105
135
  // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
106
136
 
107
137
  return request;
@@ -167,42 +197,3 @@ export class AzureArc extends BaseManagedIdentitySource {
167
197
  return this.getServerTokenResponse(retryResponse || originalResponse);
168
198
  }
169
199
  }
170
-
171
- const validateEnvironmentVariables = (
172
- identityEndpoint: string | undefined,
173
- imdsEndpoint: string | undefined,
174
- logger: Logger
175
- ): [boolean, string | undefined] => {
176
- let endpointUrlString: string | undefined;
177
-
178
- // if either of the identity or imds endpoints are undefined, this MSI provider is unavailable.
179
- if (!identityEndpoint || !imdsEndpoint) {
180
- logger.info(
181
- "[Managed Identity] Azure Arc managed identity is unavailable because one or both of the 'IDENTITY_ENDPOINT' and 'IMDS_ENDPOINT' environment variables are missing."
182
- );
183
- return [false, endpointUrlString];
184
- }
185
-
186
- try {
187
- endpointUrlString = new UrlString(identityEndpoint).urlString;
188
- } catch (error) {
189
- logger.info(
190
- "[Managed Identity] App service managed identity is unavailable because the 'IDENTITY_ENDPOINT' environment variable is malformed."
191
- );
192
-
193
- throw createManagedIdentityError(
194
- ManagedIdentityErrorCodes.urlParseError
195
- );
196
- }
197
-
198
- logger.info(
199
- `[Managed Identity] Environment variables validation passed for Azure Arc managed identity. Endpoint URI: ${endpointUrlString}. Creating Azure Arc managed identity.`
200
- );
201
- return [
202
- true,
203
- // remove trailing slash
204
- endpointUrlString.endsWith("/")
205
- ? endpointUrlString.slice(0, -1)
206
- : endpointUrlString,
207
- ];
208
- };
@@ -18,14 +18,30 @@ import {
18
18
  TimeUtils,
19
19
  createClientAuthError,
20
20
  AuthenticationResult,
21
+ UrlString,
21
22
  } from "@azure/msal-common";
22
23
  import { ManagedIdentityId } from "../../config/ManagedIdentityId";
23
24
  import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
24
25
  import { CryptoProvider } from "../../crypto/CryptoProvider";
25
26
  import { ManagedIdentityRequest } from "../../request/ManagedIdentityRequest";
26
- import { HttpMethod } from "../../utils/Constants";
27
+ import { HttpMethod, ManagedIdentityIdType } from "../../utils/Constants";
27
28
  import { ManagedIdentityTokenResponse } from "../../response/ManagedIdentityTokenResponse";
28
29
  import { NodeStorage } from "../../cache/NodeStorage";
30
+ import {
31
+ ManagedIdentityErrorCodes,
32
+ createManagedIdentityError,
33
+ } from "../../error/ManagedIdentityError";
34
+
35
+ /**
36
+ * Managed Identity User Assigned Id Query Parameter Names
37
+ */
38
+ export const ManagedIdentityUserAssignedIdQueryParameterNames = {
39
+ MANAGED_IDENTITY_CLIENT_ID: "client_id",
40
+ MANAGED_IDENTITY_OBJECT_ID: "object_id",
41
+ MANAGED_IDENTITY_RESOURCE_ID: "mi_res_id",
42
+ } as const;
43
+ export type ManagedIdentityUserAssignedIdQueryParameterNames =
44
+ (typeof ManagedIdentityUserAssignedIdQueryParameterNames)[keyof typeof ManagedIdentityUserAssignedIdQueryParameterNames];
29
45
 
30
46
  export abstract class BaseManagedIdentitySource {
31
47
  protected logger: Logger;
@@ -104,8 +120,10 @@ export abstract class BaseManagedIdentitySource {
104
120
 
105
121
  const headers: Record<string, string> = networkRequest.headers;
106
122
  headers[HeaderNames.CONTENT_TYPE] = Constants.URL_FORM_CONTENT_TYPE;
123
+
107
124
  const networkRequestOptions: NetworkRequestOptions = { headers };
108
- if (managedIdentityRequest.forceRefresh) {
125
+
126
+ if (Object.keys(networkRequest.bodyParameters).length) {
109
127
  networkRequestOptions.body =
110
128
  networkRequest.computeParametersBodyString();
111
129
  }
@@ -166,4 +184,54 @@ export abstract class BaseManagedIdentitySource {
166
184
  managedIdentityRequest
167
185
  );
168
186
  }
187
+
188
+ public getManagedIdentityUserAssignedIdQueryParameterKey(
189
+ managedIdentityIdType: ManagedIdentityIdType
190
+ ): string {
191
+ switch (managedIdentityIdType) {
192
+ case ManagedIdentityIdType.USER_ASSIGNED_CLIENT_ID:
193
+ this.logger.info(
194
+ "[Managed Identity] Adding user assigned client id to the request."
195
+ );
196
+ return ManagedIdentityUserAssignedIdQueryParameterNames.MANAGED_IDENTITY_CLIENT_ID;
197
+
198
+ case ManagedIdentityIdType.USER_ASSIGNED_RESOURCE_ID:
199
+ this.logger.info(
200
+ "[Managed Identity] Adding user assigned resource id to the request."
201
+ );
202
+ return ManagedIdentityUserAssignedIdQueryParameterNames.MANAGED_IDENTITY_RESOURCE_ID;
203
+
204
+ case ManagedIdentityIdType.USER_ASSIGNED_OBJECT_ID:
205
+ this.logger.info(
206
+ "[Managed Identity] Adding user assigned object id to the request."
207
+ );
208
+ return ManagedIdentityUserAssignedIdQueryParameterNames.MANAGED_IDENTITY_OBJECT_ID;
209
+ default:
210
+ throw createManagedIdentityError(
211
+ ManagedIdentityErrorCodes.invalidManagedIdentityIdType
212
+ );
213
+ }
214
+ }
215
+
216
+ public static getValidatedEnvVariableUrlString = (
217
+ envVariableStringName: string,
218
+ envVariable: string,
219
+ sourceName: string,
220
+ logger: Logger
221
+ ): string => {
222
+ try {
223
+ return new UrlString(envVariable).urlString;
224
+ } catch (error) {
225
+ logger.info(
226
+ `[Managed Identity] ${sourceName} managed identity is unavailable because the '${envVariableStringName}' environment variable is malformed.`
227
+ );
228
+
229
+ throw createManagedIdentityError(
230
+ ManagedIdentityErrorCodes
231
+ .MsiEnvironmentVariableUrlMalformedErrorCodes[
232
+ envVariableStringName
233
+ ]
234
+ );
235
+ }
236
+ };
169
237
  }
@@ -3,17 +3,102 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { ManagedIdentityId } from "../../config/ManagedIdentityId";
6
+ import { INetworkModule, Logger } from "@azure/msal-common";
7
7
  import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters";
8
8
  import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
9
+ import { NodeStorage } from "../../cache/NodeStorage";
10
+ import { CryptoProvider } from "../../crypto/CryptoProvider";
11
+ import {
12
+ HttpMethod,
13
+ METADATA_HEADER_NAME,
14
+ ManagedIdentityEnvironmentVariableNames,
15
+ ManagedIdentityIdType,
16
+ ManagedIdentitySourceNames,
17
+ RESOURCE_BODY_OR_QUERY_PARAMETER_NAME,
18
+ } from "../../utils/Constants";
19
+ import {
20
+ ManagedIdentityErrorCodes,
21
+ createManagedIdentityError,
22
+ } from "../../error/ManagedIdentityError";
23
+ import { ManagedIdentityId } from "../../config/ManagedIdentityId";
9
24
 
25
+ /**
26
+ * Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/CloudShellManagedIdentitySource.cs
27
+ */
10
28
  export class CloudShell extends BaseManagedIdentitySource {
11
- public createRequest(
12
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
- _resource: string,
14
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
- _managedIdentityId: ManagedIdentityId
16
- ): ManagedIdentityRequestParameters {
17
- throw new Error("Method not implemented.");
29
+ private msiEndpoint: string;
30
+
31
+ constructor(
32
+ logger: Logger,
33
+ nodeStorage: NodeStorage,
34
+ networkClient: INetworkModule,
35
+ cryptoProvider: CryptoProvider,
36
+ msiEndpoint: string
37
+ ) {
38
+ super(logger, nodeStorage, networkClient, cryptoProvider);
39
+
40
+ this.msiEndpoint = msiEndpoint;
41
+ }
42
+
43
+ public static tryCreate(
44
+ logger: Logger,
45
+ nodeStorage: NodeStorage,
46
+ networkClient: INetworkModule,
47
+ cryptoProvider: CryptoProvider,
48
+ managedIdentityId: ManagedIdentityId
49
+ ): CloudShell | null {
50
+ const msiEndpoint: string | undefined =
51
+ process.env[ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT];
52
+
53
+ // if the msi endpoint environment variable is undefined, this MSI provider is unavailable.
54
+ if (!msiEndpoint) {
55
+ logger.info(
56
+ `[Managed Identity] ${ManagedIdentitySourceNames.CLOUD_SHELL} managed identity is unavailable because the '${ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT} environment variable is not defined.`
57
+ );
58
+ return null;
59
+ }
60
+
61
+ const validatedMsiEndpoint: string =
62
+ CloudShell.getValidatedEnvVariableUrlString(
63
+ ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT,
64
+ msiEndpoint,
65
+ ManagedIdentitySourceNames.CLOUD_SHELL,
66
+ logger
67
+ );
68
+
69
+ logger.info(
70
+ `[Managed Identity] Environment variable validation passed for ${ManagedIdentitySourceNames.CLOUD_SHELL} managed identity. Endpoint URI: ${validatedMsiEndpoint}. Creating ${ManagedIdentitySourceNames.CLOUD_SHELL} managed identity.`
71
+ );
72
+
73
+ if (
74
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
75
+ ) {
76
+ throw createManagedIdentityError(
77
+ ManagedIdentityErrorCodes.unableToCreateCloudShell
78
+ );
79
+ }
80
+
81
+ return new CloudShell(
82
+ logger,
83
+ nodeStorage,
84
+ networkClient,
85
+ cryptoProvider,
86
+ msiEndpoint
87
+ );
88
+ }
89
+
90
+ public createRequest(resource: string): ManagedIdentityRequestParameters {
91
+ const request: ManagedIdentityRequestParameters =
92
+ new ManagedIdentityRequestParameters(
93
+ HttpMethod.POST,
94
+ this.msiEndpoint
95
+ );
96
+
97
+ request.headers[METADATA_HEADER_NAME] = "true";
98
+
99
+ request.bodyParameters[RESOURCE_BODY_OR_QUERY_PARAMETER_NAME] =
100
+ resource;
101
+
102
+ return request;
18
103
  }
19
104
  }
@@ -9,12 +9,13 @@ import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRe
9
9
  import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource";
10
10
  import { CryptoProvider } from "../../crypto/CryptoProvider";
11
11
  import {
12
+ API_VERSION_QUERY_PARAMETER_NAME,
12
13
  HttpMethod,
13
- MANAGED_IDENTITY_CLIENT_ID,
14
- MANAGED_IDENTITY_OBJECT_ID,
15
- MANAGED_IDENTITY_RESOURCE_ID,
16
14
  METADATA_HEADER_NAME,
15
+ ManagedIdentityEnvironmentVariableNames,
17
16
  ManagedIdentityIdType,
17
+ ManagedIdentitySourceNames,
18
+ RESOURCE_BODY_OR_QUERY_PARAMETER_NAME,
18
19
  } from "../../utils/Constants";
19
20
  import { NodeStorage } from "../../cache/NodeStorage";
20
21
 
@@ -26,18 +27,18 @@ const IMDS_API_VERSION: string = "2018-02-01";
26
27
 
27
28
  // Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/ImdsManagedIdentitySource.cs
28
29
  export class Imds extends BaseManagedIdentitySource {
29
- private endpoint: string;
30
+ private identityEndpoint: string;
30
31
 
31
32
  constructor(
32
33
  logger: Logger,
33
34
  nodeStorage: NodeStorage,
34
35
  networkClient: INetworkModule,
35
36
  cryptoProvider: CryptoProvider,
36
- endpoint: string
37
+ identityEndpoint: string
37
38
  ) {
38
39
  super(logger, nodeStorage, networkClient, cryptoProvider);
39
40
 
40
- this.endpoint = endpoint;
41
+ this.identityEndpoint = identityEndpoint;
41
42
  }
42
43
 
43
44
  public static tryCreate(
@@ -46,18 +47,40 @@ export class Imds extends BaseManagedIdentitySource {
46
47
  networkClient: INetworkModule,
47
48
  cryptoProvider: CryptoProvider
48
49
  ): Imds {
49
- let endpoint: string;
50
+ let validatedIdentityEndpoint: string;
50
51
 
51
- if (process.env["AZURE_POD_IDENTITY_AUTHORITY_HOST"]) {
52
+ if (
53
+ process.env[
54
+ ManagedIdentityEnvironmentVariableNames
55
+ .AZURE_POD_IDENTITY_AUTHORITY_HOST
56
+ ]
57
+ ) {
52
58
  logger.info(
53
- `[Managed Identity] Environment variable AZURE_POD_IDENTITY_AUTHORITY_HOST for IMDS returned endpoint: ${process.env["AZURE_POD_IDENTITY_AUTHORITY_HOST"]}`
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
54
78
  );
55
- endpoint = `${process.env["AZURE_POD_IDENTITY_AUTHORITY_HOST"]}${IMDS_TOKEN_PATH}`;
56
79
  } else {
57
80
  logger.info(
58
- "[Managed Identity] Unable to find AZURE_POD_IDENTITY_AUTHORITY_HOST environment variable for IMDS, using the default endpoint."
81
+ `[Managed Identity] Unable to find ${ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST} environment variable for ${ManagedIdentitySourceNames.IMDS}, using the default endpoint.`
59
82
  );
60
- endpoint = DEFAULT_IMDS_ENDPOINT;
83
+ validatedIdentityEndpoint = DEFAULT_IMDS_ENDPOINT;
61
84
  }
62
85
 
63
86
  return new Imds(
@@ -65,7 +88,7 @@ export class Imds extends BaseManagedIdentitySource {
65
88
  nodeStorage,
66
89
  networkClient,
67
90
  cryptoProvider,
68
- endpoint
91
+ validatedIdentityEndpoint
69
92
  );
70
93
  }
71
94
 
@@ -74,39 +97,30 @@ export class Imds extends BaseManagedIdentitySource {
74
97
  managedIdentityId: ManagedIdentityId
75
98
  ): ManagedIdentityRequestParameters {
76
99
  const request: ManagedIdentityRequestParameters =
77
- new ManagedIdentityRequestParameters(HttpMethod.GET, this.endpoint);
100
+ new ManagedIdentityRequestParameters(
101
+ HttpMethod.GET,
102
+ this.identityEndpoint
103
+ );
78
104
 
79
105
  request.headers[METADATA_HEADER_NAME] = "true";
80
- request.queryParameters["api-version"] = IMDS_API_VERSION;
81
- request.queryParameters["resource"] = resource;
82
- // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
83
106
 
84
- switch (managedIdentityId.idType) {
85
- case ManagedIdentityIdType.USER_ASSIGNED_CLIENT_ID:
86
- this.logger.info(
87
- "[Managed Identity] Adding user assigned client id to the request."
88
- );
89
- request.queryParameters[MANAGED_IDENTITY_CLIENT_ID] =
90
- managedIdentityId.id;
91
- break;
107
+ request.queryParameters[API_VERSION_QUERY_PARAMETER_NAME] =
108
+ IMDS_API_VERSION;
109
+ request.queryParameters[RESOURCE_BODY_OR_QUERY_PARAMETER_NAME] =
110
+ resource;
92
111
 
93
- case ManagedIdentityIdType.USER_ASSIGNED_RESOURCE_ID:
94
- this.logger.info(
95
- "[Managed Identity] Adding user assigned resource id to the request."
96
- );
97
- request.queryParameters[MANAGED_IDENTITY_RESOURCE_ID] =
98
- managedIdentityId.id;
99
- break;
100
-
101
- case ManagedIdentityIdType.USER_ASSIGNED_OBJECT_ID:
102
- this.logger.info(
103
- "[Managed Identity] Adding user assigned object id to the request."
104
- );
105
- request.queryParameters[MANAGED_IDENTITY_OBJECT_ID] =
106
- managedIdentityId.id;
107
- break;
112
+ if (
113
+ managedIdentityId.idType !== ManagedIdentityIdType.SYSTEM_ASSIGNED
114
+ ) {
115
+ request.queryParameters[
116
+ this.getManagedIdentityUserAssignedIdQueryParameterKey(
117
+ managedIdentityId.idType
118
+ )
119
+ ] = managedIdentityId.id;
108
120
  }
109
121
 
122
+ // bodyParameters calculated in BaseManagedIdentity.acquireTokenWithManagedIdentity
123
+
110
124
  return request;
111
125
  }
112
126
  }