Skip to content

Commit 1e32e4e

Browse files
committed
fix: refactor Key Vault access policies to avoid import issues
1 parent b706b35 commit 1e32e4e

1 file changed

Lines changed: 34 additions & 22 deletions

File tree

infra/modules/keyvault/keyvault.tf

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@
33
# ------------------------------------------------------------------------------------------------------
44
data "azurerm_client_config" "current" {}
55

6+
locals {
7+
# Build list of access policies dynamically
8+
access_policies = concat(
9+
[
10+
# Access policy for the current Terraform client (local runs)
11+
{
12+
tenant_id = data.azurerm_client_config.current.tenant_id
13+
object_id = data.azurerm_client_config.current.object_id
14+
secret_permissions = [
15+
"Get", "List", "Set", "Delete", "Purge", "Recover"
16+
]
17+
}
18+
],
19+
# Conditionally add GitHub Actions access policy if object_id is provided
20+
var.github_actions_principal_id != null ? [
21+
{
22+
tenant_id = data.azurerm_client_config.current.tenant_id
23+
object_id = var.github_actions_principal_id
24+
secret_permissions = [
25+
"Get", "List", "Set", "Delete", "Purge", "Recover"
26+
]
27+
}
28+
] : []
29+
)
30+
}
31+
632
resource "azurerm_key_vault" "kv" {
733
name = "${var.prefix}-kv"
834
location = var.location
@@ -13,34 +39,20 @@ resource "azurerm_key_vault" "kv" {
1339

1440
tags = var.tags
1541

16-
# Access policy for the current Terraform client (local runs)
17-
access_policy {
18-
tenant_id = data.azurerm_client_config.current.tenant_id
19-
object_id = data.azurerm_client_config.current.object_id
20-
21-
secret_permissions = [
22-
"Get", "List", "Set", "Delete", "Purge", "Recover"
23-
]
42+
# Apply all access policies (local client + GitHub Actions if provided)
43+
dynamic "access_policy" {
44+
for_each = local.access_policies
45+
content {
46+
tenant_id = access_policy.value.tenant_id
47+
object_id = access_policy.value.object_id
48+
secret_permissions = access_policy.value.secret_permissions
49+
}
2450
}
2551
}
2652

27-
# Access policy for GitHub Actions service principal (CI/CD)
28-
resource "azurerm_key_vault_access_policy" "github_actions" {
29-
count = var.github_actions_principal_id != null ? 1 : 0
30-
key_vault_id = azurerm_key_vault.kv.id
31-
tenant_id = data.azurerm_client_config.current.tenant_id
32-
object_id = var.github_actions_principal_id
33-
34-
secret_permissions = [
35-
"Get", "List", "Set", "Delete", "Purge", "Recover"
36-
]
37-
}
38-
3953
resource "azurerm_key_vault_secret" "secrets" {
4054
count = length(var.secrets)
4155
name = var.secrets[count.index].name
4256
value = var.secrets[count.index].value
4357
key_vault_id = azurerm_key_vault.kv.id
44-
45-
depends_on = [azurerm_key_vault_access_policy.github_actions]
4658
}

0 commit comments

Comments
 (0)