Merge pull request 'Manage IUCLabs GitHub org settings' (#121) from benvin/github-iuclabs-org into main
ci/woodpecker/push/apply Pipeline was successful
ci/woodpecker/push/apply Pipeline was successful
Reviewed-on: #121
This commit was merged in pull request #121.
This commit is contained in:
@@ -23,7 +23,8 @@ define github_env
|
||||
GITHUB_APP_ID=$$(vault kv get -field=app_id $(GITHUB_VAULT_PATH)) && \
|
||||
GITHUB_APP_INSTALLATION_ID=$$(vault kv get -field=installation_id $(GITHUB_VAULT_PATH)) && \
|
||||
GITHUB_APP_PEM_FILE=$$(vault kv get -field=pem $(GITHUB_VAULT_PATH)) && \
|
||||
export GITHUB_APP_ID GITHUB_APP_INSTALLATION_ID GITHUB_APP_PEM_FILE
|
||||
TF_VAR_billing_email=$$(vault kv get -field=billing_email $(GITHUB_VAULT_PATH)) && \
|
||||
export GITHUB_APP_ID GITHUB_APP_INSTALLATION_ID GITHUB_APP_PEM_FILE TF_VAR_billing_email
|
||||
endef
|
||||
|
||||
# Runs $(1) in $$dir for the gitea units, then the github units with App creds in a subshell; either failing fails the target without skipping the other.
|
||||
|
||||
@@ -6,7 +6,9 @@ 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.
|
||||
|
||||
`modules/github_org` validates config and calls `modules/team` (team + memberships) and `modules/repository` (repo, default branch, collaborators, CODEOWNERS, rulesets via `modules/ruleset`).
|
||||
`modules/github_org` validates config, manages org-level settings, and calls `modules/team` (team + memberships) and `modules/repository` (repo, default branch, collaborators, CODEOWNERS, rulesets via `modules/ruleset`).
|
||||
|
||||
`config.yaml` (optional) manages org settings: profile (`name`, `description`, `blog`, `location`, `email`, `twitter_username`, `company`), `default_repository_permission` (read, or none), `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. `billing_email` comes from the `billing_email` field of the same Vault path (`TF_VAR_billing_email`). These resources are `prevent_destroy`, so deleting `config.yaml` fails the plan; to stop managing them, add `removed { from = <resource>; lifecycle { destroy = false } }` blocks to `modules/github_org`.
|
||||
|
||||
`team/<name>.yaml`:
|
||||
|
||||
|
||||
+5
-2
@@ -79,11 +79,14 @@ locals {
|
||||
}
|
||||
}
|
||||
|
||||
github = {
|
||||
github = merge({
|
||||
for kind in ["team", "ruleset", "repository"] : kind => {
|
||||
for file_path, content in local.github_configs :
|
||||
"${content.organisation}/${content.name}" => content
|
||||
if content.kind == kind
|
||||
}
|
||||
}
|
||||
}, {
|
||||
# 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" }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
name: IUCLabs
|
||||
description: Institute of Unnecessary Complexity
|
||||
default_repository_permission: read
|
||||
members_can_create_repositories: false
|
||||
public_runners: true
|
||||
@@ -28,9 +28,27 @@ provider "github" {
|
||||
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]
|
||||
id = "${local.owner}"
|
||||
}
|
||||
|
||||
import {
|
||||
to = github_actions_runner_group.default[0]
|
||||
id = "1"
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
inputs = {
|
||||
owner = local.owner
|
||||
team = { for k, v in local.github.team : v.name => v if v.organisation == local.owner }
|
||||
ruleset = { for k, v in local.github.ruleset : v.name => v if v.organisation == local.owner }
|
||||
repository = { for k, v in local.github.repository : v.name => v if v.organisation == local.owner }
|
||||
settings = lookup(local.github.settings, local.owner, null)
|
||||
}
|
||||
|
||||
@@ -22,3 +22,68 @@ module "repository" {
|
||||
for e in concat(each.value.maintainers, each.value.codeowners) : trimprefix(e, "@") => module.team[trimprefix(e, "@")].slug if startswith(e, "@")
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
resource "github_organization_settings" "this" {
|
||||
count = var.settings == null ? 0 : 1
|
||||
|
||||
billing_email = var.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
|
||||
members_can_create_internal_repositories = false
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
# Free plan only has the Default runner group; it is imported and updated, never created or deleted.
|
||||
resource "github_actions_runner_group" "default" {
|
||||
count = var.settings == null ? 0 : 1
|
||||
|
||||
name = "Default"
|
||||
visibility = "all"
|
||||
allows_public_repositories = var.settings.public_runners
|
||||
|
||||
lifecycle {
|
||||
prevent_destroy = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ mock_provider "github" {
|
||||
}
|
||||
|
||||
variables {
|
||||
owner = "IUCLabs"
|
||||
owner = "IUCLabs"
|
||||
billing_email = "billing@example.com"
|
||||
team = {
|
||||
maintainers = { name = "maintainers", maintainers = ["alice"], members = ["bob"] }
|
||||
}
|
||||
@@ -329,3 +330,134 @@ run "empty_status_check_contexts_rejected" {
|
||||
|
||||
expect_failures = [var.ruleset]
|
||||
}
|
||||
|
||||
run "org_settings_unmanaged_without_config" {
|
||||
command = plan
|
||||
|
||||
assert {
|
||||
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 = { name = "IUCLabs", description = "Labs" }
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
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", "members_can_create_internal_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, internal repos, 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" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
settings = { public_runners = true }
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
!github_organization_settings.this[0].members_can_create_repositories &&
|
||||
!github_organization_settings.this[0].members_can_create_public_repositories &&
|
||||
!github_organization_settings.this[0].members_can_create_private_repositories &&
|
||||
github_organization_settings.this[0].default_repository_permission == "read"
|
||||
)
|
||||
error_message = "repo creation must be blocked for every visibility, default permission read"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
github_actions_runner_group.default[0].name == "Default" &&
|
||||
github_actions_runner_group.default[0].allows_public_repositories
|
||||
)
|
||||
error_message = "public_runners must open the Default runner group to public repositories"
|
||||
}
|
||||
}
|
||||
|
||||
run "org_settings_allow_repo_creation" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
settings = { members_can_create_repositories = true }
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
github_organization_settings.this[0].members_can_create_public_repositories &&
|
||||
github_organization_settings.this[0].members_can_create_private_repositories &&
|
||||
!github_actions_runner_group.default[0].allows_public_repositories
|
||||
)
|
||||
error_message = "per-visibility creation flags must follow members_can_create_repositories; public_runners defaults off"
|
||||
}
|
||||
}
|
||||
|
||||
run "default_repository_permission_none_accepted" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
settings = { default_repository_permission = "none" }
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = github_organization_settings.this[0].default_repository_permission == "none"
|
||||
error_message = "default_repository_permission none must be accepted"
|
||||
}
|
||||
}
|
||||
|
||||
run "invalid_default_repository_permission_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
settings = { default_repository_permission = "write" }
|
||||
}
|
||||
|
||||
expect_failures = [var.settings]
|
||||
}
|
||||
|
||||
run "settings_without_billing_email_rejected" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
settings = {}
|
||||
billing_email = null
|
||||
}
|
||||
|
||||
expect_failures = [var.billing_email]
|
||||
}
|
||||
|
||||
@@ -114,3 +114,37 @@ variable "repository" {
|
||||
error_message = "Unknown teams referenced: ${join(", ", distinct(flatten([for name, repo in var.repository : [for e in concat(repo.maintainers, repo.codeowners) : "${name}/${e}" if startswith(e, "@") && !contains(keys(var.team), trimprefix(e, "@"))]])))}."
|
||||
}
|
||||
}
|
||||
|
||||
variable "settings" {
|
||||
description = "Organisation settings from config.yaml; null leaves them unmanaged"
|
||||
type = object({
|
||||
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)
|
||||
})
|
||||
default = null
|
||||
|
||||
validation {
|
||||
condition = var.settings == null || contains(["read", "none"], try(var.settings.default_repository_permission, ""))
|
||||
error_message = "default_repository_permission must be read or none."
|
||||
}
|
||||
}
|
||||
|
||||
variable "billing_email" {
|
||||
description = "Organisation billing email, from TF_VAR_billing_email; required when settings is set"
|
||||
type = string
|
||||
default = null
|
||||
sensitive = true
|
||||
|
||||
validation {
|
||||
condition = var.settings == null || var.billing_email != null
|
||||
error_message = "billing_email must be set when settings is set."
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user