Skip to content

Commit e93279a

Browse files
authored
Add examples for getting secrets for VMs (#56)
1 parent c48c781 commit e93279a

7 files changed

Lines changed: 770 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Inspect the following files for instructions to test PSRule for Azure rules by c
4545
- [deployments/contoso/landing-zones/subscription-1/rg-app-001/dev.bicepparam](deployments/contoso/landing-zones/subscription-1/rg-app-001/dev.bicepparam)
4646
- [deployments/contoso/landing-zones/subscription-1/rg-app-002/deploy.bicep](deployments/contoso/landing-zones/subscription-1/rg-app-002/deploy.bicep)
4747

48+
For examples of how to reference resources that require secrets to be passed in, see:
49+
50+
- [deployments/contoso/landing-zones/subscription-1/rg-vm-001/deploy.bicep](deployments/contoso/landing-zones/subscription-1/rg-vm-001/deploy.bicep)
51+
4852
## Support
4953

5054
This project uses GitHub Issues to track bugs and feature requests.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
// Note:
5+
// This Azure Bicep code demonstrates a deployment of a VM that uses a password with two common
6+
// deployment options. 1. Using a password from the pipeline. 2. Using a password from a Key Vault secret.
7+
8+
// ---------------------------------------------------------------
9+
// OPTION 1: A VM deployment using the password from the pipeline.
10+
// ---------------------------------------------------------------
11+
12+
// If your pipeline passes a password in as a parameter to the deployment script, use this option.
13+
// For expansion with PSRule, a dummy value for `adminPassword` is used set in `ps-rule.yaml` with
14+
// the `AZURE_PARAMETER_DEFAULTS` configuration option. This allows PSRule to expand the deployment,
15+
// without exposing your secret in the code or PSRule.
16+
17+
@secure()
18+
@description('Load the admin password from the pipeline.')
19+
param adminPassword string
20+
21+
@description('A VM deployment using a password from the pipeline.')
22+
module vm001 '../../../../../modules/virtual-machine-windows/v1/main.bicep' = {
23+
params: {
24+
name: 'vm-001'
25+
adminPassword: adminPassword
26+
adminUsername: 'vm-admin'
27+
imageSKU: '2022-Datacenter'
28+
size: 'Standard_D4ds_v4'
29+
subnetId: vnet.id
30+
tags: {
31+
env: 'dev'
32+
}
33+
}
34+
}
35+
36+
// ---------------------------------------------------------------------
37+
// OPTION 2: A VM deployment using the password from a Key Vault secret.
38+
// ---------------------------------------------------------------------
39+
40+
// If your VM deployment is able to use a Key Vault secret that is already deployed to Azure, use this option.
41+
// When you reference a Key Vault secret, PSRule will automatically substitute a placeholder for the secret value
42+
// during expansion. So you can use the secret in your deployment without exposing it as a deployment parameter.
43+
44+
// NB: PSRule never actually attempts to retrieve the secret value, so it does not need access to the secret.
45+
46+
@description('An existing Key Vault to use for the VM deployment.')
47+
resource vault 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
48+
name: 'kv-001'
49+
}
50+
51+
@description('Load the admin password from a Key Vault secret.')
52+
module vm002 '../../../../../modules/virtual-machine-windows/v1/main.bicep' = {
53+
params: {
54+
name: 'vm-002'
55+
adminPassword: vault.getSecret('vm002-admin-password')
56+
adminUsername: 'vm-admin'
57+
imageSKU: '2022-Datacenter'
58+
size: 'Standard_D4ds_v4'
59+
subnetId: vnet.id
60+
tags: {
61+
env: 'dev'
62+
}
63+
}
64+
}
65+
66+
// ---------------
67+
// Other resources
68+
// ---------------
69+
70+
// An existing virtual network and subnet to connect the VM.
71+
resource vnet 'Microsoft.Network/virtualNetworks/subnets@2023-05-01' existing = {
72+
name: 'vnet-001/subnet-001'
73+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
// Configure role assignments for the Virtual Machine
5+
6+
// ----------
7+
// PARAMETERS
8+
// ----------
9+
10+
@sys.description('The display name of the role to assign or the GUID.')
11+
param role string
12+
13+
@sys.description('The GUID of the identity object to assign.')
14+
param principalId string
15+
16+
@sys.description('A description of the assignment.')
17+
param description string = ''
18+
19+
@allowed([
20+
'ServicePrincipal'
21+
'Group'
22+
'User'
23+
'ForeignGroup'
24+
'Device'
25+
])
26+
@sys.description('The principal type to assign.')
27+
param principalType string = 'ServicePrincipal'
28+
29+
@sys.description('The name of the Virtual Machine name.')
30+
param resource string
31+
32+
// ---------
33+
// VARIABLES
34+
// ---------
35+
36+
// Map of common RBAC role names to their IDs.
37+
// Azure uses specific GUIDs for built-in roles however it is easier to reference them by name.
38+
var roles = {
39+
Owner: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635')
40+
Contributor: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c')
41+
Reader: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')
42+
'User Access Administrator': subscriptionResourceId(
43+
'Microsoft.Authorization/roleDefinitions',
44+
'18d7d88d-d35e-4fb5-a5c3-7773c20a72d9'
45+
)
46+
'Virtual Machine Contributor': subscriptionResourceId(
47+
'Microsoft.Authorization/roleDefinitions',
48+
'9980e02c-c2be-4d73-94e8-173b1dc7cf3c'
49+
)
50+
}
51+
52+
var roleDefinitionId = roles[?role] ?? subscriptionResourceId('Microsoft.Authorization/roleDefinitions', role)
53+
54+
// ---------
55+
// RESOURCES
56+
// ---------
57+
58+
resource vm 'Microsoft.Compute/virtualMachines@2023-03-01' existing = {
59+
name: resource
60+
}
61+
62+
@sys.description('Assign permissions to an Azure AD principal.')
63+
resource rbac 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
64+
name: guid(vm.id, principalId, roleDefinitionId)
65+
scope: vm
66+
properties: {
67+
principalId: principalId
68+
roleDefinitionId: roleDefinitionId
69+
principalType: principalType
70+
description: description
71+
}
72+
}

0 commit comments

Comments
 (0)