Skip to content

Commit 7ce3c49

Browse files
authored
feat: Add firewall support safer-cluster modules (#570)
1 parent 6ff27f9 commit 7ce3c49

File tree

18 files changed

+114
-25
lines changed

18 files changed

+114
-25
lines changed

.kitchen.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ suites:
5151
systems:
5252
- name: safer_cluster
5353
backend: local
54+
controls:
55+
- gcloud
56+
- name: inspec-gcp
57+
backend: gcp
58+
controls:
59+
- network
5460
- name: "simple_regional"
5561
driver:
5662
root_module_directory: test/fixtures/simple_regional

autogen/main/cluster.tf.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ resource "google_container_node_pool" "pools" {
509509
{% endif %}
510510

511511
shielded_instance_config {
512-
enable_secure_boot = lookup(each.value, "enable_secure_boot", false)
512+
enable_secure_boot = lookup(each.value, "enable_secure_boot", false)
513513
enable_integrity_monitoring = lookup(each.value, "enable_integrity_monitoring", true)
514514
}
515515
}

autogen/main/firewall.tf.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ resource "google_compute_firewall" "intra_egress" {
4848
allow { protocol = "esp" }
4949
allow { protocol = "ah" }
5050

51+
{% if not private_cluster %}
5152
depends_on = [
5253
google_container_cluster.primary,
5354
]
55+
{% endif %}
5456
}
5557

5658

@@ -77,8 +79,10 @@ resource "google_compute_firewall" "master_webhooks" {
7779
ports = var.firewall_inbound_ports
7880
}
7981

82+
{% if not private_cluster %}
8083
depends_on = [
8184
google_container_cluster.primary,
8285
]
86+
{% endif %}
8387

8488
}

autogen/safer-cluster/main.tf.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ module "gke" {
4949
ip_range_pods = var.ip_range_pods
5050
ip_range_services = var.ip_range_services
5151

52+
add_cluster_firewall_rules = var.add_cluster_firewall_rules
53+
firewall_priority = var.firewall_priority
54+
firewall_inbound_ports = var.firewall_inbound_ports
55+
5256
horizontal_pod_autoscaling = var.horizontal_pod_autoscaling
5357
http_load_balancing = var.http_load_balancing
5458

autogen/safer-cluster/variables.tf.tmpl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,21 @@ variable "gce_pd_csi_driver" {
342342
description = "(Beta) Whether this cluster should enable the Google Compute Engine Persistent Disk Container Storage Interface (CSI) Driver."
343343
default = true
344344
}
345+
346+
variable "add_cluster_firewall_rules" {
347+
type = bool
348+
description = "Create additional firewall rules"
349+
default = false
350+
}
351+
352+
variable "firewall_priority" {
353+
type = number
354+
description = "Priority rule for firewall rules"
355+
default = 1000
356+
}
357+
358+
variable "firewall_inbound_ports" {
359+
type = list(string)
360+
description = "List of TCP ports for admission/webhook controllers"
361+
default = ["8443", "9443", "15017"]
362+
}

examples/safer_cluster/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ module "gke" {
4949
ip_range_services = local.svc_range_name
5050
compute_engine_service_account = var.compute_engine_service_account
5151
master_ipv4_cidr_block = "172.16.0.0/28"
52+
add_cluster_firewall_rules = true
53+
firewall_inbound_ports = ["9443", "15017"]
5254

5355
master_authorized_networks = [
5456
{

modules/beta-private-cluster-update-variant/firewall.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ resource "google_compute_firewall" "intra_egress" {
4848
allow { protocol = "esp" }
4949
allow { protocol = "ah" }
5050

51-
depends_on = [
52-
google_container_cluster.primary,
53-
]
5451
}
5552

5653

@@ -77,8 +74,5 @@ resource "google_compute_firewall" "master_webhooks" {
7774
ports = var.firewall_inbound_ports
7875
}
7976

80-
depends_on = [
81-
google_container_cluster.primary,
82-
]
8377

8478
}

modules/beta-private-cluster/firewall.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ resource "google_compute_firewall" "intra_egress" {
4848
allow { protocol = "esp" }
4949
allow { protocol = "ah" }
5050

51-
depends_on = [
52-
google_container_cluster.primary,
53-
]
5451
}
5552

5653

@@ -77,8 +74,5 @@ resource "google_compute_firewall" "master_webhooks" {
7774
ports = var.firewall_inbound_ports
7875
}
7976

80-
depends_on = [
81-
google_container_cluster.primary,
82-
]
8377

8478
}

modules/private-cluster-update-variant/firewall.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ resource "google_compute_firewall" "intra_egress" {
4848
allow { protocol = "esp" }
4949
allow { protocol = "ah" }
5050

51-
depends_on = [
52-
google_container_cluster.primary,
53-
]
5451
}
5552

5653

@@ -77,8 +74,5 @@ resource "google_compute_firewall" "master_webhooks" {
7774
ports = var.firewall_inbound_ports
7875
}
7976

80-
depends_on = [
81-
google_container_cluster.primary,
82-
]
8377

8478
}

modules/private-cluster/firewall.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ resource "google_compute_firewall" "intra_egress" {
4848
allow { protocol = "esp" }
4949
allow { protocol = "ah" }
5050

51-
depends_on = [
52-
google_container_cluster.primary,
53-
]
5451
}
5552

5653

@@ -77,8 +74,5 @@ resource "google_compute_firewall" "master_webhooks" {
7774
ports = var.firewall_inbound_ports
7875
}
7976

80-
depends_on = [
81-
google_container_cluster.primary,
82-
]
8377

8478
}

modules/safer-cluster-update-variant/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ For simplicity, we suggest using `roles/container.admin` and
200200

201201
| Name | Description | Type | Default | Required |
202202
|------|-------------|:----:|:-----:|:-----:|
203+
| add\_cluster\_firewall\_rules | Create additional firewall rules | bool | `"false"` | no |
203204
| authenticator\_security\_group | The name of the RBAC security group for use with Google security groups in Kubernetes RBAC. Group name must be in format [email protected] | string | `"null"` | no |
204205
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
205206
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
@@ -214,6 +215,8 @@ For simplicity, we suggest using `roles/container.admin` and
214215
| enable\_resource\_consumption\_export | Whether to enable resource consumption metering on this cluster. When enabled, a table will be created in the resource export BigQuery dataset to store resource consumption data. The resulting table can be joined with the resource usage table or with BigQuery billing export. | bool | `"true"` | no |
215216
| enable\_shielded\_nodes | Enable Shielded Nodes features on all nodes in this cluster. | bool | `"true"` | no |
216217
| enable\_vertical\_pod\_autoscaling | Vertical Pod Autoscaling automatically adjusts the resources of pods controlled by it | bool | `"false"` | no |
218+
| firewall\_inbound\_ports | List of TCP ports for admission/webhook controllers | list(string) | `<list>` | no |
219+
| firewall\_priority | Priority rule for firewall rules | number | `"1000"` | no |
217220
| gce\_pd\_csi\_driver | (Beta) Whether this cluster should enable the Google Compute Engine Persistent Disk Container Storage Interface (CSI) Driver. | bool | `"true"` | no |
218221
| grant\_registry\_access | Grants created cluster-specific service account storage.objectViewer role. | bool | `"true"` | no |
219222
| horizontal\_pod\_autoscaling | Enable horizontal pod autoscaling addon | bool | `"true"` | no |

modules/safer-cluster-update-variant/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ module "gke" {
4545
ip_range_pods = var.ip_range_pods
4646
ip_range_services = var.ip_range_services
4747

48+
add_cluster_firewall_rules = var.add_cluster_firewall_rules
49+
firewall_priority = var.firewall_priority
50+
firewall_inbound_ports = var.firewall_inbound_ports
51+
4852
horizontal_pod_autoscaling = var.horizontal_pod_autoscaling
4953
http_load_balancing = var.http_load_balancing
5054

modules/safer-cluster-update-variant/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,21 @@ variable "gce_pd_csi_driver" {
342342
description = "(Beta) Whether this cluster should enable the Google Compute Engine Persistent Disk Container Storage Interface (CSI) Driver."
343343
default = true
344344
}
345+
346+
variable "add_cluster_firewall_rules" {
347+
type = bool
348+
description = "Create additional firewall rules"
349+
default = false
350+
}
351+
352+
variable "firewall_priority" {
353+
type = number
354+
description = "Priority rule for firewall rules"
355+
default = 1000
356+
}
357+
358+
variable "firewall_inbound_ports" {
359+
type = list(string)
360+
description = "List of TCP ports for admission/webhook controllers"
361+
default = ["8443", "9443", "15017"]
362+
}

modules/safer-cluster/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ For simplicity, we suggest using `roles/container.admin` and
200200

201201
| Name | Description | Type | Default | Required |
202202
|------|-------------|:----:|:-----:|:-----:|
203+
| add\_cluster\_firewall\_rules | Create additional firewall rules | bool | `"false"` | no |
203204
| authenticator\_security\_group | The name of the RBAC security group for use with Google security groups in Kubernetes RBAC. Group name must be in format [email protected] | string | `"null"` | no |
204205
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
205206
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
@@ -214,6 +215,8 @@ For simplicity, we suggest using `roles/container.admin` and
214215
| enable\_resource\_consumption\_export | Whether to enable resource consumption metering on this cluster. When enabled, a table will be created in the resource export BigQuery dataset to store resource consumption data. The resulting table can be joined with the resource usage table or with BigQuery billing export. | bool | `"true"` | no |
215216
| enable\_shielded\_nodes | Enable Shielded Nodes features on all nodes in this cluster. | bool | `"true"` | no |
216217
| enable\_vertical\_pod\_autoscaling | Vertical Pod Autoscaling automatically adjusts the resources of pods controlled by it | bool | `"false"` | no |
218+
| firewall\_inbound\_ports | List of TCP ports for admission/webhook controllers | list(string) | `<list>` | no |
219+
| firewall\_priority | Priority rule for firewall rules | number | `"1000"` | no |
217220
| gce\_pd\_csi\_driver | (Beta) Whether this cluster should enable the Google Compute Engine Persistent Disk Container Storage Interface (CSI) Driver. | bool | `"true"` | no |
218221
| grant\_registry\_access | Grants created cluster-specific service account storage.objectViewer role. | bool | `"true"` | no |
219222
| horizontal\_pod\_autoscaling | Enable horizontal pod autoscaling addon | bool | `"true"` | no |

modules/safer-cluster/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ module "gke" {
4545
ip_range_pods = var.ip_range_pods
4646
ip_range_services = var.ip_range_services
4747

48+
add_cluster_firewall_rules = var.add_cluster_firewall_rules
49+
firewall_priority = var.firewall_priority
50+
firewall_inbound_ports = var.firewall_inbound_ports
51+
4852
horizontal_pod_autoscaling = var.horizontal_pod_autoscaling
4953
http_load_balancing = var.http_load_balancing
5054

modules/safer-cluster/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,21 @@ variable "gce_pd_csi_driver" {
342342
description = "(Beta) Whether this cluster should enable the Google Compute Engine Persistent Disk Container Storage Interface (CSI) Driver."
343343
default = true
344344
}
345+
346+
variable "add_cluster_firewall_rules" {
347+
type = bool
348+
description = "Create additional firewall rules"
349+
default = false
350+
}
351+
352+
variable "firewall_priority" {
353+
type = number
354+
description = "Priority rule for firewall rules"
355+
default = 1000
356+
}
357+
358+
variable "firewall_inbound_ports" {
359+
type = list(string)
360+
description = "List of TCP ports for admission/webhook controllers"
361+
default = ["8443", "9443", "15017"]
362+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
project_id = attribute('project_id')
16+
cluster_name = attribute('cluster_name')
17+
18+
control "network" do
19+
title "gcp network configuration"
20+
describe google_compute_firewalls(project: project_id) do
21+
its('firewall_names') { should include "gke-#{cluster_name[0,25]}-intra-cluster-egress" }
22+
its('firewall_names') { should include "gke-#{cluster_name[0,25]}-webhooks" }
23+
end
24+
25+
end

test/integration/safer_cluster/inspec.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
name: safer_cluster
2+
depends:
3+
- name: inspec-gcp
4+
git: https://github.com/inspec/inspec-gcp.git
5+
tag: v0.11.0
26
attributes:
37
- name: project_id
48
required: true

0 commit comments

Comments
 (0)