From 9469429188f0348d0d3cb792d5f50fd7d433e455 Mon Sep 17 00:00:00 2001 From: Brian Ojeda <9335829+sgtoj@users.noreply.github.com> Date: Sat, 7 Nov 2026 10:04:18 -0500 Subject: [PATCH 1/4] ci: pin actions, run terraform test and tflint, and fix major version bumps Every action is pinned to a commit of its latest release, and checkouts don't persist credentials. Tests run terraform fmt, validate, and test, validate the complete example, and lint with tflint, on every pull request. The release rules listed feat:minor and refactor:minor. A custom rule that matches a commit's type wins over the commit analyzer's "breaking change -> major" rule, so feat! and refactor! commits were released as minor versions. Those rules are removed, and a release is only created when the action computes a new tag. The semantic check no longer checks out code. It runs on pull_request_target, with a token that can write to the repository, and the action doesn't need the code. Dependabot had no configuration. It now updates the actions, the Terraform providers, and the build's base images weekly. Co-Authored-By: Claude Opus 5.5 --- .github/dependabot.yml | 20 ++++++++ .github/workflows/release.yml | 21 +++++--- .github/workflows/semantic-check.yml | 6 +-- .github/workflows/test.yml | 71 +++++++++------------------- .gitignore | 13 +++-- .tflint.hcl | 8 ++++ 6 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .tflint.hcl diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..3882fea --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,20 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + groups: + github-actions: + patterns: ["*"] + + - package-ecosystem: "terraform" + directory: "/" + schedule: + interval: "weekly" + + # base images of the function's build + - package-ecosystem: "docker" + directory: "/assets/lambda-function" + schedule: + interval: "weekly" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1a899bc..2836c29 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,23 +8,32 @@ on: permissions: contents: write +concurrency: + group: release + cancel-in-progress: false + jobs: release: runs-on: ubuntu-latest steps: - name: Checkout Code - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 - + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Bump Version id: tag_version - uses: mathieudutour/github-tag-action@d28fa2ccfbd16e871a4bdf35e11b3ad1bd56c0c1 # v6.2 + uses: mathieudutour/github-tag-action@af99e60ce8132224b8e6ebab5023449fe256ed46 # v7.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} default_bump: minor - custom_release_rules: bug:patch:Fixes,chore:patch:Chores,docs:patch:Documentation,feat:minor:Features,refactor:minor:Refactors,test:patch:Tests,ci:patch:Development,dev:patch:Development - + # a custom rule that matches a commit's type wins over the default + # "breaking change -> major" rule, so types that carry breaking + # changes (feat, fix, refactor) must not be listed here. feat and + # fix use the default rules, and refactor uses default_bump. + custom_release_rules: bug:patch:Fixes,chore:patch:Chores,docs:patch:Documentation,test:patch:Tests,ci:patch:Development,dev:patch:Development - name: Create Release - uses: ncipollo/release-action@b7eabc95ff50cbeeedec83973935c8f306dfcd0b # v1.20.0 + if: steps.tag_version.outputs.new_tag != '' + uses: ncipollo/release-action@339a81892b84b4eeb0f6e744e4574d79d0d9b8dd # v1.21.0 with: tag: ${{ steps.tag_version.outputs.new_tag }} name: ${{ steps.tag_version.outputs.new_tag }} diff --git a/.github/workflows/semantic-check.yml b/.github/workflows/semantic-check.yml index da1dad8..7de9607 100644 --- a/.github/workflows/semantic-check.yml +++ b/.github/workflows/semantic-check.yml @@ -15,10 +15,8 @@ jobs: name: Semantic Commit Message Check runs-on: ubuntu-latest steps: - - name: Checkout Code - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 - - name: Check PR for Semantic Commit Message - uses: amannn/action-semantic-pull-request@48f256284bd46cdaab1048c3721360e808335d50 # v6.1.1 + - uses: amannn/action-semantic-pull-request@2252a14e810aa9b85c2d560bd7070c802206ecf6 # v6.1.2 + name: Check PR for Semantic Commit Message env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7d6cf73..65f32a6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,61 +2,36 @@ name: test on: pull_request: - branches: [ main ] - push: - branches: [ main ] permissions: contents: read + checks: write pull-requests: write -env: - TF_IN_AUTOMATION: true - jobs: - lint: - name: lint + test: runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 - - name: Setup Terraform - uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 - - name: Cache Terraform Plugins - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + - name: Checkout Code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - path: ${{ runner.temp }}/.terraform.d/plugin-cache - key: tf-plugins-${{ runner.os }}-${{ hashFiles('**/.terraform.lock.hcl') }} - - name: Initialize Terraform - run: terraform init - - name: Suggest Terraform Format - if: github.event_name == 'pull_request' - uses: reviewdog/action-suggester@fc4b06f8697e1e66bd66703f660005079be0b3dc # v1.24.0 - with: - tool_name: terraform-fmt - level: warning - github_token: ${{ secrets.GITHUB_TOKEN }} - fail_level: error - - name: Check Terraform Format - if: github.event_name == 'push' - run: terraform fmt -recursive -check - - name: Validate Terraform - uses: reviewdog/action-terraform-validate@6f4d41dfe9cf0c095aec3d341110eadb4c90c360 # v1.16.1 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - reporter: github-pr-review - level: warning - fail_level: error - # - name: Lint Terraform - # uses: reviewdog/action-tflint@9cbfc0aeae4af75fb73e8d5b6efbcd82e9483532 # v1.25.0 - # with: - # reporter: github-pr-review - # filter_mode: nofilter - # fail_level: error - - name: Lint GitHub Actions - uses: reviewdog/action-actionlint@2ca4336c1821eaff87db1f72fd3923b3250e0427 # v1.69.0 + persist-credentials: false + - name: Setup Terraform + uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1 + - name: Terraform Setup + run: terraform init -backend=false + - name: Terraform Format + run: terraform fmt -check -recursive + - name: Terraform Validate + run: terraform validate + - name: Terraform Test + run: terraform test + - name: Validate Example + run: | + terraform -chdir=examples/complete init -backend=false + terraform -chdir=examples/complete validate + - name: Lint Terraform + uses: reviewdog/action-tflint@8de8f6134bd8068970d7261f6b831964df82b92c # v1.29.0 with: - reporter: github-pr-check - fail_level: error - filter_mode: nofilter + github_token: ${{ secrets.github_token }} + filter_mode: "nofilter" diff --git a/.gitignore b/.gitignore index c979019..3596c5d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,25 +14,28 @@ override.tf.json *_override.tf *_override.tf.json -# Crash log files +# crash log files crash.log crash.*.log -# IDE +# ide .idea/ .vscode/ *.swp *.swo *~ -# OS +# os .DS_Store Thumbs.db -# Environment variables +# environment variables .env .env.local -# Build artifacts +# build artifacts dist/ *.zip + +# worktrees +.worktrees/ diff --git a/.tflint.hcl b/.tflint.hcl new file mode 100644 index 0000000..e96e113 --- /dev/null +++ b/.tflint.hcl @@ -0,0 +1,8 @@ +config { + call_module_type = "all" +} + +plugin "terraform" { + enabled = true + preset = "recommended" +} From 75c71201beb2f9b28b5886680ae759e7570e24ab Mon Sep 17 00:00:00 2001 From: Brian Ojeda <9335829+sgtoj@users.noreply.github.com> Date: Sat, 7 Nov 2026 10:04:33 -0500 Subject: [PATCH 2/4] feat!: support github-ops-app v1, build with buildkit, and keep secrets in ssm Deploy github-ops-app v1 and fix the module's own bugs. Build: - build the function with the cruxstack/buildkit provider instead of the docker-based artifact packager, and forget the packager's resources from the state with a removed block instead of destroying them - set source_code_hash, so the function redeploys when its code changes; v0 never updated the code after the first apply - build for the function's architecture, arm64 by default; v0 always built an amd64 binary, so an arm64 function couldn't start - pin the app to v1.0.0, build with go 1.27 and the lambda.norpc tag, and let newer app versions download the toolchain they need - require terraform >= 1.7, aws >= 6.72.0, buildkit >= 0.0.2, and random >= 3.9.1 github-ops-app v1: - pass the okta key id as APP_OKTA_PRIVATE_KEY_ID; v0 set APP_OKTA_KEY_ID, which the app never read - type and validate the okta sync rules like the app does, and leave unset fields out of the json; default github_user_field to githubUsername - make installation_id optional, add base_url for github enterprise server, okta scopes, and log_level, and set APP_PUBLIC_URL - support the app's installer: with installer_config, the function serves /setup and saves the github app's credentials to ssm under the prefix, and its role may write only there Secrets: - store each secret the module passes to the function as an ssm SecureString parameter, optionally with a customer managed key, and pass the parameter's arn; the role can read only those parameters - pass a secret given as a parameter arn as is, and make it readable Fixes: - the $default stage set APP_BASE_PATH to /$default, which broke every route, and the webhook_url output had a double slash - the pr bypass footer note was only sent with a pr bypass channel - a disabled module failed to plan - a schedule without its feature enabled is an error Also: a 600 second timeout by default, so a full sync can finish; API Gateway throttling; no CORS, which webhooks don't use; the eventbridge event id is passed through; and terraform tests with mocked providers. BREAKING CHANGE: bot_version, bot_repo, and bot_force_rebuild_id are renamed app_version (pinned to v1.0.0), app_repo, and app_force_rebuild_id; okta_config.key_id is private_key_id; sync rules are typed; github_user_field defaults to githubUsername; api_gateway_config.cors_allow_origins and lambda_config.runtime are removed; the function runs on arm64 and keeps secrets in ssm by default. github-ops-app v1 also needs two more github app permissions. See the README's upgrade section. Co-Authored-By: Claude Opus 5.5 --- assets/lambda-function/Dockerfile | 42 +-- examples/complete/main.tf | 188 +++++-------- main.tf | 322 ++++++++++++++++------ outputs.tf | 66 +++-- tests/module.tftest.hcl | 437 ++++++++++++++++++++++++++++++ variables.tf | 258 +++++++++++++----- versions.tf | 24 +- 7 files changed, 1011 insertions(+), 326 deletions(-) create mode 100644 tests/module.tftest.hcl diff --git a/assets/lambda-function/Dockerfile b/assets/lambda-function/Dockerfile index d1b04a3..774598e 100644 --- a/assets/lambda-function/Dockerfile +++ b/assets/lambda-function/Dockerfile @@ -1,31 +1,35 @@ -# --------------------------------------------------------------------- base --- +# Builds github-ops-app (cruxstack/github-ops-app) for AWS Lambda +# (provided.al2023) and packages it as a zip. -FROM golang:1.24-alpine AS base +# --------------------------------------------------------------------- base --- -ARG BOT_VERSION=latest -ARG BOT_REPO=https://github.com/cruxstack/github-ops-app.git +FROM golang:1.27 AS base -WORKDIR /build +# a git tag, branch, or commit of github-ops-app; "latest" builds the default +# branch, which can include breaking changes +ARG APP_VERSION=v1.0.0 +ARG APP_REPO=https://github.com/cruxstack/github-ops-app.git +ARG GOARCH=arm64 -RUN apk add --no-cache git make zip +ENV GOOS=linux +ENV GOARCH=${GOARCH} +ENV CGO_ENABLED=0 +# let newer app versions download the go toolchain their go.mod requires +ENV GOTOOLCHAIN=auto -RUN if [ "${BOT_VERSION}" = "latest" ]; then \ - git clone --depth 1 ${BOT_REPO} .; \ - else \ - git clone --depth 1 --branch ${BOT_VERSION} ${BOT_REPO} .; \ - fi +WORKDIR /opt/app -RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build \ - -ldflags="-s -w" \ - -o bootstrap \ - ./cmd/lambda +RUN git clone "$APP_REPO" . \ + && if [ "$APP_VERSION" != "latest" ]; then git checkout "$APP_VERSION"; fi +RUN go mod download +RUN go build -trimpath -tags lambda.norpc -ldflags "-s -w -X main.version=${APP_VERSION}" -o dist/bootstrap ./cmd/lambda # ------------------------------------------------------------------ package --- -FROM alpine:latest AS package +FROM alpine:3.24 AS package -COPY --from=base /build/bootstrap /opt/app/dist/bootstrap +COPY --from=base /opt/app/dist /opt/app/dist -RUN apk add zip \ +RUN apk add --no-cache zip \ && cd /opt/app/dist \ - && zip -r /tmp/package.zip . + && zip -X -r /tmp/package.zip . diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 4273289..469e963 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -7,57 +7,52 @@ module "github_ops_app" { # naming context namespace = "eg" - environment = "uw2" + environment = "ue1" stage = "prod" name = "github-ops" - # bot configuration - bot_version = "latest" # or use a specific version like "v0.1.0" + # github-ops-app release to build + app_version = "v1.0.0" - # github app configuration (required) - # webhook_secret is optional - if not provided, one will be generated + # github app (required); the installation is looked up from the org, and a + # webhook secret is generated when none is given github_app_config = { - app_id = var.github_app_id - private_key = var.github_app_private_key - installation_id = var.github_app_installation_id - org = var.github_org - # webhook_secret = var.github_webhook_secret # optional - auto-generated if omitted + org = var.github_org + app_id = var.github_app_id + private_key = var.github_app_private_key } - # okta configuration (optional) + # or let the function create the github app: leave out app_id and + # private_key, enable the installer, and visit the setup_url output + # installer_config = { enabled = true } + + # okta group sync (optional) okta_config = { - enabled = true - domain = var.okta_domain - client_id = var.okta_client_id - private_key = var.okta_private_key - key_id = var.okta_key_id # optional - github_user_field = "login" - sync_safety_threshold = 0.5 - orphaned_user_notifications = true + enabled = true + domain = var.okta_domain + client_id = var.okta_client_id + private_key = var.okta_private_key + private_key_id = var.okta_private_key_id + github_user_field = "githubUsername" sync_rules = [ - # simple format - direct mapping + # one okta group to one team { - okta_group = "engineering-team" - github_team = "engineers" + name = "platform" + okta_group_name = "Platform Engineers" + github_team_name = "platform" + create_team_if_missing = true }, + # every okta group named github-eng-* to a team named eng-* { - okta_group = "platform-team" - github_team = "platform" + name = "engineering-teams" + okta_group_pattern = "^github-eng-" + strip_prefix = "github-eng-" + github_team_prefix = "eng-" + create_team_if_missing = true }, - # pattern-based format - for multiple groups - # { - # name = "sync-all-github-teams" - # enabled = true - # okta_group_pattern = "^github-.*" - # github_team_prefix = "" - # strip_prefix = "github-" - # sync_members = true - # create_team_if_missing = true - # } ] } - # schedule okta sync every hour okta_sync_schedule = { enabled = true schedule_expression = "rate(1 hour)" @@ -67,19 +62,19 @@ module "github_ops_app" { pr_compliance_config = { enabled = true monitored_branches = ["main", "master", "release/*"] + slack_footer_note = "_Please review the ._" } - # security alerts monitoring + # stale security alerts security_alerts_config = { enabled = true - min_age_days = 30 # only report alerts older than 30 days - min_severity = "high" # minimum severity: critical, high, medium, low + min_age_days = 30 + min_severity = "high" } - # schedule security alerts check daily security_alerts_schedule = { enabled = true - schedule_expression = "rate(24 hours)" + schedule_expression = "cron(0 13 ? * MON *)" } # slack notifications (optional) @@ -87,83 +82,56 @@ module "github_ops_app" { enabled = true token = var.slack_token channel = var.slack_channel - channel_pr_bypass = var.slack_channel_pr_bypass # optional: override for PR bypass alerts - channel_okta_sync = var.slack_channel_okta_sync # optional: override for sync reports - channel_orphaned_users = var.slack_channel_orphaned_users # optional: override for orphaned user alerts - channel_security_alerts = var.slack_channel_security_alerts # optional: override for security alerts + channel_security_alerts = var.slack_channel_security_alerts } - # lambda configuration lambda_config = { - memory_size = 256 - timeout = 30 - architecture = "x86_64" + architecture = "arm64" + timeout = 600 } - lambda_log_retention_days = 30 - - # api gateway configuration api_gateway_config = { - enabled = true - stage_name = "$default" - cors_allow_origins = ["*"] + stage_name = "$default" } } -# ----------------------------------------------------------------------------- -# variables -# ----------------------------------------------------------------------------- - -variable "github_app_id" { - type = string - description = "GitHub App ID" -} - -variable "github_app_private_key" { - type = string - sensitive = true - description = "GitHub App private key (PEM format)" -} +# ---------------------------------------------------------------- variables --- -variable "github_app_installation_id" { +variable "github_org" { type = string - description = "GitHub App installation ID" + description = "GitHub organization name" } -variable "github_org" { +variable "github_app_id" { type = string - description = "GitHub organization name" + description = "GitHub App ID" } -variable "github_webhook_secret" { +variable "github_app_private_key" { type = string sensitive = true - description = "GitHub webhook secret (optional - auto-generated if not provided)" - default = "" + description = "GitHub App private key (PEM)" } variable "okta_domain" { type = string - description = "Okta domain (e.g., 'mycompany.okta.com')" - default = "" + description = "Okta domain, such as example.okta.com" } variable "okta_client_id" { type = string description = "Okta OAuth client ID" - default = "" } variable "okta_private_key" { type = string sensitive = true - description = "Okta private key for OAuth" - default = "" + description = "Okta OAuth private key (PEM)" } -variable "okta_key_id" { +variable "okta_private_key_id" { type = string - description = "Okta private key ID (optional)" + description = "Okta OAuth key ID (kid), if the app has more than one key" default = "" } @@ -171,65 +139,39 @@ variable "slack_token" { type = string sensitive = true description = "Slack bot token" - default = "" } variable "slack_channel" { type = string description = "Slack channel ID for notifications" - default = "" -} - -variable "slack_channel_pr_bypass" { - type = string - description = "Slack channel ID for PR bypass alerts (optional, falls back to slack_channel)" - default = "" -} - -variable "slack_channel_okta_sync" { - type = string - description = "Slack channel ID for Okta sync reports (optional, falls back to slack_channel)" - default = "" -} - -variable "slack_channel_orphaned_users" { - type = string - description = "Slack channel ID for orphaned user alerts (optional, falls back to slack_channel)" - default = "" } variable "slack_channel_security_alerts" { type = string - description = "Slack channel ID for security alerts (optional, falls back to slack_channel)" + description = "Slack channel ID for security alert reports; falls back to slack_channel" default = "" } -# ----------------------------------------------------------------------------- -# outputs -# ----------------------------------------------------------------------------- - -output "lambda_function_arn" { - description = "ARN of the GitHub Ops App Lambda function" - value = module.github_ops_app.lambda_function_arn -} - -output "lambda_function_name" { - description = "Name of the GitHub Ops App Lambda function" - value = module.github_ops_app.lambda_function_name -} +# ------------------------------------------------------------------ outputs --- output "webhook_url" { - description = "Webhook URL to configure in GitHub App settings" + description = "Webhook URL to set in the GitHub App's settings" value = module.github_ops_app.webhook_url } -output "api_gateway_endpoint" { - description = "API Gateway endpoint URL" - value = module.github_ops_app.api_gateway_endpoint -} - output "webhook_secret" { - description = "Webhook secret to configure in GitHub App settings" + description = "Webhook secret to set in the GitHub App's settings" value = module.github_ops_app.webhook_secret sensitive = true } + +output "admin_token" { + description = "Admin token for the /server/* and /scheduled/* endpoints" + value = module.github_ops_app.admin_token + sensitive = true +} + +output "lambda_function_name" { + description = "Name of the Lambda function" + value = module.github_ops_app.lambda_function_name +} diff --git a/main.tf b/main.tf index c1b8924..8e22e1a 100644 --- a/main.tf +++ b/main.tf @@ -4,65 +4,123 @@ locals { enabled = module.this.enabled aws_account_id = data.aws_caller_identity.current.account_id - aws_region_name = data.aws_region.current.id + aws_region_name = data.aws_region.current.region aws_partition = data.aws_partition.current.partition - # use provided webhook secret or generate one - github_webhook_secret = var.github_app_config.webhook_secret != "" ? var.github_app_config.webhook_secret : random_password.webhook_secret[0].result + goarch = var.lambda_config.architecture == "arm64" ? "arm64" : "amd64" + + # the config objects are sensitive as a whole; these flags reveal nothing + installer_enabled = var.installer_config.enabled + okta_enabled = nonsensitive(var.okta_config.enabled) + slack_enabled = nonsensitive(var.slack_config.enabled) + admin_enabled = nonsensitive(var.admin_token_config.enabled) + store_secrets = var.ssm_config.store_secrets + + generate_webhook_secret = !local.installer_enabled && nonsensitive(var.github_app_config.webhook_secret == "") + generate_admin_token = local.admin_enabled && nonsensitive(var.admin_token_config.token == "") + + # one() is null when the module is disabled and nothing is generated + github_webhook_secret = local.installer_enabled ? "" : (local.generate_webhook_secret ? one(random_password.webhook_secret[*].result) : var.github_app_config.webhook_secret) + admin_token = local.admin_enabled ? (local.generate_admin_token ? one(random_password.admin_token[*].result) : var.admin_token_config.token) : "" + + ssm_parameter_prefix = var.ssm_config.parameter_prefix != "" ? var.ssm_config.parameter_prefix : "/${module.this.id}/" + ssm_parameter_arn_prefix = "arn:${local.aws_partition}:ssm:${local.aws_region_name}:${local.aws_account_id}:parameter${local.ssm_parameter_prefix}" + + # secrets passed to the function, as ssm parameters or plain variables; in + # installer mode the installer saves the github app's own credentials + provided_secrets = merge( + local.installer_enabled ? {} : { APP_GITHUB_APP_PRIVATE_KEY = var.github_app_config.private_key }, + local.installer_enabled || local.generate_webhook_secret ? {} : { APP_GITHUB_WEBHOOK_SECRET = var.github_app_config.webhook_secret }, + local.okta_enabled ? { APP_OKTA_PRIVATE_KEY = var.okta_config.private_key } : {}, + local.slack_enabled ? { APP_SLACK_TOKEN = var.slack_config.token } : {}, + local.admin_enabled && !local.generate_admin_token ? { APP_ADMIN_TOKEN = var.admin_token_config.token } : {}, + ) + generated_secrets = merge( + local.generate_webhook_secret ? { APP_GITHUB_WEBHOOK_SECRET = local.github_webhook_secret } : {}, + local.generate_admin_token ? { APP_ADMIN_TOKEN = local.admin_token } : {}, + ) + secrets = merge(local.provided_secrets, local.generated_secrets) + secret_names = nonsensitive(toset(keys(local.secrets))) + # a secret given as an ssm parameter arn is passed as is, and its parameter + # is readable; only whether it is an arn is revealed. generated secrets are + # never arns, and are unknown until apply. + secret_given_arns = { + for name in nonsensitive(keys(local.provided_secrets)) : name => nonsensitive(local.provided_secrets[name]) + if nonsensitive(can(regex("^arn:aws[a-z-]*:ssm:", local.provided_secrets[name]))) + } + stored_secret_names = local.store_secrets ? toset([ + for name in local.secret_names : name if !contains(keys(local.secret_given_arns), name) + ]) : toset([]) + secret_environment = { + for name in local.secret_names : + name => contains(local.stored_secret_names, name) ? try(aws_ssm_parameter.secret[name].arn, null) : local.secrets[name] + } + readable_parameter_arns = distinct(concat(var.ssm_parameter_arns, values(local.secret_given_arns))) + + # the installer saves the credentials under the prefix, and the function + # reads them from there once they exist + github_credentials_environment = local.installer_enabled ? merge({ + GITHUB_APP_INSTALLER_ENABLED = "true" + STORAGE_MODE = "aws-ssm" + AWS_SSM_PARAMETER_PREFIX = local.ssm_parameter_prefix + APP_GITHUB_APP_ID = "${local.ssm_parameter_arn_prefix}APP_GITHUB_APP_ID" + APP_GITHUB_APP_PRIVATE_KEY = "${local.ssm_parameter_arn_prefix}APP_GITHUB_APP_PRIVATE_KEY" + APP_GITHUB_WEBHOOK_SECRET = "${local.ssm_parameter_arn_prefix}APP_GITHUB_WEBHOOK_SECRET" + }, var.ssm_config.kms_key_arn != "" ? { AWS_SSM_KMS_KEY_ID = var.ssm_config.kms_key_arn } : {}) : { + APP_GITHUB_APP_ID = var.github_app_config.app_id + } + + # with a named stage, api gateway passes paths with the stage prefix + api_enabled = local.enabled && var.api_gateway_config.enabled + base_path = var.api_gateway_config.enabled && var.api_gateway_config.stage_name != "$default" ? "/${var.api_gateway_config.stage_name}" : "" + public_url = local.api_enabled ? "${one(aws_apigatewayv2_api.this[*].api_endpoint)}${local.base_path}" : "" - # use provided admin token or generate one (only if enabled) - admin_token = var.admin_token_config.enabled ? (var.admin_token_config.token != "" ? var.admin_token_config.token : random_password.admin_token[0].result) : "" + # unset rule fields are left out, so the app applies its defaults + okta_sync_rules = jsonencode([ + for rule in var.okta_config.sync_rules : { for k, v in rule : k => v if v != null } + ]) lambda_environment = merge( { - # github app config - APP_GITHUB_APP_ID = var.github_app_config.app_id - APP_GITHUB_APP_PRIVATE_KEY = var.github_app_config.private_key - APP_GITHUB_INSTALLATION_ID = var.github_app_config.installation_id - APP_GITHUB_ORG = var.github_app_config.org - APP_GITHUB_WEBHOOK_SECRET = local.github_webhook_secret - - # api gateway config - APP_BASE_PATH = var.api_gateway_config.enabled ? "/${var.api_gateway_config.stage_name}" : "" - - # pr compliance config - APP_PR_COMPLIANCE_ENABLED = tostring(var.pr_compliance_config.enabled) - APP_PR_MONITORED_BRANCHES = var.pr_compliance_config.enabled ? join(",", var.pr_compliance_config.monitored_branches) : "" - - # security alerts config + APP_LOG_LEVEL = var.log_level + APP_GITHUB_ORG = var.github_app_config.org + APP_PR_COMPLIANCE_ENABLED = tostring(var.pr_compliance_config.enabled) APP_SECURITY_ALERTS_ENABLED = tostring(var.security_alerts_config.enabled) }, + var.github_app_config.installation_id != "" ? { APP_GITHUB_INSTALLATION_ID = var.github_app_config.installation_id } : {}, + var.github_app_config.base_url != "" ? { APP_GITHUB_BASE_URL = var.github_app_config.base_url } : {}, + local.base_path != "" ? { APP_BASE_PATH = local.base_path } : {}, + local.public_url != "" ? { APP_PUBLIC_URL = local.public_url } : {}, + var.pr_compliance_config.enabled ? { + APP_PR_MONITORED_BRANCHES = join(",", var.pr_compliance_config.monitored_branches) + } : {}, var.security_alerts_config.enabled ? { APP_SECURITY_ALERTS_MIN_AGE_DAYS = tostring(var.security_alerts_config.min_age_days) APP_SECURITY_ALERTS_MIN_SEVERITY = var.security_alerts_config.min_severity } : {}, - # admin token config (conditional) - local.admin_token != "" ? { APP_ADMIN_TOKEN = local.admin_token } : {}, - # okta config (conditional) - var.okta_config.enabled ? merge({ - APP_OKTA_DOMAIN = var.okta_config.domain - APP_OKTA_CLIENT_ID = var.okta_config.client_id - APP_OKTA_PRIVATE_KEY = var.okta_config.private_key - APP_OKTA_GITHUB_USER_FIELD = var.okta_config.github_user_field - APP_OKTA_SYNC_RULES = length(var.okta_config.sync_rules) > 0 ? jsonencode(var.okta_config.sync_rules) : "" - APP_OKTA_SYNC_SAFETY_THRESHOLD = tostring(var.okta_config.sync_safety_threshold) - APP_OKTA_ORPHANED_USER_NOTIFICATIONS = tostring(var.okta_config.orphaned_user_notifications) - }, var.okta_config.key_id != "" ? { APP_OKTA_KEY_ID = var.okta_config.key_id } : {}) : {}, - # slack config (conditional) - var.slack_config.enabled ? merge({ - APP_SLACK_TOKEN = var.slack_config.token - APP_SLACK_CHANNEL = var.slack_config.channel + local.okta_enabled ? merge( + { + APP_OKTA_DOMAIN = var.okta_config.domain + APP_OKTA_CLIENT_ID = var.okta_config.client_id + APP_OKTA_GITHUB_USER_FIELD = var.okta_config.github_user_field + APP_OKTA_SYNC_RULES = local.okta_sync_rules + APP_OKTA_SYNC_SAFETY_THRESHOLD = tostring(var.okta_config.sync_safety_threshold) + APP_OKTA_ORPHANED_USER_NOTIFICATIONS = tostring(var.okta_config.orphaned_user_notifications) }, + var.okta_config.private_key_id != "" ? { APP_OKTA_PRIVATE_KEY_ID = var.okta_config.private_key_id } : {}, + length(var.okta_config.scopes) > 0 ? { APP_OKTA_SCOPES = join(",", var.okta_config.scopes) } : {}, + ) : {}, + local.slack_enabled ? merge( + { APP_SLACK_CHANNEL = var.slack_config.channel }, + var.slack_config.channel_pr_bypass != "" ? { APP_SLACK_CHANNEL_PR_BYPASS = var.slack_config.channel_pr_bypass } : {}, var.slack_config.channel_okta_sync != "" ? { APP_SLACK_CHANNEL_OKTA_SYNC = var.slack_config.channel_okta_sync } : {}, var.slack_config.channel_orphaned_users != "" ? { APP_SLACK_CHANNEL_ORPHANED_USERS = var.slack_config.channel_orphaned_users } : {}, var.slack_config.channel_security_alerts != "" ? { APP_SLACK_CHANNEL_SECURITY_ALERTS = var.slack_config.channel_security_alerts } : {}, - var.slack_config.channel_pr_bypass != "" ? { - APP_SLACK_CHANNEL_PR_BYPASS = var.slack_config.channel_pr_bypass - APP_SLACK_FOOTER_NOTE_PR_BYPASS = var.pr_compliance_config.slack_footer_note - } : {}, + var.pr_compliance_config.slack_footer_note != "" ? { APP_SLACK_FOOTER_NOTE_PR_BYPASS = var.pr_compliance_config.slack_footer_note } : {}, ) : {}, - # additional environment variables - var.lambda_environment_variables + local.github_credentials_environment, + local.secret_environment, + var.lambda_environment_variables, ) } @@ -70,67 +128,108 @@ data "aws_caller_identity" "current" {} data "aws_region" "current" {} data "aws_partition" "current" {} -# ======================================================== webhook secret === +# ================================================================= secrets === resource "random_password" "webhook_secret" { - count = local.enabled && var.github_app_config.webhook_secret == "" ? 1 : 0 + count = local.enabled && local.generate_webhook_secret ? 1 : 0 length = 32 special = false } resource "random_password" "admin_token" { - count = local.enabled && var.admin_token_config.enabled && var.admin_token_config.token == "" ? 1 : 0 + count = local.enabled && local.generate_admin_token ? 1 : 0 length = 32 special = false } +resource "aws_ssm_parameter" "secret" { + for_each = local.enabled ? local.stored_secret_names : toset([]) + + name = "${local.ssm_parameter_prefix}${each.key}" + description = "${each.key} for ${module.this.id}" + type = "SecureString" + value = local.secrets[each.key] + key_id = var.ssm_config.kms_key_arn != "" ? var.ssm_config.kms_key_arn : null + + tags = module.this.tags +} + # ================================================================== lambda === -module "bot_artifact" { - source = "github.com/cruxstack/terraform-docker-artifact-packager?ref=v1.4.0" +# transitional: forget the pre-buildkit module.bot_artifact from state on +# upgrade, instead of trying to destroy its docker image +removed { + from = module.bot_artifact + lifecycle { + destroy = false + } +} + +data "buildkit_context" "app" { count = local.enabled ? 1 : 0 - attributes = ["lambda"] - artifact_src_path = "/tmp/package.zip" - artifact_dst_directory = "${path.module}/dist" - docker_build_context = abspath("${path.module}/assets/lambda-function") - docker_build_target = "package" - force_rebuild_id = var.bot_force_rebuild_id + path = abspath("${path.module}/assets/lambda-function") +} + +# github-ops-app built at app_version for the function's architecture +resource "buildkit_artifact" "app" { + count = local.enabled ? 1 : 0 - docker_build_args = { - BOT_VERSION = var.bot_version - BOT_REPO = var.bot_repo + build_context = abspath("${path.module}/assets/lambda-function") + dockerfile = "Dockerfile" + target = "package" + artifact_src_path = "/tmp/package.zip" + artifact_src_type = "zip" + artifact_dst_path = "${path.module}/dist/${module.this.id}/package.zip" + + build_args = { + APP_VERSION = var.app_version + APP_REPO = var.app_repo + GOARCH = local.goarch } - context = module.this.context + triggers = { + context = data.buildkit_context.app[0].digest + app_version = var.app_version + app_repo = var.app_repo + goarch = local.goarch + force_rebuild_id = var.app_force_rebuild_id + } } resource "aws_lambda_function" "this" { count = local.enabled ? 1 : 0 function_name = module.this.id - description = "GitHub Ops App - Automates GitHub organization operations including Okta sync, PR compliance, and team management" + description = "GitHub Ops App: Okta group sync, PR compliance, and security alert reports" role = aws_iam_role.this[0].arn handler = "bootstrap" - runtime = var.lambda_config.runtime + runtime = "provided.al2023" memory_size = var.lambda_config.memory_size timeout = var.lambda_config.timeout reserved_concurrent_executions = var.lambda_config.reserved_concurrent_executions architectures = [var.lambda_config.architecture] - filename = module.bot_artifact[0].artifact_package_path + filename = buildkit_artifact.app[0].artifact_path + source_code_hash = buildkit_artifact.app[0].artifact_sha256 environment { variables = local.lambda_environment } + lifecycle { + precondition { + condition = local.installer_enabled || nonsensitive(var.github_app_config.app_id != "" && var.github_app_config.private_key != "") + error_message = "github_app_config.app_id and private_key are required unless installer_config.enabled is true." + } + } + depends_on = [ aws_cloudwatch_log_group.lambda, aws_iam_role_policy.this, - module.bot_artifact ] tags = module.this.tags @@ -147,29 +246,27 @@ resource "aws_cloudwatch_log_group" "lambda" { # ============================================================= api gateway === resource "aws_apigatewayv2_api" "this" { - count = local.enabled && var.api_gateway_config.enabled ? 1 : 0 + count = local.api_enabled ? 1 : 0 name = module.this.id protocol_type = "HTTP" - description = "API Gateway for GitHub Ops App webhooks" - - cors_configuration { - allow_origins = var.api_gateway_config.cors_allow_origins - allow_methods = ["POST", "GET", "OPTIONS"] - allow_headers = ["Content-Type", "X-Hub-Signature-256", "X-GitHub-Event", "X-GitHub-Delivery"] - max_age = 300 - } + description = "GitHub Ops App webhooks and admin API" tags = module.this.tags } resource "aws_apigatewayv2_stage" "this" { - count = local.enabled && var.api_gateway_config.enabled ? 1 : 0 + count = local.api_enabled ? 1 : 0 api_id = aws_apigatewayv2_api.this[0].id name = var.api_gateway_config.stage_name auto_deploy = true + default_route_settings { + throttling_burst_limit = var.api_gateway_config.throttling_burst_limit + throttling_rate_limit = var.api_gateway_config.throttling_rate_limit + } + access_log_settings { destination_arn = aws_cloudwatch_log_group.api_gateway[0].arn format = jsonencode({ @@ -177,11 +274,13 @@ resource "aws_apigatewayv2_stage" "this" { ip = "$context.identity.sourceIp" requestTime = "$context.requestTime" httpMethod = "$context.httpMethod" + path = "$context.path" routeKey = "$context.routeKey" status = "$context.status" protocol = "$context.protocol" responseLength = "$context.responseLength" integrationStatus = "$context.integrationStatus" + integrationError = "$context.integrationErrorMessage" }) } @@ -189,7 +288,7 @@ resource "aws_apigatewayv2_stage" "this" { } resource "aws_cloudwatch_log_group" "api_gateway" { - count = local.enabled && var.api_gateway_config.enabled ? 1 : 0 + count = local.api_enabled ? 1 : 0 name = "/aws/apigateway/${module.this.id}" retention_in_days = var.lambda_log_retention_days @@ -197,7 +296,7 @@ resource "aws_cloudwatch_log_group" "api_gateway" { } resource "aws_apigatewayv2_integration" "this" { - count = local.enabled && var.api_gateway_config.enabled ? 1 : 0 + count = local.api_enabled ? 1 : 0 api_id = aws_apigatewayv2_api.this[0].id integration_type = "AWS_PROXY" @@ -206,7 +305,7 @@ resource "aws_apigatewayv2_integration" "this" { } resource "aws_apigatewayv2_route" "proxy" { - count = local.enabled && var.api_gateway_config.enabled ? 1 : 0 + count = local.api_enabled ? 1 : 0 api_id = aws_apigatewayv2_api.this[0].id route_key = "ANY /{proxy+}" @@ -214,7 +313,7 @@ resource "aws_apigatewayv2_route" "proxy" { } resource "aws_lambda_permission" "api_gateway" { - count = local.enabled && var.api_gateway_config.enabled ? 1 : 0 + count = local.api_enabled ? 1 : 0 statement_id = "AllowExecutionFromAPIGateway" action = "lambda:InvokeFunction" @@ -225,13 +324,22 @@ resource "aws_lambda_permission" "api_gateway" { # ============================================================= eventbridge === +# each schedule sends an event whose detail names the scheduled action + resource "aws_cloudwatch_event_rule" "okta_sync" { count = local.enabled && var.okta_sync_schedule.enabled ? 1 : 0 name = "${module.this.id}-okta-sync" - description = "Scheduled trigger for Okta-GitHub team sync" + description = "Scheduled Okta group sync of ${module.this.id}" schedule_expression = var.okta_sync_schedule.schedule_expression tags = module.this.tags + + lifecycle { + precondition { + condition = local.okta_enabled + error_message = "okta_sync_schedule requires okta_config.enabled." + } + } } resource "aws_cloudwatch_event_target" "okta_sync" { @@ -243,6 +351,7 @@ resource "aws_cloudwatch_event_target" "okta_sync" { input_transformer { input_paths = { + id = "$.id" source = "$.source" detail_type = "$.detail-type" time = "$.time" @@ -251,6 +360,7 @@ resource "aws_cloudwatch_event_target" "okta_sync" { input_template = <<-EOF { "version": "0", + "id": , "source": , "detail-type": , "time":