Skip to content

Commit 14bcad3

Browse files
committed
Remove the reliance on socket.getfqdn()
1 parent 83c102f commit 14bcad3

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

msal/managed_identity.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import json
77
import logging
88
import os
9-
import socket
109
import sys
1110
import time
1211
from urllib.parse import urlparse # Python 3+
@@ -146,7 +145,10 @@ class ManagedIdentityClient(object):
146145
(like what a ``PublicClientApplication`` does),
147146
not a token with application permissions for an app.
148147
"""
149-
__instance, _tenant = None, "managed_identity" # Placeholders
148+
__instance = "localhost" # We used to get this value from socket.getfqdn()
149+
# but it is unreliable because getfqdn() either hangs or returns empty value
150+
# on some misconfigured machines
151+
_tenant = "managed_identity"
150152
_TOKEN_SOURCE = "token_source"
151153
_TOKEN_SOURCE_IDP = "identity_provider"
152154
_TOKEN_SOURCE_CACHE = "cache"
@@ -252,11 +254,6 @@ def __init__(
252254
self._token_cache = token_cache or TokenCache()
253255
self._client_capabilities = client_capabilities
254256

255-
def _get_instance(self):
256-
if self.__instance is None:
257-
self.__instance = socket.getfqdn() # Moved from class definition to here
258-
return self.__instance
259-
260257
def acquire_token_for_client(
261258
self,
262259
*,
@@ -302,7 +299,7 @@ def acquire_token_for_client(
302299
target=[resource],
303300
query=dict(
304301
client_id=client_id_in_cache,
305-
environment=self._get_instance(),
302+
environment=self.__instance,
306303
realm=self._tenant,
307304
home_account_id=None,
308305
),
@@ -344,7 +341,7 @@ def acquire_token_for_client(
344341
client_id=client_id_in_cache,
345342
scope=[resource],
346343
token_endpoint="https://{}/{}".format(
347-
self._get_instance(), self._tenant),
344+
self.__instance, self._tenant),
348345
response=result,
349346
params={},
350347
data={},

tests/test_mi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ def test_happy_path_of_vm(self):
190190
headers={'Metadata': 'true'},
191191
)
192192

193-
@patch("msal.managed_identity.socket.getfqdn", new=lambda: "MixedCaseHostName")
194-
def test_happy_path_of_windows_vm(self):
193+
@patch.object(ManagedIdentityClient, "_ManagedIdentityClient__instance", "MixedCaseHostName")
194+
def test_happy_path_of_theoretical_mixed_case_hostname(self):
195+
"""Historically, we used to get the host name from socket.getfqdn(),
196+
which could return a mixed-case host name on Windows.
197+
Although we no longer use getfqdn(), we still keep this test case to ensure we tolerate it.
198+
"""
195199
self.test_happy_path_of_vm()
196200

197201
@patch.dict(os.environ, {"AZURE_POD_IDENTITY_AUTHORITY_HOST": "http://localhost:1234//"})

0 commit comments

Comments
 (0)