pin every github org setting and gate org resources on config.yaml
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful

This commit is contained in:
2026-11-10 22:26:48 +11:00
parent be7aa54856
commit 02d39c43e7
7 changed files with 88 additions and 11 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ Manage Gitea resources, teams, repos, and Woodpecker CI via Terraform.
`config/github.com/IUCLabs/` feeds `modules/github_org` via `environments/github/iuclabs`, authenticated as a GitHub App (`kv/service/github/iuclabs/terraform`). Org membership invites are manual.
`config.yaml` (optional) manages org settings: `billing_email`, `default_repository_permission` (read), `members_can_create_repositories` (false), `public_runners` (false; lets public repos use the Default runner group). Every org also gets a read-only, non-approving `GITHUB_TOKEN`.
`config.yaml` (optional) manages org settings: `billing_email`, profile (`name`, `description`, `blog`, `location`, `email`, `twitter_username`, `company`), `default_repository_permission` (read), `members_can_create_repositories` (false), `public_runners` (false; lets public repos use the Default runner group). Every other org setting is pinned in the module (no Pages, no repo projects, legacy security toggles off), and `GITHUB_TOKEN` is read-only and non-approving. Without it, none of this is managed.
`team/<name>.yaml`:
+2 -1
View File
@@ -86,6 +86,7 @@ locals {
if content.kind == kind
}
}, {
settings = { for file_path, content in local.github_configs : content.organisation => content if content.kind == "config.yaml" }
# raw yaml: github_configs overwrites name with the file basename
settings = { for file_path, content in local.github_configs : content.organisation => local.all_configs[file_path] if content.kind == "config.yaml" }
})
}
+2
View File
@@ -1,3 +1,5 @@
name: IUCLabs
description: Institute of Unnecessary Complexity Labs
billing_email: neotheo@gmail.com
default_repository_permission: read
members_can_create_repositories: false
@@ -31,6 +31,7 @@ EOF
generate "imports" {
path = "imports.tf"
if_exists = "overwrite"
disable = lookup(local.github.settings, local.owner, null) == null
contents = <<EOF
import {
to = github_organization_settings.this[0]
+34 -5
View File
@@ -210,23 +210,52 @@ resource "github_repository_ruleset" "this" {
}
resource "github_actions_organization_workflow_permissions" "this" {
count = var.settings == null ? 0 : 1
organization_slug = var.owner
default_workflow_permissions = "read"
can_approve_pull_request_reviews = false
lifecycle {
prevent_destroy = true
}
}
# Every field the provider can send is pinned: unset bools fall back to provider defaults (mostly true) on update.
resource "github_organization_settings" "this" {
count = var.settings == null ? 0 : 1
billing_email = var.settings.billing_email
default_repository_permission = var.settings.default_repository_permission
members_can_create_repositories = var.settings.members_can_create_repositories
# the public/private flags default to true and override the umbrella flag
billing_email = var.settings.billing_email
name = var.settings.name
description = var.settings.description
blog = var.settings.blog
location = var.settings.location
email = var.settings.email
twitter_username = var.settings.twitter_username
company = var.settings.company
default_repository_permission = var.settings.default_repository_permission
members_can_create_repositories = var.settings.members_can_create_repositories
members_can_create_public_repositories = var.settings.members_can_create_repositories
members_can_create_private_repositories = var.settings.members_can_create_repositories
# private pages need Enterprise Cloud; the provider default (true) would be rejected on free
members_can_fork_private_repositories = false
members_can_create_pages = false
members_can_create_public_pages = false
members_can_create_private_pages = false
has_organization_projects = true
has_repository_projects = false
web_commit_signoff_required = false
# legacy per-org toggles, superseded by code security configurations
advanced_security_enabled_for_new_repositories = false
dependabot_alerts_enabled_for_new_repositories = false
dependabot_security_updates_enabled_for_new_repositories = false
dependency_graph_enabled_for_new_repositories = false
secret_scanning_enabled_for_new_repositories = false
secret_scanning_push_protection_enabled_for_new_repositories = false
lifecycle {
prevent_destroy = true
}
+41 -4
View File
@@ -298,17 +298,54 @@ run "org_settings_unmanaged_without_config" {
command = plan
assert {
condition = length(github_organization_settings.this) == 0 && length(github_actions_runner_group.default) == 0
error_message = "org settings and the default runner group must stay unmanaged without config.yaml"
condition = (
length(github_organization_settings.this) == 0 &&
length(github_actions_runner_group.default) == 0 &&
length(github_actions_organization_workflow_permissions.this) == 0
)
error_message = "org settings, workflow permissions and the default runner group must stay unmanaged without config.yaml"
}
}
run "org_settings_pinned" {
command = plan
variables {
settings = { billing_email = "ops@example.com", name = "IUCLabs", description = "Labs" }
}
assert {
condition = (
github_actions_organization_workflow_permissions.this.default_workflow_permissions == "read" &&
!github_actions_organization_workflow_permissions.this.can_approve_pull_request_reviews
github_actions_organization_workflow_permissions.this[0].default_workflow_permissions == "read" &&
!github_actions_organization_workflow_permissions.this[0].can_approve_pull_request_reviews
)
error_message = "GITHUB_TOKEN must default to read-only and never approve PRs"
}
assert {
condition = (
github_organization_settings.this[0].name == "IUCLabs" &&
github_organization_settings.this[0].description == "Labs" &&
github_organization_settings.this[0].blog == null
)
error_message = "profile fields must come from config, unset ones stay null"
}
assert {
condition = alltrue([for f in [
"members_can_create_pages", "members_can_create_public_pages", "members_can_create_private_pages",
"members_can_fork_private_repositories", "has_repository_projects", "web_commit_signoff_required",
"advanced_security_enabled_for_new_repositories", "dependabot_alerts_enabled_for_new_repositories",
"dependabot_security_updates_enabled_for_new_repositories", "dependency_graph_enabled_for_new_repositories",
"secret_scanning_enabled_for_new_repositories", "secret_scanning_push_protection_enabled_for_new_repositories",
] : github_organization_settings.this[0][f] == false])
error_message = "pages, private forks, repo projects, signoff and legacy security toggles must be pinned false"
}
assert {
condition = github_organization_settings.this[0].has_organization_projects == true
error_message = "organization projects must stay enabled"
}
}
run "org_settings_from_config" {
+7
View File
@@ -114,6 +114,13 @@ variable "settings" {
description = "Organisation settings from config.yaml; null leaves them unmanaged"
type = object({
billing_email = string
name = optional(string)
description = optional(string)
blog = optional(string)
location = optional(string)
email = optional(string)
twitter_username = optional(string)
company = optional(string)
default_repository_permission = optional(string, "read")
members_can_create_repositories = optional(bool, false)
public_runners = optional(bool, false)