diff --git a/modules/webhook-github-app/README.md b/modules/webhook-github-app/README.md index 66505f2ecb..1579519acd 100644 --- a/modules/webhook-github-app/README.md +++ b/modules/webhook-github-app/README.md @@ -34,7 +34,8 @@ No modules. | 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 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..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 } @@ -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,32 @@ 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) + + 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" getParameter) + else + APP_PRIVATE_KEY_BASE64=$(getParameter) + 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 609eb85279..2ab01e4e5f 100644 --- a/modules/webhook-github-app/main.tf +++ b/modules/webhook-github-app/main.tf @@ -1,12 +1,25 @@ 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"] - 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 ? 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 a9bd738509..b41b0d7a4b 100644 --- a/modules/webhook-github-app/variables.tf +++ b/modules/webhook-github-app/variables.tf @@ -1,11 +1,29 @@ 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 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 = 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 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" { + 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" {