diff --git a/src/HealthChecks.OpenIdConnectServer/DependencyInjection/IdSvrHealthCheckBuilderExtensions.cs b/src/HealthChecks.OpenIdConnectServer/DependencyInjection/IdSvrHealthCheckBuilderExtensions.cs index cf62a2af8a..cad4e41af8 100644 --- a/src/HealthChecks.OpenIdConnectServer/DependencyInjection/IdSvrHealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.OpenIdConnectServer/DependencyInjection/IdSvrHealthCheckBuilderExtensions.cs @@ -9,12 +9,14 @@ namespace Microsoft.Extensions.DependencyInjection; public static class IdSvrHealthCheckBuilderExtensions { private const string NAME = "idsvr"; + internal const string IDSVR_DISCOVER_CONFIGURATION_SEGMENT = ".well-known/openid-configuration"; /// /// Add a health check for Identity Server. /// /// The . /// The uri of the Identity Server to check. + /// Identity Server discover configuration segment. /// The health check name. Optional. If null the type name 'idsvr' will be used for the name. /// /// The that should be reported when the health check fails. Optional. If null then @@ -26,6 +28,7 @@ public static class IdSvrHealthCheckBuilderExtensions public static IHealthChecksBuilder AddIdentityServer( this IHealthChecksBuilder builder, Uri idSvrUri, + string discoverConfigurationSegment = IDSVR_DISCOVER_CONFIGURATION_SEGMENT, string? name = default, HealthStatus? failureStatus = default, IEnumerable? tags = default, @@ -37,16 +40,18 @@ public static IHealthChecksBuilder AddIdentityServer( return builder.Add(new HealthCheckRegistration( registrationName, - sp => new IdSvrHealthCheck(() => sp.GetRequiredService().CreateClient(registrationName)), + sp => new IdSvrHealthCheck(() => sp.GetRequiredService().CreateClient(registrationName), discoverConfigurationSegment), failureStatus, tags, timeout)); } + /// /// Add a health check for Identity Server. /// /// The . /// Factory for providing the uri of the Identity Server to check. + /// Identity Server discover configuration segment. /// The health check name. Optional. If null the type name 'idsvr' will be used for the name. /// /// The that should be reported when the health check fails. Optional. If null then @@ -57,6 +62,7 @@ public static IHealthChecksBuilder AddIdentityServer( public static IHealthChecksBuilder AddIdentityServer( this IHealthChecksBuilder builder, Func uriProvider, + string discoverConfigurationSegment = IDSVR_DISCOVER_CONFIGURATION_SEGMENT, string? name = null, HealthStatus? failureStatus = null, IEnumerable? tags = null, @@ -64,15 +70,11 @@ public static IHealthChecksBuilder AddIdentityServer( { var registrationName = name ?? NAME; - builder.Services.AddHttpClient(registrationName, (sp, client) => - { - var idSvrUri = uriProvider(sp); - client.BaseAddress = idSvrUri; - }); + builder.Services.AddHttpClient(registrationName, (sp, client) => client.BaseAddress = uriProvider(sp)); return builder.Add(new HealthCheckRegistration( registrationName, - sp => new IdSvrHealthCheck(() => sp.GetRequiredService().CreateClient(registrationName)), + sp => new IdSvrHealthCheck(() => sp.GetRequiredService().CreateClient(registrationName), discoverConfigurationSegment), failureStatus, tags, timeout)); diff --git a/src/HealthChecks.OpenIdConnectServer/IdSvrHealthCheck.cs b/src/HealthChecks.OpenIdConnectServer/IdSvrHealthCheck.cs index ef086ae3cf..62a39e7a97 100644 --- a/src/HealthChecks.OpenIdConnectServer/IdSvrHealthCheck.cs +++ b/src/HealthChecks.OpenIdConnectServer/IdSvrHealthCheck.cs @@ -1,16 +1,22 @@ +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; namespace HealthChecks.IdSvr; public class IdSvrHealthCheck : IHealthCheck { - private const string IDSVR_DISCOVER_CONFIGURATION_SEGMENT = ".well-known/openid-configuration"; - private readonly Func _httpClientFactory; + private readonly string _discoverConfigurationSegment; public IdSvrHealthCheck(Func httpClientFactory) + : this(httpClientFactory, IdSvrHealthCheckBuilderExtensions.IDSVR_DISCOVER_CONFIGURATION_SEGMENT) + { + } + + public IdSvrHealthCheck(Func httpClientFactory, string discoverConfigurationSegment) { _httpClientFactory = Guard.ThrowIfNull(httpClientFactory); + _discoverConfigurationSegment = discoverConfigurationSegment; } /// @@ -19,7 +25,7 @@ public async Task CheckHealthAsync(HealthCheckContext context try { var httpClient = _httpClientFactory(); - using var response = await httpClient.GetAsync(IDSVR_DISCOVER_CONFIGURATION_SEGMENT, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + using var response = await httpClient.GetAsync(_discoverConfigurationSegment, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); return response.IsSuccessStatusCode ? HealthCheckResult.Healthy() diff --git a/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.approved.txt b/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.approved.txt index 299ff2777b..6921a50bd0 100644 --- a/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.approved.txt +++ b/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.approved.txt @@ -3,6 +3,7 @@ namespace HealthChecks.IdSvr public class IdSvrHealthCheck : Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck { public IdSvrHealthCheck(System.Func httpClientFactory) { } + public IdSvrHealthCheck(System.Func httpClientFactory, string discoverConfigurationSegment) { } public System.Threading.Tasks.Task CheckHealthAsync(Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckContext context, System.Threading.CancellationToken cancellationToken = default) { } } } @@ -10,7 +11,7 @@ namespace Microsoft.Extensions.DependencyInjection { public static class IdSvrHealthCheckBuilderExtensions { - public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func uriProvider, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } - public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Uri idSvrUri, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } + public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func uriProvider, string discoverConfigurationSegment = ".well-known/openid-configuration", string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } + public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Uri idSvrUri, string discoverConfigurationSegment = ".well-known/openid-configuration", string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } } } \ No newline at end of file