-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add timeout to IMDS when used in DefaultAzureCredential #1016
Conversation
The DefaultAzureCredential iterates through multiple methods for obtaining a credential until one succeeds (or all fail). Currently, the default is to check the Environment, IMDS, then the CLI. In many non-Azure workloads (such as MSFT corp computers), the IMDS token request takes an exceedingly long time to fail. This makes using DefaultAzureCredential without disabling IMDS on corp machines painfully slow. This PR changes the order to Environment, CLI, then IMDS, which is beneficial for developers using the examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes a lot of sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d like to review this more. The order should match other implementations lie .NET.
.NET Azure.Identity.DefaultAzureCredentials has this order:
Java com.azure.identity.DefaultAzureCredential has this order:
It would be nice if all credential options failed fast if unavailable. I think our There are multiple sources for ManagedIdentity. I think we need to split them up. private static ManagedIdentitySource SelectManagedIdentitySource(ManagedIdentityClientOptions options)
{
return
ServiceFabricManagedIdentitySource.TryCreate(options) ??
AppServiceV2019ManagedIdentitySource.TryCreate(options) ??
AppServiceV2017ManagedIdentitySource.TryCreate(options) ??
CloudShellManagedIdentitySource.TryCreate(options) ??
AzureArcManagedIdentitySource.TryCreate(options) ??
TokenExchangeManagedIdentitySource.TryCreate(options) ??
new ImdsManagedIdentitySource(options);
} FYI, the difference between the two AppService: The AppServiceManagedIdentitySource validates the environment variables to see if the identity is available & fail fast if not: protected static bool TryValidateEnvVars(string msiEndpoint, string secret, out Uri endpointUri)
{
endpointUri = null;
// if BOTH the env vars endpoint and secret values are null, this MSI provider is unavailable.
// Also validate that IdentityServerThumbprint is null or empty to differentiate from Service Fabric.
if (string.IsNullOrEmpty(msiEndpoint) || string.IsNullOrEmpty(secret))
{
return false;
} Perhaps we can make |
What's the timeout of the IMDS request? Maybe that could be shortened to a few seconds? |
I also just hit this - using A bit of searching shows that the other SDKs seem to have a short default timeout when trying to access IMDS from within
Seems like we should align with the behaviour of the Go and .NET SDKs here, and add a 1 sec timeout for first connection when used via |
I've updated the PR to use |
Rather than expanding our dependencies, this uses azure-core's sleep to provide the future used for timing out and lifts the future from futures-time.
Originally, the update was implemented using @yoshuawuyts 's |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a good approach. Thank you both.
The DefaultAzureCredential iterates through multiple methods for obtaining a credential until one succeeds (or all fail).
Currently, the default is to check the Environment, IMDS, then the CLI.
In many non-Azure workloads (such as MSFT corp computers), the IMDS token request takes an exceedingly long time to fail. This makes using DefaultAzureCredential without disabling IMDS on corp machines painfully slow.
This PR adds a timeout of 1 second to IMDS, per research of @johnbatty (see below).