未加星标

Creating Azure Storage SAS Tokens with ARM Templates

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Shared access signatures, sometimes also called SAS tokens, allow for delegating access to a designated part of an Azure resource with a defined set of permissions. They can be used to allow various types of access to your Azure services while keeping your access keys secret.

In a recent update to Azure Resource Manager, Microsoft has added the ability to create SAS tokens from ARM templates. While this is a general-purpose feature that will hopefully work across a multitude of Azure services, for now it only seems to work with Azure Storage (at least of the services I’ve checked). In this post I’ll explain why this is useful, and give some example ARM templates that illustrate creating both account and service SAS tokens.

Use Cases

There are a few situations where it’s helpful to be able to create SAS tokens for an Azure Storage account from an ARM template. One example is when using the Run From Package feature an ARM template can deploy an App Service, deploy a storage account, and create a SAS token for a package blob even if it doesn’t exist at deployment time.

Another example might be for an Azure Functions app. A very common scenario is for a function to receive a file as input, transform it in some way, and save it to blob storage. Rather than using the root access keys for the storage account, we could create a SAS token and add it to the Azure Functions app’s config settings, like in this example ARM template .

How It Works

ARM templates now support a new set of functions for generating SAS tokens. For Azure Storage, there are two types of SAS tokens account and service and the listAccountSas and listServiceSas functions correspond to these, respectively.

Behind the scenes, these functions invoke the corresponding functions on the Azure Storage ARM provider API. Somewhat confusingly, even though the functions’ names start with list , they are actually creating SAS tokens and not listing or working with previously created tokens. (In fact, SAS tokens are not resources that Azure can track, so there’s nothing to list.)

Example

Int his post I’ll show ARM templates that use a variety of types of SAS for different types of situations. For simplicity I’ve used the outputs section to emit the SASs, but these could easily be passed through to other parts of the template such as App Service app settings ( as in this example ). Also, in this post I’ve only dealt with blob storage SAS tokens, but the same process can easily be used for queues and tables, and even let us restrict token holders to only access to table partitions or sets of rows.

Creating a Service SAS

By using a service SAS, we can grant permissions to a specific blob in blob storage, or to all blobs within a container. Similarly we can grant permissions to an individual queue, or to a subset of entities within a table. More documentation on constructing service SASs is available here , and the details of the listServiceSas function is here.

Service SASs require us to provide a canonicalizedResource , which is just a way of describing the scope of the token. We can use the path to an individual blob by using the form /blob/{accountName}/{containerName}/{path} , such as /blob/mystorageaccountname/images/cat.jpeg or /blob/mystorageaccountname/images/cats/fluffy.jpeg . Or, we can use the path to a container by using the form /blob/mystorageaccountname/images . The examples below show different types of canonicalizedResource property values.

Read Any Blob Within a Container

Our first SAS token will let us read all of the blobs within a given container. For this, we’ll need to provide four properties.

First, we’ll provide a canonicalizedResource . This will be the path to the container we want to allow the holder of the token to read from. We’ll construct this field dynamically based on the ARM template input.

Second, we need to provide a signedResource . This is the type of resource that the token is scoped to. In this case, we’re creating a token that works across a whole blob container and so we’ll use the value c .

Third, we provide the signedPermission . This is the permission, or set of permissions, that the token will allow. There are different permissions for reading, creating, and deleting blobs and other storage entities. Importantly, listing is considered to be separate to reading, so bear this in mind too. Because we just want to allow reading blobs, we’ll use the value r .

Finally, we have to provide an expiry date for the token. I’ve set this to January 1, 2050.

When we execute the ARM template with the listServiceSas function, we need to provide these values as an object. Here’s what our object looks like:

"serviceSasFunctionValues": {
"canonicalizedResource": "[concat('/blob/', parameters('storageAccountName'), '/', parameters('containerName'))]",
"signedResource": "c",
"signedPermission": "r",
"signedExpiry": "2050-01-01T00:00:00Z"
}

And here’s the ARM template check out line 62, where the listServiceSas function is actually invoked.

Read A Single Blob

In many cases we will want to issues SAS tokens to only read a single blob, and not all blobs within a container. In this case we make a couple of changes from the first example.

First, our canonicalizedResource now will have the path to the individual blob, not to a container. Again, we’ll pull these from the ARM template parameters.

Second, the signedResource is now a blob rather than a container, so we use the value b instead of c .

Here’s our properties object for this SAS token:

"serviceSasFunctionValues": {
"canonicalizedResource": "[concat('/blob/', parameters('storageAccountName'), '/', parameters('containerName'), parameters('blobName'))]",
"signedResource": "b",
"signedPermission": "r",
"signedExpiry": "2050-01-01T00:00:00Z"
}

And here’s the full ARM template:

Write A New Blob SAS tokens aren’t just for reading, of course. We can also create a token that will allow creating or writing to blobs. One common use case is to allow the holder of a SAS to create a new blob, but not to overwrite anything that already exists. To create a SAS for this scenario, we work back at the container level again so the canonicalizedResource property is set to the path to the container, and the signedResource is set to c . This time, we set signedPermission to c to allow for blobs to be created. (If we wanted to also allow over

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

代码区博客精选文章
分页:12
转载请注明
本文标题:Creating Azure Storage SAS Tokens with ARM Templates
本站链接:https://www.codesec.net/view/611364.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(55)