Skip to content

Commit fddb2c4

Browse files
committedJun 6, 2024
Update as discussed
1 parent d2201cf commit fddb2c4

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed
 

‎src/client/Microsoft.Identity.Client/ManagedIdentity/AzureArcManagedIdentitySource.cs

+24-33
Original file line numberDiff line numberDiff line change
@@ -138,36 +138,18 @@ private void ValidateSplitChallenge(string[] splitChallenge)
138138
}
139139

140140
_requestContext.Logger.Verbose(() => $"[Managed Identity] Challenge is valid. FilePath: {splitChallenge[1]}");
141+
string path = Path.GetFullPath(new Uri(splitChallenge[1]).LocalPath)
142+
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
141143

142-
if (DesktopOsHelper.IsWindows())
143-
{
144-
if (!IsValidWindowsPath(splitChallenge[1]))
145-
{
146-
throw CreateManagedIdentityException(
147-
MsalError.ManagedIdentityRequestFailed,
148-
MsalErrorMessage.ManagedIdentityInvalidFile);
149-
}
150-
151-
_requestContext.Logger.Verbose(() => "[Managed Identity] Windows path is valid.");
152-
}
153-
else if (DesktopOsHelper.IsLinux())
154-
{
155-
if (!IsValidLinuxPath(splitChallenge[1]))
156-
{
157-
throw CreateManagedIdentityException(
158-
MsalError.ManagedIdentityRequestFailed,
159-
MsalErrorMessage.ManagedIdentityInvalidFile);
160-
}
161-
162-
_requestContext.Logger.Verbose(() => "[Managed Identity] Linux path is valid.");
163-
}
164-
else
144+
if (!IsValidPath(splitChallenge[1]))
165145
{
166146
throw CreateManagedIdentityException(
167147
MsalError.ManagedIdentityRequestFailed,
168-
MsalErrorMessage.ManagedIdentityPlatformNotSupported);
148+
MsalErrorMessage.ManagedIdentityInvalidFile);
169149
}
170150

151+
_requestContext.Logger.Verbose(() => $"[Managed Identity] File path is valid. Path: {path}");
152+
171153
var length = new FileInfo(splitChallenge[1]).Length;
172154

173155
if ((!File.Exists(splitChallenge[1]) || (length) > 4096))
@@ -191,19 +173,28 @@ private MsalException CreateManagedIdentityException(string errorCode, string er
191173
null);
192174
}
193175

194-
private bool IsValidLinuxPath(string path)
176+
private bool IsValidPath(string path)
195177
{
196-
string linuxPath = "/var/opt/azcmagent/tokens/";
178+
string expectedFilePath;
197179

198-
return path.StartsWith(linuxPath, StringComparison.OrdinalIgnoreCase) &&
199-
path.EndsWith(".key", StringComparison.OrdinalIgnoreCase);
200-
}
180+
if (DesktopOsHelper.IsWindows())
181+
{
182+
string expandedExpectedPath = Environment.ExpandEnvironmentVariables("%ProgramData%\\AzureConnectedMachineAgent\\Tokens\\");
201183

202-
private bool IsValidWindowsPath(string path)
203-
{
204-
string expandedExpectedPath = Environment.ExpandEnvironmentVariables("%ProgramData%\\AzureConnectedMachineAgent\\Tokens\\");
184+
expectedFilePath = expandedExpectedPath + Path.GetFileName(path);
185+
}
186+
else if (DesktopOsHelper.IsLinux())
187+
{
188+
expectedFilePath = "/var/opt/azcmagent/tokens/" + Path.GetFileName(path);
189+
}
190+
else
191+
{
192+
throw CreateManagedIdentityException(
193+
MsalError.ManagedIdentityRequestFailed,
194+
MsalErrorMessage.ManagedIdentityPlatformNotSupported);
195+
}
205196

206-
return path.StartsWith(expandedExpectedPath, StringComparison.OrdinalIgnoreCase) &&
197+
return path.Equals(expectedFilePath, StringComparison.OrdinalIgnoreCase) &&
207198
path.EndsWith(".key", StringComparison.OrdinalIgnoreCase);
208199
}
209200
}

‎tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/AzureArcTests.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ await mi.AcquireTokenForManagedIdentity("scope")
8181

8282
[DataTestMethod]
8383
[DataRow("somefile=filename", MsalErrorMessage.ManagedIdentityInvalidChallenge)]
84-
[DataRow("path/filename", MsalErrorMessage.ManagedIdentityInvalidFile)]
84+
[DataRow("C:\\ProgramData\\AzureConnectedMachineAgent\\Tokens\\filename.txt", MsalErrorMessage.ManagedIdentityInvalidFile)]
85+
[DataRow("C:\\ProgramData\\AzureConnectedMachineAgent\\Tokens\\...\\etc\\filename.key", MsalErrorMessage.ManagedIdentityInvalidFile)]
8586
public async Task AzureArcAuthHeaderInvalidAsync(string filename, string errorMessage)
8687
{
8788
using (new EnvVariableContext())
@@ -97,7 +98,7 @@ public async Task AzureArcAuthHeaderInvalidAsync(string filename, string errorMe
9798

9899
var mi = miBuilder.Build();
99100

100-
httpManager.AddManagedIdentityWSTrustMockHandler(ManagedIdentityTests.AzureArcEndpoint, "somevalue=filepath");
101+
httpManager.AddManagedIdentityWSTrustMockHandler(ManagedIdentityTests.AzureArcEndpoint, filename);
101102

102103
MsalServiceException ex = await Assert.ThrowsExceptionAsync<MsalServiceException>(async () =>
103104
await mi.AcquireTokenForManagedIdentity("scope")
@@ -106,7 +107,7 @@ await mi.AcquireTokenForManagedIdentity("scope")
106107
Assert.IsNotNull(ex);
107108
Assert.AreEqual(ManagedIdentitySource.AzureArc.ToString(), ex.AdditionalExceptionData[MsalException.ManagedIdentitySource]);
108109
Assert.AreEqual(MsalError.ManagedIdentityRequestFailed, ex.ErrorCode);
109-
Assert.AreEqual(MsalErrorMessage.ManagedIdentityInvalidChallenge, ex.Message);
110+
Assert.AreEqual(errorMessage, ex.Message);
110111
}
111112
}
112113

0 commit comments

Comments
 (0)