Skip to content

Commit e9b3913

Browse files
committed
Fix incorrect arc detection and add test cases
1 parent fe8f758 commit e9b3913

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

msal/managed_identity.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,12 @@ def _scope_to_resource(scope): # This is an experimental reasonable-effort appr
319319
def _get_arc_endpoint():
320320
if "IDENTITY_ENDPOINT" in os.environ and "IMDS_ENDPOINT" in os.environ:
321321
return os.environ["IDENTITY_ENDPOINT"]
322-
if ( # Defined in https://msazure.visualstudio.com/One/_wiki/wikis/One.wiki/233012/VM-Extension-Authoring-for-Arc?anchor=determining-which-endpoint-to-use
323-
sys.platform == "linux" and os.path.exists("/var/opt/azcmagent/bin/himds")
322+
if ( # Defined in https://eng.ms/docs/cloud-ai-platform/azure-core/azure-management-and-platforms/control-plane-bburns/hybrid-resource-provider/azure-arc-for-servers/specs/extension_authoring
323+
sys.platform == "linux" and os.path.exists("/opt/azcmagent/bin/himds")
324324
or sys.platform == "win32" and os.path.exists(os.path.expandvars(
325-
r"%ProgramFiles%\AzureConnectedMachineAgent\himds.exe"))
325+
# Avoid Windows-only "%EnvVar%" syntax so that tests can be run on Linux
326+
r"${ProgramFiles}\AzureConnectedMachineAgent\himds.exe"
327+
))
326328
):
327329
return "http://localhost:40342/metadata/identity/oauth2/token"
328330

tests/test_mi.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,23 @@ def test_machine_learning(self):
303303
"IDENTITY_ENDPOINT": "http://localhost",
304304
"IMDS_ENDPOINT": "http://localhost",
305305
})
306-
def test_arc(self):
306+
def test_arc_by_env_var(self):
307307
self.assertEqual(get_managed_identity_source(), AZURE_ARC)
308308

309+
@patch("msal.managed_identity.os.path.exists", return_value=True)
310+
@patch("msal.managed_identity.sys.platform", new="linux")
311+
def test_arc_by_file_existence_on_linux(self, mocked_exists):
312+
self.assertEqual(get_managed_identity_source(), AZURE_ARC)
313+
mocked_exists.assert_called_with("/opt/azcmagent/bin/himds")
314+
315+
@patch("msal.managed_identity.os.path.exists", return_value=True)
316+
@patch("msal.managed_identity.sys.platform", new="win32")
317+
@patch.dict(os.environ, {"ProgramFiles": "C:\Program Files"})
318+
def test_arc_by_file_existence_on_windows(self, mocked_exists):
319+
self.assertEqual(get_managed_identity_source(), AZURE_ARC)
320+
mocked_exists.assert_called_with(
321+
r"C:\Program Files\AzureConnectedMachineAgent\himds.exe")
322+
309323
@patch.dict(os.environ, {
310324
"AZUREPS_HOST_ENVIRONMENT": "cloud-shell-foo",
311325
})

0 commit comments

Comments
 (0)