Skip to content

Commit eca589b

Browse files
authored
Merge pull request #43 from alphagov/pp-13731/allow-no-environment-env-var
PP-13731: Make ENVIRONMENT env var optional, and fail if needed
2 parents c9ecdee + d840697 commit eca589b

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

spec/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,11 @@ describe('General processing', () => {
457457
expect(result.records[0].recordId).toEqual('testRecordId')
458458
})
459459

460-
test('should error if ENVIRONMENT env var is not set', async () => {
461-
process.env.AWS_ACCOUNT_NAME = 'test'
462-
process.env.AWS_ACCOUNT_ID = '223851549868'
460+
test('should error if ENVIRONMENT env var is not set, but is required', async () => {
463461
process.env.ENVIRONMENT = ''
464-
await expect(async () => await handler(aCloudWatchEventWith([]), mockContext, mockCallback) as FirehoseTransformationResult).rejects.toThrow('"ENVIRONMENT" env var is not set')
462+
const result = await handler(anApplicationLogCloudWatchEvent.input, mockContext, mockCallback) as FirehoseTransformationResult
463+
expect(result.records[0].result).toEqual('ProcessingFailed')
464+
expect(result.records[0].recordId).toEqual('LogEvent-1')
465465
})
466466

467467
test('should error if AWS_ACCOUNT_NAME env var is not set', async () => {

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ function getMandatoryEnvVar(varName: string): string {
1616
}
1717

1818
function getEnvVars(): EnvVars {
19-
return {
20-
environment: getMandatoryEnvVar('ENVIRONMENT'),
19+
const envVars: EnvVars = {
2120
aws_account_name: getMandatoryEnvVar('AWS_ACCOUNT_NAME'),
2221
aws_account_id: getMandatoryEnvVar('AWS_ACCOUNT_ID')
2322
}
23+
24+
if (Object.hasOwn(process.env, 'ENVIRONMENT')) {
25+
envVars.environment = process.env.ENVIRONMENT
26+
}
27+
28+
return envVars
2429
}
2530

2631
function debugTransformation(records: FirehoseTransformationResultRecord[]): void {

src/transformData.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ function transformALBLog(data: S3LogRecord, envVars: EnvVars, approximateArrival
2121
time = approximateArrivalTimestamp
2222
}
2323

24+
if (envVars.environment === undefined) {
25+
throw new Error(`"ENVIRONMENT" env var is not set`)
26+
}
27+
2428
return {
2529
host: data.ALB as string,
2630
source: 'ALB',
@@ -98,6 +102,9 @@ function transformCloudWatchData(data: CloudWatchLogsDecodedData, envVars: EnvVa
98102
}
99103

100104
if (logType !== CloudWatchLogTypes['cloudtrail']) {
105+
if (envVars.environment === undefined || envVars.environment === '') {
106+
throw new Error(`"ENVIRONMENT" env var is not set`)
107+
}
101108
fields.environment = envVars.environment
102109
}
103110

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export type S3LogRecord = {
3131
}
3232

3333
export type EnvVars = {
34-
environment: string
34+
environment?: string
3535
aws_account_name: string
3636
aws_account_id: string
3737
}

0 commit comments

Comments
 (0)