Skip to content
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 msi source detect logic #4761

Merged
merged 9 commits into from
May 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,41 @@ private static AbstractManagedIdentity SelectManagedIdentitySource(RequestContex
AzureArcManagedIdentitySource.TryCreate(requestContext) ??
new ImdsManagedIdentitySource(requestContext);
}

internal static ManagedIdentitySource GetManagedIdentitySource()
{
string identityEndpoint = EnvironmentVariables.IdentityEndpoint;
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
string identityHeader = EnvironmentVariables.IdentityHeader;
string identityServerThumbprint = EnvironmentVariables.IdentityServerThumbprint;
string msiSecret = EnvironmentVariables.IdentityHeader;
string msiEndpoint = EnvironmentVariables.MsiEndpoint;
string imdsEndpoint = EnvironmentVariables.ImdsEndpoint;
string podIdentityEndpoint = EnvironmentVariables.PodIdentityEndpoint;

if (!string.IsNullOrEmpty(identityEndpoint) && !string.IsNullOrEmpty(identityHeader) && !string.IsNullOrEmpty(identityServerThumbprint))
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
{
return ManagedIdentitySource.ServiceFabric;
}
else if (!string.IsNullOrEmpty(identityEndpoint) && !string.IsNullOrEmpty(identityHeader))
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
{
return ManagedIdentitySource.AppService;
}
else if (!string.IsNullOrEmpty(msiEndpoint))
{
return ManagedIdentitySource.CloudShell;
}
else if (!string.IsNullOrEmpty(identityEndpoint) && !string.IsNullOrEmpty(imdsEndpoint))
{
return ManagedIdentitySource.AzureArc;
}
else if (!string.IsNullOrEmpty(podIdentityEndpoint))
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
{
return ManagedIdentitySource.Imds;
}
else
{
return ManagedIdentitySource.DefaultToImds;
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public enum ManagedIdentitySource
/// <summary>
/// The source to acquire token for managed identity is Service Fabric.
/// </summary>
ServiceFabric
ServiceFabric,

/// <summary>
/// The source to acquire token for managed identity is defaulted to IMDS as no environment variables are found.
/// This is specifically used when the source is not known.
/// </summary>
DefaultToImds
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,14 @@ public AcquireTokenForManagedIdentityParameterBuilder AcquireTokenForManagedIden
ClientExecutorFactory.CreateManagedIdentityExecutor(this),
resource);
}

/// <summary>
/// Detects and returns the managed identity source available on the environment.
/// </summary>
/// <returns>Managed identity source detected on the environment if any.</returns>
public static ManagedIdentitySource GetManagedIdentitySource()
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
{
return ManagedIdentityClient.GetManagedIdentitySource();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ public class ManagedIdentityTests : TestBase
internal const string ExpectedErrorCode = "ErrorCode";
internal const string ExpectedCorrelationId = "Some GUID";

[DataTestMethod]
[DataRow("http://127.0.0.1:41564/msi/token/", ManagedIdentitySource.AppService, ManagedIdentitySource.AppService)]
[DataRow(AppServiceEndpoint, ManagedIdentitySource.AppService, ManagedIdentitySource.AppService)]
[DataRow(ImdsEndpoint, ManagedIdentitySource.Imds, ManagedIdentitySource.Imds)]
[DataRow(null, ManagedIdentitySource.Imds, ManagedIdentitySource.DefaultToImds)]
[DataRow(AzureArcEndpoint, ManagedIdentitySource.AzureArc, ManagedIdentitySource.AzureArc)]
[DataRow(CloudShellEndpoint, ManagedIdentitySource.CloudShell, ManagedIdentitySource.CloudShell)]
[DataRow(ServiceFabricEndpoint, ManagedIdentitySource.ServiceFabric, ManagedIdentitySource.ServiceFabric)]
public void GetManagedIdentityTests(
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
string endpoint,
ManagedIdentitySource managedIdentitySource,
ManagedIdentitySource expectedManagedIdentitySource)
{
using (new EnvVariableContext())
{
SetEnvironmentVariables(managedIdentitySource, endpoint);

Assert.AreEqual(expectedManagedIdentitySource, ManagedIdentityApplication.GetManagedIdentitySource());
}
}

[DataTestMethod]
[DataRow("http://127.0.0.1:41564/msi/token/", Resource, ManagedIdentitySource.AppService)]
[DataRow(AppServiceEndpoint, Resource, ManagedIdentitySource.AppService)]
Expand Down