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

2261 Add basic authentication support to Solr health check #2263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static IHealthChecksBuilder AddSolr(
TimeSpan? timeout = default)
{
var options = new SolrOptions();
options.UseServer(solrUri, solrCore, timeout: null);
options.UseServer(solrUri, solrCore, username: null, password: null, timeout: null);

return builder.Add(new HealthCheckRegistration(
name ?? NAME,
Expand Down
6 changes: 6 additions & 0 deletions src/HealthChecks.Solr/SolrHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using HttpWebAdapters;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using SolrNet.Impl;

Expand Down Expand Up @@ -29,6 +30,11 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
Timeout = (int)_options.Timeout.TotalMilliseconds
};

if (!string.IsNullOrWhiteSpace(_options.Username) && !string.IsNullOrWhiteSpace(_options.Password))
{
solrConnection.HttpWebRequestFactory = new BasicAuthHttpWebRequestFactory(_options.Username, _options.Password);
}

if (!_connections.TryAdd(url, solrConnection))
{
solrConnection = _connections[url];
Expand Down
8 changes: 7 additions & 1 deletion src/HealthChecks.Solr/SolrOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ public class SolrOptions

public string Core { get; private set; } = null!;

public string? Username { get; private set; }

public string? Password { get; private set; }

public TimeSpan Timeout { get; private set; }

public SolrOptions UseServer(string uri, string core, TimeSpan? timeout)
public SolrOptions UseServer(string uri, string core, string? username, string? password, TimeSpan? timeout)
{
Uri = Guard.ThrowIfNull(uri);
Core = Guard.ThrowIfNull(core);
Username = username;
Password = password;
Timeout = timeout ?? TimeSpan.FromMilliseconds(1000);

return this;
Expand Down
23 changes: 23 additions & 0 deletions test/HealthChecks.Solr.Tests/Functional/SolrHealthCheckTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ public async Task be_healthy_if_solr_is_available()
response.StatusCode.ShouldBe(HttpStatusCode.OK, await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task be_healthy_if_solr_with_credentials_is_available()
{
var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddHealthChecks()
.AddSolr(options => options.UseServer("http://localhost:8983/solr", "solrcore", "solr", "SolrRocks", null), tags: new string[] { "solr" });
})
.Configure(app =>
{
app.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("solr"),
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
});

using var server = new TestServer(webHostBuilder);
using var response = await server.CreateRequest("/health").GetAsync();
response.StatusCode.ShouldBe(HttpStatusCode.OK, await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task be_unhealthy_if_solr_ping_is_disabled()
{
Expand Down
Loading