diff --git a/bootstrap/sso-gitea/README.md b/bootstrap/sso-gitea/README.md new file mode 100644 index 0000000..50d061a --- /dev/null +++ b/bootstrap/sso-gitea/README.md @@ -0,0 +1,23 @@ +# Gitea Authentik SSO + +This stack creates the Authentik side of Gitea OpenID Connect login: + +- OAuth2 provider +- Authentik application +- `gitea-users` and `gitea-admins` access groups +- provider policy bindings + +`./jeannie sso-gitea apply` applies this stack, then configures the existing +Debian-hosted Gitea container with: + +```text +gitea admin auth add-oauth/update-oauth --provider openidConnect +``` + +The Gitea callback URL is: + +```text +https://lab2025.duckdns.org/git/user/oauth2/authentik/callback +``` + +Add real users to `gitea-users` in Authentik before testing login. diff --git a/bootstrap/sso-gitea/main.tf b/bootstrap/sso-gitea/main.tf new file mode 100644 index 0000000..cf8ec1a --- /dev/null +++ b/bootstrap/sso-gitea/main.tf @@ -0,0 +1,95 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + authentik = { + source = "goauthentik/authentik" + version = "= 2026.8.0" + } + random = { + source = "hashicorp/random" + version = "~> 3.7" + } + } +} + +provider "authentik" { + url = var.authentik_url + token = var.authentik_token +} + +data "authentik_flow" "authorization" { + slug = "default-provider-authorization-implicit-consent" +} + +data "authentik_flow" "invalidation" { + slug = "default-provider-invalidation-flow" +} + +data "authentik_property_mapping_provider_scope" "gitea" { + managed_list = [ + "goauthentik.io/providers/oauth2/scope-openid", + "goauthentik.io/providers/oauth2/scope-profile", + "goauthentik.io/providers/oauth2/scope-email", + ] +} + +resource "random_password" "gitea_client_secret" { + length = 48 + special = false +} + +resource "authentik_group" "gitea_users" { + name = "gitea-users" +} + +resource "authentik_group" "gitea_admins" { + name = "gitea-admins" +} + +resource "authentik_provider_oauth2" "gitea" { + name = "Gitea" + client_id = var.gitea_client_id + client_secret = random_password.gitea_client_secret.result + grant_types = ["authorization_code"] + authorization_flow = data.authentik_flow.authorization.id + invalidation_flow = data.authentik_flow.invalidation.id + property_mappings = data.authentik_property_mapping_provider_scope.gitea.ids + + allowed_redirect_uris = [{ + matching_mode = "strict" + url = "${trimsuffix(var.gitea_public_url, "/")}/user/oauth2/${var.gitea_auth_source_name}/callback" + }] +} + +resource "authentik_application" "gitea" { + name = "Gitea" + slug = "gitea" + meta_launch_url = trimsuffix(var.gitea_public_url, "/") + protocol_provider = authentik_provider_oauth2.gitea.id +} + +resource "authentik_policy_binding" "gitea_users" { + target = authentik_application.gitea.uuid + group = authentik_group.gitea_users.id + order = 0 +} + +resource "authentik_policy_binding" "gitea_admins" { + target = authentik_application.gitea.uuid + group = authentik_group.gitea_admins.id + order = 1 +} + +output "gitea_client_id" { + value = var.gitea_client_id +} + +output "gitea_client_secret" { + value = random_password.gitea_client_secret.result + sensitive = true +} + +output "gitea_discovery_url" { + value = "${trimsuffix(var.authentik_url, "/")}/application/o/gitea/.well-known/openid-configuration" +} diff --git a/bootstrap/sso-gitea/variables.tf b/bootstrap/sso-gitea/variables.tf new file mode 100644 index 0000000..31e2974 --- /dev/null +++ b/bootstrap/sso-gitea/variables.tf @@ -0,0 +1,50 @@ +variable "authentik_url" { + description = "Public Authentik URL. Defaults from homelab.yml through Jeannie." + type = string + default = "" + + validation { + condition = can(regex("^https://", var.authentik_url)) + error_message = "authentik_url must be an HTTPS URL." + } +} + +variable "authentik_token" { + description = "Scoped Authentik API token, normally read from the bootstrap secret by Jeannie." + type = string + sensitive = true + default = "" + + validation { + condition = length(var.authentik_token) > 0 + error_message = "authentik_token must be set." + } +} + +variable "gitea_public_url" { + description = "Public Gitea root URL." + type = string + default = "" + + validation { + condition = can(regex("^https://", var.gitea_public_url)) + error_message = "gitea_public_url must be an HTTPS URL." + } +} + +variable "gitea_auth_source_name" { + description = "Gitea OAuth2 authentication source name. This is part of the callback URL." + type = string + default = "authentik" + + validation { + condition = can(regex("^[A-Za-z0-9._-]+$", var.gitea_auth_source_name)) + error_message = "gitea_auth_source_name may only contain letters, digits, dot, underscore, and dash." + } +} + +variable "gitea_client_id" { + description = "OAuth2 client ID used by Gitea." + type = string + default = "gitea" +} diff --git a/jeannie b/jeannie index dcb2796..ed2a692 100755 --- a/jeannie +++ b/jeannie @@ -102,6 +102,7 @@ Operator View And Reports Observability And GitOps grafana-dashboards {list|apply} List or apply repo-managed Grafana dashboards. sso-grafana {plan|apply} Provision Authentik OIDC and enable Grafana SSO. + sso-gitea {plan|apply} Provision Authentik OIDC and enable Gitea SSO. authentik-secret {ensure|file} Generate/check SOPS-encrypted Authentik bootstrap secrets. gitops-status Print focused Argo CD health and sync status. platform Deploy platform stack only. @@ -310,6 +311,9 @@ case "${1:-}" in sso-grafana) grafana_sso "${2:-plan}" ;; + sso-gitea) + gitea_sso "${2:-plan}" + ;; authentik-secret) authentik_secret "${2:-ensure}" ;; diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 7a12dce..c66191e 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -193,6 +193,8 @@ export_homelab_inventory_tf_vars() { export_if_unset TF_VAR_authentik_backend_port "${LAB_AUTHENTIK_HTTP_PORT:-}" export_if_unset TF_VAR_grafana_authentik_url "${LAB_AUTHENTIK_PUBLIC_URL:-}" export_if_unset TF_VAR_grafana_public_url "https://grafana.${LAB_DOMAIN:-}" + export_if_unset TF_VAR_gitea_public_url "${LAB_GITEA_ROOT_URL:-}" + export_if_unset TF_VAR_gitea_auth_source_name "${LAB_GITEA_AUTHENTIK_SOURCE_NAME:-authentik}" export_if_unset TF_VAR_jellyfin_backend_host "${LAB_JELLYFIN_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}" export_if_unset TF_VAR_jellyfin_backend_port "${LAB_JELLYFIN_HTTP_PORT:-}" export_if_unset TF_VAR_gitops_repo_url "${LAB_GITOPS_REPO_URL:-}" @@ -607,6 +609,97 @@ grafana_sso() { esac } +configure_gitea_sso_source() { + local stack="bootstrap/sso-gitea" + local container_name="${LAB_GITEA_CONTAINER_NAME:-homelab-gitea}" + local source_name="${TF_VAR_gitea_auth_source_name:-${LAB_GITEA_AUTHENTIK_SOURCE_NAME:-authentik}}" + local client_id + local client_secret + local discovery_url + local auth_id + local -a auth_base + + client_id="$(tofu -chdir="${REPO_ROOT}/${stack}" output -raw gitea_client_id)" + client_secret="$(tofu -chdir="${REPO_ROOT}/${stack}" output -raw gitea_client_secret)" + discovery_url="$(tofu -chdir="${REPO_ROOT}/${stack}" output -raw gitea_discovery_url)" + + if ! sudo docker inspect "${container_name}" >/dev/null 2>&1; then + echo "Gitea container ${container_name} was not found. Run ./jeannie deploy-gitea first." >&2 + exit 1 + fi + if ! sudo docker ps --format '{{.Names}}' | grep -Fxq "${container_name}"; then + echo "Gitea container ${container_name} is not running. Run ./jeannie deploy-gitea first." >&2 + exit 1 + fi + + auth_base=(sudo docker exec -u git "${container_name}" gitea -c /data/gitea/conf/app.ini admin auth) + auth_id="$("${auth_base[@]}" list | awk -v name="${source_name}" 'NR > 1 && $2 == name { print $1; exit }')" + + if [[ -n "${auth_id}" ]]; then + echo "Updating existing Gitea Authentik auth source ${source_name} (id ${auth_id})..." + "${auth_base[@]}" update-oauth \ + --id "${auth_id}" \ + --name "${source_name}" \ + --provider openidConnect \ + --key "${client_id}" \ + --secret "${client_secret}" \ + --auto-discover-url "${discovery_url}" \ + --scopes "openid email profile" \ + --group-claim-name "groups" \ + --admin-group "gitea-admins" + else + echo "Creating Gitea Authentik auth source ${source_name}..." + "${auth_base[@]}" add-oauth \ + --name "${source_name}" \ + --provider openidConnect \ + --key "${client_id}" \ + --secret "${client_secret}" \ + --auto-discover-url "${discovery_url}" \ + --scopes "openid email profile" \ + --group-claim-name "groups" \ + --admin-group "gitea-admins" + fi +} + +gitea_sso() { + local action="${1:-plan}" + local stack="bootstrap/sso-gitea" + local authentik_url="${LAB_AUTHENTIK_PUBLIC_URL:-}" + local gitea_url="${LAB_GITEA_ROOT_URL:-}" + local bootstrap_token + + require_debian_server "sso-gitea" + + if [[ "${authentik_url}" != https://* ]]; then + echo "LAB_AUTHENTIK_PUBLIC_URL must be set to an HTTPS URL from homelab.yml." >&2 + exit 1 + fi + if [[ "${gitea_url}" != https://* ]]; then + echo "LAB_GITEA_ROOT_URL must be set to an HTTPS URL from homelab.yml." >&2 + exit 1 + fi + + bootstrap_token="$(authentik_bootstrap_api_token)" + export TF_VAR_authentik_token="${bootstrap_token}" + export TF_VAR_authentik_url="${authentik_url}" + export TF_VAR_gitea_public_url="${gitea_url}" + export TF_VAR_gitea_auth_source_name="${LAB_GITEA_AUTHENTIK_SOURCE_NAME:-authentik}" + + case "${action}" in + plan) + run_tofu_plan_stack "${stack}" + ;; + apply) + run_tofu_stack "${stack}" + configure_gitea_sso_source + ;; + *) + echo "Usage: ./jeannie sso-gitea {plan|apply}" >&2 + exit 1 + ;; + esac +} + backup_tofu_state() { local backup_dir="${HOMELAB_TOFU_STATE_BACKUP_DIR:-${HOMELAB_STATE_DIR}/tofu-state-backups}" local timestamp @@ -627,6 +720,8 @@ backup_tofu_state() { bootstrap/platform/terraform.tfstate.backup \ bootstrap/sso-grafana/terraform.tfstate \ bootstrap/sso-grafana/terraform.tfstate.backup \ + bootstrap/sso-gitea/terraform.tfstate \ + bootstrap/sso-gitea/terraform.tfstate.backup \ bootstrap/apps/terraform.tfstate \ bootstrap/apps/terraform.tfstate.backup \ bootstrap/edge/terraform.tfstate \