gpt4 book ai didi

azure - Bicep 循环 key 保管库 key 并加密存储帐户

转载 作者:行者123 更新时间:2023-12-02 08:19:59 25 4
gpt4 key购买 nike

我对实现逻辑有一些巨大的阻碍。

我正在与二头肌合作来创建这些资源。

  • 存储帐户
  • keystore
  • 将存储帐户连接字符串传递到 key 保管库 secret
  • 在 key 保管库中创建 key 并使用该 key 加密存储帐户。

前三个步骤已完成。如果我声明 2 个存储帐户,它将自动创建 2 个 secret 连接字符串和 2 个 key 。它们匹配的所有配对(存储名称和连接字符串)。

现在我面临的问题如下,首先,这是我的代码。

param tenantCode array = [
'dsec'
'sdre'
]

var storageName = [for item in tenantCode :{
name: string('sthrideveur${item}')
}]


var connectionStringSecretName = [for connection in storageName :{
name: '${connection.name}'

}]

output connectionStringSecretName array= [for connection in storageName :{
name: '${connection.name}'

}]






resource storage_Accounts 'Microsoft.Storage/storageAccounts@2021-06-01' = [ for name in storageName :{
name: '${name.name}'
location: 'westeurope'
sku: {
name: 'Standard_RAGRS'
}
kind: 'StorageV2'
properties: {
allowCrossTenantReplication: true
minimumTlsVersion: 'TLS1_2'
allowBlobPublicAccess: false
allowSharedKeyAccess: true
networkAcls: {
bypass: 'AzureServices'
virtualNetworkRules: []
ipRules: []
defaultAction: 'Allow'
}
supportsHttpsTrafficOnly: true
encryption: {
services: {
file: {
keyType: 'Account'
enabled: true
}
blob: {
keyType: 'Account'
enabled: true
}
}
keySource: 'Microsoft.Storage'
keyvaultproperties: {
keyname: '${tenantKey[0]}'
keyvaulturi: keyVault.id
}
}
accessTier: 'Cool'
}
}]

resource keyVault 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
name : 'XXX'
}

// Store the connection string in KV if specified
resource storageAccountConnectionString 'Microsoft.KeyVault/vaults/secrets@2019-09-01' = [for name in storageName :{
name: '${keyVault.name}/${name.name}'
properties: {
value: 'DefaultEndpointsProtocol=https;AccountName=${storage_Accounts[0].name};AccountKey=${listKeys('${storage_Accounts[0].id}', '${storage_Accounts[0].apiVersion}').keys[0].value};EndpointSuffix=${environment().suffixes.storage}'
}
}]

resource tenantKey 'Microsoft.KeyVault/vaults/keys@2021-06-01-preview' = [for tenant in tenantCode : {
name: '${keyVault.name}/Client-Key-${tenant}'
properties: {
keySize: 2048
kty: 'RSA'
}

}]

我想创建 2 个存储帐户。而按键,则包含存储代码。我想做但在实现时遇到问题的是如何将正确的 key 与正确的存储帐户相匹配。在这种特定情况下,我必须编写如下代码:

dsec
sdre

二头肌脚本将创建 2 个存储帐户和相应命名的 key :

sthrideveurdsec
sthrideveursdre

AND 2 secrets with the same name
sthrideveurdsec
sthrideveursdre

AND 2 Keys named
Client-Key-dsec
Client-Key-sdre

我要做的是使用 key DSEC 加密存储帐户 DSEC,并使用 key SDRE 加密存储 SDRE。但由于我是二头肌新手,因此在实现此方法时遇到一些问题。

如果有人能帮助我了解如何实现这种正确的配对,我将不胜感激。

更新:测试托马斯解决方案后,这是我收到的错误:

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"BadRequest","message":"{\r\n  \"error\": {\r\n    \"code\": \"KeyVaultPolicyError\",\r\n    \"message\": \"Keyvault policy recoverable is not set\"\r\n  }\r\n}"},{"code":"BadRequest","message":"{\r\n  \"error\": {\r\n    \"code\": \"KeyVaultPolicyError\",\r\n    \"message\": \"Keyvault policy recoverable is not set\"\r\n  }\r\n}"}]}}

最佳答案

我假设 key 保管库已创建并使用访问策略。

如果您想使用客户管理的 key 创建存储,则存储需要在创建之前获得对 key 保管库的访问权限,因此我在示例中使用用户分配的身份。

步骤如下:

  • 创建托管身份并授予对 key 保管库的 key 权限
  • 在 key 保管库中创建两个 key
  • 创建存储、分配托管身份并使用 key 进行加密
// Default values I'm using to test 
param location string = resourceGroup().location
param keyVaultName string = 'kvthomastest'
param managedIdentityName string = 'mi-storage-encryption-thomas-test'

param tenantCodes array = [
'dsec'
'sdre'
]

// I'm using prefix so I dont need to create additional arrays
var keyVaultKeyPrefix = 'Client-Key-'
var storagePrefix = 'stthomastest'

// Get a reference to key vault
resource keyVault 'Microsoft.KeyVault/vaults@2021-06-01-preview' existing = {
name: keyVaultName
}

// Create a managed identity
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: managedIdentityName
location: location
}

// Grant permissions to key vault
resource accessPolicy 'Microsoft.KeyVault/vaults/accessPolicies@2019-09-01' = {
name: 'add'
parent: keyVault
properties: {
accessPolicies: [
{
tenantId: subscription().tenantId
objectId: managedIdentity.properties.principalId
permissions: {
// minimum required permissions
keys: [
'get'
'unwrapKey'
'wrapKey'
]
}
}
]
}
}

// Create key vault keys
resource keyVaultKeys 'Microsoft.KeyVault/vaults/keys@2021-06-01-preview' = [for tenantCode in tenantCodes: {
name: '${keyVaultKeyPrefix}${tenantCode}'
parent: keyVault
properties: {
keySize: 2048
kty: 'RSA'
// storage key should only needs these operations
keyOps: [
'unwrapKey'
'wrapKey'
]
}
}]

// Create storage accounts
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = [for tenantCode in tenantCodes: {
name: '${storagePrefix}${tenantCode}'
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_RAGRS'
}
// Assign the identity
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${managedIdentity.id}': {}
}
}
properties: {
allowCrossTenantReplication: true
minimumTlsVersion: 'TLS1_2'
allowBlobPublicAccess: false
allowSharedKeyAccess: true
networkAcls: {
bypass: 'AzureServices'
virtualNetworkRules: []
ipRules: []
defaultAction: 'Allow'
}
supportsHttpsTrafficOnly: true
encryption: {
identity: {
// specify which identity to use
userAssignedIdentity: managedIdentity.id
}
keySource: 'Microsoft.Keyvault'
keyvaultproperties: {
keyname: '${keyVaultKeyPrefix}${tenantCode}'
keyvaulturi: keyVault.properties.vaultUri
}
services: {
file: {
keyType: 'Account'
enabled: true
}
blob: {
keyType: 'Account'
enabled: true
}
}
}
accessTier: 'Cool'
}
}]

关于azure - Bicep 循环 key 保管库 key 并加密存储帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69665879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com