Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions msal/managed_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,12 @@ def _scope_to_resource(scope): # This is an experimental reasonable-effort appr
def _get_arc_endpoint():
if "IDENTITY_ENDPOINT" in os.environ and "IMDS_ENDPOINT" in os.environ:
return os.environ["IDENTITY_ENDPOINT"]
if ( # Defined in https://msazure.visualstudio.com/One/_wiki/wikis/One.wiki/233012/VM-Extension-Authoring-for-Arc?anchor=determining-which-endpoint-to-use
sys.platform == "linux" and os.path.exists("/var/opt/azcmagent/bin/himds")
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
sys.platform == "linux" and os.path.exists("/opt/azcmagent/bin/himds")
or sys.platform == "win32" and os.path.exists(os.path.expandvars(
r"%ProgramFiles%\AzureConnectedMachineAgent\himds.exe"))
# Avoid Windows-only "%EnvVar%" syntax so that tests can be run on Linux
r"${ProgramFiles}\AzureConnectedMachineAgent\himds.exe"
))
):
return "http://localhost:40342/metadata/identity/oauth2/token"

Expand Down
16 changes: 15 additions & 1 deletion tests/test_mi.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,23 @@ def test_machine_learning(self):
"IDENTITY_ENDPOINT": "http://localhost",
"IMDS_ENDPOINT": "http://localhost",
})
def test_arc(self):
def test_arc_by_env_var(self):
self.assertEqual(get_managed_identity_source(), AZURE_ARC)

@patch("msal.managed_identity.os.path.exists", return_value=True)
@patch("msal.managed_identity.sys.platform", new="linux")
def test_arc_by_file_existence_on_linux(self, mocked_exists):
self.assertEqual(get_managed_identity_source(), AZURE_ARC)
mocked_exists.assert_called_with("/opt/azcmagent/bin/himds")

@patch("msal.managed_identity.os.path.exists", return_value=True)
@patch("msal.managed_identity.sys.platform", new="win32")
@patch.dict(os.environ, {"ProgramFiles": "C:\Program Files"})
def test_arc_by_file_existence_on_windows(self, mocked_exists):
self.assertEqual(get_managed_identity_source(), AZURE_ARC)
mocked_exists.assert_called_with(
r"C:\Program Files\AzureConnectedMachineAgent\himds.exe")

@patch.dict(os.environ, {
"AZUREPS_HOST_ENVIRONMENT": "cloud-shell-foo",
})
Expand Down