|
| 1 | +#---------------------------------------------------------- |
| 2 | +# Fetch & compute required data for organizational install |
| 3 | +#---------------------------------------------------------- |
| 4 | + |
| 5 | +data "aws_organizations_organization" "org" { |
| 6 | + count = var.is_organizational ? 1 : 0 |
| 7 | +} |
| 8 | + |
| 9 | +locals { |
| 10 | + # check if both old and new org parameters are provided, we fail early |
| 11 | + both_org_configuration_params = var.is_organizational && length(var.org_units) > 0 && ( |
| 12 | + length(var.include_ouids) > 0 || |
| 13 | + length(var.exclude_ouids) > 0 || |
| 14 | + length(var.include_accounts) > 0 || |
| 15 | + length(var.exclude_accounts) > 0 |
| 16 | + ) |
| 17 | + |
| 18 | + # check if old org_units parameter is provided, for backwards compatibility we will always give preference to it |
| 19 | + check_old_ouid_param = var.is_organizational && length(var.org_units) > 0 |
| 20 | + |
| 21 | + # fetch the AWS Root OU under org |
| 22 | + # As per https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html#organization-structure, there can be only one root |
| 23 | + root_org_unit = var.is_organizational ? [for root in data.aws_organizations_organization.org[0].roots : root.id] : [] |
| 24 | +} |
| 25 | + |
| 26 | +check "validate_org_configuration_params" { |
| 27 | + assert { |
| 28 | + condition = length(var.org_units) == 0 # if this condition is false we throw warning |
| 29 | + error_message = <<-EOT |
| 30 | + WARNING: TO BE DEPRECATED 'org_units': Please work with Sysdig to migrate your Terraform installs to use 'include_ouids' instead. |
| 31 | + EOT |
| 32 | + } |
| 33 | + |
| 34 | + assert { |
| 35 | + condition = !local.both_org_configuration_params # if this condition is false we throw error |
| 36 | + error_message = <<-EOT |
| 37 | + ERROR: If both org_units and include_ouids/exclude_ouids/include_accounts/exclude_accounts variables are populated, |
| 38 | + ONLY org_units will be considered. Please use only one of the two methods. |
| 39 | +
|
| 40 | + Note: org_units is going to be DEPRECATED soon, please work with Sysdig to migrate your Terraform installs. |
| 41 | + EOT |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +# ***************************************************************************************************************************************************** |
| 46 | +# INCLUDE/EXCLUDE CONFIGURATION SUPPORT |
| 47 | +# |
| 48 | +# 1. Inclusions will always be handled for TF cloud provisioning. |
| 49 | +# NOTE: |
| 50 | +# Till AWS issue with UNION filter (https://github.com/aws-cloudformation/aws-cloudformation-resource-providers-cloudformation/issues/100) |
| 51 | +# is fixed, we can't deploy using UNION filters for inclusions. As a workaround to ensure we don't skip any accounts, we deploy it to entire org. |
| 52 | +# |
| 53 | +# 2. We handle exclusions when only exclusion parameters are provided i.e out of all 4 configuration inputs, |
| 54 | +# a. only exclude_ouids are provided, OR |
| 55 | +# b. only exclude_accounts are provided, OR |
| 56 | +# c. only exclude_ouids AND exclude_accounts are provided |
| 57 | +# Else we ignore exclusions during cloud resource provisioning through TF. This is because AWS does not allow both operations - to include some |
| 58 | +# accounts and to exclude some. Hence, we will always prioritize include over exclude. |
| 59 | +# |
| 60 | +# 3. Sysdig however will honor all combinations of configuration inputs exactly as desired. |
| 61 | +# ***************************************************************************************************************************************************** |
| 62 | + |
| 63 | +#------------------------------------------------------------ |
| 64 | +# Manage configurations to determine OU targets to deploy in |
| 65 | +#------------------------------------------------------------ |
| 66 | + |
| 67 | +locals { |
| 68 | + # OU CONFIGURATION (determine user provided org configuration) |
| 69 | + org_configuration = ( |
| 70 | + # case1 - if old method is used where ONLY org_units is provided, use those |
| 71 | + local.check_old_ouid_param ? ( |
| 72 | + "old_ouid_param" |
| 73 | + ) : ( |
| 74 | + # case2 - if no include/exclude ous provided, include entire org |
| 75 | + var.is_organizational && length(var.include_ouids) == 0 && length(var.exclude_ouids) == 0 ? ( |
| 76 | + "entire_org" |
| 77 | + ) : ( |
| 78 | + # case3 - if only included ouids provided, include those ous only |
| 79 | + var.is_organizational && length(var.include_ouids) > 0 && length(var.exclude_ouids) == 0 ? ( |
| 80 | + "included_ous_only" |
| 81 | + ) : ( |
| 82 | + # case4 - if only excluded ouids provided, exclude their accounts from rest of org |
| 83 | + var.is_organizational && length(var.include_ouids) == 0 && length(var.exclude_ouids) > 0 ? ( |
| 84 | + "excluded_ous_only" |
| 85 | + ) : ( |
| 86 | + # case5 - if both include and exclude ouids are provided, includes override excludes |
| 87 | + var.is_organizational && length(var.include_ouids) > 0 && length(var.exclude_ouids) > 0 ? ( |
| 88 | + "mixed_ous" |
| 89 | + ) : "" |
| 90 | + ) |
| 91 | + ) |
| 92 | + ) |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + # switch cases for various user provided org configuration to be onboarded |
| 97 | + deployment_options = { |
| 98 | + old_ouid_param = { |
| 99 | + org_units_to_deploy = var.org_units |
| 100 | + } |
| 101 | + entire_org = { |
| 102 | + org_units_to_deploy = local.root_org_unit |
| 103 | + } |
| 104 | + included_ous_only = { |
| 105 | + org_units_to_deploy = var.include_ouids |
| 106 | + } |
| 107 | + excluded_ous_only = { |
| 108 | + # onboard entire org and filter out all accounts in excluded OUs using account filter |
| 109 | + org_units_to_deploy = local.root_org_unit |
| 110 | + } |
| 111 | + mixed_ous = { |
| 112 | + # if both include and exclude ouids are provided, includes override excludes |
| 113 | + org_units_to_deploy = var.include_ouids |
| 114 | + } |
| 115 | + default = { |
| 116 | + org_units_to_deploy = local.root_org_unit |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + # final targets to deploy organizational resources in |
| 121 | + deployment_targets_ous = lookup(local.deployment_options, local.org_configuration, local.deployment_options.default) |
| 122 | + |
| 123 | + exclude_root_ou = length(local.root_org_unit) > 0 ? contains(var.exclude_ouids, local.root_org_unit[0]) : false |
| 124 | +} |
| 125 | + |
| 126 | +#----------------------------------------------------------------- |
| 127 | +# Manage configurations to determine account targets to deploy in |
| 128 | +#----------------------------------------------------------------- |
| 129 | + |
| 130 | +# if only exclude_ouids are provided and as long as it isn't Root OU, fetch all their child accounts to filter exclusions |
| 131 | +data "aws_organizations_organizational_unit_descendant_accounts" "ou_accounts_to_exclude" { |
| 132 | + for_each = local.org_configuration == "excluded_ous_only" && !local.exclude_root_ou ? var.exclude_ouids : [] |
| 133 | + parent_id = each.key |
| 134 | +} |
| 135 | + |
| 136 | +locals { |
| 137 | + # ACCOUNTS CONFIGURATION (determine user provided accounts configuration) |
| 138 | + accounts_configuration = ( |
| 139 | + # case1 - if old method is used where ONLY org_units is provided, this configuration is a noop |
| 140 | + local.check_old_ouid_param ? ( |
| 141 | + "NONE" |
| 142 | + ) : ( |
| 143 | + # case2 - if only included accounts provided, include those accts as well |
| 144 | + var.is_organizational && length(var.include_accounts) > 0 && length(var.exclude_accounts) == 0 ? ( |
| 145 | + "UNION" |
| 146 | + ) : ( |
| 147 | + # case3 - if only excluded accounts or only excluded ouids provided, exclude those accounts |
| 148 | + var.is_organizational && length(var.include_accounts) == 0 && ( length(var.exclude_accounts) > 0 || local.org_configuration == "excluded_ous_only" ) ? ( |
| 149 | + "DIFFERENCE" |
| 150 | + ) : ( |
| 151 | + # case4 - if both include and exclude accounts are provided, includes override excludes |
| 152 | + var.is_organizational && length(var.include_accounts) > 0 && length(var.exclude_accounts) > 0 ? ( |
| 153 | + "MIXED" |
| 154 | + ) : "" |
| 155 | + ) |
| 156 | + ) |
| 157 | + ) |
| 158 | + ) |
| 159 | + |
| 160 | + ou_accounts_to_exclude = flatten([ for ou_accounts in data.aws_organizations_organizational_unit_descendant_accounts.ou_accounts_to_exclude: [ ou_accounts.accounts[*].id ] ]) |
| 161 | + accounts_to_exclude = setunion(local.ou_accounts_to_exclude, var.exclude_accounts) |
| 162 | + |
| 163 | + # switch cases for various user provided accounts configuration to be onboarded |
| 164 | + deployment_account_options = { |
| 165 | + NONE = { |
| 166 | + accounts_to_deploy = [] |
| 167 | + account_filter_type = "NONE" |
| 168 | + } |
| 169 | + UNION = { |
| 170 | + accounts_to_deploy = var.include_accounts |
| 171 | + account_filter_type = "UNION" |
| 172 | + } |
| 173 | + DIFFERENCE = { |
| 174 | + accounts_to_deploy = local.accounts_to_exclude |
| 175 | + account_filter_type = "DIFFERENCE" |
| 176 | + } |
| 177 | + MIXED = { |
| 178 | + accounts_to_deploy = var.include_accounts |
| 179 | + account_filter_type = "UNION" |
| 180 | + } |
| 181 | + default = { |
| 182 | + # default when neither of include/exclude accounts are provided |
| 183 | + accounts_to_deploy = [] |
| 184 | + account_filter_type = "NONE" |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + # list of accounts to deploy organizational resources in |
| 189 | + deployment_targets_accounts = lookup(local.deployment_account_options, local.accounts_configuration, local.deployment_account_options.default) |
| 190 | +} |
| 191 | + |
| 192 | +# ----------------------------------------------------------------------------------------------------- |
| 193 | +# Remove below conditional once AWS issue is fixed - |
| 194 | +# https://github.com/aws-cloudformation/aws-cloudformation-resource-providers-cloudformation/issues/100 |
| 195 | +# ----------------------------------------------------------------------------------------------------- |
| 196 | +locals { |
| 197 | + # XXX: due to AWS bug of not having UNION filter fully working, there is no way to add those extra accounts requested. |
| 198 | + # to not miss out on those extra accounts, deploy the cloud resources across entire org and noop the UNION filter. |
| 199 | + # i.e till we can't deploy UNION, we deploy it all |
| 200 | + deployment_targets_org_units = local.deployment_targets_accounts.account_filter_type == "UNION" ? local.root_org_unit : local.deployment_targets_ous.org_units_to_deploy |
| 201 | + deployment_targets_accounts_filter = local.deployment_targets_accounts.account_filter_type == "UNION" ? "NONE" : local.deployment_targets_accounts.account_filter_type |
| 202 | +} |
0 commit comments