Skip to main content

Service Bus and Shared Access Signature (SAS)

Until few months ago, Shared Access Signature (SAS) could be used only wilt Azure Storage (Blobs, Tables and Queues). From now one we can use SAS with Service Bus. We can define SAS to use in combination with topics, queues or notification hub
This security feature is pretty great, especially when you have an application that use 3rd party. You don’t need any more to share with 3rd parties the account name and key. From now one you can give them a unique token that give them access to some part of your namespace services and also limit what kind of operation they are allow to do.
In this moment you are allow to define only 12 rules in a namespace, but in the near future I expect to be able to define more than 12 rules.
The access rights that can be controlled in this moment are:

  • Listen – to be able to receive message
  • Send – to be able to send messages
  • Manage – to be able to manage the resource
From Service Bus perspective, this access rights are enough and in combination with expiration date it works pretty good. Be aware, that you can set Manage access rights, only if you set also the Send and Listen rights.
The interesting thing is how the rules are defined. The rules are defined over a URL. This means that you can define a rule over a namespace or over a specific topic or queue. The SAS will be valid for all resources under the specific URL.
When you generate a SAS rule, two keys will be generated for the same rules. Both keys can be used in the same time. This is done to help customers in the moment when they need to generate new keys and they don’t want to block the access to Service Bus.
The SAS rules can be defined and manage not only from code (using REST API) but also from the management portal of Azure. The portal can be very helpful when you are in the development phase or you have some issues with SAS and you want to check what rules are already defined.

How to create a SAS from code
First step is to create a SharedAccessAuthorizationRule and set the specific rights.
SharedAccessAuthorizationRule saar = new SharedAccessAuthorizationRule(
    “myFooName”,
    SharedAccessAuthorizationRule.GenerateRandomKey(),
    new[] {
        AccessRights.Manage, 
        AccessRights.Listen, 
        AccessRights.Send }));
GenerateRandomKey method is used to generate random keys.
Once the rule is created, you can add it to the queue, topic or notification hub using the description class
QueueDescription qd = …
qd.Autothorization.Add(saar);
Don’t forget to save the name of the name and the key of the rule.

How to use a SAS key
Once you created the rule and have the name and the key of the rule, you will be able to use them in the moment when you create the MessagingFactory.
MessagingFactory mf = MessagingFactory.Create(
    "uri,
    TokenProvider.CreateSharedAccessSignatureTokenProvider"myFooName", "myFooKey"));
QueueClient queue = mf.CreateQueueClient("myFooQueue");

Enjoy!

Comments

  1. I am creating a client-server application and need to let the clients create a subscription to a topic. But creating a subscription client seems like it needs the Manage right. Is it possible to create a subscription client only having the Listen right?

    ReplyDelete
    Replies
    1. No, it is not possible. You need Manage rights to create a subscription.

      Delete

Post a Comment

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see...

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provi...