Skip to content

Commit 7db6c2c

Browse files
authored
allow MI endpoint changing through environment variable (#754)
useful during development where you are using SSH tunnelling to utilise the credentials assigned to an actual instance
1 parent 33dbe3e commit 7db6c2c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

msal/managed_identity.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ class ManagedIdentityClient(object):
134134
135135
It also provides token cache support.
136136
137+
.. admonition:: Special case when your local development wants to use a managed identity on Azure VM.
138+
139+
By setting the environment variable ``MSAL_MANAGED_IDENTITY_ENDPOINT``
140+
you override the default identity URL used in MSAL's Azure VM managed identity
141+
code path.
142+
143+
This is useful during local development where it may be desirable to
144+
utilise the credentials assigned to an actual VM instance via SSH tunnelling.
145+
146+
For example, if you create your SSH tunnel this way (assuming your VM is on ``192.0.2.1``)::
147+
148+
ssh -L 8000:169.254.169.254:80 192.0.2.1
149+
150+
Then your code could run locally using::
151+
152+
env MSAL_MANAGED_IDENTITY_ENDPOINT=http://localhost:8000/metadata/identity/oauth2/token python your_script.py
153+
137154
.. note::
138155
139156
Cloud Shell support is NOT implemented in this class.
@@ -446,7 +463,7 @@ def _obtain_token_on_azure_vm(http_client, managed_identity, resource):
446463
}
447464
_adjust_param(params, managed_identity)
448465
resp = http_client.get(
449-
"http://169.254.169.254/metadata/identity/oauth2/token",
466+
os.getenv('MSAL_MANAGED_IDENTITY_ENDPOINT', 'http://169.254.169.254/metadata/identity/oauth2/token'),
450467
params=params,
451468
headers={"Metadata": "true"},
452469
)

0 commit comments

Comments
 (0)