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 health cheks vault #2249

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions ApiMarker.cs
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 14 additions & 0 deletions AspNetCore.Diagnostics.HealthChecks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.Azure.Messagin
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.AzureServiceBus.Tests", "test\HealthChecks.AzureServiceBus.Tests\HealthChecks.AzureServiceBus.Tests.csproj", "{3B812989-2C4E-4FCE-B3A0-EF9C00A9B3A5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthCheks.Vault", "HealthCheks.Vault\HealthCheks.Vault.csproj", "{510505AA-05FC-42F2-80BB-74281D3C31E9}"
EndProject
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecks.Vault.Core.Tests", "HealthChecks.Vault.Core.Tests\HealthChecks.Vault.Core.Tests.csproj", "{CE2DED0F-917F-439E-BBD8-48A6C0C79F11}"
EndProject
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -856,6 +860,14 @@ Global
{3B812989-2C4E-4FCE-B3A0-EF9C00A9B3A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B812989-2C4E-4FCE-B3A0-EF9C00A9B3A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B812989-2C4E-4FCE-B3A0-EF9C00A9B3A5}.Release|Any CPU.Build.0 = Release|Any CPU
{510505AA-05FC-42F2-80BB-74281D3C31E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{510505AA-05FC-42F2-80BB-74281D3C31E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{510505AA-05FC-42F2-80BB-74281D3C31E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{510505AA-05FC-42F2-80BB-74281D3C31E9}.Release|Any CPU.Build.0 = Release|Any CPU
{CE2DED0F-917F-439E-BBD8-48A6C0C79F11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE2DED0F-917F-439E-BBD8-48A6C0C79F11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE2DED0F-917F-439E-BBD8-48A6C0C79F11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE2DED0F-917F-439E-BBD8-48A6C0C79F11}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -997,6 +1009,8 @@ Global
{2C02BEDF-FFFA-4747-920A-A1ED6CFFC21A} = {FF4414C2-8863-4ADA-8A1D-4B9F25C361FE}
{09160C0F-BB5E-4DF2-AF44-E859E8EDE6BD} = {FF4414C2-8863-4ADA-8A1D-4B9F25C361FE}
{3B812989-2C4E-4FCE-B3A0-EF9C00A9B3A5} = {FF4414C2-8863-4ADA-8A1D-4B9F25C361FE}
{510505AA-05FC-42F2-80BB-74281D3C31E9} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{CE2DED0F-917F-439E-BBD8-48A6C0C79F11} = {FF4414C2-8863-4ADA-8A1D-4B9F25C361FE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B8C62A1-11B6-469F-874C-A02443256568}
Expand Down
1 change: 1 addition & 0 deletions CallerArgumentExpressionAttribute.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of having this empty file?

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

30 changes: 30 additions & 0 deletions HealthChecks.Vault.Core.Tests/HealthChecks.Vault.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageReference Include="Shouldly" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HealthCheks.Vault\HealthCheks.Vault.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
206 changes: 206 additions & 0 deletions HealthChecks.Vault.Core.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using HealthCheks.Vault;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using Moq;
using Shouldly;
using VaultSharp;
using VaultSharp.V1.SystemBackend;

namespace HealthChecks.Vault.Core.Tests;

public class UnitTest1
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename class name UnitTest1 to RegistrationTests

protected readonly string _defaultCheckName = "vault";

[Fact]
public void AddHealthCheck_WithBasicAuthentication_ShouldBeProperlyConfigured()
{
// Arrange
var services = new ServiceCollection();
var options = new VaultHealthCheckOptions()
.UseBasicAuthentication("basic-token")
.WithVaultAddress("http://127.0.0.1:8200");
services.AddSingleton(options);
services.AddSingleton<IHealthCheck, HealthChecksVault>();

services.AddHealthChecks()
.AddCheck<HealthChecksVault>(_defaultCheckName);

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

try
{
// Act
var registration = healthCheckOptions.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

// Assert
registration.Name.ShouldBe(_defaultCheckName);
check.ShouldBeOfType<HealthChecksVault>();
}
finally
{
// Dispose the service provider
if (serviceProvider is IDisposable disposable)
{
disposable.Dispose();
}
}
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void AddHealthCheck_WithRadiusAuthentication_ShouldBeProperlyConfigured()
{
// Arrange
var services = new ServiceCollection();
var options = new VaultHealthCheckOptions()
.UseRadiusAuthentication("radius-password", "radius-username")
.WithVaultAddress("http://127.0.0.1:8200");
services.AddSingleton(options);
services.AddSingleton<IHealthCheck, HealthChecksVault>();

services.AddHealthChecks()
.AddCheck<HealthChecksVault>(_defaultCheckName);

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

try
{
// Act
var registration = healthCheckOptions.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

// Assert
registration.Name.ShouldBe(_defaultCheckName);
check.ShouldBeOfType<HealthChecksVault>();
}
finally
{
// Dispose the service provider
if (serviceProvider is IDisposable disposable)
{
disposable.Dispose();
}
}
}
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public void AddHealthCheck_WithLdapAuthentication_ShouldBeProperlyConfigured()
{
// Arrange
var services = new ServiceCollection();
var options = new VaultHealthCheckOptions()
.UseLdapAuthentication("ldap-password", "ldap-username")
.WithVaultAddress("http://127.0.0.1:8200");
services.AddSingleton(options);
services.AddSingleton<IHealthCheck, HealthChecksVault>();

services.AddHealthChecks()
.AddCheck<HealthChecksVault>(_defaultCheckName);

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

try
{
// Act
var registration = healthCheckOptions.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

// Assert
registration.Name.ShouldBe(_defaultCheckName);
check.ShouldBeOfType<HealthChecksVault>();
}
finally
{
// Dispose the service provider
if (serviceProvider is IDisposable disposable)
{
disposable.Dispose();
}
}
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void AddHealthCheck_WithOktaAuthentication_ShouldBeProperlyConfigured()
{
// Arrange
var services = new ServiceCollection();
var options = new VaultHealthCheckOptions()
.UseOktaAuthentication("okta-password", "okta-username")
.WithVaultAddress("http://127.0.0.1:8200");
services.AddSingleton(options);
services.AddSingleton<IHealthCheck, HealthChecksVault>();

services.AddHealthChecks()
.AddCheck<HealthChecksVault>(_defaultCheckName);

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

try
{
// Act
var registration = healthCheckOptions.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

// Assert
registration.Name.ShouldBe(_defaultCheckName);
check.ShouldBeOfType<HealthChecksVault>();
}
finally
{
// Dispose the service provider
if (serviceProvider is IDisposable disposable)
{
disposable.Dispose();
}
}
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public async Task CheckHealthAsync_VaultHealthy_ShouldReturnHealthyAsync()
{
var services = new ServiceCollection();

var mockVaultClient = new Mock<IVaultClient>();
services.AddSingleton<IVaultClient>(mockVaultClient.Object);
var cancellationToken = CancellationToken.None;
var mockSystemBackend = new Mock<ISystemBackend>();
mockSystemBackend
.Setup(m => m.GetHealthStatusAsync(false, 200, 429, 503, 501, null))
.ReturnsAsync(new VaultSharp.V1.SystemBackend.HealthStatus { Initialized = true, Sealed = false });

mockVaultClient.Setup(m => m.V1.System).Returns(mockSystemBackend.Object);

var options = new VaultHealthCheckOptions()
.UseBasicAuthentication("basic-token")
.WithVaultAddress("http://127.0.0.1:8200");

services.AddSingleton(options);
services.AddSingleton<IHealthCheck, HealthChecksVault>();

var serviceProvider = services.BuildServiceProvider();
var healthChecksVault = serviceProvider.GetRequiredService<IHealthCheck>();

try
{
// Act
var result = await healthChecksVault.CheckHealthAsync(new HealthCheckContext()).ConfigureAwait(true);

// Assert
result.Status.ShouldBe(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Healthy);
}
finally
{
// Dispose the service provider
if (serviceProvider is IDisposable disposable)
{
disposable.Dispose();
}
}
}
}
SaeedSafi1999 marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading