|
| 1 | +/** |
| 2 | + * Copyright 2023 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 | +locals { |
| 18 | + prefix = "hierarchical" |
| 19 | +} |
| 20 | + |
| 21 | +resource "random_string" "random_suffix" { |
| 22 | + length = 6 |
| 23 | + special = false |
| 24 | + lower = true |
| 25 | + upper = false |
| 26 | +} |
| 27 | + |
| 28 | +resource "google_service_account" "service_account" { |
| 29 | + project = var.project_id |
| 30 | + account_id = "${local.prefix}-fw-test-svc-acct" |
| 31 | + display_name = "${local.prefix} firewall policy test service account" |
| 32 | +} |
| 33 | + |
| 34 | +resource "google_compute_network" "network" { |
| 35 | + project = var.project_id |
| 36 | + name = "${local.prefix}-network" |
| 37 | +} |
| 38 | + |
| 39 | +resource "google_compute_network" "network_backup" { |
| 40 | + project = var.project_id |
| 41 | + name = "${local.prefix}-network-backup" |
| 42 | +} |
| 43 | + |
| 44 | +module "firewal_policy" { |
| 45 | + source = "terraform-google-modules/network/google//modules/hierarchical-firewall-policy" |
| 46 | + version = "~> 9.0" |
| 47 | + |
| 48 | + parent_node = "folders/${var.folder1}" |
| 49 | + policy_name = "${local.prefix}-firewall-policy-${random_string.random_suffix.result}" |
| 50 | + description = "test ${local.prefix} firewall policy" |
| 51 | + target_org = var.org_id |
| 52 | + target_folders = [var.folder2, var.folder3] |
| 53 | + |
| 54 | + rules = [ |
| 55 | + { |
| 56 | + priority = "1" |
| 57 | + direction = "INGRESS" |
| 58 | + action = "allow" |
| 59 | + rule_name = "ingress-1" |
| 60 | + description = "test ingres rule 1" |
| 61 | + enable_logging = true |
| 62 | + match = { |
| 63 | + src_ip_ranges = ["10.100.0.1/32"] |
| 64 | + src_fqdns = ["example.com"] |
| 65 | + src_region_codes = ["US"] |
| 66 | + src_threat_intelligences = ["iplist-public-clouds"] |
| 67 | + layer4_configs = [ |
| 68 | + { |
| 69 | + ip_protocol = "all" |
| 70 | + }, |
| 71 | + ] |
| 72 | + } |
| 73 | + }, |
| 74 | + { |
| 75 | + priority = "2" |
| 76 | + direction = "INGRESS" |
| 77 | + action = "deny" |
| 78 | + rule_name = "ingress-2" |
| 79 | + disabled = true |
| 80 | + description = "test ingres rule 2" |
| 81 | + target_resources = [ |
| 82 | + "projects/${var.project_id}/global/networks/${local.prefix}-network-backup", |
| 83 | + ] |
| 84 | + match = { |
| 85 | + src_ip_ranges = ["10.100.0.2/32"] |
| 86 | + src_fqdns = ["example.org"] |
| 87 | + src_region_codes = ["BE"] |
| 88 | + layer4_configs = [ |
| 89 | + { |
| 90 | + ip_protocol = "all" |
| 91 | + }, |
| 92 | + ] |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + priority = "3" |
| 97 | + direction = "INGRESS" |
| 98 | + action = "allow" |
| 99 | + rule_name = "ingress-3" |
| 100 | + disabled = true |
| 101 | + description = "test ingres rule 3" |
| 102 | + enable_logging = true |
| 103 | + target_service_accounts = ["fw-test-svc-acct@${var.project_id}.iam.gserviceaccount.com"] |
| 104 | + match = { |
| 105 | + src_ip_ranges = ["10.100.0.3/32"] |
| 106 | + dest_ip_ranges = ["10.100.0.103/32"] |
| 107 | + layer4_configs = [ |
| 108 | + { |
| 109 | + ip_protocol = "tcp" |
| 110 | + ports = ["80"] |
| 111 | + }, |
| 112 | + ] |
| 113 | + } |
| 114 | + }, |
| 115 | + { |
| 116 | + priority = "101" |
| 117 | + direction = "EGRESS" |
| 118 | + action = "allow" |
| 119 | + rule_name = "egress-101" |
| 120 | + description = "test egress rule 101" |
| 121 | + enable_logging = true |
| 122 | + match = { |
| 123 | + src_ip_ranges = ["10.100.0.2/32"] |
| 124 | + dest_fqdns = ["example.com"] |
| 125 | + dest_region_codes = ["US"] |
| 126 | + dest_threat_intelligences = ["iplist-public-clouds"] |
| 127 | + layer4_configs = [ |
| 128 | + { |
| 129 | + ip_protocol = "all" |
| 130 | + }, |
| 131 | + ] |
| 132 | + } |
| 133 | + }, |
| 134 | + { |
| 135 | + priority = "102" |
| 136 | + direction = "EGRESS" |
| 137 | + action = "deny" |
| 138 | + rule_name = "egress-102" |
| 139 | + disabled = true |
| 140 | + description = "test egress rule 102" |
| 141 | + target_resources = [ |
| 142 | + "projects/${var.project_id}/global/networks/${local.prefix}-network", |
| 143 | + ] |
| 144 | + match = { |
| 145 | + src_ip_ranges = ["10.100.0.102/32"] |
| 146 | + dest_ip_ranges = ["10.100.0.2/32"] |
| 147 | + dest_region_codes = ["AR"] |
| 148 | + layer4_configs = [ |
| 149 | + { |
| 150 | + ip_protocol = "all" |
| 151 | + }, |
| 152 | + ] |
| 153 | + } |
| 154 | + }, |
| 155 | + { |
| 156 | + priority = "103" |
| 157 | + direction = "EGRESS" |
| 158 | + action = "allow" |
| 159 | + rule_name = "egress-103" |
| 160 | + disabled = true |
| 161 | + description = "test ingres rule 103" |
| 162 | + enable_logging = true |
| 163 | + target_service_accounts = ["fw-test-svc-acct@${var.project_id}.iam.gserviceaccount.com"] |
| 164 | + match = { |
| 165 | + dest_ip_ranges = ["10.100.0.103/32"] |
| 166 | + layer4_configs = [ |
| 167 | + { |
| 168 | + ip_protocol = "tcp" |
| 169 | + ports = ["80", "8080", "8081-8085"] |
| 170 | + }, |
| 171 | + ] |
| 172 | + } |
| 173 | + }, |
| 174 | + |
| 175 | + ] |
| 176 | + depends_on = [ |
| 177 | + google_compute_network.network, |
| 178 | + google_compute_network.network_backup, |
| 179 | + ] |
| 180 | + |
| 181 | +} |
| 182 | + |
| 183 | +module "firewal_policy_no_rule" { |
| 184 | + source = "terraform-google-modules/network/google//modules/hierarchical-firewall-policy" |
| 185 | + version = "~> 9.0" |
| 186 | + |
| 187 | + parent_node = "folders/${var.folder1}" |
| 188 | + policy_name = "${local.prefix}-firewall-policy-no-rules-${random_string.random_suffix.result}" |
| 189 | + description = "${local.prefix} test firewall policy without any rules" |
| 190 | +} |
0 commit comments