Skip to content

Commit

Permalink
Fail AzureKeyVaultHealthCheck if no keys, certificates or secrets con…
Browse files Browse the repository at this point in the history
…figured
  • Loading branch information
sungam3r committed Jul 23, 2023
1 parent 8f7d707 commit 8d4301c
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 55 deletions.
4 changes: 4 additions & 0 deletions src/HealthChecks.AzureKeyVault/AzureKeyVaultHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
// https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/785
if (_options._keys.Count == 0 && _options.Certificates.Count == 0 && _options._secrets.Count == 0)
return HealthCheckResult.Unhealthy("No keys, certificates or secrets configured.");

foreach (string secret in _options.Secrets)
{
var secretClient = CreateSecretClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task be_healthy_if_arangodb_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public async Task be_healthy_if_multiple_arango_are_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ public async Task be_unhealthy_if_arango_is_not_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Net;
using Azure.Identity;

namespace HealthChecks.AzureKeyVault.Tests.Functional;

public class AzureKeyVaultHealthCheckTests
{
// https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/785
[Fact]
public async Task be_unhealthy_if_keyvalue_is_not_available()
{
var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddHealthChecks()
.AddAzureKeyVault(
new Uri("https://www.thisisnotarealurl.com"),
new DefaultAzureCredential(),
setup =>
{
//setup
// .UseKeyVaultUrl("https://www.thisisnotarealurl2.com")
// .UseAzureManagedServiceIdentity();
});
})
.Configure(app =>
{
app.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true,
});
});

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<ProjectReference Include="..\..\src\HealthChecks.AzureKeyVault\HealthChecks.AzureKeyVault.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task be_healthy_if_consul_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down Expand Up @@ -57,7 +57,7 @@ public async Task be_unhealthy_if_consul_is_not_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task be_healthy_if_elasticsearch_is_using_valid_api_key()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health")
var response = await server.CreateRequest("/health")
.GetAsync()
.ConfigureAwait(false);

Expand Down Expand Up @@ -77,7 +77,7 @@ public async Task be_healthy_if_elasticsearch_is_using_valid_user_and_password()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health")
var response = await server.CreateRequest("/health")
.GetAsync()
.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task be_healthy_if_elasticsearch_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand All @@ -49,7 +49,7 @@ public async Task be_unhealthy_if_elasticsearch_is_not_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task be_healthy_if_eventstore_is_available_with_uri_format()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK, await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
Expand All @@ -50,7 +50,7 @@ public async Task be_healthy_if_eventstore_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK, await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
Expand All @@ -76,7 +76,7 @@ public async Task be_unhealthy_if_eventstore_is_not_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable, await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task be_healthy_if_eventstore_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task be_healthy_if_gremlin_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down Expand Up @@ -63,7 +63,7 @@ public async Task be_healthy_if_multiple_gremlin_are_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task be_unhealthy_if_gremlin_is_not_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task be_healthy_if_ssl_is_valid()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand All @@ -60,7 +60,7 @@ public async Task be_unhealthy_if_ssl_is_not_present()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand All @@ -84,7 +84,7 @@ public async Task be_unhealthy_if_ssl_is_not_valid()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand All @@ -108,7 +108,7 @@ public async Task be_unhealthy_if_ssl_is_expired()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand All @@ -132,7 +132,7 @@ public async Task be_degraded_if_ssl_daysbefore()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
var resultAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task be_unhealthy_if_idsvr_is_unavailable()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand All @@ -47,7 +47,7 @@ public async Task be_healthy_if_idsvr_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task be_healthy_if_rabbitmq_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand All @@ -52,7 +52,7 @@ public async Task be_healthy_if_rabbitmq_is_available_using_ssloption()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand All @@ -76,7 +76,7 @@ public async Task be_unhealthy_if_rabbitmq_is_not_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task be_healthy_if_rabbitmq_is_available_using_iconnectionfactory()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public async Task be_healthy_if_rabbitmq_is_available_using_iconnection()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand All @@ -173,7 +173,7 @@ public async Task be_healthy_if_rabbitmq_is_available_and_specify_default_sslopt

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ public async Task be_not_crash_on_startup_when_rabbitmq_is_down_at_startup()

using var server = new TestServer(webHostBuilder);

var response1 = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response1 = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);
response1.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}

Expand All @@ -234,7 +234,7 @@ public async Task be_healthy_if_rabbitmq_is_available_using_iServiceProvider()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task be_healthy_if_ravendb_is_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK, await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public async Task be_healthy_if_ravendb_is_available_and_contains_specific_datab

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.OK, await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public async Task be_unhealthy_if_ravendb_is_not_available()

using var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health").GetAsync().ConfigureAwait(false);
var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false);

response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable, await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
Expand Down
Loading

0 comments on commit 8d4301c

Please sign in to comment.