SAS令牌作为SecureString不能与使用Azure PowerShell的ARM模板部署一起使用

时间:2019-02-13 10:40:57

标签: azure-powershell arm-template securestring sas-token

我有一堆嵌套的ARM模板,打算使用Azure PS进行部署。

唯一的方法是将这些模板托管在Azure blob container中,然后生成SAS token并在main ARM template中发送这两个参数(指向嵌套参数)。

这是我的生成SAS令牌的PS:

$SasToken = ConvertTo-SecureString -AsPlainText -Force (New-AzureStorageContainerSASToken -Container $StorageContainerName -Context $StorageAccount.Context -Permission r -ExpiryTime (Get-Date).AddHours(4))

这是我的部署脚本的2部分,它们将令牌传递给主要的ARM模板:

$Parameters['_artifactsLocationSasToken'] = $SasToken

New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
    -ResourceGroupName $ResourceGroupName `
    -TemplateFile $TemplateFile `
    -TemplateParameterObject $Parameters `
    -Force -Verbose `
    -ErrorVariable ErrorMessages

这是主ARM模板的接收参数的声明:

"_artifactsLocationSasToken": {
      "type": "securestring"
    }

这是同一主ARM模板中的嵌套资源模板(恰好是一个cosmos db):

{
      "apiVersion": "2017-05-10",
      "dependsOn": [
        "[concat('Microsoft.Resources/deployments/', variables('vnetConfig').Name)]"
      ],
      "name": "[variables('cosmosDbConfig').Name]",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[concat(parameters('_artifactsLocation'), '/', variables('nestedTemplatesFolder'), '/cosmosdb.json', parameters('_artifactsLocationSasToken'))]"
        },
        "parameters": {
          "cosmosDbConfig": {
            "value": "[variables('cosmosDbConfig')]"
          }
        }
      },
      "type": "Microsoft.Resources/deployments"
    }

运行这些命令时,出现此错误:

  

错误:代码= InvalidTemplate; Message =部署模板验证   失败:“为模板参数提供的值   第'16'行和第'39'列的'_artifactsLocationSasToken'不是   有效。'

如果我在嵌套模板资源(在主模板中)中对SAS token进行硬编码,然后将类型从securestring更改为string,就可以了! 我想念的是什么?

2 个答案:

答案 0 :(得分:0)

似乎您错过了templateLink中的uri(),请尝试以下操作,请参阅也使用securestring的{​​{3}}。

"templateLink": {
          "uri": "[uri(concat(parameters('_artifactsLocation'), '/', variables('nestedTemplatesFolder'), '/cosmosdb.json', parameters('_artifactsLocationSasToken')))]"
        }

答案 1 :(得分:0)

我发现您可以重现此问题,即当将SAS令牌作为参数传递给ARM模板时,“模板验证失败”。

在我的情况下,SAS令牌用于Azure Function应用程序的“ WEBSITE_RUN_FROM_PACKAGE”应用程序设置。

我对这个问题的解决方案是在SAS令牌的值(我从PowerShell传递到ARM模板)的前面加上前缀-使其不再是有效的URL。 例如,如果为SAS令牌添加下划线前缀,并将其传递给ARM模板,则不会再出现此问题。 然后,您可以从ARM模板中删除下划线前缀。