Skip to content

Commit dd29f97

Browse files
Use endpoint environment variable for managed identities if set
Some environments in azure don't use the default metadata endpoint, and instead inject an env var that should be used.
1 parent 40b6db6 commit dd29f97

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

dnsapi/dns_azure.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,17 @@ _azure_getaccess_token() {
340340

341341
if [ "$managedIdentity" = true ]; then
342342
# https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-http
343-
export _H1="Metadata: true"
344-
response="$(_get http://169.254.169.254/metadata/identity/oauth2/token\?api-version=2018-02-01\&resource=https://management.azure.com/)"
343+
if [ -n "$IDENTITY_ENDPOINT" ]; then
344+
# Some Azure environments may set IDENTITY_ENDPOINT (formerly MSI_ENDPOINT) to have an alternative metadata endpoint
345+
url="$IDENTITY_ENDPOINT?api-version=2019-08-01&resource=https://management.azure.com/"
346+
headers="X-IDENTITY-HEADER: $IDENTITY_HEADER"
347+
else
348+
url="http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/"
349+
headers="Metadata: true"
350+
fi
351+
352+
export _H1="$headers"
353+
response="$(_get "$url")"
345354
response="$(echo "$response" | _normalizeJson)"
346355
accesstoken=$(echo "$response" | _egrep_o "\"access_token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
347356
expires_on=$(echo "$response" | _egrep_o "\"expires_on\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")

0 commit comments

Comments
 (0)