-
Notifications
You must be signed in to change notification settings - Fork 30
Add examples for getting secrets for VMs #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BernieWhite
wants to merge
11
commits into
Azure:main
Choose a base branch
from
BernieWhite:bewhite/add-keyvault-secret-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3d67dc6
Add examples for getting secrets for VMs
BernieWhite e2c7c94
Update readme
BernieWhite 71053ab
Merge branch 'main' into bewhite/add-keyvault-secret-example
BernieWhite f79ee30
Merge branch 'main' into bewhite/add-keyvault-secret-example
BernieWhite 119821b
Fixes for CI
BernieWhite 20f5114
Merge branch 'bewhite/add-keyvault-secret-example' of https://github.…
BernieWhite f8924d1
Merge branch 'main' into bewhite/add-keyvault-secret-example
BernieWhite 5675325
Merge branch 'main' into bewhite/add-keyvault-secret-example
BernieWhite c367088
Merge branch 'main' into bewhite/add-keyvault-secret-example
BernieWhite b531b8d
Merge branch 'main' into bewhite/add-keyvault-secret-example
BernieWhite 6be2e26
Merge branch 'main' into bewhite/add-keyvault-secret-example
BernieWhite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
deployments/contoso/landing-zones/subscription-1/rg-vm-001/deploy.bicep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // Note: | ||
| // This Azure Bicep code demonstrates a deployment of a VM that uses a password with two common | ||
| // deployment options. 1. Using a password from the pipeline. 2. Using a password from a Key Vault secret. | ||
|
|
||
| // --------------------------------------------------------------- | ||
| // OPTION 1: A VM deployment using the password from the pipeline. | ||
| // --------------------------------------------------------------- | ||
|
|
||
| // If your pipeline passes a password in as a parameter to the deployment script, use this option. | ||
| // For expansion with PSRule, a dummy value for `adminPassword` is used set in `ps-rule.yaml` with | ||
| // the `AZURE_PARAMETER_DEFAULTS` configuration option. This allows PSRule to expand the deployment, | ||
| // without exposing your secret in the code or PSRule. | ||
|
|
||
| @secure() | ||
| @description('Load the admin password from the pipeline.') | ||
| param adminPassword string | ||
|
|
||
| @description('A VM deployment using a password from the pipeline.') | ||
| module vm001 '../../../../../modules/virtual-machine-windows/v1/main.bicep' = { | ||
| params: { | ||
| name: 'vm-001' | ||
| adminPassword: adminPassword | ||
| adminUsername: 'vm-admin' | ||
| imageSKU: '2022-Datacenter' | ||
| size: 'Standard_D4ds_v4' | ||
| subnetId: vnet.id | ||
| tags: { | ||
| env: 'dev' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------- | ||
| // OPTION 2: A VM deployment using the password from a Key Vault secret. | ||
| // --------------------------------------------------------------------- | ||
|
|
||
| // If your VM deployment is able to use a Key Vault secret that is already deployed to Azure, use this option. | ||
| // When you reference a Key Vault secret, PSRule will automatically substitute a placeholder for the secret value | ||
| // during expansion. So you can use the secret in your deployment without exposing it as a deployment parameter. | ||
|
|
||
| // NB: PSRule never actually attempts to retrieve the secret value, so it does not need access to the secret. | ||
|
|
||
| @description('An existing Key Vault to use for the VM deployment.') | ||
| resource vault 'Microsoft.KeyVault/vaults@2023-07-01' existing = { | ||
| name: 'kv-001' | ||
| } | ||
|
|
||
| @description('Load the admin password from a Key Vault secret.') | ||
| module vm002 '../../../../../modules/virtual-machine-windows/v1/main.bicep' = { | ||
| params: { | ||
| name: 'vm-002' | ||
| adminPassword: vault.getSecret('vm002-admin-password') | ||
| adminUsername: 'vm-admin' | ||
| imageSKU: '2022-Datacenter' | ||
| size: 'Standard_D4ds_v4' | ||
| subnetId: vnet.id | ||
| tags: { | ||
| env: 'dev' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // --------------- | ||
| // Other resources | ||
| // --------------- | ||
|
|
||
| // An existing virtual network and subnet to connect the VM. | ||
| resource vnet 'Microsoft.Network/virtualNetworks/subnets@2023-05-01' existing = { | ||
| name: 'vnet-001/subnet-001' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // Configure role assignments for the Virtual Machine | ||
|
|
||
| // ---------- | ||
| // PARAMETERS | ||
| // ---------- | ||
|
|
||
| @sys.description('The display name of the role to assign or the GUID.') | ||
| param role string | ||
|
|
||
| @sys.description('The GUID of the identity object to assign.') | ||
| param principalId string | ||
|
|
||
| @sys.description('A description of the assignment.') | ||
| param description string = '' | ||
|
|
||
| @allowed([ | ||
| 'ServicePrincipal' | ||
| 'Group' | ||
| 'User' | ||
| 'ForeignGroup' | ||
| 'Device' | ||
| ]) | ||
| @sys.description('The principal type to assign.') | ||
| param principalType string = 'ServicePrincipal' | ||
|
|
||
| @sys.description('The name of the Virtual Machine name.') | ||
| param resource string | ||
|
|
||
| // --------- | ||
| // VARIABLES | ||
| // --------- | ||
|
|
||
| var roles = { | ||
| Owner: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635') | ||
| Contributor: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c') | ||
| Reader: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'acdd72a7-3385-48ef-bd42-f606fba81ae7') | ||
| 'User Access Administrator': subscriptionResourceId( | ||
| 'Microsoft.Authorization/roleDefinitions', | ||
| '18d7d88d-d35e-4fb5-a5c3-7773c20a72d9' | ||
| ) | ||
| 'Virtual Machine Contributor': subscriptionResourceId( | ||
| 'Microsoft.Authorization/roleDefinitions', | ||
| '9980e02c-c2be-4d73-94e8-173b1dc7cf3c' | ||
| ) | ||
| } | ||
| var roleDefinitionId = contains(roles, role) | ||
| ? roles[role] | ||
| : subscriptionResourceId('Microsoft.Authorization/roleDefinitions', role) | ||
|
|
||
| // --------- | ||
| // RESOURCES | ||
| // --------- | ||
|
|
||
| resource vm 'Microsoft.Compute/virtualMachines@2023-03-01' existing = { | ||
| name: resource | ||
| } | ||
|
|
||
| @sys.description('Assign permissions to an Azure AD principal.') | ||
| resource rbac 'Microsoft.Authorization/roleAssignments@2022-04-01' = { | ||
| name: guid(vm.id, principalId, roleDefinitionId) | ||
| scope: vm | ||
| properties: { | ||
| principalId: principalId | ||
| roleDefinitionId: roleDefinitionId | ||
| principalType: principalType | ||
| description: description | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Thoughts: Can we add any documentation which contains these relationships and GUID relevance please.