-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hierarchical firewall policy sub-module (#553)
- Loading branch information
1 parent
761db96
commit c7c0f07
Showing
21 changed files
with
998 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# hierarchical Firewall Policy Rule | ||
|
||
This example creates a Service Account and 2 hierarchical firewall policy. First policy will have a few rules and will be attached to folders. Second policy will not be attached and any folders/org and will not have any rules. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| folder1 | The folder\_id ID 1 to to create firewall policy in | `any` | n/a | yes | | ||
| folder2 | The folder\_id ID 2 to attach firewal policy to | `any` | n/a | yes | | ||
| folder3 | The folder\_id ID 3 to attach firewal policy to | `any` | n/a | yes | | ||
| org\_id | The org ID attach firewal policy to | `any` | n/a | yes | | ||
| project\_id | The project ID to host the network in | `any` | n/a | yes | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| firewal\_policy\_no\_rules\_id | ID of Firewall policy created without any rules and association | | ||
| firewal\_policy\_no\_rules\_name | Name of Firewall policy created without any rules and association | | ||
| firewal\_policy\_no\_rules\_parent\_folder | Firewall policy parent | | ||
| fw\_policy\_id | Firewall policy ID | | ||
| fw\_policy\_name | Firewall policy name | | ||
| fw\_policy\_parent\_folder | Firewall policy parent | | ||
| project\_id | Project ID | | ||
| rules | Firewall policy rules | | ||
| target\_associations | Firewall policy association | | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
locals { | ||
prefix = "hierarchical" | ||
} | ||
|
||
resource "random_string" "random_suffix" { | ||
length = 6 | ||
special = false | ||
lower = true | ||
upper = false | ||
} | ||
|
||
resource "google_service_account" "service_account" { | ||
project = var.project_id | ||
account_id = "${local.prefix}-fw-test-svc-acct" | ||
display_name = "${local.prefix} firewall policy test service account" | ||
} | ||
|
||
resource "google_compute_network" "network" { | ||
project = var.project_id | ||
name = "${local.prefix}-network" | ||
} | ||
|
||
resource "google_compute_network" "network_backup" { | ||
project = var.project_id | ||
name = "${local.prefix}-network-backup" | ||
} | ||
|
||
module "firewal_policy" { | ||
source = "terraform-google-modules/network/google//modules/hierarchical-firewall-policy" | ||
version = "~> 9.0" | ||
|
||
parent_node = "folders/${var.folder1}" | ||
policy_name = "${local.prefix}-firewall-policy-${random_string.random_suffix.result}" | ||
description = "test ${local.prefix} firewall policy" | ||
target_org = var.org_id | ||
target_folders = [var.folder2, var.folder3] | ||
|
||
rules = [ | ||
{ | ||
priority = "1" | ||
direction = "INGRESS" | ||
action = "allow" | ||
rule_name = "ingress-1" | ||
description = "test ingres rule 1" | ||
enable_logging = true | ||
match = { | ||
src_ip_ranges = ["10.100.0.1/32"] | ||
src_fqdns = ["example.com"] | ||
src_region_codes = ["US"] | ||
src_threat_intelligences = ["iplist-public-clouds"] | ||
layer4_configs = [ | ||
{ | ||
ip_protocol = "all" | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
priority = "2" | ||
direction = "INGRESS" | ||
action = "deny" | ||
rule_name = "ingress-2" | ||
disabled = true | ||
description = "test ingres rule 2" | ||
target_resources = [ | ||
"projects/${var.project_id}/global/networks/${local.prefix}-network-backup", | ||
] | ||
match = { | ||
src_ip_ranges = ["10.100.0.2/32"] | ||
src_fqdns = ["example.org"] | ||
src_region_codes = ["BE"] | ||
layer4_configs = [ | ||
{ | ||
ip_protocol = "all" | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
priority = "3" | ||
direction = "INGRESS" | ||
action = "allow" | ||
rule_name = "ingress-3" | ||
disabled = true | ||
description = "test ingres rule 3" | ||
enable_logging = true | ||
target_service_accounts = ["fw-test-svc-acct@${var.project_id}.iam.gserviceaccount.com"] | ||
match = { | ||
src_ip_ranges = ["10.100.0.3/32"] | ||
dest_ip_ranges = ["10.100.0.103/32"] | ||
layer4_configs = [ | ||
{ | ||
ip_protocol = "tcp" | ||
ports = ["80"] | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
priority = "101" | ||
direction = "EGRESS" | ||
action = "allow" | ||
rule_name = "egress-101" | ||
description = "test egress rule 101" | ||
enable_logging = true | ||
match = { | ||
src_ip_ranges = ["10.100.0.2/32"] | ||
dest_fqdns = ["example.com"] | ||
dest_region_codes = ["US"] | ||
dest_threat_intelligences = ["iplist-public-clouds"] | ||
layer4_configs = [ | ||
{ | ||
ip_protocol = "all" | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
priority = "102" | ||
direction = "EGRESS" | ||
action = "deny" | ||
rule_name = "egress-102" | ||
disabled = true | ||
description = "test egress rule 102" | ||
target_resources = [ | ||
"projects/${var.project_id}/global/networks/${local.prefix}-network", | ||
] | ||
match = { | ||
src_ip_ranges = ["10.100.0.102/32"] | ||
dest_ip_ranges = ["10.100.0.2/32"] | ||
dest_region_codes = ["AR"] | ||
layer4_configs = [ | ||
{ | ||
ip_protocol = "all" | ||
}, | ||
] | ||
} | ||
}, | ||
{ | ||
priority = "103" | ||
direction = "EGRESS" | ||
action = "allow" | ||
rule_name = "egress-103" | ||
disabled = true | ||
description = "test ingres rule 103" | ||
enable_logging = true | ||
target_service_accounts = ["fw-test-svc-acct@${var.project_id}.iam.gserviceaccount.com"] | ||
match = { | ||
dest_ip_ranges = ["10.100.0.103/32"] | ||
layer4_configs = [ | ||
{ | ||
ip_protocol = "tcp" | ||
ports = ["80", "8080", "8081-8085"] | ||
}, | ||
] | ||
} | ||
}, | ||
|
||
] | ||
depends_on = [ | ||
google_compute_network.network, | ||
google_compute_network.network_backup, | ||
] | ||
|
||
} | ||
|
||
module "firewal_policy_no_rule" { | ||
source = "terraform-google-modules/network/google//modules/hierarchical-firewall-policy" | ||
version = "~> 9.0" | ||
|
||
parent_node = "folders/${var.folder1}" | ||
policy_name = "${local.prefix}-firewall-policy-no-rules-${random_string.random_suffix.result}" | ||
description = "${local.prefix} test firewall policy without any rules" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
output "project_id" { | ||
value = var.project_id | ||
description = "Project ID" | ||
} | ||
|
||
output "fw_policy_id" { | ||
value = module.firewal_policy.fw_policy.name | ||
description = "Firewall policy ID" | ||
} | ||
|
||
output "fw_policy_parent_folder" { | ||
value = module.firewal_policy.fw_policy.parent | ||
description = "Firewall policy parent" | ||
} | ||
|
||
output "fw_policy_name" { | ||
value = module.firewal_policy.fw_policy.short_name | ||
description = "Firewall policy name" | ||
} | ||
|
||
output "target_associations" { | ||
value = module.firewal_policy.target_associations | ||
description = "Firewall policy association" | ||
} | ||
|
||
output "rules" { | ||
value = module.firewal_policy.rules | ||
description = "Firewall policy rules" | ||
} | ||
|
||
output "firewal_policy_no_rules_id" { | ||
value = module.firewal_policy_no_rule.fw_policy.name | ||
description = "ID of Firewall policy created without any rules and association" | ||
} | ||
|
||
output "firewal_policy_no_rules_name" { | ||
value = module.firewal_policy_no_rule.fw_policy.short_name | ||
description = "Name of Firewall policy created without any rules and association" | ||
} | ||
|
||
output "firewal_policy_no_rules_parent_folder" { | ||
value = module.firewal_policy.fw_policy.parent | ||
description = "Firewall policy parent" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
variable "project_id" { | ||
description = "The project ID to host the network in" | ||
} | ||
|
||
variable "folder1" { | ||
description = "The folder_id ID 1 to to create firewall policy in" | ||
} | ||
|
||
variable "folder2" { | ||
description = "The folder_id ID 2 to attach firewal policy to" | ||
} | ||
|
||
variable "folder3" { | ||
description = "The folder_id ID 3 to attach firewal policy to" | ||
} | ||
|
||
variable "org_id" { | ||
description = "The org ID attach firewal policy to" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.