Skip to content

Commit d45769b

Browse files
authored
Improve token handling and cleanup in docker.md
Refactor token loading and cleanup functions in docker script. Fixes issue with this error during cleanup WRITE ERROR: VS30063: You are not authorized to access https://dev.azure.com. Also fixed issue with hard code AZP_TOKEN_FILE location being a hard coded path /azp/.token which may not exists. Changed it to use the same location as the script location.
1 parent 1fa9f5f commit d45769b

1 file changed

Lines changed: 37 additions & 25 deletions

File tree

docs/pipelines/agents/docker.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -316,42 +316,45 @@ Next, create the Dockerfile.
316316
#!/bin/bash
317317
set -e
318318
319-
if [ -z "${AZP_URL}" ]; then
320-
echo 1>&2 "error: missing AZP_URL environment variable"
321-
exit 1
322-
fi
319+
# Load a token either from the environment variable or by using the service principal credentials.
320+
load_azp_token() {
321+
if [ -n "$AZP_CLIENTID" ]; then
322+
echo "Using service principal credentials to get token"
323+
az login --allow-no-subscriptions --service-principal --username "$AZP_CLIENTID" --password "$AZP_CLIENTSECRET" --tenant "$AZP_TENANTID"
324+
# adapted from https://learn.microsoft.com/en-us/azure/databricks/dev-tools/user-aad-token
325+
AZP_TOKEN=$(az account get-access-token --query accessToken --output tsv)
326+
echo "Token retrieved"
327+
328+
# Ensure credentials are not visible to the agent by ensuring it is a local variable only
329+
export -n AZP_CLIENTSECRET
330+
fi
323331
324-
if [ -n "$AZP_CLIENTID" ]; then
325-
echo "Using service principal credentials to get token"
326-
az login --allow-no-subscriptions --service-principal --username "$AZP_CLIENTID" --password "$AZP_CLIENTSECRET" --tenant "$AZP_TENANTID"
327-
# adapted from https://learn.microsoft.com/en-us/azure/databricks/dev-tools/user-aad-token
328-
AZP_TOKEN=$(az account get-access-token --query accessToken --output tsv)
329-
echo "Token retrieved"
330-
fi
332+
if [ -z "${AZP_TOKEN_FILE}" ]; then
333+
if [ -z "${AZP_TOKEN}" ]; then
334+
echo 1>&2 "error: missing AZP_TOKEN environment variable"
335+
exit 1
336+
fi
331337
332-
if [ -z "${AZP_TOKEN_FILE}" ]; then
333-
if [ -z "${AZP_TOKEN}" ]; then
334-
echo 1>&2 "error: missing AZP_TOKEN environment variable"
335-
exit 1
338+
AZP_TOKEN_FILE="$(dirname "$0")/.token"
336339
fi
337-
338-
AZP_TOKEN_FILE="/azp/.token"
339340
echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}"
340-
fi
341341
342-
unset AZP_CLIENTSECRET
343-
unset AZP_TOKEN
344-
345-
if [ -n "${AZP_WORK}" ]; then
346-
mkdir -p "${AZP_WORK}"
347-
fi
342+
# Ensure credentials are not visible to the agent by ensuring it is a local variable only
343+
export -n AZP_TOKEN
344+
}
348345
346+
# Cleanup function to remove the agent configuration.
349347
cleanup() {
350348
trap "" EXIT
351349
352350
if [ -e ./config.sh ]; then
353351
print_header "Cleanup. Removing Azure Pipelines agent..."
354352
353+
# Ensure we have a new token if using service principal credentials, as the old one might have expired between the time it was waiting to run a job and now.
354+
if [ -n "$AZP_CLIENTID" ]; then
355+
load_azp_token
356+
fi
357+
355358
# If the agent has some running jobs, the configuration removal process will fail.
356359
# So, give it some time to finish the job.
357360
while true; do
@@ -369,8 +372,17 @@ Next, create the Dockerfile.
369372
echo -e "\n${lightcyan}$1${nocolor}\n"
370373
}
371374
375+
if [ -z "${AZP_URL}" ]; then
376+
echo 1>&2 "error: missing AZP_URL environment variable"
377+
exit 1
378+
fi
379+
380+
if [ -n "${AZP_WORK}" ]; then
381+
mkdir -p "${AZP_WORK}"
382+
fi
383+
372384
# Let the agent ignore the token env variables
373-
export VSO_AGENT_IGNORE="AZP_TOKEN,AZP_TOKEN_FILE"
385+
export VSO_AGENT_IGNORE="AZP_TOKEN,AZP_TOKEN_FILE,AZP_CLIENTSECRET"
374386
375387
print_header "1. Determining matching Azure Pipelines agent..."
376388

0 commit comments

Comments
 (0)