|
| 1 | +resource "random_string" "suffix" { |
| 2 | + length = 4 |
| 3 | + special = false |
| 4 | + upper = false |
| 5 | +} |
| 6 | + |
| 7 | +locals { |
| 8 | + name_prefix = "mondoo-wif-${random_string.suffix.result}" |
| 9 | +} |
| 10 | + |
| 11 | +data "azurerm_subscription" "current" {} |
| 12 | +data "azurerm_client_config" "current" {} |
| 13 | + |
| 14 | +################################################################################ |
| 15 | +# Resource Group |
| 16 | +################################################################################ |
| 17 | + |
| 18 | +resource "azurerm_resource_group" "rg" { |
| 19 | + name = "${local.name_prefix}-rg" |
| 20 | + location = var.location |
| 21 | + |
| 22 | + tags = { |
| 23 | + Environment = "Mondoo Operator WIF Tests" |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +################################################################################ |
| 28 | +# Management Cluster (runs the operator, OIDC + Workload Identity enabled) |
| 29 | +################################################################################ |
| 30 | + |
| 31 | +resource "azurerm_kubernetes_cluster" "management" { |
| 32 | + name = "${local.name_prefix}-mgmt" |
| 33 | + location = azurerm_resource_group.rg.location |
| 34 | + resource_group_name = azurerm_resource_group.rg.name |
| 35 | + dns_prefix = "${local.name_prefix}-mgmt" |
| 36 | + kubernetes_version = var.k8s_version |
| 37 | + |
| 38 | + oidc_issuer_enabled = true |
| 39 | + workload_identity_enabled = true |
| 40 | + |
| 41 | + default_node_pool { |
| 42 | + name = "default" |
| 43 | + node_count = 1 |
| 44 | + vm_size = "Standard_D2s_v3" |
| 45 | + } |
| 46 | + |
| 47 | + identity { |
| 48 | + type = "SystemAssigned" |
| 49 | + } |
| 50 | + |
| 51 | + tags = { |
| 52 | + Environment = "Mondoo Operator WIF Tests" |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +################################################################################ |
| 57 | +# Target Cluster (to be scanned, Azure RBAC enabled) |
| 58 | +################################################################################ |
| 59 | + |
| 60 | +resource "azurerm_kubernetes_cluster" "target" { |
| 61 | + name = "${local.name_prefix}-target" |
| 62 | + location = azurerm_resource_group.rg.location |
| 63 | + resource_group_name = azurerm_resource_group.rg.name |
| 64 | + dns_prefix = "${local.name_prefix}-target" |
| 65 | + kubernetes_version = var.k8s_version |
| 66 | + |
| 67 | + azure_active_directory_role_based_access_control { |
| 68 | + managed = true |
| 69 | + azure_rbac_enabled = true |
| 70 | + } |
| 71 | + |
| 72 | + default_node_pool { |
| 73 | + name = "default" |
| 74 | + node_count = 1 |
| 75 | + vm_size = "Standard_D2s_v3" |
| 76 | + } |
| 77 | + |
| 78 | + identity { |
| 79 | + type = "SystemAssigned" |
| 80 | + } |
| 81 | + |
| 82 | + tags = { |
| 83 | + Environment = "Mondoo Operator WIF Tests" |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +################################################################################ |
| 88 | +# Azure AD Application + Service Principal for the scanner |
| 89 | +################################################################################ |
| 90 | + |
| 91 | +resource "azuread_application" "scanner" { |
| 92 | + display_name = "${local.name_prefix}-scanner" |
| 93 | +} |
| 94 | + |
| 95 | +resource "azuread_service_principal" "scanner" { |
| 96 | + client_id = azuread_application.scanner.client_id |
| 97 | +} |
| 98 | + |
| 99 | +# Federated identity credential: links the management cluster's KSA to the Azure AD app |
| 100 | +resource "azuread_application_federated_identity_credential" "scanner" { |
| 101 | + application_id = azuread_application.scanner.id |
| 102 | + display_name = "mondoo-operator-wif" |
| 103 | + audiences = ["api://AzureADTokenExchange"] |
| 104 | + issuer = azurerm_kubernetes_cluster.management.oidc_issuer_url |
| 105 | + subject = "system:serviceaccount:${var.scanner_namespace}:${var.scanner_service_account}" |
| 106 | +} |
| 107 | + |
| 108 | +################################################################################ |
| 109 | +# Role Assignments on the target cluster |
| 110 | +################################################################################ |
| 111 | + |
| 112 | +# Allow the service principal to get cluster credentials |
| 113 | +resource "azurerm_role_assignment" "cluster_user" { |
| 114 | + scope = azurerm_kubernetes_cluster.target.id |
| 115 | + role_definition_name = "Azure Kubernetes Service Cluster User Role" |
| 116 | + principal_id = azuread_service_principal.scanner.object_id |
| 117 | +} |
| 118 | + |
| 119 | +# Allow the service principal to read K8s resources via Azure RBAC |
| 120 | +resource "azurerm_role_assignment" "rbac_reader" { |
| 121 | + scope = azurerm_kubernetes_cluster.target.id |
| 122 | + role_definition_name = "Azure Kubernetes Service RBAC Reader" |
| 123 | + principal_id = azuread_service_principal.scanner.object_id |
| 124 | +} |
0 commit comments