-
Notifications
You must be signed in to change notification settings - Fork 4
/
configmap.tf
366 lines (325 loc) · 11.8 KB
/
configmap.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
resource "kubernetes_config_map" "fluent-bit-config" {
count = var.enable_modsec ? 1 : 0
metadata {
name = "fluent-bit-config"
namespace = "ingress-controllers"
labels = {
"k8s-app" = var.controller_name
}
}
data = {
"fluent-bit.conf" = <<-EOT
[SERVICE]
Flush 1
Log_Level info
Daemon Off
Grace 30
Parsers_File parsers.conf
Parsers_File custom_parsers.conf
HTTP_Server On
HTTP_Listen 0.0.0.0
HTTP_Port 2020
Storage.path /var/log/flb-storage/
Storage.max_chunks_up 128
Storage.backlog.mem_limit 100MB
[INPUT]
Name tail
Alias modsec_nginx_ingress_audit
Tag cp-ingress-modsec-audit.*
Path /var/log/audit/**/**/*
Parser docker
Refresh_Interval 5
Buffer_Max_Size 5MB
Buffer_Chunk_Size 1M
Offset_Key pause_position_modsec-audit
DB cp-ingress-modsec-audit.db
DB.locking true
Storage.type filesystem
Storage.pause_on_chunks_overlimit True
[INPUT]
Name tail
Alias modsec_nginx_ingress_debug
Tag cp-ingress-modsec-debug.*
Path /var/log/debug/debug.log
Refresh_Interval 5
Buffer_Max_Size 5MB
Buffer_Chunk_Size 1M
Offset_Key pause_position_modsec-debug
DB cp-ingress-modsec-debug.db
DB.locking true
Storage.type filesystem
Storage.pause_on_chunks_overlimit True
[INPUT]
Name tail
Alias modsec_nginx_ingress_stdout
Tag cp-ingress-modsec-stdout.*
Path /var/log/containers/*nginx-ingress-modsec-controller*_ingress-controllers_controller-*.log
Parser cri-containerd
Refresh_Interval 5
Buffer_Max_Size 5MB
Buffer_Chunk_Size 1M
Offset_Key pause_position_modsec_stdout
DB cp-ingress-modsec-stdout.db
DB.locking true
Storage.type filesystem
Storage.pause_on_chunks_overlimit True
[FILTER]
Name grep
Match cp-ingress-modsec-stdout.*
regex log (ModSecurity-nginx|modsecurity|OWASP_CRS|owasp-modsecurity-crs)
[FILTER]
Name kubernetes
Alias modsec_nginx_ingress_stdout
Match cp-ingress-modsec-stdout.*
Kube_Tag_Prefix cp-ingress-modsec-stdout.var.log.containers.
Kube_URL https://kubernetes.default.svc:443
Kube_CA_File /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
Kube_Token_File /var/run/secrets/kubernetes.io/serviceaccount/token
K8S-Logging.Parser On
K8S-Logging.Exclude On
Keep_Log On
Merge_Log On
Merge_Log_Key log_processed
Buffer_Size 5MB
[FILTER]
Name parser
Parser modsec-debug-logs
Match cp-ingress-modsec-debug.*
Key_Name log
[FILTER]
Name lua
Match cp-ingress-modsec-stdout.*
script /fluent-bit/scripts/cb_extract_tag_value.lua
call cb_extract_tag_value
[FILTER]
Name lua
Match cp-ingress-modsec-audit.*
script /fluent-bit/scripts/cb_extract_tag_value.lua
call cb_extract_tag_value
[FILTER]
Name lua
Match cp-ingress-modsec-debug.*
script /fluent-bit/scripts/cb_tag_all_value.lua
call cb_tag_all_value
[FILTER]
Name parser
Parser generic-json
Match cp-ingress-modsec-audit.*
Key_Name log
Reserve_Data On
Preserve_Key On
[OUTPUT]
Name opensearch
Alias modsec_nginx_ingress_audit
Match cp-ingress-modsec-audit.*
Host ${var.opensearch_modsec_audit_host}
Port 443
Type _doc
Time_Key @timestamp
Logstash_Prefix ${var.cluster}_k8s_modsec_ingress
tls On
Logstash_Format On
Replace_Dots On
Generate_ID On
Retry_Limit False
AWS_AUTH On
AWS_REGION eu-west-2
Suppress_Type_Name On
Buffer_Size False
[OUTPUT]
Name opensearch
Alias modsec_nginx_ingress_stdout
Match cp-ingress-modsec-stdout.*
Host ${var.opensearch_modsec_audit_host}
Port 443
Type _doc
Time_Key @timestamp
Logstash_Prefix ${var.cluster}_k8s_modsec_ingress
tls On
Logstash_Format On
Replace_Dots On
Generate_ID On
Retry_Limit False
AWS_AUTH On
AWS_REGION eu-west-2
Suppress_Type_Name On
Buffer_Size False
[OUTPUT]
Name opensearch
Alias modsec_nginx_ingress_debug
Match cp-ingress-modsec-debug.*
Host ${var.opensearch_modsec_audit_host}
Port 443
Type _doc
Time_Key @timestamp
Logstash_Prefix ${var.cluster}_k8s_modsec_ingress_debug
tls On
Logstash_Format On
Replace_Dots On
Generate_ID On
Retry_Limit False
AWS_AUTH On
AWS_REGION eu-west-2
Suppress_Type_Name On
Buffer_Size False
EOT
"custom_parsers.conf" = <<-EOT
[PARSER]
Name initial-json
Format json
Time_Key time
Time_Keep On
# CRI-containerd Parser
[PARSER]
# https://rubular.com/r/DjPmoX5HnQMesk
Name cri-containerd
Format regex
Regex ^(?<time>[^ ]+) (?<stream>stdout|stderr) (?<logtag>[^ ]*) (?<log>.*)$
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L%z
[PARSER]
# https://rubular.com/r/HSac4mikEZ3iF8
Name modsec-debug-logs
Format regex
Regex ^(?<debug_uid>\[\d+\.\d+\]) (?<uri>\[.*\]) (?<log>\[\d+\].*)$
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L%z
[PARSER]
Name generic-json
Format json
Time_Key time
Time_Format %Y-%b-%dT%H:%M:%S
Time_Keep On
# Command | Decoder | Field | Optional Action
# =============|==================|=================
Decode_Field_As escaped_utf8 log do_next
Decode_Field_As json log
EOT
}
depends_on = [
kubernetes_namespace.ingress_controllers,
]
lifecycle {
ignore_changes = [metadata[0].annotations]
}
}
resource "kubernetes_config_map" "fluent_bit_lua_script" {
count = var.enable_modsec ? 1 : 0
metadata {
name = "fluent-bit-luascripts"
namespace = "ingress-controllers"
labels = {
"k8s-app" = var.controller_name
}
}
data = {
"cb_tag_all_value.lua" = <<-EOT
function cb_tag_all_value(tag, timestamp, record)
local new_record = record
new_record["github_teams"] = "all-org-members"
return 1, timestamp, new_record
end
EOT
"cb_extract_tag_value.lua" = <<-EOT
function cb_extract_tag_value(tag, timestamp, record)
local github_team = string.gmatch(record["log"], '%[tag "github_team=([%w+|%-]*)"%]')
local github_team_from_json = string.gmatch(record["log"], '"tags":%[.*"github_team=([%w+|%-]*)".*%]')
local new_record = record
local team_matches = {}
local json_matches = {}
for team in github_team do
table.insert(team_matches, team)
end
for team in github_team_from_json do
table.insert(json_matches, team)
end
if #team_matches > 0 then
new_record["github_teams"] = team_matches
return 1, timestamp, new_record
elseif #json_matches > 0 then
new_record["github_teams"] = json_matches
return 1, timestamp, new_record
else
return 0, timestamp, record
end
end
EOT
}
depends_on = [
kubernetes_namespace.ingress_controllers,
]
lifecycle {
ignore_changes = [metadata[0].annotations]
}
}
resource "kubernetes_config_map" "modsecurity_nginx_config" {
count = var.enable_modsec ? 1 : 0
metadata {
name = "modsecurity-nginx-config"
namespace = "ingress-controllers"
labels = {
"k8s-app" = var.controller_name
}
}
data = {
"modsecurity.conf" = file("${path.module}/templates/modsecurity.conf"),
}
depends_on = [
kubernetes_namespace.ingress_controllers,
]
lifecycle {
ignore_changes = [metadata[0].annotations]
}
}
resource "kubernetes_config_map" "logrotate_config" {
count = var.enable_modsec ? 1 : 0
metadata {
name = "logrotate-config"
namespace = "ingress-controllers"
labels = {
"k8s-app" = var.controller_name
}
}
data = {
"logrotate.conf" = <<-EOT
/var/log/audit/**/**/* {
hourly
rotate 0
missingok
maxage 1
}
/var/log/audit/*.log {
su root 82
hourly
rotate 2
missingok
compress
delaycompress
copytruncate
maxage 1
}
/var/log/debug/**/**/* {
hourly
rotate 0
missingok
maxage 1
}
/var/log/debug/*.log {
su root 82
hourly
rotate 2
missingok
compress
delaycompress
copytruncate
maxage 1
}
EOT
}
depends_on = [
kubernetes_namespace.ingress_controllers,
]
lifecycle {
ignore_changes = [metadata[0].annotations]
}
}