【问题标题】:Azure: store resource's secret in Key Vault created by ARM templateAzure:将资源的机密存储在由 ARM 模板创建的 Key Vault 中
【发布时间】:2020-09-18 17:45:53
【问题描述】:

我有提供数据湖的 ARM 模板,我想将其秘密存储在密钥库中。 我假设我应该使用 ARM 中的输出部分,像这样的 JSON,但是我应该如何将它存储在已经存在的 (!) Key Vault 中?

"outputs": {
    "storageAccountName": {
        "type": "string",
        "value": "[variables('storageAccountName')]"
    },
    "storageAccountConnectionString": {
        "type": "string",
        "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountResourceId'), variables('storageAccountApiVersion')).keys[0].value)]"
    }
}

【问题讨论】:

  • 下面的答案很好,但有一点 - 不要将秘密放在模板部署的输出中 - 对部署(例如资源组)具有“读取”权限的用户可以看到输出,所以它会将秘密暴露给这些用户。相反,输出包含秘密的资源的 resourceId() 并在需要它的模板中使用它(下面的答案中的示例)

标签: azure azure-resource-manager azure-keyvault azure-template


【解决方案1】:

您可以使用 ARM 模板向 Key Vault 添加值,也可以在 ARM 模板中读取它们。

resource 下方为每个密钥保管库机密添加

{
      "type": "Microsoft.KeyVault/vaults/secrets",
      "location": "[parameters('location')]",
      "name": "[concat(parameters('keyVaultName'), '/', 'api', '--storageAccountConnectionString')]",
      "apiVersion": "parameters('apiVersion')",
      "dependsOn": [
        "[variables('keyVaultResourceId')]",
        "[variables('serviceBusResourceId')]"
      ],
      "properties": {
        "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountResourceId'), variables('storageAccountApiVersion')).keys[0].value)]",
        "contentType": "text/plain"
      }
    },

部署后通过ARM模板中的参数值读取这个secret:

"storageAccountConnectionString": {
      "reference": {
        "keyVault": {
          "id": "/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP/providers/Microsoft.KeyVault/vaults/KEY_VAULT_NAME"
        },
        "secretName": "api--storageAccountConnectionString"
      }
    },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2017-04-24
    • 2016-12-03
    • 2016-06-28
    • 2018-09-22
    • 2020-09-09
    • 2017-07-07
    相关资源
    最近更新 更多