-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
196 lines (171 loc) · 5.01 KB
/
main.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
provider "google" {
project = var.project
region = var.region
zone = var.zone
}
locals {
use_custom_rules = var.custom_rules_path != ""
rule_file = "/opt/my.rules"
rule_entry = local.use_custom_rules ? " - ${local.rule_file}" : ""
custom_rule_bucket = local.use_custom_rules ? split("/", var.custom_rules_path)[2] : ""
default_suricata_config = templatefile("${path.module}/templates/suricata.yaml", {
rule_entry = local.rule_entry
})
suricata_config = var.suricata_config_path != "" ? file(var.suricata_config_path) : local.default_suricata_config
}
resource "google_compute_instance_template" "ids" {
name_prefix = "${var.prefix}-"
machine_type = "e2-medium"
disk {
source_image = "debian-cloud/debian-9"
auto_delete = true
boot = true
}
metadata_startup_script = templatefile("${path.module}/templates/startup.sh", {
fast_config = var.enable_fast_export ? file("${path.module}/templates/fast.conf") : ""
eve_config = var.enable_eve_export ? file("${path.module}/templates/eve.conf") : ""
suricata_config = local.suricata_config
custom_rules_path = var.custom_rules_path
rule_file = local.rule_file
})
service_account {
email = google_service_account.ids.email
scopes = ["cloud-platform"]
}
network_interface {
subnetwork = var.subnet
}
lifecycle {
create_before_destroy = true
}
}
resource "google_compute_instance_group_manager" "ids" {
name = "${var.prefix}-igm"
base_instance_name = var.prefix
zone = var.zone
target_size = 1
version {
name = "suricata-v1"
instance_template = google_compute_instance_template.ids.id
}
auto_healing_policies {
health_check = google_compute_health_check.ids.id
initial_delay_sec = 300
}
}
resource "google_compute_packet_mirroring" "ids" {
name = "${var.prefix}-mirroring"
description = "Packet mirror for Suricata"
network {
url = var.network
}
collector_ilb {
url = google_compute_forwarding_rule.ids.id
}
mirrored_resources {
tags = var.target_tags
dynamic "subnetworks" {
for_each = var.target_subnets
content {
url = subnetworks.value
}
}
dynamic "instances" {
for_each = var.target_instances
content {
url = instances.value
}
}
}
filter {
ip_protocols = var.filter.ip_protocols
cidr_ranges = var.filter.cidr_ranges
direction = var.filter.direction
}
}
resource "google_compute_region_backend_service" "ids" {
name = "${var.prefix}-svc"
health_checks = [google_compute_health_check.ids.id]
backend {
group = google_compute_instance_group_manager.ids.instance_group
balancing_mode = "CONNECTION"
}
}
resource "google_compute_health_check" "ids" {
name = "${var.prefix}-healthcheck"
check_interval_sec = 300
timeout_sec = 20
tcp_health_check {
port = "80"
}
}
resource "google_compute_forwarding_rule" "ids" {
name = "${var.prefix}-ilb"
is_mirroring_collector = true
ip_protocol = "TCP"
load_balancing_scheme = "INTERNAL"
backend_service = google_compute_region_backend_service.ids.id
all_ports = true
network = var.network
subnetwork = var.subnet
network_tier = "PREMIUM"
}
resource "google_service_account" "ids" {
account_id = var.prefix
display_name = "Suricata Service Account"
}
resource "google_project_iam_member" "ids" {
for_each = toset([
"roles/logging.logWriter",
"roles/monitoring.metricWriter",
])
role = each.value
member = "serviceAccount:${google_service_account.ids.email}"
}
resource "google_storage_bucket_iam_member" "ids" {
count = local.use_custom_rules ? 1 : 0
bucket = local.custom_rule_bucket
role = "roles/storage.objectViewer"
member = "serviceAccount:${google_service_account.ids.email}"
}
resource "google_compute_firewall" "ids" {
name = "allow-all-to-suricata-except-ssh"
network = var.network
priority = var.base_priority
allow {
protocol = "all"
}
source_ranges = ["0.0.0.0/0"]
target_service_accounts = [google_service_account.ids.email]
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}
resource "google_compute_firewall" "ids_deny_ssh" {
name = "deny-ssh-to-suricata"
network = var.network
priority = var.base_priority + 1
deny {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["35.235.240.0/20"]
target_service_accounts = [google_service_account.ids.email]
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}
resource "google_compute_firewall" "ids_iap" {
name = "allow-iap-to-suricata"
network = var.network
priority = var.base_priority + 2
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["35.235.240.0/20"]
target_service_accounts = [google_service_account.ids.email]
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}