Skip to content
Merged
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
48 changes: 48 additions & 0 deletions spec/fixtures/bastion_fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Fixture } from './general_fixtures'

export const aBastionLogCloudWatchEvent: Fixture = {
input: {
deliveryStreamArn: 'someDeliveryStreamArn',
invocationId: 'someId',
region: 'eu-west-1',
records: [{
approximateArrivalTimestamp: 1234,
recordId: 'LogEvent-1111',
data: Buffer.from(JSON.stringify({
owner: '123456789',
logGroup: 'test-12_bastion',
logStream: 'bastionTaskId',
subscriptionFilters: [],
messageType: 'DATA_MESSAGE',
logEvents: [
{
id: 'cloudwatch-log-message-id-1',
timestamp: '1234',
message: '{"time":"2025-03-28T09:41:30.493596011Z","level":"INFO","msg":"Container started. Starting bastion service."}'
}
]
})).toString('base64')
}
]
},
expected: {
records: [{
result: 'Ok',
recordId: 'LogEvent-1111',
data: Buffer.from([
{
host: 'bastionTaskId',
source: 'bastion',
sourcetype: 'linux_bastion',
index: 'pay_host',
event: '{"time":"2025-03-28T09:41:30.493596011Z","level":"INFO","msg":"Container started. Starting bastion service."}',
fields: {
account: 'test',
environment: 'test-12'
},
time: 1743154890.493
}
].map(x => JSON.stringify(x)).join('\n')).toString('base64')
}]
}
}
14 changes: 14 additions & 0 deletions spec/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
anInvalidApplicationLogFirehoseTransformationEventRecord
} from './fixtures/application_fixtures'

import {
aBastionLogCloudWatchEvent
} from './fixtures/bastion_fixtures'

import {
aConcourseSyslogCloudWatchEvent,
aConcourseAuditCloudWatchEvent,
Expand Down Expand Up @@ -76,6 +80,16 @@ describe('Processing CloudWatchLogEvents', () => {
expect(Buffer.from(result.records[1].data as string, 'base64').toString()).toEqual(Buffer.from(expectedSecondRecord.data as string, 'base64').toString())
})
})
describe('From Bastion', () => {
test('should transform bastion logs from CloudWatch', async () => {
const result = await handler(aBastionLogCloudWatchEvent.input, mockContext, mockCallback) as FirehoseTransformationResult

const expectedFirstRecord = aBastionLogCloudWatchEvent.expected.records[0]
expect(result.records[0].result).toEqual(expectedFirstRecord.result)
expect(result.records[0].recordId).toEqual(expectedFirstRecord.recordId)
expect(Buffer.from(result.records[0].data as string, 'base64').toString()).toEqual(Buffer.from(expectedFirstRecord.data as string, 'base64').toString())
})
})

describe('From Nginx', () => {
test('should transform nginx forward proxy logs from CloudWatch', async () => {
Expand Down
12 changes: 12 additions & 0 deletions src/extractTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export function extractAppLogTime(log: string): number | undefined {
return parseStringToEpoch(extractedTime[1])
}

export function extractBastionLogTime(log: string): number | undefined {
const regex = /"time"\s*:\s*"(.*?)"/
const extractedTime = regexTimeFromLog(regex, log)
if (extractedTime === undefined) {
return undefined
}

return parseStringToEpoch(extractedTime[1])
}

export function extractSquidLogTime(log: string): number | undefined {
let extractedTime = regexTimeFromLog(SQUID_ACCESS_LOG_FORMAT_REGEX, log)
if (extractedTime !== undefined) {
Expand Down Expand Up @@ -133,6 +143,8 @@ export function parseTimeFromLog(log: string, logType: CloudWatchLogTypes): numb
switch (logType) {
case CloudWatchLogTypes.app:
return extractAppLogTime(log)
case CloudWatchLogTypes.bastion:
return extractBastionLogTime(log)
case CloudWatchLogTypes.squid:
return extractSquidLogTime(log)
case CloudWatchLogTypes.syslog:
Expand Down
4 changes: 4 additions & 0 deletions src/transformData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ function sourceTypeFromLogGroup(logType: CloudWatchLogTypes, msg: string): strin
switch (logType) {
case CloudWatchLogTypes.app:
return 'ST004:application_json'
case CloudWatchLogTypes.bastion:
return 'linux_bastion'
case CloudWatchLogTypes['nginx-forward-proxy']:
case CloudWatchLogTypes['nginx-reverse-proxy']:
return 'nginx:plus:kv'
Expand Down Expand Up @@ -181,6 +183,8 @@ function indexFromLogType(logType: CloudWatchLogTypes): string {
switch (logType) {
case CloudWatchLogTypes.app:
return 'pay_application'
case CloudWatchLogTypes.bastion:
return 'pay_host'
case CloudWatchLogTypes['nginx-forward-proxy']:
case CloudWatchLogTypes['nginx-reverse-proxy']:
return 'pay_ingress'
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export enum CloudWatchLogTypes {
'apt',
'audit',
'auth',
'bastion',
'concourse',
'cloudtrail',
'dmesg',
Expand Down