Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions modules/terraform/azure/aks-cli/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
"" :
Expand Down Expand Up @@ -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)
)
Comment thread
xinWeiWei24 marked this conversation as resolved.
Resolved

subnet_id_parameter = (local.aks_subnet_id == null ?
"" :
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions modules/terraform/azure/aks-cli/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
})
}

30 changes: 30 additions & 0 deletions modules/terraform/azure/aks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {}

Expand All @@ -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

Comment thread
xinWeiWei24 marked this conversation as resolved.
Resolved
# Wait for KMS role assignment to propagate
depends_on = [
azurerm_role_assignment.aks_identity_kms_roles
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions modules/terraform/azure/aks/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
145 changes: 145 additions & 0 deletions modules/terraform/azure/disk-encryption-set/README.md
Original file line number Diff line number Diff line change
@@ -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)
71 changes: 71 additions & 0 deletions modules/terraform/azure/disk-encryption-set/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions modules/terraform/azure/disk-encryption-set/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
Loading