Repository navigation
Add support for Disk Encryption Sets (DES) #1042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e9dce76
Add disk encription module
xinWeiWei24 4a3d56d
Add disk encription to nap complex to test
xinWeiWei24 3ff7d22
Add reader role to the cluster
xinWeiWei24 d093f4d
Add reader role to principle id
xinWeiWei24 0881fe8
flexible run
xinWeiWei24 0319344
no need to use azurerm_client_config
xinWeiWei24 bec702a
commnet cleanup in karpenter collect step
xinWeiWei24 c093b5a
fix timeout in kubelogin
xinWeiWei24 c0867a1
don't delete deployment
xinWeiWei24 ebf4122
don't delete ns
xinWeiWei24 0a9c33b
disable disk encryption
xinWeiWei24 f4240ca
Revert "disable disk encryption"
xinWeiWei24 1a3118f
Revert "don't delete ns"
xinWeiWei24 112edfc
Revert "don't delete deployment"
xinWeiWei24 2d0db7c
Revert "commnet cleanup in karpenter collect step"
xinWeiWei24 1e12a3c
Revert "fix timeout in kubelogin"
xinWeiWei24 b53370c
passing disk_encryption_sets to aks module
xinWeiWei24 ab51f95
Add role assignment to both identities
xinWeiWei24 9464313
Add testing
xinWeiWei24 0bbe7d1
consider dry run
xinWeiWei24 603c76d
Revert "Add disk encription to nap complex to test"
xinWeiWei24 a1c6e6b
full test
xinWeiWei24 f6f3a65
Optimize role assignment
xinWeiWei24 70d0f35
Revert testing
xinWeiWei24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.