rebus-org / Rebus.AzureServiceBus

:bus: Azure Service Bus transport for Rebus
https://mookid.dk/category/rebus
Other
33 stars 20 forks source link

Using managed identity with Rebus.AzureServiceBus #86

Closed Ilincescu closed 1 year ago

Ilincescu commented 1 year ago

I tried to use the UseAzureServiceBus with managed identity and send the TokenCredential. I tested using the local azure user from visual studio that I gave permission (in the IAM) to the Service Bus Namespace. But I get an Rebus.Injection.ResolutionException exception with the message : DefaultAzureCredential failed to retrieve a token from the included credentials.

The Rebus.AzureServiceBus is working with managed identity ?

mookid8000 commented 1 year ago

It should be working with managed identity, yes.

Could you maybe post the entire exception? (i.e. including inner exceptions and call stack etc)

xuanta1 commented 1 year ago

I also have the same problem and do not yet understand the reason. Even though identity is configured

Source: using var bus = Configure .With(new BuiltinHandlerActivator()) .Transport(t => t.UseAzureServiceBus("namespace.servicebus.windows.net", "queue-name", new DefaultAzureCredential())) .Routing(r => r.TypeBased().Map(Constants.DocumentVersionUpdatedPdfEto)) .Start(); await bus.Publish(documentVersionUpdateEto, headers);

Exception: Exception while executing function: FunctionConvertPdf Could not resolve Rebus.AzureServiceBus.AzureServiceBusTransport with decorator depth 0 - registrations: Rebus.Injection.Injectionist+Handler Could not interpret 'namespace.servicebus.windows.net' as a key-value pair

mookid8000 commented 1 year ago

This part:

(...) Could not interpret 'doclanrenewal.servicebus.windows.net' as a key-value pair (...)

means that the string "doclanrenewal.servicebus.windows.net" could not be interpreted as a connection string.

You need to pass a string on the form "Endpoint=<something something>" e.g. like "Endpoint=sb://doclanrenewal.servicebus.windows.net;<more stuff here>"

xuanta1 commented 1 year ago

But I want to use identity instead of root key in connectionString. If I use connectionString is "Endpoint=sb://namespace.servicebus.windows.net/;Authentication=ManagedIdentity" then exception: Exception while executing function: FunctionConvertPdf Could not resolve Rebus.AzureServiceBus.AzureServiceBusTransport with decorator depth 0 - registrations: Rebus.Injection.Injectionist+Handler The value 'Endpoint=sb://namespace.servicebus.windows.net/;Authentication=Managed Identity' is not a well-formed Service Bus fully qualified namespace. (Parameter 'fullyQualifiedNamespace')

mookid8000 commented 1 year ago

How about just using

.UseAzureServiceBus("Endpoint=sb://doclanrenewal.servicebus.windows.net", "queue-name", new DefaultAzureCredential())

?

xuanta1 commented 1 year ago

I still get the same error as above

mookid8000 commented 1 year ago

Any chance you could post the full exception details?

xuanta1 commented 1 year ago

I resolved to be the problem. The reason for this exception is the version of rebus. After I updated to the latest version then it worked for me. Thanks

mookid8000 commented 1 year ago

Ok, good to hear! 🙂

So to sum it up, this works:

var connectionString = "Endpoint=namespace.servicebus.windows.net";
var queueName = "queue-name";

using var bus = Configure.With(new BuiltinHandlerActivator())
    .Transport(t => t.UseAzureServiceBus(connectionString, queueName , new DefaultAzureCredential()))
    .Routing(r => r.TypeBased().Map(Constants.DocumentVersionUpdatedPdfEto))
    .Start();

await bus.Publish(documentVersionUpdateEto, headers);