diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..3ccd18d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,43 @@ +name: Test + +on: + pull_request: + branches: [master] + paths: + - 'terraform/**' + - '.github/workflows/test.yml' + push: + branches: [master] + paths: + - 'terraform/**' + - '.github/workflows/test.yml' + workflow_dispatch: + +defaults: + run: + shell: bash + +concurrency: + group: test-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + terraform: + permissions: + contents: read + name: Terraform + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + persist-credentials: false + - name: Setup terraform + uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0 + with: + terraform_version: 1.12.0 + terraform_wrapper: false + - name: Check terraform formatting + run: terraform fmt -check terraform terraform/tests terraform/tests/fixtures + - name: Run terraform tests + run: terraform/tests/run.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 5549e1d..46bc871 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,8 +21,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - shared action for adding a label to all repositories - issue_label support - new args for repositories and branch protection rules +- plan-time check that repository collaborator and team permissions are GitHub built-in roles (`pull`, `triage`, `moderate`, `push`, `maintain`, `admin`) or custom roles listed in `custom_repository_roles`, with a hint when a GitHub UI role name (`read`, `write`) is used +- Test workflow that runs `terraform fmt -check` and the Terraform tests in `terraform/tests` (with a mocked GitHub provider) on changes to `terraform/` ### Changed +- **BREAKING**: if your YAML grants custom repository roles to collaborators or teams, list them in `custom_repository_roles` in `terraform/locals_override.tf` before upgrading, or plans will fail (the Plan workflow uses the base branch's Terraform files, so merge the `locals_override.tf` change before any PR that grants a new custom role) - workflows: added separate GitHub Actions environments for reading organization state, writing organization state, and pushing repository changes - workflows: pin third-party actions to latest release SHAs and replan from the merged commit before applying - docs: update template repository references from `github-mgmt-template` to `github-as-code` diff --git a/docs/EXAMPLE.yml b/docs/EXAMPLE.yml index b4c6c1e..771e910 100644 --- a/docs/EXAMPLE.yml +++ b/docs/EXAMPLE.yml @@ -44,7 +44,7 @@ repositories: # This group defines repositories (https://registry.terraform.io/p contexts: - Comment strict: true - collaborators: # This group defines repository collaborators (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_collaborator) + collaborators: # This group defines repository collaborators (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_collaborator); keys are API permission names, so use pull for Read and push for Write admin: # This array defines repository collaborators with permission=admin - peter maintain: # This array defines repository collaborators with permission=maintain @@ -52,7 +52,7 @@ repositories: # This group defines repositories (https://registry.terraform.io/p push: [] # This array defines repository collaborators with permission=push triage: [] # This array defines repository collaborators with permission=triage pull: [] # This array defines repository collaborators with permission=pull - teams: # This group defines teams with access to the repository (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository) + teams: # This group defines teams with access to the repository (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository); keys are API permission names, so use pull for Read and push for Write admin: [] # This array defines teams with permission=admin maintain: [] # This array defines teams with permission=maintain push: # This array defines teams with permission=push diff --git a/terraform/locals.tf b/terraform/locals.tf index c05d3aa..6650d24 100644 --- a/terraform/locals.tf +++ b/terraform/locals.tf @@ -4,6 +4,15 @@ locals { advanced_security = false config = yamldecode(file("${path.module}/../github/${local.organization}.yml")) state = jsondecode(file("${path.module}/${local.organization}.tfstate.json")) + # Custom repository roles already created in the GitHub organization that collaborators and teams can be granted, in addition to GitHub's built-in roles. + custom_repository_roles = [] + # GitHub's built-in repository roles by API name (Read is pull, Write is push), followed by the custom ones. + repository_permissions = concat(["pull", "triage", "moderate", "push", "maintain", "admin"], local.custom_repository_roles) + # Role names from the GitHub UI that people often write instead of the API name. + repository_permission_hints = { + "read" = "pull" + "write" = "push" + } ignore = { "repositories" = [] "teams" = [] diff --git a/terraform/resources.tf b/terraform/resources.tf index 73a03cb..63520cf 100644 --- a/terraform/resources.tf +++ b/terraform/resources.tf @@ -112,6 +112,12 @@ resource "github_repository_collaborator" "this" { lifecycle { ignore_changes = [] + + # var.resources is only set by the sync script's terraform import, which has no YAML permission to check. + precondition { + condition = var.resources != null || contains(local.repository_permissions, each.value.permission) + error_message = "repositories.${each.value.repository}.collaborators.${each.value.permission} in github/${local.organization}.yml: \"${each.value.permission}\" (granted to collaborator \"${try(each.value.username, each.key)}\") is not a repository permission this configuration allows.${contains(keys(local.repository_permission_hints), each.value.permission) ? " Use \"${local.repository_permission_hints[each.value.permission]}\" instead." : ""} Valid permissions: ${join(", ", local.repository_permissions)}. To use a custom repository role that exists in GitHub, add it to local.custom_repository_roles in terraform/locals_override.tf." + } } } @@ -188,6 +194,12 @@ resource "github_team_repository" "this" { lifecycle { ignore_changes = [] + + # var.resources is only set by the sync script's terraform import, which has no YAML permission to check. + precondition { + condition = var.resources != null || contains(local.repository_permissions, each.value.permission) + error_message = "repositories.${each.value.repository}.teams.${each.value.permission} in github/${local.organization}.yml: \"${each.value.permission}\" (granted to team \"${try(each.value.team, each.value.team_id)}\") is not a repository permission this configuration allows.${contains(keys(local.repository_permission_hints), each.value.permission) ? " Use \"${local.repository_permission_hints[each.value.permission]}\" instead." : ""} Valid permissions: ${join(", ", local.repository_permissions)}. To use a custom repository role that exists in GitHub, add it to local.custom_repository_roles in terraform/locals_override.tf." + } } } diff --git a/terraform/tests/fixtures/repository_permissions_custom_role.yml b/terraform/tests/fixtures/repository_permissions_custom_role.yml new file mode 100644 index 0000000..611ad5e --- /dev/null +++ b/terraform/tests/fixtures/repository_permissions_custom_role.yml @@ -0,0 +1,8 @@ +repositories: + demo: + collaborators: + security-auditor: [alice] + teams: + security-auditor: [auditors] +teams: + auditors: {} diff --git a/terraform/tests/fixtures/repository_permissions_custom_role_override.tf b/terraform/tests/fixtures/repository_permissions_custom_role_override.tf new file mode 100644 index 0000000..f16f701 --- /dev/null +++ b/terraform/tests/fixtures/repository_permissions_custom_role_override.tf @@ -0,0 +1,3 @@ +locals { + custom_repository_roles = ["security-auditor"] +} diff --git a/terraform/tests/fixtures/repository_permissions_ui_names.yml b/terraform/tests/fixtures/repository_permissions_ui_names.yml new file mode 100644 index 0000000..706395a --- /dev/null +++ b/terraform/tests/fixtures/repository_permissions_ui_names.yml @@ -0,0 +1,8 @@ +repositories: + demo: + collaborators: + read: [alice] + teams: + write: [developers] +teams: + developers: {} diff --git a/terraform/tests/fixtures/repository_permissions_undeclared_custom_role.yml b/terraform/tests/fixtures/repository_permissions_undeclared_custom_role.yml new file mode 100644 index 0000000..611ad5e --- /dev/null +++ b/terraform/tests/fixtures/repository_permissions_undeclared_custom_role.yml @@ -0,0 +1,8 @@ +repositories: + demo: + collaborators: + security-auditor: [alice] + teams: + security-auditor: [auditors] +teams: + auditors: {} diff --git a/terraform/tests/fixtures/repository_permissions_valid.state.json b/terraform/tests/fixtures/repository_permissions_valid.state.json new file mode 100644 index 0000000..7773409 --- /dev/null +++ b/terraform/tests/fixtures/repository_permissions_valid.state.json @@ -0,0 +1,32 @@ +{ + "values": { + "root_module": { + "resources": [ + { + "address": "github_repository.this[\"old\"]", + "mode": "managed", + "type": "github_repository", + "name": "this", + "index": "old", + "values": {"name": "old", "archived": true} + }, + { + "address": "github_repository_collaborator.this[\"old:grace\"]", + "mode": "managed", + "type": "github_repository_collaborator", + "name": "this", + "index": "old:grace", + "values": {"repository": "old", "username": "grace", "permission": "push"} + }, + { + "address": "github_team_repository.this[\"developers:old\"]", + "mode": "managed", + "type": "github_team_repository", + "name": "this", + "index": "developers:old", + "values": {"repository": "old", "team_id": "123", "permission": "push", "etag": ""} + } + ] + } + } +} diff --git a/terraform/tests/fixtures/repository_permissions_valid.yml b/terraform/tests/fixtures/repository_permissions_valid.yml new file mode 100644 index 0000000..2566bbb --- /dev/null +++ b/terraform/tests/fixtures/repository_permissions_valid.yml @@ -0,0 +1,25 @@ +repositories: + active: + collaborators: + admin: [alice] + maintain: [bob] + moderate: [carol] + push: [dave] + triage: [erin] + pull: [frank] + teams: + admin: [admins] + maintain: [maintainers] + moderate: [moderators] + push: [developers] + triage: [triagers] + pull: [readers] + old: + archived: true +teams: + admins: {} + maintainers: {} + moderators: {} + developers: {} + triagers: {} + readers: {} diff --git a/terraform/tests/repository_permissions_custom_role.tftest.hcl b/terraform/tests/repository_permissions_custom_role.tftest.hcl new file mode 100644 index 0000000..b4379e9 --- /dev/null +++ b/terraform/tests/repository_permissions_custom_role.tftest.hcl @@ -0,0 +1,7 @@ +# A custom repository role listed in local.custom_repository_roles plans cleanly. + +mock_provider "github" {} + +run "accepts_declared_custom_role" { + command = plan +} diff --git a/terraform/tests/repository_permissions_ui_names.tftest.hcl b/terraform/tests/repository_permissions_ui_names.tftest.hcl new file mode 100644 index 0000000..1871633 --- /dev/null +++ b/terraform/tests/repository_permissions_ui_names.tftest.hcl @@ -0,0 +1,12 @@ +# GitHub UI role names (read, write) are not API permissions, so plan fails. + +mock_provider "github" {} + +run "rejects_ui_role_names" { + command = plan + + expect_failures = [ + github_repository_collaborator.this, + github_team_repository.this, + ] +} diff --git a/terraform/tests/repository_permissions_undeclared_custom_role.tftest.hcl b/terraform/tests/repository_permissions_undeclared_custom_role.tftest.hcl new file mode 100644 index 0000000..3dd368d --- /dev/null +++ b/terraform/tests/repository_permissions_undeclared_custom_role.tftest.hcl @@ -0,0 +1,12 @@ +# A custom repository role missing from local.custom_repository_roles fails plan. + +mock_provider "github" {} + +run "rejects_undeclared_custom_role" { + command = plan + + expect_failures = [ + github_repository_collaborator.this, + github_team_repository.this, + ] +} diff --git a/terraform/tests/repository_permissions_valid.tftest.hcl b/terraform/tests/repository_permissions_valid.tftest.hcl new file mode 100644 index 0000000..4f838df --- /dev/null +++ b/terraform/tests/repository_permissions_valid.tftest.hcl @@ -0,0 +1,18 @@ +# Built-in permissions in the YAML, plus grants on an archived repository that +# come from Terraform state (which has team_id but no team name), plan cleanly. + +mock_provider "github" {} + +run "accepts_builtin_permissions" { + command = plan + + assert { + condition = length(github_repository_collaborator.this) == 7 + error_message = "Expected 6 collaborators from the YAML and 1 from the archived repository's state." + } + + assert { + condition = length(github_team_repository.this) == 7 + error_message = "Expected 6 team grants from the YAML and 1 from the archived repository's state." + } +} diff --git a/terraform/tests/run.sh b/terraform/tests/run.sh new file mode 100755 index 0000000..c719fb2 --- /dev/null +++ b/terraform/tests/run.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Runs each tests/.tftest.hcl against a scratch copy of this module whose +# github/.yml comes from tests/fixtures/.yml. A case can also +# provide tests/fixtures/.state.json (state the config reads, such as +# grants on archived repositories) and tests/fixtures/_override.tf +# (copied into the module, such as extra locals). The GitHub provider is +# mocked, so no credentials or network access to GitHub are needed. +set -euo pipefail + +module="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT + +mkdir -p "$work/github" "$work/terraform" +cp "$module"/*.tf "$work/terraform/" +cp -R "$module/tests" "$work/terraform/tests" +cd "$work/terraform" +terraform init -backend=false -input=false -no-color > /dev/null + +status=0 +for test in tests/*.tftest.hcl; do + name="$(basename "$test" .tftest.hcl)" + cp "tests/fixtures/$name.yml" "../github/$name.yml" + if [[ -f "tests/fixtures/$name.state.json" ]]; then + cp "tests/fixtures/$name.state.json" "$name.tfstate.json" + else + echo '{}' > "$name.tfstate.json" + fi + rm -f zz_case_override.tf + if [[ -f "tests/fixtures/${name}_override.tf" ]]; then + cp "tests/fixtures/${name}_override.tf" zz_case_override.tf + fi + TF_WORKSPACE="$name" terraform test -no-color -filter="$test" || status=1 +done +exit "$status"