Skip to content

Commit 1f469e0

Browse files
committed
Add Key Vault ID as a tag to virtual machines and enhance variable descriptions
Signed-off-by: Andre Licht <al@cloudeteer.de>
1 parent ea4250d commit 1f469e0

5 files changed

Lines changed: 86 additions & 18 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,11 @@ Default: `null`
599599

600600
### <a name="input_key_vault_id"></a> [key\_vault\_id](#input\_key\_vault\_id)
601601

602-
Description: Key Vault ID to store the generated admin password or admin SSH private key. Required when admin\_password or admin\_ssh\_public\_key is not set. Must not be set if either admin\_password or admin\_ssh\_public\_key is set.
602+
Description: The resource ID of the Azure Key Vault where the generated admin password or SSH private key should be stored as a secret.
603+
604+
Required when `store_secret_in_key_vault` is `true`.
605+
606+
Must be `null` when `store_secret_in_key_vault` is `false`.
603607

604608
Type: `string`
605609

@@ -810,7 +814,11 @@ Default: `null`
810814

811815
### <a name="input_store_secret_in_key_vault"></a> [store\_secret\_in\_key\_vault](#input\_store\_secret\_in\_key\_vault)
812816

813-
Description: If set to `true`, the secrets generated by this module will be stored in the Key Vault specified by `key_vault_id`.
817+
Description: Whether to store generated secrets (admin password or SSH private key) in an Azure Key Vault.
818+
819+
- If `true`, you must provide `key_vault_id`.
820+
821+
- If `false`, `key_vault_id` must be null and no secrets will be stored.
814822

815823
Type: `bool`
816824

r-vm.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ locals {
22
virtual_machine = (local.is_linux ? azurerm_linux_virtual_machine.this[0] :
33
(local.is_windows ? azurerm_windows_virtual_machine.this[0] : null)
44
)
5+
tags_virtual_machine = merge(
6+
var.tags,
7+
var.tags_virtual_machine,
8+
(var.key_vault_id != null ? { key_vault_id = var.key_vault_id } : {})
9+
)
510
}
611

712
# trivy:ignore:avd-azu-0039
@@ -11,7 +16,7 @@ resource "azurerm_linux_virtual_machine" "this" {
1116
name = var.name
1217
location = var.location
1318
resource_group_name = var.resource_group_name
14-
tags = merge(var.tags, var.tags_virtual_machine)
19+
tags = local.tags_virtual_machine
1520

1621
admin_password = local.admin_password
1722
admin_username = var.admin_username
@@ -113,7 +118,7 @@ resource "azurerm_windows_virtual_machine" "this" {
113118
name = var.name
114119
location = var.location
115120
resource_group_name = var.resource_group_name
116-
tags = merge(var.tags, var.tags_virtual_machine)
121+
tags = local.tags_virtual_machine
117122

118123
admin_password = local.admin_password
119124
admin_username = var.admin_username

tests/local/input_authentication.tftest.hcl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ run "should_input_admin_password_output_same_value_on_windows" {
3939
authentication_type = "Password"
4040
image = "Win2022Datacenter"
4141
store_secret_in_key_vault = false
42+
key_vault_id = null
4243
}
4344

4445
assert {
@@ -106,6 +107,7 @@ run "should_input_admin_password_output_same_value_on_linux" {
106107
image = "Ubuntu2204"
107108
operating_system = "Linux"
108109
store_secret_in_key_vault = false
110+
key_vault_id = null
109111
}
110112

111113
assert {
@@ -153,6 +155,7 @@ run "should_input_admin_ssh_public_key_output_same_value_on_linux" {
153155
image = "Ubuntu2204"
154156
operating_system = "Linux"
155157
store_secret_in_key_vault = false
158+
key_vault_id = null
156159
}
157160

158161
assert {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
mock_provider "azapi" { source = "tests/local/mocks" }
2+
mock_provider "azurerm" { source = "tests/local/mocks" }
3+
mock_provider "random" { source = "tests/local/mocks" }
4+
mock_provider "tls" { source = "tests/local/mocks" }
5+
6+
run "should_not_add_key_vault_id_as_tag_to_virtual_machine" {
7+
command = plan
8+
9+
variables {
10+
image = "Ubuntu2204"
11+
operating_system = "Linux"
12+
store_secret_in_key_vault = false
13+
admin_password = bcrypt(uuid())
14+
tags = {
15+
"var_tags" = "var_tag"
16+
}
17+
tags_virtual_machine = {
18+
"var_vm_tags" = "var_vm_tag"
19+
}
20+
key_vault_id = null
21+
}
22+
23+
assert {
24+
condition = contains(keys(azurerm_linux_virtual_machine.this[0].tags), "key_vault_id") == false
25+
error_message = "Tag key_vault_id should not exist on the virtual machine"
26+
}
27+
}
28+
29+
run "should_add_key_vault_id_as_tag_to_virtual_machine" {
30+
command = plan
31+
32+
variables {
33+
image = "Ubuntu2204"
34+
operating_system = "Linux"
35+
key_vault_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.KeyVault/vaults/localvault"
36+
}
37+
38+
assert {
39+
condition = azurerm_linux_virtual_machine.this[0].tags["key_vault_id"] == "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.KeyVault/vaults/localvault"
40+
error_message = "Expected to add key vault id as tag to virtual machine"
41+
}
42+
}

variables.tf

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,18 +454,21 @@ variable "image" {
454454
}
455455

456456
variable "key_vault_id" {
457-
description = "Key Vault ID to store the generated admin password or admin SSH private key. Required when admin_password or admin_ssh_public_key is not set. Must not be set if either admin_password or admin_ssh_public_key is set."
458-
default = null
459-
type = string
457+
description = <<-EOT
458+
The resource ID of the Azure Key Vault where the generated admin password or SSH private key should be stored as a secret.
459+
460+
Required when `store_secret_in_key_vault` is `true`.
461+
462+
Must be `null` when `store_secret_in_key_vault` is `false`.
463+
EOT
460464

461-
# validation {
462-
# condition = var.key_vault_id == null ? (
463-
# (var.authentication_type == "Password" && var.admin_password != null) || (var.authentication_type == "SSH" && var.admin_ssh_public_key != null)
464-
# ) : (
465-
# (var.authentication_type == "Password" && var.admin_password == null) || (var.authentication_type == "SSH" && var.admin_ssh_public_key == null)
466-
# )
467-
# error_message = "Invalid combination of key_vault_id, admin_password, and admin_ssh_public_key. If key_vault_id is null, admin_password or admin_ssh_public_key must be non-null. If key_vault_id is not null, admin_password and admin_ssh_public_key must be null."
468-
# }
465+
default = null
466+
type = string
467+
468+
validation {
469+
condition = var.store_secret_in_key_vault ? var.key_vault_id != null : var.key_vault_id == null
470+
error_message = "When store_secret_in_key_vault is true, key_vault_id must be set. When false, it must be null."
471+
}
469472
}
470473

471474
variable "license_type" {
@@ -691,9 +694,16 @@ variable "source_image_id" {
691694
}
692695

693696
variable "store_secret_in_key_vault" {
694-
description = "If set to `true`, the secrets generated by this module will be stored in the Key Vault specified by `key_vault_id`."
695-
type = bool
696-
default = true
697+
description = <<-EOT
698+
Whether to store generated secrets (admin password or SSH private key) in an Azure Key Vault.
699+
700+
- If `true`, you must provide `key_vault_id`.
701+
702+
- If `false`, `key_vault_id` must be null and no secrets will be stored.
703+
EOT
704+
705+
type = bool
706+
default = true
697707
}
698708

699709
variable "subnet_id" {

0 commit comments

Comments
 (0)