-
Notifications
You must be signed in to change notification settings - Fork 3
/
discriminat.tf
391 lines (296 loc) · 10.1 KB
/
discriminat.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
## Inputs
variable "project_id" {
type = string
description = "The GCP Project ID for this deployment. For example: my-project-111222"
}
variable "subnetwork_name" {
type = string
description = "The name of the subnetwork to deploy the DiscrimiNAT Firewall instances in. This must already exist."
}
variable "region" {
type = string
description = "The region the specified subnetwork is to be found in."
}
##
## Defaults
variable "zones_names" {
type = list(string)
description = "Specific zones if you wish to override the default behaviour. If not overridden, defaults to all zones found in the specified region."
default = []
}
variable "client_cidrs" {
type = list(string)
description = "Additional CIDR blocks of clients which should be able to connect to, and hence route via, DiscrimiNAT instances. Defaults to RFC1918 ranges."
default = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
}
variable "labels" {
type = map(any)
description = "Map of key-value label pairs to apply to resources created by this module. See examples for use."
default = {}
}
variable "machine_type" {
type = string
description = "The default of `e2-small` should suffice for light to medium levels of usage. Anything less than 2 CPU cores and 2 GB of RAM is not recommended. For faster access to the Internet and for projects with a large number of VMs, you may want to upgrade to `n2-highcpu-2` or `n2d-highcpu-2`."
default = "e2-small"
}
variable "block-project-ssh-keys" {
type = bool
description = "Strongly suggested to leave this to the default, that is to NOT allow project-wide SSH keys to login into the firewall."
default = true
}
variable "user_data_base64" {
type = string
description = "Strongly suggested to NOT run custom, startup scripts on the firewall instances. But if you had to, supply a base64 encoded version here."
default = null
}
variable "custom_service_account_email" {
type = string
description = "Override with a specific, custom service account email in case support for architectures with Shared VPC and/or Serverless VPC Access is needed. Default is to use the Google Compute Engine service account."
default = null
}
variable "image_project" {
type = string
description = "Reserved for use with Chaser support. Allows overriding the source image project for DiscrimiNAT."
default = null
}
variable "image_family" {
type = string
description = "Reserved for use with Chaser support. Allows overriding the source image version for DiscrimiNAT."
default = null
}
variable "random_deployment_id" {
type = bool
description = "Set to true to change the region name in resource names to a randomised word. This will be the default behaviour in the next minor version update."
default = false
}
variable "byol" {
type = string
sensitive = true
default = null
description = "If using the BYOL version from the marketplace, supply the licence key as supplied by Chaser Systems here."
}
variable "ashr" {
type = bool
default = true
description = "Automated System Health Reporting. See note in README to learn more. Set to false to disable. Default is true and hence enabled."
}
##
## Lookups
data "google_compute_subnetwork" "context" {
name = var.subnetwork_name
region = var.region
project = var.project_id
}
data "google_compute_zones" "auto" {
region = var.region
project = var.project_id
}
data "google_compute_image" "discriminat" {
family = var.image_family == null ? "discriminat" : var.image_family
project = var.image_project == null ? "chasersystems-public" : var.image_project
}
##
## Compute
resource "google_compute_address" "discriminat" {
count = length(local.zones)
region = var.region
project = var.project_id
subnetwork = var.subnetwork_name
name = "discriminat-${local.zonal_name_suffixes[count.index]}"
address_type = "INTERNAL"
}
resource "google_compute_instance_template" "discriminat" {
count = length(local.zones)
name_prefix = "discriminat-${local.zonal_name_suffixes[count.index]}-"
lifecycle {
create_before_destroy = true
}
region = var.region
project = var.project_id
tags = ["discriminat-itself"]
machine_type = var.machine_type
can_ip_forward = true
metadata = {
block-project-ssh-keys = var.block-project-ssh-keys
user-data = var.user_data_base64 != null ? base64decode(var.user_data_base64) : local.cloud_config == "" ? null : local.cloud_config
}
disk {
source_image = data.google_compute_image.discriminat.self_link
disk_type = "pd-ssd"
auto_delete = true
boot = true
}
network_interface {
subnetwork = var.subnetwork_name
subnetwork_project = var.project_id
network_ip = google_compute_address.discriminat[count.index].address
}
shielded_instance_config {
enable_secure_boot = true
enable_vtpm = true
enable_integrity_monitoring = true
}
service_account {
email = var.custom_service_account_email
scopes = var.custom_service_account_email == null ? ["compute-rw", "logging-write", "monitoring-write"] : ["cloud-platform"]
}
labels = local.labels
}
resource "google_compute_health_check" "discriminat" {
name = "discriminat-${local.suffix}"
project = var.project_id
healthy_threshold = 2
unhealthy_threshold = 2
check_interval_sec = 2
timeout_sec = 2
http_health_check {
port = 1042
}
depends_on = [google_compute_firewall.discriminat-from-healthcheckers]
}
resource "google_compute_instance_group_manager" "discriminat" {
count = length(local.zones)
project = var.project_id
name = "discriminat-${local.zonal_name_suffixes[count.index]}"
base_instance_name = "discriminat"
target_size = 1
zone = local.zones[count.index]
wait_for_instances = true
version {
instance_template = google_compute_instance_template.discriminat[count.index].id
}
auto_healing_policies {
health_check = google_compute_health_check.discriminat.id
initial_delay_sec = 360
}
update_policy {
type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_fixed = 1
max_unavailable_fixed = 1
}
}
##
## VPC
resource "google_compute_route" "discriminat" {
count = length(local.zones)
project = var.project_id
name = "discriminat-${local.zonal_name_suffixes[count.index]}"
tags = ["discriminat-${local.zonal_name_suffixes[count.index]}"]
dest_range = "0.0.0.0/0"
network = data.google_compute_subnetwork.context.network
next_hop_ip = google_compute_address.discriminat[count.index].address
priority = 200
}
resource "google_compute_firewall" "discriminat-to-internet" {
name = "discriminat-${local.suffix}-to-internet"
network = data.google_compute_subnetwork.context.network
project = var.project_id
direction = "EGRESS"
priority = 200
destination_ranges = ["0.0.0.0/0"]
target_tags = ["discriminat-itself"]
allow {
protocol = "all"
}
}
resource "google_compute_firewall" "discriminat-from-healthcheckers" {
name = "discriminat-${local.suffix}-from-healthcheckers"
network = data.google_compute_subnetwork.context.network
project = var.project_id
direction = "INGRESS"
priority = 200
source_ranges = ["35.191.0.0/16", "130.211.0.0/22"]
target_tags = ["discriminat-itself"]
allow {
protocol = "tcp"
ports = [1042]
}
}
resource "google_compute_firewall" "discriminat-from-clients" {
name = "discriminat-${local.suffix}-from-clients"
network = data.google_compute_subnetwork.context.network
project = var.project_id
direction = "INGRESS"
priority = 200
target_tags = ["discriminat-itself"]
source_ranges = concat([data.google_compute_subnetwork.context.ip_cidr_range], var.client_cidrs)
allow {
protocol = "tcp"
}
}
resource "google_compute_firewall" "discriminat-from-rest" {
name = "discriminat-${local.suffix}-from-rest"
network = data.google_compute_subnetwork.context.network
project = var.project_id
direction = "INGRESS"
priority = 400
target_tags = ["discriminat-itself"]
source_ranges = ["0.0.0.0/0"]
deny {
protocol = "all"
}
}
##
## Locals
resource "random_pet" "deployment_id" {
keepers = {
region = var.region
subnetwork_name = var.subnetwork_name
}
length = 1
}
locals {
suffix = var.random_deployment_id ? random_pet.deployment_id.id : var.region
}
locals {
labels = merge(
{
"product" : "discriminat",
"vendor" : "chasersystems_com"
},
var.labels
)
}
locals {
zones = length(var.zones_names) > 0 ? var.zones_names : data.google_compute_zones.auto.names
}
locals {
zonal_name_suffixes = var.random_deployment_id ? [for z in local.zones : format("${random_pet.deployment_id.id}-%s", substr(z, -1, 1))] : local.zones
}
locals {
cc_byol = var.byol == null ? "" : "- encoding: base64\n path: /etc/chaser/licence-key.der\n permissions: 0404\n content: ${var.byol}\n"
cc_ashr = var.ashr == true ? "" : "- path: /etc/chaser/disable_automated-system-health-reporting\n permissions: 0404\n"
}
locals {
cc_write_files = "${local.cc_byol}${local.cc_ashr}"
}
locals {
cloud_config = local.cc_write_files == "" ? "" : "#cloud-config\nwrite_files:\n${local.cc_write_files}"
}
##
## Constraints
terraform {
required_version = "> 1, < 2"
required_providers {
google = {
source = "hashicorp/google"
version = "> 3, < 6"
}
google-beta = {
source = "hashicorp/google-beta"
version = "> 3, < 6"
}
}
}
##
## Outputs
output "zonal_network_tags" {
value = { for i, z in local.zones : z => "discriminat-${local.zonal_name_suffixes[i]}" }
description = "Network Tags – to be associated with protected applications – for filtering traffic through the nearest DiscrimiNAT Firewall instance."
}
output "deployment_id" {
value = var.random_deployment_id ? random_pet.deployment_id.id : null
description = "The unique identifier, forming a part of various resource names, for this deployment."
}
##