Skip to content

Commit e0f3994

Browse files
feat(VSECPC-10307): CGI-Terraform Alignment and General Improvements (#19)
Co-authored-by: guybarak <[email protected]>
1 parent 57f3a5b commit e0f3994

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2375
-162
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#cloud-config
22
runcmd:
33
- |
4-
python3 /etc/cloud_config.py enableCloudWatch=\"${EnableCloudWatch}\" sicKey=\"${SICKey}\" installationType=\"autoscale\" osVersion=\"${OsVersion}\" allowUploadDownload=\"${AllowUploadDownload}\" templateVersion=\"1.0.2\" templateName=\"autoscale\" templateType=\"terraform_registry\" shell=\"${Shell}\" enableInstanceConnect=\"${EnableInstanceConnect}\" passwordHash=\"${PasswordHash}\" MaintenanceModePassword=\"${MaintenanceModePassword}\" bootstrapScript64=\"${BootstrapScript}\"
4+
python3 /etc/cloud_config.py enableCloudWatch=\"${EnableCloudWatch}\" sicKey=\"${SICKey}\" installationType=\"autoscale\" osVersion=\"${OsVersion}\" allowUploadDownload=\"${AllowUploadDownload}\" templateVersion=\"1.0.4\" templateName=\"autoscale\" templateType=\"terraform_registry\" shell=\"${Shell}\" enableInstanceConnect=\"${EnableInstanceConnect}\" passwordHash=\"${PasswordHash}\" MaintenanceModePassword=\"${MaintenanceModePassword}\" bootstrapScript64=\"${BootstrapScript}\"

modules/autoscale/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ variable "enable_volume_encryption" {
8585
}
8686
variable "instances_tags" {
8787
type = map(string)
88-
description = "(Optional) A map of tags as key=value pairs. All tags will be added on all AutoScaling Group instances"
88+
description = "(Optional) A map of tags as key=value pairs. All tags will be added on all Auto Scaling Group instances"
8989
default = {}
9090
}
9191
variable "metadata_imdsv2_required" {

modules/autoscale_gwlb/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ provider "aws" {}
2626
module "example_module" {
2727
2828
source = "CheckPointSW/cloudguard-network-security/aws//modules/autoscale_gwlb"
29-
version = "1.0.2"
29+
version = "1.0.4"
3030
3131
// --- Environment ---
3232
prefix = "env1"

modules/autoscale_gwlb/asg_userdata.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ bootcmd:
2626
- echo "cpprod_util CPPROD_SetValue \"fw1\" \"AwsGwlb\" 4 1 1" >> /etc/rc.local
2727
runcmd:
2828
- |
29-
python3 /etc/cloud_config.py enableCloudWatch=\"${EnableCloudWatch}\" sicKey=\"${SICKey}\" installationType=\"autoscale\" osVersion=\"${OsVersion}\" allowUploadDownload=\"${AllowUploadDownload}\" templateVersion=\"1.0.2\" templateName=\"autoscale_gwlb\" templateType=\"terraform_registry\" shell=\"${Shell}\" enableInstanceConnect=\"${EnableInstanceConnect}\" passwordHash=\"${PasswordHash}\" MaintenanceModePassword=\"${MaintenanceModePassword}\" bootstrapScript64=\"${BootstrapScript}\"
29+
python3 /etc/cloud_config.py enableCloudWatch=\"${EnableCloudWatch}\" sicKey=\"${SICKey}\" installationType=\"autoscale\" osVersion=\"${OsVersion}\" allowUploadDownload=\"${AllowUploadDownload}\" templateVersion=\"1.0.4\" templateName=\"autoscale_gwlb\" templateType=\"terraform_registry\" shell=\"${Shell}\" enableInstanceConnect=\"${EnableInstanceConnect}\" passwordHash=\"${PasswordHash}\" MaintenanceModePassword=\"${MaintenanceModePassword}\" bootstrapScript64=\"${BootstrapScript}\"

modules/autoscale_gwlb/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ variable "enable_volume_encryption" {
9797
}
9898
variable "instances_tags" {
9999
type = map(string)
100-
description = "(Optional) A map of tags as key=value pairs. All tags will be added on all AutoScaling Group instances"
100+
description = "(Optional) A map of tags as key=value pairs. All tags will be added on all Auto Scaling Group instances"
101101
default = {}
102102
}
103103
variable "metadata_imdsv2_required" {

modules/autoscale_master/README.md

Lines changed: 168 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#cloud-config
2+
runcmd:
3+
- |
4+
python3 /etc/cloud_config.py enableCloudWatch=\"${EnableCloudWatch}\" sicKey=\"${SICKey}\" installationType=\"autoscale\" osVersion=\"${OsVersion}\" allowUploadDownload=\"${AllowUploadDownload}\" templateVersion=\"1.0.4\" templateName=\"autoscale\" templateType=\"terraform_registry\" shell=\"${Shell}\" enableInstanceConnect=\"${EnableInstanceConnect}\" passwordHash=\"${PasswordHash}\" MaintenanceModePassword=\"${MaintenanceModePassword}\" bootstrapScript64=\"${BootstrapScript}\"

modules/autoscale_master/locals.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
locals {
2+
regex_valid_vpc_cidr = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
3+
// Will fail if var.vpc_cidr is invalid
4+
regex_vpc_cidr = regex(local.regex_valid_vpc_cidr, var.vpc_cidr) == var.vpc_cidr ? 0 : "Variable [vpc_cidr] must be a valid vpc cidr"
5+
6+
asg_name = format("%s%s", var.prefix != "" ? format("%s-", var.prefix) : "", var.asg_name)
7+
create_iam_role = var.enable_cloudwatch ? 1 : 0
8+
9+
gateways_provision_address_type_allowed_values = [
10+
"public",
11+
"private"
12+
]
13+
// Will fail if var.gateways_provision_address_type is invalid
14+
validate_gateways_provision_address_type = index(local.gateways_provision_address_type_allowed_values, var.gateways_provision_address_type)
15+
16+
admin_shell_allowed_values = [
17+
"/etc/cli.sh",
18+
"/bin/bash",
19+
"/bin/csh",
20+
"/bin/tcsh"
21+
]
22+
// Will fail if var.admin_shell is invalid
23+
validate_admin_shell = index(local.admin_shell_allowed_values, var.admin_shell)
24+
25+
regex_valid_key_name = "[\\S\\s]+[\\S]+"
26+
// will fail if var.key_name is invalid
27+
regex_key_name_result=regex(local.regex_valid_key_name, var.key_name) == var.key_name ? 0 : "Variable [key_name] must be a none empty string"
28+
regex_valid_gateway_password_hash = "^[\\$\\./a-zA-Z0-9]*$"
29+
// Will fail if var.gateway_password_hash is invalid
30+
regex_gateway_password_hash = regex(local.regex_valid_gateway_password_hash, var.gateway_password_hash) == var.gateway_password_hash ? 0 : "Variable [gateway_password_hash] must be a valid password hash"
31+
regex_gateway_maintenance_mode_password_hash = regex(local.regex_valid_gateway_password_hash, var.gateway_maintenance_mode_password_hash) == var.gateway_maintenance_mode_password_hash ? 0 : "Variable [gateway_maintenance_mode_password_hash] must be a valid password hash"
32+
33+
regex_valid_sic_key = "^[a-zA-Z0-9]{8,}$"
34+
// Will fail if var.gateway_SICKey is invalid
35+
regex_sic_result = regex(local.regex_valid_sic_key, var.gateway_SICKey) == var.gateway_SICKey ? 0 : "Variable [gateway_SICKey] must be at least 8 alphanumeric characters"
36+
37+
proxy_elb_type_allowed_values = [
38+
"none",
39+
"internal",
40+
"internet-facing"
41+
]
42+
// Will fail if var.proxy_elb_type is invalid
43+
validate_proxy_elb_type = index(local.proxy_elb_type_allowed_values, var.proxy_elb_type)
44+
45+
regex_valid_cidr_range = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(/(3[0-2]|2[0-9]|1[0-9]|[0-9]))?$"
46+
// Will fail if var.proxy_elb_clients is invalid
47+
regex_cidr_result = regex(local.regex_valid_cidr_range, var.proxy_elb_clients) == var.proxy_elb_clients ? 0 : "Variable [proxy_elb_clients] must be a valid CIDR range"
48+
49+
tags_asg_format = null_resource.tags_as_list_of_maps.*.triggers
50+
51+
//Splits the version and licence and returns the os version
52+
version_split = element(split("-", var.gateway_version), 0)
53+
gateway_bootstrap_script64 = base64encode(var.gateway_bootstrap_script)
54+
gateway_password_hash_base64 = base64encode(var.gateway_password_hash)
55+
maintenance_mode_password_hash_base64 = base64encode(var.gateway_maintenance_mode_password_hash)
56+
gateway_SICkey_base64 = base64encode(var.gateway_SICKey)
57+
}
58+
resource "null_resource" "tags_as_list_of_maps" {
59+
count = length(keys(var.instances_tags))
60+
61+
triggers = {
62+
"key" = keys(var.instances_tags)[count.index]
63+
"value" = values(var.instances_tags)[count.index]
64+
"propagate_at_launch" = "true"
65+
}
66+
}

modules/autoscale_master/main.tf

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
module "launch_vpc" {
2+
source = "../vpc"
3+
4+
vpc_cidr = var.vpc_cidr
5+
public_subnets_map = var.public_subnets_map
6+
private_subnets_map = var.private_subnets_map
7+
subnets_bit_length = var.subnets_bit_length
8+
}
9+
10+
module "amis" {
11+
source = "../amis"
12+
13+
version_license = var.gateway_version
14+
}
15+
16+
resource "aws_security_group" "permissive_sg" {
17+
name_prefix = format("%s_PermissiveSecurityGroup", local.asg_name)
18+
description = "Permissive security group"
19+
vpc_id = module.launch_vpc.vpc_id
20+
21+
dynamic "ingress" {
22+
for_each = [for rule in var.security_rules : rule if rule.direction == "ingress"]
23+
content {
24+
from_port = ingress.value.from_port
25+
to_port = ingress.value.to_port
26+
protocol = ingress.value.protocol
27+
cidr_blocks = ingress.value.cidr_blocks
28+
}
29+
}
30+
31+
dynamic ingress {
32+
for_each = length([for rule in var.security_rules : rule if rule.direction == "ingress"]) == 0 ? [1] : []
33+
content{
34+
from_port = 0
35+
to_port = 0
36+
protocol = "-1"
37+
cidr_blocks = ["0.0.0.0/0"]
38+
}
39+
}
40+
41+
dynamic "egress" {
42+
for_each = [for rule in var.security_rules : rule if rule.direction == "egress"]
43+
content {
44+
from_port = egress.value.from_port
45+
to_port = egress.value.to_port
46+
protocol = egress.value.protocol
47+
cidr_blocks = egress.value.cidr_blocks
48+
}
49+
}
50+
51+
dynamic egress {
52+
for_each = length([for rule in var.security_rules : rule if rule.direction == "egress"]) == 0 ? [1] : []
53+
content{
54+
from_port = 0
55+
to_port = 0
56+
protocol = "-1"
57+
cidr_blocks = ["0.0.0.0/0"]
58+
}
59+
}
60+
tags = {
61+
Name = format("%s_PermissiveSecurityGroup", local.asg_name)
62+
}
63+
}
64+
65+
resource "aws_launch_template" "asg_launch_template" {
66+
name_prefix = local.asg_name
67+
image_id = module.amis.ami_id
68+
instance_type = var.gateway_instance_type
69+
key_name = var.key_name
70+
network_interfaces {
71+
associate_public_ip_address = true
72+
security_groups = [aws_security_group.permissive_sg.id]
73+
}
74+
75+
metadata_options {
76+
http_tokens = var.metadata_imdsv2_required ? "required" : "optional"
77+
}
78+
79+
iam_instance_profile {
80+
name = ( var.enable_cloudwatch ? aws_iam_instance_profile.instance_profile[0].name : "")
81+
}
82+
monitoring {
83+
enabled = true
84+
}
85+
86+
block_device_mappings {
87+
device_name = "/dev/xvda"
88+
ebs {
89+
volume_type = "gp3"
90+
volume_size = var.volume_size
91+
encrypted = var.enable_volume_encryption
92+
}
93+
}
94+
description = "Initial template version"
95+
96+
97+
user_data = base64encode(templatefile("${path.module}/asg_userdata.yaml", {
98+
// script's arguments
99+
PasswordHash = local.gateway_password_hash_base64,
100+
MaintenanceModePassword = local.maintenance_mode_password_hash_base64
101+
EnableCloudWatch = var.enable_cloudwatch,
102+
EnableInstanceConnect = var.enable_instance_connect,
103+
Shell = var.admin_shell,
104+
SICKey = local.gateway_SICkey_base64,
105+
AllowUploadDownload = var.allow_upload_download,
106+
BootstrapScript = local.gateway_bootstrap_script64,
107+
OsVersion = local.version_split
108+
}))
109+
}
110+
resource "aws_autoscaling_group" "asg" {
111+
name_prefix = local.asg_name
112+
launch_template {
113+
id = aws_launch_template.asg_launch_template.id
114+
version = aws_launch_template.asg_launch_template.latest_version
115+
}
116+
min_size = var.minimum_group_size
117+
max_size = var.maximum_group_size
118+
load_balancers = aws_elb.proxy_elb.*.name
119+
target_group_arns = var.target_groups
120+
vpc_zone_identifier = module.launch_vpc.public_subnets_ids_list
121+
health_check_grace_period = 3600
122+
health_check_type = "ELB"
123+
124+
tag {
125+
key = "Name"
126+
value = format("%s%s", var.prefix != "" ? format("%s-", var.prefix) : "", var.gateway_name)
127+
propagate_at_launch = true
128+
}
129+
130+
tag {
131+
key = "x-chkp-tags"
132+
value = format("management=%s:template=%s:ip-address=%s", var.management_server, var.configuration_template, var.gateways_provision_address_type)
133+
propagate_at_launch = true
134+
}
135+
136+
dynamic "tag" {
137+
for_each = var.instances_tags
138+
content {
139+
key = tag.key
140+
value = tag.value
141+
propagate_at_launch = true
142+
}
143+
}
144+
}
145+
146+
data "aws_iam_policy_document" "assume_role_policy_document" {
147+
version = "2012-10-17"
148+
statement {
149+
actions = ["sts:AssumeRole"]
150+
principals {
151+
type = "Service"
152+
identifiers = ["ec2.amazonaws.com"]
153+
}
154+
effect = "Allow"
155+
}
156+
}
157+
158+
resource "aws_iam_role" "role" {
159+
count = local.create_iam_role
160+
name_prefix = format("%s-iam_role", local.asg_name)
161+
assume_role_policy = data.aws_iam_policy_document.assume_role_policy_document.json
162+
path = "/"
163+
}
164+
module "attach_cloudwatch_policy" {
165+
source = "../cloudwatch_policy"
166+
count = local.create_iam_role
167+
role = aws_iam_role.role[count.index].name
168+
tag_name = local.asg_name
169+
}
170+
171+
resource "aws_iam_instance_profile" "instance_profile" {
172+
count = local.create_iam_role
173+
name_prefix = format("%s-iam_instance_profile", local.asg_name)
174+
path = "/"
175+
role = aws_iam_role.role[count.index].name
176+
}
177+
178+
// Proxy ELB
179+
locals {
180+
proxy_elb_condition = var.proxy_elb_type != "none" ? 1 : 0
181+
}
182+
resource "random_id" "proxy_elb_uuid" {
183+
byte_length = 5
184+
}
185+
resource "aws_elb" "proxy_elb" {
186+
count = local.proxy_elb_condition
187+
name = format("%s-proxy-elb-%s", var.prefix, random_id.proxy_elb_uuid.hex)
188+
internal = var.proxy_elb_type == "internal"
189+
cross_zone_load_balancing = true
190+
listener {
191+
instance_port = var.proxy_elb_port
192+
instance_protocol = "TCP"
193+
lb_port = var.proxy_elb_port
194+
lb_protocol = "TCP"
195+
}
196+
health_check {
197+
target = format("TCP:%s", var.proxy_elb_port)
198+
healthy_threshold = 3
199+
unhealthy_threshold = 5
200+
interval = 30
201+
timeout = 5
202+
}
203+
subnets = module.launch_vpc.public_subnets_ids_list
204+
security_groups = [aws_security_group.elb_security_group[count.index].id]
205+
}
206+
resource "aws_load_balancer_policy" "proxy_elb_policy" {
207+
count = local.proxy_elb_condition
208+
load_balancer_name = aws_elb.proxy_elb[count.index].name
209+
policy_name = "EnableProxyProtocol"
210+
policy_type_name = "ProxyProtocolPolicyType"
211+
212+
policy_attribute {
213+
name = "ProxyProtocol"
214+
value = "true"
215+
}
216+
}
217+
resource "aws_security_group" "elb_security_group" {
218+
count = local.proxy_elb_condition
219+
description = "ELB security group"
220+
vpc_id = module.launch_vpc.vpc_id
221+
egress {
222+
from_port = 0
223+
to_port = 0
224+
protocol = "-1"
225+
cidr_blocks = ["0.0.0.0/0"]
226+
}
227+
ingress {
228+
protocol = "tcp"
229+
cidr_blocks = [var.proxy_elb_clients]
230+
from_port = var.proxy_elb_port
231+
to_port = var.proxy_elb_port
232+
}
233+
}
234+
235+
// Scaling metrics
236+
resource "aws_cloudwatch_metric_alarm" "cpu_alarm_low" {
237+
alarm_name = format("%s_alarm_low", aws_autoscaling_group.asg.name)
238+
metric_name = "CPUUtilization"
239+
alarm_description = "Scale-down if CPU < 60% for 10 minutes"
240+
namespace = "AWS/EC2"
241+
statistic = "Average"
242+
period = 300
243+
evaluation_periods = 2
244+
threshold = 60
245+
alarm_actions = [aws_autoscaling_policy.scale_down_policy.arn]
246+
dimensions = {
247+
AutoScalingGroupName = aws_autoscaling_group.asg.name
248+
}
249+
comparison_operator = "LessThanThreshold"
250+
}
251+
resource "aws_autoscaling_policy" "scale_down_policy" {
252+
autoscaling_group_name = aws_autoscaling_group.asg.name
253+
name = format("%s_scale_down", aws_autoscaling_group.asg.name)
254+
adjustment_type = "ChangeInCapacity"
255+
cooldown = 300
256+
scaling_adjustment = -1
257+
}
258+
resource "aws_cloudwatch_metric_alarm" "cpu_alarm_high" {
259+
alarm_name = format("%s_alarm_high", aws_autoscaling_group.asg.name)
260+
metric_name = "CPUUtilization"
261+
alarm_description = "Scale-up if CPU > 80% for 10 minutes"
262+
namespace = "AWS/EC2"
263+
statistic = "Average"
264+
period = 300
265+
evaluation_periods = 2
266+
threshold = 80
267+
alarm_actions = [aws_autoscaling_policy.scale_up_policy.arn]
268+
dimensions = {
269+
AutoScalingGroupName = aws_autoscaling_group.asg.name
270+
}
271+
comparison_operator = "GreaterThanThreshold"
272+
}
273+
resource "aws_autoscaling_policy" "scale_up_policy" {
274+
autoscaling_group_name = aws_autoscaling_group.asg.name
275+
name = format("%s_scale_up", aws_autoscaling_group.asg.name)
276+
adjustment_type = "ChangeInCapacity"
277+
cooldown = 300
278+
scaling_adjustment = 1
279+
}

0 commit comments

Comments
 (0)