Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idsvr uriprovider #637

Merged
merged 12 commits into from
Jul 5, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,49 @@ public static IHealthChecksBuilder AddIdentityServer(
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="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
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddIdentityServer(
this IHealthChecksBuilder builder,
Func<IServiceProvider, Uri> uriProvider,
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;
});

return builder.Add(new HealthCheckRegistration(
registrationName,
sp => new IdSvrHealthCheck(() => sp.GetRequiredService<IHttpClientFactory>().CreateClient(registrationName)),
failureStatus,
tags,
timeout));
}
private static HttpClient CreateIdentityServerHttpClient(IServiceProvider sp, string registrationName, Func<IServiceProvider, Uri> uriProvider)
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
{
var authorityUri = uriProvider(sp);
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var client = httpClientFactory.CreateClient(registrationName);
client.BaseAddress = authorityUri;

return client;
}
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ public void add_named_health_check_when_properly_configured()
var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.Should().Be("my-idsvr-group");
check.GetType().Should().Be(typeof(IdSvrHealthCheck));
}
[Fact]
public void add_health_check_when_properly_configured_with_uri_provider()
{
var services = new ServiceCollection();
services.AddHealthChecks()
.AddIdentityServer(sp => new Uri("http://myidsvr"));

using var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.Should().Be("idsvr");
check.GetType().Should().Be(typeof(IdSvrHealthCheck));
}
[Fact]
public void add_named_health_check_when_properly_configured_with_uri_provider()
{
var services = new ServiceCollection();
services.AddHealthChecks()
.AddIdentityServer(sp => new Uri("http://myidsvr"), name: "my-idsvr-group");

using var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.Should().Be("my-idsvr-group");
check.GetType().Should().Be(typeof(IdSvrHealthCheck));
}
Expand Down