Skip to content

Commit

Permalink
Add IdenttiyServer.Messaging.Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn committed Jun 3, 2024
1 parent 2dcf002 commit 9c12f84
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 196 deletions.
2 changes: 1 addition & 1 deletion All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{17CDA34C-429
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IdentityServer", "IdentityServer", "{D264CAEC-2137-46D8-B637-FCE7CBE3B390}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messaging.AzureServiceBus", "src\IdentityServer\Messaging.AzureServiceBus\Messaging.AzureServiceBus.csproj", "{C8F188CA-2671-4364-B53C-43832627C0DA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messaging.Azure", "src\IdentityServer\Messaging.Azure\Messaging.Azure.csproj", "{C8F188CA-2671-4364-B53C-43832627C0DA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messaging.RabbitMQ", "src\IdentityServer\Messaging.RabbitMQ\Messaging.RabbitMQ.csproj", "{9C1A424A-64A5-4BAA-BCE5-529F2148497F}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using MassTransit;
using Microsoft.Extensions.Hosting;

namespace IdOps.IdentityServer.AzureEventHub;
namespace IdOps.IdentityServer.Azure;

public sealed class EventHubSender : BackgroundService, IEventSenderWorker
{
Expand All @@ -26,15 +26,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Yield();

var producer = await _producerProvider.GetProducer("identity-events");
IEventHubProducer producer = await _producerProvider.GetProducer("identity-events");

// we reuse the buffer to avoid allocations
var buffer = new IdentityEventMessage[50];
try
{
while (await _channelReader.WaitToReadAsync(stoppingToken))
{
// we read as many messages as we can
// we read as many messages as we can
for (var i = 0; i < buffer.Length; i++)
{
if (!_channelReader.TryRead(out IdentityEventMessage? entity))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using Azure.Identity;
using IdOps.IdentityServer.Abstractions;
using IdOps.IdentityServer.Azure;
using MassTransit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using static IdOps.IdentityServer.Wellknown.ConfigSections;

namespace IdOps.IdentityServer.Azure
{
public static class AzureServiceBusIdOpsBuilderExtensions
{
public static IIdOpsIdentityServerBuilder UseAzure(this BusBuilder builder)
{
AzureOptions? options = builder
.IdOpsBuilder.Configuration?.GetSection($"{Messaging}:Azure")
.Get<AzureOptions>();

if (options == null)
{
throw new ApplicationException(
"Could not get AzureOptions configuration from "
+ $"{Messaging}:Azure."
+ "Please check you configuration");
}

return builder.UseAzure(options);
}

private static IIdOpsIdentityServerBuilder UseAzure(
this BusBuilder builder,
AzureOptions options)
{
if (options.EventHub is not null)
{
builder.IdOpsBuilder.Services.AddSingleton<IEventSenderWorker, EventHubSender>();
}

builder.IdOpsBuilder.Services.AddMassTransit(s =>
{
builder.BusSetup?.Invoke(s);

if (options.ServiceBus is { })
{
s.RegisterServiceBus(options.ServiceBus, builder);
}

if (options.EventHub is { } eventHub)
{
s.RegisterEventHub(eventHub);
}
});

return builder.IdOpsBuilder;
}

private static void RegisterEventHub(
this IBusRegistrationConfigurator configurator,
EventHubOptions eventHub)
{
configurator.AddRider(x =>
x.UsingEventHub((_, k) =>
{
if (eventHub.Namespace is { } @namespace)
{
k.Host(@namespace, new DefaultAzureCredential());
}
else if (eventHub.ConnectionString is not null)
{
k.Host(eventHub.ConnectionString);
}
else
{
throw new ApplicationException(
"EventHub configuration is missing. Please check your settings.");
}

if (eventHub.Storage is { } storageOption)
{
if (storageOption.Url is { } url)
{
k.Storage(new Uri(url), new DefaultAzureCredential());
}
else if (storageOption.ConnectionString is { } connectionString)
{
k.Storage(connectionString);
}
else
{
throw new ApplicationException(
"EventHub storage configuration is missing. Please check your settings.");
}
}
})
);
}

private static void RegisterServiceBus(
this IBusRegistrationConfigurator configurator,
AzureServiceBusOptions options,
BusBuilder builder)
{
configurator.UsingAzureServiceBus((provider, cfg) =>
{
var serverGroup = builder.IdOpsBuilder.Options!.ServerGroup.ToLower();
var environmentName = builder.IdOpsBuilder.Options!.EnvironmentName.ToLower();
cfg.Host(options.ConnectionString);
cfg.ReceiveEndpoint(
$"id-{serverGroup}-{environmentName}",
e =>
{
e.ConfigureConsumers(provider);
e.PrefetchCount = options.PrefetchCount;
});
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" />
<PackageReference Update="Microsoft.SourceLink.GitHub" />
<PackageReference Include="Masstransit.EventHub"/>
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/IdentityServer/Messaging.Azure/Options/AzureOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace IdOps.IdentityServer.Azure;

public sealed class AzureOptions
{
public AzureServiceBusOptions? ServiceBus { get; set; } = default!;
public EventHubOptions? EventHub { get; set; } = default!;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace IdOps.IdentityServer.Azure;

public class AzureServiceBusOptions
{
public string ConnectionString { get; set; } = default!;

public int PrefetchCount { get; set; } = 10;
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace IdOps.IdentityServer.AzureEventHub;
namespace IdOps.IdentityServer.Azure;

public class EventHubOptions
{
Expand All @@ -7,4 +7,4 @@ public class EventHubOptions
public string? Namespace { get; set; }

public EventStorageHubOptions? Storage { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace IdOps.IdentityServer.AzureEventHub;
namespace IdOps.IdentityServer.Azure;

public sealed class EventStorageHubOptions
{
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 9c12f84

Please sign in to comment.