Skip to content
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
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ It supports creating:
- A Google Virtual Private Network (VPC)
- Subnets within the VPC
- Secondary ranges for the subnets (if applicable)

Sub modules are provided for creating individual vpc, subnets, and routes. See the modules directory for the various sub modules usage.
- routes
- firewall rules

[Sub modules](./modules/) are provided for creating individual vpc, subnets, routes, firewall rules, and firewall policies. See the [modules](./modules/) directory for the various sub modules usage.
- [vpc](./modules/vpc/)
- [subnet](./modules/subnets/)
- [route](./modules/routes/)
- [firewall rules](./modules/firewall-rules/)
- [hierarchical firewall policy](./modules/hierarchical-firewall-policy/)
- [network firewall policy](./modules/network-firewall-policy/)
- [serverless vpc access connector](./modules/vpc-serverless-connector-beta/)
- [hierarchical firewall policy](./modules/hierarchical-firewall-policy/)

## Compatibility

This module is meant for use with Terraform 1.3+ and tested using Terraform 1.4+.
If you find incompatibilities using Terraform `>=1.3`, please open an issue.

If you haven't [upgraded][terraform-0.13-upgrade] and need a Terraform
0.12.x-compatible version of this module, the last released version
intended for Terraform 0.12.x is [2.6.0].

## Usage
You can go to the examples folder, however the usage of the module could be like this in your own main.tf file:
You can go to the [examples](./examples/) folder, however the usage of the module could be like this in your own main.tf file:

```hcl
module "vpc" {
Expand Down
30 changes: 30 additions & 0 deletions examples/hierarchical-firewall-policy/README.md
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 -->
190 changes: 190 additions & 0 deletions examples/hierarchical-firewall-policy/main.tf
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 = [
Copy link
Contributor

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?

Copy link
Collaborator Author

@imrannayer imrannayer Apr 4, 2024

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 as target_resources. These are the only options available for hierarchical firewall policies
example Line 82, 103, 142 and 163.

Copy link
Contributor

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 in rules adds an additional FW rule for the for those resources specifically. Did I get that right?

Copy link
Collaborator Author

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.

{
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"
}
60 changes: 60 additions & 0 deletions examples/hierarchical-firewall-policy/outputs.tf
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"
}
35 changes: 35 additions & 0 deletions examples/hierarchical-firewall-policy/variables.tf
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"
}
1 change: 0 additions & 1 deletion modules/fabric-net-firewall/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ resource "google_compute_firewall" "allow-tag-https" {
################################################################################

resource "google_compute_firewall" "custom" {
# provider = "google-beta"
for_each = var.custom_rules
name = each.key
description = each.value.description
Expand Down
1 change: 1 addition & 0 deletions modules/hierarchical-firewall-policy/.gitignore
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
Loading