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" +} diff --git a/README.md b/README.md index 967f0f4..ad4aa86 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,38 @@ # terraform-aws-github-ops-app -Terraform module that deploys the -[GitHub Ops App](https://github.com/cruxstack/github-ops-app) to AWS Lambda with -API Gateway for webhook handling. +Terraform module that deploys +[github-ops-app](https://github.com/cruxstack/github-ops-app) to AWS Lambda, +with an API Gateway HTTP API for GitHub webhooks and EventBridge schedules for +its periodic jobs. ## Features -- **Okta Group Sync** - Automatically syncs Okta groups to GitHub teams based on - configurable rules -- **Orphaned User Detection** - Identifies org members not in any synced Okta - teams -- **PR Compliance Monitoring** - Detects when PRs bypass branch protection rules -- **Automatic Reconciliation** - Detects external team changes and triggers sync -- **Slack Notifications** - Rich messages for violations and sync reports +- **Okta group sync** - Sync Okta group members to GitHub teams on a schedule + and after someone changes a team outside Okta +- **PR compliance monitoring** - Report PRs merged without the reviews and + status checks their branch requires +- **Security alerts monitoring** - Report stale Dependabot, code scanning, and + secret scanning alerts +- **GitHub App installer** - Optionally create the GitHub App from the + function, with its credentials saved to SSM +- **Secrets in SSM** - Secrets are SecureString parameters the function reads + at startup, not values in its configuration +- **Slack notifications** - Reports for each feature, in per-feature channels + +## Requirements + +| Name | Version | +| --------- | ----------- | +| terraform | `>= 1.7` | +| aws | `>= 6.72.0` | +| buildkit | `>= 0.0.2` | +| random | `>= 3.9.1` | + +The module builds the function from source with the +[cruxstack/buildkit](https://registry.terraform.io/providers/cruxstack/buildkit) +provider, which needs a BuildKit endpoint where Terraform runs: Docker Desktop, +OrbStack, or Colima, `BUILDKIT_HOST`, or an embedded rootless `buildkitd` on +Linux. ## Usage @@ -22,211 +42,187 @@ module "github_ops_app" { name = "github-ops" - # github app configuration (required) - # webhook_secret is optional - if not provided, one will be auto-generated github_app_config = { - app_id = "123456" - private_key = var.github_app_private_key - installation_id = "12345678" - org = "my-org" + org = "acme" + app_id = "123456" + private_key = var.github_app_private_key } - # okta configuration (optional) okta_config = { - enabled = true - domain = "mycompany.okta.com" - client_id = var.okta_client_id - private_key = var.okta_private_key - github_user_field = "login" - sync_safety_threshold = 0.5 + enabled = true + domain = "acme.okta.com" + client_id = var.okta_client_id + private_key = var.okta_private_key sync_rules = [ { - okta_group = "engineering-team" - github_team = "engineers" - } + okta_group_pattern = "^github-eng-" + strip_prefix = "github-eng-" + github_team_prefix = "eng-" + create_team_if_missing = true + }, ] } - # schedule okta sync every hour okta_sync_schedule = { - enabled = true - schedule_expression = "rate(1 hour)" + enabled = true } - # pr compliance monitoring pr_compliance_config = { enabled = true - monitored_branches = ["main", "master"] + monitored_branches = ["main", "release/*"] } - # slack notifications (optional) slack_config = { - enabled = true - token = var.slack_token - channel = "#github-ops" - channel_pr_bypass = "#security-alerts" # optional: override for PR bypass alerts - channel_okta_sync = "#team-sync" # optional: override for sync reports - channel_orphaned_users = "#security-alerts" # optional: override for orphaned user alerts + enabled = true + token = var.slack_token + channel = "C01234ABCDE" } } ``` -## Quick Start - -- **Create a GitHub App** with the required permissions - - [GitHub Ops App documentation](https://github.com/cruxstack/github-ops-app)) -- **Deploy this module** with your GitHub App credentials -- **Configure the webhook URL** in your GitHub App settings using the - `webhook_url` output -- **Optionally configure Okta** for team synchronization -- **Optionally configure Slack** for notifications +Then set the GitHub App's webhook URL and secret to the `webhook_url` and +`webhook_secret` outputs. The app needs the permissions listed in +[github-ops-app's setup guide](https://github.com/cruxstack/github-ops-app/blob/main/docs/github-app-setup.md#permissions). -## Examples +See [examples/complete](examples/complete) for every feature. -- [Complete Example](examples/complete) - Full configuration with all features - enabled +### GitHub App Installer -## Inputs - -| Name | Description | Type | Default | Required | -| ------------------------------ | ----------------------------------------------------------------------------- | -------------- | --------------------------------------------------- | :------: | -| `admin_token_config` | Configuration for admin token protecting some endpoints | `object` | `{ enabled = true }` | no | -| `api_gateway_config` | Configuration for the API Gateway | `object` | `{}` | no | -| `bot_force_rebuild_id` | ID to force rebuilding the Lambda function source code | `string` | `""` | no | -| `bot_repo` | GitHub repository URL for the GitHub Ops App source code | `string` | `"https://github.com/cruxstack/github-ops-app.git"` | no | -| `bot_version` | Version of the GitHub Ops App to use (`latest` or specific tag like `v0.1.0`) | `string` | `"latest"` | no | -| `github_app_config` | GitHub App configuration for authentication and webhook handling | `object` | n/a | yes | -| `lambda_config` | Configuration for the Lambda function | `object` | `{}` | no | -| `lambda_environment_variables` | Additional environment variables for the Lambda function | `map(string)` | `{}` | no | -| `lambda_log_retention_days` | Number of days to retain Lambda function logs | `number` | `30` | no | -| `okta_config` | Okta configuration for user and group synchronization | `object` | `{}` | no | -| `okta_sync_schedule` | EventBridge schedule configuration for automatic Okta sync | `object` | `{}` | no | -| `pr_compliance_config` | Configuration for PR compliance monitoring | `object` | `{}` | no | -| `slack_config` | Slack integration configuration for notifications | `object` | `{}` | no | -| `ssm_parameter_arns` | List of SSM Parameter Store ARNs for secrets retrieval | `list(string)` | `[]` | no | - -### GitHub App Config +Leave out the GitHub App's credentials and enable the installer to create the +app from the function: ```hcl -github_app_config = { - app_id = string # GitHub App ID - private_key = string # GitHub App private key (PEM format) - installation_id = string # GitHub App installation ID - org = string # GitHub organization name - webhook_secret = string # (optional) GitHub webhook secret - auto-generated if not provided -} -``` +module "github_ops_app" { + source = "github.com/cruxstack/terraform-aws-github-ops-app?ref=v1.0.0" -### Okta Config + name = "github-ops" -```hcl -okta_config = { - enabled = bool # Enable Okta integration (default: false) - domain = string # Okta domain (e.g., "mycompany.okta.com") - client_id = string # Okta OAuth client ID - private_key = string # Okta private key for OAuth - key_id = string # (optional) Okta private key ID - github_user_field = string # Okta field containing GitHub username (default: "login") - sync_rules = list(any) # List of sync rules mapping Okta groups to GitHub teams - sync_safety_threshold = number # Safety threshold to prevent mass removal (default: 0.5) - orphaned_user_notifications = bool # Notify about users not in any synced team (default: true) + github_app_config = { org = "acme" } + installer_config = { enabled = true } } ``` -#### Sync Rules - -Sync rules support both simple and pattern-based formats: - -```hcl -# Simple format - direct mapping -sync_rules = [ - { - okta_group = "engineering-team" - github_team = "engineers" - } -] - -# Pattern-based format - for multiple groups -sync_rules = [ - { - name = "sync-engineering-teams" - enabled = true - okta_group_pattern = "^github-eng-.*" - github_team_prefix = "eng-" - strip_prefix = "github-eng-" - sync_members = true - create_team_if_missing = true - } -] -``` +1. Apply, and open the `setup_url` output. +2. Create the app; the installer fills in the webhook URL, and GitHub asks you + to confirm. +3. Install the app on the organization from the success page. -See the -[GitHub Ops App documentation](https://github.com/cruxstack/github-ops-app/blob/main/docs/okta-setup.md#step-10-configure-sync-rules) -for detailed sync rule configuration. +The installer saves the app's credentials as SSM parameters under the +`ssm_parameter_prefix` output, and the next invocation reads them. Once an app +is registered, the function refuses new installer codes. Afterwards, set +`installer_config.enabled = false`; the function keeps reading the saved +credentials. Terraform doesn't manage the parameters the installer creates, so +`terraform destroy` leaves them. -### Lambda Config +### Secrets -```hcl -lambda_config = { - memory_size = number # Memory in MB (default: 256) - timeout = number # Timeout in seconds (default: 30) - runtime = string # Lambda runtime (default: "provided.al2023") - architecture = string # CPU architecture (default: "x86_64") - reserved_concurrent_executions = number # Reserved concurrency (default: -1, unreserved) -} -``` +Each secret the module passes to the function (the GitHub App private key, +webhook secret, Okta private key, Slack token, and admin token) is a +SecureString parameter under `ssm_config.parameter_prefix`, and the function +gets the parameter's ARN, so the value doesn't appear in the function's +configuration. The function's role can read only those parameters. Set +`ssm_config.store_secrets = false` to pass them as plain environment variables +instead. -### Slack Config +To keep a secret out of Terraform, set it to the ARN of a parameter you +manage. The module passes the ARN as is and lets the function read that +parameter: ```hcl -slack_config = { - enabled = bool # Enable Slack integration (default: false) - token = string # Slack bot token (xoxb-...) - channel = string # Default Slack channel ID for notifications - channel_pr_bypass = string # (optional) Channel for PR bypass alerts - channel_okta_sync = string # (optional) Channel for sync reports - channel_orphaned_users = string # (optional) Channel for orphaned user alerts -} + slack_config = { + enabled = true + token = "arn:aws:ssm:us-east-1:111122223333:parameter/slack/token" + channel = "C01234ABCDE" + } ``` -Per-notification channels are optional and fall back to the default `channel` if -not specified. +## Inputs -### Admin Token Config +In addition to the variables documented below, this module includes several +other optional variables (e.g., `name`, `tags`, etc.) provided by the +`cloudposse/label/null` module. Please refer to its [documentation](https://registry.terraform.io/modules/cloudposse/label/null/latest) +for more details on these variables. + +| Name | Description | Type | Default | Required | +| ------------------------------ | ------------------------------------------------------------------------------------------------------- | -------------- | --------------------------------------------------- | -------- | +| `github_app_config` | GitHub App: `org`, `app_id`, `private_key`, `installation_id`, `webhook_secret`, `base_url` (sensitive) | `object` | n/a | yes | +| `app_version` | git tag, branch, or commit of github-ops-app to build; `latest` builds its default branch | `string` | `"v1.0.0"` | no | +| `app_repo` | git URL of the github-ops-app source | `string` | `"https://github.com/cruxstack/github-ops-app.git"` | no | +| `app_force_rebuild_id` | change to force a rebuild of the function's code | `string` | `""` | no | +| `log_level` | `debug`, `info`, `warn`, or `error` | `string` | `"info"` | no | +| `installer_config` | `enabled`: serve the GitHub App installer at `/setup` | `object` | `{}` | no | +| `ssm_config` | `store_secrets`, `parameter_prefix` (default `//`), `kms_key_arn` | `object` | `{}` | no | +| `ssm_parameter_arns` | other parameter ARNs the function may read | `list(string)` | `[]` | no | +| `admin_token_config` | `enabled`, `token` (generated if empty, at least 16 characters) (sensitive) | `object` | `{}` | no | +| `okta_config` | Okta group sync; see [below](#okta-sync-rules) (sensitive) | `object` | `{}` | no | +| `okta_sync_schedule` | `enabled`, `schedule_expression` (default `rate(1 hour)`) | `object` | `{}` | no | +| `pr_compliance_config` | `enabled`, `monitored_branches` (names or globs), `slack_footer_note` | `object` | `{}` | no | +| `security_alerts_config` | `enabled`, `min_age_days` (default `30`), `min_severity` (default `high`) | `object` | `{}` | no | +| `security_alerts_schedule` | `enabled`, `schedule_expression` (default `rate(24 hours)`) | `object` | `{}` | no | +| `slack_config` | `enabled`, `token`, `channel`, and `channel_*` overrides per report (sensitive) | `object` | `{}` | no | +| `api_gateway_config` | `enabled`, `stage_name` (default `v1`), `throttling_burst_limit`, `throttling_rate_limit` | `object` | `{}` | no | +| `lambda_config` | `memory_size` (256), `timeout` (600), `architecture` (`arm64`), `reserved_concurrent_executions` | `object` | `{}` | no | +| `lambda_log_retention_days` | days to keep the function's and API's logs | `number` | `30` | no | +| `lambda_environment_variables` | more environment variables for the function; they override the module's | `map(string)` | `{}` | no | + +### Okta Sync Rules + +`okta_config` takes the domain, `client_id`, `private_key`, optional +`private_key_id` and `scopes`, `github_user_field` (default `githubUsername`), +`sync_safety_threshold` (default `0.5`), `orphaned_user_notifications` +(default `true`), and `sync_rules`. Each rule sets exactly one of +`okta_group_name` and `okta_group_pattern`: ```hcl -admin_token_config = { - enabled = bool # Enable admin token protection (default: true) - token = string # (optional) Admin token - auto-generated if not provided -} +sync_rules = [ + { + name = "platform" + okta_group_name = "Platform Engineers" + github_team_name = "platform" + create_team_if_missing = true + }, + { + name = "engineering-teams" + okta_group_pattern = "^github-eng-" + strip_prefix = "github-eng-" + github_team_prefix = "eng-" + team_privacy = "secret" + }, +] ``` -When enabled, requests to `/server/*` and `/scheduled/*` endpoints require an -`Authorization: Bearer ` header. If no token is provided, a secure -32-character token is automatically generated and available via the -`admin_token` output. +The other fields are `enabled`, `sync_members`, and `create_team_if_missing`, +which defaults to `false`: a missing team is reported instead of created. See +[github-ops-app's sync rules](https://github.com/cruxstack/github-ops-app#okta-sync-rules) +for what each does. ## Outputs -| Name | Description | -| ------------------------------- | -------------------------------------------------------- | -| `lambda_function_arn` | ARN of the GitHub Ops App Lambda function | -| `lambda_function_name` | Name of the GitHub Ops App Lambda function | -| `lambda_function_qualified_arn` | Qualified ARN of the Lambda function | -| `lambda_function_invoke_arn` | Invoke ARN of the Lambda function | -| `lambda_role_arn` | ARN of the IAM role used by the Lambda function | -| `lambda_role_name` | Name of the IAM role used by the Lambda function | -| `cloudwatch_log_group_name` | Name of the CloudWatch Log Group | -| `cloudwatch_log_group_arn` | ARN of the CloudWatch Log Group | -| `api_gateway_id` | ID of the API Gateway HTTP API | -| `api_gateway_arn` | ARN of the API Gateway HTTP API | -| `api_gateway_endpoint` | Base URL of the API Gateway | -| `api_gateway_execution_arn` | Execution ARN of the API Gateway | -| `webhook_url` | Full webhook URL to configure in GitHub App settings | -| `webhook_secret` | Webhook secret to configure in GitHub App | -| `admin_token` | Admin token for `/server/*` and `/scheduled/*` endpoints | -| `eventbridge_rule_arn` | ARN of the EventBridge rule for scheduled Okta sync | -| `eventbridge_rule_name` | Name of the EventBridge rule | +| Name | Description | +| --------------------------------------- | ---------------------------------------------------------------------------- | +| `webhook_url` | webhook URL to set in the GitHub App's settings | +| `webhook_secret` | webhook secret to set in the GitHub App (sensitive); null with the installer | +| `setup_url` | URL of the GitHub App installer, when enabled | +| `public_url` | base URL of the app's routes, including the stage | +| `admin_token` | token for the `/server/*` and `/scheduled/*` endpoints (sensitive) | +| `ssm_parameter_prefix` | SSM path of the secrets and the installer's credentials | +| `ssm_parameter_arns` | ARNs of the secret parameters the module manages, by name | +| `lambda_function_arn` | ARN of the Lambda function | +| `lambda_function_name` | name of the Lambda function | +| `lambda_function_qualified_arn` | qualified ARN of the Lambda function | +| `lambda_function_invoke_arn` | invoke ARN of the Lambda function | +| `lambda_role_arn` | ARN of the function's IAM role | +| `lambda_role_name` | name of the function's IAM role | +| `cloudwatch_log_group_name` | name of the function's log group | +| `cloudwatch_log_group_arn` | ARN of the function's log group | +| `api_gateway_id` | ID of the HTTP API | +| `api_gateway_arn` | ARN of the HTTP API | +| `api_gateway_endpoint` | base URL of the HTTP API, without the stage | +| `api_gateway_execution_arn` | execution ARN of the HTTP API | +| `eventbridge_rule_arn` | ARN of the Okta sync schedule, if enabled | +| `eventbridge_rule_name` | name of the Okta sync schedule, if enabled | +| `eventbridge_security_alerts_rule_arn` | ARN of the security alerts schedule, if enabled | +| `eventbridge_security_alerts_rule_name` | name of the security alerts schedule, if enabled | ## Architecture @@ -235,21 +231,21 @@ When enabled, requests to `/server/*` and `/scheduled/*` endpoints require an | GitHub App | +--------+---------+ | - | Webhooks + | webhooks v -+------------------+ +----------+---------+ -| EventBridge | | API Gateway | -| (Scheduled) | | HTTP API | -+--------+---------+ +----------+---------+ - | | - | /scheduled/okta-sync | /webhooks - | | - +----------------+------------------+ - | - v - +----------+---------+ - | Lambda | - | GitHub Ops App | ++------------------+ +----------+---------+ +-------------+ +| EventBridge | | API Gateway | | SSM | +| schedules | | HTTP API | | secrets | ++--------+---------+ +----------+---------+ +------+------+ + | | | + | {"action": "okta-sync"} | /webhooks | + | | | + +----------------+------------------+ | + | | + v | + +----------+---------+ | + | Lambda | <-----------------------------+ + | github-ops-app | +----------+---------+ | +---------------+---------------+ @@ -261,12 +257,44 @@ When enabled, requests to `/server/*` and `/scheduled/*` endpoints require an +-------------+ +-------------+ +-------------+ ``` -## Requirements - -| Name | Version | -| --------- | ------- | -| terraform | >= 1.3 | -| aws | >= 5.0 | +## Upgrading to v1 + +v1 runs [github-ops-app v1](https://github.com/cruxstack/github-ops-app/blob/main/docs/upgrading-to-v1.md), +which validates its configuration at startup, requires signed webhooks, and +needs two more GitHub App permissions; read its upgrade guide too. + +| v0 | v1 | +| ------------------------------------------------- | --------------------------------------------------------------- | +| `bot_version` (default `latest`) | `app_version` (pinned to `v1.0.0`) | +| `bot_repo` | `app_repo` | +| `bot_force_rebuild_id` | `app_force_rebuild_id` | +| `okta_config.key_id` | `okta_config.private_key_id`; the old one never reached the app | +| `okta_config.github_user_field` (default `login`) | default `githubUsername`, as in the app | +| `okta_config.sync_rules` (`list(any)`) | typed rules; unknown fields are an error | +| `api_gateway_config.cors_allow_origins` | removed; webhooks don't need CORS | +| `lambda_config.runtime` | removed; always `provided.al2023` | + +Other changes: + +- **The function is built with the buildkit provider** instead of the + docker-based artifact packager, and redeploys when its code changes. The + packager's resources are forgotten from the state, not destroyed. +- **Secrets move to SSM** SecureString parameters. Set + `ssm_config.store_secrets = false` to keep plain environment variables. +- **The function runs on `arm64`** by default, built for the architecture you + choose; v0 always built an amd64 binary. Set + `lambda_config.architecture = "x86_64"` to keep the v0 architecture. +- **The timeout is 600 seconds** by default, so a full Okta sync can finish. +- `github_app_config.installation_id` is optional; the app looks it up. +- `github_app_config.base_url` supports GitHub Enterprise Server. +- `create_team_if_missing` defaults to `false` in the app; set it on rules that + should create teams. +- With `stage_name = "$default"`, routes have no base path; v0 set a base path + of `/$default`, which broke every route. +- The PR bypass footer note is sent without a PR bypass channel. +- A schedule without its feature enabled is an error. +- API Gateway routes are throttled (burst 100, 50 requests per second). +- The module is checked with `terraform test` and tflint in CI. ## License 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":