Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api management support multiple roles validation AB#22184 #440

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."
}
Loading