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(...

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...