diff --git a/README.md b/README.md index a403ca4..54c13b9 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,9 @@ required_checks: [] # generated `required-checks` ruleset (strict) rulesets: [] # custom rulesets from ruleset/ ``` -`ruleset/.yaml` (custom extras; `branch-protection`, `required-checks` and `tag-protection` are reserved): +Public repositories always get generated `ref-names-branch` and `ref-names-tag` rulesets blocking creation of refs with a `+` path component, which would act as a wildcard in Vault policy paths templated from the GitHub OIDC `ref` claim. + +`ruleset/.yaml` (custom extras; `branch-protection`, `required-checks`, `tag-protection`, `ref-names-branch` and `ref-names-tag` are reserved): ```yaml target: branch diff --git a/modules/github_org/modules/repository/main.tf b/modules/github_org/modules/repository/main.tf index f092097..ca2e950 100644 --- a/modules/github_org/modules/repository/main.tf +++ b/modules/github_org/modules/repository/main.tf @@ -57,6 +57,21 @@ locals { } } } : k => v.ruleset if v.enabled } + + # a "+" ref component becomes a wildcard in Vault policy paths templated from the GitHub OIDC ref claim + ref_name_rulesets = var.config.private ? {} : { for t, prefix in { branch = "refs/heads", tag = "refs/tags" } : "ref-names-${t}" => { + name = "ref-names-${t}" + target = t + enforcement = "active" + conditions = { ref_name = { include = ["${prefix}/**/+", "${prefix}/**/+/**/*"], exclude = [] } } + rules = { + creation = true + deletion = false + non_fast_forward = false + pull_request = null + required_status_checks = null + } + } } } resource "github_repository" "this" { @@ -122,7 +137,7 @@ resource "github_repository_file" "codeowners" { module "ruleset" { source = "../ruleset" - for_each = merge(var.rulesets, local.generated_rulesets) + for_each = merge(var.rulesets, local.generated_rulesets, local.ref_name_rulesets) repository = github_repository.this.name ruleset = each.value diff --git a/modules/github_org/modules/ruleset/main.tf b/modules/github_org/modules/ruleset/main.tf index 44448cb..e0775aa 100644 --- a/modules/github_org/modules/ruleset/main.tf +++ b/modules/github_org/modules/ruleset/main.tf @@ -15,6 +15,7 @@ resource "github_repository_ruleset" "this" { } rules { + creation = var.ruleset.rules.creation deletion = var.ruleset.rules.deletion non_fast_forward = var.ruleset.rules.non_fast_forward diff --git a/modules/github_org/modules/ruleset/variables.tf b/modules/github_org/modules/ruleset/variables.tf index a6b1c31..997481d 100644 --- a/modules/github_org/modules/ruleset/variables.tf +++ b/modules/github_org/modules/ruleset/variables.tf @@ -14,6 +14,7 @@ variable "ruleset" { }) }) rules = object({ + creation = optional(bool, false) deletion = bool non_fast_forward = bool pull_request = object({ diff --git a/modules/github_org/tests/github_org.tftest.hcl b/modules/github_org/tests/github_org.tftest.hcl index b1ffc78..d1e0bf4 100644 --- a/modules/github_org/tests/github_org.tftest.hcl +++ b/modules/github_org/tests/github_org.tftest.hcl @@ -34,8 +34,19 @@ run "public_repo_defaults" { } assert { - condition = keys(module.repository[".github"].rulesets) == ["branch-protection", "tag-protection"] && length(module.team["maintainers"].membership) == 2 - error_message = "public repo must get only the generated branch-protection and tag-protection rulesets" + condition = keys(module.repository[".github"].rulesets) == ["branch-protection", "ref-names-branch", "ref-names-tag", "tag-protection"] && length(module.team["maintainers"].membership) == 2 + error_message = "public repo must get only the generated branch-protection, ref-names and tag-protection rulesets" + } + + assert { + condition = alltrue([for t, prefix in { branch = "refs/heads", tag = "refs/tags" } : ( + module.repository[".github"].rulesets["ref-names-${t}"].target == t && + module.repository[".github"].rulesets["ref-names-${t}"].enforcement == "active" && + module.repository[".github"].rulesets["ref-names-${t}"].conditions[0].ref_name[0].include == tolist(["${prefix}/**/+", "${prefix}/**/+/**/*"]) && + module.repository[".github"].rulesets["ref-names-${t}"].rules[0].creation && + !module.repository[".github"].rulesets["ref-names-${t}"].rules[0].deletion + )]) + error_message = "ref-names rulesets must block creating branches and tags with a + path component" } assert { @@ -115,8 +126,8 @@ run "generated_and_custom_rulesets" { } assert { - condition = toset(keys(module.repository["checked"].rulesets)) == toset(["required-checks", "signed"]) - error_message = "required_checks with protections off yields only required-checks plus custom rulesets" + condition = toset(keys(module.repository["checked"].rulesets)) == toset(["required-checks", "ref-names-branch", "ref-names-tag", "signed"]) + error_message = "required_checks with protections off yields only required-checks, ref-names plus custom rulesets" } assert { @@ -152,7 +163,7 @@ run "public_tag_protection_only" { } assert { - condition = keys(module.repository["tags"].rulesets) == ["tag-protection"] + condition = keys(module.repository["tags"].rulesets) == ["ref-names-branch", "ref-names-tag", "tag-protection"] error_message = "tag_protection defaults on for public repos independently of branch_protection" } } @@ -219,6 +230,19 @@ run "reserved_ruleset_name_rejected" { expect_failures = [var.ruleset] } +run "reserved_ref_names_rejected" { + command = plan + + variables { + ruleset = { + ref-names-tag = { name = "ref-names-tag", target = "tag", rules = { deletion = true } } + } + repository = {} + } + + expect_failures = [var.ruleset] +} + run "unknown_ruleset_rejected" { command = plan diff --git a/modules/github_org/variables.tf b/modules/github_org/variables.tf index b487c5c..180d07e 100644 --- a/modules/github_org/variables.tf +++ b/modules/github_org/variables.tf @@ -46,8 +46,8 @@ variable "ruleset" { default = {} validation { - condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks", "tag-protection"])) == 0 - error_message = "Ruleset names branch-protection, required-checks and tag-protection are reserved for generated rulesets." + condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks", "tag-protection", "ref-names-branch", "ref-names-tag"])) == 0 + error_message = "Ruleset names branch-protection, required-checks, tag-protection, ref-names-branch and ref-names-tag are reserved for generated rulesets." } validation {