# cicd-signals / github / t0 "Quick start"
#
# Turns on secret scanning, push protection, and Dependabot alerts for every
# new repository in an organisation, makes the Actions token read-only by
# default, and sends every security alert to your collector as it happens.
#
# Built for a scratch organisation. The settings resource manages every
# organisation setting it covers, so in a real one read the plan first.
#
# Sign in with a token that has admin:org, then change the three values below:
#   export GITHUB_TOKEN=<token>
#   export TF_VAR_webhook_secret=<a long random string your collector also knows>
#   terraform init && terraform apply
#
# Verify: in a new repository, push a new fine-grained personal access token
# with no permissions. Push protection blocks the push. Bypass it, and the
# alert reaches your collector. Revoke the token straight after.

locals {
  organization  = "your-org"                                     # change me
  billing_email = "billing@example.com"                          # change me
  collector_url = "https://collector.example.com/github/webhook" # change me
}

variable "webhook_secret" {
  description = "Shared secret GitHub signs each delivery with, so the collector can reject forgeries. Set it with TF_VAR_webhook_secret. It is stored in state."
  type        = string
  sensitive   = true
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 6.13"
    }
  }
}

provider "github" {
  owner = local.organization
}

# Security defaults for repositories created from now on. Existing ones keep
# their settings; the Standard version covers named existing repositories.
resource "github_organization_settings" "this" {
  billing_email = local.billing_email

  dependency_graph_enabled_for_new_repositories                = true
  dependabot_alerts_enabled_for_new_repositories               = true
  dependabot_security_updates_enabled_for_new_repositories     = true
  advanced_security_enabled_for_new_repositories               = true
  secret_scanning_enabled_for_new_repositories                 = true
  secret_scanning_push_protection_enabled_for_new_repositories = true
}

# A workflow gets a token for the repository it runs in. Read-only by
# default means a compromised step cannot push code or change releases
# unless the workflow asks for more in its own permissions block.
resource "github_actions_organization_workflow_permissions" "this" {
  organization_slug                = local.organization
  default_workflow_permissions     = "read"
  can_approve_pull_request_reviews = false
}

# Every alert and every change to a repository's security settings or
# visibility, delivered as it happens. security_and_analysis fires when
# someone switches a scanner off; repository fires when one is made public.
resource "github_organization_webhook" "security" {
  active = true
  events = [
    "secret_scanning_alert",
    "secret_scanning_alert_location",
    "dependabot_alert",
    "code_scanning_alert",
    "security_and_analysis",
    "repository",
  ]
  configuration {
    url          = local.collector_url
    content_type = "json"
    secret       = var.webhook_secret
    insecure_ssl = false
  }
}

output "webhook_id" {
  description = "Recent deliveries, and whether the collector accepted them, are listed under the organisation's webhook settings."
  value       = github_organization_webhook.security.id
}
