Recommended approach for outputting/getting secrets #6173
-
What is the recommended approach for outputting secrets? I understand why it's not great to output secrets from a module (as described here https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/azure-resource-manager/bicep/linter-rule-outputs-should-not-contain-secrets.md#linter-rule---outputs-should-not-contain-secrets), but what is then the recommended approach to get eg. a connection string in your Bicep? If I create a module that creates for example a If I try to use
Then I get this error:
So can't I use modules for this? Or am I missing the point. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 31 replies
-
I had the same issue creating a Function App, but there I was able to configure it to have the function keys stored in a key vault. |
Beta Was this translation helpful? Give feedback.
-
Hi @jonas-lomholdt, The output is probably not allowed because it potentiallyly can leak secrets. The way to retrieve secrets is to use the resource kv 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
name: kvName
scope: resourceGroup(subscriptionId, kvResourceGroup )
} If you have done this you can use the symbolic name |
Beta Was this translation helpful? Give feedback.
-
FWIW, I recently also met this issue and tried many ways to use the modules and try to set secret with it at same time. --------------- First ------------------ resource storageAccountRef 'Microsoft.Storage/storageAccounts@2022-05-01' existing = {
// DO NOT USE storageAccount.outputs.name
name: storageAccount.name
scope: resourceGroup
}
var storageAccountKey = listKeys(resourceId(subscription().subscriptionId, resourceGroup.name, storageAccountRef.type, storageAccountRef.name), storageAccountRef.apiVersion).keys[0].value But this requires
--------------- Second ------------------ And then use accessor operator to access the key or connection string of it and set it as secure value. You also need to pass the // main.bicep
module keyVault 'key-vault.bicep' = {
name: '${keyVaultNamePrefix}${location}'
scope: resourceGroup
params: {
namePrefix: keyVaultNamePrefix
location: location
}
}
module storageAccount 'storage-account.bicep' = {
name: '${storageAccountNamePrefix}${location}'
scope: resourceGroup
params: {
namePrefix: storageAccountNamePrefix
location: location
keyVaultName: keyVault.name
}
} // key-vault-secret.bicep
param keyVaultName string
param secretName string
@secure()
param secretValue string
resource keyVaultSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
name: '${keyVaultName}/${secretName}'
properties: {
...
value: secretValue
}
} // storage-account.bicep
param location string = resourceGroup().location
param namePrefix string
param keyVaultName string
var connectionStringName = 'storage-account-connection-string'
resource storageAccount 'Microsoft.Storage/storageAccounts@2022-05-01' = {
name: '${namePrefix}${location}'
location: location
properties: {
...
}
}
// THIS IS THE MOST IMPORTANT STEP
module storageConnectionString 'key-vault-secret.bicep' = {
name: connectionStringName
params: {
keyVaultName: keyVaultName
secretName: connectionStringName
secretValue: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};AccountKey=${storageAccount.listKeys().keys[0].value};EndpointSuffix=${environment().suffixes.storage}'
}
} |
Beta Was this translation helpful? Give feedback.
Hi @jonas-lomholdt,
The output is probably not allowed because it potentiallyly can leak secrets.
The way to retrieve secrets is to use the
existing
keyword "calling" your keyvault.If you have done this you can use the symbolic name
kv
and call thekv.getSecret('myKvKey')
method. I would use this approach instead of outputting secrets.