|
| 1 | +# Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 3 | +# |
| 4 | + |
| 5 | +resource "oci_identity_dynamic_group" "app_dynamic_group" { |
| 6 | + name = "${local.app_name_normalized}-kms-dg-${var.app_details.app_deployment_id}" |
| 7 | + description = "${var.app_details.app_name} KMS for OKE Dynamic Group (${var.app_details.app_deployment_id})" |
| 8 | + compartment_id = var.tenancy_ocid |
| 9 | + matching_rule = "ANY {${join(",", local.dynamic_group_matching_rules)}}" |
| 10 | + |
| 11 | + provider = oci.home_region |
| 12 | + |
| 13 | + count = var.create_dynamic_group_for_nodes_in_compartment ? 1 : 0 |
| 14 | +} |
| 15 | +resource "oci_identity_policy" "app_compartment_policies" { |
| 16 | + name = "${local.app_name_normalized}-kms-compartment-policies-${var.app_details.app_deployment_id}" |
| 17 | + description = "${var.app_details.app_name} KMS for OKE Compartment Policies (${var.app_details.app_deployment_id})" |
| 18 | + compartment_id = var.oke_cluster_compartment_ocid |
| 19 | + statements = local.app_compartment_statements |
| 20 | + |
| 21 | + depends_on = [oci_identity_dynamic_group.app_dynamic_group] |
| 22 | + |
| 23 | + provider = oci.home_region |
| 24 | + |
| 25 | + count = var.create_compartment_policies ? 1 : 0 |
| 26 | +} |
| 27 | +resource "oci_identity_policy" "kms_user_group_compartment_policies" { |
| 28 | + name = "${local.app_name_normalized}-kms-compartment-policies-${var.app_details.app_deployment_id}" |
| 29 | + description = "${var.app_details.app_name} KMS User Group Compartment Policies (${var.app_details.app_deployment_id})" |
| 30 | + compartment_id = var.oke_cluster_compartment_ocid |
| 31 | + statements = local.kms_user_group_compartment_statements |
| 32 | + |
| 33 | + depends_on = [oci_identity_dynamic_group.app_dynamic_group] |
| 34 | + |
| 35 | + provider = oci.home_region |
| 36 | + |
| 37 | + count = (var.create_compartment_policies && var.create_vault_policies_for_group) ? 1 : 0 |
| 38 | +} |
| 39 | + |
| 40 | +# Concat Matching Rules and Policy Statements |
| 41 | +locals { |
| 42 | + dynamic_group_matching_rules = concat( |
| 43 | + local.instances_in_compartment_rule, |
| 44 | + local.clusters_in_compartment_rule |
| 45 | + ) |
| 46 | + app_compartment_statements = concat( |
| 47 | + var.use_encryption_from_oci_vault ? local.allow_oke_use_oci_vault_keys_statements : [] |
| 48 | + ) |
| 49 | + kms_user_group_compartment_statements = concat( |
| 50 | + local.allow_group_manage_vault_keys_statements |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +# Individual Rules |
| 55 | +locals { |
| 56 | + instances_in_compartment_rule = ["ALL {instance.compartment.id = '${var.oke_cluster_compartment_ocid}'}"] |
| 57 | + clusters_in_compartment_rule = ["ALL {resource.type = 'cluster', resource.compartment.id = '${var.oke_cluster_compartment_ocid}'}"] |
| 58 | +} |
| 59 | + |
| 60 | +# Individual Policy Statements |
| 61 | +locals { |
| 62 | + allow_oke_use_oci_vault_keys_statements = [ |
| 63 | + "Allow service oke to use vaults in compartment id ${var.oke_cluster_compartment_ocid}", |
| 64 | + "Allow service oke to use keys in compartment id ${var.oke_cluster_compartment_ocid} where target.key.id = '${local.oci_vault_key_id}'", |
| 65 | + "Allow service oke to use key-delegates in compartment id ${var.oke_cluster_compartment_ocid} where target.key.id = '${local.oci_vault_key_id}'", |
| 66 | + "Allow service blockstorage to use keys in compartment id ${var.oke_cluster_compartment_ocid} where target.key.id = '${local.oci_vault_key_id}'", |
| 67 | + "Allow dynamic-group ${local.app_dynamic_group} to use keys in compartment id ${var.oke_cluster_compartment_ocid} where target.key.id = '${local.oci_vault_key_id}'", |
| 68 | + "Allow dynamic-group ${local.app_dynamic_group} to use key-delegates in compartment id ${var.oke_cluster_compartment_ocid} where target.key.id = '${local.oci_vault_key_id}'" |
| 69 | + ] |
| 70 | + allow_group_manage_vault_keys_statements = [ |
| 71 | + "Allow group ${var.user_admin_group_for_vault_policy} to manage vaults in compartment id ${var.oke_cluster_compartment_ocid}", |
| 72 | + "Allow group ${var.user_admin_group_for_vault_policy} to manage keys in compartment id ${var.oke_cluster_compartment_ocid}", |
| 73 | + "Allow group ${var.user_admin_group_for_vault_policy} to use key-delegate in compartment id ${var.oke_cluster_compartment_ocid}" |
| 74 | + ] |
| 75 | +} |
| 76 | + |
| 77 | +# Conditional locals |
| 78 | +locals { |
| 79 | + app_dynamic_group = var.create_dynamic_group_for_nodes_in_compartment ? oci_identity_dynamic_group.app_dynamic_group.0.name : "void" |
| 80 | + app_name_normalized = var.app_details.app_name_normalized |
| 81 | +} |
0 commit comments