diff --git a/modules/terraform/azure/aks-cli/main.tf b/modules/terraform/azure/aks-cli/main.tf index b4d9b86d7f..1d6a8d637c 100644 --- a/modules/terraform/azure/aks-cli/main.tf +++ b/modules/terraform/azure/aks-cli/main.tf @@ -26,6 +26,16 @@ locals { ) } : null + # Disk Encryption Set for OS disk encryption with Customer-Managed Keys + # Reference: https://learn.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys + disk_encryption_set_id = ( + var.aks_cli_config.disk_encryption_set_name != null ? + try( + var.disk_encryption_sets[var.aks_cli_config.disk_encryption_set_name], + error("Specified disk_encryption_set_name '${var.aks_cli_config.disk_encryption_set_name}' does not exist in Disk Encryption Sets: ${join(", ", keys(var.disk_encryption_sets))}") + ) : null + ) + kubernetes_version = ( var.aks_cli_config.kubernetes_version == null ? "" : @@ -81,6 +91,12 @@ locals { "Key Vault Crypto User" = local.key_management_service.key_vault_id } : {} + # Disk Encryption Set parameters for OS disk encryption with Customer-Managed Keys + disk_encryption_parameters = ( + local.disk_encryption_set_id == null ? + "" : + format("--node-osdisk-diskencryptionset-id %s", local.disk_encryption_set_id) + ) subnet_id_parameter = (local.aks_subnet_id == null ? "" : @@ -151,6 +167,7 @@ locals { local.kubernetes_version, local.optional_parameters, local.kms_parameters, + local.disk_encryption_parameters, local.subnet_id_parameter, local.managed_identity_parameter, local.api_server_vnet_integration_parameter, @@ -277,4 +294,32 @@ resource "azurerm_role_assignment" "aks_identity_kms_roles" { scope = each.value role_definition_name = each.key principal_id = azurerm_user_assigned_identity.userassignedidentity[0].principal_id +} + +# Fetch AKS cluster to get identities for DES role assignments +data "azurerm_kubernetes_cluster" "aks" { + count = var.aks_cli_config.disk_encryption_set_name != null && !var.aks_cli_config.dry_run ? 1 : 0 + + depends_on = [terraform_data.aks_cli] + + name = var.aks_cli_config.aks_name + resource_group_name = var.resource_group_name +} + +# Grant Reader access to Disk Encryption Set for kubelet identity +resource "azurerm_role_assignment" "des_reader_kubelet" { + count = var.aks_cli_config.disk_encryption_set_name != null && !var.aks_cli_config.dry_run ? 1 : 0 + + scope = local.disk_encryption_set_id + role_definition_name = "Reader" + principal_id = data.azurerm_kubernetes_cluster.aks[0].kubelet_identity[0].object_id +} + +# Grant Reader access to Disk Encryption Set for cluster identity +resource "azurerm_role_assignment" "des_reader_cluster" { + count = var.aks_cli_config.disk_encryption_set_name != null && !var.aks_cli_config.dry_run ? 1 : 0 + + scope = local.disk_encryption_set_id + role_definition_name = "Reader" + principal_id = var.aks_cli_config.managed_identity_name != null ? azurerm_user_assigned_identity.userassignedidentity[0].principal_id : data.azurerm_kubernetes_cluster.aks[0].identity[0].principal_id } \ No newline at end of file diff --git a/modules/terraform/azure/aks-cli/variables.tf b/modules/terraform/azure/aks-cli/variables.tf index 20fcf481d3..e1e0652547 100644 --- a/modules/terraform/azure/aks-cli/variables.tf +++ b/modules/terraform/azure/aks-cli/variables.tf @@ -34,6 +34,12 @@ variable "key_vaults" { default = {} } +variable "disk_encryption_sets" { + description = "Map of Disk Encryption Set names to their IDs for OS/data disk encryption. Reference: https://learn.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys" + type = map(string) + default = {} +} + variable "aks_aad_enabled" { description = "Indicates whether Azure Active Directory integration is enabled for AKS" @@ -84,6 +90,8 @@ variable "aks_cli_config" { network_access = optional(string, "Public") }), null) dry_run = optional(bool, false) # If true, only print the command without executing it. Useful for testing. + # Disk Encryption Set configuration for OS disk encryption with Customer-Managed Keys + disk_encryption_set_name = optional(string, null) # Name of the Disk Encryption Set to use for OS disk encryption }) } diff --git a/modules/terraform/azure/aks/main.tf b/modules/terraform/azure/aks/main.tf index c933eeaf99..cb9212a4b7 100644 --- a/modules/terraform/azure/aks/main.tf +++ b/modules/terraform/azure/aks/main.tf @@ -26,6 +26,15 @@ locals { "Key Vault Crypto User" = local.key_management_service.key_vault_id } : {} + + # Disk Encryption Set for OS disk encryption with Customer-Managed Keys + disk_encryption_set_id = ( + var.aks_config.disk_encryption_set_name != null ? + try( + var.disk_encryption_sets[var.aks_config.disk_encryption_set_name], + error("Specified disk_encryption_set_name '${var.aks_config.disk_encryption_set_name}' does not exist in Disk Encryption Sets: ${join(", ", keys(var.disk_encryption_sets))}") + ) : null + ) } data "azurerm_client_config" "current" {} @@ -51,6 +60,9 @@ resource "azurerm_kubernetes_cluster" "aks" { sku_tier = var.aks_config.sku_tier support_plan = var.aks_config.support_plan + # Disk Encryption Set for OS disk encryption with Customer-Managed Keys + disk_encryption_set_id = local.disk_encryption_set_id + # Wait for KMS role assignment to propagate depends_on = [ azurerm_role_assignment.aks_identity_kms_roles @@ -197,4 +209,22 @@ resource "azurerm_role_assignment" "aks_identity_kms_roles" { scope = each.value role_definition_name = each.key principal_id = azurerm_user_assigned_identity.aks_identity[0].principal_id +} + +# Grant Reader access to Disk Encryption Set for kubelet identity +resource "azurerm_role_assignment" "des_reader_kubelet" { + count = var.aks_config.disk_encryption_set_name != null ? 1 : 0 + + scope = local.disk_encryption_set_id + role_definition_name = "Reader" + principal_id = azurerm_kubernetes_cluster.aks.kubelet_identity[0].object_id +} + +# Grant Reader access to Disk Encryption Set for cluster identity +resource "azurerm_role_assignment" "des_reader_cluster" { + count = var.aks_config.disk_encryption_set_name != null ? 1 : 0 + + scope = local.disk_encryption_set_id + role_definition_name = "Reader" + principal_id = local.key_management_service != null ? azurerm_user_assigned_identity.aks_identity[0].principal_id : azurerm_kubernetes_cluster.aks.identity[0].principal_id } \ No newline at end of file diff --git a/modules/terraform/azure/aks/variables.tf b/modules/terraform/azure/aks/variables.tf index afad007492..98765882b5 100644 --- a/modules/terraform/azure/aks/variables.tf +++ b/modules/terraform/azure/aks/variables.tf @@ -72,6 +72,12 @@ variable "key_vaults" { default = {} } +variable "disk_encryption_sets" { + description = "Map of Disk Encryption Set names to their IDs for OS/data disk encryption. Reference: https://learn.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys" + type = map(string) + default = {} +} + variable "aks_config" { type = object({ role = string @@ -163,6 +169,8 @@ variable "aks_config" { key_vault_name = string network_access = optional(string, "Public") }), null) + # Disk Encryption Set configuration for OS disk encryption with Customer-Managed Keys + disk_encryption_set_name = optional(string, null) # Name of the Disk Encryption Set to use for OS disk encryption }) validation { diff --git a/modules/terraform/azure/disk-encryption-set/README.md b/modules/terraform/azure/disk-encryption-set/README.md new file mode 100644 index 0000000000..e73a2bf178 --- /dev/null +++ b/modules/terraform/azure/disk-encryption-set/README.md @@ -0,0 +1,145 @@ +# Disk Encryption Set Module + +This module creates an Azure Disk Encryption Set (DES) for encrypting AKS OS and data disks with Customer-Managed Keys (CMK). + +## Overview + +Azure Disk Encryption Sets allow you to encrypt managed disks using your own encryption keys stored in Azure Key Vault, providing enhanced control over encryption keys for compliance and security requirements. + +**Reference**: [Azure AKS Disk Encryption with Customer-Managed Keys](https://learn.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys) + +## Features + +- Creates Disk Encryption Set with System-Assigned Managed Identity +- Supports multiple encryption types (single CMK, double encryption, confidential VMs) +- Automatic Key Vault RBAC role assignments for key access +- Optional automatic key rotation + +## Usage + +This module is typically invoked from the parent Azure module. It requires a Key Vault with encryption keys to be created first. + +### Example Configuration + +```hcl +# In parent module or tfvars file + +# First, create the Key Vault with encryption key +key_vault_config_list = [ + { + name = "mykeyvault" + keys = [ + { key_name = "disk-encryption-key" } + ] + } +] + +# Then, create the Disk Encryption Set +disk_encryption_set_config_list = [ + { + name = "my-disk-encryption-set" + key_vault_name = "mykeyvault" + key_name = "disk-encryption-key" + encryption_type = "EncryptionAtRestWithCustomerKey" + auto_key_rotation_enabled = false + } +] +``` + +### Using with AKS + +Reference the Disk Encryption Set in your AKS configuration: + +```hcl +aks_cli_config_list = [ + { + role = "cluster" + aks_name = "my-cluster" + sku_tier = "Standard" + disk_encryption_set_name = "my-disk-encryption-set" + # ... other configuration + } +] +``` + +## Input Variables + +| Variable | Type | Required | Default | Description | +|----------|------|----------|---------|-------------| +| `resource_group_name` | string | Yes | - | Name of the resource group | +| `location` | string | Yes | - | Azure region location | +| `tags` | map(string) | No | `{}` | Tags to apply to resources | +| `key_vaults` | map(any) | No | `{}` | Map of Key Vault configurations (passed from parent module) | +| `disk_encryption_set_config` | object | No | `null` | Disk Encryption Set configuration | + +### disk_encryption_set_config Object + +| Attribute | Type | Required | Default | Description | +|-----------|------|----------|---------|-------------| +| `name` | string | Yes | - | Name of the Disk Encryption Set (1-80 characters) | +| `key_vault_name` | string | Yes | - | Name of the Key Vault containing the encryption key | +| `key_name` | string | Yes | - | Name of the encryption key in the Key Vault | +| `encryption_type` | string | No | `"EncryptionAtRestWithCustomerKey"` | Type of encryption | +| `auto_key_rotation_enabled` | bool | No | `false` | Enable automatic key rotation | + +### Encryption Types + +| Type | Description | +|------|-------------| +| `EncryptionAtRestWithCustomerKey` | Disk is encrypted with customer-managed key only (default) | +| `EncryptionAtRestWithPlatformAndCustomerKeys` | Double encryption with both platform and customer-managed keys | +| `ConfidentialVmEncryptedWithCustomerKey` | For confidential VM scenarios | + +## Outputs + +| Output | Description | +|--------|-------------| +| `disk_encryption_set_id` | The resource ID of the Disk Encryption Set (used with `--node-osdisk-diskencryptionset-id` in AKS CLI) | + +## Role Assignments + +The module automatically creates the following RBAC role assignments for the Disk Encryption Set's managed identity: + +| Role | Scope | Purpose | +|------|-------|---------| +| `Key Vault Crypto Service Encryption User` | Key Vault | Access to the Key Vault for cryptographic operations | +| `Key Vault Crypto Service Encryption User` | Key | Wrap/unwrap key operations on the specific encryption key | + +## Dependencies + +This module depends on: +- Key Vault module (must be created first with the required encryption key) +- Resource group must exist + +## Architecture + +``` +┌─────────────────────────┐ +│ Disk Encryption Set │ +│ (System-Assigned MI) │ +└───────────┬─────────────┘ + │ + │ RBAC: Key Vault Crypto + │ Service Encryption User + ▼ +┌─────────────────────────┐ +│ Key Vault │ +│ ┌───────────────────┐ │ +│ │ Encryption Key │ │ +│ └───────────────────┘ │ +└─────────────────────────┘ + │ + │ Used by + ▼ +┌─────────────────────────┐ +│ AKS Cluster │ +│ (OS Disk Encryption) │ +└─────────────────────────┘ +``` + +## Related Documentation + +- [Azure Disk Encryption Set](https://learn.microsoft.com/en-us/azure/virtual-machines/disk-encryption-sets) +- [AKS Customer-Managed Keys](https://learn.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys) +- [Key Vault Module](../key-vault/) +- [AKS CLI Module](../aks-cli/README.md) diff --git a/modules/terraform/azure/disk-encryption-set/main.tf b/modules/terraform/azure/disk-encryption-set/main.tf new file mode 100644 index 0000000000..356224a403 --- /dev/null +++ b/modules/terraform/azure/disk-encryption-set/main.tf @@ -0,0 +1,71 @@ +# Disk Encryption Set for Azure managed disks with Customer-Managed Keys +# Reference: https://learn.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys +resource "azurerm_disk_encryption_set" "des" { + count = var.disk_encryption_set_config != null ? 1 : 0 + + name = var.disk_encryption_set_config.name + location = var.location + resource_group_name = var.resource_group_name + key_vault_key_id = local.key_vault_key_id + encryption_type = var.disk_encryption_set_config.encryption_type + auto_key_rotation_enabled = var.disk_encryption_set_config.auto_key_rotation_enabled + + identity { + type = "SystemAssigned" + } + + tags = var.tags +} + +locals { + # Get the Key Vault key ID from the key_vaults map + key_vault_key_id = var.disk_encryption_set_config != null ? ( + try( + var.key_vaults[var.disk_encryption_set_config.key_vault_name].keys[var.disk_encryption_set_config.key_name].id, + error("Specified key '${var.disk_encryption_set_config.key_name}' does not exist in Key Vault '${var.disk_encryption_set_config.key_vault_name}'") + ) + ) : null + + key_vault_id = var.disk_encryption_set_config != null ? ( + try( + var.key_vaults[var.disk_encryption_set_config.key_vault_name].id, + error("Specified Key Vault '${var.disk_encryption_set_config.key_vault_name}' does not exist in Key Vaults") + ) + ) : null + + key_vault_key_resource_id = var.disk_encryption_set_config != null ? ( + try( + var.key_vaults[var.disk_encryption_set_config.key_vault_name].keys[var.disk_encryption_set_config.key_name].resource_id, + error("Specified key '${var.disk_encryption_set_config.key_name}' resource_id not found in Key Vault '${var.disk_encryption_set_config.key_vault_name}'") + ) + ) : null + +} + +locals { + # Check if both disk_encryption_set_config and required key vault exist + should_create_role_assignments = ( + var.disk_encryption_set_config != null && + length(var.key_vaults) > 0 && + contains(keys(var.key_vaults), var.disk_encryption_set_config.key_vault_name) + ) +} + +# Grant DiskEncryptionSet identity access to Key Vault for wrap/unwrap key operations +# Required for the DES to use the customer-managed key for encryption +resource "azurerm_role_assignment" "des_key_vault_crypto_user" { + count = local.should_create_role_assignments ? 1 : 0 + + scope = local.key_vault_id + role_definition_name = "Key Vault Crypto Service Encryption User" + principal_id = azurerm_disk_encryption_set.des[0].identity[0].principal_id +} + +# Role assignment at the key level for wrap/unwrap operations +resource "azurerm_role_assignment" "des_key_crypto_user" { + count = local.should_create_role_assignments ? 1 : 0 + + scope = local.key_vault_key_resource_id + role_definition_name = "Key Vault Crypto Service Encryption User" + principal_id = azurerm_disk_encryption_set.des[0].identity[0].principal_id +} diff --git a/modules/terraform/azure/disk-encryption-set/outputs.tf b/modules/terraform/azure/disk-encryption-set/outputs.tf new file mode 100644 index 0000000000..d4374affca --- /dev/null +++ b/modules/terraform/azure/disk-encryption-set/outputs.tf @@ -0,0 +1,4 @@ +output "disk_encryption_set_id" { + description = "The ID of the Disk Encryption Set (for use with --node-osdisk-diskencryptionset-id in AKS CLI)" + value = var.disk_encryption_set_config != null ? azurerm_disk_encryption_set.des[0].id : null +} diff --git a/modules/terraform/azure/disk-encryption-set/variables.tf b/modules/terraform/azure/disk-encryption-set/variables.tf new file mode 100644 index 0000000000..0c6eace670 --- /dev/null +++ b/modules/terraform/azure/disk-encryption-set/variables.tf @@ -0,0 +1,59 @@ +variable "resource_group_name" { + description = "Name of the resource group" + type = string +} + +variable "location" { + description = "Azure region location" + type = string +} + +variable "tags" { + description = "Tags to apply to resources" + type = map(string) + default = {} +} + +variable "key_vaults" { + description = "Map of Key Vault configurations with keys (passed from parent module)" + type = map(any) + default = {} +} + +variable "disk_encryption_set_config" { + description = "Configuration for Disk Encryption Set with Customer-Managed Keys" + type = object({ + name = string # Name of the Disk Encryption Set + key_vault_name = string # Name of the Key Vault containing the encryption key + key_name = string # Name of the encryption key in the Key Vault + encryption_type = optional(string, "EncryptionAtRestWithCustomerKey") # Type of encryption + # Supported values: + # - EncryptionAtRestWithCustomerKey (default): Disk is encrypted with customer-managed key + # - EncryptionAtRestWithPlatformAndCustomerKeys: Double encryption (platform + customer key) + # - ConfidentialVmEncryptedWithCustomerKey: For confidential VMs + auto_key_rotation_enabled = optional(bool, false) # Enable automatic key rotation + }) + default = null + + validation { + condition = ( + var.disk_encryption_set_config == null ? true : ( + length(var.disk_encryption_set_config.name) >= 1 && + length(var.disk_encryption_set_config.name) <= 80 && + length(var.disk_encryption_set_config.key_vault_name) >= 1 && + length(var.disk_encryption_set_config.key_name) >= 1 + ) + ) + error_message = "Disk Encryption Set name must be 1-80 characters, and key_vault_name and key_name must be specified." + } + + validation { + condition = ( + var.disk_encryption_set_config == null ? true : contains( + ["EncryptionAtRestWithCustomerKey", "EncryptionAtRestWithPlatformAndCustomerKeys", "ConfidentialVmEncryptedWithCustomerKey"], + var.disk_encryption_set_config.encryption_type + ) + ) + error_message = "encryption_type must be one of: EncryptionAtRestWithCustomerKey, EncryptionAtRestWithPlatformAndCustomerKeys, or ConfidentialVmEncryptedWithCustomerKey." + } +} diff --git a/modules/terraform/azure/key-vault/main.tf b/modules/terraform/azure/key-vault/main.tf index d4c81d4910..5ef2cade5c 100644 --- a/modules/terraform/azure/key-vault/main.tf +++ b/modules/terraform/azure/key-vault/main.tf @@ -23,6 +23,8 @@ resource "azurerm_key_vault" "kv" { tenant_id = data.azurerm_client_config.current.tenant_id sku_name = "standard" rbac_authorization_enabled = true + purge_protection_enabled = true + soft_delete_retention_days = 7 tags = var.tags } diff --git a/modules/terraform/azure/main.tf b/modules/terraform/azure/main.tf index 56362d72d8..71a5fa30b9 100644 --- a/modules/terraform/azure/main.tf +++ b/modules/terraform/azure/main.tf @@ -33,9 +33,10 @@ locals { aks_cli_custom_config_path = "${path.cwd}/../../../scenarios/${var.scenario_type}/${var.scenario_name}/config/aks_custom_config.json" - all_subnets = merge([for network in var.network_config_list : module.virtual_network[network.role].subnets]...) - all_nics = merge([for network in var.network_config_list : module.virtual_network[network.role].nics]...) - all_key_vaults = merge([for kv_name, kv in module.key_vault : { (kv_name) = kv.key_vaults }]...) + all_subnets = merge([for network in var.network_config_list : module.virtual_network[network.role].subnets]...) + all_nics = merge([for network in var.network_config_list : module.virtual_network[network.role].nics]...) + all_key_vaults = merge([for kv_name, kv in module.key_vault : { (kv_name) = kv.key_vaults }]...) + all_disk_encryption_sets = merge([for des_name, des in module.disk_encryption_set : { (des_name) = des.disk_encryption_set_id }]...) updated_aks_config_list = length(var.aks_config_list) > 0 ? [ for aks in var.aks_config_list : merge( aks, @@ -67,12 +68,14 @@ locals { key_vault_config_map = { for kv in var.key_vault_config_list : kv.name => kv } vm_config_map = { for vm in var.vm_config_list : vm.role => vm } + + disk_encryption_set_config_map = { for des in var.disk_encryption_set_config_list : des.name => des } } provider "azurerm" { features { key_vault { - purge_soft_delete_on_destroy = true + purge_soft_delete_on_destroy = false recover_soft_deleted_key_vaults = false } } @@ -144,25 +147,39 @@ module "key_vault" { tags = local.tags } +module "disk_encryption_set" { + for_each = local.disk_encryption_set_config_map + + source = "./disk-encryption-set" + resource_group_name = local.run_id + location = local.region + disk_encryption_set_config = each.value + key_vaults = local.all_key_vaults + tags = local.tags + + depends_on = [module.key_vault] +} + module "aks" { for_each = local.aks_config_map - source = "./aks" - resource_group_name = local.run_id - location = local.region - aks_config = each.value - tags = local.tags - subnet_id = try(local.all_subnets[each.value.subnet_name], null) - vnet_id = try(module.virtual_network[each.value.role].vnet_id, null) - subnets = try(local.all_subnets, null) - k8s_machine_type = local.k8s_machine_type - k8s_os_disk_type = local.k8s_os_disk_type - network_dataplane = local.aks_network_dataplane - network_policy = local.aks_network_policy - dns_zones = try(module.dns_zones.dns_zone_ids, null) - aks_aad_enabled = local.aks_aad_enabled - key_vaults = local.all_key_vaults - depends_on = [module.route_table, module.virtual_network] + source = "./aks" + resource_group_name = local.run_id + location = local.region + aks_config = each.value + tags = local.tags + subnet_id = try(local.all_subnets[each.value.subnet_name], null) + vnet_id = try(module.virtual_network[each.value.role].vnet_id, null) + subnets = try(local.all_subnets, null) + k8s_machine_type = local.k8s_machine_type + k8s_os_disk_type = local.k8s_os_disk_type + network_dataplane = local.aks_network_dataplane + network_policy = local.aks_network_policy + dns_zones = try(module.dns_zones.dns_zone_ids, null) + aks_aad_enabled = local.aks_aad_enabled + key_vaults = local.all_key_vaults + disk_encryption_sets = local.all_disk_encryption_sets + depends_on = [module.route_table, module.virtual_network, module.disk_encryption_set] } module "aks-cli" { @@ -177,7 +194,8 @@ module "aks-cli" { aks_cli_custom_config_path = local.aks_cli_custom_config_path key_vaults = local.all_key_vaults aks_aad_enabled = local.aks_aad_enabled - depends_on = [module.route_table, module.virtual_network] + disk_encryption_sets = local.all_disk_encryption_sets + depends_on = [module.route_table, module.virtual_network, module.disk_encryption_set] } module "virtual_machine" { diff --git a/modules/terraform/azure/tests/test_aks_cli.tftest.hcl b/modules/terraform/azure/tests/test_aks_cli.tftest.hcl index d5c07b3c08..cd1c3bb617 100644 --- a/modules/terraform/azure/tests/test_aks_cli.tftest.hcl +++ b/modules/terraform/azure/tests/test_aks_cli.tftest.hcl @@ -1,5 +1,12 @@ mock_provider "azurerm" { source = "./tests" + + mock_data "azurerm_client_config" { + defaults = { + tenant_id = "00000000-0000-0000-0000-000000000000" + subscription_id = "12345678-1234-1234-1234-123456789012" + } + } } variables { @@ -82,3 +89,31 @@ run "test_aws_cli_automatic" { error_message = "Actual: ${replace(module.aks-cli["client"].aks_cli_command, "/\\s+/", " ")}" } } + +# Test case: Verify AKS CLI command does NOT include --node-osdisk-diskencryptionset-id when disk_encryption_set_name is null +run "test_aks_cli_without_disk_encryption_set" { + + command = apply + + variables { + aks_cli_config_list = [ + { + role = "client" + aks_name = "test-no-des" + sku_tier = "Standard" + use_aks_preview_cli_extension = true + default_node_pool = { + name = "default" + node_count = 2 + vm_size = "Standard_D2s_v3" + } + dry_run = true + } + ] + } + + assert { + condition = !can(regex("--node-osdisk-diskencryptionset-id", module.aks-cli["client"].aks_cli_command)) + error_message = "AKS CLI command should NOT include --node-osdisk-diskencryptionset-id when disk_encryption_set_name is not provided" + } +} diff --git a/modules/terraform/azure/tests/test_disk_encryption.tftest.hcl b/modules/terraform/azure/tests/test_disk_encryption.tftest.hcl new file mode 100644 index 0000000000..d21a58b95c --- /dev/null +++ b/modules/terraform/azure/tests/test_disk_encryption.tftest.hcl @@ -0,0 +1,98 @@ +mock_provider "azurerm" { + source = "./tests" + + mock_data "azurerm_client_config" { + defaults = { + tenant_id = "00000000-0000-0000-0000-000000000000" + subscription_id = "12345678-1234-1234-1234-123456789012" + } + } +} + +# Test case 1: Verify AKS config works without disk_encryption_set_name (null case) +run "aks_without_disk_encryption" { + command = plan + + variables { + owner = "test" + scenario_type = "perf-eval" + scenario_name = "test" + network_config_list = [] + aks_config_list = [ + { + role = "test" + aks_name = "test-aks" + dns_prefix = "testaks" + sku_tier = "Standard" + network_profile = { + network_plugin = "azure" + } + default_node_pool = { + name = "default" + node_count = 1 + vm_size = "Standard_D2s_v3" + os_disk_type = "Managed" + only_critical_addons_enabled = false + temporary_name_for_rotation = "defaulttmp" + } + extra_node_pool = [] + } + ] + aks_cli_config_list = [] + public_ip_config_list = [] + dns_zones = [] + disk_encryption_set_config_list = [] + key_vault_config_list = [] + json_input = { + region = "East US" + run_id = "test123" + } + deletion_delay = "720h" + } + + assert { + condition = local.updated_aks_config_list[0].disk_encryption_set_name == null + error_message = "disk_encryption_set_name should be null when not specified" + } +} + +# Test case 2: Verify AKS CLI config works without disk_encryption_set_name (null case) +run "aks_cli_without_disk_encryption" { + command = plan + + variables { + owner = "test" + scenario_type = "perf-eval" + scenario_name = "test" + network_config_list = [] + aks_config_list = [] + aks_cli_config_list = [ + { + role = "test-cli" + aks_name = "test-aks-cli" + sku_tier = "Standard" + default_node_pool = { + name = "system" + node_count = 2 + vm_size = "Standard_D2s_v3" + } + extra_node_pool = [] + dry_run = true + } + ] + public_ip_config_list = [] + dns_zones = [] + disk_encryption_set_config_list = [] + key_vault_config_list = [] + json_input = { + region = "East US" + run_id = "test123" + } + deletion_delay = "720h" + } + + assert { + condition = local.updated_aks_cli_config_list[0].disk_encryption_set_name == null + error_message = "disk_encryption_set_name should be null when not specified in AKS CLI config" + } +} diff --git a/modules/terraform/azure/variables.tf b/modules/terraform/azure/variables.tf index 6a480a1670..4ad1262050 100644 --- a/modules/terraform/azure/variables.tf +++ b/modules/terraform/azure/variables.tf @@ -344,6 +344,8 @@ variable "aks_config_list" { key_vault_name = string network_access = optional(string, "Public") }), null) + # Disk Encryption Set configuration for OS disk encryption with Customer-Managed Keys + disk_encryption_set_name = optional(string, null) # Name of the Disk Encryption Set to use for OS disk encryption })) default = [] } @@ -448,7 +450,37 @@ variable "aks_cli_config_list" { network_access = optional(string, "Public") }), null) dry_run = optional(bool, false) # If true, only print the command without executing it. Useful for testing. + # Disk Encryption Set configuration for OS disk encryption with Customer-Managed Keys + disk_encryption_set_name = optional(string, null) # Name of the Disk Encryption Set to use for OS disk encryption })) default = [] } +variable "disk_encryption_set_config_list" { + description = "List of Disk Encryption Set configurations for encrypting AKS OS/data disks with Customer-Managed Keys. Reference: https://learn.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys" + type = list(object({ + name = string # Name of the Disk Encryption Set + key_vault_name = string # Name of the Key Vault containing the encryption key + key_name = string # Name of the encryption key in the Key Vault + encryption_type = optional(string, "EncryptionAtRestWithCustomerKey") # Type of encryption + # Supported values: + # - EncryptionAtRestWithCustomerKey (default): Disk is encrypted with customer-managed key + # - EncryptionAtRestWithPlatformAndCustomerKeys: Double encryption (platform + customer key) + # - ConfidentialVmEncryptedWithCustomerKey: For confidential VMs + auto_key_rotation_enabled = optional(bool, false) # Enable automatic key rotation + })) + default = [] + + validation { + condition = alltrue([ + for config in var.disk_encryption_set_config_list : ( + length(config.name) >= 1 && + length(config.name) <= 80 && + length(config.key_vault_name) >= 1 && + length(config.key_name) >= 1 + ) + ]) + error_message = "Each Disk Encryption Set config must have name 1-80 characters, and key_vault_name and key_name must be specified." + } +} +