Skip to content

Commit

Permalink
Merge branch 'mi-arc-detection' into release-1.31.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rayluo committed Nov 1, 2024
2 parents 866e4ce + e9b3913 commit f0218b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 5 additions & 3 deletions msal/managed_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,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

0 comments on commit f0218b3

Please sign in to comment.