Skip to content

Commit

Permalink
Delay getfqdn() from import time to runtime. Fix #715
Browse files Browse the repository at this point in the history
  • Loading branch information
rayluo committed Aug 6, 2024
1 parent 2b03751 commit 95e1bb0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions msal/managed_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class ManagedIdentityClient(object):
(like what a ``PublicClientApplication`` does),
not a token with application permissions for an app.
"""
_instance, _tenant = socket.getfqdn(), "managed_identity" # Placeholders
__instance, _tenant = None, "managed_identity" # Placeholders

def __init__(
self,
Expand Down Expand Up @@ -232,6 +232,11 @@ def __init__(
)
self._token_cache = token_cache or TokenCache()

def _get_instance(self):
if self.__instance is None:
self.__instance = socket.getfqdn() # Moved from class definition to here
return self.__instance

def acquire_token_for_client(self, *, resource): # We may support scope in the future
"""Acquire token for the managed identity.
Expand All @@ -257,7 +262,7 @@ def acquire_token_for_client(self, *, resource): # We may support scope in the
target=[resource],
query=dict(
client_id=client_id_in_cache,
environment=self._instance,
environment=self._get_instance(),
realm=self._tenant,
home_account_id=None,
),
Expand Down Expand Up @@ -287,7 +292,8 @@ def acquire_token_for_client(self, *, resource): # We may support scope in the
self._token_cache.add(dict(
client_id=client_id_in_cache,
scope=[resource],
token_endpoint="https://{}/{}".format(self._instance, self._tenant),
token_endpoint="https://{}/{}".format(
self._get_instance(), self._tenant),
response=result,
params={},
data={},
Expand Down

0 comments on commit 95e1bb0

Please sign in to comment.