Skip to content

Commit ad304f8

Browse files
committed
feat: Add option to let the module manage the webhook secret
1 parent a71f1d8 commit ad304f8

File tree

17 files changed

+124
-38
lines changed

17 files changed

+124
-38
lines changed

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ To be able to support a number of use-cases, the module has quite a lot of confi
1414
- Spot vs on-demand. The runners use either the EC2 spot or on-demand life cycle. Runners will be created via the AWS [CreateFleet API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateFleet.html). The module (scale up lambda) will request via the CreateFleet API to create instances in one of the subnets and of the specified instance types.
1515
- ARM64 support via Graviton/Graviton2 instance-types. When using the default example or top-level module, specifying `instance_types` that match a Graviton/Graviton 2 (ARM64) architecture (e.g. a1, t4g or any 6th-gen `g` or `gd` type), you must also specify `runner_architecture = "arm64"` and the sub-modules will be automatically configured to provision with ARM64 AMIs and leverage GitHub's ARM64 action runner. See below for more details.
1616
- Disable default labels for the runners (os, architecture and `self-hosted`) can achieve by setting `runner_disable_default_labels` = true. If enabled, the runner will only have the extra labels provided in `runner_extra_labels`. In case you on own start script is used, this configuration parameter needs to be parsed via SSM.
17+
- Managed vs self-managed webhook secret. The module can manage the webhook secret for you. In that case simply do not provide a value for `github_app.webhook_secret`. If you want to manage the secret yourself, provide a value for `github_app.webhook_secret`. The secret will be managed and a rotation is triggered once running terraform apply again after `github_app.webhook_secret_rotation_days` days. **Important note**: THe managed webhook secret depends on a local-exec (bash) to update the secret in GitNub. It will also update the webhook url.
1718

1819
## AWS SSM Parameters
1920

docs/getting-started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module "github-runner" {
8585
github_app = {
8686
key_base64 = "base64string"
8787
id = "1"
88-
webhook_secret = "webhook_secret"
88+
webhook_secret = "webhook_secret" # optional, if not set the module will manage the secret.
8989
}
9090
9191
webhook_lambda_zip = "lambdas-download/webhook.zip"
@@ -109,7 +109,7 @@ The lambda for syncing the GitHub distribution to S3 is triggered via CloudWatch
109109
### Setup the webhook / GitHub App (part 2)
110110

111111
At this point you have two options. Either create a separate webhook (enterprise,
112-
org, or repo), or create a webhook in the App.
112+
org, or repo), or create a webhook in the App. In case you have not provided a Webhook secret the module will create one and update the GitHub app with both the secret and the webhook url.
113113

114114
#### Option 1: Webhook
115115

examples/default/.terraform.lock.hcl

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/default/main.tf

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ locals {
33
aws_region = var.aws_region
44
}
55

6-
resource "random_id" "random" {
7-
byte_length = 20
8-
}
9-
106
module "base" {
117
source = "../base"
128

@@ -27,9 +23,9 @@ module "runners" {
2723
}
2824

2925
github_app = {
30-
key_base64 = var.github_app.key_base64
31-
id = var.github_app.id
32-
webhook_secret = random_id.random.hex
26+
key_base64 = var.github_app.key_base64
27+
id = var.github_app.id
28+
# webhook_secret = random_id.random.hex
3329
}
3430

3531
# configure the block device mappings, default for Amazon Linux2
@@ -143,18 +139,6 @@ module "runners" {
143139
# kms_key_arn = aws_kms_key.github.arn
144140
}
145141

146-
module "webhook_github_app" {
147-
source = "../../modules/webhook-github-app"
148-
depends_on = [module.runners]
149-
150-
github_app = {
151-
key_base64 = var.github_app.key_base64
152-
id = var.github_app.id
153-
webhook_secret = random_id.random.hex
154-
}
155-
webhook_endpoint = module.runners.webhook.endpoint
156-
}
157-
158142
# enable CMK instead of aws managed key for encryptions
159143
# resource "aws_kms_key" "github" {
160144
# is_enabled = true

examples/default/outputs.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,3 @@ output "runners" {
77
output "webhook_endpoint" {
88
value = module.runners.webhook.endpoint
99
}
10-
11-
output "webhook_secret" {
12-
sensitive = true
13-
value = random_id.random.hex
14-
}
15-

main.tf

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ locals {
1212
runner_labels = (var.runner_disable_default_labels == false) ? sort(concat(local.default_runner_labels, var.runner_extra_labels)) : var.runner_extra_labels
1313

1414
ssm_root_path = var.ssm_paths.use_prefix ? "/${var.ssm_paths.root}/${var.prefix}" : "/${var.ssm_paths.root}"
15+
16+
github_app = merge(var.github_app, {
17+
webhook_secret = var.github_app.webhook_secret != null ? var.github_app.webhook_secret : module.rotating_random[0].random.hex
18+
})
19+
}
20+
21+
module "rotating_random" {
22+
count = var.github_app.webhook_secret == null ? 1 : 0
23+
source = "./modules/rotating-random"
24+
25+
rotation_days = var.github_app.webhook_secret_rotation_days
1526
}
1627

1728
resource "random_string" "random" {
@@ -91,10 +102,18 @@ module "ssm" {
91102

92103
kms_key_arn = var.kms_key_arn
93104
path_prefix = "${local.ssm_root_path}/${var.ssm_paths.app}"
94-
github_app = var.github_app
105+
github_app = local.github_app
95106
tags = local.tags
96107
}
97108

109+
module "webhook_github_app" {
110+
count = var.github_app.webhook_secret == null ? 1 : 0
111+
source = "./modules/webhook-github-app"
112+
113+
github_app = local.github_app
114+
webhook_endpoint = "${module.webhook.gateway.api_endpoint}/${module.webhook.endpoint_relative_path}"
115+
}
116+
98117
module "webhook" {
99118
source = "./modules/webhook"
100119

modules/multi-runner/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ locals {
1616
unique_os_and_arch = { for i, v in local.tmp_distinct_list_unique_os_and_arch : "${v.os_type}_${v.architecture}" => v }
1717

1818
ssm_root_path = "/${var.ssm_paths.root}/${var.prefix}"
19+
20+
github_app = merge(var.github_app, {
21+
webhook_secret = var.github_app.webhook_secret != null ? var.github_app.webhook_secret : module.rotating_random[0].random.hex
22+
})
23+
}
24+
25+
module "rotating_random" {
26+
count = var.github_app.webhook_secret == null ? 1 : 0
27+
source = "./../rotating-random"
28+
29+
rotation_days = var.github_app.webhook_secret_rotation_days
1930
}
2031

2132
resource "random_string" "random" {

modules/multi-runner/ssm.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module "ssm" {
33

44
kms_key_arn = var.kms_key_arn
55
path_prefix = "${local.ssm_root_path}/${var.ssm_paths.app}"
6-
github_app = var.github_app
6+
github_app = local.github_app
77
tags = local.tags
88
}

modules/multi-runner/variables.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
variable "github_app" {
2-
description = "GitHub app parameters, see your github app. Ensure the key is the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`)."
2+
description = <<EOF
3+
GitHub app parameters, see your github app. Ensure the key is the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`)."
4+
5+
If `webhook_secret` is not set, a random secret will be generated and stored in SSM. The secret is used to validate the webhook events. If you want to use your own secret, set the `webhook_secret` parameter.
6+
When the secret is managed by the module, it will be rotated every `webhook_secret_rotation_days` days.
7+
EOF
38
type = object({
4-
key_base64 = string
5-
id = string
6-
webhook_secret = string
9+
key_base64 = string
10+
id = string
11+
webhook_secret = optional(string)
12+
webhook_secret_rotation_days = optional(number, 30)
713
})
814
}
915

modules/multi-runner/webhook.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
module "webhook_github_app" {
2+
count = var.github_app.webhook_secret == null ? 1 : 0
3+
source = "./../webhook-github-app"
4+
5+
github_app = local.github_app
6+
webhook_endpoint = "${module.webhook.gateway.api_endpoint}/${module.webhook.endpoint_relative_path}"
7+
}
8+
19
module "webhook" {
210
source = "../webhook"
311
prefix = var.prefix

0 commit comments

Comments
 (0)