如何使用 SAS 令牌部署专用 ARM 模板

如果 Azure 资源管理器模板(ARM 模板)位于存储帐户中,可以限制对该模板的访问,以免将其公开暴露。 访问受保护模板的方法是:为模板创建一个共享访问签名 (SAS) 令牌,在部署时提供该令牌。 本文介绍如何使用 Azure PowerShell 或 Azure CLI 通过 SAS 令牌来安全地部署 ARM 模板。

你将会了解到如何保护和管理对专用 ARM 模板的访问,包括关于如何执行以下操作的说明:

  • 使用受保护的容器创建存储帐户
  • 将模板上传到存储帐户
  • 在部署期间提供 SAS 令牌

重要

请考虑使用模板规格(而不是使用 SAS 令牌)来保护专用模板。 使用模板规范,用户可以与组织中的其他用户共享模板,并通过 Azure RBAC 管理对模板的访问权限。

使用受保护的容器创建存储帐户

以下脚本创建存储帐户和容器,其中的公共访问权限已禁用以确保模板安全性。

注意

在可以在由世纪互联运营的 Microsoft Azure 中使用 Azure CLI 之前,请先运行 az cloud set -n AzureChinaCloud 来更改云环境。 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloud

az cloud set -n AzureChinaCloud
az log

az group create \
  --name "ExampleGroup" \
  --location "China North"
az storage account create \
    --resource-group ExampleGroup \
    --location "China North" \
    --sku Standard_LRS \
    --kind Storage \
    --name {your-unique-name}
connection=$(az storage account show-connection-string \
    --resource-group ExampleGroup \
    --name {your-unique-name} \
    --query connectionString)
az storage container create \
    --name templates \
    --public-access Off \
    --connection-string $connection

将专用模板上传到存储帐户

现在可以将模板上传到存储帐户了。 提供要使用的模板的路径。

az storage blob upload \
    --container-name templates \
    --file azuredeploy.json \
    --name azuredeploy.json \
    --connection-string $connection

在部署期间提供 SAS 令牌

要在存储帐户中部署专用模板,请生成 SAS 令牌,并将其包括在模板的 URI 中。 设置到期时间以允许足够的时间来完成部署。

重要

只有帐户所有者才能访问包含该专用模板的 Blob。 但是,如果为 blob 创建 SAS 令牌,则拥有该 URI 的任何人都可以访问 blob。 如果其他用户截获了该 URI,则此用户可以访问该模板。 使用 SAS 令牌是限制对模板的访问的好办法,但不应直接在模板中包括密码等敏感数据。

下面的示例在本地 Shell 中使用 Bash 环境。 其他环境可能需要不同的语法来创建 SAS 令牌的到期时间。

expiretime=$(date -u -d '30 minutes' +%Y-%m-%dT%H:%MZ)
connection=$(az storage account show-connection-string \
    --resource-group ExampleGroup \
    --name {your-unique-name} \
    --query connectionString)
token=$(az storage blob generate-sas \
    --container-name templates \
    --name azuredeploy.json \
    --expiry $expiretime \
    --permissions r \
    --output tsv \
    --connection-string $connection)
url=$(az storage blob url \
    --container-name templates \
    --name azuredeploy.json \
    --output tsv \
    --connection-string $connection)
az deployment group create \
  --resource-group ExampleGroup \
  --template-uri $url?$token

有关将 SAS 令牌与链接模板配合使用的示例,请参阅将已链接的模版与 Azure 资源管理器配合使用

后续步骤