-
Notifications
You must be signed in to change notification settings - Fork 139
Add Entra auth using az cli #539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gpcastro
wants to merge
2
commits into
microsoft:master
Choose a base branch
from
gpcastro:gpcastro/entra-auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| import { exec } from "child_process"; | ||
| import { promisify } from "util"; | ||
| import trace = require("./trace"); | ||
|
|
||
| const execAsync = promisify(exec); | ||
|
|
||
| const AZURE_DEVOPS_SCOPE = "https://app.vssps.visualstudio.com/.default"; | ||
| const AZURE_DEVOPS_RESOURCE = "499b84ac-1321-427f-aa17-267ca6975798"; | ||
| const ENTRA_TOKEN_ENV_VAR = "TFX_ENTRA_TOKEN"; | ||
|
|
||
| function isAzureCliMissing(err: any): boolean { | ||
| const errorText = [err && err.stderr, err && err.message] | ||
| .filter(message => !!message) | ||
| .map(message => String(message)) | ||
| .join("\n") | ||
| .toLowerCase(); | ||
|
|
||
| if (err && err.code === "ENOENT") { | ||
| return true; | ||
| } | ||
|
|
||
| return ( | ||
| errorText.includes("'az' is not recognized as an internal or external command") || | ||
| errorText.includes('"az" is not recognized as an internal or external command') || | ||
| errorText.includes("the term 'az' is not recognized") || | ||
| errorText.includes("az: not found") || | ||
| errorText.includes("az: command not found") || | ||
| (errorText.includes("az account get-access-token") && errorText.includes("operable program or batch file")) | ||
| ); | ||
| } | ||
|
|
||
| function getAzureCliMissingError(): Error { | ||
| return new Error( | ||
| "Azure CLI (az) is required for Microsoft Entra authentication. Install Azure CLI and run 'az login' (or 'az login --service-principal' / 'az login --identity'), or set TFX_ENTRA_TOKEN.", | ||
| ); | ||
| } | ||
|
|
||
| function getEntraTokenFromEnvironment(): string { | ||
| const token = process.env[ENTRA_TOKEN_ENV_VAR]; | ||
| if (token && token.trim()) { | ||
| trace.debug(`Using Microsoft Entra token from ${ENTRA_TOKEN_ENV_VAR}.`); | ||
| return token.trim(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| async function getTokenFromAzureCli(command: string): Promise<string> { | ||
| trace.debug(`Acquiring Microsoft Entra token with Azure CLI: ${command}`); | ||
| const result = await execAsync(command); | ||
| const token = result.stdout && result.stdout.trim(); | ||
|
|
||
| if (!token) { | ||
| throw new Error("Azure CLI returned an empty access token."); | ||
| } | ||
|
|
||
| return token; | ||
| } | ||
|
|
||
| function getAzureCliErrorMessage(err: any): string { | ||
| if (err && err.stderr && String(err.stderr).trim()) { | ||
| return String(err.stderr).trim(); | ||
| } | ||
|
|
||
| if (err && err.message && String(err.message).trim()) { | ||
| return String(err.message).trim(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export async function getEntraAccessToken(): Promise<string> { | ||
| const envToken = getEntraTokenFromEnvironment(); | ||
| if (envToken) { | ||
| return envToken; | ||
| } | ||
|
|
||
| const scopeCommand = `az account get-access-token --only-show-errors --scope "${AZURE_DEVOPS_SCOPE}" --query accessToken -o tsv`; | ||
| try { | ||
| return await getTokenFromAzureCli(scopeCommand); | ||
| } catch (scopeError) { | ||
| if (isAzureCliMissing(scopeError)) { | ||
| throw getAzureCliMissingError(); | ||
| } | ||
|
|
||
| const resourceCommand = `az account get-access-token --only-show-errors --resource ${AZURE_DEVOPS_RESOURCE} --query accessToken -o tsv`; | ||
| try { | ||
| return await getTokenFromAzureCli(resourceCommand); | ||
| } catch (resourceError) { | ||
| if (isAzureCliMissing(resourceError)) { | ||
| throw getAzureCliMissingError(); | ||
| } | ||
|
|
||
| const details = [getAzureCliErrorMessage(scopeError), getAzureCliErrorMessage(resourceError)] | ||
| .filter(message => !!message) | ||
| .filter((message, index, messages) => messages.indexOf(message) === index); | ||
|
|
||
| let message = | ||
| "Unable to acquire a Microsoft Entra access token. Run 'az login' (or 'az login --service-principal' / 'az login --identity') first, or set TFX_ENTRA_TOKEN."; | ||
|
|
||
| if (details.length > 0) { | ||
| message += " Azure CLI output: " + details.join(" "); | ||
| } | ||
|
|
||
| throw new Error(message); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.