Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/logs/audit-enricher.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { enrichWithPolicyRules, computeRuleStats, EnrichedLogEntry } from './audit-enricher';
import { ParsedLogEntry, PolicyManifest, PolicyRule } from '../types';
import { createLogEntry } from './log-test-fixtures.test-utils';

function makeEntry(overrides: Partial<ParsedLogEntry> = {}): ParsedLogEntry {
return {
return createLogEntry({
timestamp: 1700000000.000,
clientIp: '172.30.0.20',
clientPort: '39748',
host: 'github.com:443',
destIp: '140.82.114.22',
destPort: '443',
protocol: '1.1',
method: 'CONNECT',
statusCode: 200,
decision: 'TCP_TUNNEL:HIER_DIRECT',
url: 'github.com:443',
userAgent: 'curl/7.81.0',
domain: 'github.com',
isAllowed: true,
isHttps: true,
...overrides,
};
});
}

function makeManifest(rules: PolicyRule[]): PolicyManifest {
Expand Down
48 changes: 19 additions & 29 deletions src/logs/log-aggregator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { aggregateLogs, loadAllLogs, loadAndAggregate } from './log-aggregator';
import { ParsedLogEntry, LogSource } from '../types';
import { createLogEntry, createRawLogLine } from './log-test-fixtures.test-utils';
import execa from 'execa';
import * as fs from 'fs';

Expand Down Expand Up @@ -234,7 +235,7 @@ describe('log-aggregator', () => {

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 "-"',
createRawLogLine(),
].join('\n');

mockedFs.existsSync.mockReturnValue(true);
Expand Down Expand Up @@ -283,7 +284,7 @@ describe('log-aggregator', () => {

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 "-"',
createRawLogLine(),
'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 "-"',
Expand Down Expand Up @@ -436,9 +437,22 @@ describe('log-aggregator', () => {

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"',
createRawLogLine(),
createRawLogLine({
timestamp: 1761074375.123,
clientPort: '39749',
}),
createRawLogLine({
timestamp: 1761074376.456,
clientPort: '39750',
host: 'evil.com:443',
destIp: '-',
destPort: '-',
statusCode: 403,
decision: 'TCP_DENIED:HIER_NONE',
url: 'evil.com:443',
userAgent: 'curl/7.81.0',
}),
].join('\n');

mockedFs.existsSync.mockReturnValue(true);
Expand All @@ -458,27 +472,3 @@ describe('log-aggregator', () => {
});
});
});

/**
* 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,
};
}
29 changes: 5 additions & 24 deletions src/logs/log-formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { LogFormatter } from './log-formatter';
import { ParsedLogEntry } from '../types';
import { createLogEntry } from './log-test-fixtures.test-utils';

// Mock chalk to avoid terminal output issues in tests
jest.mock('chalk', () => ({
Expand All @@ -13,41 +14,21 @@ jest.mock('chalk', () => ({

describe('LogFormatter', () => {
// Sample parsed log entries for testing
const allowedEntry: ParsedLogEntry = {
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,
};

const deniedEntry: ParsedLogEntry = {
const allowedEntry: ParsedLogEntry = createLogEntry();

const deniedEntry: ParsedLogEntry = createLogEntry({
timestamp: 1760994429.358,
clientIp: '172.30.0.20',
clientPort: '36274',
host: 'github.com:8443',
destIp: '-',
destPort: '-',
protocol: '1.1',
method: 'CONNECT',
statusCode: 403,
decision: 'TCP_DENIED:HIER_NONE',
url: 'github.com:8443',
userAgent: 'curl/7.81.0',
domain: 'github.com',
isAllowed: false,
isHttps: true,
};
});

describe('formatEntry - pretty format', () => {
it('should format allowed entry without color', () => {
Expand Down
10 changes: 4 additions & 6 deletions src/logs/log-streamer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as fs from 'fs';
import { streamLogs } from './log-streamer';
import { LogFormatter } from './log-formatter';
import { LogSource } from '../types';
import { createRawLogLine } from './log-test-fixtures.test-utils';
import execa from 'execa';
import { Readable } from 'stream';
import { trackPidForPortSync, isPidTrackingAvailable } from '../pid-tracker';
Expand Down Expand Up @@ -234,8 +235,7 @@ describe('log-streamer', () => {
};
const formatter = new LogFormatter({ format: 'json' });

const logLine =
'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 "-"';
const logLine = createRawLogLine();

mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(logLine);
Expand Down Expand Up @@ -304,8 +304,7 @@ describe('log-streamer', () => {
};
const formatter = new LogFormatter({ format: 'json' });

const logLine =
'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 "-"';
const logLine = createRawLogLine();

mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(logLine);
Expand Down Expand Up @@ -339,8 +338,7 @@ describe('log-streamer', () => {
};
const formatter = new LogFormatter({ format: 'json' });

const logLine =
'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 "-"';
const logLine = createRawLogLine();

mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(logLine);
Expand Down
30 changes: 30 additions & 0 deletions src/logs/log-test-fixtures.test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ParsedLogEntry } from '../types';

export 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,
};
}

type RawLogLineFields = Omit<ParsedLogEntry, 'domain' | 'isAllowed' | 'isHttps'>;

export function createRawLogLine(overrides: Partial<RawLogLineFields> = {}): string {
const entry = createLogEntry(overrides);

return `${entry.timestamp} ${entry.clientIp}:${entry.clientPort} ${entry.host} ${entry.destIp}:${entry.destPort} ${entry.protocol} ${entry.method} ${entry.statusCode} ${entry.decision} ${entry.url} "${entry.userAgent}"`;
}
Loading