{"id":93457,"date":"2022-03-14T17:00:58","date_gmt":"2022-03-14T17:00:58","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=93457"},"modified":"2022-05-22T21:03:22","modified_gmt":"2022-05-22T21:03:22","slug":"azure-storage-policies-control-sas-from-the-server-side","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/azure-storage-policies-control-sas-from-the-server-side\/","title":{"rendered":"Azure Storage Policies: Control SAS from the server side"},"content":{"rendered":"<p>*Update at the end<\/p>\n<p><strong>Azure Storage Policies<\/strong> are around for some time already, but they manage to stay unnoticed. This feature is very powerful and few people know about it.<\/p>\n<p>Probably you already know about <strong>Storage Keys<\/strong> and <strong>Shared Access Security<\/strong>. It&#8217;s important to highlight one limitation: Once we generate a SAS key, we can&#8217;t change or cancel it anymore.<\/p>\n<p>The SAS keys are not stored on the server side. Once they are generated, the server has no control over it and we can&#8217;t block it or make any change.<\/p>\n<p>Very few people know there is a solution for this: <strong>Azure Storage Policies<\/strong>. The process is very simple. You create a Storage Policy for your container. You create a SAS key based on the storage policy and the magic is done. The policy is a server object which give you control over the generated SAS keys.<\/p>\n<h2>Creating and Testing a Storage Policy<\/h2>\n<p>Let&#8217;s follow some steps and test this concept.<\/p>\n<p>1) Locate a file in a private blob container on your Azure subscription<\/p>\n<p>2) Try to open the file URL. You can&#8217;t, unless you provide authentication. We will do this with a SAS key.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93466\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/02\/StoragePolicy08.png\" alt=\"\" width=\"1280\" height=\"102\" \/><\/p>\n<p>3) On the container, click &#8220;&#8230;&#8221; to open the menu and click <strong>Access Policies<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93459\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/02\/StoragePolicy01.png\" alt=\"\" width=\"630\" height=\"324\" \/><\/p>\n<p>4) Configure the permission, start date and expiration date<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93467\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/02\/StoragePolicy09.png\" alt=\"\" width=\"531\" height=\"403\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>This was the easy part. The next step is to create the SAS key based on the storage policy. You can&#8217;t do this through the portal. You can use <strong>Powershell<\/strong> to create the key or use <strong>Azure Storage Explorer<\/strong>. The Azure Portal doesn&#8217;t implement this feature. This explains a lot why this feature goes unnoticed.<\/p>\n<p>5) On Azure Storage Explorer, right click the file and select the option called\u00a0<strong>Generate Shared Access Signature<\/strong>. The resulting window allows you to choose an existing Azure Policy to tie the key with.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93461\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/02\/StoragePolicy03.png\" alt=\"\" width=\"618\" height=\"720\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93462\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/02\/StoragePolicy04.png\" alt=\"\" width=\"608\" height=\"679\" \/><\/p>\n<p>6) Click <strong>Create<\/strong> button to generate the key.<\/p>\n<p>The resulting key is different than the usual SAS keys. It doesn&#8217;t include the parameters for the expiration date and the user permission. It includes a reference to the policy and it will use the information it has.<\/p>\n<p>This enables you to manage many different scenarios for the generated SAS keys:<\/p>\n<ul>\n<li>Modify the user permission in the SAS keys<\/li>\n<li>Change the expiration date in the SAS keys<\/li>\n<li>Disable the SAS keys &#8211; by dropping the policy<\/li>\n<\/ul>\n<p>Let&#8217;s complete some final tests to illustrate this:<\/p>\n<p>7) Try to download the file using the generated link. You will be able to download it.<\/p>\n<p>8) On the portal, delete the storage policy.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93464\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/02\/StoragePolicy06.png\" alt=\"\" width=\"638\" height=\"218\" \/><\/p>\n<p>9) Try to download the file again. You may need to use an anonymous window to avoid cache. You will receive an authentication error.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93465\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/02\/StoragePolicy07.png\" alt=\"\" width=\"1098\" height=\"144\" \/><\/p>\n<h2>Limitations<\/h2>\n<p>The Storage Account policies are created on the level of the container. You can control the user permission, but you can&#8217;t control the granularity. The use will have access to the entire container.<\/p>\n<p>The policies are available for blob containers only, they can&#8217;t be created on file shares.<\/p>\n<h2>Summary<\/h2>\n<p>Storage Account Policy is a hidden feature which enables us to do what most people believe not being possible: Manage SAS keys from the server side.<\/p>\n<p><strong>UPDATE 22\/05:<\/strong> When I wrote this blog the policies could only be used in Azure Storage Explorer. Some weeks later, a new option became available on the Azure portal, allowing us to use the policies with SAS keys generated in the portal.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-94454\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2022\/03\/StoragePolicy10.png\" alt=\"\" width=\"612\" height=\"405\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>*Update at the end Azure Storage Policies are around for some time already, but they manage to stay unnoticed. This feature is very powerful and few people know about it. Probably you already know about Storage Keys and Shared Access Security. It&#8217;s important to highlight one limitation: Once we generate a SAS key, we can&#8217;t&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5364,124839,5336],"coauthors":[6810],"class_list":["post-93457","post","type-post","status-publish","format-standard","hentry","category-blogs","tag-azure","tag-azure-blob-storage","tag-cloud"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/93457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=93457"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/93457\/revisions"}],"predecessor-version":[{"id":94455,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/93457\/revisions\/94455"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=93457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=93457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=93457"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=93457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}