Skip to content

Commit

Permalink
Api management support multiple roles validation AB#22184 (#440)
Browse files Browse the repository at this point in the history
* Api management support multiple roles validation

* fix fmt

* adding service principal role assignments

* resolving PR comments

* fix formating
  • Loading branch information
patrik-pa4k authored Dec 18, 2024
1 parent 632020e commit c99e758
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
10 changes: 8 additions & 2 deletions modules/azure/api_management_api/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@ resource "azurerm_api_management_api_policy" "api_policy" {
<claim name="iss" match="any">
<value>${var.aad_settings.issuer}</value>
</claim>
%{if var.role_assignment != null}
%{if length(var.role_assignments) > 0}
<claim name="roles" match="any">
<value>${var.role_assignment}</value>
%{
for role in var.role_assignments
}
<value>${role}</value>
%{
endfor
}
</claim>
%{endif}
</required-claims>
Expand Down
8 changes: 4 additions & 4 deletions modules/azure/api_management_api/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ variable "custom_backend_policy" {
default = null
}

variable "role_assignment" {
type = string
description = "Role to validate in the JWT token's 'roles' claim for access control."
default = null
variable "role_assignments" {
type = list(string)
description = "Roles to validate in the JWT token's 'roles' claim for access control."
default = []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
terraform {
required_version = "~> 1.3"

required_providers {
azuread = {
source = "hashicorp/azuread"
version = "~> 2.36"
}
}

backend "azurerm" {}
}

provider "azuread" {}

resource "azuread_service_principal" "internal" {
for_each = toset([for assignment in var.assignments : assignment.client_id])
client_id = each.key
use_existing = var.use_existing_service_principal
}

resource "azuread_app_role_assignment" "role_assignment" {
for_each = {
for assignment in var.assignments :
"${assignment.role_id}_${assignment.object_id}_${assignment.client_id}" => assignment
}
app_role_id = each.value.role_id
principal_object_id = each.value.object_id
resource_object_id = azuread_service_principal.internal[each.value.client_id].object_id
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "assignments" {
type = list(object({
object_id = string,
role_id = string,
client_id = string
}))
description = "The assignments you want to add to an application."
}
variable "use_existing_service_principal" {
type = bool
default = false
description = "When true, any existing service principal linked to the same application will be automatically imported. When false, an import error will be raised for any pre-existing service principal."
}

0 comments on commit c99e758

Please sign in to comment.