从二头肌模块中检索存储帐户访问密钥

问题描述 投票:0回答:2

在通过 Bicep 模块部署存储帐户时是否可以检索存储帐户的访问密钥?

我的父母二头肌使用模块文件创建了一个存储帐户,然后它需要一个访问密钥,但我无法让它以安全的方式工作:

父母二头肌

module functionAppStorageModule 'storage-account.bicep' = {
  name: 'functionAppStorage'
  params: {
    ...
  }
}

resource functionApp 'Microsoft.Web/sites@2021-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  properties: {
    siteConfig: {
      appSettings: [
        {
          name: 'store_key'
          value: ???
        }
      ]
    }
  }
}

如果我在模块文件上设置输出,并在父二头肌中使用该输出,我就能让它工作:

二头肌模块

output storageAccountStr string = 'AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'

父母二头肌

properties: {
        siteConfig: {
          appSettings: [
            {
              name: 'store_key'
              value: functionAppStorageModule.outputs.storageAccountStr 
            }
          ]
        }
      }

但这对我来说似乎并不安全,因为密钥以纯文本形式出现在 Azure 门户的“部署”输出部分中。

或者,我可以通过在不使用模块文件的情况下预先部署存储帐户来解决问题,因为模块的使用似乎是个问题,但只是想知道我在上面尝试的是不可能的?

谢谢

azure azure-functions azure-storage azure-resource-manager azure-bicep
2个回答
8
投票

如果您在不同的模块中创建函数应用程序,这应该有效。

storage-account.bicep
文件:

param storageAccountName string
...

// Create the storage account
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
  name: storageAccountName
  ...
}

// return the name
output name string = storageAccount.name

function-app.bicep
文件:

...
param storageAccountName string 

// Get a reference to the existing storage
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' existing = {
  name: storageAccountName
}

// Create the function app
resource functionApp 'Microsoft.Web/sites@2021-03-01' = {
  ...
  properties: {
    siteConfig: {
      appSettings: [
        {
          name: 'store_key'
          // Here we can securely get the access key
          value: 'AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'
        }
      ]
    }
  }
}

然后在你的

main.bicep

// Create the storage account
module storage 'storage-account.bicep' = {
  name: 'functionAppStorage'
  params: {
    storageAccountName: storageAccountName
    ...
  }
}

// create the function app once the storage has been created
module functionApp 'function-app.bicep' = {
  name: 'functionApp'
  params: {
    ...
    // depends on storage module
    storageAccountName: storage.outputs.name
  }
}


0
投票

我找到了答案。这是一个示例,说明如何重写外部

listKeys()
调用以使用资源中的辅助函数。

旧:

AzureWebJobsStorage: 'DefaultEndpointsProtocol=https;AccountName=${res_functionStorage.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(res_functionStorage.id, res_functionStorage.apiVersion).keys[0].value}'

新:

AzureWebJobsStorage: 'DefaultEndpointsProtocol=https;AccountName=${res_functionStorage.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${res_functionStorage.listKeys().keys[0].value}'
© www.soinside.com 2019 - 2024. All rights reserved.