-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathAzureKeyVaultKeysHealthCheck.cs
More file actions
36 lines (31 loc) · 1.25 KB
/
AzureKeyVaultKeysHealthCheck.cs
File metadata and controls
36 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using Azure;
using Azure.Security.KeyVault.Keys;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace HealthChecks.Azure.KeyVault.Keys;
public sealed class AzureKeyVaultKeysHealthCheck : IHealthCheck
{
private readonly KeyClient _keyClient;
private readonly AzureKeyVaultKeysHealthCheckOptions _options;
public AzureKeyVaultKeysHealthCheck(KeyClient keyClient, AzureKeyVaultKeysHealthCheckOptions? options = default)
{
_keyClient = Guard.ThrowIfNull(keyClient);
_options = options ?? new AzureKeyVaultKeysHealthCheckOptions();
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
string keyName = _options.KeyName;
try
{
await _keyClient.GetKeyAsync(keyName, cancellationToken: cancellationToken).ConfigureAwait(false);
return new HealthCheckResult(HealthStatus.Healthy);
}
catch (RequestFailedException azureEx) when (azureEx.Status == 404)
{
return new HealthCheckResult(HealthStatus.Healthy);
}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}