From a22efc40f313d4961b2245921d956e143fbdeb97 Mon Sep 17 00:00:00 2001 From: Henning Normann Date: Thu, 24 Oct 2024 22:38:07 +0200 Subject: [PATCH] - Renamed CreateBlobClient to CreateContainerClient - Added invalidation of client cache if authentication failure --- src/Storage/Repository/BlobRepository.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Storage/Repository/BlobRepository.cs b/src/Storage/Repository/BlobRepository.cs index 4d031643..594cee04 100644 --- a/src/Storage/Repository/BlobRepository.cs +++ b/src/Storage/Repository/BlobRepository.cs @@ -58,6 +58,7 @@ public async Task ReadBlob(string org, string blobStoragePath, int? stor _logger.LogWarning("Authentication failed. Invalidating credentials and retrying download operation."); _memoryCache.Remove(_credsCacheKey); + _memoryCache.Remove(GetClientCacheKey(org, storageContainerNumber)); return await DownloadBlobAsync(org, blobStoragePath, storageContainerNumber); case "BlobNotFound": @@ -92,6 +93,7 @@ public async Task ReadBlob(string org, string blobStoragePath, int? stor _logger.LogWarning("Authentication failed. Invalidating credentials."); _memoryCache.Remove(_credsCacheKey); + _memoryCache.Remove(GetClientCacheKey(org, storageContainerNumber)); // No use retrying upload as the original stream can't be reset back to start. throw; @@ -116,6 +118,7 @@ public async Task DeleteBlob(string org, string blobStoragePath, int? stor _logger.LogWarning("Authentication failed. Invalidating credentials and retrying delete operation."); _memoryCache.Remove(_credsCacheKey); + _memoryCache.Remove(GetClientCacheKey(org, storageContainerNumber)); return await DeleteIfExistsAsync(org, blobStoragePath, storageContainerNumber); default: @@ -127,7 +130,7 @@ public async Task DeleteBlob(string org, string blobStoragePath, int? stor /// public async Task DeleteDataBlobs(Instance instance, int? storageContainerNumber) { - BlobContainerClient container = CreateBlobClient(instance.Org, storageContainerNumber); + BlobContainerClient container = CreateContainerClient(instance.Org, storageContainerNumber); if (container == null) { @@ -144,7 +147,6 @@ public async Task DeleteDataBlobs(Instance instance, int? storageContainer } catch (Exception e) { - _memoryCache.Remove(_credsCacheKey); _logger.LogError( e, "BlobService // DeleteDataBlobs // Org: {Instance}", @@ -188,14 +190,14 @@ private async Task DeleteIfExistsAsync(string org, string fileName, int? s private BlobClient CreateBlobClient(string org, string blobName, int? storageContainerNumber) { - return CreateBlobClient(org, storageContainerNumber).GetBlobClient(blobName); + return CreateContainerClient(org, storageContainerNumber).GetBlobClient(blobName); } - private BlobContainerClient CreateBlobClient(string org, int? storageContainerNumber) + private BlobContainerClient CreateContainerClient(string org, int? storageContainerNumber) { if (!_storageConfiguration.AccountName.Equals("devstoreaccount1")) { - string cacheKey = $"blob-{org}-{storageContainerNumber}"; + string cacheKey = GetClientCacheKey(org, storageContainerNumber); if (!_memoryCache.TryGetValue(cacheKey, out BlobContainerClient client)) { string accountName = string.Format(_storageConfiguration.OrgStorageAccount, org); @@ -233,5 +235,10 @@ private TokenCredential GetCachedCredentials() return creds; } + + private static string GetClientCacheKey(string org, int? storageContainerNumber) + { + return $"blob-{org}-{storageContainerNumber}"; + } } }