feat: replace Authentik SSO with Dex
Homelab Main / validate (push) Successful in 16s Details
Homelab Main / running-update (push) Successful in 7s Details
Homelab Main / debian-ops (push) Successful in 1m14s Details

This commit is contained in:
juvenal.diaz 2026-09-30 11:15:09 -06:00
parent 10e4e6a64b
commit 41f2b05704
19 changed files with 530 additions and 404 deletions

View File

@ -216,9 +216,9 @@ if ! curl -sS --connect-timeout 10 "http://$heimdall_host:$heimdall_port/" >/dev
echo "Run ./jeannie deploy-heimdall on the Debian host if the service is missing." >&2
fi
if ! curl -sS --connect-timeout 10 "http://$authentik_host:$authentik_port/-/health/live/" >/dev/null; then
echo "Warning: cannot reach Authentik backend http://$authentik_host:$authentik_port/ from the edge host." >&2
echo "Run ./jeannie deploy-authentik on the Debian host before testing the public hostname." >&2
if ! curl -sS --connect-timeout 10 "http://$authentik_host:$authentik_port/healthz" >/dev/null; then
echo "Warning: cannot reach Dex backend http://$authentik_host:$authentik_port/ from the edge host." >&2
echo "Run ./jeannie deploy-dex on the Debian host before testing the public hostname." >&2
fi
if ! curl -sS --connect-timeout 10 "http://$jellyfin_host:$jellyfin_port/" >/dev/null; then

View File

@ -1987,30 +1987,30 @@ resource "helm_release" "prometheus_stack" {
}
grafana = {
nodeSelector = local.prometheus_stack_node_selector
envValueFrom = var.grafana_authentik_sso_enabled ? {
envValueFrom = var.grafana_sso_enabled ? {
GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET = {
secretKeyRef = {
name = var.grafana_authentik_secret_name
name = var.grafana_oauth_secret_name
key = "GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET"
}
}
} : {}
"grafana.ini" = var.grafana_authentik_sso_enabled ? {
"grafana.ini" = var.grafana_sso_enabled ? {
server = {
root_url = "${trimsuffix(var.grafana_public_url, "/")}/"
}
"auth.generic_oauth" = {
enabled = true
name = "Authentik"
name = "Dex"
allow_sign_up = true
auto_login = false
client_id = "grafana"
scopes = "openid profile email"
auth_url = "${trimsuffix(var.grafana_authentik_url, "/")}/application/o/authorize/"
token_url = "${trimsuffix(var.grafana_authentik_url, "/")}/application/o/token/"
api_url = "${trimsuffix(var.grafana_authentik_url, "/")}/application/o/userinfo/"
auth_url = "${trimsuffix(var.grafana_oidc_url, "/")}/auth"
token_url = "${trimsuffix(var.grafana_oidc_url, "/")}/token"
api_url = "${trimsuffix(var.grafana_oidc_url, "/")}/userinfo"
login_attribute_path = "sub"
role_attribute_path = "contains(groups[*], 'grafana-admins') && 'Admin' || 'Viewer'"
role_attribute_path = "'Admin'"
}
} : {}
sidecar = {

View File

@ -257,14 +257,14 @@ variable "prometheus_stack_node_selector" {
}
}
variable "grafana_authentik_sso_enabled" {
variable "grafana_sso_enabled" {
description = "Enable Grafana Generic OAuth when the SSO stack's client-secret exists."
type = bool
default = false
}
variable "grafana_authentik_url" {
description = "Public Authentik URL used by Grafana's OIDC client."
variable "grafana_oidc_url" {
description = "Public OIDC issuer URL used by Grafana's OAuth client."
type = string
default = ""
}
@ -280,9 +280,9 @@ variable "grafana_namespace" {
default = "monitoring"
}
variable "grafana_authentik_secret_name" {
variable "grafana_oauth_secret_name" {
type = string
default = "grafana-authentik-oauth"
default = "grafana-dex-oauth"
}
variable "extra_helm_releases" {

View File

@ -1,13 +1,9 @@
# Gitea Authentik SSO
# Gitea SSO
This stack creates the Authentik side of Gitea OpenID Connect login:
Gitea SSO is now managed by Jeannie through the shared Dex IdP, not through
this directory's former Authentik OpenTofu stack.
- 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
`./jeannie sso-gitea apply` deploys Dex if needed, then configures the existing
Debian-hosted Gitea container with:
```text
@ -17,7 +13,5 @@ gitea admin auth add-oauth/update-oauth --provider openidConnect
The Gitea callback URL is:
```text
https://lab2025.duckdns.org/git/user/oauth2/authentik/callback
https://lab2025.duckdns.org/git/user/oauth2/dex/callback
```
Add real users to `gitea-users` in Authentik before testing login.

View File

@ -1,95 +0,0 @@
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"
}

View File

@ -1,50 +0,0 @@
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"
}

View File

@ -1,24 +1,14 @@
# Grafana SSO
This OpenTofu stack creates the Authentik OIDC provider/application for Grafana,
the `grafana-users` and `grafana-admins` access groups, and the Kubernetes
Secret consumed by the Grafana Helm chart.
Grafana SSO is now managed by Jeannie through the shared Dex IdP, not through
this directory's former Authentik OpenTofu stack.
Run it from the Debian homelab server:
Run from the Debian homelab server:
```bash
./jeannie sso-grafana plan
./jeannie sso-grafana apply
```
Jeannie reads `AUTHENTIK_BOOTSTRAP_TOKEN` from the existing SOPS-encrypted
Authentik secret. Authentik creates this as an API token for `akadmin` on its
first startup, so the same encrypted bootstrap secret can recreate this stack
after an Authentik database destroy/recreate cycle. Add your login account to
`grafana-users`; add it to `grafana-admins` only if it needs Grafana
administrator access.
The OAuth client secret is not committed. It is generated by OpenTofu and
written to the `monitoring/grafana-authentik-oauth` Kubernetes Secret.
OpenTofu state contains this secret, so keep the state directory and its
backups owner-readable only.
The Grafana OAuth client secret comes from the SOPS-encrypted Dex secret file
and is written to the `monitoring/grafana-dex-oauth` Kubernetes Secret.

View File

@ -1,103 +0,0 @@
terraform {
required_version = ">= 1.0"
required_providers {
authentik = {
source = "goauthentik/authentik"
version = "= 2026.8.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.26"
}
random = {
source = "hashicorp/random"
version = "~> 3.7"
}
}
}
provider "authentik" {
url = var.authentik_url
token = var.authentik_token
}
provider "kubernetes" {
config_path = var.kubeconfig_path
}
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" "grafana" {
managed_list = [
"goauthentik.io/providers/oauth2/scope-openid",
"goauthentik.io/providers/oauth2/scope-profile",
"goauthentik.io/providers/oauth2/scope-email",
]
}
resource "random_password" "grafana_client_secret" {
length = 48
special = false
}
resource "authentik_group" "grafana_users" {
name = "grafana-users"
}
resource "authentik_group" "grafana_admins" {
name = "grafana-admins"
}
resource "authentik_provider_oauth2" "grafana" {
name = "Grafana"
client_id = "grafana"
client_secret = random_password.grafana_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.grafana.ids
allowed_redirect_uris = [{
matching_mode = "strict"
url = "${trimsuffix(var.grafana_public_url, "/")}/login/generic_oauth"
}]
}
resource "authentik_application" "grafana" {
name = "Grafana"
slug = "grafana"
meta_launch_url = trimsuffix(var.grafana_public_url, "/")
protocol_provider = authentik_provider_oauth2.grafana.id
}
resource "authentik_policy_binding" "grafana_users" {
target = authentik_application.grafana.uuid
group = authentik_group.grafana_users.id
order = 0
}
resource "authentik_policy_binding" "grafana_admins" {
target = authentik_application.grafana.uuid
group = authentik_group.grafana_admins.id
order = 1
}
resource "kubernetes_secret_v1" "grafana_authentik_oauth" {
metadata {
name = var.grafana_secret_name
namespace = var.grafana_namespace
}
data = {
GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET = random_password.grafana_client_secret.result
}
type = "Opaque"
}

View File

@ -1,46 +0,0 @@
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 supplied as AUTHENTIK_TOKEN."
type = string
sensitive = true
default = ""
validation {
condition = length(var.authentik_token) > 0
error_message = "Set AUTHENTIK_TOKEN to a scoped Authentik API token."
}
}
variable "kubeconfig_path" {
type = string
}
variable "grafana_public_url" {
type = string
default = ""
validation {
condition = can(regex("^https://", var.grafana_public_url))
error_message = "grafana_public_url must be an HTTPS URL."
}
}
variable "grafana_namespace" {
type = string
default = "monitoring"
}
variable "grafana_secret_name" {
type = string
default = "grafana-authentik-oauth"
}

View File

@ -92,6 +92,12 @@ services:
install_dir: /data/homelab-authentik
http_port: 9000
public_url: https://auth.lab2025.duckdns.org/
dex:
host: debian
install_dir: /data/homelab-dex
http_port: 5556
telemetry_port: 5558
public_url: https://auth.lab2025.duckdns.org/
jellyfin:
host: debian
http_port: 8096

30
infra/dex/README.md Normal file
View File

@ -0,0 +1,30 @@
# Dex SSO
Dex is the homelab's lightweight OIDC provider for services that only need a
repeatable login flow.
Jeannie renders the Dex config from the SOPS-encrypted secret file and deploys
the Compose stack:
```sh
./jeannie dex-secret ensure
./jeannie deploy-dex
./jeannie sso apply
```
Public issuer:
```text
https://auth.lab2025.duckdns.org/
```
Managed OIDC clients:
- Grafana: `https://grafana.lab2025.duckdns.org/login/generic_oauth`
- Gitea: `https://lab2025.duckdns.org/git/user/oauth2/dex/callback`
The encrypted Dex secret is expected at:
```text
apps/dex/dex.secret.yaml
```

View File

@ -0,0 +1,12 @@
services:
dex:
image: ${DEX_IMAGE:-ghcr.io/dexidp/dex:v2.45.1-alpine}
container_name: ${DEX_CONTAINER_NAME:-homelab-dex}
restart: unless-stopped
command: ["serve", "/etc/dex/config.yaml"]
ports:
- "${DEX_BIND_IP:-0.0.0.0}:${DEX_HTTP_PORT:-5556}:5556"
- "${DEX_BIND_IP:-0.0.0.0}:${DEX_TELEMETRY_PORT:-5558}:5558"
volumes:
- ./config.yaml:/etc/dex/config.yaml:ro
- ${DEX_DATA_DIR:-/data/homelab-dex/data}:/var/dex

View File

@ -152,6 +152,6 @@
icon: "mdi-cube-outline"
- Other:
- Authentik:
- Dex:
href: "https://auth.lab2025.duckdns.org/"
description: "Homelab identity provider and SSO control plane"
description: "Homelab OIDC provider for Grafana and Gitea SSO"

View File

@ -35,11 +35,11 @@
"class": "Jellyfin"
},
{
"title": "Authentik",
"title": "Dex",
"url": "https://auth.lab2025.duckdns.org/",
"description": "Homelab identity provider and SSO control plane",
"colour": "#fd4b2d",
"class": "Authentik"
"description": "Homelab OIDC provider for Grafana and Gitea SSO",
"colour": "#334155",
"class": "Dex"
},
{
"title": "Matrix",

17
jeannie
View File

@ -53,6 +53,7 @@ Build And Bootstrap
up Deploy the full homelab pipeline.
deploy-gitea Deploy the Debian-hosted Gitea container.
deploy-heimdall Deploy the Debian-hosted Heimdall dashboard.
deploy-dex Deploy the Debian-hosted Dex SSO stack.
deploy-authentik Deploy the Debian-hosted Authentik SSO stack.
deploy-matrix Deploy the Matrix homeserver (Synapse + Element + Postgres).
deploy-omniroute Deploy the OmniRoute LLM routing gateway.
@ -101,8 +102,10 @@ 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.
sso {plan|apply} Deploy Dex and enable Grafana + Gitea SSO.
sso-grafana {plan|apply} Enable Grafana SSO through Dex.
sso-gitea {plan|apply} Enable Gitea SSO through Dex.
dex-secret {ensure|file} Generate/check SOPS-encrypted Dex bootstrap secrets.
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.
@ -314,6 +317,12 @@ case "${1:-}" in
sso-gitea)
gitea_sso "${2:-plan}"
;;
sso)
sso "${2:-plan}"
;;
dex-secret)
dex_secret "${2:-ensure}"
;;
authentik-secret)
authentik_secret "${2:-ensure}"
;;
@ -360,6 +369,10 @@ case "${1:-}" in
record_change_journal "deploy-heimdall" "$@"
deploy_heimdall
;;
deploy-dex)
record_change_journal "deploy-dex" "$@"
deploy_dex
;;
deploy-authentik)
record_change_journal "deploy-authentik" "$@"
deploy_authentik

View File

@ -1397,7 +1397,7 @@ status_report() {
fi
status_compose_stack "gitea" "/data/homelab-gitea"
status_compose_stack "heimdall" "${LAB_HEIMDALL_INSTALL_DIR:-/data/homelab-heimdall}"
status_compose_stack "authentik" "${LAB_AUTHENTIK_INSTALL_DIR:-/data/homelab-authentik}"
status_compose_stack "dex" "${LAB_DEX_INSTALL_DIR:-/data/homelab-dex}"
status_compose_stack "arr-stack" "${REPO_ROOT}/infra/arr-stack"
status_kubernetes
@ -1416,10 +1416,10 @@ status_report() {
status_http "website public" "${LAB_PUBLIC_URL:?LAB_PUBLIC_URL is required from homelab.yml}/"
status_http "gitea public" "${LAB_GITEA_ROOT_URL:-${LAB_PUBLIC_URL:?LAB_PUBLIC_URL is required from homelab.yml}/git/}"
status_http "heimdall public" "${LAB_HEIMDALL_PUBLIC_URL:-https://heimdall.${LAB_DOMAIN:?LAB_DOMAIN is required from homelab.yml}/}"
status_http "authentik public" "${LAB_AUTHENTIK_PUBLIC_URL:-https://auth.${LAB_DOMAIN:?LAB_DOMAIN is required from homelab.yml}/}"
status_http "dex public" "${LAB_DEX_PUBLIC_URL:-https://auth.${LAB_DOMAIN:?LAB_DOMAIN is required from homelab.yml}/}"
status_http "gitea local" "http://127.0.0.1:${LAB_GITEA_HTTP_PORT:-3000}/"
status_http "heimdall local" "http://127.0.0.1:${LAB_HEIMDALL_HTTP_PORT:-8082}/"
status_http "authentik local" "http://127.0.0.1:${LAB_AUTHENTIK_HTTP_PORT:-9000}/-/health/live/"
status_http "dex local" "http://127.0.0.1:${LAB_DEX_HTTP_PORT:-5556}/healthz"
status_http "traefik lb" "http://${LAB_TRAEFIK_LB_IP:?LAB_TRAEFIK_LB_IP is required from homelab.yml}/"
status_http "arr radarr" "http://127.0.0.1:7878/"
status_http "arr sonarr" "http://127.0.0.1:8989/"

View File

@ -52,6 +52,10 @@ mapping = {
"services.authentik.install_dir": "LAB_AUTHENTIK_INSTALL_DIR",
"services.authentik.http_port": "LAB_AUTHENTIK_HTTP_PORT",
"services.authentik.public_url": "LAB_AUTHENTIK_PUBLIC_URL",
"services.dex.install_dir": "LAB_DEX_INSTALL_DIR",
"services.dex.http_port": "LAB_DEX_HTTP_PORT",
"services.dex.telemetry_port": "LAB_DEX_TELEMETRY_PORT",
"services.dex.public_url": "LAB_DEX_PUBLIC_URL",
"services.jellyfin.http_port": "LAB_JELLYFIN_HTTP_PORT",
"services.jellyfin.public_url": "LAB_JELLYFIN_PUBLIC_URL",
"services.local_registry.endpoint": "LAB_REGISTRY_ENDPOINT",
@ -188,8 +192,12 @@ export_homelab_inventory_tf_vars() {
export_if_unset TF_VAR_heimdall_backend_port "${LAB_HEIMDALL_HTTP_PORT:-}"
export_if_unset TF_VAR_homepage_backend_host "${LAB_HOMEPAGE_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
export_if_unset TF_VAR_homepage_backend_port "${LAB_HOMEPAGE_HTTP_PORT:-}"
export_if_unset TF_VAR_authentik_backend_host "${LAB_AUTHENTIK_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
export_if_unset TF_VAR_authentik_backend_port "${LAB_AUTHENTIK_HTTP_PORT:-}"
export_if_unset TF_VAR_authentik_backend_host "${LAB_DEX_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
export_if_unset TF_VAR_authentik_backend_port "${LAB_DEX_HTTP_PORT:-}"
export_if_unset TF_VAR_grafana_oidc_url "${LAB_DEX_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_DEX_SOURCE_NAME:-dex}"
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:-}"

View File

@ -53,6 +53,10 @@ mapping = {
"services.authentik.install_dir": "LAB_AUTHENTIK_INSTALL_DIR",
"services.authentik.http_port": "LAB_AUTHENTIK_HTTP_PORT",
"services.authentik.public_url": "LAB_AUTHENTIK_PUBLIC_URL",
"services.dex.install_dir": "LAB_DEX_INSTALL_DIR",
"services.dex.http_port": "LAB_DEX_HTTP_PORT",
"services.dex.telemetry_port": "LAB_DEX_TELEMETRY_PORT",
"services.dex.public_url": "LAB_DEX_PUBLIC_URL",
"services.jellyfin.http_port": "LAB_JELLYFIN_HTTP_PORT",
"services.jellyfin.public_url": "LAB_JELLYFIN_PUBLIC_URL",
"services.local_registry.endpoint": "LAB_REGISTRY_ENDPOINT",
@ -189,12 +193,12 @@ export_homelab_inventory_tf_vars() {
export_if_unset TF_VAR_heimdall_backend_port "${LAB_HEIMDALL_HTTP_PORT:-}"
export_if_unset TF_VAR_homepage_backend_host "${LAB_HOMEPAGE_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
export_if_unset TF_VAR_homepage_backend_port "${LAB_HOMEPAGE_HTTP_PORT:-}"
export_if_unset TF_VAR_authentik_backend_host "${LAB_AUTHENTIK_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
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_authentik_backend_host "${LAB_DEX_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
export_if_unset TF_VAR_authentik_backend_port "${LAB_DEX_HTTP_PORT:-}"
export_if_unset TF_VAR_grafana_oidc_url "${LAB_DEX_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_gitea_auth_source_name "${LAB_GITEA_DEX_SOURCE_NAME:-dex}"
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:-}"
@ -205,13 +209,13 @@ export_homelab_inventory_tf_vars() {
configure_platform_grafana_sso() {
local namespace="${TF_VAR_grafana_namespace:-monitoring}"
local secret_name="${TF_VAR_grafana_authentik_secret_name:-grafana-authentik-oauth}"
local secret_name="${TF_VAR_grafana_oauth_secret_name:-grafana-dex-oauth}"
if command -v kubectl >/dev/null 2>&1 &&
kubectl --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" get secret "${secret_name}" >/dev/null 2>&1; then
export TF_VAR_grafana_authentik_sso_enabled=true
export TF_VAR_grafana_sso_enabled=true
else
export TF_VAR_grafana_authentik_sso_enabled=false
export TF_VAR_grafana_sso_enabled=false
fi
}
@ -574,32 +578,209 @@ run_tofu_plan_stack() {
tofu -chdir="${REPO_ROOT}/${stack}" plan "${plan_args[@]}"
}
grafana_sso() {
local action="${1:-plan}"
local stack="bootstrap/sso-grafana"
local bootstrap_token
local authentik_url="${LAB_AUTHENTIK_PUBLIC_URL:-}"
dex_public_url() {
printf '%s\n' "${LAB_DEX_PUBLIC_URL:-https://auth.${LAB_DOMAIN:-lab2025.duckdns.org}/}"
}
dex_bcrypt_hash() {
local image="$1"
local password="$2"
local output
output="$(sudo docker run --rm "${image}" hash-password "${password}")"
printf '%s\n' "${output}" | grep -Eo '\$2[aby]\$[0-9]{2}\$[^[:space:]]+' | tail -n 1
}
render_dex_config() {
local public_url="${1%/}"
local grafana_public_url="${2%/}"
local gitea_public_url="${3%/}"
local gitea_source_name="$4"
local grafana_client_secret="$5"
local gitea_client_secret="$6"
local user_email="$7"
local user_name="$8"
local user_id="$9"
local user_password_hash="${10}"
cat <<CONFIG_EOF
issuer: ${public_url}
storage:
type: sqlite3
config:
file: /var/dex/dex.db
web:
http: 0.0.0.0:5556
telemetry:
http: 0.0.0.0:5558
oauth2:
skipApprovalScreen: true
responseTypes:
- code
enablePasswordDB: true
staticClients:
- id: grafana
name: Grafana
secret: "${grafana_client_secret}"
redirectURIs:
- ${grafana_public_url}/login/generic_oauth
- id: gitea
name: Gitea
secret: "${gitea_client_secret}"
redirectURIs:
- ${gitea_public_url}/user/oauth2/${gitea_source_name}/callback
staticPasswords:
- email: "${user_email}"
username: "${user_name}"
userID: "${user_id}"
hash: "${user_password_hash}"
CONFIG_EOF
}
deploy_dex() {
local mode="${LAB_DEX_DEPLOY:-true}"
local install_dir="${LAB_DEX_INSTALL_DIR:-/data/homelab-dex}"
local data_dir="${LAB_DEX_DATA_DIR:-${install_dir}/data}"
local http_port="${LAB_DEX_HTTP_PORT:-5556}"
local telemetry_port="${LAB_DEX_TELEMETRY_PORT:-5558}"
local bind_ip="${LAB_DEX_BIND_IP:-0.0.0.0}"
local public_url
local image="${DEX_IMAGE:-ghcr.io/dexidp/dex:v2.45.1-alpine}"
local source_dir="${REPO_ROOT}/infra/dex"
local gitea_url="${LAB_GITEA_ROOT_URL:-}"
local grafana_url="https://grafana.${LAB_DOMAIN:-lab2025.duckdns.org}"
local gitea_source_name="${LAB_GITEA_DEX_SOURCE_NAME:-dex}"
local dex_env
local dex_user_password_hash
local elapsed=0
require_debian_server "deploy-dex"
if disabled_value "${mode}"; then
return 0
fi
public_url="$(dex_public_url)"
public_url="${public_url%/}"
gitea_url="${gitea_url%/}"
if [[ "${public_url}" != https://* ]]; then
echo "LAB_DEX_PUBLIC_URL must be an HTTPS URL." >&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
if [[ ! -s "${source_dir}/docker-compose.yml" ]]; then
echo "Missing Dex source files in ${source_dir}" >&2
exit 1
fi
dex_env="$(dex_secret_values)" || exit 1
eval "${dex_env}"
echo "Deploying Dex on the Debian host at ${install_dir}..."
ensure_local_docker_compose "DEX" "${LAB_DEX_INSTALL_DOCKER:-false}"
repair_local_docker_iptables
sudo docker pull "${image}"
dex_user_password_hash="$(dex_bcrypt_hash "${image}" "${DEX_USER_PASSWORD}")"
if [[ -z "${dex_user_password_hash}" ]]; then
echo "Could not generate Dex bcrypt password hash." >&2
exit 1
fi
sudo mkdir -p "${install_dir}" "${data_dir}"
sudo cp "${source_dir}/docker-compose.yml" "${install_dir}/docker-compose.yml"
render_dex_config \
"${public_url}" \
"${grafana_url}" \
"${gitea_url}" \
"${gitea_source_name}" \
"${DEX_GRAFANA_CLIENT_SECRET}" \
"${DEX_GITEA_CLIENT_SECRET}" \
"${DEX_USER_EMAIL}" \
"${DEX_USER_NAME}" \
"${DEX_USER_ID}" \
"${dex_user_password_hash}" |
sudo tee "${install_dir}/config.yaml" >/dev/null
sudo chmod 600 "${install_dir}/config.yaml"
sudo tee "${install_dir}/.env" >/dev/null <<ENV_EOF
DEX_IMAGE=${image}
DEX_CONTAINER_NAME=${DEX_CONTAINER_NAME:-homelab-dex}
DEX_HTTP_PORT=${http_port}
DEX_TELEMETRY_PORT=${telemetry_port}
DEX_BIND_IP=${bind_ip}
DEX_DATA_DIR=${data_dir}
ENV_EOF
sudo chmod 600 "${install_dir}/.env"
(
cd "${install_dir}"
sudo docker compose --env-file "${install_dir}/.env" pull
sudo docker compose --env-file "${install_dir}/.env" up -d --remove-orphans
sudo docker compose --env-file "${install_dir}/.env" ps
)
until curl -fsS --connect-timeout 5 "http://127.0.0.1:${http_port}/healthz" >/dev/null; do
if ((elapsed >= 180)); then
echo "Timed out waiting for Dex on http://127.0.0.1:${http_port}/healthz" >&2
(cd "${install_dir}" && sudo docker compose --env-file "${install_dir}/.env" ps && sudo docker compose --env-file "${install_dir}/.env" logs --tail=160) >&2 || true
exit 1
fi
sleep 5
elapsed=$((elapsed + 5))
done
echo "Dex is reachable at http://127.0.0.1:${http_port}/ and ${public_url}/."
echo "Initial Dex user: ${DEX_USER_EMAIL}"
}
configure_grafana_dex_secret() {
local namespace="${TF_VAR_grafana_namespace:-monitoring}"
local secret_name="${TF_VAR_grafana_oauth_secret_name:-grafana-dex-oauth}"
local dex_env
require_debian_server "sso-grafana"
if [[ "${authentik_url}" != https://* ]]; then
echo "LAB_AUTHENTIK_PUBLIC_URL must be an HTTPS URL in homelab.yml." >&2
return 1
if ! command -v kubectl >/dev/null 2>&1; then
echo "kubectl is required to configure Grafana SSO." >&2
exit 1
fi
# Authentik creates this API token for akadmin on its first startup. Using
# the SOPS-encrypted bootstrap value makes the identity stack reproducible
# after an Authentik database destroy/recreate cycle.
bootstrap_token="$(authentik_bootstrap_api_token)" || return 1
export TF_VAR_authentik_token="${bootstrap_token}"
export TF_VAR_authentik_url="${authentik_url}"
dex_env="$(dex_secret_values)" || exit 1
eval "${dex_env}"
kubectl --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" create secret generic "${secret_name}" \
--from-literal=GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET="${DEX_GRAFANA_CLIENT_SECRET}" \
--dry-run=client -o yaml |
kubectl --kubeconfig "${KUBECONFIG_PATH}" apply -f -
}
grafana_sso() {
local action="${1:-plan}"
require_debian_server "sso-grafana"
export TF_VAR_grafana_oidc_url="$(dex_public_url)"
case "${action}" in
plan)
run_tofu_plan_stack "${stack}"
configure_platform_grafana_sso
run_tofu_plan_stack "bootstrap/platform"
;;
apply)
run_tofu_stack "${stack}"
deploy_dex
configure_grafana_dex_secret
run_tofu_stack "bootstrap/platform"
;;
*)
@ -610,18 +791,21 @@ grafana_sso() {
}
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 source_name="${TF_VAR_gitea_auth_source_name:-${LAB_GITEA_DEX_SOURCE_NAME:-dex}}"
local client_id="gitea"
local client_secret
local discovery_url
local public_url
local dex_env
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)"
dex_env="$(dex_secret_values)" || exit 1
eval "${dex_env}"
client_secret="${DEX_GITEA_CLIENT_SECRET}"
public_url="$(dex_public_url)"
discovery_url="${public_url%/}/.well-known/openid-configuration"
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
@ -636,7 +820,7 @@ configure_gitea_sso_source() {
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})..."
echo "Updating existing Gitea Dex auth source ${source_name} (id ${auth_id})..."
"${auth_base[@]}" update-oauth \
--id "${auth_id}" \
--name "${source_name}" \
@ -644,53 +828,42 @@ configure_gitea_sso_source() {
--key "${client_id}" \
--secret "${client_secret}" \
--auto-discover-url "${discovery_url}" \
--scopes "openid email profile" \
--group-claim-name "groups" \
--admin-group "gitea-admins"
--scopes "openid email profile"
else
echo "Creating Gitea Authentik auth source ${source_name}..."
echo "Creating Gitea Dex 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"
--scopes "openid email profile"
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}"
export TF_VAR_gitea_auth_source_name="${LAB_GITEA_DEX_SOURCE_NAME:-dex}"
case "${action}" in
plan)
run_tofu_plan_stack "${stack}"
echo "Dex Gitea SSO is configured by ./jeannie sso-gitea apply."
echo "Provider: openidConnect"
echo "Discovery URL: $(dex_public_url | sed 's:/*$::')/.well-known/openid-configuration"
echo "Callback URL: ${gitea_url%/}/user/oauth2/${TF_VAR_gitea_auth_source_name}/callback"
;;
apply)
run_tofu_stack "${stack}"
deploy_dex
configure_gitea_sso_source
;;
*)
@ -700,6 +873,32 @@ gitea_sso() {
esac
}
sso() {
local action="${1:-plan}"
require_debian_server "sso"
export TF_VAR_grafana_oidc_url="$(dex_public_url)"
export TF_VAR_gitea_auth_source_name="${LAB_GITEA_DEX_SOURCE_NAME:-dex}"
case "${action}" in
plan)
grafana_sso plan
gitea_sso plan
;;
apply)
deploy_dex
run_tofu_stack "bootstrap/edge"
configure_grafana_dex_secret
run_tofu_stack "bootstrap/platform"
configure_gitea_sso_source
;;
*)
echo "Usage: ./jeannie sso {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
@ -6032,9 +6231,9 @@ spec = {
"command": [jeannie, "__up-task", "heimdall-deploy"],
},
{
"id": "authentik-deploy",
"title": "Authentik deploy",
"command": [jeannie, "__up-task", "authentik-deploy"],
"id": "dex-deploy",
"title": "Dex deploy",
"command": [jeannie, "__up-task", "dex-deploy"],
},
{
"id": "matrix-deploy",
@ -6108,8 +6307,8 @@ up_task() {
heimdall-deploy)
deploy_heimdall
;;
authentik-deploy)
deploy_authentik
dex-deploy)
deploy_dex
;;
matrix-deploy)
deploy_matrix

View File

@ -191,6 +191,174 @@ security_attack_path() {
"${REPO_ROOT}/scripts/security-attack-path" "${@:2}"
}
dex_secret_file() {
printf '%s\n' "${HOMELAB_DEX_SECRET_FILE:-${REPO_ROOT}/apps/dex/dex.secret.yaml}"
}
generate_dex_secret_value() {
if command -v openssl >/dev/null 2>&1; then
openssl rand -base64 48 | tr -d '\n'
elif command -v python3 >/dev/null 2>&1; then
python3 - <<'PY'
import secrets
import string
alphabet = string.ascii_letters + string.digits + "-_"
print("".join(secrets.choice(alphabet) for _ in range(64)))
PY
else
echo "openssl or python3 is required to generate Dex secrets." >&2
return 1
fi
}
generate_dex_user_id() {
if command -v python3 >/dev/null 2>&1; then
python3 - <<'PY'
import uuid
print(uuid.uuid4())
PY
else
generate_dex_secret_value
fi
}
ensure_dex_secret_file() {
local secret_file
local secret_dir
local tmp_file
local grafana_client_secret
local gitea_client_secret
local user_password
local user_email="${HOMELAB_DEX_USER_EMAIL:-jv@lab2025.duckdns.org}"
local user_name="${HOMELAB_DEX_USER_NAME:-jv}"
local user_id
secret_file="$(dex_secret_file)"
if [[ -s "${secret_file}" ]]; then
return 0
fi
ensure_sops_age_tools
secret_dir="$(dirname "${secret_file}")"
mkdir -p "${secret_dir}"
tmp_file="$(mktemp)"
grafana_client_secret="$(generate_dex_secret_value)"
gitea_client_secret="$(generate_dex_secret_value)"
user_password="$(generate_dex_secret_value)"
user_id="$(generate_dex_user_id)"
chmod 600 "${tmp_file}"
{
printf 'apiVersion: v1\n'
printf 'kind: Secret\n'
printf 'metadata:\n'
printf ' name: dex-secrets\n'
printf ' namespace: dex\n'
printf ' labels:\n'
printf ' app.kubernetes.io/name: dex\n'
printf ' app.kubernetes.io/managed-by: jeannie\n'
printf 'type: Opaque\n'
printf 'stringData:\n'
printf ' DEX_GRAFANA_CLIENT_SECRET: "%s"\n' "${grafana_client_secret}"
printf ' DEX_GITEA_CLIENT_SECRET: "%s"\n' "${gitea_client_secret}"
printf ' DEX_USER_EMAIL: "%s"\n' "${user_email}"
printf ' DEX_USER_NAME: "%s"\n' "${user_name}"
printf ' DEX_USER_ID: "%s"\n' "${user_id}"
printf ' DEX_USER_PASSWORD: "%s"\n' "${user_password}"
} >"${tmp_file}"
SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops \
--encrypt \
--input-type yaml \
--output-type yaml \
--filename-override "${secret_file}" \
"${tmp_file}" >"${secret_file}"
rm -f "${tmp_file}"
chmod 600 "${secret_file}"
echo "Created SOPS-encrypted Dex secret at ${secret_file}."
echo "Commit this encrypted file after validating the deployment; it contains no plaintext secret."
}
dex_secret_values() {
local secret_file
local key_file
local decrypted_text
ensure_dex_secret_file
secret_file="$(dex_secret_file)"
key_file="$(sops_age_key_file)"
if [[ -s "${key_file}" ]]; then
decrypted_text="$(SOPS_AGE_KEY_FILE="${key_file}" sops -d "${secret_file}")" || return 1
else
decrypted_text="$(sops -d "${secret_file}")" || return 1
fi
SECRET_TEXT="${decrypted_text}" python3 - <<'PY'
import os
import shlex
import sys
try:
import yaml
except Exception as exc:
print(f"PyYAML is required to parse the Dex secret: {exc}", file=sys.stderr)
sys.exit(1)
document = yaml.safe_load(os.environ.get("SECRET_TEXT", "")) or {}
values = {}
for section in ("stringData", "data"):
raw = document.get(section) or {}
if isinstance(raw, dict):
values.update(raw)
required = [
"DEX_GRAFANA_CLIENT_SECRET",
"DEX_GITEA_CLIENT_SECRET",
"DEX_USER_EMAIL",
"DEX_USER_NAME",
"DEX_USER_ID",
"DEX_USER_PASSWORD",
]
missing = [key for key in required if not values.get(key)]
if missing:
print(f"Missing Dex secret keys: {', '.join(missing)}", file=sys.stderr)
sys.exit(1)
for key in required:
value = str(values[key])
if "\n" in value or "\r" in value:
print(f"Secret value for {key} cannot contain a newline", file=sys.stderr)
sys.exit(1)
print(f"{key}={shlex.quote(value)}")
PY
}
dex_secret() {
local action="${1:-ensure}"
require_debian_server "dex-secret"
case "${action}" in
ensure)
ensure_dex_secret_file
echo "Dex secret file is ready."
;;
file)
ensure_dex_secret_file
dex_secret_file
;;
*)
echo "Usage: ./jeannie dex-secret {ensure|file}" >&2
exit 1
;;
esac
}
authentik_secret_file() {
printf '%s\n' "${HOMELAB_AUTHENTIK_SECRET_FILE:-${REPO_ROOT}/apps/authentik/authentik.secret.yaml}"
}