Skip to content

Commit

Permalink
Expose Identity Server discover configuration segment
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jul 23, 2023
1 parent 07d59d6 commit 8c55d4d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/// <summary>
/// Add a health check for Identity Server.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="idSvrUri">The uri of the Identity Server to check.</param>
/// <param name="discoverConfigurationSegment">Identity Server discover configuration segment.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'idsvr' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
Expand All @@ -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<string>? tags = default,
Expand All @@ -37,16 +40,18 @@ public static IHealthChecksBuilder AddIdentityServer(

return builder.Add(new HealthCheckRegistration(
registrationName,
sp => new IdSvrHealthCheck(() => sp.GetRequiredService<IHttpClientFactory>().CreateClient(registrationName)),
sp => new IdSvrHealthCheck(() => sp.GetRequiredService<IHttpClientFactory>().CreateClient(registrationName), discoverConfigurationSegment),
failureStatus,
tags,
timeout));
}

/// <summary>
/// Add a health check for Identity Server.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="uriProvider">Factory for providing the uri of the Identity Server to check.</param>
/// <param name="discoverConfigurationSegment">Identity Server discover configuration segment.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'idsvr' will be used for the name.</param>
/// <param name="failureStatus"></param>
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
Expand All @@ -57,22 +62,19 @@ public static IHealthChecksBuilder AddIdentityServer(
public static IHealthChecksBuilder AddIdentityServer(
this IHealthChecksBuilder builder,
Func<IServiceProvider, Uri> uriProvider,
string discoverConfigurationSegment = IDSVR_DISCOVER_CONFIGURATION_SEGMENT,
string? name = null,
HealthStatus? failureStatus = null,
IEnumerable<string>? tags = null,
TimeSpan? timeout = null)
{
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<IHttpClientFactory>().CreateClient(registrationName)),
sp => new IdSvrHealthCheck(() => sp.GetRequiredService<IHttpClientFactory>().CreateClient(registrationName), discoverConfigurationSegment),
failureStatus,
tags,
timeout));
Expand Down
12 changes: 9 additions & 3 deletions src/HealthChecks.OpenIdConnectServer/IdSvrHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -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<HttpClient> _httpClientFactory;
private readonly string _discoverConfigurationSegment;

public IdSvrHealthCheck(Func<HttpClient> httpClientFactory)
: this(httpClientFactory, IdSvrHealthCheckBuilderExtensions.IDSVR_DISCOVER_CONFIGURATION_SEGMENT)
{
}

public IdSvrHealthCheck(Func<HttpClient> httpClientFactory, string discoverConfigurationSegment)
{
_httpClientFactory = Guard.ThrowIfNull(httpClientFactory);
_discoverConfigurationSegment = discoverConfigurationSegment;
}

/// <inheritdoc />
Expand All @@ -19,7 +25,7 @@ public async Task<HealthCheckResult> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ namespace HealthChecks.IdSvr
public class IdSvrHealthCheck : Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
{
public IdSvrHealthCheck(System.Func<System.Net.Http.HttpClient> httpClientFactory) { }
public IdSvrHealthCheck(System.Func<System.Net.Http.HttpClient> httpClientFactory, string discoverConfigurationSegment) { }
public System.Threading.Tasks.Task<Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult> CheckHealthAsync(Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckContext context, System.Threading.CancellationToken cancellationToken = default) { }
}
}
namespace Microsoft.Extensions.DependencyInjection
{
public static class IdSvrHealthCheckBuilderExtensions
{
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func<System.IServiceProvider, System.Uri> uriProvider, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? 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<string>? tags = null, System.TimeSpan? timeout = default) { }
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddIdentityServer(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Func<System.IServiceProvider, System.Uri> uriProvider, string discoverConfigurationSegment = ".well-known/openid-configuration", string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? 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<string>? tags = null, System.TimeSpan? timeout = default) { }
}
}

0 comments on commit 8c55d4d

Please sign in to comment.