Skip to content

Commit c440da7

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

5 files changed

Lines changed: 85 additions & 18 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,10 @@ 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+
- Must be `null` when `store_secret_in_key_vault` is `false`.
603606

604607
Type: `string`
605608

@@ -810,7 +813,10 @@ Default: `null`
810813

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

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

815821
Type: `bool`
816822

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ 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+
# Unset default, set in variables.auto.tfvars
43+
key_vault_id = null
4244
}
4345

4446
assert {
@@ -106,6 +108,8 @@ run "should_input_admin_password_output_same_value_on_linux" {
106108
image = "Ubuntu2204"
107109
operating_system = "Linux"
108110
store_secret_in_key_vault = false
111+
# Unset default, set in variables.auto.tfvars
112+
key_vault_id = null
109113
}
110114

111115
assert {
@@ -153,6 +157,8 @@ run "should_input_admin_ssh_public_key_output_same_value_on_linux" {
153157
image = "Ubuntu2204"
154158
operating_system = "Linux"
155159
store_secret_in_key_vault = false
160+
# Unset default, set in variables.auto.tfvars
161+
key_vault_id = null
156162
}
157163

158164
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: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,18 +454,20 @@ 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+
- Must be `null` when `store_secret_in_key_vault` is `false`.
462+
EOT
460463

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-
# }
464+
default = null
465+
type = string
466+
467+
validation {
468+
condition = var.store_secret_in_key_vault ? var.key_vault_id != null : var.key_vault_id == null
469+
error_message = "When store_secret_in_key_vault is true, key_vault_id must be set. When false, it must be null."
470+
}
469471
}
470472

471473
variable "license_type" {
@@ -691,9 +693,15 @@ variable "source_image_id" {
691693
}
692694

693695
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
696+
description = <<-EOT
697+
Whether to store generated secrets (admin password or SSH private key) in an Azure Key Vault.
698+
699+
- If `true`, you must provide `key_vault_id`.
700+
- If `false`, `key_vault_id` must be unset and no secrets will be stored.
701+
EOT
702+
703+
type = bool
704+
default = true
697705
}
698706

699707
variable "subnet_id" {

0 commit comments

Comments
 (0)