-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Improve token handling and cleanup in docker.md #14365
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -316,42 +316,58 @@ Next, create the Dockerfile. | |||||||||||||||||||||||||||||||||||||||
| #!/bin/bash | ||||||||||||||||||||||||||||||||||||||||
| set -e | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if [ -z "${AZP_URL}" ]; then | ||||||||||||||||||||||||||||||||||||||||
| echo 1>&2 "error: missing AZP_URL environment variable" | ||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
| # Load a token either from the environment variable or by using the service principal credentials. | ||||||||||||||||||||||||||||||||||||||||
| load_azp_token() { | ||||||||||||||||||||||||||||||||||||||||
| if [ -n "$AZP_CLIENTID" ]; then | ||||||||||||||||||||||||||||||||||||||||
| if [ -z "$AZP_CLIENTSECRET" ]; then | ||||||||||||||||||||||||||||||||||||||||
| echo 1>&2 "error: AZP_CLIENTSECRET must be set when AZP_CLIENTID is used" | ||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if [ -z "$AZP_TENANTID" ]; then | ||||||||||||||||||||||||||||||||||||||||
| echo 1>&2 "error: AZP_TENANTID must be set when AZP_CLIENTID is used" | ||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| echo "Using service principal credentials to get token" | ||||||||||||||||||||||||||||||||||||||||
| az login --allow-no-subscriptions --service-principal --username "$AZP_CLIENTID" --password "$AZP_CLIENTSECRET" --tenant "$AZP_TENANTID" | ||||||||||||||||||||||||||||||||||||||||
| # adapted from https://learn.microsoft.com/en-us/azure/databricks/dev-tools/user-aad-token | ||||||||||||||||||||||||||||||||||||||||
| AZP_TOKEN=$(az account get-access-token --query accessToken --output tsv) | ||||||||||||||||||||||||||||||||||||||||
| echo "Token retrieved" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Ensure credentials are not visible to the agent by un-exporting the variable | ||||||||||||||||||||||||||||||||||||||||
| export -n AZP_CLIENTSECRET | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+338
to
+339
|
||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if [ -n "$AZP_CLIENTID" ]; then | ||||||||||||||||||||||||||||||||||||||||
| echo "Using service principal credentials to get token" | ||||||||||||||||||||||||||||||||||||||||
| az login --allow-no-subscriptions --service-principal --username "$AZP_CLIENTID" --password "$AZP_CLIENTSECRET" --tenant "$AZP_TENANTID" | ||||||||||||||||||||||||||||||||||||||||
| # adapted from https://learn.microsoft.com/en-us/azure/databricks/dev-tools/user-aad-token | ||||||||||||||||||||||||||||||||||||||||
| AZP_TOKEN=$(az account get-access-token --query accessToken --output tsv) | ||||||||||||||||||||||||||||||||||||||||
| echo "Token retrieved" | ||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
| if [ -z "${AZP_TOKEN_FILE}" ]; then | ||||||||||||||||||||||||||||||||||||||||
| if [ -z "${AZP_TOKEN}" ]; then | ||||||||||||||||||||||||||||||||||||||||
| echo 1>&2 "error: missing AZP_TOKEN environment variable" | ||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if [ -z "${AZP_TOKEN_FILE}" ]; then | ||||||||||||||||||||||||||||||||||||||||
| if [ -z "${AZP_TOKEN}" ]; then | ||||||||||||||||||||||||||||||||||||||||
| echo 1>&2 "error: missing AZP_TOKEN environment variable" | ||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||
| AZP_TOKEN_FILE="$(dirname "$0")/.token" | ||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| AZP_TOKEN_FILE="/azp/.token" | ||||||||||||||||||||||||||||||||||||||||
| echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}" | ||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| unset AZP_CLIENTSECRET | ||||||||||||||||||||||||||||||||||||||||
| unset AZP_TOKEN | ||||||||||||||||||||||||||||||||||||||||
| if [ -n "${AZP_TOKEN}" ]; then | ||||||||||||||||||||||||||||||||||||||||
| echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}" | |
| echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}" | |
| else | |
| if [ ! -f "${AZP_TOKEN_FILE}" ]; then | |
| echo 1>&2 "error: AZP_TOKEN_FILE does not exist: ${AZP_TOKEN_FILE}" | |
| exit 1 | |
| fi | |
| if [ ! -r "${AZP_TOKEN_FILE}" ]; then | |
| echo 1>&2 "error: AZP_TOKEN_FILE is not readable: ${AZP_TOKEN_FILE}" | |
| exit 1 | |
| fi | |
| if [ ! -s "${AZP_TOKEN_FILE}" ]; then | |
| echo 1>&2 "error: AZP_TOKEN_FILE is empty: ${AZP_TOKEN_FILE}" | |
| exit 1 | |
| fi |
Copilot
AI
May 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation on the echo line is inconsistent with the surrounding block, which makes the embedded script harder to read and maintain in the docs. Align the indentation to match the rest of the function.
| echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}" | |
| echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}" |
Copilot
AI
May 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export -n only removes the variables from the exported environment; it does not clear the values from the current shell. Since this script can run for a long time, AZP_CLIENTSECRET (and potentially AZP_TOKEN) remain accessible within the process. Consider explicitly unset-ing these variables after they are no longer needed (e.g., after successfully writing the token to AZP_TOKEN_FILE) to reduce secret exposure. (Optional hardening: ensure any Azure CLI cached credentials are stored in a disposable location and removed on exit.)
| # Ensure credentials are not visible to the agent by un-exporting the variable | |
| export -n AZP_TOKEN | |
| # Ensure credentials are not retained in the current shell after writing the token file | |
| unset AZP_TOKEN |
Uh oh!
There was an error while loading. Please reload this page.