Skip to content

Commit f85781d

Browse files
committed
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 f85781d

1 file changed

Lines changed: 53 additions & 25 deletions

File tree

docs/pipelines/agents/docker.md

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -316,42 +316,58 @@ 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+
if [ -z "$AZP_CLIENTSECRET" ]; then
323+
echo 1>&2 "error: AZP_CLIENTSECRET must be set when AZP_CLIENTID is used"
324+
exit 1
325+
fi
326+
327+
if [ -z "$AZP_TENANTID" ]; then
328+
echo 1>&2 "error: AZP_TENANTID must be set when AZP_CLIENTID is used"
329+
exit 1
330+
fi
331+
332+
echo "Using service principal credentials to get token"
333+
az login --allow-no-subscriptions --service-principal --username "$AZP_CLIENTID" --password "$AZP_CLIENTSECRET" --tenant "$AZP_TENANTID"
334+
# adapted from https://learn.microsoft.com/en-us/azure/databricks/dev-tools/user-aad-token
335+
AZP_TOKEN=$(az account get-access-token --query accessToken --output tsv)
336+
echo "Token retrieved"
337+
338+
# Ensure credentials are not visible to the agent by un-exporting the variable
339+
export -n AZP_CLIENTSECRET
340+
fi
323341
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
342+
if [ -z "${AZP_TOKEN_FILE}" ]; then
343+
if [ -z "${AZP_TOKEN}" ]; then
344+
echo 1>&2 "error: missing AZP_TOKEN environment variable"
345+
exit 1
346+
fi
331347
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
348+
AZP_TOKEN_FILE="$(dirname "$0")/.token"
336349
fi
337350
338-
AZP_TOKEN_FILE="/azp/.token"
339-
echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}"
340-
fi
341-
342-
unset AZP_CLIENTSECRET
343-
unset AZP_TOKEN
351+
if [ -n "${AZP_TOKEN}" ]; then
352+
echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}"
353+
fi
344354
345-
if [ -n "${AZP_WORK}" ]; then
346-
mkdir -p "${AZP_WORK}"
347-
fi
355+
# Ensure credentials are not visible to the agent by un-exporting the variable
356+
export -n AZP_TOKEN
357+
}
348358
359+
# Cleanup function to remove the agent configuration.
349360
cleanup() {
350361
trap "" EXIT
351362
352363
if [ -e ./config.sh ]; then
353364
print_header "Cleanup. Removing Azure Pipelines agent..."
354365
366+
# 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.
367+
if [ -n "$AZP_CLIENTID" ]; then
368+
load_azp_token
369+
fi
370+
355371
# If the agent has some running jobs, the configuration removal process will fail.
356372
# So, give it some time to finish the job.
357373
while true; do
@@ -369,8 +385,20 @@ Next, create the Dockerfile.
369385
echo -e "\n${lightcyan}$1${nocolor}\n"
370386
}
371387
388+
if [ -z "${AZP_URL}" ]; then
389+
echo 1>&2 "error: missing AZP_URL environment variable"
390+
exit 1
391+
fi
392+
393+
# Load the AZP token for initial setup.
394+
load_azp_token
395+
396+
if [ -n "${AZP_WORK}" ]; then
397+
mkdir -p "${AZP_WORK}"
398+
fi
399+
372400
# Let the agent ignore the token env variables
373-
export VSO_AGENT_IGNORE="AZP_TOKEN,AZP_TOKEN_FILE"
401+
export VSO_AGENT_IGNORE="AZP_TOKEN,AZP_TOKEN_FILE,AZP_CLIENTSECRET"
374402
375403
print_header "1. Determining matching Azure Pipelines agent..."
376404

0 commit comments

Comments
 (0)