{"id":6961,"date":"2016-02-18T15:06:12","date_gmt":"2016-02-18T14:06:12","guid":{"rendered":"https:\/\/blog.redbaronofazure.com\/?p=6961"},"modified":"2016-02-19T17:10:28","modified_gmt":"2016-02-19T16:10:28","slug":"limiting-a-sas-token-to-be-used-from-an-ip-range","status":"publish","type":"post","link":"https:\/\/blog.redbaronofazure.com\/?p=6961","title":{"rendered":"Limiting a SAS-Token to be used from an IP Range"},"content":{"rendered":"<p>This post is also about data ingestion to Blob Storage using a SAS-Token. But if the previous showed how you could create a SAS-Token just-in-time to upload a file via a web browser, this post covers the scenario where you want to lock down upload rights to a specific ip address or range of ip addresses. It might be a B2B scenario or a mobile unit uploading data from somewhere in the field.<\/p>\n<p><strong>Generating the SAS-token with IP address range<\/strong><\/p>\n<p>It is really quite simple to\u00a0lock the SAS-Token to a certain ip range. You just use one of the overlapped methods of GetSharedAccessSignature that takes the IPAddressOrRange object as the last parameter. The IPAddressOrRange takes a from-to addres range, which means you have to specify it as 11.22.33.41 and 11.22.33.49 if that is the range you want.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Generate.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-6971\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Generate.png\" alt=\"SAS-Tokens-2-Generate\" width=\"812\" height=\"428\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Generate.png 812w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Generate-300x158.png 300w\" sizes=\"(max-width: 812px) 100vw, 812px\" \/><\/a>In the above example, the SAS-Token is generated on the Container level with permission to Write for a certain amount of time, so that the sending application can upload files to this container.<\/p>\n<p>The generated url must be passed on and perhaps stored in a config file in the sending application.<\/p>\n<p><strong>Using the SAS-token to upload files<\/strong><\/p>\n<p>Since you only have a SAS-Token for a Container and not the key to the Storage Account, you have to work directly with the Container when you upload files. The code to upload a file is really just a few lines. The SAS-Token is read from the config file and we grab access to the Container using it. The GetBlockBlobReference names the blob and gives us a reference to where we would like to upload it to.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Upload.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-6981\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Upload.png\" alt=\"SAS-Tokens-2-Upload\" width=\"866\" height=\"342\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Upload.png 866w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/02\/SAS-Tokens-2-Upload-300x118.png 300w\" sizes=\"(max-width: 866px) 100vw, 866px\" \/><\/a><\/p>\n<p>If you try to do this from an ip address that is not in the allowed range, the upload will fail with a HTTP 403 Forbidden status code.<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>This technique is publically documented but\u00a0yet suprisingly many people I&#8217;ve met do not know it and therefor say that you can&#8217;t limit who can upload files to an Azure Storage Container. Generating a SAS-Token with the IPAddressOrRange specifies may be just what you are looking for since it retricts access to ip addresses.<\/p>\n<p><strong>References<\/strong><\/p>\n<p>MSDN Documentation for GetSharedAccessSignature<br \/>\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/azure\/mt616570.aspx\" target=\"_blank\">https:\/\/msdn.microsoft.com\/en-us\/library\/azure\/mt616570.aspx<\/a><\/p>\n<p>SharedAccessSignature &#8211; documentation<br \/>\n<a href=\"https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/storage-dotnet-shared-access-signature-part-1\/\" target=\"_blank\">https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/storage-dotnet-shared-access-signature-part-1\/<\/a><\/p>\n<p>&#8230;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is also about data ingestion to Blob Storage using a SAS-Token. But if the previous showed how you could create a SAS-Token just-in-time to upload a file via a web browser, this post covers the scenario where you want to lock down upload rights to a specific ip address or range of ip [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[321],"tags":[381,31],"_links":{"self":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/6961"}],"collection":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6961"}],"version-history":[{"count":0,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/6961\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}