From 20df56fff8ccf8dad28ba2561f2e753c843165d5 Mon Sep 17 00:00:00 2001 From: Justin Kreisher Date: Wed, 18 Nov 2026 16:09:33 -0500 Subject: [PATCH 1/3] feat(webhook-github-app): read the GitHub App key from SSM Add github_app.key_base64_ssm, read through the AWS provider's ephemeral aws_ssm_parameter so the key stays out of plan and state. Pass the key, App ID, endpoint, and secret to update-app.sh as environment variables instead of command-line flags. Co-Authored-By: Claude Opus 5.5 --- modules/webhook-github-app/README.md | 7 +++++-- modules/webhook-github-app/main.tf | 15 +++++++++++++-- modules/webhook-github-app/variables.tf | 10 ++++++++-- modules/webhook-github-app/versions.tf | 6 +++++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/modules/webhook-github-app/README.md b/modules/webhook-github-app/README.md index 66505f2ecb..63c040e585 100644 --- a/modules/webhook-github-app/README.md +++ b/modules/webhook-github-app/README.md @@ -11,13 +11,15 @@ This module updates the GitHub App webhook with the endpoint and secret and can | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.5.6 | +| [terraform](#requirement\_terraform) | >= 1.10 | +| [aws](#requirement\_aws) | >= 6.33 | | [null](#requirement\_null) | ~> 3 | ## Providers | Name | Version | |------|---------| +| [aws](#provider\_aws) | >= 6.33 | | [null](#provider\_null) | ~> 3 | ## Modules @@ -28,13 +30,14 @@ No modules. | Name | Type | |------|------| +| [aws_ssm_parameter.key_base64](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/ephemeral-resources/ssm_parameter) | ephemeral resource | | [null_resource.update_app](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [github\_app](#input\_github\_app) | 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`). |
object({
key_base64 = string
id = string
webhook_secret = string
})
| n/a | yes | +| [github\_app](#input\_github\_app) | GitHub app parameters, see your GitHub app. Provide the private key as `key_base64`, the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`), or as `key_base64_ssm`, an SSM parameter holding that value. The SSM value is read with an ephemeral resource and is not stored in plan or state. |
object({
key_base64 = optional(string)
key_base64_ssm = optional(object({ arn = string, name = string }))
id = string
webhook_secret = string
})
| n/a | yes | | [webhook\_endpoint](#input\_webhook\_endpoint) | The endpoint to use for the webhook, defaults to the endpoint of the runners module. | `string` | n/a | yes | ## Outputs diff --git a/modules/webhook-github-app/main.tf b/modules/webhook-github-app/main.tf index 609eb85279..c07220b0eb 100644 --- a/modules/webhook-github-app/main.tf +++ b/modules/webhook-github-app/main.tf @@ -1,3 +1,8 @@ +ephemeral "aws_ssm_parameter" "key_base64" { + count = var.github_app.key_base64_ssm != null ? 1 : 0 + arn = var.github_app.key_base64_ssm.arn +} + resource "null_resource" "update_app" { triggers = { webhook_endpoint = var.webhook_endpoint @@ -6,7 +11,13 @@ resource "null_resource" "update_app" { provisioner "local-exec" { interpreter = ["bash", "-c"] - command = "${path.module}/bin/update-app.sh -e ${var.webhook_endpoint} -s ${var.github_app.webhook_secret} -a ${var.github_app.id} -k ${var.github_app.key_base64}" - on_failure = continue + command = "${path.module}/bin/update-app.sh" + environment = { + APP_ID = var.github_app.id + APP_PRIVATE_KEY_BASE64 = var.github_app.key_base64_ssm != null ? ephemeral.aws_ssm_parameter.key_base64[0].value : var.github_app.key_base64 + WEBHOOK_ENDPOINT = var.webhook_endpoint + WEBHOOK_SECRET = var.github_app.webhook_secret + } + on_failure = continue } } diff --git a/modules/webhook-github-app/variables.tf b/modules/webhook-github-app/variables.tf index a9bd738509..b80d24e509 100644 --- a/modules/webhook-github-app/variables.tf +++ b/modules/webhook-github-app/variables.tf @@ -1,11 +1,17 @@ variable "github_app" { - 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`)." + description = "GitHub app parameters, see your GitHub app. Provide the private key as `key_base64`, the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`), or as `key_base64_ssm`, an SSM parameter holding that value. The SSM value is read with an ephemeral resource and is not stored in plan or state." type = object({ - key_base64 = string + key_base64 = optional(string) + key_base64_ssm = optional(object({ arn = string, name = string })) id = string webhook_secret = string }) sensitive = true + + validation { + condition = (var.github_app.key_base64 == null) != (var.github_app.key_base64_ssm == null) + error_message = "Set exactly one of github_app.key_base64 or github_app.key_base64_ssm." + } } variable "webhook_endpoint" { diff --git a/modules/webhook-github-app/versions.tf b/modules/webhook-github-app/versions.tf index ad5dce45d4..8c6a0c8bb2 100644 --- a/modules/webhook-github-app/versions.tf +++ b/modules/webhook-github-app/versions.tf @@ -1,7 +1,11 @@ terraform { - required_version = ">= 1.5.6" + required_version = ">= 1.10" required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 6.33" + } null = { source = "hashicorp/null" version = "~> 3" From 8e4758b152901476b2cf08b0b1f4d899d4862ac6 Mon Sep 17 00:00:00 2001 From: Justin Kreisher Date: Wed, 18 Nov 2026 22:26:19 -0500 Subject: [PATCH 2/3] fix(webhook-github-app): read the SSM key in the script, keep Terraform 1.5.6 Replace the ephemeral aws_ssm_parameter with an AWS CLI read in update-app.sh, so the module keeps its Terraform 1.5.6 floor and only calls AWS when the webhook update runs. Add ssm_assume_role_arn for shells whose credentials differ from the provider's. key_base64_ssm now takes precedence over key_base64, matching the root module. Co-Authored-By: Claude Opus 5.5 --- modules/webhook-github-app/README.md | 8 ++--- modules/webhook-github-app/bin/update-app.sh | 32 ++++++++++++++++++-- modules/webhook-github-app/main.tf | 15 ++++----- modules/webhook-github-app/variables.tf | 12 ++++++-- modules/webhook-github-app/versions.tf | 6 +--- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/modules/webhook-github-app/README.md b/modules/webhook-github-app/README.md index 63c040e585..1579519acd 100644 --- a/modules/webhook-github-app/README.md +++ b/modules/webhook-github-app/README.md @@ -11,15 +11,13 @@ This module updates the GitHub App webhook with the endpoint and secret and can | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.10 | -| [aws](#requirement\_aws) | >= 6.33 | +| [terraform](#requirement\_terraform) | >= 1.5.6 | | [null](#requirement\_null) | ~> 3 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 6.33 | | [null](#provider\_null) | ~> 3 | ## Modules @@ -30,14 +28,14 @@ No modules. | Name | Type | |------|------| -| [aws_ssm_parameter.key_base64](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/ephemeral-resources/ssm_parameter) | ephemeral resource | | [null_resource.update_app](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [github\_app](#input\_github\_app) | GitHub app parameters, see your GitHub app. Provide the private key as `key_base64`, the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`), or as `key_base64_ssm`, an SSM parameter holding that value. The SSM value is read with an ephemeral resource and is not stored in plan or state. |
object({
key_base64 = optional(string)
key_base64_ssm = optional(object({ arn = string, name = string }))
id = string
webhook_secret = string
})
| n/a | yes | +| [github\_app](#input\_github\_app) | GitHub app parameters, see your GitHub app. Provide the private key as `key_base64`, the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`), or as `key_base64_ssm`, an SSM parameter holding that value. The script reads the SSM parameter with the AWS CLI when it runs, so the key is not stored in plan or state. `key_base64_ssm` takes precedence over `key_base64`, and takes the same `{ arn, name }` object as the root module; only `arn` is used. |
object({
key_base64 = optional(string)
key_base64_ssm = optional(object({ arn = string, name = string }))
id = string
webhook_secret = string
})
| n/a | yes | +| [ssm\_assume\_role\_arn](#input\_ssm\_assume\_role\_arn) | IAM role the script assumes before reading `github_app.key_base64_ssm`. Set it when the shell running Terraform has different AWS credentials than the role allowed to read the parameter, for example when the AWS provider assumes a role. | `string` | `null` | no | | [webhook\_endpoint](#input\_webhook\_endpoint) | The endpoint to use for the webhook, defaults to the endpoint of the runners module. | `string` | n/a | yes | ## Outputs diff --git a/modules/webhook-github-app/bin/update-app.sh b/modules/webhook-github-app/bin/update-app.sh index d9d468b20b..6608bff5ba 100755 --- a/modules/webhook-github-app/bin/update-app.sh +++ b/modules/webhook-github-app/bin/update-app.sh @@ -23,6 +23,8 @@ usages() { echo " -a APP_ID GitHub App ID" >&2 echo " -k APP_PRIVATE_KEY_BASE64 Base64 encoded private key of the GitHub App" >&2 echo " -f APP APP_PRIVATE_KEY_FILE Path to the private key of the GitHub App" >&2 + echo " -p APP_PRIVATE_KEY_SSM_ARN ARN of an SSM parameter holding the base64 encoded private key, takes precedence over -k and -f" >&2 + echo " -r SSM_ASSUME_ROLE_ARN IAM role to assume before reading the SSM parameter" >&2 echo " -e WEBHOOK_ENDPOINT Webhook endpoint" >&2 echo " -s WEBHOOK_SECRET Webhook secret" >&2 echo " -h Show this help message" >&2 @@ -30,12 +32,14 @@ usages() { } # hadd h flag to show help -while getopts a:f:k:s:e:h flag +while getopts a:f:k:p:r:s:e:h flag do case "${flag}" in a) APP_ID=${OPTARG};; f) APP_PRIVATE_KEY_FILE=${OPTARG};; k) APP_PRIVATE_KEY_BASE64=${OPTARG};; + p) APP_PRIVATE_KEY_SSM_ARN=${OPTARG};; + r) SSM_ASSUME_ROLE_ARN=${OPTARG};; e) WEBHOOK_ENDPOINT=${OPTARG};; s) WEBHOOK_SECRET=${OPTARG};; h) usages ;; @@ -48,8 +52,8 @@ if [ -z "$APP_ID" ]; then fi # check one of variables APP_PRIVATE_KEY_PATH or APP_PRIVATE_KEY are set -if [ -z "$APP_PRIVATE_KEY_BASE64" ] && [ -z "$APP_PRIVATE_KEY_FILE" ]; then - echo "APP_PRIVATE_KEY_BASE64 or APP_PRIVATE_KEY_FILE must be set" +if [ -z "$APP_PRIVATE_KEY_BASE64" ] && [ -z "$APP_PRIVATE_KEY_FILE" ] && [ -z "$APP_PRIVATE_KEY_SSM_ARN" ]; then + echo "APP_PRIVATE_KEY_BASE64, APP_PRIVATE_KEY_FILE or APP_PRIVATE_KEY_SSM_ARN must be set" usages fi @@ -65,6 +69,28 @@ if [ -z "$WEBHOOK_SECRET" ]; then WEBHOOK_SECRET=$(terraform output --raw webhook_secret) fi +### READ PRIVATE KEY FROM SSM ### + +if [ -n "$APP_PRIVATE_KEY_SSM_ARN" ]; then + testCommand aws + # The region is the fourth field of arn:aws:ssm:::parameter/ + SSM_REGION=$(echo "$APP_PRIVATE_KEY_SSM_ARN" | cut -d: -f4) + + # Assumed-role credentials are scoped to the get-parameter call only + if [ -n "$SSM_ASSUME_ROLE_ARN" ]; then + ROLE_CREDENTIALS=$(aws sts assume-role \ + --role-arn "$SSM_ASSUME_ROLE_ARN" \ + --role-session-name webhook-github-app \ + --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \ + --output text) + read -r ROLE_ACCESS_KEY_ID ROLE_SECRET_ACCESS_KEY ROLE_SESSION_TOKEN <<< "$ROLE_CREDENTIALS" + APP_PRIVATE_KEY_BASE64=$(AWS_ACCESS_KEY_ID="$ROLE_ACCESS_KEY_ID" AWS_SECRET_ACCESS_KEY="$ROLE_SECRET_ACCESS_KEY" AWS_SESSION_TOKEN="$ROLE_SESSION_TOKEN" \ + aws ssm get-parameter --region "$SSM_REGION" --name "$APP_PRIVATE_KEY_SSM_ARN" --with-decryption --query Parameter.Value --output text) + else + APP_PRIVATE_KEY_BASE64=$(aws ssm get-parameter --region "$SSM_REGION" --name "$APP_PRIVATE_KEY_SSM_ARN" --with-decryption --query Parameter.Value --output text) + fi +fi + ### CREATE JWT TOKEN ### # Generate the JWT header and payload diff --git a/modules/webhook-github-app/main.tf b/modules/webhook-github-app/main.tf index c07220b0eb..a9606e7f77 100644 --- a/modules/webhook-github-app/main.tf +++ b/modules/webhook-github-app/main.tf @@ -1,8 +1,3 @@ -ephemeral "aws_ssm_parameter" "key_base64" { - count = var.github_app.key_base64_ssm != null ? 1 : 0 - arn = var.github_app.key_base64_ssm.arn -} - resource "null_resource" "update_app" { triggers = { webhook_endpoint = var.webhook_endpoint @@ -13,10 +8,12 @@ resource "null_resource" "update_app" { interpreter = ["bash", "-c"] command = "${path.module}/bin/update-app.sh" environment = { - APP_ID = var.github_app.id - APP_PRIVATE_KEY_BASE64 = var.github_app.key_base64_ssm != null ? ephemeral.aws_ssm_parameter.key_base64[0].value : var.github_app.key_base64 - WEBHOOK_ENDPOINT = var.webhook_endpoint - WEBHOOK_SECRET = var.github_app.webhook_secret + APP_ID = var.github_app.id + APP_PRIVATE_KEY_BASE64 = var.github_app.key_base64_ssm == null ? var.github_app.key_base64 : "" + APP_PRIVATE_KEY_SSM_ARN = var.github_app.key_base64_ssm != null ? var.github_app.key_base64_ssm.arn : "" + SSM_ASSUME_ROLE_ARN = var.ssm_assume_role_arn != null ? var.ssm_assume_role_arn : "" + WEBHOOK_ENDPOINT = var.webhook_endpoint + WEBHOOK_SECRET = var.github_app.webhook_secret } on_failure = continue } diff --git a/modules/webhook-github-app/variables.tf b/modules/webhook-github-app/variables.tf index b80d24e509..95dc74ae74 100644 --- a/modules/webhook-github-app/variables.tf +++ b/modules/webhook-github-app/variables.tf @@ -1,5 +1,5 @@ variable "github_app" { - description = "GitHub app parameters, see your GitHub app. Provide the private key as `key_base64`, the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`), or as `key_base64_ssm`, an SSM parameter holding that value. The SSM value is read with an ephemeral resource and is not stored in plan or state." + description = "GitHub app parameters, see your GitHub app. Provide the private key as `key_base64`, the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`), or as `key_base64_ssm`, an SSM parameter holding that value. The script reads the SSM parameter with the AWS CLI when it runs, so the key is not stored in plan or state. `key_base64_ssm` takes precedence over `key_base64`, and takes the same `{ arn, name }` object as the root module; only `arn` is used." type = object({ key_base64 = optional(string) key_base64_ssm = optional(object({ arn = string, name = string })) @@ -9,11 +9,17 @@ variable "github_app" { sensitive = true validation { - condition = (var.github_app.key_base64 == null) != (var.github_app.key_base64_ssm == null) - error_message = "Set exactly one of github_app.key_base64 or github_app.key_base64_ssm." + condition = var.github_app.key_base64 != null || var.github_app.key_base64_ssm != null + error_message = "Set github_app.key_base64 or github_app.key_base64_ssm." } } +variable "ssm_assume_role_arn" { + description = "IAM role the script assumes before reading `github_app.key_base64_ssm`. Set it when the shell running Terraform has different AWS credentials than the role allowed to read the parameter, for example when the AWS provider assumes a role." + type = string + default = null +} + variable "webhook_endpoint" { description = "The endpoint to use for the webhook, defaults to the endpoint of the runners module." type = string diff --git a/modules/webhook-github-app/versions.tf b/modules/webhook-github-app/versions.tf index 8c6a0c8bb2..ad5dce45d4 100644 --- a/modules/webhook-github-app/versions.tf +++ b/modules/webhook-github-app/versions.tf @@ -1,11 +1,7 @@ terraform { - required_version = ">= 1.10" + required_version = ">= 1.5.6" required_providers { - aws = { - source = "hashicorp/aws" - version = ">= 6.33" - } null = { source = "hashicorp/null" version = "~> 3" From fc4cc70ddb3f6a090974f27d5ff3bca63e04690e Mon Sep 17 00:00:00 2001 From: Justin Kreisher Date: Wed, 18 Nov 2026 22:32:35 -0500 Subject: [PATCH 3/3] fix(webhook-github-app): fail on a missing tool, validate the SSM ARN - testCommand exits 1 instead of 0 when a required tool is missing - assume-role uses the region from the parameter ARN - validate that key_base64_ssm.arn is an SSM parameter ARN - trigger a webhook update when the SSM inputs change; the trigger keys are only added when set, so key_base64 callers are unaffected - share one get-parameter call between the role and no-role paths Co-Authored-By: Claude Opus 5.5 --- modules/webhook-github-app/bin/update-app.sh | 12 ++++++++---- modules/webhook-github-app/main.tf | 13 +++++++++---- modules/webhook-github-app/variables.tf | 6 ++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/modules/webhook-github-app/bin/update-app.sh b/modules/webhook-github-app/bin/update-app.sh index 6608bff5ba..b9d84aa200 100755 --- a/modules/webhook-github-app/bin/update-app.sh +++ b/modules/webhook-github-app/bin/update-app.sh @@ -7,7 +7,7 @@ function testCommand() { if ! command -v $1 &> /dev/null then echo "$1 could not be found" - exit + exit 1 fi } @@ -76,18 +76,22 @@ if [ -n "$APP_PRIVATE_KEY_SSM_ARN" ]; then # The region is the fourth field of arn:aws:ssm:::parameter/ SSM_REGION=$(echo "$APP_PRIVATE_KEY_SSM_ARN" | cut -d: -f4) + getParameter() { + aws ssm get-parameter --region "$SSM_REGION" --name "$APP_PRIVATE_KEY_SSM_ARN" --with-decryption --query Parameter.Value --output text + } + # Assumed-role credentials are scoped to the get-parameter call only if [ -n "$SSM_ASSUME_ROLE_ARN" ]; then ROLE_CREDENTIALS=$(aws sts assume-role \ + --region "$SSM_REGION" \ --role-arn "$SSM_ASSUME_ROLE_ARN" \ --role-session-name webhook-github-app \ --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \ --output text) read -r ROLE_ACCESS_KEY_ID ROLE_SECRET_ACCESS_KEY ROLE_SESSION_TOKEN <<< "$ROLE_CREDENTIALS" - APP_PRIVATE_KEY_BASE64=$(AWS_ACCESS_KEY_ID="$ROLE_ACCESS_KEY_ID" AWS_SECRET_ACCESS_KEY="$ROLE_SECRET_ACCESS_KEY" AWS_SESSION_TOKEN="$ROLE_SESSION_TOKEN" \ - aws ssm get-parameter --region "$SSM_REGION" --name "$APP_PRIVATE_KEY_SSM_ARN" --with-decryption --query Parameter.Value --output text) + APP_PRIVATE_KEY_BASE64=$(AWS_ACCESS_KEY_ID="$ROLE_ACCESS_KEY_ID" AWS_SECRET_ACCESS_KEY="$ROLE_SECRET_ACCESS_KEY" AWS_SESSION_TOKEN="$ROLE_SESSION_TOKEN" getParameter) else - APP_PRIVATE_KEY_BASE64=$(aws ssm get-parameter --region "$SSM_REGION" --name "$APP_PRIVATE_KEY_SSM_ARN" --with-decryption --query Parameter.Value --output text) + APP_PRIVATE_KEY_BASE64=$(getParameter) fi fi diff --git a/modules/webhook-github-app/main.tf b/modules/webhook-github-app/main.tf index a9606e7f77..2ab01e4e5f 100644 --- a/modules/webhook-github-app/main.tf +++ b/modules/webhook-github-app/main.tf @@ -1,8 +1,13 @@ resource "null_resource" "update_app" { - triggers = { - webhook_endpoint = var.webhook_endpoint - webhook_secret = var.github_app.webhook_secret - } + # The SSM keys are added only when set, so callers using key_base64 keep their existing triggers + triggers = merge( + { + webhook_endpoint = var.webhook_endpoint + webhook_secret = var.github_app.webhook_secret + }, + var.github_app.key_base64_ssm != null ? { key_base64_ssm_arn = var.github_app.key_base64_ssm.arn } : {}, + var.ssm_assume_role_arn != null ? { ssm_assume_role_arn = var.ssm_assume_role_arn } : {}, + ) provisioner "local-exec" { interpreter = ["bash", "-c"] diff --git a/modules/webhook-github-app/variables.tf b/modules/webhook-github-app/variables.tf index 95dc74ae74..b41b0d7a4b 100644 --- a/modules/webhook-github-app/variables.tf +++ b/modules/webhook-github-app/variables.tf @@ -12,6 +12,12 @@ variable "github_app" { condition = var.github_app.key_base64 != null || var.github_app.key_base64_ssm != null error_message = "Set github_app.key_base64 or github_app.key_base64_ssm." } + + validation { + # can() also absorbs the error from reading arn on a null key_base64_ssm + condition = var.github_app.key_base64_ssm == null || can(regex("^arn:aws[a-z-]*:ssm:[a-z0-9-]+:[0-9]{12}:parameter/", var.github_app.key_base64_ssm.arn)) + error_message = "github_app.key_base64_ssm.arn must be an SSM parameter ARN; the script reads the region from it." + } } variable "ssm_assume_role_arn" {