Skip to content

Commit 95e1bb0

Browse files
committed
Delay getfqdn() from import time to runtime. Fix #715
1 parent 2b03751 commit 95e1bb0

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

msal/managed_identity.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class ManagedIdentityClient(object):
144144
(like what a ``PublicClientApplication`` does),
145145
not a token with application permissions for an app.
146146
"""
147-
_instance, _tenant = socket.getfqdn(), "managed_identity" # Placeholders
147+
__instance, _tenant = None, "managed_identity" # Placeholders
148148

149149
def __init__(
150150
self,
@@ -232,6 +232,11 @@ def __init__(
232232
)
233233
self._token_cache = token_cache or TokenCache()
234234

235+
def _get_instance(self):
236+
if self.__instance is None:
237+
self.__instance = socket.getfqdn() # Moved from class definition to here
238+
return self.__instance
239+
235240
def acquire_token_for_client(self, *, resource): # We may support scope in the future
236241
"""Acquire token for the managed identity.
237242
@@ -257,7 +262,7 @@ def acquire_token_for_client(self, *, resource): # We may support scope in the
257262
target=[resource],
258263
query=dict(
259264
client_id=client_id_in_cache,
260-
environment=self._instance,
265+
environment=self._get_instance(),
261266
realm=self._tenant,
262267
home_account_id=None,
263268
),
@@ -287,7 +292,8 @@ def acquire_token_for_client(self, *, resource): # We may support scope in the
287292
self._token_cache.add(dict(
288293
client_id=client_id_in_cache,
289294
scope=[resource],
290-
token_endpoint="https://{}/{}".format(self._instance, self._tenant),
295+
token_endpoint="https://{}/{}".format(
296+
self._get_instance(), self._tenant),
291297
response=result,
292298
params={},
293299
data={},

0 commit comments

Comments
 (0)