Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 9df5fb8

Browse files
authored
fix(logging): Adjusting webhook logs and levels (#1287)
* fix(logging): Adjusting webhook logs and levels * Correcting object key * Minor refactoring to support additional logging
1 parent 7dd662c commit 9df5fb8

File tree

2 files changed

+41
-61
lines changed

2 files changed

+41
-61
lines changed

Diff for: modules/webhook/lambdas/webhook/src/lambda.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { handle } from './webhook/handler';
2-
import { APIGatewayEvent, Context } from 'aws-lambda';
2+
import { APIGatewayEvent, Context, Callback } from 'aws-lambda';
33

44
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5-
export const githubWebhook = async (event: APIGatewayEvent, context: Context, callback: any): Promise<void> => {
5+
export const githubWebhook = async (event: APIGatewayEvent, context: Context, callback: Callback): Promise<void> => {
66
try {
7-
const statusCode = await handle(event.headers, event.body);
7+
const statusCode = await handle(event.headers, event.body as string);
88
callback(null, {
99
statusCode: statusCode,
1010
});
1111
} catch (e) {
12-
callback(e);
12+
callback(e as Error);
1313
}
1414
};

Diff for: modules/webhook/lambdas/webhook/src/webhook/handler.ts

+37-57
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,59 @@
11
import { IncomingHttpHeaders } from 'http';
22
import { Webhooks } from '@octokit/webhooks';
33
import { sendActionRequest } from '../sqs';
4-
import { CheckRunEvent } from '@octokit/webhooks-types';
4+
import { CheckRunEvent, WorkflowJobEvent } from '@octokit/webhooks-types';
55
import { getParameterValue } from '../ssm';
66

7-
// Event type not available yet in SDK
8-
export interface WorkflowJob {
9-
action: 'queued' | 'created' | 'completed';
10-
workflow_job: {
11-
id: number;
12-
labels: [string];
13-
};
14-
repository: {
15-
id: number;
16-
name: string;
17-
full_name: string;
18-
owner: {
19-
login: string;
20-
};
21-
};
22-
organization: {
23-
login: string;
24-
};
25-
installation?: {
26-
id?: number;
27-
};
28-
}
29-
30-
export const handle = async (headers: IncomingHttpHeaders, payload: any): Promise<number> => {
7+
export const handle = async (headers: IncomingHttpHeaders, body: string): Promise<number> => {
318
// ensure header keys lower case since github headers can contain capitals.
329
for (const key in headers) {
3310
headers[key.toLowerCase()] = headers[key];
3411
}
3512

36-
const signature = headers['x-hub-signature'] as string;
37-
if (!signature) {
38-
console.error("Github event doesn't have signature. This webhook requires a secret to be configured.");
39-
return 500;
40-
}
41-
42-
const secret = await getParameterValue(process.env.ENVIRONMENT as string, 'github_app_webhook_secret');
13+
const githubEvent = headers['x-github-event'] as string;
4314

44-
const webhooks = new Webhooks({
45-
secret: secret,
46-
});
47-
if (!(await webhooks.verify(payload as string, signature))) {
48-
console.error('Unable to verify signature!');
49-
return 401;
15+
let status = await verifySignature(githubEvent, headers['x-hub-signature'] as string, body);
16+
if (status != 200) {
17+
return status;
5018
}
19+
const payload = JSON.parse(body);
20+
console.info(`Received Github event ${githubEvent} from ${payload.repository.full_name}`);
5121

52-
const githubEvent = headers['x-github-event'] as string;
53-
54-
console.debug(`Received Github event: "${githubEvent}"`);
22+
if (isRepoNotAllowed(payload.repository.full_name)) {
23+
console.error(`Received event from unauthorized repository ${payload.repository.full_name}`);
24+
return 403;
25+
}
5526

56-
let status = 200;
5727
if (githubEvent == 'workflow_job') {
58-
status = await handleWorkflowJob(JSON.parse(payload) as WorkflowJob, githubEvent);
28+
status = await handleWorkflowJob(payload as WorkflowJobEvent, githubEvent);
5929
} else if (githubEvent == 'check_run') {
60-
status = await handleCheckRun(JSON.parse(payload) as CheckRunEvent, githubEvent);
30+
status = await handleCheckRun(payload as CheckRunEvent, githubEvent);
6131
} else {
62-
console.debug('Ignore event ' + githubEvent);
32+
console.warn(`Ignoring unsupported event ${githubEvent}`);
6333
}
6434

6535
return status;
6636
};
6737

68-
async function handleWorkflowJob(body: WorkflowJob, githubEvent: string): Promise<number> {
69-
if (isRepoNotAllowed(body)) {
70-
console.error(`Received event from unauthorized repository ${body.repository.full_name}`);
71-
return 403;
38+
async function verifySignature(githubEvent: string, signature: string, body: string): Promise<number> {
39+
if (!signature) {
40+
console.error("Github event doesn't have signature. This webhook requires a secret to be configured.");
41+
return 500;
42+
}
43+
44+
const secret = await getParameterValue(process.env.ENVIRONMENT as string, 'github_app_webhook_secret');
45+
46+
const webhooks = new Webhooks({
47+
secret: secret,
48+
});
49+
if (!(await webhooks.verify(body, signature))) {
50+
console.error('Unable to verify signature!');
51+
return 401;
7252
}
53+
return 200;
54+
}
7355

56+
async function handleWorkflowJob(body: WorkflowJobEvent, githubEvent: string): Promise<number> {
7457
const disableCheckWorkflowJobLabelsEnv = process.env.DISABLE_CHECK_WORKFLOW_JOB_LABELS || 'false';
7558
const disableCheckWorkflowJobLabels = JSON.parse(disableCheckWorkflowJobLabelsEnv) as boolean;
7659
if (!disableCheckWorkflowJobLabels && !canRunJob(body)) {
@@ -91,15 +74,11 @@ async function handleWorkflowJob(body: WorkflowJob, githubEvent: string): Promis
9174
installationId: installationId,
9275
});
9376
}
77+
console.info(`Successfully queued job for ${body.repository.full_name}`);
9478
return 200;
9579
}
9680

9781
async function handleCheckRun(body: CheckRunEvent, githubEvent: string): Promise<number> {
98-
if (isRepoNotAllowed(body)) {
99-
console.error(`Received event from unauthorized repository ${body.repository.full_name}`);
100-
return 403;
101-
}
102-
10382
let installationId = body.installation?.id;
10483
if (installationId == null) {
10584
installationId = 0;
@@ -113,17 +92,18 @@ async function handleCheckRun(body: CheckRunEvent, githubEvent: string): Promise
11392
installationId: installationId,
11493
});
11594
}
95+
console.info(`Successfully queued job for ${body.repository.full_name}`);
11696
return 200;
11797
}
11898

119-
function isRepoNotAllowed(body: WorkflowJob | CheckRunEvent): boolean {
99+
function isRepoNotAllowed(repo_full_name: string): boolean {
120100
const repositoryWhiteListEnv = process.env.REPOSITORY_WHITE_LIST || '[]';
121101
const repositoryWhiteList = JSON.parse(repositoryWhiteListEnv) as Array<string>;
122102

123-
return repositoryWhiteList.length > 0 && !repositoryWhiteList.includes(body.repository.full_name);
103+
return repositoryWhiteList.length > 0 && !repositoryWhiteList.includes(repo_full_name);
124104
}
125105

126-
function canRunJob(job: WorkflowJob): boolean {
106+
function canRunJob(job: WorkflowJobEvent): boolean {
127107
const runnerLabelsEnv = process.env.RUNNER_LABELS || '[]';
128108
const runnerLabels = new Set(JSON.parse(runnerLabelsEnv) as Array<string>);
129109

0 commit comments

Comments
 (0)