Skip to content

Commit ab45659

Browse files

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

msal/region.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import json
32
import logging
43

54
logger = logging.getLogger(__name__)
@@ -17,7 +16,17 @@ def _detect_region_of_azure_function():
1716

1817

1918
def _detect_region_of_azure_vm(http_client):
20-
url = "http://169.254.169.254/metadata/instance?api-version=2021-01-01"
19+
url = (
20+
"http://169.254.169.254/metadata/instance"
21+
22+
# Utilize the "route parameters" feature to obtain region as a string
23+
# https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service?tabs=linux#route-parameters
24+
"/compute/location?format=text"
25+
26+
# Location info is available since API version 2017-04-02
27+
# https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service?tabs=linux#response-1
28+
"&api-version=2021-01-01"
29+
)
2130
logger.info(
2231
"Connecting to IMDS {}. "
2332
"You may want to use a shorter timeout on your http_client".format(url))
@@ -29,5 +38,5 @@ def _detect_region_of_azure_vm(http_client):
2938
"IMDS {} unavailable. Perhaps not running in Azure VM?".format(url))
3039
return None
3140
else:
32-
return json.loads(resp.text)["compute"]["location"]
41+
return resp.text.strip()
3342

0 commit comments

Comments
 (0)