-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add hierarchical firewall policy sub-module #553
Merged
imrannayer
merged 9 commits into
terraform-google-modules:master
from
imrannayer:feat/hierarchical-firewall-policies
Apr 15, 2024
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e222d45
added hierarchical sub-module
imrannayer a0a4ec1
added hierarchical sub-module
imrannayer 2498965
added hierarchical sub-module
imrannayer 62407e4
random folder names
imrannayer 04f78cc
added test
imrannayer b073eaf
fixed README
imrannayer bd64aba
added integration test
imrannayer 35a071c
updated README
imrannayer a99b861
Merge branch 'master' into feat/hierarchical-firewall-policies
imrannayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 @@ | ||
terraform.tfvars | ||
imrannayer marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How are you associating rules to the respective containers? Based on this diagram it seems like rules can be specified at org/folder/project level but this rules block doesn't have that association.
Can you explain that a bit for me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firewall policy itself can attach to org or or folder.
Rules can be for specific VMs with service account which are provided as
target_service_accounts
or you can apply a rule to a specific VPC which is provided astarget_resources
. These are the only options available for hierarchical firewall policiesexample Line 82, 103, 142 and 163.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. So line 51 and 52 are default targets and additionally a
target_resources
slice inrules
adds an additional FW rule for the for those resources specifically. Did I get that right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 51 and 52 applies whole firewall policy (all rules) to folders or an org. But Line 82, 103, 143 and 163 allow users to apply a firewall rule to VMs of specific VPC network of VM with specific service account instead of all the VMs under the folders/org.