Skip to content

Commit 72197c9

Browse files
author
Felix Zieger
committed
feat: grant admin consent in code
1 parent 960727d commit 72197c9

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ For an overview of the module structure, refer to [generated terraform docs](./T
5555
terraform output -json
5656
```
5757
58-
7. Grant admin consent to the newly created Replicator Service Principal. See the terraform output on how to do this.
59-
60-
8. Grant access on the enrollment account as described in the [meshcloud public docs](https://docs.dev.meshcloud.io/docs/meshstack.how-to.integrate-meshplatform-azure-manually.html#set-up-subscription-provisioning).
58+
7. Grant access on the enrollment account as described in the [meshcloud public docs](https://docs.dev.meshcloud.io/docs/meshstack.how-to.integrate-meshplatform-azure-manually.html#set-up-subscription-provisioning).
6159
6260
### Using CLI
6361

modules/meshcloud-idp-lookup-spp/module.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ terraform {
1212
}
1313
}
1414

15+
data "azuread_application_published_app_ids" "well_known" {}
16+
17+
resource "azuread_service_principal" "msgraph" {
18+
application_id = data.azuread_application_published_app_ids.well_known.result.MicrosoftGraph
19+
use_existing = true
20+
}
21+
1522
resource "azuread_application" "meshcloud_idp_lookup" {
1623
display_name = "idplookup.${var.spp_name_suffix}"
1724

@@ -21,12 +28,12 @@ resource "azuread_application" "meshcloud_idp_lookup" {
2128
}
2229
}
2330
required_resource_access {
24-
resource_app_id = "00000003-0000-0000-c000-000000000000" # Microsoft Graph
31+
resource_app_id = data.azuread_application_published_app_ids.well_known.result.MicrosoftGraph
2532

2633
# We only require this User.Read.All permission to see all of the Users in the AAD https://docs.microsoft.com/en-us/graph/permissions-reference#microsoft-graph-permission-names
2734
# Since this is a role (and not a scope) permission, you also have to enable admin consent in azure portal
2835
resource_access {
29-
id = "df021288-bdef-4463-88db-98f22de89214" # User.Read.All
36+
id = azuread_service_principal.msgraph.app_role_ids["User.Read.All"]
3037
type = "Role"
3138
}
3239

@@ -47,6 +54,12 @@ resource "azuread_service_principal" "meshcloud_idp_lookup" {
4754
application_id = azuread_application.meshcloud_idp_lookup.application_id
4855
}
4956

57+
resource "azuread_app_role_assignment" "meshcloud_idp_lookup" {
58+
app_role_id = azuread_service_principal.msgraph.app_role_ids["User.Read.All"]
59+
principal_object_id = azuread_service_principal.meshcloud_idp_lookup.object_id
60+
resource_object_id = azuread_service_principal.msgraph.object_id
61+
}
62+
5063
resource "azuread_service_principal_password" "spp_pw" {
5164
service_principal_id = azuread_service_principal.meshcloud_idp_lookup.id
5265
end_date = "2999-01-01T01:02:03Z" # no expiry

modules/meshcloud-idp-lookup-spp/outputs.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
output "service_principal" {
22
value = {
3-
INFO = "On the very first time running tf apply, you have to run 'az ad app permission admin-consent --id ${azuread_service_principal.meshcloud_idp_lookup.application_id}' or click the 'Grant Admin consent button' in the portal"
43
object_id = azuread_service_principal.meshcloud_idp_lookup.id
54
app_id = azuread_service_principal.meshcloud_idp_lookup.application_id
65
password = "Execute `terraform output idp_lookup_spp_password` to see the password"

modules/meshcloud-replicator-spp/module.tf

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ resource "azurerm_role_definition" "meshcloud_replicator" {
5353
]
5454
}
5555

56+
data "azuread_application_published_app_ids" "well_known" {}
57+
58+
resource "azuread_service_principal" "msgraph" {
59+
application_id = data.azuread_application_published_app_ids.well_known.result.MicrosoftGraph
60+
use_existing = true
61+
}
62+
5663
resource "azuread_application" "meshcloud_replicator" {
5764
display_name = "replicator.${var.spp_name_suffix}"
5865

@@ -62,20 +69,20 @@ resource "azuread_application" "meshcloud_replicator" {
6269
}
6370
}
6471
required_resource_access {
65-
resource_app_id = "00000003-0000-0000-c000-000000000000" # Microsoft Graph
72+
resource_app_id = data.azuread_application_published_app_ids.well_known.result.MicrosoftGraph
6673

6774
resource_access {
68-
id = "7ab1d382-f21e-4acd-a863-ba3e13f7da61" # Directory.Read.All
75+
id = azuread_service_principal.msgraph.app_role_ids["Directory.Read.All"]
6976
type = "Role"
7077
}
7178

7279
resource_access {
73-
id = "62a82d76-70ea-41e2-9197-370581804d09" # Group.ReadWrite.All
80+
id = azuread_service_principal.msgraph.app_role_ids["Group.ReadWrite.All"]
7481
type = "Role"
7582
}
7683

7784
resource_access {
78-
id = "09850681-111b-4a89-9bed-3f2cae46d706" # User.Invite.All
85+
id = azuread_service_principal.msgraph.app_role_ids["User.Invite.All"]
7986
type = "Role"
8087
}
8188
}
@@ -122,6 +129,24 @@ resource "azurerm_role_assignment" "meshcloud_replicator" {
122129
principal_id = azuread_service_principal.meshcloud_replicator.id
123130
}
124131

132+
resource "azuread_app_role_assignment" "meshcloud_replicator-directory" {
133+
app_role_id = azuread_service_principal.msgraph.app_role_ids["Directory.Read.All"]
134+
principal_object_id = azuread_service_principal.meshcloud_replicator.object_id
135+
resource_object_id = azuread_service_principal.msgraph.object_id
136+
}
137+
138+
resource "azuread_app_role_assignment" "meshcloud_replicator-group" {
139+
app_role_id = azuread_service_principal.msgraph.app_role_ids["Group.ReadWrite.All"]
140+
principal_object_id = azuread_service_principal.meshcloud_replicator.object_id
141+
resource_object_id = azuread_service_principal.msgraph.object_id
142+
}
143+
144+
resource "azuread_app_role_assignment" "meshcloud_replicator-user" {
145+
app_role_id = azuread_service_principal.msgraph.app_role_ids["User.Invite.All"]
146+
principal_object_id = azuread_service_principal.meshcloud_replicator.object_id
147+
resource_object_id = azuread_service_principal.msgraph.object_id
148+
}
149+
125150
resource "azuread_service_principal_password" "spp_pw" {
126151
service_principal_id = azuread_service_principal.meshcloud_replicator.id
127152
end_date = "2999-01-01T01:02:03Z" # no expiry

modules/meshcloud-replicator-spp/outputs.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
output "service_principal" {
22
value = {
3-
INFO = "On the very first time running tf apply, you have to run 'az ad app permission admin-consent --id ${azuread_service_principal.meshcloud_replicator.application_id}' or click the 'Grant Admin consent button' in the portal"
43
object_id = azuread_service_principal.meshcloud_replicator.id
54
app_id = azuread_service_principal.meshcloud_replicator.application_id
65
password = "Execute `terraform output replicator_spp_password` to see the password"

0 commit comments

Comments
 (0)