Merge pull request 'Block + ref components on public GitHub repos' (#120) from benvin/github-ref-name-ruleset into main
ci/woodpecker/push/apply Pipeline was successful
ci/woodpecker/push/apply Pipeline was successful
Reviewed-on: #120
This commit was merged in pull request #120.
This commit is contained in:
@@ -33,7 +33,9 @@ required_checks: [] # generated `required-checks` ruleset (strict)
|
|||||||
rulesets: [] # custom rulesets from ruleset/
|
rulesets: [] # custom rulesets from ruleset/
|
||||||
```
|
```
|
||||||
|
|
||||||
`ruleset/<name>.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/<name>.yaml` (custom extras; `branch-protection`, `required-checks`, `tag-protection`, `ref-names-branch` and `ref-names-tag` are reserved):
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
target: branch
|
target: branch
|
||||||
|
|||||||
@@ -57,6 +57,21 @@ locals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} : k => v.ruleset if v.enabled }
|
} : 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" {
|
resource "github_repository" "this" {
|
||||||
@@ -122,7 +137,7 @@ resource "github_repository_file" "codeowners" {
|
|||||||
module "ruleset" {
|
module "ruleset" {
|
||||||
source = "../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
|
repository = github_repository.this.name
|
||||||
ruleset = each.value
|
ruleset = each.value
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ resource "github_repository_ruleset" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rules {
|
rules {
|
||||||
|
creation = var.ruleset.rules.creation
|
||||||
deletion = var.ruleset.rules.deletion
|
deletion = var.ruleset.rules.deletion
|
||||||
non_fast_forward = var.ruleset.rules.non_fast_forward
|
non_fast_forward = var.ruleset.rules.non_fast_forward
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ variable "ruleset" {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
rules = object({
|
rules = object({
|
||||||
|
creation = optional(bool, false)
|
||||||
deletion = bool
|
deletion = bool
|
||||||
non_fast_forward = bool
|
non_fast_forward = bool
|
||||||
pull_request = object({
|
pull_request = object({
|
||||||
|
|||||||
@@ -34,8 +34,19 @@ run "public_repo_defaults" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = keys(module.repository[".github"].rulesets) == ["branch-protection", "tag-protection"] && length(module.team["maintainers"].membership) == 2
|
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 and tag-protection rulesets"
|
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 {
|
assert {
|
||||||
@@ -115,8 +126,8 @@ run "generated_and_custom_rulesets" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = toset(keys(module.repository["checked"].rulesets)) == toset(["required-checks", "signed"])
|
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 plus custom rulesets"
|
error_message = "required_checks with protections off yields only required-checks, ref-names plus custom rulesets"
|
||||||
}
|
}
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
@@ -152,7 +163,7 @@ run "public_tag_protection_only" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert {
|
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"
|
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]
|
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" {
|
run "unknown_ruleset_rejected" {
|
||||||
command = plan
|
command = plan
|
||||||
|
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ variable "ruleset" {
|
|||||||
default = {}
|
default = {}
|
||||||
|
|
||||||
validation {
|
validation {
|
||||||
condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks", "tag-protection"])) == 0
|
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 and tag-protection are reserved for generated rulesets."
|
error_message = "Ruleset names branch-protection, required-checks, tag-protection, ref-names-branch and ref-names-tag are reserved for generated rulesets."
|
||||||
}
|
}
|
||||||
|
|
||||||
validation {
|
validation {
|
||||||
|
|||||||
Reference in New Issue
Block a user