-
Notifications
You must be signed in to change notification settings - Fork 0
feat: azure rg building block #175
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
Open
younGihan
wants to merge
1
commit into
main
Choose a base branch
from
feature/azure-resource-group
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| output "documentation_md" { | ||
| value = <<EOF | ||
| # Azure Resource Group Building Block | ||
|
|
||
| The Resource Group Building Block creates an empty Azure Resource Group for a meshStack project. | ||
| The resource group name is automatically generated following the schema `rg-<workspaceId>-<projectId>`, | ||
| ensuring consistent naming across all landing zones. | ||
|
|
||
| # Azure Resource Group Building Block Backplane | ||
|
|
||
| This module automates the IAM setup required for the Resource Group building block within Azure. | ||
|
|
||
| ## Role Definition | ||
|
|
||
| | Name | ID | | ||
| | --- | --- | | ||
| | ${azurerm_role_definition.buildingblock_deploy.name} | ${azurerm_role_definition.buildingblock_deploy.id} | | ||
|
|
||
| ## Role Assignments | ||
|
|
||
| | Principal ID | | ||
| | --- | | ||
| | ${join("\n", concat([for assignment in azurerm_role_assignment.existing_principals : assignment.principal_id], var.create_service_principal_name != null ? [azurerm_role_assignment.created_principal[0].principal_id] : []))} | | ||
|
|
||
| ## Scope | ||
|
|
||
| - **Scope**: `${var.scope}` | ||
|
|
||
| EOF | ||
| description = "Markdown documentation with information about the Resource Group building block backplane." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| data "azurerm_subscription" "current" {} | ||
|
|
||
| resource "azuread_application" "buildingblock_deploy" { | ||
| count = var.create_service_principal_name != null ? 1 : 0 | ||
|
|
||
| display_name = "${var.name}-${var.create_service_principal_name}" | ||
| } | ||
|
|
||
| resource "azuread_service_principal" "buildingblock_deploy" { | ||
| count = var.create_service_principal_name != null ? 1 : 0 | ||
|
|
||
| client_id = azuread_application.buildingblock_deploy[0].client_id | ||
| app_role_assignment_required = false | ||
| } | ||
|
|
||
| resource "azuread_application_federated_identity_credential" "buildingblock_deploy" { | ||
| count = var.create_service_principal_name != null && var.workload_identity_federation != null ? 1 : 0 | ||
|
|
||
| application_id = azuread_application.buildingblock_deploy[0].id | ||
| display_name = var.create_service_principal_name | ||
| audiences = ["api://AzureADTokenExchange"] | ||
| issuer = var.workload_identity_federation.issuer | ||
| subject = var.workload_identity_federation.subject | ||
| } | ||
|
|
||
| resource "azuread_application_password" "buildingblock_deploy" { | ||
| count = var.create_service_principal_name != null && var.workload_identity_federation == null ? 1 : 0 | ||
|
|
||
| application_id = azuread_application.buildingblock_deploy[0].id | ||
| display_name = "${var.create_service_principal_name}-password" | ||
| } | ||
|
|
||
| resource "azurerm_role_definition" "buildingblock_deploy" { | ||
| name = "${var.name}-deploy" | ||
| description = "Enables deployment of the ${var.name} building block to subscriptions" | ||
| scope = var.scope | ||
|
|
||
| permissions { | ||
| actions = [ | ||
| # Register resource providers in Azure Resource Manager | ||
| "*/register/action", | ||
|
|
||
| # Resource Groups - full lifecycle management | ||
| "Microsoft.Resources/subscriptions/resourceGroups/*", | ||
|
|
||
| # Read subscription providers | ||
| "Microsoft.Resources/subscriptions/providers/read", | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| resource "azurerm_role_assignment" "existing_principals" { | ||
| for_each = var.existing_principal_ids | ||
|
|
||
| role_definition_id = azurerm_role_definition.buildingblock_deploy.role_definition_resource_id | ||
| principal_id = each.value | ||
| scope = var.scope | ||
| } | ||
|
|
||
| resource "azurerm_role_assignment" "created_principal" { | ||
| count = var.create_service_principal_name != null ? 1 : 0 | ||
|
|
||
| role_definition_id = azurerm_role_definition.buildingblock_deploy.role_definition_resource_id | ||
| principal_id = azuread_service_principal.buildingblock_deploy[0].object_id | ||
| scope = var.scope | ||
| } | ||
|
|
||
| resource "azuread_directory_role" "directory_readers" { | ||
| display_name = "Directory Readers" | ||
| } | ||
|
|
||
| resource "azuread_directory_role_assignment" "directory_readers_existing" { | ||
| for_each = var.existing_principal_ids | ||
| role_id = azuread_directory_role.directory_readers.template_id | ||
| principal_object_id = each.value | ||
| } | ||
|
|
||
| resource "azuread_directory_role_assignment" "directory_readers_created" { | ||
| count = var.create_service_principal_name != null ? 1 : 0 | ||
| role_id = azuread_directory_role.directory_readers.template_id | ||
| principal_object_id = azuread_service_principal.buildingblock_deploy[0].object_id | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| output "role_definition_id" { | ||
| value = azurerm_role_definition.buildingblock_deploy.id | ||
| description = "The ID of the role definition that enables deployment of the building block." | ||
| } | ||
|
|
||
| output "role_definition_name" { | ||
| value = azurerm_role_definition.buildingblock_deploy.name | ||
| description = "The name of the role definition that enables deployment of the building block." | ||
| } | ||
|
|
||
| output "role_assignment_ids" { | ||
| value = concat( | ||
| [for id in azurerm_role_assignment.existing_principals : id.id], | ||
| var.create_service_principal_name != null ? [azurerm_role_assignment.created_principal[0].id] : [] | ||
| ) | ||
| description = "The IDs of the role assignments for all service principals." | ||
| } | ||
|
|
||
| output "role_assignment_principal_ids" { | ||
| value = concat( | ||
| [for id in azurerm_role_assignment.existing_principals : id.principal_id], | ||
| var.create_service_principal_name != null ? [azurerm_role_assignment.created_principal[0].principal_id] : [] | ||
| ) | ||
| description = "The principal IDs of all service principals that have been assigned the role." | ||
| } | ||
|
|
||
| output "created_service_principal" { | ||
| value = var.create_service_principal_name != null ? { | ||
| object_id = azuread_service_principal.buildingblock_deploy[0].object_id | ||
| client_id = azuread_service_principal.buildingblock_deploy[0].client_id | ||
| display_name = azuread_service_principal.buildingblock_deploy[0].display_name | ||
| name = var.create_service_principal_name | ||
| } : null | ||
| description = "Information about the created service principal." | ||
| } | ||
|
|
||
| output "created_application" { | ||
| value = var.create_service_principal_name != null ? { | ||
| object_id = azuread_application.buildingblock_deploy[0].object_id | ||
| client_id = azuread_application.buildingblock_deploy[0].client_id | ||
| display_name = azuread_application.buildingblock_deploy[0].display_name | ||
| } : null | ||
| description = "Information about the created Azure AD application." | ||
| } | ||
|
|
||
| output "workload_identity_federation" { | ||
| value = var.create_service_principal_name != null && var.workload_identity_federation != null ? { | ||
| credential_id = azuread_application_federated_identity_credential.buildingblock_deploy[0].credential_id | ||
| display_name = azuread_application_federated_identity_credential.buildingblock_deploy[0].display_name | ||
| issuer = azuread_application_federated_identity_credential.buildingblock_deploy[0].issuer | ||
| subject = azuread_application_federated_identity_credential.buildingblock_deploy[0].subject | ||
| audiences = azuread_application_federated_identity_credential.buildingblock_deploy[0].audiences | ||
| } : null | ||
| description = "Information about the created workload identity federation credential." | ||
| } | ||
|
|
||
| output "application_password" { | ||
| value = var.create_service_principal_name != null && var.workload_identity_federation == null ? { | ||
| key_id = azuread_application_password.buildingblock_deploy[0].key_id | ||
| display_name = azuread_application_password.buildingblock_deploy[0].display_name | ||
| } : null | ||
| description = "Information about the created application password (excludes the actual password value for security)." | ||
| sensitive = true | ||
| } | ||
|
|
||
| output "scope" { | ||
| value = var.scope | ||
| description = "The scope where the role definition and role assignments are applied." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| provider "azurerm" { | ||
| features {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| variable "name" { | ||
| type = string | ||
| nullable = false | ||
| default = "resource-group" | ||
| description = "Name of the building block, used for naming Azure resources." | ||
| validation { | ||
| condition = can(regex("^[-a-z0-9]+$", var.name)) | ||
| error_message = "Only alphanumeric lowercase characters and dashes are allowed." | ||
| } | ||
| } | ||
|
|
||
| variable "scope" { | ||
| type = string | ||
| nullable = false | ||
| description = "Scope where the building block should be deployable, typically the parent management group of all landing zones." | ||
| } | ||
|
|
||
| variable "existing_principal_ids" { | ||
| type = set(string) | ||
| nullable = false | ||
| default = [] | ||
| description = "Set of existing principal IDs that will be granted permissions to deploy the building block." | ||
| } | ||
|
|
||
| variable "create_service_principal_name" { | ||
| type = string | ||
| nullable = true | ||
| default = null | ||
| description = "If set, creates a new service principal with the given name for deploying the building block." | ||
| } | ||
|
|
||
| variable "workload_identity_federation" { | ||
| type = object({ | ||
| issuer = string | ||
| subject = string | ||
| }) | ||
| nullable = true | ||
| default = null | ||
| description = "If set, configures workload identity federation for the created service principal." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| terraform { | ||
| required_version = ">= 1.0" | ||
|
|
||
| required_providers { | ||
| azurerm = { | ||
| source = "hashicorp/azurerm" | ||
| version = "~> 4.64" | ||
| } | ||
| azuread = { | ||
| source = "hashicorp/azuread" | ||
| version = "~> 3.8" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| locals { | ||
| resource_group_name = "rg-${var.workspace_identifier}-${var.project_identifier}" | ||
| } | ||
|
|
||
| resource "azurerm_resource_group" "this" { | ||
| name = local.resource_group_name | ||
| location = var.location | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| output "resource_group_name" { | ||
| value = azurerm_resource_group.this.name | ||
| description = "The name of the created resource group (e.g. 'rg-myworkspace-myproject')." | ||
| } | ||
|
|
||
| output "resource_group_id" { | ||
| value = azurerm_resource_group.this.id | ||
| description = "The Azure resource ID of the created resource group." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| provider "azurerm" { | ||
| subscription_id = var.subscription_id | ||
| features {} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| variable "subscription_id" { | ||
| type = string | ||
| description = "The Azure subscription ID where the resource group will be created." | ||
| } | ||
|
|
||
| variable "workspace_identifier" { | ||
| type = string | ||
| description = "The meshStack workspace identifier. Used to generate the resource group name." | ||
| } | ||
|
|
||
| variable "project_identifier" { | ||
| type = string | ||
| description = "The meshStack project identifier. Used to generate the resource group name." | ||
| } | ||
|
|
||
| variable "location" { | ||
| type = string | ||
| description = "The Azure region where the resource group will be created (e.g. 'westeurope', 'eastus')." | ||
| default = "westeurope" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| terraform { | ||
| required_version = ">= 1.0" | ||
|
|
||
| required_providers { | ||
| azurerm = { | ||
| source = "hashicorp/azurerm" | ||
| version = "~> 4.64" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the ENV var pls and not adding vars in the provider. we dont need them there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can also remove it from the the vars. We can use the tenant id.