From de672361561401e5fb813f5be1f4d5d2aef1723f Mon Sep 17 00:00:00 2001 From: nickle Date: Sun, 4 Feb 2024 21:30:55 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fixes=20issues=20for=20Blazor=20?= =?UTF-8?q?SSR=20using=20Token=20Credential=20=F0=9F=9B=A0=EF=B8=8F=20Maki?= =?UTF-8?q?ng=20explicit=20Token=20Credential=20workflow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Images/IO/AzureImagesBlobStore.cs | 2 +- .../Infrastructure/Messaging/AzureQueueMessages.cs | 4 ++-- .../Messaging/AzureStorageSettings.cs | 6 ++++-- .../Infrastructure/Registration.cs | 13 ++++++++----- .../Bootstrapping/Registration.cs | 5 +++-- src/AnimeFeedManager.Web/Program.cs | 10 ++++++++-- 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/AnimeFeedManager.Features/Images/IO/AzureImagesBlobStore.cs b/src/AnimeFeedManager.Features/Images/IO/AzureImagesBlobStore.cs index 62a44305..dea41b28 100644 --- a/src/AnimeFeedManager.Features/Images/IO/AzureImagesBlobStore.cs +++ b/src/AnimeFeedManager.Features/Images/IO/AzureImagesBlobStore.cs @@ -23,7 +23,7 @@ private static BlobContainerClient GetClient(AzureStorageSettings azureSettings) ConnectionStringSettings connectionStringOptions => new BlobContainerClient( connectionStringOptions.StorageConnectionString, Container), TokenCredentialSettings tokenCredentialOptions => new BlobContainerClient( - new Uri(tokenCredentialOptions.BlobUri, Container), new DefaultAzureCredential()), + new Uri(tokenCredentialOptions.BlobUri, Container), tokenCredentialOptions.DefaultTokenCredential()), _ => throw new ArgumentException( "Provided Table Storage configuration is not valid. Make sure Configurations for Azure table Storage is correct for either connection string or managed identities", nameof(TableClientOptions)) diff --git a/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureQueueMessages.cs b/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureQueueMessages.cs index 7a314716..782fe2ca 100644 --- a/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureQueueMessages.cs +++ b/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureQueueMessages.cs @@ -40,7 +40,7 @@ public AzureQueueMessages(AzureStorageSettings tableStorageSettings) { _azureSettings = tableStorageSettings; _jsonOptions = new JsonSerializerOptions(new JsonSerializerOptions - {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}); + { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); _queueClientOptions = new QueueClientOptions { MessageEncoding = QueueMessageEncoding.Base64 @@ -91,7 +91,7 @@ private QueueClient GetClient(string destiny) connectionStringOptions.StorageConnectionString, destiny, _queueClientOptions), TokenCredentialSettings tokenCredentialOptions => new QueueClient( - tokenCredentialOptions.QueueUri, new DefaultAzureCredential(), _queueClientOptions), + new Uri(tokenCredentialOptions.QueueUri, destiny), tokenCredentialOptions.DefaultTokenCredential(), _queueClientOptions), _ => throw new ArgumentException( "Provided Table Storage configuration is not valid. Make sure Configurations for Azure table Storage is correct for either connection string or managed identities", nameof(TableClientOptions)) diff --git a/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureStorageSettings.cs b/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureStorageSettings.cs index d5acc259..81c6c737 100644 --- a/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureStorageSettings.cs +++ b/src/AnimeFeedManager.Features/Infrastructure/Messaging/AzureStorageSettings.cs @@ -1,10 +1,12 @@ -namespace AnimeFeedManager.Features.Infrastructure.Messaging; +using Azure.Core; + +namespace AnimeFeedManager.Features.Infrastructure.Messaging; public abstract record AzureStorageSettings; public sealed record ConnectionStringSettings(string StorageConnectionString) : AzureStorageSettings; -public sealed record TokenCredentialSettings(QueueUri QueueUri, BlobUri BlobUri) : AzureStorageSettings; +public sealed record TokenCredentialSettings(QueueUri QueueUri, BlobUri BlobUri, Func DefaultTokenCredential) : AzureStorageSettings; public readonly record struct QueueUri(Uri Uri) { diff --git a/src/AnimeFeedManager.Features/Infrastructure/Registration.cs b/src/AnimeFeedManager.Features/Infrastructure/Registration.cs index 2792c3d2..e2c5cdc3 100644 --- a/src/AnimeFeedManager.Features/Infrastructure/Registration.cs +++ b/src/AnimeFeedManager.Features/Infrastructure/Registration.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using AnimeFeedManager.Features.Infrastructure.Messaging; +using Azure.Core; using Azure.Identity; using Microsoft.Extensions.Configuration; @@ -21,12 +22,12 @@ public static IServiceCollection RegisterStorage(this IServiceCollection service } public static IServiceCollection RegisterStorage(this IServiceCollection services, - IConfigurationManager configuration) + IConfigurationManager configuration, Func defaultCredential) { var storageAccountName = configuration["StorageAccountName"]; if (!string.IsNullOrEmpty(storageAccountName)) { - RegisterWithAzureIdentity(services, storageAccountName); + RegisterWithAzureIdentity(services, storageAccountName,defaultCredential); } else { @@ -44,15 +45,15 @@ private static void RegisterCommonServices(this IServiceCollection services) services.TryAddSingleton(typeof(ITableClientFactory<>), typeof(TableClientFactory<>)); } - private static void RegisterWithAzureIdentity(IServiceCollection services, string storageAccountName) + private static void RegisterWithAzureIdentity(IServiceCollection services, string storageAccountName, Func defaultTokenCredential) { if (CreateUri(TableBaseUrl, storageAccountName, out var tableUri) && CreateUri(QueueBaseUrl, storageAccountName, out var queueUri) && CreateUri(BlobBaseUrl, storageAccountName, out var blobUri)) { services.TryAddSingleton( - new TokenCredentialSettings(new QueueUri(queueUri), new BlobUri(blobUri))); - services.TryAddSingleton(new TableServiceClient(tableUri, new DefaultAzureCredential())); + new TokenCredentialSettings(new QueueUri(queueUri), new BlobUri(blobUri), defaultTokenCredential)); + services.TryAddSingleton(new TableServiceClient(tableUri, defaultTokenCredential())); } else { @@ -70,4 +71,6 @@ private static bool CreateUri(string baseUrl, string storageAccountName, [NotNul { return Uri.TryCreate(string.Format(baseUrl,storageAccountName), UriKind.Absolute, out tableUri); } + + } \ No newline at end of file diff --git a/src/AnimeFeedManager.Web/Bootstrapping/Registration.cs b/src/AnimeFeedManager.Web/Bootstrapping/Registration.cs index 013bd4d2..52421e54 100644 --- a/src/AnimeFeedManager.Web/Bootstrapping/Registration.cs +++ b/src/AnimeFeedManager.Web/Bootstrapping/Registration.cs @@ -11,6 +11,7 @@ using AnimeFeedManager.Features.Tv; using AnimeFeedManager.Features.Users; using AnimeFeedManager.Web.Features.Security; +using Azure.Core; using Azure.Identity; using MediatR.NotificationPublishers; using Microsoft.AspNetCore.Authentication.Cookies; @@ -61,12 +62,12 @@ internal static IServiceCollection RegisterSecurityServices(this IServiceCollect } internal static IServiceCollection RegisterAppDependencies(this IServiceCollection services, - IConfigurationManager configuration) + IConfigurationManager configuration, Func defaultTokenCredential) { // MediatR services.RegisterMediatR(); // Storage - services.RegisterStorage(configuration); + services.RegisterStorage(configuration, defaultTokenCredential); // App services.RegisterSeasonsServices(); services.RegisterImageServices(); diff --git a/src/AnimeFeedManager.Web/Program.cs b/src/AnimeFeedManager.Web/Program.cs index 02ef1aad..0a1162f4 100644 --- a/src/AnimeFeedManager.Web/Program.cs +++ b/src/AnimeFeedManager.Web/Program.cs @@ -1,6 +1,8 @@ using AnimeFeedManager.Web.Bootstrapping; using AnimeFeedManager.Web.Features; using AnimeFeedManager.Web.Features.Common; +using Azure.Core; +using Azure.Identity; using Microsoft.AspNetCore.Components.Web; using TvEndpoints = AnimeFeedManager.Web.Features.Tv.Endpoints; using AdminEndpoints = AnimeFeedManager.Web.Features.Admin.Endpoints; @@ -23,7 +25,7 @@ builder.Services.AddScoped(); // Application dependencies -builder.Services.RegisterAppDependencies(builder.Configuration); +builder.Services.RegisterAppDependencies(builder.Configuration, GetDefaultCredential(builder.Environment)); builder.Services.AddApplicationInsightsTelemetry(); var app = builder.Build(); @@ -48,4 +50,8 @@ TvEndpoints.Map(app); AdminEndpoints.Map(app); -app.Run(); \ No newline at end of file +app.Run(); +return; + +static Func GetDefaultCredential(IWebHostEnvironment environment) => () => + !environment.IsDevelopment() ? new EnvironmentCredential() : new AzureCliCredential(); \ No newline at end of file