Skip to content

Commit bc7703f

Browse files
authored
Merge pull request #273742 from mumian/0429-get-secret
Refresh getSecret()
2 parents d2d748c + 9691a3b commit bc7703f

File tree

1 file changed

+34
-56
lines changed

1 file changed

+34
-56
lines changed

articles/azure-resource-manager/bicep/key-vault-parameter.md

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Key Vault secret with Bicep
33
description: Shows how to pass a secret from a key vault as a parameter during Bicep deployment.
44
ms.topic: conceptual
55
ms.custom: devx-track-azurepowershell, devx-track-azurecli, devx-track-bicep
6-
ms.date: 06/23/2023
6+
ms.date: 05/06/2024
77
---
88

99
# Use Azure Key Vault to pass secure parameter value during Bicep deployment
@@ -151,22 +151,25 @@ The following procedure shows how to create a role with the minimum permission,
151151

152152
When using a key vault with the Bicep file for a [Managed Application](../managed-applications/overview.md), you must grant access to the **Appliance Resource Provider** service principal. For more information, see [Access Key Vault secret when deploying Azure Managed Applications](../managed-applications/key-vault-access.md).
153153

154-
## Use getSecret function
154+
## Retrieve secrets in Bicep file
155155

156-
You can use the [getSecret function](./bicep-functions-resource.md#getsecret) to obtain a key vault secret and pass the value to a `string` parameter of a module. The `getSecret` function can only be called on a `Microsoft.KeyVault/vaults` resource and can be used only with parameter with `@secure()` decorator.
156+
You can use the [getSecret function](./bicep-functions-resource.md#getsecret) in Bicep files to obtain a key vault secret. Note that the `getSecret` function is exclusively applicable to a `Microsoft.KeyVault/vaults` resource. Additionally, it's restricted to usage within the `params` section of a module and can only be used with parameters with the `@secure()` decorator.
157157

158-
The following Bicep file creates an Azure SQL server. The `adminPassword` parameter has a `@secure()` decorator.
158+
Another function called `az.getSecret()` function can be used in Bicep parameter files to retrieve key vault secrets. For more information, see [Retrieve secrets in parameters file](#retrieve-secrets-in-parameters-file).
159+
160+
Because the `getSecret` function can only be used in the `params` section of a module. Let's create a *sql.bicep* in the same directory as the *main.bicep* file with the following content:
159161

160162
```bicep
161163
param sqlServerName string
164+
param location string = resourceGroup().location
162165
param adminLogin string
163166

164167
@secure()
165168
param adminPassword string
166169

167-
resource sqlServer 'Microsoft.Sql/servers@2020-11-01-preview' = {
170+
resource sqlServer 'Microsoft.Sql/servers@2023-08-01-preview' = {
168171
name: sqlServerName
169-
location: resourceGroup().location
172+
location: location
170173
properties: {
171174
administratorLogin: adminLogin
172175
administratorLoginPassword: adminPassword
@@ -175,9 +178,9 @@ resource sqlServer 'Microsoft.Sql/servers@2020-11-01-preview' = {
175178
}
176179
```
177180

178-
Let's use the preceding Bicep file as a module given the file name is *sql.bicep* in the same directory as the main Bicep file.
181+
Notice in the preceding Bicep file, the `adminPassword` parameter has a `@secure()` decorator.
179182

180-
The following Bicep file consumes the sql.bicep as a module. The Bicep file references an existing key vault, and calls the `getSecret` function to retrieve the key vault secret, and then passes the value as a parameter to the module.
183+
The following Bicep file consumes the *sql.bicep* as a module. The Bicep file references an existing key vault, and calls the `getSecret` function to retrieve the key vault secret, and then passes the value as a parameter to the module.
181184

182185
```bicep
183186
param sqlServerName string
@@ -187,7 +190,7 @@ param subscriptionId string
187190
param kvResourceGroup string
188191
param kvName string
189192
190-
resource kv 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
193+
resource kv 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
191194
name: kvName
192195
scope: resourceGroup(subscriptionId, kvResourceGroup )
193196
}
@@ -202,36 +205,21 @@ module sql './sql.bicep' = {
202205
}
203206
```
204207

205-
Also, `getSecret` function (or with the namespace qualifier `az.getSecret`) can be used in a `.bicepparam` file to retrieve the value of a secret from a key vault.
206-
207-
```bicep
208-
using './main.bicep'
209-
210-
param secureUserName = getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretUserName', 'exampleSecretVersion')
211-
param securePassword = az.getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretPassword')
212-
```
213-
214-
## Reference secrets in parameters file
208+
## Retrieve secrets in parameters file
215209

216-
If you don't want to use a module, you can reference the key vault directly in the parameters file. The following image shows how the parameters file references the secret and passes that value to the Bicep file.
217-
218-
![Resource Manager key vault integration diagram](./media/key-vault-parameter/statickeyvault.png)
219-
220-
> [!NOTE]
221-
> Currently you can only reference the key vault in JSON parameters files. You can't reference key vault in Bicep parameters file.
210+
If you don't want to use a module, you can retrieve key vault secrets in parameters file. However, the approach varies depending on whether you're using a JSON parameter file or a Bicep parameter file.
222211

223212
The following Bicep file deploys a SQL server that includes an administrator password. The password parameter is set to a secure string. But the Bicep doesn't specify where that value comes from.
224213

225214
```bicep
215+
param sqlServerName string
226216
param location string = resourceGroup().location
227217
param adminLogin string
228218
229219
@secure()
230220
param adminPassword string
231221
232-
param sqlServerName string
233-
234-
resource sqlServer 'Microsoft.Sql/servers@2022-11-01-preview' = {
222+
resource sqlServer 'Microsoft.Sql/servers@2023-08-01-preview' = {
235223
name: sqlServerName
236224
location: location
237225
properties: {
@@ -242,9 +230,23 @@ resource sqlServer 'Microsoft.Sql/servers@2022-11-01-preview' = {
242230
}
243231
```
244232

245-
---
233+
Now, create a parameters file for the preceding Bicep file.
234+
235+
### Bicep parameter file
236+
237+
[`az.getSecret`](./bicep-functions-parameters-file.md#getsecret) function can be used in a `.bicepparam` file to retrieve the value of a secret from a key vault.
238+
239+
```bicep
240+
using './main.bicep'
241+
242+
param sqlServerName = '<your-server-name>'
243+
param adminLogin = '<your-admin-login>'
244+
param adminPassword = az.getSecret('<subscription-id>', '<rg-name>', '<key-vault-name>', '<secret-name>', '<secret-version>')
245+
```
246+
247+
### JSON parameter file
246248

247-
Now, create a parameters file for the preceding Bicep file. In the parameters file, specify a parameter that matches the name of the parameter in the Bicep file. For the parameter value, reference the secret from the key vault. You reference the secret by passing the resource identifier of the key vault and the name of the secret:
249+
In the JSON parameters file, specify a parameter that matches the name of the parameter in the Bicep file. For the parameter value, reference the secret from the key vault. You reference the secret by passing the resource identifier of the key vault and the name of the secret:
248250

249251
In the following parameters file, the key vault secret must already exist, and you provide a static value for its resource ID.
250252

@@ -254,12 +256,12 @@ In the following parameters file, the key vault secret must already exist, and y
254256
"contentVersion": "1.0.0.0",
255257
"parameters": {
256258
"adminLogin": {
257-
"value": "exampleadmin"
259+
"value": "<your-admin-login>"
258260
},
259261
"adminPassword": {
260262
"reference": {
261263
"keyVault": {
262-
"id": "/subscriptions/<subscription-id>/resourceGroups/<rg-name>/providers/Microsoft.KeyVault/vaults/<vault-name>"
264+
"id": "/subscriptions/<subscription-id>/resourceGroups/<rg-name>/providers/Microsoft.KeyVault/vaults/<key-vault-name>"
263265
},
264266
"secretName": "ExamplePassword"
265267
}
@@ -278,30 +280,6 @@ If you need to use a version of the secret other than the current version, inclu
278280
"secretVersion": "cd91b2b7e10e492ebb870a6ee0591b68"
279281
```
280282

281-
Deploy the template and pass in the parameters file:
282-
283-
# [Azure CLI](#tab/azure-cli)
284-
285-
```azurecli-interactive
286-
az group create --name SqlGroup --location westus2
287-
az deployment group create \
288-
--resource-group SqlGroup \
289-
--template-file <Bicep-file> \
290-
--parameters <parameters-file>
291-
```
292-
293-
# [PowerShell](#tab/azure-powershell)
294-
295-
```azurepowershell-interactive
296-
New-AzResourceGroup -Name $resourceGroupName -Location $location
297-
New-AzResourceGroupDeployment `
298-
-ResourceGroupName $resourceGroupName `
299-
-TemplateFile <Bicep-file> `
300-
-TemplateParameterFile <parameters-file>
301-
```
302-
303-
---
304-
305283
## Next steps
306284

307285
- For general information about key vaults, see [What is Azure Key Vault?](../../key-vault/general/overview.md)

0 commit comments

Comments
 (0)