harden IUCLabs org settings, actions token and runner access
This commit is contained in:
@@ -6,6 +6,8 @@ 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`.
|
||||
|
||||
`team/<name>.yaml`:
|
||||
|
||||
```yaml
|
||||
|
||||
+4
-2
@@ -79,11 +79,13 @@ 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
|
||||
}
|
||||
}
|
||||
}, {
|
||||
settings = { for file_path, content in local.github_configs : content.organisation => content if content.kind == "config.yaml" }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
billing_email: neotheo@gmail.com
|
||||
default_repository_permission: read
|
||||
members_can_create_repositories: false
|
||||
public_runners: true
|
||||
@@ -28,9 +28,26 @@ provider "github" {
|
||||
EOF
|
||||
}
|
||||
|
||||
generate "imports" {
|
||||
path = "imports.tf"
|
||||
if_exists = "overwrite"
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -208,3 +208,37 @@ resource "github_repository_ruleset" "this" {
|
||||
|
||||
depends_on = [github_repository_file.codeowners]
|
||||
}
|
||||
|
||||
resource "github_actions_organization_workflow_permissions" "this" {
|
||||
organization_slug = var.owner
|
||||
default_workflow_permissions = "read"
|
||||
can_approve_pull_request_reviews = false
|
||||
}
|
||||
|
||||
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
|
||||
members_can_create_public_repositories = var.settings.members_can_create_repositories
|
||||
members_can_create_private_repositories = var.settings.members_can_create_repositories
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,3 +293,63 @@ 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
|
||||
error_message = "org settings and the default runner group must stay unmanaged without config.yaml"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
github_actions_organization_workflow_permissions.this.default_workflow_permissions == "read" &&
|
||||
!github_actions_organization_workflow_permissions.this.can_approve_pull_request_reviews
|
||||
)
|
||||
error_message = "GITHUB_TOKEN must default to read-only and never approve PRs"
|
||||
}
|
||||
}
|
||||
|
||||
run "org_settings_from_config" {
|
||||
command = plan
|
||||
|
||||
variables {
|
||||
settings = { billing_email = "ops@example.com", 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 = { billing_email = "ops@example.com", 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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,3 +109,14 @@ variable "repository" {
|
||||
error_message = "Unknown rulesets referenced: ${join(", ", distinct(flatten([for name, repo in var.repository : [for rs in repo.rulesets : "${name}/${rs}" if !contains(keys(var.ruleset), rs)]])))}."
|
||||
}
|
||||
}
|
||||
|
||||
variable "settings" {
|
||||
description = "Organisation settings from config.yaml; null leaves them unmanaged"
|
||||
type = object({
|
||||
billing_email = string
|
||||
default_repository_permission = optional(string, "read")
|
||||
members_can_create_repositories = optional(bool, false)
|
||||
public_runners = optional(bool, false)
|
||||
})
|
||||
default = null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user