Skip to content

Commit

Permalink
feat: Allow user assigned identities (#6)
Browse files Browse the repository at this point in the history
* allow supplying user assigned identities
  • Loading branch information
Dennisvandermeulen authored Jan 23, 2025
1 parent 53b9bc3 commit 7df9153
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ No modules.
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs that are allowed to access this storage account. Defaults to an empty list. | `list(string)` | `[]` | no |
| <a name="input_system_assigned_identity_enabled"></a> [system\_assigned\_identity\_enabled](#input\_system\_assigned\_identity\_enabled) | Enable or disable the system-assigned managed identity for this storage account. Defaults to true. | `bool` | `true` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to the resource. | `map(string)` | `{}` | no |
| <a name="input_user_assigned_identities"></a> [user\_assigned\_identities](#input\_user\_assigned\_identities) | List of user assigned identities to assign to the storage account | `list(string)` | `[]` | no |
| <a name="input_versioning_enabled"></a> [versioning\_enabled](#input\_versioning\_enabled) | Is versioning enabled? | `bool` | `true` | no |

## Outputs
Expand Down
19 changes: 18 additions & 1 deletion locals.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
locals {
cmk = var.cmk_key_vault_id == null ? 0 : 1
identity_system_assigned_user_assigned = (var.system_assigned_identity_enabled && (length(var.user_assigned_identities) > 0)) ? {
this = {
type = "SystemAssigned, UserAssigned"
user_assigned_resource_ids = var.user_assigned_identities
}
} : null
identity_system_assigned = var.system_assigned_identity_enabled ? {
this = {
type = "SystemAssigned"
user_assigned_resource_ids = null
}
} : null
identity_user_assigned = (length(var.user_assigned_identities) > 0) ? {
this = {
type = "UserAssigned"
user_assigned_resource_ids = var.user_assigned_identities
}
} : null
}
20 changes: 11 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ resource "azurerm_storage_account" "this" {
infrastructure_encryption_enabled = var.infrastructure_encryption_enabled
sftp_enabled = var.sftp_enabled
allow_nested_items_to_be_public = var.allow_nested_items_to_be_public
queue_encryption_key_type = (var.enable_cmk_encryption || local.cmk == 1) ? "Account" : "Service"
table_encryption_key_type = (var.enable_cmk_encryption || local.cmk == 1) ? "Account" : "Service"
queue_encryption_key_type = (var.enable_cmk_encryption || var.cmk_key_vault_id != null) ? "Account" : "Service"
table_encryption_key_type = (var.enable_cmk_encryption || var.cmk_key_vault_id != null) ? "Account" : "Service"

blob_properties {
delete_retention_policy {
Expand All @@ -32,10 +32,11 @@ resource "azurerm_storage_account" "this" {
}

dynamic "identity" {
for_each = var.system_assigned_identity_enabled ? [true] : []
for_each = coalesce(local.identity_system_assigned_user_assigned, local.identity_system_assigned, local.identity_user_assigned, {})

content {
type = "SystemAssigned"
type = identity.value.type
identity_ids = identity.value.user_assigned_resource_ids
}
}

Expand Down Expand Up @@ -85,19 +86,20 @@ resource "azurerm_role_assignment" "extra" {
}

resource "azurerm_storage_account_customer_managed_key" "this" {
count = local.cmk
count = var.cmk_key_vault_id != null ? 1 : 0

storage_account_id = azurerm_storage_account.this.id
key_vault_id = var.cmk_key_vault_id
key_name = var.cmk_key_name
storage_account_id = azurerm_storage_account.this.id
user_assigned_identity_id = local.identity_user_assigned != null ? var.user_assigned_identities[0] : null
key_vault_id = var.cmk_key_vault_id
key_name = var.cmk_key_name

depends_on = [
azurerm_role_assignment.cmk
]
}

resource "azurerm_role_assignment" "cmk" {
count = local.cmk
count = (var.cmk_key_vault_id != null && (local.identity_system_assigned != null || local.identity_system_assigned_user_assigned != null)) ? 1 : 0

scope = var.cmk_key_vault_id
role_definition_name = "Key Vault Crypto Service Encryption User"
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ variable "system_assigned_identity_enabled" {
description = "Enable or disable the system-assigned managed identity for this storage account. Defaults to true."
}

variable "user_assigned_identities" {
type = list(string)
default = []
description = "List of user assigned identities to assign to the storage account"
}

variable "allow_nested_items_to_be_public" {
description = "Allow or disallow nested items to be public. Defaults to false."
type = bool
Expand Down

0 comments on commit 7df9153

Please sign in to comment.