From 612b4f725083acec9ff7b260b311e2fd1b80db4e Mon Sep 17 00:00:00 2001 From: Jochen Mehlich Date: Sat, 24 Oct 2026 10:15:01 +0200 Subject: [PATCH] Doku und Skripte: authentik-LDAP-Outpost mit sshd (SSSD), Self-only-Check in docker-deploy.sh Co-Authored-By: Claude Sonnet 5.5 --- check-sshd-ldap.sh | 37 +++++++ docker-deploy.sh | 4 + docs/authentik-ldap-sshd.md | 99 +++++++++++++++++++ setup-sshd-ldap.sh | 186 ++++++++++++++++++++++++++++++++++++ 4 files changed, 326 insertions(+) create mode 100755 check-sshd-ldap.sh create mode 100644 docs/authentik-ldap-sshd.md create mode 100755 setup-sshd-ldap.sh diff --git a/check-sshd-ldap.sh b/check-sshd-ldap.sh new file mode 100755 index 0000000..a0e417a --- /dev/null +++ b/check-sshd-ldap.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# check-sshd-ldap.sh [server] +# +# Diagnose der Kette authentik-LDAP-Outpost → SSSD → NSS/PAM → sshd. +set -uo pipefail +export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + +USER_NAME="${1:-}" +SERVER="${2:-H1}" +[[ -n "$USER_NAME" ]] || { echo "Verwendung: $(basename "$0") [server]" >&2; exit 1; } + +fail=0 +check() { # check "Beschreibung" befehl... + local desc="$1"; shift + if "$@" >/dev/null 2>&1; then printf '✅ %s\n' "$desc"; else printf '❌ %s\n' "$desc"; fail=1; fi +} + +check "sssd läuft" systemctl is-active --quiet sssd +check "sssd.conf: Rechte 0600 root" test "$(stat -c '%a %u' /etc/sssd/sssd.conf)" = "600 0" +check "sshd-Konfiguration gültig" sshd -t +check "sshd: AuthorizedKeysCommand gesetzt" bash -c "sshd -T | grep -qi '^authorizedkeyscommand .*sss_ssh_authorizedkeys'" +check "NSS: passwd enthält sss" bash -c "grep -Eq '^passwd:.*\bsss\b' /etc/nsswitch.conf" +check "User '$USER_NAME' per NSS auflösbar" getent passwd "$USER_NAME" +check "Gruppe server_${SERVER}_ssh_access" bash -c "id -nG '$USER_NAME' | tr ' ' '\n' | grep -qx 'server_${SERVER}_ssh_access'" +check "Gruppe server_${SERVER}_docker_compose" bash -c "id -nG '$USER_NAME' | tr ' ' '\n' | grep -qx 'server_${SERVER}_docker_compose'" +check "SSH-Keys-Abfrage funktioniert (Aufruf)" sss_ssh_authorizedkeys "$USER_NAME" + +echo +echo "── getent passwd $USER_NAME"; getent passwd "$USER_NAME" || true +echo "── id $USER_NAME"; id "$USER_NAME" 2>&1 || true +echo "── sssctl domain-status authentik"; sssctl domain-status authentik 2>&1 | head -n 15 || true + +if [[ $fail -ne 0 ]]; then + echo + echo "Hinweis: Debug mit 'sssctl debug-level 6' und 'journalctl -u sssd -u ssh -f'." +fi +exit "$fail" diff --git a/docker-deploy.sh b/docker-deploy.sh index c679be5..1365112 100644 --- a/docker-deploy.sh +++ b/docker-deploy.sh @@ -84,6 +84,10 @@ fi [[ "$APP_USER" =~ ^[a-z_][a-z0-9_-]{0,31}$ ]] || die "Ungültiger Username: '$APP_USER'" id "$APP_USER" &>/dev/null || die "User '$APP_USER' existiert nicht auf diesem System" user_in_group "$APP_USER" "$DEPLOY_GROUP" || die "User '$APP_USER' ist nicht in Gruppe '$DEPLOY_GROUP'" +# Per sudo darf jeder nur für sich selbst deployen (sudoers erlaubt die ganze Gruppe) +if [[ -n "${SUDO_USER:-}" && "$SUDO_USER" != "root" && "$SUDO_USER" != "$APP_USER" ]]; then + die "'$SUDO_USER' darf nicht für '$APP_USER' deployen" +fi in_array "$ACTION" "${ALLOWED_ACTIONS[@]}" || die "Unbekannte Aktion: '$ACTION' (erlaubt: ${ALLOWED_ACTIONS[*]})" if $USE_CACHE && [[ "$ACTION" != "build" && "$ACTION" != "deploy" ]]; then die "--cache ist nur bei build/deploy sinnvoll" diff --git a/docs/authentik-ldap-sshd.md b/docs/authentik-ldap-sshd.md new file mode 100644 index 0000000..043d116 --- /dev/null +++ b/docs/authentik-ldap-sshd.md @@ -0,0 +1,99 @@ +# authentik-LDAP-Outpost ↔ sshd + +Anmeldung per SSH gegen authentik: **sshd → PAM/NSS → SSSD → LDAP-Outpost (LDAPS) → authentik**. +Die Deploy-User aus `create-deploy-user.py` (Service-Accounts) melden sich mit ihrem +App-Passwort an, normale User per Passwort oder SSH-Key. + +``` + Client ──ssh──▶ sshd ──PAM/NSS──▶ SSSD ──ldaps:636──▶ LDAP-Outpost ──API──▶ authentik + │ (Bind: Service-Account) + └─ AuthorizedKeysCommand: sss_ssh_authorizedkeys +``` + +| Baustein | Aufgabe | +|---|---| +| `setup-sshd-ldap.sh` | Konfiguriert SSSD, NSS, PAM, sshd, `docker-deploy.sh`-Gruppe und sudoers | +| `check-sshd-ldap.sh` | Diagnose der gesamten Kette | +| `create-deploy-user.py` | Legt Deploy-User + Gruppenmitgliedschaften in authentik an | +| `docker-deploy.sh` | Deployt per sudo; Berechtigung über `server__docker_compose` | + +## 1. authentik vorbereiten + +1. **Gruppen** anlegen (Verzeichnis *Gruppen*): + - `server__ssh_access` (Login auf dem Server erlaubt) + - `server__docker_compose` (darf `docker-deploy.sh` nutzen) + - Server-Standard ist `H1`. +2. **Bind-Account** für SSSD: Service-Account `ldapsvc` anlegen (*Verzeichnis → User → Service-Account*), Token/Passwort notieren. + Eine Gruppe `ldap-search` anlegen, den Account hinzufügen und dieser Gruppe die + Berechtigung *Search full LDAP directory* geben. Ohne diese Berechtigung sieht SSSD keine Gruppen. +3. **LDAP-Provider** anlegen (*Anwendungen → Provider → LDAP Provider*): + - Bind-Flow: `default-authentication-flow` (oder ein Flow ohne MFA, wenn Service-Accounts per App-Passwort binden – MFA lässt sich per LDAP nicht beantworten) + - Search-Group: `ldap-search` + - Base-DN: z. B. `dc=ldap,dc=goauthentik,dc=io` + - *Bind mode* / *Search mode*: Direct (keine Cached-Variante, damit Gruppenänderungen sofort greifen) +4. **Application** zum Provider anlegen, dann einen **LDAP-Outpost** erstellen und die Application zuweisen. +5. **TLS:** Der Outpost liefert LDAPS auf 636. Für ein vertrauenswürdiges Zertifikat dem Outpost ein Zertifikat zuweisen (Provider → *TLS Server name* / Zertifikat) und die CA auf dem SSH-Server hinterlegen (`--ca-cert`). Klartext-LDAP wird vom Setup-Skript abgelehnt. +6. **User-Attribute** (setzt `create-deploy-user.py` für Deploy-User automatisch): + - `homeDirectory`: Home-Verzeichnis, Standard `/home/` + - `notes`: Link zum Gitea-Repo + - `sshPublicKey` (optional): öffentlicher SSH-Schlüssel für Key-Login + - `uidNumber`/`gidNumber` berechnet authentik selbst. + +## 2. SSH-Server einrichten + +Voraussetzung: Debian/Ubuntu, root-Zugriff, Netzwerkzugang zum Outpost auf Port 636. + +```bash +sudo ./setup-sshd-ldap.sh \ + --ldap-uri ldaps://ldap.example.org:636 \ + --base-dn dc=ldap,dc=goauthentik,dc=io \ + --bind-dn cn=ldapsvc,ou=users,dc=ldap,dc=goauthentik,dc=io \ + --server H1 \ + --ca-cert /pfad/zur/ca.pem +``` + +Das Skript (idempotent, sichert vorhandene `sssd.conf`): + +- installiert `sssd`, `sssd-ldap`, `libnss-sss`, `libpam-sss` +- schreibt `/etc/sssd/sssd.conf` (0600) mit Zugriffsfilter + `(memberOf=cn=server__ssh_access,ou=groups,)` – nur Gruppenmitglieder dürfen sich per LDAP anmelden +- ergänzt `sss` in `/etc/nsswitch.conf` und aktiviert `pam_sss` sowie `pam_mkhomedir` + (Home wird beim ersten Login angelegt; `/etc/skel/deployment` legt das Deploy-Verzeichnis gleich mit an) +- schreibt `/etc/ssh/sshd_config.d/50-authentik-ldap.conf` (`UsePAM`, `AuthorizedKeysCommand sss_ssh_authorizedkeys`) +- schreibt `/etc/docker-deploy.conf` (`DEPLOY_SERVER=`) und `/etc/sudoers.d/docker-deploy` + (`%server__docker_compose ALL=(root) NOPASSWD: /usr/local/bin/docker-deploy.sh`; abschaltbar mit `--no-sudoers`) + +Danach `docker-deploy.sh` nach `/usr/local/bin/` kopieren (`install -m 0755 docker-deploy.sh /usr/local/bin/`). + +## 3. Prüfen + +```bash +./check-sshd-ldap.sh [server] +ssh @ +``` + +Typische Fehler: + +| Symptom | Ursache / Lösung | +|---|---| +| `getent passwd ` leer | Bind-Account ohne *Search full LDAP directory*; falsche Base-DN; Outpost nicht erreichbar → `sssctl domain-status authentik` | +| Gruppen fehlen in `id` | Nutzer nicht in der Gruppe; `ldap_group_*`-Suchbasis falsch; Cache: `sss_cache -E` | +| Login abgelehnt trotz korrektem Passwort | Nicht in `server__ssh_access` (SSSD-Access-Filter) | +| TLS-Fehler | CA fehlt oder Hostname passt nicht zum Zertifikat (`ldap_tls_reqcert = demand`) | +| Key-Login klappt nicht | Attribut `sshPublicKey` fehlt/falsch benannt; `sss_ssh_authorizedkeys ` testen | +| `sudo`: *darf nicht für … deployen* | Gewollt: Aufrufer darf nur für sich selbst deployen (Admins als echter root) | + +Debug: `sssctl debug-level 6`, `journalctl -u sssd -u ssh -f`. + +## 4. Deploy-Ablauf + +1. `create-deploy-user.py --project-path owner/repo [--server H1]` legt User und Gruppen an und setzt `deploy_user`/`deploy_pass` im Gitea-Repo. +2. Die CI meldet sich per SSH als Deploy-User an, lädt `docker-compose.yml` nach `~/deployment/` und ruft + `sudo docker-deploy.sh deploy` auf. + +## Sicherheitshinweise + +- Bind-Passwort steht in `/etc/sssd/sssd.conf` (0600, root) – für den Bind-Account nur minimale Rechte vergeben. +- Wer `docker compose` als root aus einem User-Verzeichnis ausführen lässt, gibt dem User faktisch root-Rechte (Compose-Datei kann beliebige Mounts definieren). Die Gruppe `server__docker_compose` nur an vertrauenswürdige Projekte vergeben. +- Passwort-Login ist für Service-Accounts nötig; für normale User sollte zusätzlich Key-Login bevorzugt und MFA auf einem separaten Flow gehalten werden. +- Die genauen Attributnamen (`sshPublicKey`, `homeDirectory`, `memberOf`) und die Berechtigung des Search-Accounts hängen von der authentik-Version ab. Beim ersten Einsatz mit `ldapsearch -H ldaps://… -D -W -b "(cn=)"` verifizieren. diff --git a/setup-sshd-ldap.sh b/setup-sshd-ldap.sh new file mode 100755 index 0000000..ef6fdb2 --- /dev/null +++ b/setup-sshd-ldap.sh @@ -0,0 +1,186 @@ +#!/usr/bin/env bash +# setup-sshd-ldap.sh +# +# Bindet sshd über SSSD an den authentik-LDAP-Outpost an (Debian/Ubuntu). +# Siehe docs/authentik-ldap-sshd.md. +# +# Verwendung: +# sudo ./setup-sshd-ldap.sh --ldap-uri ldaps://ldap.example.org:636 \ +# --base-dn dc=ldap,dc=goauthentik,dc=io \ +# --bind-dn cn=ldapsvc,ou=users,dc=ldap,dc=goauthentik,dc=io \ +# [--server H1] [--ca-cert /pfad/ca.pem] [--bind-password-file /pfad] +set -Eeuo pipefail +IFS=$'\n\t' +export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + +readonly SSSD_CONF="/etc/sssd/sssd.conf" +readonly SSHD_DROPIN="/etc/ssh/sshd_config.d/50-authentik-ldap.conf" +readonly SUDOERS_FILE="/etc/sudoers.d/docker-deploy" +readonly DEPLOY_CONF="/etc/docker-deploy.conf" +readonly CA_TARGET="/etc/sssd/authentik-ldap-ca.pem" + +SERVER="H1" +LDAP_URI="" BASE_DN="" BIND_DN="" CA_CERT="" PW_FILE="" +INSTALL_SUDOERS=true + +log() { printf '[%(%H:%M:%S)T] %s\n' -1 "$*"; } +die() { printf '❌ %s\n' "$*" >&2; exit 1; } + +usage() { + cat >&2 < server_H1_ssh_access / server_H1_docker_compose + --ca-cert FILE CA-Zertifikat für das LDAPS-Zertifikat des Outposts + --no-sudoers sudoers-Regel für docker-deploy.sh nicht anlegen +EOF + exit 1 +} + +while [[ $# -gt 0 ]]; do + case "$1" in + --ldap-uri) LDAP_URI="${2:?}"; shift 2 ;; + --base-dn) BASE_DN="${2:?}"; shift 2 ;; + --bind-dn) BIND_DN="${2:?}"; shift 2 ;; + --bind-password-file) PW_FILE="${2:?}"; shift 2 ;; + --server) SERVER="${2:?}"; shift 2 ;; + --ca-cert) CA_CERT="${2:?}"; shift 2 ;; + --no-sudoers) INSTALL_SUDOERS=false; shift ;; + -h|--help) usage ;; + *) usage ;; + esac +done + +[[ $EUID -eq 0 ]] || die "Muss als root laufen" +[[ -n "$LDAP_URI" && -n "$BASE_DN" && -n "$BIND_DN" ]] || usage +[[ "$LDAP_URI" == ldaps://* ]] || die "Nur ldaps:// ist erlaubt (Passwörter dürfen nicht im Klartext übertragen werden)" +[[ "$SERVER" =~ ^[A-Za-z0-9_-]+$ ]] || die "Ungültiger Servername: '$SERVER'" +[[ -z "$CA_CERT" || -r "$CA_CERT" ]] || die "CA-Zertifikat nicht lesbar: $CA_CERT" +command -v apt-get >/dev/null || die "Nur Debian/Ubuntu (apt) unterstützt" + +if [[ -n "$PW_FILE" ]]; then + [[ -r "$PW_FILE" ]] || die "Passwortdatei nicht lesbar: $PW_FILE" + BIND_PW="$(<"$PW_FILE")" +else + read -rsp "Bind-Passwort für $BIND_DN: " BIND_PW; echo +fi +[[ -n "$BIND_PW" ]] || die "Leeres Bind-Passwort" + +SSH_GROUP="server_${SERVER}_ssh_access" +DOCKER_GROUP="server_${SERVER}_docker_compose" + +# ── Pakete ─────────────────────────────────────────────────────────────────── +log "📦 Pakete installieren" +DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + sssd sssd-ldap libnss-sss libpam-sss libsss-sudo openssh-server ldap-utils >/dev/null + +# ── CA ─────────────────────────────────────────────────────────────────────── +TLS_CACERT_LINE="# ldap_tls_cacert: System-CA-Store" +if [[ -n "$CA_CERT" ]]; then + install -m 0644 -o root -g root "$CA_CERT" "$CA_TARGET" + TLS_CACERT_LINE="ldap_tls_cacert = $CA_TARGET" +fi + +# ── SSSD ───────────────────────────────────────────────────────────────────── +log "🔧 $SSSD_CONF schreiben" +install -d -m 0711 /etc/sssd +[[ -e "$SSSD_CONF" ]] && cp -a "$SSSD_CONF" "${SSSD_CONF}.bak.$(date +%s)" +umask 077 +cat >"$SSSD_CONF" </dev/null + +# ── sshd ───────────────────────────────────────────────────────────────────── +log "🔧 $SSHD_DROPIN schreiben" +install -d -m 0755 /etc/ssh/sshd_config.d +grep -Eq '^\s*Include\s+/etc/ssh/sshd_config.d/\*\.conf' /etc/ssh/sshd_config \ + || sed -i '1i Include /etc/ssh/sshd_config.d/*.conf' /etc/ssh/sshd_config +cat >"$SSHD_DROPIN" <"$DEPLOY_CONF" +chown root:root "$DEPLOY_CONF"; chmod 0644 "$DEPLOY_CONF" + +if $INSTALL_SUDOERS; then + log "🔧 $SUDOERS_FILE schreiben" + tmp="$(mktemp)" + printf '%%%s ALL=(root) NOPASSWD: /usr/local/bin/docker-deploy.sh\n' "$DOCKER_GROUP" >"$tmp" + visudo -cf "$tmp" >/dev/null || { rm -f "$tmp"; die "sudoers-Regel ungültig"; } + install -m 0440 -o root -g root "$tmp" "$SUDOERS_FILE" + rm -f "$tmp" +fi + +# ── Dienste ────────────────────────────────────────────────────────────────── +log "🔄 Dienste (neu) starten" +systemctl enable --now sssd >/dev/null +systemctl restart sssd +systemctl reload ssh 2>/dev/null || systemctl reload sshd + +log "✅ Fertig. Prüfen mit: ./check-sshd-ldap.sh "