Search code examples
c#azureazure-blob-storagehealth-monitoringhealth-check

Is there an easy way to health-check an Azure blob storage with multiple keys?


I am building an Azure web service using ASP.NET Core (C#, .NET 6). I have an Azure storage account. The storage account has two (2) access keys. The idea is that if the first key expires or needs to be changed, the application can seemlessly switch to the second key while operators renew the first key.

My application has a health check, and I would like to include a health check of the storage account. I took inspiration from this article: https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/monitor-app-health#healthchecks-implementation-in-eshoponcontainers

I wrote code like this, using NuGet package AspNetCore.HealthChecks.AzureStorage:

services.AddHealthChecks()
    .AddAzureBlobStorage(
        $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={key1};EndpointSuffix=core.windows.net",
        name: "catalog-storage-check",
        tags: new string[] { "catalogstorage" });

The problem is that this health check will fail if key 1 is expired or revoked. That is not what I want. I want the health check to succeed as long as at least 1 of the 2 keys works, and fail if both keys fail.

Is there an easy way to do this, or will I have to code it on my own?


Solution

  • I looked at the implementation of AspNetCore.HealthChecks.AzureStorage.

    https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/2f582f42a3e5c7dc73bfdb114d6031d711552af6/test/HealthChecks.AzureStorage.Tests/DependencyInjection/AzureBlobStorageRegistrationTests.cs

    It is actually really simple. It just checks whether a blob container exists.

    So I just did my own implementation using the same idea but retrying if the first key does not work. (I already had a generic way to retry an operation if the first key fails.)