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

Feature Request: Support Managed Service Identity for Service Bus #2113

Open
Zenuka opened this issue Feb 6, 2019 · 0 comments
Open

Feature Request: Support Managed Service Identity for Service Bus #2113

Zenuka opened this issue Feb 6, 2019 · 0 comments

Comments

@Zenuka
Copy link

Zenuka commented Feb 6, 2019

Simmilar to the feature request done by NullMDR (#2109) but for service bus.
It would be nice to have build in support for managed service identity & azure service bus.

Known workarounds

I've made a workaround based on NullMDR's workaround for using MSI & storage.
Create the following class:

// Source: https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus/MessagingProvider.cs
public class ManagedIdentityMessagingProvider : MessagingProvider
{
    private readonly ServiceBusOptions _options;
    private readonly ConcurrentDictionary<string, MessageReceiver> _messageReceiverCache = new ConcurrentDictionary<string, MessageReceiver>();
    private readonly ConcurrentDictionary<string, MessageSender> _messageSenderCache = new ConcurrentDictionary<string, MessageSender>();

    public ManagedIdentityMessagingProvider(IOptions<ServiceBusOptions> serviceBusOptions) : base(serviceBusOptions)
    {
        _options = serviceBusOptions?.Value ?? throw new ArgumentNullException(nameof(serviceBusOptions));
    }

    public override MessageProcessor CreateMessageProcessor(string entityPath, string connectionString)
    {
        return new MessageProcessor(CreateMessageReceiver(entityPath, connectionString), _options.MessageHandlerOptions);
    }

    public override MessageReceiver CreateMessageReceiver(string entityPath, string connectionString)
    {
        return GetOrAddMessageReceiver(entityPath, connectionString);
    }

    public override MessageSender CreateMessageSender(string entityPath, string connectionString)
    {
        return GetOrAddMessageSender(entityPath, connectionString);
    }

    private MessageReceiver GetOrAddMessageReceiver(string entityPath, string connectionString)
    {
        string cacheKey = $"{entityPath}-{connectionString}";
        return _messageReceiverCache.GetOrAdd(cacheKey,
            new MessageReceiver(connectionString, entityPath, TokenProvider.CreateManagedServiceIdentityTokenProvider())
            {
                PrefetchCount = _options.PrefetchCount
            });
    }

    private MessageSender GetOrAddMessageSender(string entityPath, string connectionString)
    {
        string cacheKey = $"{entityPath}-{connectionString}";
        return _messageSenderCache.GetOrAdd(cacheKey, new MessageSender(connectionString, entityPath, TokenProvider.CreateManagedServiceIdentityTokenProvider()));
    }
}

Register it like this:

var builder = new HostBuilder()
    .ConfigureWebJobs(b => 
    {
        b.Services.AddSingleton<MessagingProvider, ManagedIdentityMessagingProvider>();
        b.AddServiceBus();
    })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant