-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
273 lines (237 loc) · 10.7 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
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
locals {
org_integration = var.integration_type == "ORGANIZATION"
project_id = length(var.project_id) > 0 ? var.project_id : data.google_project.selected[0].project_id
sink_name = length(var.existing_sink_name) > 0 ? var.existing_sink_name : (
local.org_integration ? "${var.prefix}-${var.organization_id}-lacework-sink-${random_id.uniq.hex}" : "${var.prefix}-lacework-sink-${random_id.uniq.hex}"
)
exclude_folders = length(var.folders_to_exclude) != 0
explicit_folders = length(var.folders_to_include) != 0
logging_sink_writer_identity = length(var.existing_sink_name) > 0 ? ["serviceAccount:${local.service_account_json_key.client_email}"] : (
(local.org_integration && !(local.exclude_folders || local.explicit_folders)) ? (
[google_logging_organization_sink.lacework_organization_sink[0].writer_identity]
) : (
(local.org_integration && (local.exclude_folders || local.explicit_folders)) ? (
concat(
[for v in google_logging_folder_sink.lacework_folder_sink : v.writer_identity],
[for v in google_logging_project_sink.lacework_root_project_sink : v.writer_identity]
)
) : (
[google_logging_project_sink.lacework_project_sink[0].writer_identity]
)
)
)
service_account_name = var.use_existing_service_account ? (
var.service_account_name
) : (
length(var.service_account_name) > 0 ? var.service_account_name : "${var.prefix}-${random_id.uniq.hex}"
)
service_account_json_key = jsondecode(var.use_existing_service_account ? (
base64decode(var.service_account_private_key)
) : (
base64decode(module.lacework_al_ps_svc_account.private_key)
))
log_filter_map = {
default = "(protoPayload.@type=type.googleapis.com/google.cloud.audit.AuditLog) AND NOT (protoPayload.methodName:\"storage.objects\")"
k8s_only = "(protoPayload.@type=type.googleapis.com/google.cloud.audit.AuditLog) AND NOT (protoPayload.serviceName=\"k8s.io\") AND NOT (protoPayload.methodName:\"storage.objects\")"
workspace_only = "(protoPayload.@type=type.googleapis.com/google.cloud.audit.AuditLog) AND NOT (protoPayload.methodName:\"storage.objects\") AND NOT (protoPayload.serviceName:\"login.googleapis.com\")"
k8s_workspace_combined = "(protoPayload.@type=type.googleapis.com/google.cloud.audit.AuditLog) AND NOT (protoPayload.serviceName=\"k8s.io\") AND NOT (protoPayload.serviceName:\"login.googleapis.com\") AND NOT (protoPayload.methodName:\"storage.objects\")"
}
log_filter = length(var.custom_filter) > 0 ? (var.custom_filter) : (
!var.k8s_filter && !var.google_workspace_filter ? ("${lookup(local.log_filter_map, "default")}") : (
var.k8s_filter && !var.google_workspace_filter ?
"${lookup(local.log_filter_map, "k8s_only")}" : (
!var.k8s_filter && var.google_workspace_filter ?
"${lookup(local.log_filter_map, "workspace_only")}" : (
"${lookup(local.log_filter_map, "k8s_workspace_combined")}"
)
)
)
)
folders = [
# if setting up an org-level integration and excluding folders
(local.org_integration && local.exclude_folders) ? (
# for the folders in the given org, get just the folders that haven't been excluded
setsubtract(data.google_folders.my-org-folders[0].folders[*].name, var.folders_to_exclude)
) : ( # org integration but NOT excluding folders, including them; use the supplied foldes to include
local.org_integration && local.explicit_folders) ? (
var.folders_to_include
) : (
# if we got this far, this is an org integration but no folder inclusions or exclusions have been set
# we'll return an empty set but the logic for lacework_organization_sink will deploy an org-wide sink
# that'll catch all folders (not true if inclusions/exclusions are set)
toset([])
)
]
root_projects = [
(local.org_integration && local.exclude_folders && var.include_root_projects) ? (
toset(data.google_projects.my-org-projects[0].projects[*].project_id)
) : (
toset([])
)
]
version_file = "${abspath(path.module)}/VERSION"
module_name = "terraform-gcp-pub-sub-audit-log"
module_version = fileexists(local.version_file) ? file(local.version_file) : ""
}
resource "random_id" "uniq" {
byte_length = 4
}
data "google_project" "selected" {
count = length(var.project_id) > 0 ? 0 : 1
}
data "google_folders" "my-org-folders" {
count = (local.org_integration && local.exclude_folders) ? 1 : 0
parent_id = "organizations/${var.organization_id}"
}
data "google_projects" "my-org-projects" {
count = (local.exclude_folders && var.include_root_projects) ? 1 : 0
filter = "parent.id=${var.organization_id}"
}
resource "google_project_service" "required_apis" {
for_each = var.required_apis
project = local.project_id
service = each.value
disable_on_destroy = false
}
module "lacework_al_ps_svc_account" {
source = "lacework/service-account/gcp"
version = "~> 2.0"
create = var.use_existing_service_account ? false : true
service_account_name = local.service_account_name
project_id = local.project_id
}
resource "google_pubsub_topic" "lacework_topic" {
name = "${var.prefix}-lacework-topic-${random_id.uniq.hex}"
project = local.project_id
depends_on = [google_project_service.required_apis]
labels = merge(var.labels, var.pubsub_topic_labels)
}
resource "google_pubsub_topic_iam_binding" "topic_publisher" {
members = local.logging_sink_writer_identity
role = "roles/pubsub.publisher"
project = local.project_id
topic = google_pubsub_topic.lacework_topic.name
depends_on = [google_pubsub_topic.lacework_topic]
}
resource "google_pubsub_subscription" "lacework_subscription" {
project = local.project_id
name = "${var.prefix}-${local.project_id}-lacework-subscription-${random_id.uniq.hex}"
topic = google_pubsub_topic.lacework_topic.name
ack_deadline_seconds = 300
message_retention_duration = "432000s"
labels = merge(var.labels, var.pubsub_subscription_labels)
depends_on = [google_pubsub_topic.lacework_topic]
}
resource "google_logging_project_sink" "lacework_project_sink" {
count = length(var.existing_sink_name) > 0 ? 0 : (local.org_integration ? 0 : 1)
project = local.project_id
name = local.sink_name
destination = "pubsub.googleapis.com/${google_pubsub_topic.lacework_topic.id}"
unique_writer_identity = true
filter = local.log_filter
depends_on = [google_pubsub_topic.lacework_topic]
}
resource "google_logging_project_sink" "lacework_root_project_sink" {
for_each = local.root_projects[0]
project = each.value
name = local.sink_name
destination = "pubsub.googleapis.com/${google_pubsub_topic.lacework_topic.id}"
unique_writer_identity = true
filter = local.log_filter
depends_on = [google_pubsub_topic.lacework_topic]
}
resource "google_logging_folder_sink" "lacework_folder_sink" {
for_each = local.folders[0]
name = local.sink_name
folder = each.value
destination = "pubsub.googleapis.com/${google_pubsub_topic.lacework_topic.id}"
include_children = true
filter = local.log_filter
}
resource "google_logging_organization_sink" "lacework_organization_sink" {
count = length(var.existing_sink_name) > 0 ? 0 : ((local.org_integration && !(local.exclude_folders || local.explicit_folders) ? 1 : 0))
name = local.sink_name
org_id = var.organization_id
destination = "pubsub.googleapis.com/${google_pubsub_topic.lacework_topic.id}"
include_children = true
filter = local.log_filter
depends_on = [google_pubsub_topic.lacework_topic]
}
resource "google_pubsub_subscription_iam_binding" "lacework" {
project = local.project_id
role = "roles/pubsub.subscriber"
members = ["serviceAccount:${local.service_account_json_key.client_email}"]
subscription = google_pubsub_subscription.lacework_subscription.name
depends_on = [google_pubsub_subscription.lacework_subscription]
}
resource "google_project_iam_audit_config" "project_audit_logs" {
project = local.project_id
service = "container.googleapis.com"
audit_log_config {
log_type = "ADMIN_READ"
}
audit_log_config {
log_type = "DATA_READ"
}
audit_log_config {
log_type = "DATA_WRITE"
}
}
resource "google_organization_iam_audit_config" "organization_audit_logs" {
count = local.org_integration ? 1 : 0
org_id = var.organization_id
service = "container.googleapis.com"
audit_log_config {
log_type = "ADMIN_READ"
}
audit_log_config {
log_type = "DATA_READ"
}
audit_log_config {
log_type = "DATA_WRITE"
}
}
resource "google_project_iam_member" "for_lacework_service_account" {
project = local.project_id
role = "roles/monitoring.viewer"
member = "serviceAccount:${local.service_account_json_key.client_email}"
}
resource "google_organization_iam_member" "for_lacework_service_account" {
count = local.org_integration ? 1 : 0
org_id = var.organization_id
role = "roles/browser"
member = "serviceAccount:${local.service_account_json_key.client_email}"
}
# wait for X seconds for things to settle down in the GCP side
# before trying to create the Lacework external integration
resource "time_sleep" "wait_time" {
create_duration = var.wait_time
depends_on = [
google_pubsub_subscription_iam_binding.lacework,
module.lacework_al_ps_svc_account,
google_project_iam_audit_config.project_audit_logs,
google_organization_iam_audit_config.organization_audit_logs,
google_project_iam_member.for_lacework_service_account,
google_organization_iam_member.for_lacework_service_account
]
}
resource "lacework_integration_gcp_pub_sub_audit_log" "default" {
count = var.skip_create_lacework_integration ? 0 : 1
name = var.lacework_integration_name
integration_type = var.integration_type
project_id = local.project_id
organization_id = var.organization_id
subscription_id = google_pubsub_subscription.lacework_subscription.id
topic_id = google_pubsub_topic.lacework_topic.id
credentials {
client_id = local.service_account_json_key.client_id
private_key_id = local.service_account_json_key.private_key_id
client_email = local.service_account_json_key.client_email
private_key = local.service_account_json_key.private_key
}
depends_on = [time_sleep.wait_time]
}
data "lacework_metric_module" "lwmetrics" {
name = local.module_name
version = local.module_version
}