-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall.tf
234 lines (192 loc) · 7.32 KB
/
firewall.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Copyright 2022 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.
*/
// This file was automatically generated from a template in ./autogen/main
/******************************************
Match the gke-<CLUSTER>-<ID>-all INGRESS
firewall rule created by GKE but for EGRESS
Required for clusters when VPCs enforce
a default-deny egress rule
*****************************************/
resource "google_compute_firewall" "intra_egress" {
count = var.add_cluster_firewall_rules ? 1 : 0
name = "gke-${substr(var.name, 0, min(36, length(var.name)))}-intra-cluster-egress"
description = "Managed by terraform gke module: Allow pods to communicate with each other and the master"
project = local.network_project_id
network = var.network
priority = var.firewall_priority
direction = "EGRESS"
target_tags = [local.cluster_network_tag]
destination_ranges = concat([
local.cluster_endpoint_for_nodes,
local.cluster_subnet_cidr,
],
local.pod_all_ip_ranges
)
# Allow all possible protocols
allow { protocol = "tcp" }
allow { protocol = "udp" }
allow { protocol = "icmp" }
allow { protocol = "sctp" }
allow { protocol = "esp" }
allow { protocol = "ah" }
depends_on = [
google_container_cluster.primary,
]
}
/******************************************
Allow GKE master to hit non 443 ports for
Webhooks/Admission Controllers
https://github.com/kubernetes/kubernetes/issues/79739
*****************************************/
resource "google_compute_firewall" "master_webhooks" {
count = var.add_cluster_firewall_rules || var.add_master_webhook_firewall_rules ? 1 : 0
name = "gke-${substr(var.name, 0, min(36, length(var.name)))}-webhooks"
description = "Managed by terraform gke module: Allow master to hit pods for admission controllers/webhooks"
project = local.network_project_id
network = var.network
priority = var.firewall_priority
direction = "INGRESS"
source_ranges = [local.cluster_endpoint_for_nodes]
source_tags = []
target_tags = [local.cluster_network_tag]
allow {
protocol = "tcp"
ports = var.firewall_inbound_ports
}
depends_on = [
google_container_cluster.primary,
]
}
/******************************************
Create shadow firewall rules to capture the
traffic flow between the managed firewall rules
*****************************************/
resource "google_compute_firewall" "shadow_allow_pods" {
count = var.add_shadow_firewall_rules ? 1 : 0
name = "gke-shadow-${substr(var.name, 0, min(36, length(var.name)))}-all"
description = "Managed by terraform gke module: A shadow firewall rule to match the default rule allowing pod communication."
project = local.network_project_id
network = var.network
priority = var.shadow_firewall_rules_priority
direction = "INGRESS"
source_ranges = local.pod_all_ip_ranges
target_tags = [local.cluster_network_tag]
# Allow all possible protocols
allow { protocol = "tcp" }
allow { protocol = "udp" }
allow { protocol = "icmp" }
allow { protocol = "sctp" }
allow { protocol = "esp" }
allow { protocol = "ah" }
dynamic "log_config" {
for_each = var.shadow_firewall_rules_log_config == null ? [] : [var.shadow_firewall_rules_log_config]
content {
metadata = log_config.value.metadata
}
}
}
resource "google_compute_firewall" "shadow_allow_master" {
count = var.add_shadow_firewall_rules ? 1 : 0
name = "gke-shadow-${substr(var.name, 0, min(36, length(var.name)))}-master"
description = "Managed by terraform GKE module: A shadow firewall rule to match the default rule allowing worker nodes communication."
project = local.network_project_id
network = var.network
priority = var.shadow_firewall_rules_priority
direction = "INGRESS"
source_ranges = [local.cluster_endpoint_for_nodes]
target_tags = [local.cluster_network_tag]
allow {
protocol = "tcp"
ports = ["10250", "443"]
}
dynamic "log_config" {
for_each = var.shadow_firewall_rules_log_config == null ? [] : [var.shadow_firewall_rules_log_config]
content {
metadata = log_config.value.metadata
}
}
}
resource "google_compute_firewall" "shadow_allow_nodes" {
count = var.add_shadow_firewall_rules ? 1 : 0
name = "gke-shadow-${substr(var.name, 0, min(36, length(var.name)))}-vms"
description = "Managed by Terraform GKE module: A shadow firewall rule to match the default rule allowing worker nodes communication."
project = local.network_project_id
network = var.network
priority = var.shadow_firewall_rules_priority
direction = "INGRESS"
source_ranges = [local.cluster_subnet_cidr]
target_tags = [local.cluster_network_tag]
allow {
protocol = "icmp"
}
allow {
protocol = "udp"
ports = ["1-65535"]
}
allow {
protocol = "tcp"
ports = ["1-65535"]
}
dynamic "log_config" {
for_each = var.shadow_firewall_rules_log_config == null ? [] : [var.shadow_firewall_rules_log_config]
content {
metadata = log_config.value.metadata
}
}
}
resource "google_compute_firewall" "shadow_allow_inkubelet" {
count = var.add_shadow_firewall_rules ? 1 : 0
name = "gke-shadow-${substr(var.name, 0, min(36, length(var.name)))}-inkubelet"
description = "Managed by terraform GKE module: A shadow firewall rule to match the default rule allowing worker nodes & pods communication to kubelet."
project = local.network_project_id
network = var.network
priority = var.shadow_firewall_rules_priority - 1 # rule created by GKE robot have prio 999
direction = "INGRESS"
source_ranges = local.pod_all_ip_ranges
source_tags = [local.cluster_network_tag]
target_tags = [local.cluster_network_tag]
allow {
protocol = "tcp"
ports = ["10255"]
}
dynamic "log_config" {
for_each = var.shadow_firewall_rules_log_config == null ? [] : [var.shadow_firewall_rules_log_config]
content {
metadata = log_config.value.metadata
}
}
}
resource "google_compute_firewall" "shadow_deny_exkubelet" {
count = var.add_shadow_firewall_rules ? 1 : 0
name = "gke-shadow-${substr(var.name, 0, min(36, length(var.name)))}-exkubelet"
description = "Managed by terraform GKE module: A shadow firewall rule to match the default deny rule to kubelet."
project = local.network_project_id
network = var.network
priority = var.shadow_firewall_rules_priority # rule created by GKE robot have prio 1000
direction = "INGRESS"
source_ranges = ["0.0.0.0/0"]
target_tags = [local.cluster_network_tag]
deny {
protocol = "tcp"
ports = ["10255"]
}
dynamic "log_config" {
for_each = var.shadow_firewall_rules_log_config == null ? [] : [var.shadow_firewall_rules_log_config]
content {
metadata = log_config.value.metadata
}
}
}