-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlog-aggregator.test.ts
More file actions
490 lines (411 loc) · 16 KB
/
log-aggregator.test.ts
File metadata and controls
490 lines (411 loc) · 16 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/**
* Tests for log-aggregator module
*/
import { aggregateLogs, loadAllLogs, loadAndAggregate } from './log-aggregator';
import { ParsedLogEntry, LogSource } from '../types';
import execa from 'execa';
import * as fs from 'fs';
// Mock dependencies
jest.mock('execa');
jest.mock('fs');
jest.mock('../logger', () => ({
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
}));
const mockedExeca = execa as jest.MockedFunction<typeof execa>;
const mockedFs = fs as jest.Mocked<typeof fs>;
describe('log-aggregator', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('aggregateLogs', () => {
it('should return empty stats for empty array', () => {
const stats = aggregateLogs([]);
expect(stats.totalRequests).toBe(0);
expect(stats.allowedRequests).toBe(0);
expect(stats.deniedRequests).toBe(0);
expect(stats.uniqueDomains).toBe(0);
expect(stats.byDomain.size).toBe(0);
expect(stats.timeRange).toBeNull();
});
it('should count allowed and denied requests correctly', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({ domain: 'github.com', isAllowed: true }),
createLogEntry({ domain: 'github.com', isAllowed: true }),
createLogEntry({ domain: 'evil.com', isAllowed: false }),
];
const stats = aggregateLogs(entries);
expect(stats.totalRequests).toBe(3);
expect(stats.allowedRequests).toBe(2);
expect(stats.deniedRequests).toBe(1);
});
it('should group by domain correctly', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({ domain: 'github.com', isAllowed: true }),
createLogEntry({ domain: 'github.com', isAllowed: true }),
createLogEntry({ domain: 'github.com', isAllowed: false }),
createLogEntry({ domain: 'npmjs.org', isAllowed: true }),
];
const stats = aggregateLogs(entries);
expect(stats.uniqueDomains).toBe(2);
expect(stats.byDomain.get('github.com')).toEqual({
domain: 'github.com',
allowed: 2,
denied: 1,
total: 3,
});
expect(stats.byDomain.get('npmjs.org')).toEqual({
domain: 'npmjs.org',
allowed: 1,
denied: 0,
total: 1,
});
});
it('should calculate time range correctly', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({ timestamp: 1000.5 }),
createLogEntry({ timestamp: 2000.5 }),
createLogEntry({ timestamp: 1500.5 }),
];
const stats = aggregateLogs(entries);
expect(stats.timeRange).toEqual({
start: 1000.5,
end: 2000.5,
});
});
it('should handle entries with missing domain', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({ domain: '-', isAllowed: true }),
createLogEntry({ domain: 'github.com', isAllowed: true }),
];
const stats = aggregateLogs(entries);
expect(stats.uniqueDomains).toBe(2);
expect(stats.byDomain.has('-')).toBe(true);
expect(stats.byDomain.has('github.com')).toBe(true);
});
it('should filter out transaction-end-before-headers entries', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({
domain: 'github.com',
url: 'github.com:443',
isAllowed: true
}),
createLogEntry({
domain: '-',
url: 'error:transaction-end-before-headers',
decision: 'NONE_NONE:HIER_NONE',
statusCode: 0,
isAllowed: false
}),
createLogEntry({
domain: 'npmjs.org',
url: 'npmjs.org:443',
isAllowed: true
}),
];
const stats = aggregateLogs(entries);
// Should only count the two valid entries
expect(stats.totalRequests).toBe(2); // Only actual requests, not benign operational entries
expect(stats.allowedRequests).toBe(2);
expect(stats.deniedRequests).toBe(0);
expect(stats.uniqueDomains).toBe(2);
expect(stats.byDomain.has('github.com')).toBe(true);
expect(stats.byDomain.has('npmjs.org')).toBe(true);
expect(stats.byDomain.has('-')).toBe(false); // Filtered entry not in domain stats
});
it('should handle multiple transaction-end-before-headers entries', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({
domain: 'github.com',
url: 'github.com:443',
isAllowed: true
}),
createLogEntry({
domain: '-',
url: 'error:transaction-end-before-headers',
clientIp: '::1', // healthcheck from localhost
decision: 'NONE_NONE:HIER_NONE',
statusCode: 0,
isAllowed: false
}),
createLogEntry({
domain: '-',
url: 'error:transaction-end-before-headers',
clientIp: '172.30.0.20', // shutdown-time connection closure
decision: 'NONE_NONE:HIER_NONE',
statusCode: 0,
isAllowed: false
}),
createLogEntry({
domain: 'npmjs.org',
url: 'npmjs.org:443',
isAllowed: true
}),
];
const stats = aggregateLogs(entries);
expect(stats.totalRequests).toBe(2); // Only actual requests
expect(stats.allowedRequests).toBe(2);
expect(stats.deniedRequests).toBe(0);
expect(stats.uniqueDomains).toBe(2);
});
it('should still count time range from all entries including filtered ones', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({
timestamp: 1000.0,
domain: 'github.com',
url: 'github.com:443',
isAllowed: true
}),
createLogEntry({
timestamp: 1500.0,
domain: '-',
url: 'error:transaction-end-before-headers',
decision: 'NONE_NONE:HIER_NONE',
statusCode: 0,
isAllowed: false
}),
createLogEntry({
timestamp: 2000.0,
domain: 'npmjs.org',
url: 'npmjs.org:443',
isAllowed: true
}),
];
const stats = aggregateLogs(entries);
// Time range should span all entries, even filtered ones
expect(stats.timeRange).toEqual({
start: 1000.0,
end: 2000.0,
});
});
});
describe('loadAllLogs', () => {
it('should load logs from a running container', async () => {
const mockLogContent = [
'1761074374.646 172.30.0.20:39748 api.github.com:443 140.82.114.22:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT api.github.com:443 "-"',
'1761074375.123 172.30.0.20:39749 evil.com:443 -:- 1.1 CONNECT 403 TCP_DENIED:HIER_NONE evil.com:443 "curl/7.81.0"',
].join('\n');
mockedExeca.mockResolvedValue({
stdout: mockLogContent,
stderr: '',
exitCode: 0,
} as never);
const source: LogSource = {
type: 'running',
containerName: 'awf-squid',
};
const entries = await loadAllLogs(source);
expect(entries).toHaveLength(2);
expect(entries[0].domain).toBe('api.github.com');
expect(entries[0].isAllowed).toBe(true);
expect(entries[1].domain).toBe('evil.com');
expect(entries[1].isAllowed).toBe(false);
});
it('should load logs from a file', async () => {
const mockLogContent = [
'1761074374.646 172.30.0.20:39748 api.github.com:443 140.82.114.22:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT api.github.com:443 "-"',
].join('\n');
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(mockLogContent);
const source: LogSource = {
type: 'preserved',
path: '/tmp/squid-logs-123',
};
const entries = await loadAllLogs(source);
expect(entries).toHaveLength(1);
expect(entries[0].domain).toBe('api.github.com');
expect(mockedFs.readFileSync).toHaveBeenCalledWith(
'/tmp/squid-logs-123/access.log',
'utf-8'
);
});
it('should return empty array if file does not exist', async () => {
mockedFs.existsSync.mockReturnValue(false);
const source: LogSource = {
type: 'preserved',
path: '/tmp/squid-logs-missing',
};
const entries = await loadAllLogs(source);
expect(entries).toHaveLength(0);
});
it('should return empty array if container command fails', async () => {
mockedExeca.mockRejectedValue(new Error('Container not found'));
const source: LogSource = {
type: 'running',
containerName: 'awf-squid',
};
const entries = await loadAllLogs(source);
expect(entries).toHaveLength(0);
});
it('should skip unparseable lines', async () => {
const mockLogContent = [
'1761074374.646 172.30.0.20:39748 api.github.com:443 140.82.114.22:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT api.github.com:443 "-"',
'invalid line that cannot be parsed',
'',
'1761074375.123 172.30.0.20:39749 npmjs.org:443 104.16.0.0:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT npmjs.org:443 "-"',
].join('\n');
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(mockLogContent);
const source: LogSource = {
type: 'preserved',
path: '/tmp/squid-logs-123',
};
const entries = await loadAllLogs(source);
expect(entries).toHaveLength(2);
expect(entries[0].domain).toBe('api.github.com');
expect(entries[1].domain).toBe('npmjs.org');
});
});
describe('blocked domain aggregation', () => {
it('should correctly aggregate multiple blocked domains', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({ domain: 'evil.com', isAllowed: false, decision: 'TCP_DENIED:HIER_NONE', statusCode: 403 }),
createLogEntry({ domain: 'malware.io', isAllowed: false, decision: 'TCP_DENIED:HIER_NONE', statusCode: 403 }),
createLogEntry({ domain: 'evil.com', isAllowed: false, decision: 'TCP_DENIED:HIER_NONE', statusCode: 403 }),
];
const stats = aggregateLogs(entries);
expect(stats.totalRequests).toBe(3);
expect(stats.allowedRequests).toBe(0);
expect(stats.deniedRequests).toBe(3);
expect(stats.uniqueDomains).toBe(2);
expect(stats.byDomain.get('evil.com')).toEqual({
domain: 'evil.com',
allowed: 0,
denied: 2,
total: 2,
});
expect(stats.byDomain.get('malware.io')).toEqual({
domain: 'malware.io',
allowed: 0,
denied: 1,
total: 1,
});
});
it('should correctly aggregate mixed allowed and denied for same domain', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({ domain: 'github.com', isAllowed: true }),
createLogEntry({ domain: 'github.com', isAllowed: false, decision: 'TCP_DENIED:HIER_NONE', statusCode: 403 }),
createLogEntry({ domain: 'github.com', isAllowed: true }),
];
const stats = aggregateLogs(entries);
expect(stats.byDomain.get('github.com')).toEqual({
domain: 'github.com',
allowed: 2,
denied: 1,
total: 3,
});
});
it('should handle only denied entries with no allowed entries', () => {
const entries: ParsedLogEntry[] = [
createLogEntry({ domain: 'blocked1.com', isAllowed: false }),
createLogEntry({ domain: 'blocked2.com', isAllowed: false }),
];
const stats = aggregateLogs(entries);
expect(stats.totalRequests).toBe(2);
expect(stats.allowedRequests).toBe(0);
expect(stats.deniedRequests).toBe(2);
expect(stats.uniqueDomains).toBe(2);
});
});
describe('loadAndAggregate', () => {
it('should correctly detect blocked domains from real log lines', async () => {
const mockLogContent = [
'1761074374.646 172.30.0.20:39748 api.github.com:443 140.82.114.22:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT api.github.com:443 "-"',
'1761074375.123 172.30.0.20:39749 evil.com:443 -:- 1.1 CONNECT 403 TCP_DENIED:HIER_NONE evil.com:443 "curl/7.81.0"',
'1761074376.456 172.30.0.20:39750 malware.io:443 -:- 1.1 CONNECT 403 TCP_DENIED:HIER_NONE malware.io:443 "python-requests/2.28"',
'1761074377.789 172.30.0.20:39751 npmjs.org:443 104.16.0.0:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT npmjs.org:443 "-"',
'1761074378.012 172.30.0.20:39752 evil.com:443 -:- 1.1 CONNECT 403 TCP_DENIED:HIER_NONE evil.com:443 "curl/7.81.0"',
].join('\n');
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(mockLogContent);
const source: LogSource = {
type: 'preserved',
path: '/tmp/squid-logs-blocked-test',
};
const stats = await loadAndAggregate(source);
expect(stats.totalRequests).toBe(5);
expect(stats.allowedRequests).toBe(2);
expect(stats.deniedRequests).toBe(3);
expect(stats.uniqueDomains).toBe(4);
// Verify blocked domains are correctly identified
const evilStats = stats.byDomain.get('evil.com');
expect(evilStats).toBeDefined();
expect(evilStats!.denied).toBe(2);
expect(evilStats!.allowed).toBe(0);
const malwareStats = stats.byDomain.get('malware.io');
expect(malwareStats).toBeDefined();
expect(malwareStats!.denied).toBe(1);
expect(malwareStats!.allowed).toBe(0);
// Verify allowed domains
const githubStats = stats.byDomain.get('api.github.com');
expect(githubStats).toBeDefined();
expect(githubStats!.allowed).toBe(1);
expect(githubStats!.denied).toBe(0);
});
it('should detect blocked HTTP domains from real log lines', async () => {
const mockLogContent = [
'1761074374.646 172.30.0.20:39748 example.com:80 93.184.216.34:80 1.1 GET 200 TCP_MISS:HIER_DIRECT http://example.com/ "-"',
'1761074375.123 172.30.0.20:39749 blocked.com:80 -:- 1.1 GET 403 TCP_DENIED:HIER_NONE http://blocked.com/exfil "-"',
].join('\n');
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(mockLogContent);
const source: LogSource = {
type: 'preserved',
path: '/tmp/squid-logs-http-blocked',
};
const stats = await loadAndAggregate(source);
expect(stats.totalRequests).toBe(2);
expect(stats.allowedRequests).toBe(1);
expect(stats.deniedRequests).toBe(1);
const blockedStats = stats.byDomain.get('blocked.com');
expect(blockedStats).toBeDefined();
expect(blockedStats!.denied).toBe(1);
expect(blockedStats!.allowed).toBe(0);
});
it('should load and aggregate logs in one call', async () => {
const mockLogContent = [
'1761074374.646 172.30.0.20:39748 api.github.com:443 140.82.114.22:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT api.github.com:443 "-"',
'1761074375.123 172.30.0.20:39749 api.github.com:443 140.82.114.22:443 1.1 CONNECT 200 TCP_TUNNEL:HIER_DIRECT api.github.com:443 "-"',
'1761074376.456 172.30.0.20:39750 evil.com:443 -:- 1.1 CONNECT 403 TCP_DENIED:HIER_NONE evil.com:443 "curl/7.81.0"',
].join('\n');
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(mockLogContent);
const source: LogSource = {
type: 'preserved',
path: '/tmp/squid-logs-123',
};
const stats = await loadAndAggregate(source);
expect(stats.totalRequests).toBe(3);
expect(stats.allowedRequests).toBe(2);
expect(stats.deniedRequests).toBe(1);
expect(stats.uniqueDomains).toBe(2);
});
});
});
/**
* Helper function to create a mock ParsedLogEntry with default values
*/
function createLogEntry(overrides: Partial<ParsedLogEntry> = {}): ParsedLogEntry {
return {
timestamp: 1761074374.646,
clientIp: '172.30.0.20',
clientPort: '39748',
host: 'api.github.com:443',
destIp: '140.82.114.22',
destPort: '443',
protocol: '1.1',
method: 'CONNECT',
statusCode: 200,
decision: 'TCP_TUNNEL:HIER_DIRECT',
url: 'api.github.com:443',
userAgent: '-',
domain: 'api.github.com',
isAllowed: true,
isHttps: true,
...overrides,
};
}