|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +resource "google_access_context_manager_service_perimeter_ingress_policy" "ingress_policies" { |
| 18 | + for_each = { for k, v in var.ingress_policies : k => v } |
| 19 | + |
| 20 | + perimeter = google_access_context_manager_service_perimeter.regular_service_perimeter.name |
| 21 | + title = "Ingress Policy ${each.key}" |
| 22 | + ingress_from { |
| 23 | + dynamic "sources" { |
| 24 | + for_each = merge( |
| 25 | + { for k, v in lookup(lookup(each.value["from"], "sources", {}), "resources", []) : v => "resource" }, |
| 26 | + { for k, v in lookup(lookup(each.value["from"], "sources", {}), "access_levels", []) : v => "access_level" }) |
| 27 | + content { |
| 28 | + resource = sources.value == "resource" ? sources.key : null |
| 29 | + access_level = sources.value == "access_level" ? sources.key != "*" ? "accessPolicies/${var.policy}/accessLevels/${sources.key}" : "*" : null |
| 30 | + } |
| 31 | + } |
| 32 | + identity_type = lookup(each.value["from"], "identity_type", null) |
| 33 | + identities = lookup(each.value["from"], "identities", null) |
| 34 | + } |
| 35 | + |
| 36 | + ingress_to { |
| 37 | + resources = lookup(each.value["to"], "resources", ["*"]) |
| 38 | + dynamic "operations" { |
| 39 | + for_each = lookup(each.value["to"], "operations", []) |
| 40 | + content { |
| 41 | + service_name = operations.key |
| 42 | + dynamic "method_selectors" { |
| 43 | + for_each = operations.key != "*" ? merge( |
| 44 | + { for v in lookup(operations.value, "methods", []) : v => "method" }, |
| 45 | + { for v in lookup(operations.value, "permissions", []) : v => "permission" }) : {} |
| 46 | + content { |
| 47 | + method = method_selectors.value == "method" ? method_selectors.key : null |
| 48 | + permission = method_selectors.value == "permission" ? method_selectors.key : null |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + lifecycle { |
| 55 | + create_before_destroy = true |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +resource "google_access_context_manager_service_perimeter_egress_policy" "egress_policies" { |
| 60 | + for_each = { for k, v in var.egress_policies : k => v } |
| 61 | + |
| 62 | + perimeter = google_access_context_manager_service_perimeter.regular_service_perimeter.name |
| 63 | + title = "Egress Policy ${each.key}" |
| 64 | + |
| 65 | + egress_from { |
| 66 | + identity_type = lookup(each.value["from"], "identity_type", null) |
| 67 | + identities = lookup(each.value["from"], "identities", null) |
| 68 | + dynamic "sources" { |
| 69 | + for_each = { for k, v in lookup(lookup(each.value["from"], "sources", {}), "access_levels", []) : v => "access_level" } |
| 70 | + content { |
| 71 | + access_level = sources.value == "access_level" ? sources.key != "*" ? "accessPolicies/${var.policy}/accessLevels/${sources.key}" : "*" : null |
| 72 | + } |
| 73 | + } |
| 74 | + source_restriction = lookup(each.value["from"], "sources", null) != null ? "SOURCE_RESTRICTION_ENABLED" : null |
| 75 | + } |
| 76 | + egress_to { |
| 77 | + resources = lookup(each.value["to"], "resources", ["*"]) |
| 78 | + external_resources = lookup(each.value["to"], "external_resources", []) |
| 79 | + dynamic "operations" { |
| 80 | + for_each = lookup(each.value["to"], "operations", []) |
| 81 | + content { |
| 82 | + service_name = operations.key |
| 83 | + dynamic "method_selectors" { |
| 84 | + for_each = operations.key != "*" ? merge( |
| 85 | + { for v in lookup(operations.value, "methods", []) : v => "method" }, |
| 86 | + { for v in lookup(operations.value, "permissions", []) : v => "permission" }) : {} |
| 87 | + content { |
| 88 | + method = method_selectors.value == "method" ? method_selectors.key : null |
| 89 | + permission = method_selectors.value == "permission" ? method_selectors.key : null |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + lifecycle { |
| 96 | + create_before_destroy = true |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +############################################ |
| 101 | +# DRY-RUN POLICIES # |
| 102 | +############################################ |
| 103 | + |
| 104 | +resource "google_access_context_manager_service_perimeter_dry_run_ingress_policy" "ingress_policies" { |
| 105 | + for_each = { for k, v in var.ingress_policies_dry_run : k => v } |
| 106 | + |
| 107 | + perimeter = google_access_context_manager_service_perimeter.regular_service_perimeter.name |
| 108 | + title = "Ingress Policy ${each.key}" |
| 109 | + ingress_from { |
| 110 | + dynamic "sources" { |
| 111 | + for_each = merge( |
| 112 | + { for k, v in lookup(lookup(each.value["from"], "sources", {}), "resources", []) : v => "resource" }, |
| 113 | + { for k, v in lookup(lookup(each.value["from"], "sources", {}), "access_levels", []) : v => "access_level" }) |
| 114 | + content { |
| 115 | + resource = sources.value == "resource" ? sources.key : null |
| 116 | + access_level = sources.value == "access_level" ? sources.key != "*" ? "accessPolicies/${var.policy}/accessLevels/${sources.key}" : "*" : null |
| 117 | + } |
| 118 | + } |
| 119 | + identity_type = lookup(each.value["from"], "identity_type", null) |
| 120 | + identities = lookup(each.value["from"], "identities", null) |
| 121 | + } |
| 122 | + |
| 123 | + ingress_to { |
| 124 | + resources = lookup(each.value["to"], "resources", ["*"]) |
| 125 | + dynamic "operations" { |
| 126 | + for_each = lookup(each.value["to"], "operations", []) |
| 127 | + content { |
| 128 | + service_name = operations.key |
| 129 | + dynamic "method_selectors" { |
| 130 | + for_each = operations.key != "*" ? merge( |
| 131 | + { for v in lookup(operations.value, "methods", []) : v => "method" }, |
| 132 | + { for v in lookup(operations.value, "permissions", []) : v => "permission" }) : {} |
| 133 | + content { |
| 134 | + method = method_selectors.value == "method" ? method_selectors.key : null |
| 135 | + permission = method_selectors.value == "permission" ? method_selectors.key : null |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + lifecycle { |
| 142 | + create_before_destroy = true |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +resource "google_access_context_manager_service_perimeter_dry_run_egress_policy" "egress_policies" { |
| 147 | + for_each = { for k, v in var.egress_policies_dry_run : k => v } |
| 148 | + |
| 149 | + perimeter = google_access_context_manager_service_perimeter.regular_service_perimeter.name |
| 150 | + title = "Egress Policy ${each.key}" |
| 151 | + |
| 152 | + egress_from { |
| 153 | + identity_type = lookup(each.value["from"], "identity_type", null) |
| 154 | + identities = lookup(each.value["from"], "identities", null) |
| 155 | + dynamic "sources" { |
| 156 | + for_each = { for k, v in lookup(lookup(each.value["from"], "sources", {}), "access_levels", []) : v => "access_level" } |
| 157 | + content { |
| 158 | + access_level = sources.value == "access_level" ? sources.key != "*" ? "accessPolicies/${var.policy}/accessLevels/${sources.key}" : "*" : null |
| 159 | + } |
| 160 | + } |
| 161 | + source_restriction = lookup(each.value["from"], "sources", null) != null ? "SOURCE_RESTRICTION_ENABLED" : null |
| 162 | + } |
| 163 | + egress_to { |
| 164 | + resources = lookup(each.value["to"], "resources", ["*"]) |
| 165 | + dynamic "operations" { |
| 166 | + for_each = lookup(each.value["to"], "operations", []) |
| 167 | + content { |
| 168 | + service_name = operations.key |
| 169 | + dynamic "method_selectors" { |
| 170 | + for_each = operations.key != "*" ? merge( |
| 171 | + { for v in lookup(operations.value, "methods", []) : v => "method" }, |
| 172 | + { for v in lookup(operations.value, "permissions", []) : v => "permission" }) : {} |
| 173 | + content { |
| 174 | + method = method_selectors.value == "method" ? method_selectors.key : null |
| 175 | + permission = method_selectors.value == "permission" ? method_selectors.key : null |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + lifecycle { |
| 182 | + create_before_destroy = true |
| 183 | + } |
| 184 | +} |
0 commit comments