-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflow_logs.tf
217 lines (200 loc) · 9.82 KB
/
flow_logs.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
# Copyright (c) 2023 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
locals {
flow_logs_target_types = ["vcn","subnet","vnic"]
flow_logs_compartment_ids = flatten([
for fl_key, fl_value in (var.logging_configuration.flow_logs != null ? var.logging_configuration.flow_logs : {}) : [
for cmp_id in fl_value.target_compartment_ids : [(length(regexall("^ocid1.*$", cmp_id)) > 0 ? cmp_id : var.compartments_dependency[cmp_id].id)]
]
])
subnets_flow_logs = flatten([
for fl_key, fl_value in (var.logging_configuration.flow_logs != null ? var.logging_configuration.flow_logs : {}) : [
for cmp_id in fl_value.target_compartment_ids : [
for subnet in coalesce(data.oci_core_subnets.these[(length(regexall("^ocid1.*$", cmp_id)) > 0 ? cmp_id : var.compartments_dependency[cmp_id].id)].subnets, []) : {
key = upper("${fl_key}-${replace(subnet.display_name,"/\\s+/","-")}-${substr(subnet.id,-10,-1)}")
category = "subnet"
resource_id = subnet.id
service = "flowlogs"
name = "${replace(subnet.display_name,"/\\s+/","-")}-${substr(subnet.id,-10,-1)}-flow-log"
log_group_id = fl_value.log_group_id
is_enabled = fl_value.is_enabled
retention_duration = fl_value.retention_duration
defined_tags = fl_value.defined_tags
freeform_tags = fl_value.freeform_tags
target_resource_type = fl_value.target_resource_type
enable_cis_checks = var.logging_configuration.enable_cis_checks
}
]
] if lower(fl_value.target_resource_type) == "subnet"
])
vcns_flow_logs = flatten([
for fl_key, fl_value in (var.logging_configuration.flow_logs != null ? var.logging_configuration.flow_logs : {}) : [
for cmp_id in fl_value.target_compartment_ids : [
for vcn in coalesce(data.oci_core_vcns.these[(length(regexall("^ocid1.*$", cmp_id)) > 0 ? cmp_id : var.compartments_dependency[cmp_id].id)].virtual_networks, []) : {
key = upper("${fl_key}-${replace(vcn.display_name,"/\\s+/","-")}-${substr(vcn.id,-10,-1)}")
category = "vcn"
resource_id = vcn.id
service = "flowlogs"
name = "${replace(vcn.display_name,"/\\s+/","-")}-${substr(vcn.id,-10,-1)}-flow-log"
log_group_id = fl_value.log_group_id
is_enabled = fl_value.is_enabled
retention_duration = fl_value.retention_duration
defined_tags = fl_value.defined_tags
freeform_tags = fl_value.freeform_tags
target_resource_type = fl_value.target_resource_type
enable_cis_checks = var.logging_configuration.enable_cis_checks
}
]
] if lower(fl_value.target_resource_type) == "vcn"
])
vnics_flow_logs = flatten([
for fl_key, fl_value in (var.logging_configuration.flow_logs != null ? var.logging_configuration.flow_logs : {}) : [
for cmp_id in fl_value.target_compartment_ids : [
for attach in coalesce(data.oci_core_vnic_attachments.these[(length(regexall("^ocid1.*$", cmp_id)) > 0 ? cmp_id : var.compartments_dependency[cmp_id].id)].vnic_attachments, []) : {
key = upper("${fl_key}-${replace(data.oci_core_vnic.these[attach.vnic_id].display_name,"/\\s+/","-")}")
category = "vnic"
resource_id = attach.vnic_id
service = "flowlogs"
name = "${replace(data.oci_core_vnic.these[attach.vnic_id].display_name,"/\\s+/","-")}-flow-log"
log_group_id = fl_value.log_group_id
is_enabled = fl_value.is_enabled
retention_duration = fl_value.retention_duration
defined_tags = fl_value.defined_tags
freeform_tags = fl_value.freeform_tags
target_resource_type = fl_value.target_resource_type
enable_cis_checks = var.logging_configuration.enable_cis_checks
}
]
] if lower(fl_value.target_resource_type) == "vnic"
])
vnics_ids = flatten([
for fl_key, fl_value in (var.logging_configuration.flow_logs != null ? var.logging_configuration.flow_logs : {}) : [
for cmp_id in fl_value.target_compartment_ids : [
for attach in coalesce(data.oci_core_vnic_attachments.these[(length(regexall("^ocid1.*$", cmp_id)) > 0 ? cmp_id : var.compartments_dependency[cmp_id].id)].vnic_attachments, []) : [attach.vnic_id]
]
]
])
nlbs_flow_logs = flatten([
for fl_key, fl_value in (var.logging_configuration.flow_logs != null ? var.logging_configuration.flow_logs : {}) : [
for k, v in coalesce(data.oci_core_private_ips.nlbs, {}) : [
for ip in coalesce(v.private_ips, []) : {
key = upper("${fl_key}-${replace(ip.display_name,"/\\s+/","-")}")
category = "vnic"
resource_id = ip.vnic_id
service = "flowlogs"
name = "${replace(ip.display_name,"/\\s+/","-")}-flow-log"
log_group_id = fl_value.log_group_id
is_enabled = fl_value.is_enabled
retention_duration = fl_value.retention_duration
defined_tags = fl_value.defined_tags
freeform_tags = fl_value.freeform_tags
target_resource_type = fl_value.target_resource_type
enable_cis_checks = var.logging_configuration.enable_cis_checks
}
]
] if lower(fl_value.target_resource_type) == "vnic"
])
nlbs_info = flatten([
for k,v in coalesce(data.oci_network_load_balancer_network_load_balancers.these,{}) : [
for col_elem in coalesce(v.network_load_balancer_collection,[]) : [
for nlb in coalesce(col_elem.items, []) : [
for i in coalesce(nlb.ip_addresses, []) :
{"ip_address" : i.ip_address, "subnet_id": nlb.subnet_id}
]
]
]
])
}
data "oci_identity_compartment" "these" {
for_each = toset(local.flow_logs_compartment_ids)
lifecycle {
postcondition {
condition = (self.id == each.key)
error_message = "VALIDATION FAILURE: compartment id \"${each.key}\" not found."
}
}
id = each.key
}
data "oci_core_subnets" "these" {
for_each = toset(local.flow_logs_compartment_ids)
lifecycle {
precondition {
condition = contains(keys(data.oci_identity_compartment.these),each.key)
error_message = "VALIDATION FAILURE: compartment id \"${each.key}\" not found."
}
}
compartment_id = each.key
}
data "oci_core_vcns" "these" {
for_each = toset(local.flow_logs_compartment_ids)
compartment_id = each.key
}
data "oci_core_vnic_attachments" "these" {
for_each = toset(local.flow_logs_compartment_ids)
lifecycle {
precondition {
condition = contains(keys(data.oci_identity_compartment.these),each.key)
error_message = "VALIDATION FAILURE: compartment id \"${each.key}\" not found."
}
}
compartment_id = each.key
}
data "oci_core_vnic" "these" {
for_each = toset(local.vnics_ids)
vnic_id = each.key
}
data "oci_network_load_balancer_network_load_balancers" "these" {
for_each = toset(local.flow_logs_compartment_ids)
lifecycle {
precondition {
condition = contains(keys(data.oci_identity_compartment.these),each.key)
error_message = "VALIDATION FAILURE: compartment id \"${each.key}\" not found."
}
}
compartment_id = each.key
}
data "oci_core_private_ips" "nlbs" {
for_each = {for v in local.nlbs_info : v.ip_address => v.subnet_id}
ip_address = each.key
subnet_id = each.value
}
resource "oci_logging_log" "flow_logs" {
for_each = { for v in concat(local.subnets_flow_logs, local.vcns_flow_logs, local.vnics_flow_logs, local.nlbs_flow_logs) : v.key => {
category = v.category
resource_id = v.resource_id
service = v.service
name = v.name
log_group_id = v.log_group_id
is_enabled = v.is_enabled
retention_duration = v.retention_duration
defined_tags = v.defined_tags
freeform_tags = v.freeform_tags
target_resource_type = v.target_resource_type
enable_cis_checks = v.enable_cis_checks }}
lifecycle {
precondition {
condition = (each.value.enable_cis_checks == true && each.value.retention_duration >= 90) || (each.value.enable_cis_checks == false)
error_message = "VALIDATION FAILURE: Flow log \"${each.key}\" has an invalid retention duration. For complying with CIS framework, set the \"retention_duration\" attribute to 90 or greater. For forcing a value smaller than 90, set \"enable_cis_checks\" attribute to false."
}
precondition {
condition = contains(local.flow_logs_target_types, lower(each.value.target_resource_type))
error_message = "VALIDATION FAILURE: \"${each.value.target_resource_type}\" value is invalid for \"target_resource_type\" attribute. Valid values are: ${join(",",local.flow_logs_target_types)} (case insensitive)."
}
}
display_name = each.value.name
log_group_id = contains(keys(var.logging_configuration.log_groups),each.value.log_group_id) ? oci_logging_log_group.these[each.value.log_group_id].id : (length(regexall("^ocid1.*$", each.value.log_group_id)) > 0 ? each.value.log_group_id : var.log_groups_dependency[each.value.log_group_id].id)
log_type = "SERVICE"
configuration {
#compartment_id = each.value.compartment_id
source {
category = each.value.category
resource = each.value.resource_id
service = each.value.service
source_type = "OCISERVICE"
}
}
is_enabled = coalesce(each.value.is_enabled,true)
retention_duration = each.value.retention_duration
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : var.logging_configuration.default_defined_tags
freeform_tags = merge(local.cislz_module_tag, each.value.freeform_tags != null ? each.value.freeform_tags : var.logging_configuration.default_freeform_tags)
}