【问题标题】:How to add encryption AKV on Azure storage account through ARM template如何通过 ARM 模板在 Azure 存储帐户上添加加密 AKV
【发布时间】:2021-09-13 00:31:18
【问题描述】:

我正在尝试在存储帐户 (ADLS gen2) 上添加加密。并使用带有服务主体的 ARM 模板(通过 Portal 可以正常工作)。 我找到的代码很简单,如下所示,要添加到 StorageAccount 的加密部分。

"keySource": "Microsoft.Keyvault",
"keyvaultproperties": {
    "keyvaulturi": "https://akv-***.vault.azure.net",
    "keyname":"storageKey",
    "keyversion": "c355****68e52361156"                    
}

但它没有这样做,给出错误

状态消息:由于身份验证问题,操作失败 在密钥库上。更多 信息,(代码:KeyVaultAuthenticationFailure)

我的 AKS 在另一个资源组中,而我的存储帐户在不同的资源组中(根据公司标准)。这是原因吗?那我该怎么办?

或者我读过几篇文章,建议在“Key Vault”上添加访问权限,以获取、包装和解开存储帐户的权限。我对此表示怀疑,因为我可以手动将同一密钥的加密添加到存储帐户。

需要任何帮助吗?

【问题讨论】:

  • 你好@Amit。如果我的回答对您有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢

标签: azure azure-blob-storage azure-keyvault azure-storage-account


【解决方案1】:

我重复发现 AKV 和存储帐户也应该在同一资源组和同一区域中,然后只有您才能在存储帐户上添加 AKV。

将您的 Azure 存储帐户配置为将客户管理的密钥与 Azure Key Vault 一起使用,然后指定要与存储帐户关联的密钥。

第 1 步:将此 ARM 模板保存在您的 VS 代码中,并以任意名称保存文件。

这是一个完整且功能齐全的 ARM 模板,您需要部署 2 次才能完全配置它。第一次运行将创建 Key Vault 和存储帐户。然后,您需要在 Key Vault 中手动创建密钥并再次运行部署以让它完成其余的工作。

此模板使用嵌套部署,将存储帐户的配置与关键信息与 2 个资源的创建分开,以获得完整的示例。

模板

    {
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "keyName": {
      "type": "string",
      "defaultValue": "storage-enc-test"
    },
    "keyVersion": {
      "type": "string",
      "defaultValue": ""
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "configureEncryptionKey": {
      "type": "bool",
      "defaultValue": false
    }
  },
  "variables": {
    "uniqueResourceNameBase": "simplekeyvalut7735"
  },
  "resources": [
    {
      "condition": "[not(parameters('configureEncryptionKey'))]",
      "type": "Microsoft.KeyVault/vaults",
      "name": "[variables('uniqueResourceNameBase')]",
      "apiVersion": "2016-10-01",
      "location": "[parameters('location')]",
      "properties": {
        "sku": {
          "family": "A",
          "name": "standard"
        },
        "tenantId": "[subscription().tenantid]",

        "accessPolicies": [],
        "enableSoftDelete": true,
        "enablePurgeProtection": true
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('uniqueResourceNameBase'))]"
      ]
    },
    {
      "type": "Microsoft.Storage/storageAccounts",
      "sku": {
        "name": "Standard_LRS",
        "tier": "Standard"
      },
      "kind": "Storage",
      "name": "[variables('uniqueResourceNameBase')]",
      "apiVersion": "2019-06-01",
      "location": "[parameters('location')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "properties": {
        "supportsHttpsTrafficOnly": true
      },
      "dependsOn": []
    },
    {
      "condition": "[parameters('configureEncryptionKey')]",
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2019-07-01",
      "name": "updateStorageAccount",
      "dependsOn": [
        "[resourceId('Microsoft.KeyVault/vaults', variables('uniqueResourceNameBase'))]"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "0.1.0.0",
          "resources": [
            {
              "type": "Microsoft.KeyVault/vaults/accessPolicies",
              "name": "[concat(variables('uniqueResourceNameBase'), '/add')]",
              "apiVersion": "2019-09-01",
              "properties": {
                "accessPolicies": [
                  {
                    "tenantId": "[subscription().tenantid]",
                    "objectId": "[reference(resourceId('Microsoft.Storage/storageAccounts', variables('uniqueResourceNameBase')),'2019-06-01', 'full').identity.principalId]",
                    "permissions": {
                      "keys": [
                        "wrapkey",
                        "unwrapkey",
                        "get"
                      ],
                      "secrets": [],
                      "certificates": []
                    }
                  }
                ]
              }
            },
            {
              "type": "Microsoft.Storage/storageAccounts",
              "sku": {
                "name": "Standard_LRS",
                "tier": "Standard"
              },
              "kind": "Storage",
              "name": "[variables('uniqueResourceNameBase')]",
              "apiVersion": "2019-06-01",
              "location": "[parameters('location')]",
              "identity": {
                "type": "SystemAssigned"
              },
              "properties": {
                "encryption": {
                  "services": {
                    "file": {
                      "enabled": true
                    },
                    "blob": {
                      "enabled": true
                    }
                  },
                  "keySource": "Microsoft.Keyvault",
                  "keyvaultproperties": {
                    "keyvaulturi": "[reference(resourceId('Microsoft.KeyVault/vaults', variables('uniqueResourceNameBase')),'2016-10-01', 'full').properties.vaultUri]",
                    "keyname": "[parameters('keyName')]",
                    "keyversion": "[parameters('keyversion')]"
                  }
                }
              },
              "dependsOn": [
                "[resourceId('Microsoft.KeyVault/vaults/accessPolicies', variables('uniqueResourceNameBase'), 'add')]"
              ]
            }
          ]
        }
      }
    }
  ]
}

我取了一个变量。

"uniqueResourceNameBase": "simplekeyvalut7735" 用于根据您的选择提供存储帐户和密钥保管库的名称,您可以编辑上述代码并分配其他名称。

第 2 步:输入以下命令将模板部署到资源组中。

New-AzResourceGroupDeployment -name testdeployment -ResourceGroupName v-rahul********* -模板文件 C:\Users\v-rash**\venv\Lib\site-packages\pandas\io\azuredeploy.json

第 3 步:再次运行上述命令,如第 1 步所述。

第 4 步:手动创建密钥。

第 5 步:最后,将特殊标志 configureEncryptionKey 设置为 true 以在存储帐户上运行配置步骤。

New-AzResourceGroupDeployment -name testdeployment -ResourceGroupName v-ra###M## -TemplateFile C:\Users\v-rash**\venv\Lib\site-packages\pandas\io\azuredeploy.json -configureEncryptionKey $true -Verbose

您可能会在设置 keyvault 密钥名称时遇到此错误。我已手动设置 storage-enc-test 并再次重新运行上述命令。

结果:像这样触发第二次部署:

如果我查看 Azure 门户,我们可以看到密钥已正确配置。

参考:https://www.codeisahighway.com/how-to-use-customer-managed-keys-with-azure-key-vault-and-azure-storage-encryption-using-arm-template/

【讨论】:

  • 感谢您的解决方案。好吧,我不能创建一个新的 KeyVault 作为我们公司提供的标准配置。
猜你喜欢
  • 2021-03-12
  • 2020-10-04
  • 2017-01-02
  • 2019-10-23
  • 1970-01-01
  • 2018-02-09
  • 2019-11-30
  • 1970-01-01
  • 2020-09-28
相关资源
最近更新 更多