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

Commit b1f451a

Browse files
ravenolfravenolf
authored andcommitted
feat: Added support for white listing of repositories (#915)
* add white listing of repositories Signed-off-by: ravenolf <[email protected]> * fix variable naming Signed-off-by: ravenolf <[email protected]> * add unit test * update docs * add successful unit test Co-authored-by: ravenolf <[email protected]>
1 parent 5b4cbff commit b1f451a

File tree

8 files changed

+51
-0
lines changed

8 files changed

+51
-0
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ This [Terraform](https://www.terraform.io/) module creates the required infrastr
1919
- [Debugging](#debugging)
2020
- [Requirements](#requirements)
2121
- [Providers](#providers)
22+
- [Modules](#modules)
23+
- [Resources](#resources)
2224
- [Inputs](#inputs)
2325
- [Outputs](#outputs)
2426
- [Contribution](#contribution)
@@ -366,6 +368,7 @@ No requirements.
366368
| manage\_kms\_key | Let the module manage the KMS key. | `bool` | `true` | no |
367369
| market\_options | Market options for the action runner instances. Setting the value to `null` let the scaler create on-demand instances instead of spot instances. | `string` | `"spot"` | no |
368370
| minimum\_running\_time\_in\_minutes | The time an ec2 action runner should be running at minimum before terminated if non busy. | `number` | `5` | no |
371+
| repository\_white\_list | (optional) List of github repository full names (owner/repo_name) that will be allowed to call the runners. Leave empty for no filtering | `list(string)` | `[]` | no |
369372
| role\_path | The path that will be added to role path for created roles, if not set the environment name will be used. | `string` | `null` | no |
370373
| role\_permissions\_boundary | Permissions boundary that will be added to the created roles. | `string` | `null` | no |
371374
| runner\_additional\_security\_group\_ids | (optional) List of additional security groups IDs to apply to the runner | `list(string)` | `[]` | no |

Diff for: main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module "webhook" {
4949

5050
role_path = var.role_path
5151
role_permissions_boundary = var.role_permissions_boundary
52+
repository_white_list = var.repository_white_list
5253
}
5354

5455
module "runners" {

Diff for: modules/webhook/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ No requirements.
5656
| lambda\_timeout | Time out of the lambda in seconds. | `number` | `10` | no |
5757
| lambda\_zip | File location of the lambda zip file. | `string` | `null` | no |
5858
| logging\_retention\_in\_days | Specifies the number of days you want to retain log events for the lambda log group. Possible values are: 0, 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653. | `number` | `7` | no |
59+
| repository\_white\_list | List of github repository full names (owner/repo_name) that will be allowed to call the runners. Leave empty for no filtering | `list(string)` | `[]` | no |
5960
| role\_path | The path that will be added to the role, if not set the environment name will be used. | `string` | `null` | no |
6061
| role\_permissions\_boundary | Permissions boundary that will be added to the created role for the lambda. | `string` | `null` | no |
6162
| sqs\_build\_queue | SQS queue to publish accepted build events. | <pre>object({<br> id = string<br> arn = string<br> })</pre> | n/a | yes |

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

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('handler', () => {
1414
let originalError: Console['error'];
1515

1616
beforeEach(() => {
17+
process.env.REPOSITORY_WHITE_LIST = '[]';
1718
process.env.GITHUB_APP_WEBHOOK_SECRET = 'TEST_SECRET';
1819
originalError = console.error;
1920
console.error = jest.fn();
@@ -71,4 +72,25 @@ describe('handler', () => {
7172
expect(resp).toBe(200);
7273
expect(sendActionRequest).not.toBeCalled();
7374
});
75+
76+
it('does not handle check_run events from unlisted repositories', async () => {
77+
process.env.REPOSITORY_WHITE_LIST = '["NotCodertocat/Hello-World"]';
78+
const resp = await handle(
79+
{ 'X-Hub-Signature': 'sha1=4a82d2f60346e16dab3546eb3b56d8dde4d5b659', 'X-GitHub-Event': 'check_run' },
80+
JSON.stringify(check_run_event),
81+
);
82+
expect(resp).toBe(500);
83+
expect(sendActionRequest).not.toBeCalled();
84+
});
85+
86+
it('handles check_run events from whitelisted repositories', async () => {
87+
process.env.REPOSITORY_WHITE_LIST = '["Codertocat/Hello-World"]';
88+
const resp = await handle(
89+
{ 'X-Hub-Signature': 'sha1=4a82d2f60346e16dab3546eb3b56d8dde4d5b659', 'X-GitHub-Event': 'check_run' },
90+
JSON.stringify(check_run_event),
91+
);
92+
expect(resp).toBe(200);
93+
expect(sendActionRequest).toBeCalled();
94+
});
95+
7496
});

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

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ export const handle = async (headers: IncomingHttpHeaders, payload: any): Promis
4040

4141
if (githubEvent === 'check_run') {
4242
const body = JSON.parse(payload) as CheckRunEvent;
43+
44+
const repositoryWhiteListEnv = process.env.REPOSITORY_WHITE_LIST as string || "[]";
45+
const repositoryWhiteList = JSON.parse(repositoryWhiteListEnv) as Array<string>;
46+
47+
if (repositoryWhiteList.length > 0) {
48+
const repositoryFullName = body.repository.full_name;
49+
if (!repositoryWhiteList.includes(repositoryFullName)) {
50+
console.error(`Received event from unauthorized repository ${repositoryFullName}`);
51+
return 500;
52+
}
53+
}
54+
4355
let installationId = body.installation?.id;
4456
if (installationId == null) {
4557
installationId = 0;

Diff for: modules/webhook/variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ variable "webhook_lambda_s3_object_version" {
7979
default = null
8080
}
8181

82+
variable "repository_white_list" {
83+
description = "List of repositories allowed to use the github app"
84+
type = list(string)
85+
default = []
86+
}

Diff for: modules/webhook/webhook.tf

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ resource "aws_lambda_function" "webhook" {
4444
KMS_KEY_ID = var.encryption.kms_key_id
4545
GITHUB_APP_WEBHOOK_SECRET = local.github_app_webhook_secret
4646
SQS_URL_WEBHOOK = var.sqs_build_queue.id
47+
REPOSITORY_WHITE_LIST = jsonencode(var.repository_white_list)
4748
}
4849
}
4950

Diff for: variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,9 @@ variable "instance_types" {
360360
type = set(string)
361361
default = null
362362
}
363+
364+
variable "repository_white_list" {
365+
description = "List of repositories allowed to use the github app"
366+
type = list(string)
367+
default = []
368+
}

0 commit comments

Comments
 (0)