|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +locals { |
| 18 | + apps = toset([ |
| 19 | + "Terraform", |
| 20 | + "rg-cleanup", |
| 21 | + "prow" |
| 22 | + ]) |
| 23 | + build_cluster_issuers = { |
| 24 | + aks = { |
| 25 | + issuer = "https://eastus2.oic.prod-aks.azure.com/d1aa7522-0959-442e-80ee-8c4f7fb4c184/85d5aa19-bc3c-4cdb-bc17-0cf8703cfa3f" |
| 26 | + } |
| 27 | + eks = { |
| 28 | + issuer = "https://oidc.eks.us-east-2.amazonaws.com/id/F8B73554FE6FBAF9B19569183FB39762" |
| 29 | + } |
| 30 | + gke = { |
| 31 | + issuer = "https://container.googleapis.com/v1/projects/k8s-infra-prow-build/locations/us-central1/clusters/prow-build" |
| 32 | + } |
| 33 | + } |
| 34 | + graph_api_permissions = { |
| 35 | + Terraform = { |
| 36 | + roles = [ |
| 37 | + "62a82d76-70ea-41e2-9197-370581804d09", # Group.ReadWrite.All |
| 38 | + "1bfefb4e-e0b5-418b-a88f-73c46d2cc8e9", # Application.ReadWrite.All |
| 39 | + "df021288-bdef-4463-88db-98f22de89214", # User.Read.All |
| 40 | + "9e3f62cf-ca93-4989-b6ce-bf83c28f9fe8", # RoleManagement.ReadWrite.Directory |
| 41 | + ] |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +resource "azuread_application" "apps" { |
| 48 | + for_each = local.apps |
| 49 | + display_name = each.key |
| 50 | + |
| 51 | + dynamic "required_resource_access" { |
| 52 | + for_each = try(local.graph_api_permissions[each.key], null) == null ? [] : [local.graph_api_permissions[each.key]] |
| 53 | + content { |
| 54 | + resource_app_id = "00000003-0000-0000-c000-000000000000" # Microsoft Graph |
| 55 | + |
| 56 | + dynamic "resource_access" { |
| 57 | + for_each = required_resource_access.value.roles |
| 58 | + content { |
| 59 | + id = resource_access.value |
| 60 | + type = "Role" |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +resource "azuread_service_principal" "service_principals" { |
| 68 | + for_each = local.apps |
| 69 | + client_id = azuread_application.apps[each.key].client_id |
| 70 | +} |
| 71 | + |
| 72 | +resource "azuread_application_federated_identity_credential" "terraform" { |
| 73 | + for_each = toset([ |
| 74 | + "system:serviceaccount:atlantis:atlantis", |
| 75 | + ]) |
| 76 | + display_name = reverse(split(":", each.key))[0] |
| 77 | + audiences = ["api://AzureADTokenExchange"] |
| 78 | + issuer = "https://container.googleapis.com/v1/projects/k8s-infra-prow/locations/us-central1/clusters/utility" |
| 79 | + application_id = azuread_application.apps["Terraform"].id |
| 80 | + subject = each.key |
| 81 | +} |
| 82 | + |
| 83 | +resource "azuread_application_federated_identity_credential" "rg_cleanup" { |
| 84 | + for_each = toset([ |
| 85 | + "system:serviceaccount:test-pods:rg-cleanup", |
| 86 | + ]) |
| 87 | + display_name = reverse(split(":", each.key))[0] |
| 88 | + audiences = ["api://AzureADTokenExchange"] |
| 89 | + issuer = "https://eastus2.oic.prod-aks.azure.com/d1aa7522-0959-442e-80ee-8c4f7fb4c184/85d5aa19-bc3c-4cdb-bc17-0cf8703cfa3f" |
| 90 | + application_id = azuread_application.apps["rg-cleanup"].id |
| 91 | + subject = each.key |
| 92 | +} |
| 93 | + |
| 94 | +resource "azuread_application_federated_identity_credential" "prow" { |
| 95 | + for_each = local.build_cluster_issuers |
| 96 | + display_name = each.key |
| 97 | + audiences = ["api://AzureADTokenExchange"] |
| 98 | + issuer = each.value.issuer |
| 99 | + application_id = azuread_application.apps["prow"].id |
| 100 | + subject = "system:serviceaccount:test-pods:azure" |
| 101 | +} |
0 commit comments