mirror of
https://github.com/bitwarden/server
synced 2025-12-21 10:43:44 +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>",
|
"id": "<your Installation Id>",
|
||||||
"key": "<your Installation Key>"
|
"key": "<your Installation Key>"
|
||||||
},
|
},
|
||||||
|
"events": {
|
||||||
|
"connectionString": "",
|
||||||
|
"queueName": "event"
|
||||||
|
},
|
||||||
"licenseDirectory": "<full path to license directory>",
|
"licenseDirectory": "<full path to license directory>",
|
||||||
"enableNewDeviceVerification": true,
|
"enableNewDeviceVerification": true,
|
||||||
"enableEmailVerification": true
|
"enableEmailVerification": true
|
||||||
|
|||||||
@@ -91,7 +91,8 @@ public static class EventIntegrationsServiceCollectionExtensions
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CoreHelpers.SettingHasValue(globalSettings.Events.ConnectionString))
|
if (CoreHelpers.SettingHasValue(globalSettings.Events.ConnectionString) &&
|
||||||
|
CoreHelpers.SettingHasValue(globalSettings.Events.QueueName))
|
||||||
{
|
{
|
||||||
services.TryAddSingleton<IEventWriteService, AzureQueueEventWriteService>();
|
services.TryAddSingleton<IEventWriteService, AzureQueueEventWriteService>();
|
||||||
return services;
|
return services;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace Bit.Core.Services;
|
|||||||
public class AzureQueueEventWriteService : AzureQueueService<IEvent>, IEventWriteService
|
public class AzureQueueEventWriteService : AzureQueueService<IEvent>, IEventWriteService
|
||||||
{
|
{
|
||||||
public AzureQueueEventWriteService(GlobalSettings globalSettings) : base(
|
public AzureQueueEventWriteService(GlobalSettings globalSettings) : base(
|
||||||
new QueueClient(globalSettings.Events.ConnectionString, "event"),
|
new QueueClient(globalSettings.Events.ConnectionString, globalSettings.Events.QueueName),
|
||||||
JsonHelpers.IgnoreWritingNull)
|
JsonHelpers.IgnoreWritingNull)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class GlobalSettings : IGlobalSettings
|
|||||||
public virtual EventLoggingSettings EventLogging { get; set; } = new EventLoggingSettings();
|
public virtual EventLoggingSettings EventLogging { get; set; } = new EventLoggingSettings();
|
||||||
public virtual MailSettings Mail { get; set; } = new MailSettings();
|
public virtual MailSettings Mail { get; set; } = new MailSettings();
|
||||||
public virtual IConnectionStringSettings Storage { get; set; } = new ConnectionStringSettings();
|
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 DistributedCacheSettings DistributedCache { get; set; } = new DistributedCacheSettings();
|
||||||
public virtual NotificationsSettings Notifications { get; set; } = new NotificationsSettings();
|
public virtual NotificationsSettings Notifications { get; set; } = new NotificationsSettings();
|
||||||
public virtual IFileStorageSettings Attachment { get; set; }
|
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
|
public class ConnectionStringSettings : IConnectionStringSettings
|
||||||
{
|
{
|
||||||
private string _connectionString;
|
private string _connectionString;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using Azure.Storage.Queues;
|
|||||||
using Bit.Core;
|
using Bit.Core;
|
||||||
using Bit.Core.Models.Data;
|
using Bit.Core.Models.Data;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
|
using Bit.Core.Settings;
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
namespace Bit.EventsProcessor;
|
namespace Bit.EventsProcessor;
|
||||||
@@ -13,7 +14,7 @@ namespace Bit.EventsProcessor;
|
|||||||
public class AzureQueueHostedService : IHostedService, IDisposable
|
public class AzureQueueHostedService : IHostedService, IDisposable
|
||||||
{
|
{
|
||||||
private readonly ILogger<AzureQueueHostedService> _logger;
|
private readonly ILogger<AzureQueueHostedService> _logger;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly GlobalSettings _globalSettings;
|
||||||
|
|
||||||
private Task _executingTask;
|
private Task _executingTask;
|
||||||
private CancellationTokenSource _cts;
|
private CancellationTokenSource _cts;
|
||||||
@@ -22,10 +23,10 @@ public class AzureQueueHostedService : IHostedService, IDisposable
|
|||||||
|
|
||||||
public AzureQueueHostedService(
|
public AzureQueueHostedService(
|
||||||
ILogger<AzureQueueHostedService> logger,
|
ILogger<AzureQueueHostedService> logger,
|
||||||
IConfiguration configuration)
|
GlobalSettings globalSettings)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_configuration = configuration;
|
_globalSettings = globalSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
@@ -56,11 +57,12 @@ public class AzureQueueHostedService : IHostedService, IDisposable
|
|||||||
|
|
||||||
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
private async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var storageConnectionString = _configuration["azureStorageConnectionString"];
|
var storageConnectionString = _globalSettings.Events.ConnectionString;
|
||||||
var queueName = _configuration["azureQueueServiceQueueName"];
|
var queueName = _globalSettings.Events.QueueName;
|
||||||
if (string.IsNullOrWhiteSpace(storageConnectionString) ||
|
if (string.IsNullOrWhiteSpace(storageConnectionString) ||
|
||||||
string.IsNullOrWhiteSpace(queueName))
|
string.IsNullOrWhiteSpace(queueName))
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Azure Queue Hosted Service is disabled. Missing connection string or queue name.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -727,7 +727,8 @@ public class EventIntegrationServiceCollectionExtensionsTests
|
|||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
var globalSettings = CreateGlobalSettings(new Dictionary<string, string?>
|
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);
|
services.AddEventWriteServices(globalSettings);
|
||||||
|
|||||||
Reference in New Issue
Block a user