Skip to content

Commit e22ca8a

Browse files
Reordered the condition for ManagedIdentitySource.MachineLearning to be checked after ManagedIdentitySource.AppService instead of before it. (#5079)
* fix * test * pr comments * remove --------- Co-authored-by: Gladwin Johnson <[email protected]>
1 parent b1d19e0 commit e22ca8a

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ internal static ManagedIdentitySource GetManagedIdentitySource(ILoggerAdapter lo
6262
string imdsEndpoint = EnvironmentVariables.ImdsEndpoint;
6363
string podIdentityEndpoint = EnvironmentVariables.PodIdentityEndpoint;
6464

65-
if (!string.IsNullOrEmpty(msiSecretMachineLearning) && !string.IsNullOrEmpty(msiEndpoint))
66-
{
67-
return ManagedIdentitySource.MachineLearning;
68-
}
69-
else if (!string.IsNullOrEmpty(identityEndpoint) && !string.IsNullOrEmpty(identityHeader))
65+
if (!string.IsNullOrEmpty(identityEndpoint) && !string.IsNullOrEmpty(identityHeader))
7066
{
7167
if (!string.IsNullOrEmpty(identityServerThumbprint))
7268
{
@@ -77,6 +73,10 @@ internal static ManagedIdentitySource GetManagedIdentitySource(ILoggerAdapter lo
7773
return ManagedIdentitySource.AppService;
7874
}
7975
}
76+
else if (!string.IsNullOrEmpty(msiSecretMachineLearning) && !string.IsNullOrEmpty(msiEndpoint))
77+
{
78+
return ManagedIdentitySource.MachineLearning;
79+
}
8080
else if (!string.IsNullOrEmpty(msiEndpoint))
8181
{
8282
return ManagedIdentitySource.CloudShell;

tests/Microsoft.Identity.Test.Common/Core/Helpers/ManagedIdentityTestUtil.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,41 @@ public static void SetEnvironmentVariables(ManagedIdentitySource managedIdentity
6666
}
6767
}
6868

69+
/// <summary>
70+
/// Sets environment variables for testing upgrade scenarios.
71+
/// This method mimics a scenario where older environment variables
72+
/// (e.g., MSI_ENDPOINT and MSI_SECRET) from previous versions of
73+
/// App Service (2017) still exist after an upgrade to newer versions (2019).
74+
/// It ensures that MSAL's Managed Identity source detection can correctly
75+
/// handle both legacy and new variables.
76+
/// </summary>
77+
/// <param name="managedIdentitySource">
78+
/// The type of managed identity source being tested (e.g., AppService, MachineLearning).
79+
/// </param>
80+
/// <param name="endpoint">
81+
/// The endpoint URL to be set as part of the environment variables.
82+
/// </param>
83+
/// <param name="secret">
84+
/// Optional: The secret value to be set (default is "secret").
85+
/// </param>
86+
/// <param name="thumbprint">
87+
/// Optional: The certificate thumbprint to be set (default is "thumbprint").
88+
/// </param>
89+
internal static void SetUpgradeScenarioEnvironmentVariables(ManagedIdentitySource managedIdentitySource, string endpoint, string secret = "secret", string thumbprint = "thumbprint")
90+
{
91+
// Use the common method to set base environment variables
92+
SetEnvironmentVariables(managedIdentitySource, endpoint, secret, thumbprint);
93+
94+
// Add upgrade-specific variables where needed
95+
switch (managedIdentitySource)
96+
{
97+
case ManagedIdentitySource.AppService:
98+
Environment.SetEnvironmentVariable("MSI_ENDPOINT", endpoint);
99+
Environment.SetEnvironmentVariable("MSI_SECRET", secret);
100+
break;
101+
}
102+
}
103+
69104
/// <summary>
70105
/// Create the MIA with the http proxy
71106
/// </summary>

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/AppServiceTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Microsoft.Identity.Test.Unit.ManagedIdentityTests
2020
public class AppServiceTests : TestBase
2121
{
2222
private const string AppService = "App Service";
23+
internal const string AppServiceEndpoint = "http://127.0.0.1:41564/msi/token";
24+
internal const string MachineLearningEndpoint = "http://localhost:7071/msi/token";
2325

2426
[TestMethod]
2527
public async Task AppServiceInvalidEndpointAsync()
@@ -47,5 +49,23 @@ await mi.AcquireTokenForManagedIdentity(ManagedIdentityTests.Resource)
4749
Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, MsalErrorMessage.ManagedIdentityEndpointInvalidUriError, "IDENTITY_ENDPOINT", "127.0.0.1:41564/msi/token", AppService), ex.Message);
4850
}
4951
}
52+
53+
// Regression test for Bug ID #5077 - ManagedIdentityCredential authentication failed
54+
[DataTestMethod]
55+
[DataRow("http://127.0.0.1:41564/msi/token/", ManagedIdentitySource.AppService, ManagedIdentitySource.AppService)]
56+
[DataRow(AppServiceEndpoint, ManagedIdentitySource.AppService, ManagedIdentitySource.AppService)]
57+
[DataRow(MachineLearningEndpoint, ManagedIdentitySource.MachineLearning, ManagedIdentitySource.MachineLearning)]
58+
public void TestAppServiceUpgradeScenario(
59+
string endpoint,
60+
ManagedIdentitySource managedIdentitySource,
61+
ManagedIdentitySource expectedManagedIdentitySource)
62+
{
63+
using (new EnvVariableContext())
64+
{
65+
SetUpgradeScenarioEnvironmentVariables(managedIdentitySource, endpoint);
66+
67+
Assert.AreEqual(expectedManagedIdentitySource, ManagedIdentityApplication.GetManagedIdentitySource());
68+
}
69+
}
5070
}
5171
}

0 commit comments

Comments
 (0)