Block + ref components on public GitHub repos
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ variable "ruleset" {
|
||||
})
|
||||
})
|
||||
rules = object({
|
||||
creation = optional(bool, false)
|
||||
deletion = bool
|
||||
non_fast_forward = bool
|
||||
pull_request = object({
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user