|
| 1 | +//--------------------------------------------------------------------------- |
| 2 | +// Terraform Settings |
| 3 | +//--------------------------------------------------------------------------- |
| 4 | +terraform { |
| 5 | + required_version = ">= 1.0" |
| 6 | + required_providers { |
| 7 | + azurerm = { |
| 8 | + source = "hashicorp/azurerm" |
| 9 | + version = "3.3.0" |
| 10 | + } |
| 11 | + azuread = { |
| 12 | + source = "hashicorp/azuread" |
| 13 | + version = "2.18.0" |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +# At this point, we would have liked to use a custom role for the following reasons: |
| 19 | +# - permissions are explicitedly stated and can easily be fine tuned in the future |
| 20 | +# - we are independent of changes to Built-In Roles by Microsoft |
| 21 | +# - we could have restricted the existence of the role to just it's scope |
| 22 | +# HOWEVER, since Microsoft decided you cannot assign the 'Microsoft.Billing/billingPeriods/read' via the api (Status=400 Code="InvalidActionOrNotAction" Message="'Microsoft.Billing/billingPeriods/read' does not match any of the actions supported by the providers.") |
| 23 | +# we have to use a built in role for now that has that permission. If in the future they fix this problem, we can use the following custom role snippet |
| 24 | +# resource azurerm_role_definition meshcloud_metering { |
| 25 | +# name = "metering.${var.service_principal_name_suffix}" |
| 26 | +# scope = var.scope |
| 27 | +# description = "Permissions required by meshcloud in order to supply billing and usage data via its metering module" |
| 28 | + |
| 29 | +# permissions { |
| 30 | +# actions = [ |
| 31 | +# "Microsoft.Consumption/*/read", |
| 32 | +# "Microsoft.CostManagement/*/read", |
| 33 | +# "Microsoft.Billing/billingPeriods/read", |
| 34 | +# "Microsoft.Resources/subscriptions/read", |
| 35 | +# "Microsoft.Resources/subscriptions/resourceGroups/read", |
| 36 | +# "Microsoft.Support/*", |
| 37 | +# "Microsoft.Advisor/configurations/read", |
| 38 | +# "Microsoft.Advisor/recommendations/read", |
| 39 | +# "Microsoft.Management/managementGroups/read" |
| 40 | +# ] |
| 41 | +# } |
| 42 | + |
| 43 | +# assignable_scopes = [ |
| 44 | +# var.scope |
| 45 | +# ] |
| 46 | +# } |
| 47 | + |
| 48 | +//--------------------------------------------------------------------------- |
| 49 | +// Assign Cost Management reader role to the enterprise application |
| 50 | +//--------------------------------------------------------------------------- |
| 51 | +# For now we are using the following built-in role |
| 52 | +resource "azurerm_role_assignment" "meshcloud_metering" { |
| 53 | + scope = var.scope |
| 54 | + role_definition_name = "Cost Management Reader" |
| 55 | + principal_id = azuread_service_principal.meshcloud_metering.id |
| 56 | +} |
| 57 | + |
| 58 | +//--------------------------------------------------------------------------- |
| 59 | +// Create custom role definition |
| 60 | +//--------------------------------------------------------------------------- |
| 61 | +# If more resources are collected in the future, the permissions to read those should be added here. |
| 62 | +resource "azurerm_role_definition" "meshcloud_metering_cloud_inventory_role" { |
| 63 | + name = "metering.${var.service_principal_name_suffix}_cloud_inventory_role" |
| 64 | + scope = var.scope |
| 65 | + description = "Permissions required by meshcloud in order to collect information about resources in the metering module" |
| 66 | + |
| 67 | + permissions { |
| 68 | + actions = [ |
| 69 | + "Microsoft.Network/publicIPAddresses/read", |
| 70 | + "Microsoft.Network/networkInterfaces/read", |
| 71 | + "Microsoft.Compute/virtualMachines/*/read" |
| 72 | + ] |
| 73 | + } |
| 74 | + |
| 75 | + assignable_scopes = [ |
| 76 | + var.scope |
| 77 | + ] |
| 78 | +} |
| 79 | + |
| 80 | +//--------------------------------------------------------------------------- |
| 81 | +// Assign Custom role to the enterprise application |
| 82 | +//--------------------------------------------------------------------------- |
| 83 | +resource "azurerm_role_assignment" "meshcloud_metering_cloud_inventory" { |
| 84 | + scope = var.scope |
| 85 | + role_definition_id = azurerm_role_definition.meshcloud_metering_cloud_inventory_role.role_definition_resource_id |
| 86 | + principal_id = azuread_service_principal.meshcloud_metering.id |
| 87 | +} |
| 88 | + |
| 89 | +//--------------------------------------------------------------------------- |
| 90 | +// Create New application in Microsoft Entra ID |
| 91 | +//--------------------------------------------------------------------------- |
| 92 | +resource "azuread_application" "meshcloud_metering" { |
| 93 | + display_name = "metering.${var.service_principal_name_suffix}" |
| 94 | + |
| 95 | + web { |
| 96 | + implicit_grant { |
| 97 | + access_token_issuance_enabled = false |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +//--------------------------------------------------------------------------- |
| 104 | +// Create New Enterprise application and associate it with the previously created app |
| 105 | +//--------------------------------------------------------------------------- |
| 106 | +resource "azuread_service_principal" "meshcloud_metering" { |
| 107 | + application_id = azuread_application.meshcloud_metering.application_id |
| 108 | +} |
| 109 | + |
| 110 | +//--------------------------------------------------------------------------- |
| 111 | +// Create a password for the Enterprise application |
| 112 | +//--------------------------------------------------------------------------- |
| 113 | +resource "azuread_service_principal_password" "service_principal_pw" { |
| 114 | + service_principal_id = azuread_service_principal.meshcloud_metering.id |
| 115 | + end_date = "2999-01-01T01:02:03Z" # no expiry |
| 116 | +} |
| 117 | + |
| 118 | +# facilitate migration from v0.1.0 of the module |
| 119 | +moved { |
| 120 | + from = azuread_service_principal_password.spp_pw |
| 121 | + to = azuread_service_principal_password.service_principal_pw |
| 122 | +} |
0 commit comments