Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register DataProtection_Azure asynchronously #14687

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/OrchardCore.Modules/OrchardCore.DataProtection.Azure/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Fluid;
Expand All @@ -15,7 +16,6 @@ namespace OrchardCore.DataProtection.Azure
{
public class Startup : StartupBase
{
private readonly IShellConfiguration _configuration;
private readonly ShellOptions _shellOptions;
private readonly ShellSettings _shellSettings;
private readonly ILogger _logger;
Expand All @@ -24,34 +24,40 @@ public class Startup : StartupBase
private readonly FluidParser _fluidParser = new();

public Startup(
IShellConfiguration configuration,
IOptions<ShellOptions> shellOptions,
ShellSettings shellSettings,
ILogger<Startup> logger)
{
_configuration = configuration;
_shellOptions = shellOptions.Value;
_shellSettings = shellSettings;
_logger = logger;
}

public override void ConfigureServices(IServiceCollection services)
{
var connectionString = _configuration.GetValue<string>("OrchardCore_DataProtection_Azure:ConnectionString");

if (!string.IsNullOrWhiteSpace(connectionString))
{
services.AddDataProtection().PersistKeysToAzureBlobStorage(connectionString, GetBlobContainerName(connectionString), GetBlobName());
}
else
services.Initialize(async sp =>
{
_logger.LogCritical("No connection string was supplied for OrchardCore.DataProtection.Azure. Ensure that an application setting containing a valid Azure Storage connection string is available at `Modules:OrchardCore.DataProtection.Azure:ConnectionString`.");
}
var configuration = sp.GetRequiredService<IShellConfiguration>();

var connectionString = configuration.GetValue<string>("OrchardCore_DataProtection_Azure:ConnectionString");

if (!string.IsNullOrWhiteSpace(connectionString))
{
var containerName = await GetBlobContainerNameAsync(configuration, connectionString);

services.AddDataProtection()
.PersistKeysToAzureBlobStorage(connectionString, containerName, await GetBlobNameAsync(configuration));
}
else
{
_logger.LogCritical("No connection string was supplied for OrchardCore.DataProtection.Azure. Ensure that an application setting containing a valid Azure Storage connection string is available at `Modules:OrchardCore.DataProtection.Azure:ConnectionString`.");
}
});
}

private string GetBlobContainerName(string connectionString)
private async Task<string> GetBlobContainerNameAsync(IShellConfiguration configuration, string connectionString)
{
var containerName = _configuration.GetValue("OrchardCore_DataProtection_Azure:ContainerName", "dataprotection");
var containerName = configuration.GetValue("OrchardCore_DataProtection_Azure:ContainerName", "dataprotection");

// Use Fluid directly as the service provider has not been built.
try
Expand All @@ -73,14 +79,14 @@ private string GetBlobContainerName(string connectionString)
throw;
}

var createContainer = _configuration.GetValue("OrchardCore_DataProtection_Azure:CreateContainer", true);
var createContainer = configuration.GetValue("OrchardCore_DataProtection_Azure:CreateContainer", true);
if (createContainer)
{
try
{
_logger.LogDebug("Testing data protection container {ContainerName} existence", containerName);
var _blobContainer = new BlobContainerClient(connectionString, containerName);
var response = _blobContainer.CreateIfNotExistsAsync(PublicAccessType.None).GetAwaiter().GetResult();
var response = await _blobContainer.CreateIfNotExistsAsync(PublicAccessType.None);
_logger.LogDebug("Data protection container {ContainerName} created.", containerName);
}
catch (Exception)
Expand All @@ -94,9 +100,9 @@ private string GetBlobContainerName(string connectionString)
return containerName;
}

private string GetBlobName()
private async Task<string> GetBlobNameAsync(IShellConfiguration configuration)
{
var blobName = _configuration.GetValue<string>("OrchardCore_DataProtection_Azure:BlobName");
var blobName = configuration.GetValue<string>("OrchardCore_DataProtection_Azure:BlobName");

if (string.IsNullOrEmpty(blobName))
{
Expand All @@ -114,7 +120,7 @@ private string GetBlobName()

var template = _fluidParser.Parse(blobName);

blobName = template.Render(templateContext, NullEncoder.Default);
blobName = await template.RenderAsync(templateContext, NullEncoder.Default);
blobName = blobName.Replace("\r", string.Empty).Replace("\n", string.Empty);
}
catch (Exception e)
Expand Down
Loading