mirror of
https://github.com/bitwarden/server
synced 2025-12-21 02:33:30 +00:00
Refactor configuration for azure queue service for events to include queue name (#6724)
* Refactor configuration for azure queue service for events to include queue name * Address PR feedback * Add check for queue name before writing to Azure Queue Service * Fix file encoding (lint error)
This commit is contained in:
@@ -33,6 +33,10 @@
|
||||
"id": "<your Installation Id>",
|
||||
"key": "<your Installation Key>"
|
||||
},
|
||||
"events": {
|
||||
"connectionString": "",
|
||||
"queueName": "event"
|
||||
},
|
||||
"licenseDirectory": "<full path to license directory>",
|
||||
"enableNewDeviceVerification": true,
|
||||
"enableEmailVerification": true
|
||||
|
||||
@@ -91,7 +91,8 @@ public static class EventIntegrationsServiceCollectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
if (CoreHelpers.SettingHasValue(globalSettings.Events.ConnectionString))
|
||||
if (CoreHelpers.SettingHasValue(globalSettings.Events.ConnectionString) &&
|
||||
CoreHelpers.SettingHasValue(globalSettings.Events.QueueName))
|
||||
{
|
||||
services.TryAddSingleton<IEventWriteService, AzureQueueEventWriteService>();
|
||||
return services;
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Bit.Core.Services;
|
||||
public class AzureQueueEventWriteService : AzureQueueService<IEvent>, IEventWriteService
|
||||
{
|
||||
public AzureQueueEventWriteService(GlobalSettings globalSettings) : base(
|
||||
new QueueClient(globalSettings.Events.ConnectionString, "event"),
|
||||
new QueueClient(globalSettings.Events.ConnectionString, globalSettings.Events.QueueName),
|
||||
JsonHelpers.IgnoreWritingNull)
|
||||
{ }
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class GlobalSettings : IGlobalSettings
|
||||
public virtual EventLoggingSettings EventLogging { get; set; } = new EventLoggingSettings();
|
||||
public virtual MailSettings Mail { get; set; } = new MailSettings();
|
||||
public virtual IConnectionStringSettings Storage { get; set; } = new ConnectionStringSettings();
|
||||
public virtual ConnectionStringSettings Events { get; set; } = new ConnectionStringSettings();
|
||||
public virtual AzureQueueEventSettings Events { get; set; } = new AzureQueueEventSettings();
|
||||
public virtual DistributedCacheSettings DistributedCache { get; set; } = new DistributedCacheSettings();
|
||||
public virtual NotificationsSettings Notifications { get; set; } = new NotificationsSettings();
|
||||
public virtual IFileStorageSettings Attachment { get; set; }
|
||||
@@ -395,6 +395,24 @@ public class GlobalSettings : IGlobalSettings
|
||||
}
|
||||
}
|
||||
|
||||
public class AzureQueueEventSettings : IConnectionStringSettings
|
||||
{
|
||||
private string _connectionString;
|
||||
private string _queueName;
|
||||
|
||||
public string ConnectionString
|
||||
{
|
||||
get => _connectionString;
|
||||
set => _connectionString = value?.Trim('"');
|
||||
}
|
||||
|
||||
public string QueueName
|
||||
{
|
||||
get => _queueName;
|
||||
set => _queueName = value?.Trim('"');
|
||||
}
|
||||
}
|
||||
|
||||
public class ConnectionStringSettings : IConnectionStringSettings
|
||||
{
|
||||
private string _connectionString;
|
||||
|
||||
@@ -6,6 +6,7 @@ using Azure.Storage.Queues;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.EventsProcessor;
|
||||
@@ -13,7 +14,7 @@ namespace Bit.EventsProcessor;
|
||||
public class AzureQueueHostedService : IHostedService, IDisposable
|
||||
{
|
||||
private readonly ILogger<AzureQueueHostedService> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
|
||||
private Task _executingTask;
|
||||
private CancellationTokenSource _cts;
|
||||
@@ -22,10 +23,10 @@ public class AzureQueueHostedService : IHostedService, IDisposable
|
||||
|
||||
public AzureQueueHostedService(
|
||||
ILogger<AzureQueueHostedService> logger,
|
||||
IConfiguration configuration)
|
||||
GlobalSettings globalSettings)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_globalSettings = globalSettings;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -56,11 +57,12 @@ public class AzureQueueHostedService : IHostedService, IDisposable
|
||||
|
||||
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var storageConnectionString = _configuration["azureStorageConnectionString"];
|
||||
var queueName = _configuration["azureQueueServiceQueueName"];
|
||||
var storageConnectionString = _globalSettings.Events.ConnectionString;
|
||||
var queueName = _globalSettings.Events.QueueName;
|
||||
if (string.IsNullOrWhiteSpace(storageConnectionString) ||
|
||||
string.IsNullOrWhiteSpace(queueName))
|
||||
{
|
||||
_logger.LogInformation("Azure Queue Hosted Service is disabled. Missing connection string or queue name.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -727,7 +727,8 @@ public class EventIntegrationServiceCollectionExtensionsTests
|
||||
var services = new ServiceCollection();
|
||||
var globalSettings = CreateGlobalSettings(new Dictionary<string, string?>
|
||||
{
|
||||
["GlobalSettings:Events:ConnectionString"] = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=test;EndpointSuffix=core.windows.net"
|
||||
["GlobalSettings:Events:ConnectionString"] = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=test;EndpointSuffix=core.windows.net",
|
||||
["GlobalSettings:Events:QueueName"] = "event"
|
||||
});
|
||||
|
||||
services.AddEventWriteServices(globalSettings);
|
||||
|
||||
Reference in New Issue
Block a user