harden IUCLabs org settings, actions token and runner access
This commit is contained in:
@@ -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