main.tf 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ### Datasource
  2. data "yandex_client_config" "client" {}
  3. locals {
  4. folder_id = var.folder_id == null ? data.yandex_client_config.client.folder_id : var.folder_id
  5. cloud_id = var.cloud_id == null ? data.yandex_client_config.client.cloud_id : var.cloud_id
  6. #org_id = var.org_id == null ? data.yandex_client_config.client.organization_id : var.org_id
  7. org_id = var.org_id
  8. }
  9. ### SA
  10. resource "yandex_iam_service_account" "sa" {
  11. for_each = { for v in var.sa_role_mapping : v.name => v }
  12. name = each.key
  13. folder_id = local.folder_id
  14. }
  15. ###Folder Permissions
  16. data "yandex_organizationmanager_saml_federation_user_account" "folder_account" {
  17. for_each = toset(flatten([for v in var.folder_user_role_mapping : v.fed_users_names if var.federation_id != null && var.usernames_to_ids == true]))
  18. federation_id = var.federation_id
  19. name_id = each.key
  20. }
  21. data "yandex_iam_user" "folder_account" {
  22. for_each = toset(flatten([for v in var.folder_user_role_mapping : v.iam_users_names if var.usernames_to_ids == true]))
  23. login = each.key
  24. }
  25. locals {
  26. sa_role_mapping = { for v in var.sa_role_mapping : v.name => v }
  27. sa_mappings = chunklist(flatten([for k, v in yandex_iam_service_account.sa : setproduct([v.id], local.sa_role_mapping[v.name].roles)]), 2)
  28. folder_fed_users_names = { for b in var.folder_user_role_mapping : b.job_title_name => flatten([for key, value in data.yandex_organizationmanager_saml_federation_user_account.folder_account : "federatedUser:${value.id}" if contains(b.fed_users_names, value.name_id)]) }
  29. folder_iam_users_names = { for b in var.folder_user_role_mapping : b.job_title_name => flatten([for key, value in data.yandex_iam_user.folder_account : "userAccount:${value.id}" if contains(b.iam_users_names, value.login)]) }
  30. folder_users_with_ids = { for b in var.folder_user_role_mapping : b.job_title_name => b.users_with_ids }
  31. folder_fed_user_mappings = flatten([for v in var.folder_user_role_mapping : setproduct(local.folder_fed_users_names[v.job_title_name], v.roles)])
  32. folder_iam_user_mappings = flatten([for v in var.folder_user_role_mapping : setproduct(local.folder_iam_users_names[v.job_title_name], v.roles)])
  33. folder_id_user_mappings = flatten([for v in var.folder_user_role_mapping : setproduct(local.folder_users_with_ids[v.job_title_name], v.roles)])
  34. folder_user_mappings = distinct(chunklist(concat(local.folder_fed_user_mappings, local.folder_iam_user_mappings, local.folder_id_user_mappings), 2))
  35. }
  36. #### Authoritative
  37. data "yandex_iam_policy" "bindings" {
  38. count = var.folder_binding_authoritative == false ? 0 : 1
  39. dynamic "binding" {
  40. for_each = [for v in local.folder_user_mappings : {
  41. member = v[0],
  42. role = v[1]
  43. }]
  44. content {
  45. role = binding.value.role
  46. members = [binding.value.member, ]
  47. }
  48. }
  49. dynamic "binding" {
  50. for_each = [for v in local.sa_mappings : {
  51. member = v[0],
  52. role = v[1]
  53. }]
  54. content {
  55. role = binding.value.role
  56. members = ["serviceAccount:${binding.value.member}", ]
  57. }
  58. }
  59. }
  60. resource "yandex_resourcemanager_folder_iam_policy" "folder_bindings_policy" {
  61. count = var.folder_binding_authoritative == false ? 0 : 1
  62. folder_id = local.folder_id
  63. policy_data = data.yandex_iam_policy.bindings[0].policy_data
  64. }
  65. #### NON-Authoritative
  66. resource "yandex_resourcemanager_folder_iam_member" "folder_sa_member" {
  67. count = var.folder_binding_authoritative == false ? length(local.sa_mappings) : 0
  68. folder_id = local.folder_id
  69. member = "serviceAccount:${element(local.sa_mappings, count.index)[0]}"
  70. role = element(local.sa_mappings, count.index)[1]
  71. }
  72. resource "yandex_resourcemanager_folder_iam_member" "folder_user_member" {
  73. count = var.folder_binding_authoritative == false ? length(local.folder_user_mappings) : 0
  74. folder_id = local.folder_id
  75. member = element(local.folder_user_mappings, count.index)[0]
  76. role = element(local.folder_user_mappings, count.index)[1]
  77. }
  78. ### Cloud Permissions
  79. data "yandex_organizationmanager_saml_federation_user_account" "cloud_account" {
  80. for_each = toset(flatten([for v in var.cloud_user_role_mapping : v.fed_users_names if var.federation_id != null && var.usernames_to_ids == true]))
  81. federation_id = var.federation_id
  82. name_id = each.key
  83. }
  84. data "yandex_iam_user" "cloud_account" {
  85. for_each = toset(flatten([for v in var.cloud_user_role_mapping : v.iam_users_names if var.usernames_to_ids == true]))
  86. login = each.key
  87. }
  88. locals {
  89. cloud_fed_users_names = { for b in var.cloud_user_role_mapping : b.job_title_name => flatten([for key, value in data.yandex_organizationmanager_saml_federation_user_account.cloud_account : "federatedUser:${value.id}" if contains(b.fed_users_names, value.name_id)]) }
  90. cloud_iam_users_names = { for b in var.cloud_user_role_mapping : b.job_title_name => flatten([for key, value in data.yandex_iam_user.cloud_account : "userAccount:${value.id}" if contains(b.iam_users_names, value.login)]) }
  91. cloud_users_with_ids = { for b in var.cloud_user_role_mapping : b.job_title_name => b.users_with_ids }
  92. cloud_fed_user_mappings = flatten([for v in var.cloud_user_role_mapping : setproduct(local.cloud_fed_users_names[v.job_title_name], v.roles)])
  93. cloud_iam_user_mappings = flatten([for v in var.cloud_user_role_mapping : setproduct(local.cloud_iam_users_names[v.job_title_name], v.roles)])
  94. cloud_id_user_mappings = flatten([for v in var.cloud_user_role_mapping : setproduct(local.cloud_users_with_ids[v.job_title_name], v.roles)])
  95. cloud_user_mappings = distinct(chunklist(concat(local.cloud_fed_user_mappings, local.cloud_iam_user_mappings, local.cloud_id_user_mappings), 2))
  96. }
  97. #### Authoritative
  98. resource "yandex_resourcemanager_cloud_iam_binding" "cloud_binding" {
  99. for_each = { for v in local.cloud_user_mappings : v[1] => v[0]... if var.cloud_binding_authoritative == true }
  100. cloud_id = local.cloud_id
  101. members = each.value
  102. role = each.key
  103. }
  104. #### NON-Authoritative
  105. resource "yandex_resourcemanager_cloud_iam_member" "cloud_member" {
  106. count = var.cloud_binding_authoritative == false ? length(local.cloud_user_mappings) : 0
  107. cloud_id = local.cloud_id
  108. member = element(local.cloud_user_mappings, count.index)[0]
  109. role = element(local.cloud_user_mappings, count.index)[1]
  110. }
  111. ### Organization Permissions
  112. data "yandex_organizationmanager_saml_federation_user_account" "org_account" {
  113. for_each = toset(flatten([for v in var.org_user_role_mapping : v.fed_users_names if var.federation_id != null && var.usernames_to_ids == true]))
  114. federation_id = var.federation_id
  115. name_id = each.key
  116. }
  117. data "yandex_iam_user" "org_account" {
  118. for_each = toset(flatten([for v in var.org_user_role_mapping : v.iam_users_names if var.usernames_to_ids == true]))
  119. login = each.key
  120. }
  121. locals {
  122. ##### {job=[ids]}
  123. org_fed_users_names = { for b in var.org_user_role_mapping : b.job_title_name => flatten([for key, value in data.yandex_organizationmanager_saml_federation_user_account.org_account : "federatedUser:${value.id}" if contains(b.fed_users_names, value.name_id)]) }
  124. org_iam_users_names = { for b in var.org_user_role_mapping : b.job_title_name => flatten([for key, value in data.yandex_iam_user.org_account : "userAccount:${value.id}" if contains(b.iam_users_names, value.login)]) }
  125. org_users_with_ids = { for b in var.org_user_role_mapping : b.job_title_name => b.users_with_ids }
  126. #####[id-role pairs] per type
  127. org_fed_user_mappings = flatten([for v in var.org_user_role_mapping : setproduct(local.org_fed_users_names[v.job_title_name], v.roles)])
  128. org_iam_user_mappings = flatten([for v in var.org_user_role_mapping : setproduct(local.org_iam_users_names[v.job_title_name], v.roles)])
  129. org_id_user_mappings = flatten([for v in var.org_user_role_mapping : setproduct(local.org_users_with_ids[v.job_title_name], v.roles)])
  130. #####list[pairs]
  131. org_user_mappings = distinct(chunklist(concat(local.org_fed_user_mappings, local.org_iam_user_mappings, local.org_id_user_mappings), 2))
  132. }
  133. #### Authoritative
  134. resource "yandex_organizationmanager_organization_iam_binding" "org_binding" {
  135. for_each = { for v in local.org_user_mappings : v[1] => v[0]... if var.org_binding_authoritative == true }
  136. organization_id = local.org_id
  137. members = each.value
  138. role = each.key
  139. }
  140. #### NON-Authoritative
  141. resource "yandex_organizationmanager_organization_iam_member" "org_member" {
  142. count = var.org_binding_authoritative == false ? length(local.org_user_mappings) : 0
  143. organization_id = local.org_id
  144. member = element(local.org_user_mappings, count.index)[0]
  145. role = element(local.org_user_mappings, count.index)[1]
  146. }