-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvariables.tf
424 lines (378 loc) · 13.4 KB
/
variables.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
##############################################################################
# Account Variables
##############################################################################
variable TF_VERSION {
default = "0.13"
description = "The version of the Terraform engine that's used in the Schematics workspace."
}
variable ibmcloud_api_key {
description = "The IBM Cloud platform API key needed to deploy IAM enabled resources"
type = string
}
variable unique_id {
description = "A unique identifier need to provision resources. Must begin with a letter"
type = string
default = "asset-multizone"
validation {
error_message = "Unique ID must begin and end with a letter and contain only letters, numbers, and - characters."
condition = can(regex("^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$", var.unique_id))
}
}
variable ibm_region {
description = "IBM Cloud region where all resources will be deployed"
type = string
validation {
error_message = "Must use an IBM Cloud region. Use `ibmcloud regions` with the IBM Cloud CLI to see valid regions."
condition = can(
contains([
"au-syd",
"jp-tok",
"eu-de",
"eu-gb",
"us-south",
"us-east"
], var.ibm_region)
)
}
}
variable resource_group {
description = "Name of resource group where all infrastructure will be provisioned"
type = string
default = "asset-development"
validation {
error_message = "Unique ID must begin and end with a letter and contain only letters, numbers, and - characters."
condition = can(regex("^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$", var.resource_group))
}
}
##############################################################################
##############################################################################
# Network variables
##############################################################################
variable classic_access {
description = "Enable VPC Classic Access. Note: only one VPC per region can have classic access"
type = bool
default = false
}
variable enable_public_gateway {
description = "Enable public gateways for subnets, true or false."
type = bool
default = false
}
variable cidr_blocks {
description = "An object containing lists of CIDR blocks. Each CIDR block will be used to create a subnet"
default = {
zone-1 = [
"10.10.10.0/28",
"10.20.10.0/28",
"10.30.10.0/28"
],
zone-2 = [
"10.40.10.0/28",
"10.50.10.0/28",
"10.60.10.0/28"
],
zone-3 = [
"10.70.10.0/28",
"10.80.10.0/28",
"10.90.10.0/28"
]
}
validation {
error_message = "The var.cidr_blocks objects must have 1, 2, or 3 keys."
condition = length(keys(var.cidr_blocks)) <= 3 && length(keys(var.cidr_blocks)) >= 1
}
validation {
error_message = "Each list must have at least one CIDR block."
condition = length(distinct(
[
for zone in keys(var.cidr_blocks):
false if length(var.cidr_blocks[zone]) == 0
]
)) == 0
}
validation {
error_message = "Each item in each list must contain a valid CIDR block."
condition = length(
distinct(
flatten([
for zone in keys(var.cidr_blocks):
false if length([
for cidr in var.cidr_blocks[zone]:
false if !can(regex("^(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2})\\/(3[0-2]|2[0-9]|1[0-9]|[0-9])$", cidr))
]) > 0
])
)
) == 0
}
}
variable acl_rules {
description = "Access control list rule set"
default = [
{
name = "roks-create-worker-nodes-inbound"
action = "allow"
source = "161.26.0.0/16"
destination = "0.0.0.0/0"
direction = "inbound"
},
{
name = "roks-nodes-to-service-inbound"
action = "allow"
source = "166.8.0.0/14"
destination = "0.0.0.0/0"
direction = "inbound"
},
{
name = "roks-create-worker-nodes-outbound"
action = "allow"
destination = "161.26.0.0/16"
source = "0.0.0.0/0"
direction = "outbound"
},
{
name = "roks-nodes-to-service-outbound"
action = "allow"
destination = "166.8.0.0/14"
source = "0.0.0.0/0"
direction = "outbound"
},
{
name = "allow-all-inbound"
action = "allow"
source = "0.0.0.0/0"
destination = "0.0.0.0/0"
direction = "inbound"
},
{
name = "allow-all-outbound"
action = "allow"
source = "0.0.0.0/0"
destination = "0.0.0.0/0"
direction = "outbound"
}
]
validation {
error_message = "ACL Rules can only contain the fields `name`, `action`, `source`, `destination`, `direction`, `tcp`, `icmp` and `udp`."
condition = length(distinct(
# Create a flat array of results
flatten([
# For each ACL object
for rule in var.acl_rules: [
# Check ACL rule keys
for field in keys(rule):
# Return false if there are any invalid keys
false if !contains(
["name", "action", "source", "destination", "direction", "tcp", "icmp", "udp"],
field
)
]
])
)) == 0 # Should be 0 invalid keys
}
validation {
error_message = "ACL rules can only have one of `icmp`, `udp`, or `tcp`."
condition = length(distinct(
# Get flat list of results
flatten([
# Check through rules
for rule in var.acl_rules:
# Return true if there is more than one of `icmp`, `udp`, or `tcp`
true if length(
[
for field in keys(rule):
true if contains(["icmp","udp", "tcp"], field)
]
) > 1
])
)) == 0 # Checks for length. If all fields all correct, array will be empty
}
validation {
error_message = "ACL rules must contain at least `name`, `action`, `source`, `destination`, and `direction` fields."
condition = length(distinct(
flatten([
# Check through rules
for rule in var.acl_rules:
# Return false if the keys do not contain all five fields
false if length([
for field in keys(rule):
true if contains(["name", "action", "source", "destination", "direction"], field)
]) < 5
])
)) == 0 # No object should contain less than 5 of the 5 required fields
}
validation {
error_message = "ACL rule actions can only be `allow` or `deny`."
condition = length(distinct(
flatten([
# Check through rules
for rule in var.acl_rules:
# Return false action is not valid
false if !contains(["allow", "deny"], rule.action)
])
)) == 0
}
validation {
error_message = "ACL rule direction can only be `inbound` or `outbound`."
condition = length(distinct(
flatten([
# Check through rules
for rule in var.acl_rules:
# Return false if direction is not valid
false if !contains(["inbound", "outbound"], rule.direction)
])
)) == 0
}
validation {
error_message = "ACL rule names must match the regex pattern ^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$."
condition = length(distinct(
flatten([
# Check through rules
for rule in var.acl_rules:
# Return false if direction is not valid
false if !can(regex("^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$", rule.name))
])
)) == 0
}
validation {
error_message = "`tcp` blocks must contain the `port_min`, `port_max`, `source_port_min`, and `source_port_max` fields."
condition = length(distinct(
flatten([
# Get all rules with a `tcp` field
for tcp_rule in [ for rule in var.acl_rules: rule.tcp if contains(keys(rule), "tcp")]:
# Return false if it does not contain all four fields
false if length([
for field in keys(tcp_rule):
field if contains(["port_min", "port_max", "source_port_min", "source_port_max"], field)
]) != 4
])
)) == 0
}
validation {
error_message = "`udp` blocks must contain the `port_min`, `port_max`, `source_port_min`, and `source_port_max` fields."
condition = length(distinct(
flatten([
# Get all rules with `udp` field
for udp_rule in [ for rule in var.acl_rules: rule.udp if contains(keys(rule), "udp")]:
# Return false if it does not contain all four fields
false if length([
for field in keys(udp_rule):
field if contains(["port_min", "port_max", "source_port_min", "source_port_max"], field)
]) != 4
])
)) == 0
}
validation {
error_message = "`icmp` blocks must contain the `type` and `code` fields."
condition = length(distinct(
flatten([
# Get all rules with `icmp` field
for icmp_rule in [ for rule in var.acl_rules: rule.icmp if contains(keys(rule), "icmp")]:
# Return false if it does not contain both fields
false if length([
for field in keys(icmp_rule):
field if contains(["type", "code"], field)
]) != 2
])
)) == 0
}
}
variable security_group_rules {
description = "List of security group rules to be added to default security group"
default = {
allow_all_inbound = {
source = "0.0.0.0/0"
direction = "inbound"
}
}
validation {
error_message = "Security group rules can only contain `direction`, `source`, `tcp`, `udp`, and `icmp` fields."
condition = length(flatten([
# Convert rules from object to array and search
for rule in [ for object_rule in keys(var.security_group_rules): var.security_group_rules[object_rule] ]:
# Return false if there are fields other than these five used
false if length([
for field in keys(rule):
true if !contains(["direction", "source", "tcp", "udp", "icmp"], field)
]) > 0
])) == 0
}
validation {
error_message = "Security group rules can only contain one `icmp`, `tcp`, or `udp` field."
condition = length(flatten([
# Convert rules from object to array and search
for rule in [ for object_rule in keys(var.security_group_rules): var.security_group_rules[object_rule] ]:
false if length([
for field in keys(rule):
true if contains(["icmp", "tcp", "udp"], field)
]) > 1
])) == 0
}
validation {
error_message = "Security group rules must contain `direction` and `source` fields."
condition = length(flatten([
# Convert rules from object to array and search
for rule in [ for object_rule in keys(var.security_group_rules): var.security_group_rules[object_rule] ]:
false if length([
for field in keys(rule):
true if contains(["direction", "source"], field)
]) != 2
])) == 0
}
validation {
error_message = "Security group rules direction must be `inbound` or `outbound`."
condition = length(flatten([
# Convert rules from object to array and search
for rule in [ for object_rule in keys(var.security_group_rules): var.security_group_rules[object_rule] ]:
false if !contains(["inbound", "outbound"], rule.direction)
])) == 0
}
validation {
error_message = "Security group `tcp` blocks must contain `port_min` and `port_max` fields."
condition = length(flatten([
for tcp_rule in [
# Get TCP blocks from all rules
for rule in [ for object_rule in keys(var.security_group_rules): var.security_group_rules[object_rule] ]:
rule.tcp if contains(keys(rule), "tcp")
]:
# If blocks do not contain both fields, return false
false if length([
for field in keys(tcp_rule):
# Return true if search finds fields
true if contains(["port_min", "port_max"], field)
]) != 2
])) == 0
}
validation {
error_message = "Security group `udp` blocks must contain `port_min` and `port_max` fields."
condition = length(flatten([
for udp_rule in [
# Get UDP blocks from all rules
for rule in [ for object_rule in keys(var.security_group_rules): var.security_group_rules[object_rule] ]:
rule.udp if contains(keys(rule), "udp")
]:
# If blocks do not contain both fields, return false
false if length([
for field in keys(udp_rule):
# Return true if search finds fields
true if contains(["port_min", "port_max"], field)
]) != 2
])) == 0
}
validation {
error_message = "Security group `icmp` blocks must contain `type` and `code` fields."
condition = length(flatten([
for icmp_rule in [
# Get ICMP blocks from all rules
for rule in [ for object_rule in keys(var.security_group_rules): var.security_group_rules[object_rule] ]:
rule.icmp if contains(keys(rule), "icmp")
]:
# If blocks do not contain both fields, return false
false if length([
for field in keys(icmp_rule):
# Return true if search finds fields
true if contains(["type", "code"], field)
]) != 2
])) == 0
}
}
##############################################################################