Skip to content

Commit

Permalink
Xiao/Enrich metadata failure message during metadata refresh interval (
Browse files Browse the repository at this point in the history
…#2010)

* Enrich exception during metadata refresh interval
  • Loading branch information
ciaozhang authored Feb 14, 2023
1 parent 6217459 commit 6aa3efd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ConfigurationManager<T> : BaseConfigurationManager, IConfigurationM
private readonly IConfigurationRetriever<T> _configRetriever;
private readonly IConfigurationValidator<T> _configValidator;
private T _currentConfiguration;
private Exception _fetchMetadataFailure;

/// <summary>
/// Static initializer for a new object. Static initializers run before the first instance of the type is created.
Expand Down Expand Up @@ -148,6 +149,7 @@ public async Task<T> GetConfigurationAsync(CancellationToken cancel)
}
catch (Exception ex)
{
_fetchMetadataFailure = ex;
_syncAfter = DateTimeUtil.Add(now.UtcDateTime, AutomaticRefreshInterval < RefreshInterval ? AutomaticRefreshInterval : RefreshInterval);
if (_currentConfiguration == null) // Throw an exception if there's no configuration to return.
throw LogHelper.LogExceptionMessage(
Expand All @@ -166,7 +168,7 @@ public async Task<T> GetConfigurationAsync(CancellationToken cancel)
else
throw LogHelper.LogExceptionMessage(
new InvalidOperationException(
LogHelper.FormatInvariant(LogMessages.IDX20803, LogHelper.MarkAsNonPII(MetadataAddress ?? "null"))));
LogHelper.FormatInvariant(LogMessages.IDX20803, LogHelper.MarkAsNonPII(MetadataAddress ?? "null"), LogHelper.MarkAsNonPII(_fetchMetadataFailure)), _fetchMetadataFailure));
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using Microsoft.IdentityModel.Protocols.OpenIdConnect.Configuration;
Expand Down Expand Up @@ -100,6 +101,41 @@ public void Defaults()
Assert.Equal(ConfigurationManager<OpenIdConnectConfiguration>.MinimumRefreshInterval, new TimeSpan(0, 0, 0, 1));
}

[Fact]
public void FetchMetadataFailureTest()
{
var context = new CompareContext($"{this}.FetchMetadataFailureTest");

var documentRetriever = new HttpDocumentRetriever(HttpResponseMessageUtils.SetupHttpClientThatReturns("OpenIdConnectMetadata.json", HttpStatusCode.NotFound));
var configManager = new ConfigurationManager<OpenIdConnectConfiguration>("OpenIdConnectMetadata.json", new OpenIdConnectConfigurationRetriever(), documentRetriever);

// First time to fetch metadata
try
{
var configuration = configManager.GetConfigurationAsync().Result;
}
catch (Exception firstFetchMetadataFailure)
{
if (firstFetchMetadataFailure.InnerException == null)
context.AddDiff($"Expected exception to contain inner exception for fetch metadata failure.");

// Fetch metadata again during refresh interval, the exception should be same from above
try
{
var configuration = configManager.GetConfigurationAsync().Result;
}
catch (Exception secondFetchMetadataFailure)
{
if (secondFetchMetadataFailure.InnerException == null)
context.AddDiff($"Expected exception to contain inner exception for fetch metadata failure.");

IdentityComparer.AreEqual(firstFetchMetadataFailure, secondFetchMetadataFailure, context);
}
}

TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void GetSets()
{
Expand Down

0 comments on commit 6aa3efd

Please sign in to comment.