|
| 1 | +locals { |
| 2 | + default_audience_name = "api://AzureADTokenExchange" |
| 3 | + github_issuer_url = "https://token.actions.githubusercontent.com" |
| 4 | + |
| 5 | + bootstrap_repo_name = "bootstrap" |
| 6 | + organizations_repo_name = "organizations" |
| 7 | + |
| 8 | + state_file_access_roles = { |
| 9 | + "container-${local.tf_state_container.name}-write" = { |
| 10 | + scope = "${local.tf_state_container.resource_manager_id}" |
| 11 | + role_definition_name = "Storage Blob Data Contributor" |
| 12 | + }, |
| 13 | + "storage-account-${azurerm_storage_account.github_foundations_sa.name}-contributor" = { |
| 14 | + scope = "${azurerm_storage_account.github_foundations_sa.id}" |
| 15 | + role_definition_name = "Storage Account Contributor" |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + bootstrap_project_roles = local.state_file_access_roles |
| 20 | + |
| 21 | + organizations_project_roles = merge( |
| 22 | + local.state_file_access_roles, |
| 23 | + var.kv_name != "" ? { |
| 24 | + "keyvault-${data.azurerm_key_vault.key_vault[0].name}-secret-read" = { |
| 25 | + scope = "${data.azurerm_key_vault.key_vault[0].id}" |
| 26 | + role_definition_name = "Key Vault Secrets User" |
| 27 | + } |
| 28 | + }: {}, |
| 29 | + var.kv_name != "" ? { |
| 30 | + "keyvault-${data.azurerm_key_vault.key_vault[0].name}-vault-read" = { |
| 31 | + scope = "${data.azurerm_key_vault.key_vault[0].id}" |
| 32 | + role_definition_name = "Key Vault Reader" |
| 33 | + } |
| 34 | + }: {} |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +data "azurerm_client_config" "current" {} |
| 39 | + |
| 40 | +data "azurerm_key_vault" "key_vault" { |
| 41 | + count = var.kv_name != "" ? 1 : 0 |
| 42 | + name = var.kv_name |
| 43 | + resource_group_name = var.kv_resource_group != "" ? var.kv_resource_group : local.github_foundations_rg.name |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | +* User assigned identities and roles for github state bucket and federated identity setup |
| 48 | +*/ |
| 49 | +resource "azurerm_user_assigned_identity" "bootstrap_identity" { |
| 50 | + location = local.github_foundations_rg.location |
| 51 | + resource_group_name = local.github_foundations_rg.name |
| 52 | + name = "${local.bootstrap_repo_name}-identity" |
| 53 | +} |
| 54 | + |
| 55 | +resource "azurerm_role_assignment" "bootstrap_role_assignment" { |
| 56 | + for_each = local.bootstrap_project_roles |
| 57 | + scope = each.value.scope |
| 58 | + role_definition_name = each.value.role_definition_name |
| 59 | + principal_id = azurerm_user_assigned_identity.bootstrap_identity.principal_id |
| 60 | +} |
| 61 | + |
| 62 | +resource "azurerm_user_assigned_identity" "organization_identity" { |
| 63 | + location = local.github_foundations_rg.location |
| 64 | + resource_group_name = local.github_foundations_rg.name |
| 65 | + name = "${local.organizations_repo_name}-identity" |
| 66 | +} |
| 67 | + |
| 68 | +resource "azurerm_role_assignment" "organization_role_assignment" { |
| 69 | + for_each = local.organizations_project_roles |
| 70 | + scope = each.value.scope |
| 71 | + role_definition_name = each.value.role_definition_name |
| 72 | + principal_id = azurerm_user_assigned_identity.organization_identity.principal_id |
| 73 | +} |
| 74 | + |
| 75 | +resource "azurerm_federated_identity_credential" "bootstrap_pull_request_credentials" { |
| 76 | + name = "${var.github_foundations_organization_name}-${local.bootstrap_repo_name}-pr-credentials" |
| 77 | + resource_group_name = local.github_foundations_rg.name |
| 78 | + audience = [local.default_audience_name] |
| 79 | + issuer = local.github_issuer_url |
| 80 | + parent_id = azurerm_user_assigned_identity.bootstrap_identity.id |
| 81 | + subject = "repo:${var.github_foundations_organization_name}/${local.bootstrap_repo_name}:pull_request" |
| 82 | +} |
| 83 | + |
| 84 | +resource "azurerm_federated_identity_credential" "bootstrap_drift_credentials" { |
| 85 | + name = "${var.github_foundations_organization_name}-${local.bootstrap_repo_name}-drift-credentials" |
| 86 | + resource_group_name = local.github_foundations_rg.name |
| 87 | + audience = [local.default_audience_name] |
| 88 | + issuer = local.github_issuer_url |
| 89 | + parent_id = azurerm_user_assigned_identity.bootstrap_identity.id |
| 90 | + subject = "repo:${var.github_foundations_organization_name}/${local.bootstrap_repo_name}:ref:refs/heads/${var.drift_detection_branch_name}" |
| 91 | +} |
| 92 | + |
| 93 | +resource "azurerm_federated_identity_credential" "organization_pull_request_credentials" { |
| 94 | + name = "${var.github_foundations_organization_name}-${local.organizations_repo_name}-pr-credentials" |
| 95 | + resource_group_name = local.github_foundations_rg.name |
| 96 | + audience = [local.default_audience_name] |
| 97 | + issuer = local.github_issuer_url |
| 98 | + parent_id = azurerm_user_assigned_identity.organization_identity.id |
| 99 | + subject = "repo:${var.github_foundations_organization_name}/${local.organizations_repo_name}:pull_request" |
| 100 | +} |
| 101 | + |
| 102 | +resource "azurerm_federated_identity_credential" "organization_drift_credentials" { |
| 103 | + name = "${var.github_foundations_organization_name}-${local.organizations_repo_name}-drift-credentials" |
| 104 | + resource_group_name = local.github_foundations_rg.name |
| 105 | + audience = [local.default_audience_name] |
| 106 | + issuer = local.github_issuer_url |
| 107 | + parent_id = azurerm_user_assigned_identity.organization_identity.id |
| 108 | + subject = "repo:${var.github_foundations_organization_name}/${local.organizations_repo_name}:ref:refs/heads/${var.drift_detection_branch_name}" |
| 109 | +} |
0 commit comments