11locals {
2- role_name = " CastAKSRole-${ var . aks_cluster_name } -tf"
3- app_name = substr (" CAST AI ${ var . aks_cluster_name } -${ var . resource_group } " , 0 , 64 )
2+ role_name = " CastAKSRole-${ var . aks_cluster_name } -tf"
3+ app_name = substr (" CAST AI ${ var . aks_cluster_name } -${ var . resource_group } " , 0 , 64 )
4+ federated_identity_name = substr (" castai-${ var . aks_cluster_name } -${ var . resource_group } " , 0 , 64 )
5+ }
6+
7+ data "azurerm_kubernetes_cluster" "castai" {
8+ count = var. authentication_method == " workload_identity" ? 1 : 0
9+ name = var. aks_cluster_name
10+ resource_group_name = var. resource_group
411}
512
613// Azure RM
@@ -60,22 +67,22 @@ resource "azurerm_role_definition" "castai" {
6067}
6168
6269resource "azurerm_role_assignment" "castai_resource_group" {
63- principal_id = azuread_service_principal. castai . object_id
70+ principal_id = var . authentication_method == " client_secret " ? azuread_service_principal. castai [ 0 ] . object_id : azurerm_user_assigned_identity . this [ 0 ] . principal_id
6471 role_definition_id = azurerm_role_definition. castai . role_definition_resource_id
6572 description = " castai role assignment for resource group ${ var . resource_group } "
6673 scope = " /subscriptions/${ var . subscription_id } /resourceGroups/${ var . resource_group } "
6774}
6875
6976resource "azurerm_role_assignment" "castai_node_resource_group" {
70- principal_id = azuread_service_principal. castai . object_id
77+ principal_id = var . authentication_method == " client_secret " ? azuread_service_principal. castai [ 0 ] . object_id : azurerm_user_assigned_identity . this [ 0 ] . principal_id
7178 role_definition_id = azurerm_role_definition. castai . role_definition_resource_id
7279 description = " castai role assignment for resource group ${ var . aks_cluster_name } "
7380 scope = " /subscriptions/${ var . subscription_id } /resourceGroups/${ var . node_resource_group } "
7481}
7582
7683resource "azurerm_role_assignment" "castai_additional_resource_groups" {
7784 for_each = toset (var. additional_resource_groups )
78- principal_id = azuread_service_principal. castai . object_id
85+ principal_id = var . authentication_method == " client_secret " ? azuread_service_principal. castai [ 0 ] . object_id : azurerm_user_assigned_identity . this [ 0 ] . principal_id
7986 description = " castai role assignment for resource group ${ each . key } "
8087 role_definition_id = azurerm_role_definition. castai . role_definition_resource_id
8188 scope = each. key
@@ -86,16 +93,58 @@ resource "azurerm_role_assignment" "castai_additional_resource_groups" {
8693data "azuread_client_config" "current" {}
8794
8895resource "azuread_application" "castai" {
96+ count = var. authentication_method == " client_secret" ? 1 : 0
8997 display_name = local. app_name
9098 owners = (var. azuread_owners == null ? [data . azuread_client_config . current . object_id ] : var. azuread_owners )
9199}
92100
93101resource "azuread_application_password" "castai" {
94- application_id = azuread_application. castai . id
102+ count = var. authentication_method == " client_secret" ? 1 : 0
103+ application_id = azuread_application. castai [0 ]. id
95104}
96105
97106resource "azuread_service_principal" "castai" {
98- client_id = azuread_application. castai . client_id
107+ count = var. authentication_method == " client_secret" ? 1 : 0
108+ client_id = azuread_application. castai [0 ]. client_id
99109 app_role_assignment_required = false
100110 owners = (var. azuread_owners == null ? [data . azuread_client_config . current . object_id ] : var. azuread_owners )
101111}
112+
113+ # State migration for existing users upgrading to authentication_method variable
114+ moved {
115+ from = azuread_application. castai
116+ to = azuread_application. castai [0 ]
117+ }
118+
119+ moved {
120+ from = azuread_application_password. castai
121+ to = azuread_application_password. castai [0 ]
122+ }
123+
124+ moved {
125+ from = azuread_service_principal. castai
126+ to = azuread_service_principal. castai [0 ]
127+ }
128+
129+ // Workload Identity
130+
131+ data "castai_impersonation_service_account" "this" {
132+ count = var. authentication_method == " workload_identity" ? 1 : 0
133+ }
134+
135+ resource "azurerm_user_assigned_identity" "this" {
136+ count = var. authentication_method == " workload_identity" ? 1 : 0
137+ name = " ${ var . aks_cluster_name } -castai-identity"
138+ resource_group_name = var. resource_group
139+ location = data. azurerm_kubernetes_cluster . castai [0 ]. location
140+ }
141+
142+ resource "azurerm_federated_identity_credential" "this" {
143+ count = var. authentication_method == " workload_identity" ? 1 : 0
144+ name = local. federated_identity_name
145+ resource_group_name = var. resource_group
146+ audience = [" api://AzureADTokenExchange" ]
147+ issuer = " https://accounts.google.com"
148+ parent_id = azurerm_user_assigned_identity. this [0 ]. id
149+ subject = data. castai_impersonation_service_account . this [0 ]. id
150+ }
0 commit comments