@@ -316,42 +316,69 @@ 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+ # Note: Cannot unset the variable because it may be require to get a new token later
340+ export -n AZP_CLIENTSECRET
341+ fi
323342
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
343+ if [ -z " ${AZP_TOKEN_FILE} " ]; then
344+ if [ -z " ${AZP_TOKEN} " ]; then
345+ echo 1>&2 " error: missing AZP_TOKEN environment variable"
346+ exit 1
347+ fi
331348
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
349+ AZP_TOKEN_FILE=" $( dirname " $0 " ) /.token"
336350 fi
337351
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
352+ if [ -n " ${AZP_TOKEN-} " ]; then
353+ echo -n " ${AZP_TOKEN} " > " ${AZP_TOKEN_FILE} "
354+ else
355+ if [ ! -r " ${AZP_TOKEN_FILE} " ]; then
356+ echo 1>&2 " error: AZP_TOKEN_FILE is not readable: ${AZP_TOKEN_FILE} "
357+ exit 1
358+ fi
359+ if [ ! -s " ${AZP_TOKEN_FILE} " ]; then
360+ echo 1>&2 " error: AZP_TOKEN_FILE is empty: ${AZP_TOKEN_FILE} "
361+ exit 1
362+ fi
363+ fi
344364
345- if [ -n " ${AZP_WORK} " ]; then
346- mkdir -p " ${AZP_WORK} "
347- fi
365+ # Unset the AZP_TOKEN environment variable to prevent it from being exposed.
366+ # At this point the token is only available in the file specified by AZP_TOKEN_FILE.
367+ unset AZP_TOKEN
368+ }
348369
370+ # Cleanup function to remove the agent configuration.
349371 cleanup () {
350372 trap " " EXIT
351373
352374 if [ -e ./config.sh ]; then
353375 print_header " Cleanup. Removing Azure Pipelines agent..."
354376
377+ # 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.
378+ if [ -n " $AZP_CLIENTID " ]; then
379+ load_azp_token
380+ fi
381+
355382 # If the agent has some running jobs, the configuration removal process will fail.
356383 # So, give it some time to finish the job.
357384 while true ; do
@@ -369,8 +396,20 @@ Next, create the Dockerfile.
369396 echo -e " \n${lightcyan} $1 ${nocolor} \n"
370397 }
371398
399+ if [ -z " ${AZP_URL} " ]; then
400+ echo 1>&2 " error: missing AZP_URL environment variable"
401+ exit 1
402+ fi
403+
404+ # Load the AZP token for initial setup.
405+ load_azp_token
406+
407+ if [ -n " ${AZP_WORK} " ]; then
408+ mkdir -p " ${AZP_WORK} "
409+ fi
410+
372411 # Let the agent ignore the token env variables
373- export VSO_AGENT_IGNORE=" AZP_TOKEN,AZP_TOKEN_FILE"
412+ export VSO_AGENT_IGNORE=" AZP_TOKEN,AZP_TOKEN_FILE,AZP_CLIENTSECRET "
374413
375414 print_header " 1. Determining matching Azure Pipelines agent..."
376415
0 commit comments