22#
33# https://argo-cd.readthedocs.io/en/stable/operator-manual/user-management/microsoft/#azure-ad-app-registration-auth-using-oidc
44
5- resource "random_password" "argocd" {
6- length = 32
7- special = false
8- keepers = {
9- service_principal = azuread_application.argocd.id
10- }
5+ # https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/data-sources/application_published_app_ids
6+ data "azuread_application_published_app_ids" "well_known" {}
7+
8+ resource "azuread_service_principal" "msgraph" {
9+ application_id = data . azuread_application_published_app_ids . well_known . result . MicrosoftGraph
10+ use_existing = true
1111}
1212
1313# https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application
1414resource "azuread_application" "argocd" {
1515 display_name = var. argocd_app_reg_name
16- identifier_uris = [" https://${ var . argocd_app_reg_name } " ]
16+ identifier_uris = [" https://${ var . argocd_fqdn } " ]
1717 sign_in_audience = " AzureADMyOrg"
18- group_membership_claims = " All"
18+ group_membership_claims = [ " All" ]
1919 prevent_duplicate_names = true
20+ logo_image = filebase64 (" ${ path . module } /files/argocd-logo.png" )
2021
2122 web {
2223 homepage_url = " https://${ var . argocd_fqdn } "
@@ -31,13 +32,20 @@ resource "azuread_application" "argocd" {
3132 # reference: https://github.com/mjisaak/azure-active-directory/blob/master/README.md#well-known-appids
3233 required_resource_access {
3334 # Microsoft Graph
34- resource_app_id = " 00000003-0000-0000-c000-000000000000 "
35+ resource_app_id = data . azuread_application_published_app_ids . well_known . result . MicrosoftGraph
3536
36- # User.Read - e1fe6dd8-ba31-4d61-89e7-88639da4683d - Sign in and read user profile
37+ # Oauth2Permissions are delegated permissions, type=Scope
3738 resource_access {
38- id = " e1fe6dd8-ba31-4d61-89e7-88639da4683d "
39+ id = azuread_service_principal . msgraph . oauth2_permission_scope_ids [ " User.Read " ]
3940 type = " Scope"
4041 }
42+
43+ # ? keeping for future reference
44+ # # application permissions, type=Role
45+ # resource_access {
46+ # id = azuread_service_principal.msgraph.app_role_ids["User.Read.All"]
47+ # type = "Role"
48+ # }
4149 }
4250
4351 optional_claims {
@@ -57,24 +65,26 @@ resource "azuread_application" "argocd" {
5765 }
5866}
5967
60- # TODO: add "SelfServiceAppAccess" tag to enable self-service options in Enterprise App
68+ data "azuread_client_config" "current" {}
69+
6170resource "azuread_service_principal" "argocd" {
62- application_id = azuread_application. argocd . application_id
71+ application_id = azuread_application. argocd . application_id
72+ owners = [data . azuread_client_config . current . object_id ]
73+ description = " Argo CD Service Principle"
74+ notes = " Operational notes can go here"
75+ preferred_single_sign_on_mode = " oidc"
76+ # tags = ["notApiConsumer", "webApp"]
6377}
6478
6579# https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application_password
6680resource "azuread_application_password" "argocd" {
6781 application_object_id = azuread_application. argocd . id
6882 display_name = " argocd_secret"
69- value = random_password. argocd . result
7083 end_date = " 2099-01-01T01:02:03Z"
71-
72- depends_on = [azuread_service_principal . argocd ]
7384}
7485
7586data "azurerm_client_config" "current" {}
7687
77-
7888# argocd-cm patch
7989# https://www.terraform.io/docs/provisioners/local-exec.html
8090resource "null_resource" "argocd_cm" {
@@ -115,7 +125,7 @@ resource "null_resource" "argocd_cm" {
115125resource "null_resource" "argocd_secret" {
116126 triggers = {
117127 yaml_contents = filemd5 (var. argocd_secret_yaml_path )
118- clientSecret = random_password .argocd.result
128+ clientSecret = azuread_application_password .argocd.value
119129 }
120130
121131 provisioner "local-exec" {
@@ -125,7 +135,7 @@ resource "null_resource" "argocd_secret" {
125135 ARGOCD_SECRET_PATCH_YAML = templatefile (
126136 var. argocd_secret_yaml_path ,
127137 {
128- " clientSecretBase64" = base64encode (random_password . argocd . result )
138+ " clientSecretBase64" = base64encode (azuread_application_password . argocd . value )
129139 }
130140 )
131141 }
0 commit comments