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

Add support for Basic Auth to ConsulHealthCheck #137

Merged
merged 3 commits into from
Apr 9, 2019
Merged
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
10 changes: 9 additions & 1 deletion src/HealthChecks.Consul/ConsulHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -19,7 +20,14 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
var result = await _httpClientFactory()
var client = _httpClientFactory();

if (_options.RequireBasicAuthentication) {
var authHeaderValue = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", _options.Username, _options.Password)));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
}

var result = await client
.GetAsync($"{(_options.RequireHttps ? "https" : "http")}://{_options.HostName}:{_options.Port}/v1/status/leader", cancellationToken);

return result.IsSuccessStatusCode ? HealthCheckResult.Healthy() : new HealthCheckResult(context.Registration.FailureStatus, description: "Consul response was not a successful HTTP status code");
Expand Down
6 changes: 6 additions & 0 deletions src/HealthChecks.Consul/ConsulOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ public class ConsulOptions
public int Port { get; set; }

public bool RequireHttps { get; set; }

riker09 marked this conversation as resolved.
Show resolved Hide resolved
public bool RequireBasicAuthentication { get; set; }

public string Username { get; set; }

public string Password { get; set; }
}
}