Skip to content

Commit 8b2b3de

Browse files
authored
fix trivy issues: NSG, key vault expiry (#96)
Signed-off-by: Andre Licht <al@cloudeteer.de>
1 parent 9e02432 commit 8b2b3de

16 files changed

Lines changed: 191 additions & 13 deletions

.trivyignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# AVD-AZU-0068 / AZU-0068
2+
# Module-managed NICs are always associated with an NSG in r-network.tf.
3+
# This generic module also supports externally supplied NIC IDs, which are consumer-managed.
4+
AZU-0068

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ As documented on the [image](#input_image) input variable, it is possible to use
2323
This example demonstrates the usage of the virtual machine module with default settings. It sets up all necessary dependencies, including a resource group, virtual network, subnet, recovery services vault, backup policy, and key vault, to ensure seamless deployment.
2424

2525
> [!TIP]
26-
> Our module enables password-based login for Linux virtual machines, configurable via the `authentication_type` input variable. If the [`disable_password_authentication`](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine.html#disable_password_authentication-1) setting on the [`azurerm_linux_virtual_machine`](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) resource within this module is not explicitly set to `false`, [Trivy](https://trivy.dev) will flag a high-severity warning. To suppress this warning, add the comment `#trivy:ignore:avd-azu-0039` directly above the Terraform module definition, as shown in the example below.
26+
> Our module intentionally keeps password-based login available for Linux virtual machines through the `authentication_type` input variable. Where your security policy requires SSH-only access, set `authentication_type = "SSH"`. When you intentionally allow Linux password authentication and need to suppress the corresponding [Trivy](https://trivy.dev) warning, add the comment `#trivy:ignore:AVD-AZU-0039` directly above the Terraform module definition, as shown in the example below.
2727
2828
```hcl
2929
resource "azurerm_resource_group" "example" {
@@ -94,7 +94,9 @@ resource "azurerm_key_vault" "example" {
9494
}
9595
}
9696
97-
#trivy:ignore:avd-azu-0039
97+
# Linux password authentication remains an explicit module feature controlled by authentication_type.
98+
# Prefer SSH-only authentication where organizational policy requires it.
99+
#trivy:ignore:AVD-AZU-0039
98100
module "example" {
99101
source = "cloudeteer/vm/azurerm"
100102
@@ -134,6 +136,8 @@ The following resources are used by this module:
134136
- [azurerm_linux_virtual_machine.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) (resource)
135137
- [azurerm_managed_disk.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk) (resource)
136138
- [azurerm_network_interface.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) (resource)
139+
- [azurerm_network_interface_security_group_association.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_security_group_association) (resource)
140+
- [azurerm_network_security_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group) (resource)
137141
- [azurerm_public_ip.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) (resource)
138142
- [azurerm_role_assignment.entra_id_login_admin](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) (resource)
139143
- [azurerm_role_assignment.entra_id_login_user](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) (resource)
@@ -610,6 +614,17 @@ Type: `string`
610614

611615
Default: `null`
612616

617+
### <a name="input_key_vault_secret_expiration_date"></a> [key\_vault\_secret\_expiration\_date](#input\_key\_vault\_secret\_expiration\_date)
618+
619+
Description: The RFC3339 timestamp to assign as `expiration_date` on generated Azure Key Vault secrets.
620+
621+
- Applies only when `store_secret_in_key_vault` is `true`.
622+
- Leave as `null` to keep generated secrets without an explicit expiry date.
623+
624+
Type: `string`
625+
626+
Default: `null`
627+
613628
### <a name="input_license_type"></a> [license\_type](#input\_license\_type)
614629

615630
Description: Specifies the license type to be used for this Virtual Machine.
@@ -631,6 +646,17 @@ Type: `list(string)`
631646

632647
Default: `null`
633648

649+
### <a name="input_network_security_group_id"></a> [network\_security\_group\_id](#input\_network\_security\_group\_id)
650+
651+
Description: The resource ID of an existing Azure Network Security Group to associate with the network interface created by this module.
652+
653+
- Applies only when `create_network_interface` is `true`.
654+
- If omitted and `create_network_interface` is `true`, this module creates and associates a default Network Security Group.
655+
656+
Type: `string`
657+
658+
Default: `null`
659+
634660
### <a name="input_operating_system"></a> [operating\_system](#input\_operating\_system)
635661

636662
Description: The virtual machine's operating system. Valid values are `Linux` or `Windows`. The default is `null`, which determines the operating system to use based on the virtual machine image offering.

examples/domain_join/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ data "azurerm_key_vault_secret" "domain_join_user" {
8686
}
8787

8888

89-
#trivy:ignore:avd-azu-0039
89+
# Linux password authentication remains an explicit module feature controlled by authentication_type.
90+
# Prefer SSH-only authentication where organizational policy requires it.
91+
#trivy:ignore:AVD-AZU-0039
9092
module "example" {
9193
source = "cloudeteer/vm/azurerm"
9294

examples/external_key_vault/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ variable "location" {
2727
type = string
2828
}
2929

30-
#trivy:ignore:avd-azu-0039
30+
# Linux password authentication remains an explicit module feature controlled by authentication_type.
31+
# Prefer SSH-only authentication where organizational policy requires it.
32+
#trivy:ignore:AVD-AZU-0039
3133
module "example" {
3234
source = "cloudeteer/vm/azurerm"
3335

examples/usage/main.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
This example demonstrates the usage of the virtual machine module with default settings. It sets up all necessary dependencies, including a resource group, virtual network, subnet, recovery services vault, backup policy, and key vault, to ensure seamless deployment.
22

33
> [!TIP]
4-
> Our module enables password-based login for Linux virtual machines, configurable via the `authentication_type` input variable. If the [`disable_password_authentication`](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine.html#disable_password_authentication-1) setting on the [`azurerm_linux_virtual_machine`](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) resource within this module is not explicitly set to `false`, [Trivy](https://trivy.dev) will flag a high-severity warning. To suppress this warning, add the comment `#trivy:ignore:avd-azu-0039` directly above the Terraform module definition, as shown in the example below.
4+
> Our module intentionally keeps password-based login available for Linux virtual machines through the `authentication_type` input variable. Where your security policy requires SSH-only access, set `authentication_type = "SSH"`. When you intentionally allow Linux password authentication and need to suppress the corresponding [Trivy](https://trivy.dev) warning, add the comment `#trivy:ignore:AVD-AZU-0039` directly above the Terraform module definition, as shown in the example below.

examples/usage/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ resource "azurerm_key_vault" "example" {
6666
}
6767
}
6868

69-
#trivy:ignore:avd-azu-0039
69+
# Linux password authentication remains an explicit module feature controlled by authentication_type.
70+
# Prefer SSH-only authentication where organizational policy requires it.
71+
#trivy:ignore:AVD-AZU-0039
7072
module "example" {
7173
source = "cloudeteer/vm/azurerm"
7274

r-authentication.tf

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ resource "tls_private_key" "this" {
2727
rsa_bits = var.admin_ssh_key_algorithm == "RSA" ? 4096 : null
2828
}
2929

30-
#trivy:ignore:avd-azu-0017
31-
#trivy:ignore:avd-azu-0013
30+
# Generated secrets may intentionally remain non-expiring when rotation is managed externally.
31+
# Set var.key_vault_secret_expiration_date to enforce an explicit expiry date on generated secrets.
32+
#trivy:ignore:AVD-AZU-0017
33+
#trivy:ignore:AVD-AZU-0013
3234
resource "azurerm_key_vault_secret" "this" {
3335
for_each = toset([
3436
for element in split(", ", var.authentication_type) : element if var.store_secret_in_key_vault
3537
])
3638

37-
name = "${var.name}-${var.admin_username}-${lower(each.key)}"
38-
content_type = var.authentication_type
39-
key_vault_id = var.key_vault_id
40-
value = coalesce(local.admin_password, local.admin_ssh_private_key)
39+
name = "${var.name}-${var.admin_username}-${lower(each.key)}"
40+
content_type = var.authentication_type
41+
expiration_date = var.key_vault_secret_expiration_date
42+
key_vault_id = var.key_vault_id
43+
value = coalesce(local.admin_password, local.admin_ssh_private_key)
4144
}

r-network.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,22 @@ locals {
33
azurerm_network_interface.this[*].id,
44
(var.network_interface_ids != null ? var.network_interface_ids : [])
55
)
6+
network_security_group_id = (
7+
var.network_security_group_id != null ?
8+
var.network_security_group_id :
9+
try(one(azurerm_network_security_group.this[*].id), null)
10+
)
11+
12+
}
613

14+
resource "azurerm_network_security_group" "this" {
15+
16+
count = var.create_network_interface && var.network_security_group_id == null ? 1 : 0
17+
18+
name = "nsg-${trimprefix(var.name, "vm-")}"
19+
location = var.location
20+
resource_group_name = var.resource_group_name
21+
tags = var.tags
722
}
823

924
resource "azurerm_network_interface" "this" {
@@ -26,6 +41,13 @@ resource "azurerm_network_interface" "this" {
2641
}
2742
}
2843

44+
resource "azurerm_network_interface_security_group_association" "this" {
45+
count = var.create_network_interface ? 1 : 0
46+
47+
network_interface_id = azurerm_network_interface.this[0].id
48+
network_security_group_id = local.network_security_group_id
49+
}
50+
2951
resource "azurerm_public_ip" "this" {
3052
count = var.create_public_ip_address ? 1 : 0
3153

r-vm.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ locals {
99
)
1010
}
1111

12-
# trivy:ignore:avd-azu-0039
12+
# Module-managed NICs are associated with an NSG in r-network.tf; externally supplied NICs remain consumer-managed.
13+
#trivy:ignore:AVD-AZU-0068
14+
# Linux password authentication remains an explicit module feature controlled by var.authentication_type.
15+
# Prefer SSH-only authentication where organizational policy requires it.
16+
#trivy:ignore:AVD-AZU-0039
1317
resource "azurerm_linux_virtual_machine" "this" {
1418
count = local.is_linux ? 1 : 0
1519

@@ -113,6 +117,8 @@ resource "azurerm_linux_virtual_machine" "this" {
113117
}
114118

115119
resource "azurerm_windows_virtual_machine" "this" {
120+
# Module-managed NICs are associated with an NSG in r-network.tf; externally supplied NICs remain consumer-managed.
121+
#trivy:ignore:AVD-AZU-0068
116122
count = local.is_windows ? 1 : 0
117123

118124
name = var.name
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mock_resource "azurerm_network_interface_security_group_association" {
2+
defaults = {
3+
id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/RG-MOCK/providers/Microsoft.Network/networkInterfaces/NIC-MOCK|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/RG-MOCK/providers/Microsoft.Network/networkSecurityGroups/NSG-MOCK"
4+
}
5+
}

0 commit comments

Comments
 (0)