-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Tenant ID propagation for Proto.Actor runtime (#6142)
* Fix Tenant ID propagation for Proto.Actor runtime * Rename Constants to HeaderNames
- Loading branch information
1 parent
95a2a33
commit b4bbf25
Showing
4 changed files
with
63 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Elsa.ProtoActor; | ||
|
||
public static class HeaderNames | ||
{ | ||
public const string TenantId = "TenantId"; | ||
} |
53 changes: 53 additions & 0 deletions
53
src/modules/Elsa.ProtoActor.Core/Middleware/TenantScopeMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Elsa.Common.Multitenancy; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Proto; | ||
using Proto.DependencyInjection; | ||
|
||
namespace Elsa.ProtoActor.Middleware; | ||
|
||
public static class TenantScopeMiddleware | ||
{ | ||
public static Props WithMultitenancy(this Props props, IServiceProvider sp) | ||
{ | ||
props = props.WithReceiverMiddleware(next => ReadTenant(next, sp)); | ||
props = props.WithSenderMiddleware(next => PropagateTenant(next, sp)); | ||
return props; | ||
} | ||
|
||
public static Receiver ReadTenant(this Receiver next, IServiceProvider sp) | ||
{ | ||
async Task Receiver(IReceiverContext context, MessageEnvelope envelope) | ||
{ | ||
var tenantId = envelope.Header.GetValueOrDefault(HeaderNames.TenantId); | ||
if (tenantId != null) | ||
{ | ||
var tenantFinder = sp.GetRequiredService<ITenantFinder>(); | ||
var tenant = await tenantFinder.FindByIdAsync(tenantId); | ||
var tenantScopeFactory = sp.GetRequiredService<ITenantScopeFactory>(); | ||
await using var tenantScope = tenantScopeFactory.CreateScope(tenant); | ||
var originalServiceProvider = sp; | ||
context.System.WithServiceProvider(tenantScope.ServiceProvider); | ||
await next(context, envelope); | ||
context.System.WithServiceProvider(originalServiceProvider); | ||
} | ||
else | ||
{ | ||
await next(context, envelope); | ||
} | ||
} | ||
|
||
return Receiver; | ||
} | ||
|
||
public static Sender PropagateTenant(this Sender next, IServiceProvider sp) | ||
{ | ||
async Task Sender(ISenderContext context, PID target, MessageEnvelope envelope) | ||
{ | ||
var tenantAccessor = sp.GetRequiredService<ITenantAccessor>(); | ||
if (tenantAccessor.Tenant != null) envelope.WithHeader(HeaderNames.TenantId, tenantAccessor.Tenant.Id); | ||
await next(context, target, envelope); | ||
} | ||
|
||
return Sender; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters