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

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

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.51EF 6.0.2VS2013
It seems that there …

Run native .NET application in Docker (.NET Framework 4.6.2)

Scope
The main scope of this post is to see how we can run a legacy application written in .NET Framework in Docker.

Context
First of all, let’s define what is a legacy application in our context. By a legacy application we understand an application that runs .NET Framework 3.5 or higher in a production environment where we don’t have any more the people or documentation that would help us to understand what is happening behind the scene.
In this scenarios, you might want to migrate the current solution from a standard environment to Docker. There are many advantages for such a migration, like:

Continuous DeploymentTestingIsolationSecurity at container levelVersioning ControlEnvironment Standardization
Until now, we didn’t had the possibility to run a .NET application in Docker. With .NET Core, there was support for .NET Core in Docker, but migration from a full .NET framework to .NET Core can be costly and even impossible. Not only because of lack of features, but also because once you…