-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathWafMetricCollector.java
More file actions
446 lines (391 loc) · 14.2 KB
/
Copy pathWafMetricCollector.java
File metadata and controls
446 lines (391 loc) · 14.2 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package datadog.trace.api.telemetry;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;
public class WafMetricCollector implements MetricCollector<WafMetricCollector.WafMetric> {
public static WafMetricCollector INSTANCE = new WafMetricCollector();
private static final int ABSTRACT_POWERWAF_EXCEPTION_NUMBER =
3; // only 3 error codes are possible for now in AbstractPowerwafException
public static WafMetricCollector get() {
return WafMetricCollector.INSTANCE;
}
private WafMetricCollector() {
// Prevent external instantiation
}
private static final String NAMESPACE = "appsec";
private static final BlockingQueue<WafMetric> rawMetricsQueue =
new ArrayBlockingQueue<>(RAW_QUEUE_SIZE);
private static final AtomicInteger wafInitCounter = new AtomicInteger();
private static final AtomicInteger wafUpdatesCounter = new AtomicInteger();
private static final AtomicRequestCounter wafRequestCounter = new AtomicRequestCounter();
private static final AtomicRequestCounter wafTriggeredRequestCounter = new AtomicRequestCounter();
private static final AtomicRequestCounter wafBlockedRequestCounter = new AtomicRequestCounter();
private static final AtomicRequestCounter wafTimeoutRequestCounter = new AtomicRequestCounter();
private static final AtomicLongArray raspRuleEvalCounter =
new AtomicLongArray(RuleType.getNumValues());
private static final AtomicLongArray raspRuleMatchCounter =
new AtomicLongArray(RuleType.getNumValues());
private static final AtomicLongArray raspTimeoutCounter =
new AtomicLongArray(RuleType.getNumValues());
private static final ConcurrentMap<Integer, AtomicLongArray> raspErrorCodeCounter =
new ConcurrentSkipListMap<>();
static {
for (int i = -1 * ABSTRACT_POWERWAF_EXCEPTION_NUMBER; i < 0; i++) {
raspErrorCodeCounter.put(i, new AtomicLongArray(RuleType.getNumValues()));
}
}
private static final AtomicLongArray missingUserLoginQueue =
new AtomicLongArray(LoginFramework.getNumValues() * LoginEvent.getNumValues());
private static final AtomicLongArray missingUserIdQueue =
new AtomicLongArray(LoginFramework.getNumValues());
/** WAF version that will be initialized with wafInit and reused for all metrics. */
private static String wafVersion = "";
/**
* Rules version that will be updated on each wafInit and wafUpdates. This is not entirely
* accurate, since wafRequest metrics might be collected for a period where a rules update happens
* and some requests will be incorrectly reported with the old or new rules version.
*/
private static String rulesVersion = "";
public void wafInit(final String wafVersion, final String rulesVersion, final boolean success) {
WafMetricCollector.wafVersion = wafVersion;
WafMetricCollector.rulesVersion = rulesVersion;
rawMetricsQueue.offer(
new WafInitRawMetric(wafInitCounter.incrementAndGet(), wafVersion, rulesVersion, success));
}
public void wafUpdates(final String rulesVersion, final boolean success) {
rawMetricsQueue.offer(
new WafUpdatesRawMetric(
wafUpdatesCounter.incrementAndGet(), wafVersion, rulesVersion, success));
// Flush request metrics to get the new version.
if (rulesVersion != null
&& WafMetricCollector.rulesVersion != null
&& !rulesVersion.equals(WafMetricCollector.rulesVersion)) {
WafMetricCollector.get().prepareMetrics();
}
WafMetricCollector.rulesVersion = rulesVersion;
}
public void wafRequest() {
wafRequestCounter.increment();
}
public void wafRequestTriggered() {
wafTriggeredRequestCounter.increment();
}
public void wafRequestBlocked() {
wafBlockedRequestCounter.increment();
}
public void wafRequestTimeout() {
wafTimeoutRequestCounter.increment();
}
public void raspRuleEval(final RuleType ruleType) {
raspRuleEvalCounter.incrementAndGet(ruleType.ordinal());
}
public void raspRuleMatch(final RuleType ruleType) {
raspRuleMatchCounter.incrementAndGet(ruleType.ordinal());
}
public void raspTimeout(final RuleType ruleType) {
raspTimeoutCounter.incrementAndGet(ruleType.ordinal());
}
public void raspErrorCode(final RuleType ruleType, final int ddwafRunErrorCode) {
raspErrorCodeCounter.get(ddwafRunErrorCode).incrementAndGet(ruleType.ordinal());
}
public void missingUserLogin(final LoginFramework framework, final LoginEvent eventType) {
missingUserLoginQueue.incrementAndGet(
framework.ordinal() * LoginEvent.getNumValues() + eventType.ordinal());
}
public void missingUserId(final LoginFramework framework) {
missingUserIdQueue.incrementAndGet(framework.ordinal());
}
@Override
public Collection<WafMetric> drain() {
if (!rawMetricsQueue.isEmpty()) {
List<WafMetric> list = new LinkedList<>();
int drained = rawMetricsQueue.drainTo(list);
if (drained > 0) {
return list;
}
}
return Collections.emptyList();
}
@Override
public void prepareMetrics() {
// Requests
if (wafRequestCounter.get() > 0) {
if (!rawMetricsQueue.offer(
new WafRequestsRawMetric(
wafRequestCounter.getAndReset(),
WafMetricCollector.wafVersion,
WafMetricCollector.rulesVersion,
false,
false,
false))) {
return;
}
}
// Triggered requests
if (wafTriggeredRequestCounter.get() > 0) {
if (!rawMetricsQueue.offer(
new WafRequestsRawMetric(
wafTriggeredRequestCounter.getAndReset(),
WafMetricCollector.wafVersion,
WafMetricCollector.rulesVersion,
true,
false,
false))) {
return;
}
}
// Blocked requests
if (wafBlockedRequestCounter.get() > 0) {
if (!rawMetricsQueue.offer(
new WafRequestsRawMetric(
wafBlockedRequestCounter.getAndReset(),
WafMetricCollector.wafVersion,
WafMetricCollector.rulesVersion,
true,
true,
false))) {
return;
}
}
// Timeout requests
if (wafTimeoutRequestCounter.get() > 0) {
if (!rawMetricsQueue.offer(
new WafRequestsRawMetric(
wafTimeoutRequestCounter.getAndReset(),
WafMetricCollector.wafVersion,
WafMetricCollector.rulesVersion,
false,
false,
true))) {
return;
}
}
// RASP rule eval per rule type
for (RuleType ruleType : RuleType.values()) {
long counter = raspRuleEvalCounter.getAndSet(ruleType.ordinal(), 0);
if (counter > 0) {
if (!rawMetricsQueue.offer(
new RaspRuleEval(counter, ruleType, WafMetricCollector.wafVersion))) {
return;
}
}
}
// RASP rule match per rule type
for (RuleType ruleType : RuleType.values()) {
long counter = raspRuleMatchCounter.getAndSet(ruleType.ordinal(), 0);
if (counter > 0) {
if (!rawMetricsQueue.offer(
new RaspRuleMatch(counter, ruleType, WafMetricCollector.wafVersion))) {
return;
}
}
}
// RASP timeout per rule type
for (RuleType ruleType : RuleType.values()) {
long counter = raspTimeoutCounter.getAndSet(ruleType.ordinal(), 0);
if (counter > 0) {
if (!rawMetricsQueue.offer(
new RaspTimeout(counter, ruleType, WafMetricCollector.wafVersion))) {
return;
}
}
}
// RASP rule type for each possible error code
for (int i = -1 * ABSTRACT_POWERWAF_EXCEPTION_NUMBER; i < 0; i++) {
for (RuleType ruleType : RuleType.values()) {
long counter = raspErrorCodeCounter.get(i).getAndSet(ruleType.ordinal(), 0);
if (counter > 0) {
if (!rawMetricsQueue.offer(
new RaspError(counter, ruleType, WafMetricCollector.wafVersion, i))) {
return;
}
}
}
}
// Missing user login
for (LoginFramework framework : LoginFramework.values()) {
for (LoginEvent event : LoginEvent.values()) {
final int ordinal = framework.ordinal() * LoginEvent.getNumValues() + event.ordinal();
long counter = missingUserLoginQueue.getAndSet(ordinal, 0);
if (counter > 0) {
if (!rawMetricsQueue.offer(
new MissingUserLoginMetric(counter, framework.getTag(), event.getTag()))) {
return;
}
}
}
}
// Missing user id
for (LoginFramework framework : LoginFramework.values()) {
long counter = missingUserIdQueue.getAndSet(framework.ordinal(), 0);
if (counter > 0) {
if (!rawMetricsQueue.offer(new MissingUserIdMetric(counter, framework.getTag()))) {
return;
}
}
}
}
public abstract static class WafMetric extends MetricCollector.Metric {
public WafMetric(String metricName, long counter, String... tags) {
super(NAMESPACE, true, metricName, "count", counter, tags);
}
}
public static class WafInitRawMetric extends WafMetric {
public WafInitRawMetric(
final long counter,
final String wafVersion,
final String rulesVersion,
final boolean success) {
super(
"waf.init",
counter,
"waf_version:" + wafVersion,
"event_rules_version:" + rulesVersion,
"success:" + success);
}
}
public static class WafUpdatesRawMetric extends WafMetric {
public WafUpdatesRawMetric(
final long counter,
final String wafVersion,
final String rulesVersion,
final boolean success) {
super(
"waf.updates",
counter,
"waf_version:" + wafVersion,
"event_rules_version:" + rulesVersion,
"success:" + success);
}
}
public static class MissingUserLoginMetric extends WafMetric {
public MissingUserLoginMetric(long counter, String framework, String type) {
super(
"instrum.user_auth.missing_user_login",
counter,
"framework:" + framework,
"event_type:" + type);
}
}
public static class MissingUserIdMetric extends WafMetric {
public MissingUserIdMetric(long counter, String framework) {
super(
"instrum.user_auth.missing_user_id",
counter,
"framework:" + framework,
"event_type:authenticated_request");
}
}
public static class WafRequestsRawMetric extends WafMetric {
public WafRequestsRawMetric(
final long counter,
final String wafVersion,
final String rulesVersion,
final boolean triggered,
final boolean blocked,
final boolean wafTimeout) {
super(
"waf.requests",
counter,
"waf_version:" + wafVersion,
"event_rules_version:" + rulesVersion,
"rule_triggered:" + triggered,
"request_blocked:" + blocked,
"waf_timeout:" + wafTimeout);
}
}
public static class RaspRuleEval extends WafMetric {
public RaspRuleEval(final long counter, final RuleType ruleType, final String wafVersion) {
super(
"rasp.rule.eval",
counter,
ruleType.variant != null
? new String[] {
"rule_type:" + ruleType.type,
"rule_variant:" + ruleType.variant,
"waf_version:" + wafVersion,
"event_rules_version:" + rulesVersion
}
: new String[] {"rule_type:" + ruleType.type, "waf_version:" + wafVersion});
}
}
public static class RaspRuleMatch extends WafMetric {
public RaspRuleMatch(final long counter, final RuleType ruleType, final String wafVersion) {
super(
"rasp.rule.match",
counter,
ruleType.variant != null
? new String[] {
"rule_type:" + ruleType.type,
"rule_variant:" + ruleType.variant,
"waf_version:" + wafVersion,
"event_rules_version:" + rulesVersion
}
: new String[] {"rule_type:" + ruleType.type, "waf_version:" + wafVersion});
}
}
public static class RaspTimeout extends WafMetric {
public RaspTimeout(final long counter, final RuleType ruleType, final String wafVersion) {
super(
"rasp.timeout",
counter,
ruleType.variant != null
? new String[] {
"rule_type:" + ruleType.type,
"rule_variant:" + ruleType.variant,
"waf_version:" + wafVersion,
"event_rules_version:" + rulesVersion
}
: new String[] {"rule_type:" + ruleType.type, "waf_version:" + wafVersion});
}
}
public static class RaspError extends WafMetric {
public RaspError(
final long counter,
final RuleType ruleType,
final String wafVersion,
final Integer ddwafRunError) {
super(
"rasp.error",
counter,
ruleType.variant != null
? new String[] {
"rule_type:" + ruleType.type,
"rule_variant:" + ruleType.variant,
"waf_version:" + wafVersion,
"event_rules_version:" + rulesVersion,
"waf_error:" + ddwafRunError
}
: new String[] {
"rule_type:" + ruleType.type,
"waf_version:" + wafVersion,
"waf_error:" + ddwafRunError
});
}
}
public static class AtomicRequestCounter {
private final AtomicLong atomicLong = new AtomicLong();
private volatile long timestamp;
public final long get() {
return atomicLong.get();
}
public final long getAndReset() {
timestamp = 0;
return atomicLong.getAndSet(0);
}
public final void increment() {
if (timestamp == 0) {
timestamp = System.currentTimeMillis();
}
atomicLong.incrementAndGet();
}
}
}