diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e09728a..6942ebf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,6 +45,7 @@ repos: language: system types: [yaml] files: \.(yaml|yml)$ + exclude: '(^|/).*\.(secret|enc)\.ya?ml$' - repo: local hooks: diff --git a/apps/authentik/README.md b/apps/authentik/README.md new file mode 100644 index 0000000..6271c4d --- /dev/null +++ b/apps/authentik/README.md @@ -0,0 +1,34 @@ +# Authentik + +Authentik is the homelab identity provider and future SSO control plane. + +Phase 1 deploys Authentik itself only. It does not yet move Grafana, Jellyfin, +or other applications to SSO. + +## Deploy + +Run from the Debian homelab server: + +```bash +./jeannie authentik-secret ensure +./jeannie platform +``` + +`./jeannie platform` also runs the secret step automatically before applying the +platform OpenTofu stack. + +The generated secret file is: + +```text +apps/authentik/authentik.secret.yaml +``` + +Commit only that SOPS-encrypted file, never a plaintext secret. + +## Public URL + +```text +https://auth.lab2025.duckdns.org/ +``` + +The first SSO integration target will be Grafana in a later phase. diff --git a/bootstrap/platform/main.tf b/bootstrap/platform/main.tf index 47a9871..9ef1cff 100644 --- a/bootstrap/platform/main.tf +++ b/bootstrap/platform/main.tf @@ -1099,6 +1099,15 @@ resource "kubernetes_namespace_v1" "monitoring" { } } +resource "kubernetes_namespace_v1" "authentik" { + count = var.authentik.enabled ? 1 : 0 + depends_on = [kubernetes_storage_class_v1.openebs_hostpath_retain] + + metadata { + name = var.authentik.namespace + } +} + resource "helm_release" "argocd" { depends_on = [helm_release.openebs] name = "argocd" @@ -2119,6 +2128,154 @@ EOT } } +resource "helm_release" "authentik" { + count = var.authentik.enabled ? 1 : 0 + + depends_on = [kubernetes_namespace_v1.authentik, helm_release.traefik] + name = "authentik" + repository = var.authentik.repository + chart = var.authentik.chart + version = var.authentik.version + namespace = var.authentik.namespace + create_namespace = false + timeout = 900 + wait = false + cleanup_on_fail = true + + values = [ + yamlencode({ + global = { + nodeSelector = local.prometheus_stack_node_selector + envFrom = [ + { + secretRef = { + name = var.authentik.secret_name + } + } + ] + } + authentik = { + log_level = "info" + error_reporting = { + enabled = false + } + postgresql = { + host = "authentik-postgresql" + name = "authentik" + user = "authentik" + } + } + server = { + replicas = 1 + nodeSelector = local.prometheus_stack_node_selector + ingress = { + enabled = true + ingressClassName = var.traefik.ingress_class + hosts = [var.authentik.hostname] + } + metrics = { + enabled = true + serviceMonitor = { + enabled = var.authentik.service_monitor + namespace = var.observability.namespace + } + } + resources = { + requests = { + cpu = var.authentik.server_cpu_request + memory = var.authentik.server_memory_request + } + limits = { + cpu = var.authentik.server_cpu_limit + memory = var.authentik.server_memory_limit + } + } + } + worker = { + replicas = 1 + nodeSelector = local.prometheus_stack_node_selector + resources = { + requests = { + cpu = var.authentik.worker_cpu_request + memory = var.authentik.worker_memory_request + } + limits = { + cpu = var.authentik.worker_cpu_limit + memory = var.authentik.worker_memory_limit + } + } + } + postgresql = { + enabled = true + auth = { + username = "authentik" + database = "authentik" + existingSecret = var.authentik.secret_name + secretKeys = { + adminPasswordKey = "postgres-password" + userPasswordKey = "postgresql-password" + } + } + primary = { + persistence = { + enabled = true + storageClass = var.openebs.retain_storage_class + size = var.authentik.postgresql_storage + } + resources = { + requests = { + cpu = "100m" + memory = "256Mi" + } + limits = { + cpu = "500m" + memory = "512Mi" + } + } + } + } + }) + ] +} + +resource "null_resource" "authentik_readiness" { + count = var.authentik.enabled ? 1 : 0 + + depends_on = [helm_release.authentik] + + triggers = { + kubeconfig_path = var.kubeconfig_path + namespace = var.authentik.namespace + hostname = var.authentik.hostname + release_version = var.authentik.version + } + + provisioner "local-exec" { + interpreter = ["/bin/bash", "-lc"] + command = <&2 || true + kubectl --kubeconfig "$kubeconfig" -n "$namespace" get events --sort-by=.lastTimestamp 2>/dev/null | tail -80 >&2 || true +} + +if ! kubectl --kubeconfig "$kubeconfig" -n "$namespace" rollout status deployment/authentik-server --timeout=600s; then + dump_authentik_debug + exit 1 +fi + +if ! kubectl --kubeconfig "$kubeconfig" -n "$namespace" rollout status deployment/authentik-worker --timeout=600s; then + dump_authentik_debug + exit 1 +fi +EOT + } +} + resource "kubernetes_config_map" "grafana_dashboards" { for_each = local.grafana_dashboard_files diff --git a/bootstrap/platform/variables.tf b/bootstrap/platform/variables.tf index 2861b2d..99de698 100644 --- a/bootstrap/platform/variables.tf +++ b/bootstrap/platform/variables.tf @@ -248,6 +248,48 @@ variable "observability" { } } +variable "authentik" { + type = object({ + enabled = bool + namespace = string + repository = string + chart = string + version = string + hostname = string + postgresql_storage = string + secret_name = string + service_monitor = bool + server_cpu_request = string + server_memory_request = string + server_cpu_limit = string + server_memory_limit = string + worker_cpu_request = string + worker_memory_request = string + worker_cpu_limit = string + worker_memory_limit = string + }) + + default = { + enabled = true + namespace = "authentik" + repository = "https://charts.goauthentik.io" + chart = "authentik" + version = "2026.8.3" + hostname = "auth.lab2025.duckdns.org" + postgresql_storage = "8Gi" + secret_name = "authentik-secrets" + service_monitor = false + server_cpu_request = "100m" + server_memory_request = "512Mi" + server_cpu_limit = "500m" + server_memory_limit = "1Gi" + worker_cpu_request = "100m" + worker_memory_request = "512Mi" + worker_cpu_limit = "500m" + worker_memory_limit = "1Gi" + } +} + variable "prometheus_stack_node_selector" { description = "Node selector applied to kube-prometheus-stack control workloads so they stay off the control plane." type = map(string) diff --git a/homelab.yml b/homelab.yml index 7492d4d..691adcb 100644 --- a/homelab.yml +++ b/homelab.yml @@ -14,6 +14,7 @@ domain: - heimdall.lab2025.duckdns.org - homepage.lab2025.duckdns.org - jellyfin.lab2025.duckdns.org + - auth.lab2025.duckdns.org - matrix.lab2025.duckdns.org - element.lab2025.duckdns.org - grafana.lab2025.duckdns.org diff --git a/infra/heimdall/homepage/services.yaml b/infra/heimdall/homepage/services.yaml index 4ef219d..3117ca0 100644 --- a/infra/heimdall/homepage/services.yaml +++ b/infra/heimdall/homepage/services.yaml @@ -150,3 +150,8 @@ href: "http://192.168.100.73:8089/" description: "Optional local Ethereum devnet explorer" icon: "mdi-cube-outline" + +- Other: + - Authentik: + href: "https://auth.lab2025.duckdns.org/" + description: "Homelab identity provider and SSO control plane" diff --git a/infra/heimdall/links.json b/infra/heimdall/links.json index a6f9544..2fe368e 100644 --- a/infra/heimdall/links.json +++ b/infra/heimdall/links.json @@ -34,6 +34,13 @@ "colour": "#7c3aed", "class": "Jellyfin" }, + { + "title": "Authentik", + "url": "https://auth.lab2025.duckdns.org/", + "description": "Homelab identity provider and SSO control plane", + "colour": "#fd4b2d", + "class": "Authentik" + }, { "title": "Matrix", "url": "https://matrix.lab2025.duckdns.org/", diff --git a/jeannie b/jeannie index edcf617..143701d 100755 --- a/jeannie +++ b/jeannie @@ -100,6 +100,7 @@ Operator View And Reports Observability And GitOps grafana-dashboards {list|apply} List or apply repo-managed Grafana dashboards. + authentik-secret {ensure|apply|file} Generate/apply SOPS-encrypted Authentik bootstrap secrets. gitops-status Print focused Argo CD health and sync status. platform Deploy platform stack only. edge Deploy/check the OCI edge stack only. @@ -304,6 +305,9 @@ case "${1:-}" in grafana-dashboards) grafana_dashboards "$@" ;; + authentik-secret) + authentik_secret "${2:-ensure}" + ;; workers) workers_manage "$@" ;; diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 7b99ab9..c9501c1 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -503,6 +503,9 @@ run_tofu_stack() { ensure_edge_tailscale_routes ensure_edge_haproxy_stats_password fi + if [[ "${stack}" == "bootstrap/platform" ]]; then + ensure_authentik_secret + fi if truthy "${auto_approve}"; then apply_args+=("-auto-approve") diff --git a/lib/jeannie/security.sh b/lib/jeannie/security.sh index 351fbb1..d64d341 100644 --- a/lib/jeannie/security.sh +++ b/lib/jeannie/security.sh @@ -189,4 +189,126 @@ EOF security_attack_path() { require_debian_server "security-attack-path" "${REPO_ROOT}/scripts/security-attack-path" "${@:2}" -} \ No newline at end of file +} + +authentik_secret_file() { + printf '%s\n' "${HOMELAB_AUTHENTIK_SECRET_FILE:-${REPO_ROOT}/apps/authentik/authentik.secret.yaml}" +} + +generate_authentik_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 Authentik secrets." >&2 + return 1 + fi +} + +ensure_authentik_secret_file() { + local secret_file + local secret_dir + local tmp_file + local secret_key + local postgres_password + local bootstrap_token + local bootstrap_email="${HOMELAB_AUTHENTIK_BOOTSTRAP_EMAIL:-admin@lab2025.duckdns.org}" + + secret_file="$(authentik_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)" + secret_key="$(generate_authentik_secret_value)" + postgres_password="$(generate_authentik_secret_value)" + bootstrap_token="$(generate_authentik_secret_value)" + + chmod 600 "${tmp_file}" + { + printf 'apiVersion: v1\n' + printf 'kind: Secret\n' + printf 'metadata:\n' + printf ' name: authentik-secrets\n' + printf ' namespace: authentik\n' + printf ' labels:\n' + printf ' app.kubernetes.io/name: authentik\n' + printf ' app.kubernetes.io/managed-by: jeannie\n' + printf 'type: Opaque\n' + printf 'stringData:\n' + printf ' AUTHENTIK_SECRET_KEY: "%s"\n' "${secret_key}" + printf ' AUTHENTIK_POSTGRESQL__PASSWORD: "%s"\n' "${postgres_password}" + printf ' AUTHENTIK_BOOTSTRAP_TOKEN: "%s"\n' "${bootstrap_token}" + printf ' AUTHENTIK_BOOTSTRAP_EMAIL: "%s"\n' "${bootstrap_email}" + printf ' postgres-password: "%s"\n' "${postgres_password}" + printf ' postgresql-password: "%s"\n' "${postgres_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 Authentik secret at ${secret_file}." + echo "Commit this encrypted file after validating the deployment; it contains no plaintext secret." +} + +apply_authentik_secret() { + local secret_file + + secret_file="$(authentik_secret_file)" + ensure_authentik_secret_file + + if ! command -v kubectl >/dev/null 2>&1; then + echo "kubectl is required to apply the Authentik secret." >&2 + return 1 + fi + + kubectl --kubeconfig "${KUBECONFIG_PATH}" create namespace authentik --dry-run=client -o yaml | + kubectl --kubeconfig "${KUBECONFIG_PATH}" apply -f - + SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops -d "${secret_file}" | + kubectl --kubeconfig "${KUBECONFIG_PATH}" apply -f - +} + +ensure_authentik_secret() { + require_debian_server "authentik-secret" + apply_authentik_secret >/dev/null + echo "Authentik bootstrap secret is present in Kubernetes." +} + +authentik_secret() { + local action="${1:-ensure}" + + require_debian_server "authentik-secret" + + case "${action}" in + ensure | apply) + apply_authentik_secret + echo "Authentik bootstrap secret is ready." + ;; + file) + ensure_authentik_secret_file + authentik_secret_file + ;; + *) + echo "Usage: ./jeannie authentik-secret {ensure|apply|file}" >&2 + exit 1 + ;; + esac +} diff --git a/scripts/generate-heimdall-links b/scripts/generate-heimdall-links index 4a0f41a..19fe221 100755 --- a/scripts/generate-heimdall-links +++ b/scripts/generate-heimdall-links @@ -34,6 +34,7 @@ HOST_METADATA = { "heimdall": ("Heimdall", "Homelab service dashboard", "#7c3aed", "Heimdall"), "homepage": ("Homepage", "Generated homelab operations dashboard", "#0284c7", "Homepage"), "jellyfin": ("Jellyfin", "Media streaming server", "#7c3aed", "Jellyfin"), + "auth": ("Authentik", "Homelab identity provider and SSO control plane", "#fd4b2d", "Authentik"), "matrix": ("Matrix", "Matrix Synapse homeserver", "#0f172a", "Matrix"), "element": ("Element", "Matrix web client", "#0ea5e9", "Element"), "grafana": ("Grafana", "Monitoring dashboards", "#f97316", "Grafana"),