forked from lacework/terraform-azure-ad-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
52 lines (46 loc) · 2.04 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
locals {
application_id = var.create ? (
length(azuread_application.lacework) > 0 ? azuread_application.lacework[0].application_id : ""
) : ""
application_password = var.create ? (
length(azuread_application_password.client_secret) > 0 ? azuread_application_password.client_secret[0].value : ""
) : ""
service_principal_id = var.create ? (
length(azuread_service_principal.lacework) > 0 ? azuread_service_principal.lacework[0].object_id : ""
) : ""
}
data "azuread_client_config" "current" {}
## Create a service principal and assigned Directory Reader role in Azure AD
resource "azuread_application" "lacework" {
count = var.create ? 1 : 0
display_name = var.application_name
owners = length(var.application_owners) == 0 ? [data.azuread_client_config.current.object_id] : var.application_owners
logo_image = filebase64("${path.module}/imgs/lacework_logo.png")
marketing_url = "https://www.lacework.com/"
web {
homepage_url = "https://www.lacework.com/"
}
}
resource "azuread_directory_role" "dir_reader" {
display_name = "Directory Readers"
}
resource "azuread_service_principal" "lacework" {
count = var.create ? 1 : 0
application_id = local.application_id
}
resource "azuread_application_password" "client_secret" {
count = var.create ? 1 : 0
application_object_id = azuread_application.lacework[count.index].object_id
end_date = "2299-12-31T01:02:03Z"
depends_on = [azuread_service_principal.lacework]
}
# When to use this role? When Granting service principals access to directory where
# Directory.Read.All is not an option. This way we avoid Grant Admin Consent issue.
#
# => https://docs.microsoft.com/en-us/azure/active-directory/roles/permissions-reference#directory-readers
resource "azuread_directory_role_member" "lacework_dir_reader" {
count = var.create ? 1 : 0
role_object_id = azuread_directory_role.dir_reader.object_id
member_object_id = local.service_principal_id
depends_on = [azuread_service_principal.lacework]
}