Skip to content

Commit 40e1e3f

Browse files
committed
diagnose: check both Entra token caches
The Entra diagnostic only exercises the shared Microsoft developer tools cache. Problems with GCM's application-specific cache can therefore go undetected, while stopping at the first failure would hide the state of the other cache. Run the persistence checks for both caches and collect their failures independently. Flatten aggregate exceptions in the diagnostic log so each cache-specific failure remains visible while the diagnostic still reports a single failure. Assisted-by: GPT-5.6 Sol Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent 7a67ac6 commit 40e1e3f

2 files changed

Lines changed: 75 additions & 35 deletions

File tree

‎src/Core/Commands/DiagnoseCommand.cs‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,7 @@ private async Task<int> ExecuteAsync(string output)
114114
DiagnosticResult result = await diagnostic.RunAsync();
115115
fullLog.WriteLine("Success: {0}", result.IsSuccess);
116116

117-
if (result.Exception is null)
118-
{
119-
fullLog.WriteLine("Exception: None");
120-
}
121-
else
122-
{
123-
fullLog.WriteLine("Exception:");
124-
fullLog.WriteLine(result.Exception.ToString());
125-
}
117+
WriteException(fullLog, result.Exception);
126118

127119
fullLog.WriteLine("Log:");
128120
fullLog.WriteLine(result.DiagnosticLog);
@@ -195,6 +187,29 @@ private async Task<int> ExecuteAsync(string output)
195187
return numFailed;
196188
}
197189

190+
private void WriteException(StreamWriter log, Exception exception)
191+
{
192+
if (exception is null)
193+
{
194+
log.WriteLine("Exception: None");
195+
return;
196+
}
197+
198+
if (exception is AggregateException aex)
199+
{
200+
log.WriteLine("Exception: AggregateException");
201+
log.WriteLine("InnerExceptions (flattened):");
202+
foreach (var inner in aex.Flatten().InnerExceptions)
203+
{
204+
log.WriteLine(inner.ToString());
205+
}
206+
}
207+
else
208+
{
209+
log.WriteLine("Exception: {0}", exception);
210+
}
211+
}
212+
198213
private static class ConsoleEx
199214
{
200215
public static void WriteLineIndent(string str)

‎src/Core/Diagnostics/EntraAuthenticationDiagnostic.cs‎

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,73 @@ public EntraAuthenticationDiagnostic(ICommandContext context)
1515

1616
protected override async Task<bool> RunInternalAsync(StringBuilder log, IList<string> additionalFiles)
1717
{
18-
var entraAuth = new EntraAuthentication(CommandContext, new PublicClientConfig
19-
{
20-
UseSharedCache = true,
21-
});
18+
var failures = new List<Exception>();
2219

23-
log.Append("Gathering MSAL token cache data...");
24-
StorageCreationProperties cacheProps = entraAuth.CreateUserTokenCacheProps(true);
25-
log.AppendLine(" OK");
26-
log.AppendLine($"CacheDirectory: {cacheProps.CacheDirectory}");
27-
log.AppendLine($"CacheFileName: {cacheProps.CacheFileName}");
28-
log.AppendLine($"CacheFilePath: {cacheProps.CacheFilePath}");
20+
await RunCacheDiagnosticAsync("Shared Microsoft developer tools", true, log, failures);
21+
log.AppendLine();
22+
await RunCacheDiagnosticAsync("Git Credential Manager", false, log, failures);
2923

30-
if (PlatformUtils.IsMacOS())
24+
if (failures.Count == 1)
3125
{
32-
log.AppendLine($"MacKeyChainAccountName: {cacheProps.MacKeyChainAccountName}");
33-
log.AppendLine($"MacKeyChainServiceName: {cacheProps.MacKeyChainServiceName}");
26+
throw failures[0];
3427
}
35-
else if (PlatformUtils.IsLinux())
28+
29+
if (failures.Count > 1)
3630
{
37-
log.AppendLine($"KeyringCollection: {cacheProps.KeyringCollection}");
38-
log.AppendLine($"KeyringSchemaName: {cacheProps.KeyringSchemaName}");
39-
log.AppendLine($"KeyringSecretLabel: {cacheProps.KeyringSecretLabel}");
40-
log.AppendLine($"KeyringAttribute1: ({cacheProps.KeyringAttribute1.Key},{cacheProps.KeyringAttribute1.Value})");
41-
log.AppendLine($"KeyringAttribute2: ({cacheProps.KeyringAttribute2.Key},{cacheProps.KeyringAttribute2.Value})");
31+
throw new AggregateException("Multiple MSAL token cache diagnostics failed.", failures);
4232
}
4333

44-
log.Append("Creating cache helper...");
45-
var cacheHelper = await MsalCacheHelper.CreateAsync(cacheProps);
46-
log.AppendLine(" OK");
34+
return true;
35+
}
36+
37+
private async Task RunCacheDiagnosticAsync(
38+
string cacheName,
39+
bool useSharedCache,
40+
StringBuilder log,
41+
ICollection<Exception> failures)
42+
{
43+
log.AppendLine($"{cacheName} cache");
44+
45+
var entraAuth = new EntraAuthentication(CommandContext, new PublicClientConfig
46+
{
47+
UseSharedCache = useSharedCache,
48+
});
49+
4750
try
4851
{
52+
log.Append("Gathering MSAL token cache data...");
53+
StorageCreationProperties cacheProps = entraAuth.CreateUserTokenCacheProps(true);
54+
log.AppendLine(" OK");
55+
log.AppendLine($"CacheDirectory: {cacheProps.CacheDirectory}");
56+
log.AppendLine($"CacheFileName: {cacheProps.CacheFileName}");
57+
log.AppendLine($"CacheFilePath: {cacheProps.CacheFilePath}");
58+
59+
if (PlatformUtils.IsMacOS())
60+
{
61+
log.AppendLine($"MacKeyChainAccountName: {cacheProps.MacKeyChainAccountName}");
62+
log.AppendLine($"MacKeyChainServiceName: {cacheProps.MacKeyChainServiceName}");
63+
}
64+
else if (PlatformUtils.IsLinux())
65+
{
66+
log.AppendLine($"KeyringCollection: {cacheProps.KeyringCollection}");
67+
log.AppendLine($"KeyringSchemaName: {cacheProps.KeyringSchemaName}");
68+
log.AppendLine($"KeyringSecretLabel: {cacheProps.KeyringSecretLabel}");
69+
log.AppendLine($"KeyringAttribute1: ({cacheProps.KeyringAttribute1.Key},{cacheProps.KeyringAttribute1.Value})");
70+
log.AppendLine($"KeyringAttribute2: ({cacheProps.KeyringAttribute2.Key},{cacheProps.KeyringAttribute2.Value})");
71+
}
72+
73+
log.Append("Creating cache helper...");
74+
var cacheHelper = await MsalCacheHelper.CreateAsync(cacheProps);
75+
log.AppendLine(" OK");
4976
log.Append("Verifying MSAL token cache persistence...");
5077
cacheHelper.VerifyPersistence();
5178
log.AppendLine(" OK");
5279
}
53-
catch (Exception)
80+
catch (Exception ex)
5481
{
5582
log.AppendLine(" Failed");
56-
throw;
83+
failures.Add(new Exception($"{cacheName} cache diagnostic failed.", ex));
5784
}
58-
59-
return true;
6085
}
6186
}
6287
}

0 commit comments

Comments
 (0)