@azure/identity 3.2.0-alpha.20230227.4 → 3.2.0-alpha.20230302.1

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.

Potentially problematic release.


This version of @azure/identity might be problematic. Click here for more details.

Files changed (29) hide show
  1. package/dist/index.js +126 -76
  2. package/dist/index.js.map +1 -1
  3. package/dist-esm/src/credentials/clientAssertionCredential.js +1 -1
  4. package/dist-esm/src/credentials/clientAssertionCredential.js.map +1 -1
  5. package/dist-esm/src/credentials/clientCertificateCredential.js +1 -1
  6. package/dist-esm/src/credentials/clientCertificateCredential.js.map +1 -1
  7. package/dist-esm/src/credentials/clientSecretCredential.js +1 -1
  8. package/dist-esm/src/credentials/clientSecretCredential.js.map +1 -1
  9. package/dist-esm/src/credentials/defaultAzureCredential.js +6 -4
  10. package/dist-esm/src/credentials/defaultAzureCredential.js.map +1 -1
  11. package/dist-esm/src/credentials/defaultAzureCredentialOptions.js.map +1 -1
  12. package/dist-esm/src/credentials/deviceCodeCredential.js +1 -1
  13. package/dist-esm/src/credentials/deviceCodeCredential.js.map +1 -1
  14. package/dist-esm/src/credentials/interactiveBrowserCredential.js +1 -1
  15. package/dist-esm/src/credentials/interactiveBrowserCredential.js.map +1 -1
  16. package/dist-esm/src/credentials/managedIdentityCredential/tokenExchangeMsi.js.map +1 -1
  17. package/dist-esm/src/credentials/onBehalfOfCredential.js +1 -1
  18. package/dist-esm/src/credentials/onBehalfOfCredential.js.map +1 -1
  19. package/dist-esm/src/credentials/usernamePasswordCredential.js +1 -1
  20. package/dist-esm/src/credentials/usernamePasswordCredential.js.map +1 -1
  21. package/dist-esm/src/credentials/visualStudioCodeCredential.js +1 -2
  22. package/dist-esm/src/credentials/visualStudioCodeCredential.js.map +1 -1
  23. package/dist-esm/src/credentials/workloadIdentityCredential.js +60 -10
  24. package/dist-esm/src/credentials/workloadIdentityCredential.js.map +1 -1
  25. package/dist-esm/src/credentials/workloadIdentityCredentialOptions.js.map +1 -1
  26. package/dist-esm/src/util/processMultiTenantRequest.js +7 -3
  27. package/dist-esm/src/util/processMultiTenantRequest.js.map +1 -1
  28. package/package.json +2 -2
  29. package/types/identity.d.ts +14 -6
@@ -2,6 +2,23 @@
2
2
  // Licensed under the MIT license.
3
3
  import { ClientAssertionCredential } from "./clientAssertionCredential";
4
4
  import { readFile } from "fs/promises";
5
+ import { CredentialUnavailableError } from "../errors";
6
+ import { credentialLogger, processEnvVars } from "../util/logging";
7
+ import { checkTenantId } from "../util/tenantIdUtils";
8
+ const credentialName = "WorkloadIdentityCredential";
9
+ /**
10
+ * Contains the list of all supported environment variable names so that an
11
+ * appropriate error message can be generated when no credentials can be
12
+ * configured.
13
+ *
14
+ * @internal
15
+ */
16
+ export const SupportedWorkloadEnvironmentVariables = [
17
+ "AZURE_TENANT_ID",
18
+ "AZURE_CLIENT_ID",
19
+ "AZURE_FEDERATED_TOKEN_FILE",
20
+ ];
21
+ const logger = credentialLogger(credentialName);
5
22
  /**
6
23
  * WorkloadIdentityCredential supports Azure workload identity authentication on Kubernetes.
7
24
  * Refer to <a href="https://learn.microsoft.com/azure/aks/workload-identity-overview">Azure Active Directory Workload Identity</a>
@@ -9,18 +26,38 @@ import { readFile } from "fs/promises";
9
26
  */
10
27
  export class WorkloadIdentityCredential {
11
28
  /**
12
- * WorkloadIdentityCredential supports Azure workload identity on Kubernetes.
13
- *
14
- * @param options - The identity client options to use for authentication.
29
+ * @internal
30
+ * @hidden
15
31
  */
16
- constructor(options = {}) {
32
+ constructor(options) {
17
33
  this.azureFederatedTokenFileContent = undefined;
18
34
  this.cacheDate = undefined;
19
- if (!options.tenantId || !options.clientId || !options.federatedTokenFilePath) {
20
- throw new Error("WorkloadIdentityCredential: tenantId, clientId, and federatedTokenFilePath are required parameters.");
35
+ const workloadIdentityCredentialOptions = options;
36
+ if (workloadIdentityCredentialOptions.clientId &&
37
+ workloadIdentityCredentialOptions.tenantId &&
38
+ workloadIdentityCredentialOptions.federatedTokenFilePath) {
39
+ const tenantId = workloadIdentityCredentialOptions.tenantId;
40
+ if (tenantId) {
41
+ checkTenantId(logger, tenantId);
42
+ }
43
+ this.federatedTokenFilePath = workloadIdentityCredentialOptions.federatedTokenFilePath;
44
+ logger.info(`Invoking ClientAssertionCredential with tenant ID: ${tenantId}, clientId: ${workloadIdentityCredentialOptions.clientId} and federated token path: [REDACTED]`);
45
+ this.client = new ClientAssertionCredential(tenantId, workloadIdentityCredentialOptions.clientId, this.readFileContents.bind(this), options);
46
+ }
47
+ else {
48
+ // Keep track of any missing environment variables for error details
49
+ const assigned = processEnvVars(SupportedWorkloadEnvironmentVariables).assigned.join(", ");
50
+ logger.info(`Found the following environment variables: ${assigned}`);
51
+ const tenantId = process.env.AZURE_TENANT_ID, clientId = process.env.AZURE_CLIENT_ID, federatedTokenFilePath = process.env.AZURE_FEDERATED_TOKEN_FILE;
52
+ this.federatedTokenFilePath = federatedTokenFilePath;
53
+ if (tenantId) {
54
+ checkTenantId(logger, tenantId);
55
+ }
56
+ if (tenantId && clientId && federatedTokenFilePath) {
57
+ logger.info(`Invoking ClientAssertionCredential with the following environment variables tenant ID: ${tenantId}, clientId: ${clientId} and federatedTokenFilePath: [REDACTED]`);
58
+ this.client = new ClientAssertionCredential(tenantId, clientId, this.readFileContents.bind(this), options);
59
+ }
21
60
  }
22
- this.federatedTokenFilePath = options.federatedTokenFilePath;
23
- this.client = new ClientAssertionCredential(options.tenantId, options.clientId, this.readFileContents.bind(this), options);
24
61
  }
25
62
  /**
26
63
  * Authenticates with Azure Active Directory and returns an access token if successful.
@@ -30,7 +67,17 @@ export class WorkloadIdentityCredential {
30
67
  * @param options - The options used to configure any requests this
31
68
  * TokenCredential implementation might make.
32
69
  */
33
- getToken(scopes, options) {
70
+ async getToken(scopes, options) {
71
+ if (!this.client) {
72
+ const errorMessage = `${credentialName}: is unavailable. tenantId, clientId, and federatedTokenFilePath are required parameters.
73
+ In DefaultAzureCredential and ManagedIdentityCredential, these can be provided as environment variables -
74
+ "AZURE_TENANT_ID",
75
+ "AZURE_CLIENT_ID",
76
+ "AZURE_FEDERATED_TOKEN_FILE"`;
77
+ logger.info(errorMessage);
78
+ throw new CredentialUnavailableError(errorMessage);
79
+ }
80
+ logger.info("Invoking getToken() of Client Assertion Credential");
34
81
  return this.client.getToken(scopes, options);
35
82
  }
36
83
  async readFileContents() {
@@ -38,11 +85,14 @@ export class WorkloadIdentityCredential {
38
85
  if (this.cacheDate !== undefined && Date.now() - this.cacheDate >= 1000 * 60 * 5) {
39
86
  this.azureFederatedTokenFileContent = undefined;
40
87
  }
88
+ if (!this.federatedTokenFilePath) {
89
+ throw new CredentialUnavailableError(`${credentialName}: is unavailable. Invalid file path provided ${this.federatedTokenFilePath}.`);
90
+ }
41
91
  if (!this.azureFederatedTokenFileContent) {
42
92
  const file = await readFile(this.federatedTokenFilePath, "utf8");
43
93
  const value = file.trim();
44
94
  if (!value) {
45
- throw new Error(`No content on the file ${this.federatedTokenFilePath}.`);
95
+ throw new CredentialUnavailableError(`${credentialName}: is unavailable. No content on the file ${this.federatedTokenFilePath}.`);
46
96
  }
47
97
  else {
48
98
  this.azureFederatedTokenFileContent = value;
@@ -1 +1 @@
1
- {"version":3,"file":"workloadIdentityCredential.js","sourceRoot":"","sources":["../../../src/credentials/workloadIdentityCredential.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC;;;;GAIG;AACH,MAAM,OAAO,0BAA0B;IAMrC;;;;OAIG;IACH,YAAY,UAA6C,EAAE;QARnD,mCAA8B,GAAuB,SAAS,CAAC;QAC/D,cAAS,GAAuB,SAAS,CAAC;QAQhD,IAAI,CAAC,OAAO,CAAC,QAAQ,IAAI,CAAC,OAAO,CAAC,QAAQ,IAAI,CAAC,OAAO,CAAC,sBAAsB,EAAE;YAC7E,MAAM,IAA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from \"@azure/core-auth\";\nimport { ClientAssertionCredential } from \"./clientAssertionCredential\";\nimport { WorkloadIdentityCredentialOptions } from \"./workloadIdentityCredentialOptions\";\nimport { readFile } from \"fs/promises\";\n\n/**\n * WorkloadIdentityCredential supports Azure workload identity authentication on Kubernetes.\n * Refer to <a href=\"https://learn.microsoft.com/azure/aks/workload-identity-overview\">Azure Active Directory Workload Identity</a>\n * for more information.\n */\nexport class WorkloadIdentityCredential implements TokenCredential {\n private client: ClientAssertionCredential;\n private federatedTokenFilePath: string;\n private azureFederatedTokenFileContent: string | undefined = undefined;\n private cacheDate: number | undefined = undefined;\n\n /**\n * WorkloadIdentityCredential supports Azure workload identity on Kubernetes.\n *\n * @param options - The identity client options to use for authentication.\n */\n constructor(options: WorkloadIdentityCredentialOptions = {}) {\n if (!options.tenantId || !options.clientId || !options.federatedTokenFilePath) {\n throw new Error(\n \"WorkloadIdentityCredential: tenantId, clientId, and federatedTokenFilePath are required parameters.\"\n );\n }\n\n this.federatedTokenFilePath = options.federatedTokenFilePath;\n this.client = new ClientAssertionCredential(\n options.tenantId,\n options.clientId,\n this.readFileContents.bind(this),\n options\n );\n }\n\n /**\n * Authenticates with Azure Active Directory and returns an access token if successful.\n * If authentication fails, a {@link CredentialUnavailableError} 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 getToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null> {\n return this.client.getToken(scopes, options);\n }\n\n private async readFileContents(): Promise<string> {\n // Cached assertions expire after 5 minutes\n if (this.cacheDate !== undefined && Date.now() - this.cacheDate >= 1000 * 60 * 5) {\n this.azureFederatedTokenFileContent = undefined;\n }\n if (!this.azureFederatedTokenFileContent) {\n const file = await readFile(this.federatedTokenFilePath, \"utf8\");\n const value = file.trim();\n if (!value) {\n throw new Error(`No content on the file ${this.federatedTokenFilePath}.`);\n } else {\n this.azureFederatedTokenFileContent = value;\n this.cacheDate = Date.now();\n }\n }\n return this.azureFederatedTokenFileContent;\n }\n}\n"]}
1
+ {"version":3,"file":"workloadIdentityCredential.js","sourceRoot":"","sources":["../../../src/credentials/workloadIdentityCredential.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAKxE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,0BAA0B,EAAE,MAAM,WAAW,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,MAAM,cAAc,GAAG,4BAA4B,CAAC;AACpD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,qCAAqC,GAAG;IACnD,iBAAiB;IACjB,iBAAiB;IACjB,4BAA4B;CAC7B,CAAC;AACF,MAAM,MAAM,GAAG,gBAAgB,CAAC,cAAc,CAAC,CAAC;AAChD;;;;GAIG;AACH,MAAM,OAAO,0BAA0B;IAqBrC;;;OAGG;IACH,YACE,OAAqF;QAxB/E,mCAA8B,GAAuB,SAAS,CAAC;QAC/D,cAAS,GAAuB,SAAS,CAAC;QAyBhD,MAAM,iCAAiC,GAAG,OAA4C,CAAC;QAEvF,IACE,iCAAiC,CAAC,QAAQ;YAC1C,iCAAiC,CAAC,QAAQ;YAC1C,iCAAiC,CAAC,sBAAsB,EACxD;YACA,MAAM,QAAQ,GAAG,iCAAiC,CAAC,QAAQ,CAAC;YAC5D,IAAI,QAAQ,EAAE;gBACZ,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;aACjC;YACD,IAAI,CAAC,sBAAsB,GAAG,iCAAiC,CAAC,sBAAsB,CAAC;YACvF,MAAM,CAAC,IAAI,CACT,sDAAsD,QAAQ,eAAe,iCAAiC,CAAC,QAAQ,uCAAuC,CAC/J,CAAC;YACF,I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,IAAI,CAAC,oDAAoD,CAAC,CAAC;QAClE,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;IAEO,KAAK,CAAC,gBAAgB;QAC5B,2CAA2C;QAC3C,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,SAAS,IAAI,IAAI,GAAG,EAAE,GAAG,CAAC,EAAE;YAChF,IAAI,CAAC,8BAA8B,GAAG,SAAS,CAAC;SACjD;QACD,IAAI,CAAC,IAAI,CAAC,sBAAsB,EAAE;YAChC,MAAM,IAAI,0BAA0B,CAClC,GAAG,cAAc,gDAAgD,IAAI,CAAC,sBAAsB,GAAG,CAChG,CAAC;SACH;QACD,IAAI,CAAC,IAAI,CAAC,8BAA8B,EAAE;YACxC,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,sBAAsB,EAAE,MAAM,CAAC,CAAC;YACjE,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;YAC1B,IAAI,CAAC,KAAK,EAAE;gBACV,MAAM,IAAI,0BAA0B,CAClC,GAAG,cAAc,4CAA4C,IAAI,CAAC,sBAAsB,GAAG,CAC5F,CAAC;aACH;iBAAM;gBACL,IAAI,CAAC,8BAA8B,GAAG,KAAK,CAAC;gBAC5C,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;aAC7B;SACF;QACD,OAAO,IAAI,CAAC,8BAA8B,CAAC;IAC7C,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AccessToken, GetTokenOptions, TokenCredential } from \"@azure/core-auth\";\nimport { ClientAssertionCredential } from \"./clientAssertionCredential\";\nimport {\n WorkloadIdentityCredentialOptions,\n WorkloadIdentityDefaultCredentialOptions,\n} from \"./workloadIdentityCredentialOptions\";\nimport { readFile } from \"fs/promises\";\nimport { CredentialUnavailableError } from \"../errors\";\nimport { credentialLogger, processEnvVars } from \"../util/logging\";\nimport { checkTenantId } from \"../util/tenantIdUtils\";\n\nconst credentialName = \"WorkloadIdentityCredential\";\n/**\n * Contains the list of all supported environment variable names so that an\n * appropriate error message can be generated when no credentials can be\n * configured.\n *\n * @internal\n */\nexport const SupportedWorkloadEnvironmentVariables = [\n \"AZURE_TENANT_ID\",\n \"AZURE_CLIENT_ID\",\n \"AZURE_FEDERATED_TOKEN_FILE\",\n];\nconst logger = credentialLogger(credentialName);\n/**\n * WorkloadIdentityCredential supports Azure workload identity authentication on Kubernetes.\n * Refer to <a href=\"https://learn.microsoft.com/azure/aks/workload-identity-overview\">Azure Active Directory Workload Identity</a>\n * for more information.\n */\nexport class WorkloadIdentityCredential implements TokenCredential {\n private client: ClientAssertionCredential | undefined;\n private azureFederatedTokenFileContent: string | undefined = undefined;\n private cacheDate: number | undefined = undefined;\n private federatedTokenFilePath: string | undefined;\n\n /**\n * WorkloadIdentityCredential supports Azure workload identity on Kubernetes.\n *\n * @param options - The identity client options to use for authentication.\n */\n constructor(options: WorkloadIdentityCredentialOptions);\n\n /**\n * @internal\n * @hidden\n * WorkloadIdentityCredential supports Azure workload identity on Kubernetes.\n *\n * @param options - The identity client options to use for authentication.\n */\n constructor(options?: WorkloadIdentityDefaultCredentialOptions);\n /**\n * @internal\n * @hidden\n */\n constructor(\n options: WorkloadIdentityDefaultCredentialOptions | WorkloadIdentityCredentialOptions\n ) {\n const workloadIdentityCredentialOptions = options as WorkloadIdentityCredentialOptions;\n\n if (\n workloadIdentityCredentialOptions.clientId &&\n workloadIdentityCredentialOptions.tenantId &&\n workloadIdentityCredentialOptions.federatedTokenFilePath\n ) {\n const tenantId = workloadIdentityCredentialOptions.tenantId;\n if (tenantId) {\n checkTenantId(logger, tenantId);\n }\n this.federatedTokenFilePath = workloadIdentityCredentialOptions.federatedTokenFilePath;\n logger.info(\n `Invoking ClientAssertionCredential with tenant ID: ${tenantId}, clientId: ${workloadIdentityCredentialOptions.clientId} and federated token path: [REDACTED]`\n );\n this.client = new ClientAssertionCredential(\n tenantId,\n workloadIdentityCredentialOptions.clientId,\n this.readFileContents.bind(this),\n options\n );\n } else {\n // Keep track of any missing environment variables for error details\n const assigned = processEnvVars(SupportedWorkloadEnvironmentVariables).assigned.join(\", \");\n logger.info(`Found the following environment variables: ${assigned}`);\n\n const tenantId = process.env.AZURE_TENANT_ID,\n clientId = process.env.AZURE_CLIENT_ID,\n federatedTokenFilePath = process.env.AZURE_FEDERATED_TOKEN_FILE;\n\n this.federatedTokenFilePath = federatedTokenFilePath;\n if (tenantId) {\n checkTenantId(logger, tenantId);\n }\n\n if (tenantId && clientId && federatedTokenFilePath) {\n logger.info(\n `Invoking ClientAssertionCredential with the following environment variables tenant ID: ${tenantId}, clientId: ${clientId} and federatedTokenFilePath: [REDACTED]`\n );\n this.client = new ClientAssertionCredential(\n tenantId,\n clientId,\n this.readFileContents.bind(this),\n options as WorkloadIdentityDefaultCredentialOptions\n );\n }\n }\n }\n\n /**\n * Authenticates with Azure Active Directory and returns an access token if successful.\n * If authentication fails, a {@link CredentialUnavailableError} 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 | null> {\n if (!this.client) {\n const errorMessage = `${credentialName}: is unavailable. tenantId, clientId, and federatedTokenFilePath are required parameters. \n In DefaultAzureCredential and ManagedIdentityCredential, these can be provided as environment variables - \n \"AZURE_TENANT_ID\",\n \"AZURE_CLIENT_ID\",\n \"AZURE_FEDERATED_TOKEN_FILE\"`;\n logger.info(errorMessage);\n throw new CredentialUnavailableError(errorMessage);\n }\n logger.info(\"Invoking getToken() of Client Assertion Credential\");\n return this.client.getToken(scopes, options);\n }\n\n private async readFileContents(): Promise<string> {\n // Cached assertions expire after 5 minutes\n if (this.cacheDate !== undefined && Date.now() - this.cacheDate >= 1000 * 60 * 5) {\n this.azureFederatedTokenFileContent = undefined;\n }\n if (!this.federatedTokenFilePath) {\n throw new CredentialUnavailableError(\n `${credentialName}: is unavailable. Invalid file path provided ${this.federatedTokenFilePath}.`\n );\n }\n if (!this.azureFederatedTokenFileContent) {\n const file = await readFile(this.federatedTokenFilePath, \"utf8\");\n const value = file.trim();\n if (!value) {\n throw new CredentialUnavailableError(\n `${credentialName}: is unavailable. No content on the file ${this.federatedTokenFilePath}.`\n );\n } else {\n this.azureFederatedTokenFileContent = value;\n this.cacheDate = Date.now();\n }\n }\n return this.azureFederatedTokenFileContent;\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"workloadIdentityCredentialOptions.js","sourceRoot":"","sources":["../../../src/credentials/workloadIdentityCredentialOptions.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AuthorityValidationOptions } from \"./authorityValidationOptions\";\nimport { MultiTenantTokenCredentialOptions } from \"./multiTenantTokenCredentialOptions\";\n\n/**\n * Options for the {@link WorkloadIdentityCredential}\n */\nexport interface WorkloadIdentityCredentialOptions\n extends MultiTenantTokenCredentialOptions,\n AuthorityValidationOptions {\n /**\n * ID of the application's Azure Active Directory tenant. Also called its directory ID.\n */\n tenantId?: string;\n /**\n * The client ID of an Azure AD app registration.\n */\n clientId?: string;\n /**\n * The path to a file containing a Kubernetes service account token that authenticates the identity.\n */\n federatedTokenFilePath?: string;\n}\n"]}
1
+ {"version":3,"file":"workloadIdentityCredentialOptions.js","sourceRoot":"","sources":["../../../src/credentials/workloadIdentityCredentialOptions.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AuthorityValidationOptions } from \"./authorityValidationOptions\";\nimport { MultiTenantTokenCredentialOptions } from \"./multiTenantTokenCredentialOptions\";\nimport { WorkloadIdentityCredential } from \"./workloadIdentityCredential\";\n\n/**\n * Options for the {@link WorkloadIdentityCredential}\n */\nexport interface WorkloadIdentityCredentialOptions\n extends WorkloadIdentityDefaultCredentialOptions {\n /**\n * ID of the application's Azure Active Directory tenant. Also called its directory ID.\n */\n tenantId: string;\n /**\n * The client ID of an Azure AD app registration.\n */\n clientId: string;\n /**\n * The path to a file containing a Kubernetes service account token that authenticates the identity.\n */\n federatedTokenFilePath: string;\n}\n\n/**\n * @internal\n * @hidden\n */\nexport interface WorkloadIdentityDefaultCredentialOptions\n extends MultiTenantTokenCredentialOptions,\n AuthorityValidationOptions {}\n"]}
@@ -1,5 +1,6 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT license.
3
+ import { CredentialUnavailableError } from "../errors";
3
4
  function createConfigurationErrorMessage(tenantId) {
4
5
  return `The current credential is not configured to acquire tokens for tenant ${tenantId}. To enable acquiring tokens for this tenant add it to the AdditionallyAllowedTenants on the credential options, or add "*" to AdditionallyAllowedTenants to allow acquiring tokens for any tenant.`;
5
6
  }
@@ -9,7 +10,7 @@ function createConfigurationErrorMessage(tenantId) {
9
10
  * or unless the original tenant Id is `adfs`.
10
11
  * @internal
11
12
  */
12
- export function processMultiTenantRequest(tenantId, getTokenOptions, additionallyAllowedTenantIds = []) {
13
+ export function processMultiTenantRequest(tenantId, getTokenOptions, additionallyAllowedTenantIds = [], logger) {
13
14
  var _a;
14
15
  let resolvedTenantId;
15
16
  if (process.env.AZURE_IDENTITY_DISABLE_MULTITENANTAUTH) {
@@ -21,12 +22,15 @@ export function processMultiTenantRequest(tenantId, getTokenOptions, additionall
21
22
  else {
22
23
  resolvedTenantId = (_a = getTokenOptions === null || getTokenOptions === void 0 ? void 0 : getTokenOptions.tenantId) !== null && _a !== void 0 ? _a : tenantId;
23
24
  }
24
- console.log(resolvedTenantId);
25
+ console.log("resolved tenant =", resolvedTenantId);
26
+ console.log("tenantId =", tenantId);
25
27
  if (tenantId &&
26
28
  resolvedTenantId !== tenantId &&
27
29
  !additionallyAllowedTenantIds.includes("*") &&
28
30
  !additionallyAllowedTenantIds.some((t) => t.localeCompare(resolvedTenantId) === 0)) {
29
- throw new Error(createConfigurationErrorMessage(tenantId));
31
+ const message = createConfigurationErrorMessage(tenantId);
32
+ logger === null || logger === void 0 ? void 0 : logger.info(message);
33
+ throw new CredentialUnavailableError(message);
30
34
  }
31
35
  return resolvedTenantId;
32
36
  }
@@ -1 +1 @@
1
- {"version":3,"file":"processMultiTenantRequest.js","sourceRoot":"","sources":["../../../src/util/processMultiTenantRequest.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,SAAS,+BAA+B,CAAC,QAAgB;IACvD,OAAO,yEAAyE,QAAQ,qMAAqM,CAAC;AAChS,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,QAAiB,EACjB,eAAiC,EACjC,+BAAyC,EAAE;;IAE3C,IAAI,gBAAoC,CAAC;IACzC,IAAI,OAAO,CAAC,GAAG,CAAC,sCAAsC,EAAE;QACtD,gBAAgB,GAAG,QAAQ,CAAC;KAC7B;SAAM,IAAI,QAAQ,KAAK,MAAM,EAAE;QAC9B,gBAAgB,GAAG,QAAQ,CAAC;KAC7B;SAAM;QACL,gBAAgB,GAAG,MAAA,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,QAAQ,mCAAI,QAAQ,CAAC;KAC1D;IACD,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAC9B,IACE,QAAQ;QACR,gBAAgB,KAAK,QAAQ;QAC7B,CAAC,4BAA4B,CAAC,QAAQ,CAAC,GAAG,CAAC;QAC3C,CAAC,4BAA4B,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,gBAAiB,CAAC,KAAK,CAAC,CAAC,EACnF;QACA,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,QAAQ,CAAC,CAAC,CAAC;KAC5D;IAED,OAAO,gBAAgB,CAAC;AAC1B,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { GetTokenOptions } from \"@azure/core-auth\";\n\nfunction createConfigurationErrorMessage(tenantId: string): string {\n return `The current credential is not configured to acquire tokens for tenant ${tenantId}. To enable acquiring tokens for this tenant add it to the AdditionallyAllowedTenants on the credential options, or add \"*\" to AdditionallyAllowedTenants to allow acquiring tokens for any tenant.`;\n}\n\n/**\n * Of getToken contains a tenantId, this functions allows picking this tenantId as the appropriate for authentication,\n * unless multitenant authentication has been disabled through the AZURE_IDENTITY_DISABLE_MULTITENANTAUTH (on Node.js),\n * or unless the original tenant Id is `adfs`.\n * @internal\n */\nexport function processMultiTenantRequest(\n tenantId?: string,\n getTokenOptions?: GetTokenOptions,\n additionallyAllowedTenantIds: string[] = []\n): string | undefined {\n let resolvedTenantId: string | undefined;\n if (process.env.AZURE_IDENTITY_DISABLE_MULTITENANTAUTH) {\n resolvedTenantId = tenantId;\n } else if (tenantId === \"adfs\") {\n resolvedTenantId = tenantId;\n } else {\n resolvedTenantId = getTokenOptions?.tenantId ?? tenantId;\n }\n console.log(resolvedTenantId);\n if (\n tenantId &&\n resolvedTenantId !== tenantId &&\n !additionallyAllowedTenantIds.includes(\"*\") &&\n !additionallyAllowedTenantIds.some((t) => t.localeCompare(resolvedTenantId!) === 0)\n ) {\n throw new Error(createConfigurationErrorMessage(tenantId));\n }\n\n return resolvedTenantId;\n}\n"]}
1
+ {"version":3,"file":"processMultiTenantRequest.js","sourceRoot":"","sources":["../../../src/util/processMultiTenantRequest.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAAE,0BAA0B,EAAE,MAAM,WAAW,CAAC;AAGvD,SAAS,+BAA+B,CAAC,QAAgB;IACvD,OAAO,yEAAyE,QAAQ,qMAAqM,CAAC;AAChS,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,QAAiB,EACjB,eAAiC,EACjC,+BAAyC,EAAE,EAC3C,MAAyB;;IAEzB,IAAI,gBAAoC,CAAC;IACzC,IAAI,OAAO,CAAC,GAAG,CAAC,sCAAsC,EAAE;QACtD,gBAAgB,GAAG,QAAQ,CAAC;KAC7B;SAAM,IAAI,QAAQ,KAAK,MAAM,EAAE;QAC9B,gBAAgB,GAAG,QAAQ,CAAC;KAC7B;SAAM;QACL,gBAAgB,GAAG,MAAA,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,QAAQ,mCAAI,QAAQ,CAAC;KAC1D;IACD,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,CAAC;IACnD,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IACpC,IACE,QAAQ;QACR,gBAAgB,KAAK,QAAQ;QAC7B,CAAC,4BAA4B,CAAC,QAAQ,CAAC,GAAG,CAAC;QAC3C,CAAC,4BAA4B,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,gBAAiB,CAAC,KAAK,CAAC,CAAC,EACnF;QACA,MAAM,OAAO,GAAG,+BAA+B,CAAC,QAAQ,CAAC,CAAC;QAC1D,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QACtB,MAAM,IAAI,0BAA0B,CAAC,OAAO,CAAC,CAAC;KAC/C;IAED,OAAO,gBAAgB,CAAC;AAC1B,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { GetTokenOptions } from \"@azure/core-auth\";\nimport { CredentialUnavailableError } from \"../errors\";\nimport { CredentialLogger } from \"./logging\";\n\nfunction createConfigurationErrorMessage(tenantId: string): string {\n return `The current credential is not configured to acquire tokens for tenant ${tenantId}. To enable acquiring tokens for this tenant add it to the AdditionallyAllowedTenants on the credential options, or add \"*\" to AdditionallyAllowedTenants to allow acquiring tokens for any tenant.`;\n}\n\n/**\n * Of getToken contains a tenantId, this functions allows picking this tenantId as the appropriate for authentication,\n * unless multitenant authentication has been disabled through the AZURE_IDENTITY_DISABLE_MULTITENANTAUTH (on Node.js),\n * or unless the original tenant Id is `adfs`.\n * @internal\n */\nexport function processMultiTenantRequest(\n tenantId?: string,\n getTokenOptions?: GetTokenOptions,\n additionallyAllowedTenantIds: string[] = [],\n logger?: CredentialLogger\n): string | undefined {\n let resolvedTenantId: string | undefined;\n if (process.env.AZURE_IDENTITY_DISABLE_MULTITENANTAUTH) {\n resolvedTenantId = tenantId;\n } else if (tenantId === \"adfs\") {\n resolvedTenantId = tenantId;\n } else {\n resolvedTenantId = getTokenOptions?.tenantId ?? tenantId;\n }\n console.log(\"resolved tenant =\", resolvedTenantId);\n console.log(\"tenantId =\", tenantId);\n if (\n tenantId &&\n resolvedTenantId !== tenantId &&\n !additionallyAllowedTenantIds.includes(\"*\") &&\n !additionallyAllowedTenantIds.some((t) => t.localeCompare(resolvedTenantId!) === 0)\n ) {\n const message = createConfigurationErrorMessage(tenantId);\n logger?.info(message);\n throw new CredentialUnavailableError(message);\n }\n\n return resolvedTenantId;\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@azure/identity",
3
3
  "sdk-type": "client",
4
- "version": "3.2.0-alpha.20230227.4",
4
+ "version": "3.2.0-alpha.20230302.1",
5
5
  "description": "Provides credential implementations for Azure SDK libraries that can authenticate with Azure Active Directory",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist-esm/src/index.js",
@@ -122,7 +122,7 @@
122
122
  "uuid": "^8.3.0"
123
123
  },
124
124
  "devDependencies": {
125
- "@azure-tools/test-recorder": "^2.0.0",
125
+ "@azure-tools/test-recorder": ">=3.0.0-alpha <3.0.0-alphb",
126
126
  "@azure/dev-tool": ">=1.0.0-alpha <1.0.0-alphb",
127
127
  "@azure/eslint-plugin-azure-sdk": ">=3.0.0-alpha <3.0.0-alphb",
128
128
  "@azure/keyvault-keys": "^4.2.0",
@@ -657,6 +657,11 @@ export declare interface DefaultAzureCredentialClientIdOptions extends DefaultAz
657
657
  * This client ID can also be passed through to the {@link ManagedIdentityCredential} through the environment variable: AZURE_CLIENT_ID.
658
658
  */
659
659
  managedIdentityClientId?: string;
660
+ /**
661
+ * Optionally pass in a user assigned client ID to be used by the {@link WorkloadIdentityCredential}.
662
+ * This client ID can also be passed through to the {@link WorkloadIdentityCredential} through the environment variable: AZURE_CLIENT_ID.
663
+ */
664
+ workloadIdentityClientId?: string;
660
665
  }
661
666
 
662
667
  /**
@@ -1449,15 +1454,16 @@ export declare interface VisualStudioCodeCredentialOptions extends MultiTenantTo
1449
1454
  */
1450
1455
  export declare class WorkloadIdentityCredential implements TokenCredential {
1451
1456
  private client;
1452
- private federatedTokenFilePath;
1453
1457
  private azureFederatedTokenFileContent;
1454
1458
  private cacheDate;
1459
+ private federatedTokenFilePath;
1455
1460
  /**
1456
1461
  * WorkloadIdentityCredential supports Azure workload identity on Kubernetes.
1457
1462
  *
1458
1463
  * @param options - The identity client options to use for authentication.
1459
1464
  */
1460
- constructor(options?: WorkloadIdentityCredentialOptions);
1465
+ constructor(options: WorkloadIdentityCredentialOptions);
1466
+ /* Excluded from this release type: __constructor */
1461
1467
  /**
1462
1468
  * Authenticates with Azure Active Directory and returns an access token if successful.
1463
1469
  * If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.
@@ -1473,19 +1479,21 @@ export declare class WorkloadIdentityCredential implements TokenCredential {
1473
1479
  /**
1474
1480
  * Options for the {@link WorkloadIdentityCredential}
1475
1481
  */
1476
- export declare interface WorkloadIdentityCredentialOptions extends MultiTenantTokenCredentialOptions, AuthorityValidationOptions {
1482
+ export declare interface WorkloadIdentityCredentialOptions extends WorkloadIdentityDefaultCredentialOptions {
1477
1483
  /**
1478
1484
  * ID of the application's Azure Active Directory tenant. Also called its directory ID.
1479
1485
  */
1480
- tenantId?: string;
1486
+ tenantId: string;
1481
1487
  /**
1482
1488
  * The client ID of an Azure AD app registration.
1483
1489
  */
1484
- clientId?: string;
1490
+ clientId: string;
1485
1491
  /**
1486
1492
  * The path to a file containing a Kubernetes service account token that authenticates the identity.
1487
1493
  */
1488
- federatedTokenFilePath?: string;
1494
+ federatedTokenFilePath: string;
1489
1495
  }
1490
1496
 
1497
+ /* Excluded from this release type: WorkloadIdentityDefaultCredentialOptions */
1498
+
1491
1499
  export { }