-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.tf
305 lines (264 loc) · 11.5 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
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
locals {
resource_level = var.org_integration ? "ORGANIZATION" : "PROJECT"
resource_id = var.org_integration ? var.organization_id : module.lacework_at_svc_account.project_id
project_id = length(var.project_id) > 0 ? var.project_id : data.google_project.selected[0].project_id
bucket_name = length(var.existing_bucket_name) > 0 ? var.existing_bucket_name : (
length(google_storage_bucket.lacework_bucket) > 0 ? google_storage_bucket.lacework_bucket[0].name : var.existing_bucket_name
)
sink_name = length(var.existing_sink_name) > 0 ? var.existing_sink_name : (
var.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 ? null : (
(var.org_integration && !(local.exclude_folders || local.explicit_folders)) ? (
[google_logging_organization_sink.lacework_organization_sink[0].writer_identity]
) : (
(var.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_at_svc_account.private_key)
))
bucket_roles = length(var.existing_sink_name) > 0 ? (
{
"roles/storage.objectViewer" = [
"serviceAccount:${local.service_account_json_key.client_email}"
]
}) : (
{
"roles/storage.admin" = [
"projectEditor:${local.project_id}",
"projectOwner:${local.project_id}"
],
"roles/storage.objectCreator" = local.logging_sink_writer_identity,
"roles/storage.objectViewer" = [
"serviceAccount:${local.service_account_json_key.client_email}",
"projectViewer:${local.project_id}"
]
})
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 = [
(var.org_integration && local.exclude_folders) ? (
setsubtract(data.google_folders.my-org-folders[0].folders[*].name, var.folders_to_exclude)
) : (
var.org_integration && local.explicit_folders) ? (
var.folders_to_include
) : (
toset([])
)
]
root_projects = [
(var.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-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 = (var.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_at_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_storage_bucket" "lacework_bucket" {
count = length(var.existing_bucket_name) > 0 ? 0 : 1
project = local.project_id
name = coalesce(var.custom_bucket_name, "${var.prefix}-${random_id.uniq.hex}")
force_destroy = var.bucket_force_destroy
location = var.bucket_region
depends_on = [google_project_service.required_apis]
uniform_bucket_level_access = var.enable_ubla
dynamic "lifecycle_rule" {
for_each = var.lifecycle_rule_age > 0 ? [1] : []
content {
condition {
age = var.lifecycle_rule_age
}
action {
type = "Delete"
}
}
}
labels = merge(var.labels, var.bucket_labels)
}
resource "google_storage_bucket_iam_binding" "policies" {
for_each = local.bucket_roles
role = each.key
members = each.value
bucket = local.bucket_name
}
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)
}
# By calling this data source we are accessing the storage service
# account and therefore, Google will created for us. If we don't
# ask for it, Google doesn't create it by default, more docs at:
# => https://cloud.google.com/storage/docs/projects#service-accounts
#
# If the service account is not there, we could add a local-exec
# provisioner to call an API that is documented at:
# => https://cloud.google.com/storage-transfer/docs/reference/rest/v1/googleServiceAccounts/get
data "google_storage_project_service_account" "lw" {
project = local.project_id
}
resource "google_pubsub_topic_iam_binding" "topic_publisher" {
members = ["serviceAccount:${data.google_storage_project_service_account.lw.email_address}"]
role = "roles/pubsub.publisher"
project = local.project_id
topic = google_pubsub_topic.lacework_topic.name
}
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)
}
resource "google_logging_project_sink" "lacework_project_sink" {
count = length(var.existing_sink_name) > 0 ? 0 : (var.org_integration ? 0 : 1)
project = local.project_id
name = local.sink_name
destination = "storage.googleapis.com/${local.bucket_name}"
unique_writer_identity = true
filter = local.log_filter
}
resource "google_logging_organization_sink" "lacework_organization_sink" {
count = length(var.existing_sink_name) > 0 ? 0 : ((var.org_integration && !(local.exclude_folders || local.explicit_folders) ? 1 : 0))
name = local.sink_name
org_id = var.organization_id
destination = "storage.googleapis.com/${local.bucket_name}"
include_children = true
filter = local.log_filter
}
resource "google_logging_folder_sink" "lacework_folder_sink" {
for_each = local.folders[0]
name = local.sink_name
folder = each.value
destination = "storage.googleapis.com/${local.bucket_name}"
include_children = true
filter = local.log_filter
}
resource "google_logging_project_sink" "lacework_root_project_sink" {
for_each = local.root_projects[0]
project = each.value
name = local.sink_name
destination = "storage.googleapis.com/${local.bucket_name}"
unique_writer_identity = true
filter = local.log_filter
}
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
}
resource "google_storage_notification" "lacework_notification" {
bucket = local.bucket_name
payload_format = "JSON_API_V1"
topic = google_pubsub_topic.lacework_topic.id
event_types = ["OBJECT_FINALIZE"]
depends_on = [
google_pubsub_topic_iam_binding.topic_publisher,
google_storage_bucket_iam_binding.policies
]
}
resource "google_project_iam_member" "for_lacework_service_account" {
project = local.project_id
role = "roles/storage.objectViewer"
member = "serviceAccount:${local.service_account_json_key.client_email}"
}
resource "google_organization_iam_member" "for_lacework_service_account" {
count = var.org_integration ? 1 : 0
org_id = var.organization_id
role = "roles/resourcemanager.organizationViewer"
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_storage_notification.lacework_notification,
google_pubsub_subscription_iam_binding.lacework,
module.lacework_at_svc_account,
google_project_iam_member.for_lacework_service_account,
google_organization_iam_member.for_lacework_service_account
]
}
resource "lacework_integration_gcp_at" "default" {
name = var.lacework_integration_name
resource_id = local.resource_id
resource_level = local.resource_level
subscription = google_pubsub_subscription.lacework_subscription.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
}