Skip to content

Commit

Permalink
Updating Storage Blob API package to the latest (#3359)
Browse files Browse the repository at this point in the history
* Upgrade the storage blob API from v11 to v12.

* Fixed "rest" to "Rest" in projitems file.

* Fixed another case sensitivity issue.

* Moved blob instance creation code to the initializer classes.

* Fixed build errors after merge.

* Addressing reviewer's comments.
  • Loading branch information
v-iyamauchi authored Jul 20, 2023
1 parent 3e5813c commit fc08077
Show file tree
Hide file tree
Showing 32 changed files with 682 additions and 463 deletions.

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

using Azure.Storage.Blobs;
using EnsureThat;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.Azure.ContainerRegistry;
using Microsoft.Health.Fhir.Azure.ExportDestinationClient;
Expand Down Expand Up @@ -109,7 +107,7 @@ public static IFhirServerBuilder AddAzureIntegrationDataStoreClient(this IFhirSe
{
fhirServerBuilder.Services.Add<AzureAccessTokenClientInitializerV2>()
.Transient()
.AsService<IIntegrationDataStoreClientInitilizer<CloudBlobClient>>();
.AsService<IIntegrationDataStoreClientInitializer>();

fhirServerBuilder.Services.Add<AzureAccessTokenProvider>()
.Transient()
Expand All @@ -119,7 +117,7 @@ public static IFhirServerBuilder AddAzureIntegrationDataStoreClient(this IFhirSe
{
fhirServerBuilder.Services.Add<AzureConnectionStringClientInitializerV2>()
.Transient()
.AsService<IIntegrationDataStoreClientInitilizer<CloudBlobClient>>();
.AsService<IIntegrationDataStoreClientInitializer>();
}

fhirServerBuilder.Services.Add<AzureBlobIntegrationDataStoreClient>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,65 @@

using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Specialized;
using EnsureThat;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Health.Fhir.Core.Configs;
using Microsoft.Health.Fhir.Core.Features.Operations;

namespace Microsoft.Health.Fhir.Azure.IntegrationDataStore
{
public class AzureAccessTokenClientInitializerV2 : IIntegrationDataStoreClientInitilizer<CloudBlobClient>
public class AzureAccessTokenClientInitializerV2 : IIntegrationDataStoreClientInitializer
{
private readonly IAccessTokenProvider _accessTokenProvider;
private readonly IntegrationDataStoreConfiguration _integrationDataStoreConfiguration;
private readonly ILogger<AzureAccessTokenClientInitializerV2> _logger;

public AzureAccessTokenClientInitializerV2(
IAccessTokenProvider accessTokenProvider,
IOptions<IntegrationDataStoreConfiguration> integrationDataStoreConfiguration,
ILogger<AzureAccessTokenClientInitializerV2> logger)
{
EnsureArg.IsNotNull(accessTokenProvider, nameof(accessTokenProvider));
EnsureArg.IsNotNull(integrationDataStoreConfiguration?.Value, nameof(integrationDataStoreConfiguration));
EnsureArg.IsNotNull(logger, nameof(logger));

_accessTokenProvider = accessTokenProvider;
_integrationDataStoreConfiguration = integrationDataStoreConfiguration.Value;
_logger = logger;
}

public async Task<CloudBlobClient> GetAuthorizedClientAsync(CancellationToken cancellationToken)
public Task<BlobClient> GetAuthorizedBlobClientAsync(Uri blobUri)
{
return await GetAuthorizedClientAsync(_integrationDataStoreConfiguration, cancellationToken);
EnsureArg.IsNotNull(blobUri, nameof(blobUri));
return Task.FromResult(new BlobClient(blobUri, CreateDefaultTokenCredential()));
}

public async Task<CloudBlobClient> GetAuthorizedClientAsync(IntegrationDataStoreConfiguration integrationDataStoreConfiguration, CancellationToken cancellationToken)
public Task<BlobClient> GetAuthorizedBlobClientAsync(Uri blobUri, IntegrationDataStoreConfiguration integrationDataStoreConfiguration)
{
EnsureArg.IsNotNull(blobUri, nameof(blobUri));
return Task.FromResult(new BlobClient(blobUri, CreateDefaultTokenCredential()));
}

public Task<BlockBlobClient> GetAuthorizedBlockBlobClientAsync(Uri blobUri)
{
EnsureArg.IsNotNull(blobUri, nameof(blobUri));
return Task.FromResult(new BlockBlobClient(blobUri, CreateDefaultTokenCredential()));
}

public Task<BlockBlobClient> GetAuthorizedBlockBlobClientAsync(Uri blobUri, IntegrationDataStoreConfiguration integrationDataStoreConfiguration)
{
EnsureArg.IsNotNull(blobUri, nameof(blobUri));
return Task.FromResult(new BlockBlobClient(blobUri, CreateDefaultTokenCredential()));
}

public async Task<BlobServiceClient> GetAuthorizedClientAsync()
{
return await GetAuthorizedClientAsync(_integrationDataStoreConfiguration);
}

public Task<BlobServiceClient> GetAuthorizedClientAsync(IntegrationDataStoreConfiguration integrationDataStoreConfiguration)
{
if (string.IsNullOrWhiteSpace(integrationDataStoreConfiguration.StorageAccountUri))
{
Expand All @@ -54,22 +75,21 @@ public async Task<CloudBlobClient> GetAuthorizedClientAsync(IntegrationDataStore
throw new IntegrationDataStoreClientInitializerException(Resources.InvalidStorageUri, HttpStatusCode.BadRequest);
}

string accessToken;
try
{
accessToken = await _accessTokenProvider.GetAccessTokenForResourceAsync(storageAccountUri, cancellationToken);
return Task.FromResult(new BlobServiceClient(storageAccountUri, CreateDefaultTokenCredential()));
}
catch (AccessTokenProviderException atp)
{
_logger.LogError(atp, "Unable to get access token");

throw new IntegrationDataStoreClientInitializerException(Resources.CannotGetAccessToken, HttpStatusCode.Unauthorized);
}
}

#pragma warning disable CA2000 // Dispose objects before losing scope
StorageCredentials storageCredentials = new StorageCredentials(new TokenCredential(accessToken));
#pragma warning restore CA2000 // Dispose objects before losing scope
return new CloudBlobClient(storageAccountUri, storageCredentials);
private static TokenCredential CreateDefaultTokenCredential()
{
return new DefaultAzureCredential();
}
}
}
Loading

0 comments on commit fc08077

Please # to comment.