-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure - create a custom chained token credential to place the AzureCL…
…ICredential prior to the ManagedIdentityCredential (#1009)
- Loading branch information
Showing
3 changed files
with
26 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { AzureCliCredential, AzureDeveloperCliCredential, AzurePowerShellCredential, ChainedTokenCredential, EnvironmentCredential, ManagedIdentityCredential } from "@azure/identity"; | ||
|
||
function createChainedTokenCredential(): ChainedTokenCredential { | ||
return new ChainedTokenCredential( | ||
new EnvironmentCredential(), | ||
new AzureCliCredential(), | ||
new ManagedIdentityCredential({ clientId: process.env.AZURE_CLIENT_ID }), | ||
new AzurePowerShellCredential({ tenantId: process.env.AZURE_TENANT_ID }), | ||
new AzureDeveloperCliCredential({ tenantId: process.env.AZURE_TENANT_ID }) | ||
); | ||
} | ||
|
||
export async function getAzureCredentialAccessToken(): Promise<string> { | ||
try { | ||
const credential = createChainedTokenCredential() | ||
const token = await credential.getToken('499b84ac-1321-427f-aa17-267ca6975798/.default', { | ||
tenantId: process.env.AZURE_TENANT_ID | ||
}); | ||
|
||
return token.token; | ||
} catch (error) { | ||
throw new Error('Can not acquire a Microsoft Entra ID access token. Additional information:\n\n' + error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters