Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/webhook-github-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_github_app"></a> [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`). | <pre>object({<br/> key_base64 = string<br/> id = string<br/> webhook_secret = string<br/> })</pre> | n/a | yes |
| <a name="input_github_app"></a> [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. | <pre>object({<br/> key_base64 = optional(string)<br/> key_base64_ssm = optional(object({ arn = string, name = string }))<br/> id = string<br/> webhook_secret = string<br/> })</pre> | n/a | yes |
| <a name="input_ssm_assume_role_arn"></a> [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 |
| <a name="input_webhook_endpoint"></a> [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
Expand Down
38 changes: 34 additions & 4 deletions modules/webhook-github-app/bin/update-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function testCommand() {
if ! command -v $1 &> /dev/null
then
echo "$1 could not be found"
exit
exit 1
fi
}

Expand All @@ -23,19 +23,23 @@ 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
exit 1
}

# 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 ;;
Expand All @@ -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

Expand All @@ -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:<region>:<account>:parameter/<name>
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
Expand Down
25 changes: 19 additions & 6 deletions modules/webhook-github-app/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 20 additions & 2 deletions modules/webhook-github-app/variables.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down