@@ -316,6 +316,17 @@ def _scope_to_resource(scope): # This is an experimental reasonable-effort appr
316316 return scope # There is no much else we can do here
317317
318318
319+ def _get_arc_endpoint ():
320+ if "IDENTITY_ENDPOINT" in os .environ and "IMDS_ENDPOINT" in os .environ :
321+ 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" )
324+ or sys .platform == "win32" and os .path .exists (os .path .expandvars (
325+ r"%ProgramFiles%\AzureConnectedMachineAgent\himds.exe" ))
326+ ):
327+ return "http://localhost:40342/metadata/identity/oauth2/token"
328+
329+
319330APP_SERVICE = object ()
320331AZURE_ARC = object ()
321332CLOUD_SHELL = object () # In MSAL Python, token acquisition was done by
@@ -338,7 +349,7 @@ def get_managed_identity_source():
338349 return APP_SERVICE
339350 if "MSI_ENDPOINT" in os .environ and "MSI_SECRET" in os .environ :
340351 return MACHINE_LEARNING
341- if "IDENTITY_ENDPOINT" in os . environ and "IMDS_ENDPOINT" in os . environ :
352+ if _get_arc_endpoint () :
342353 return AZURE_ARC
343354 if _is_running_in_cloud_shell ():
344355 return CLOUD_SHELL
@@ -380,18 +391,15 @@ def _obtain_token(http_client, managed_identity, resource):
380391 managed_identity ,
381392 resource ,
382393 )
383- if "IDENTITY_ENDPOINT" in os .environ and "IMDS_ENDPOINT" in os .environ :
394+ arc_endpoint = _get_arc_endpoint ()
395+ if arc_endpoint :
384396 if ManagedIdentity .is_user_assigned (managed_identity ):
385397 raise ManagedIdentityError ( # Note: Azure Identity for Python raised exception too
386398 "Invalid managed_identity parameter. "
387399 "Azure Arc supports only system-assigned managed identity, "
388400 "See also "
389401 "https://learn.microsoft.com/en-us/azure/service-fabric/configure-existing-cluster-enable-managed-identity-token-service" )
390- return _obtain_token_on_arc (
391- http_client ,
392- os .environ ["IDENTITY_ENDPOINT" ],
393- resource ,
394- )
402+ return _obtain_token_on_arc (http_client , arc_endpoint , resource )
395403 return _obtain_token_on_azure_vm (http_client , managed_identity , resource )
396404
397405
0 commit comments