From 8c2af0de52df6b835cd2a57de08000abeeab9d3a Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 4 Oct 2026 17:51:08 +0200 Subject: [PATCH] Enable Authentik login and prefer arschrock for OCR --- .gitea/workflows/deploy.yml | 8 ++--- .gitlab-ci.yml | 4 +-- README.md | 10 +++++-- app.yaml | 20 +++++++++++++ authentik-setup.py | 60 +++++++++++++++++++++++++++++++++++++ install.sh | 2 ++ setup-authentik.sh | 26 ++++++++++++++++ test.sh | 21 ++++++++----- 8 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 authentik-setup.py create mode 100755 setup-authentik.sh diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index d55c3b6..0c81f21 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -6,18 +6,18 @@ on: jobs: test: runs-on: ubuntu-latest - container: alpine/k8s:1.33.0 + container: node:22-alpine steps: - uses: actions/checkout@v4 - - run: apk add --no-cache bash jq && ./test.sh + - run: apk add --no-cache bash python3 py3-yaml && ./test.sh deploy: if: github.event_name == 'push' needs: test runs-on: ubuntu-latest - container: alpine/k8s:1.33.0 + container: node:22-alpine steps: - uses: actions/checkout@v4 - - run: apk add --no-cache bash jq + - run: apk add --no-cache bash python3 py3-yaml kubectl - run: | umask 077 echo "$KUBE_CONFIG_BASE64" | base64 -d > kubeconfig diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 62446b6..a976d78 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ test: stage: test image: alpine/k8s:1.33.0 script: - - apk add --no-cache bash jq + - apk add --no-cache bash python3 py3-yaml - ./test.sh deploy: @@ -13,7 +13,7 @@ deploy: rules: - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' script: - - apk add --no-cache bash jq + - apk add --no-cache bash python3 py3-yaml - umask 077 - echo "$KUBE_CONFIG_BASE64" | base64 -d > kubeconfig - export KUBECONFIG="$CI_PROJECT_DIR/kubeconfig" diff --git a/README.md b/README.md index 5aa7ced..ee42758 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,14 @@ One Paperless-ngx instance, SQLite, and Valkey in the `paperless-ngx` namespace. ## Install -Run `./install.sh` from any directory. It creates the namespace and a random, persistent application secret on first install, checks the manifests with a server dry run, applies them, then waits for both deployments. Open the URL and create the first administrator account in the web UI. Drop scanned files in the web UI or into `/usr/src/paperless/consume` on the PVC. +Run `./install.sh` from any directory. It creates the namespace and random, persistent application and OIDC secrets on first install, creates the Authentik OIDC application, checks the manifests with a server dry run, applies them, then waits for both deployments. Open the URL and sign in with the **authentik** button. Drop scanned files in the web UI or into `/usr/src/paperless/consume` on the PVC. Change settings in `app.yaml`, then run `./test.sh` and `./install.sh`. The Paperless image is pinned there. The CephFS inbox uses polling because network filesystems may not deliver file notifications. +Paperless prefers the 24-core `arschrock` node for OCR. This is a scheduler preference, so it can run on an Odroid when `arschrock` is unavailable. The control-plane taint is tolerated only by Paperless, not Valkey. A node failure may briefly interrupt OCR while Kubernetes reschedules the single Paperless pod. + +The 20 Gi PVC uses `rook-cephfs-ec`, whose CephFS data pool is erasure coded with three data and two coding chunks across hosts. CephFS metadata uses three replicas. The claim can be expanded if high-resolution scans outgrow it; erasure coding does not replace backups. + ## CI/CD `./test.sh` checks syntax and the deployment manifest. `./deploy.sh` updates the existing workloads, services, and ingress using `app.yaml`. The GitLab and Gitea workflows call these same scripts. Bootstrap once with `./create-ci-kubeconfig.sh`, then save its single output line as the protected CI variable / Gitea Actions secret `KUBE_CONFIG_BASE64`. Do not commit it. The deployer has named-object read and patch permissions and cannot read Secrets. Run `./install.sh` manually for first installation or changes to `storage.yaml`. @@ -16,7 +20,9 @@ The Git remote is `gitea@brunner.ninja:feedc0de/paperless-ngx-deployment.git`. N ## Authentik login -Paperless supports Authentik through OpenID Connect. In Authentik, create an OAuth2/OpenID Connect application/provider with a strict authorization redirect URI of `https://paper.brunner.ninja/accounts/oidc/authentik/login/callback/`. Save its client ID and secret outside Git. To enable it, add `PAPERLESS_APPS=allauth.socialaccount.providers.openid_connect` to the Paperless container environment in `app.yaml`, and set `PAPERLESS_SOCIALACCOUNT_PROVIDERS` from a Kubernetes Secret containing the JSON provider configuration described in [Authentik's Paperless guide](https://integrations.goauthentik.io/documentation/paperless-ngx/). Keep local login enabled until the Authentik flow has been tested. Existing Paperless users can link their Authentik account from **My Profile**. +`./setup-authentik.sh` creates the OAuth2/OpenID Connect provider, application, and `Paperless Users` and `Paperless Admins` groups in Authentik, adds `feedc0de` to both groups, and stores the client secret in the `paperless-oidc` Kubernetes Secret. It preserves that secret on later runs. `./install.sh` invokes it automatically. To grant others access, add them to **Paperless Users** in Authentik's web UI; use **Paperless Admins** only for people who should see and administer all documents. Paperless syncs those Authentik group claims at login, including administrator status. Existing Paperless users can link their Authentik account from **My Profile**. Local login stays enabled as a recovery option. The callback is `https://paper.brunner.ninja/accounts/oidc/authentik/login/callback/` per [Authentik's Paperless guide](https://integrations.goauthentik.io/documentation/paperless-ngx/). + +The first-visit create-user form is Paperless's bootstrap screen. Since the initial local account was created, link it from **My Profile → Connect new social account** before signing in with Authentik. At subsequent Authentik logins, the `Paperless Admins` claim grants administrator status. ## Recovery diff --git a/app.yaml b/app.yaml index f00ec19..85320d1 100644 --- a/app.yaml +++ b/app.yaml @@ -55,6 +55,19 @@ spec: spec: automountServiceAccountToken: false enableServiceLinks: false + tolerations: + - key: node-role.kubernetes.io/control-plane + operator: Exists + effect: NoSchedule + affinity: + nodeAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + preference: + matchExpressions: + - key: kubernetes.io/hostname + operator: In + values: [arschrock] containers: - name: paperless image: ghcr.io/paperless-ngx/paperless-ngx:3.2.1 @@ -65,6 +78,13 @@ spec: - {name: PAPERLESS_TIME_ZONE, value: "Europe/Vienna"} - {name: PAPERLESS_OCR_LANGUAGE, value: "deu+eng"} - {name: PAPERLESS_CONSUMER_POLLING_INTERVAL, value: "30"} + - {name: PAPERLESS_APPS, value: "allauth.socialaccount.providers.openid_connect"} + - {name: PAPERLESS_SOCIAL_AUTO_SIGNUP, value: "true"} + - {name: PAPERLESS_SOCIAL_ACCOUNT_SYNC_GROUPS, value: "true"} + - {name: PAPERLESS_SOCIAL_ACCOUNT_SYNC_SUPERUSER_GROUP, value: "Paperless Admins"} + - name: PAPERLESS_SOCIALACCOUNT_PROVIDERS + valueFrom: + secretKeyRef: {name: paperless-oidc, key: providers} - name: PAPERLESS_SECRET_KEY valueFrom: secretKeyRef: {name: paperless-secret-key, key: secret-key} diff --git a/authentik-setup.py b/authentik-setup.py new file mode 100644 index 0000000..a394269 --- /dev/null +++ b/authentik-setup.py @@ -0,0 +1,60 @@ +from django.db import transaction +from authentik.core.models import Application, Group, User +from authentik.policies.models import PolicyBinding +from authentik.providers.oauth2.models import OAuth2Provider + +with transaction.atomic(): + template = OAuth2Provider.objects.get(name="Argo CD") + redirect = template.redirect_uris[0] + provider, _ = OAuth2Provider.objects.get_or_create( + name="Paperless-ngx", + defaults={ + "authorization_flow": template.authorization_flow, + "invalidation_flow": template.invalidation_flow, + "client_type": "confidential", + "grant_types": ["authorization_code", "refresh_token"], + "client_id": "paperless-ngx", + "client_secret": CLIENT_SECRET, + "signing_key": template.signing_key, + }, + ) + provider.authorization_flow = template.authorization_flow + provider.invalidation_flow = template.invalidation_flow + provider.client_type = "confidential" + provider.grant_types = ["authorization_code", "refresh_token"] + provider.client_id = "paperless-ngx" + provider.client_secret = CLIENT_SECRET + provider.signing_key = template.signing_key + provider.redirect_uris = [ + type(redirect)( + matching_mode=redirect.matching_mode, + url="https://paper.brunner.ninja/accounts/oidc/authentik/login/callback/", + redirect_uri_type=redirect.redirect_uri_type, + ) + ] + provider.save() + provider.property_mappings.set( + template.property_mappings.filter(managed__startswith="goauthentik.io/providers/oauth2/scope-") + ) + + app, _ = Application.objects.get_or_create( + slug="paperless-ngx", defaults={"name": "Paperless-ngx", "provider": provider} + ) + app.name = "Paperless-ngx" + app.provider = provider + app.meta_launch_url = "https://paper.brunner.ninja" + app.save() + + group, _ = Group.objects.get_or_create(name="Paperless Users") + admin_group, _ = Group.objects.get_or_create(name="Paperless Admins") + owner = User.objects.get(username="feedc0de") + group.users.add(owner) + admin_group.users.add(owner) + binding, _ = PolicyBinding.objects.get_or_create( + target=app, group=group, defaults={"order": 0, "enabled": True} + ) + if not binding.enabled: + binding.enabled = True + binding.save(update_fields=["enabled"]) + +print("PAPERLESS_AUTHENTIK_READY") diff --git a/install.sh b/install.sh index cf4a755..c093793 100755 --- a/install.sh +++ b/install.sh @@ -7,8 +7,10 @@ if ! kubectl -n paperless-ngx get secret paperless-secret-key >/dev/null 2>&1; t key=$(openssl rand -base64 64) kubectl -n paperless-ngx create secret generic paperless-secret-key --from-literal="secret-key=${key}" fi +./setup-authentik.sh ./test.sh kubectl apply --dry-run=server -f storage.yaml -f app.yaml kubectl apply -f storage.yaml -f app.yaml kubectl -n paperless-ngx rollout status deployment/valkey --timeout=5m kubectl -n paperless-ngx rollout status deployment/paperless --timeout=15m +kubectl -n paperless-ngx exec deployment/paperless -- python manage.py shell -c 'from django.contrib.auth.models import Group; [Group.objects.get_or_create(name=name) for name in ("Paperless Users", "Paperless Admins")]' diff --git a/setup-authentik.sh b/setup-authentik.sh new file mode 100755 index 0000000..a14fa09 --- /dev/null +++ b/setup-authentik.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail +umask 077 +cd "$(dirname "${BASH_SOURCE[0]}")" +[[ $(kubectl config current-context) == kubernetes-admin@kubernetes ]] || { echo 'Wrong Kubernetes context' >&2; exit 1; } + +workdir=$(mktemp -d) +chmod 700 "$workdir" +trap 'rm -rf "$workdir"' EXIT + +if kubectl -n paperless-ngx get secret paperless-oidc -o jsonpath='{.data.providers}' 2>/dev/null | base64 -d > "$workdir/providers.json" && [[ -s "$workdir/providers.json" ]]; then + echo 'Keeping the existing Paperless OIDC client secret.' +else + openssl rand -hex 32 | tr -d '\n' > "$workdir/client-secret" + jq -n --rawfile secret "$workdir/client-secret" '{openid_connect:{OAUTH_PKCE_ENABLED:true,APPS:[{provider_id:"authentik",name:"authentik",client_id:"paperless-ngx",secret:$secret,settings:{server_url:"https://auth.brunner.ninja/application/o/paperless-ngx/.well-known/openid-configuration",fetch_userinfo:true}}],SCOPE:["openid","profile","email"]}}' > "$workdir/providers.json" + kubectl -n paperless-ngx create secret generic paperless-oidc --from-file="providers=$workdir/providers.json" +fi + +jq -er '.openid_connect.APPS[] | select(.provider_id == "authentik") | .secret' "$workdir/providers.json" | tr -d '\n' > "$workdir/client-secret" +{ + printf 'CLIENT_SECRET = ' + jq -Rs . < "$workdir/client-secret" + cat authentik-setup.py +} > "$workdir/setup.py" +chmod 600 "$workdir"/* +kubectl -n authentik exec -i deployment/authentik-worker -- ak shell --no-imports --interface python < "$workdir/setup.py" diff --git a/test.sh b/test.sh index 714025e..6527ebe 100755 --- a/test.sh +++ b/test.sh @@ -1,11 +1,16 @@ #!/usr/bin/env bash set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" -bash -n install.sh deploy.sh create-ci-kubeconfig.sh test.sh -kubectl create --dry-run=client --validate=false -f storage.yaml -f app.yaml -f ci-deployer.yaml -o json >/dev/null -images=$(kubectl create --dry-run=client --validate=false -f app.yaml -o json | jq -r 'select(.kind == "Deployment") | .spec.template.spec.containers[].image') -[[ -n $images ]] -while IFS= read -r image; do - [[ $image == *:* && $image != *:latest ]] -done <<<"${images}" -echo 'Manifest valid; deployment images are pinned.' +bash -n install.sh deploy.sh create-ci-kubeconfig.sh setup-authentik.sh test.sh +python3 - <<'PY' +import yaml + +documents = [] +for path in ("storage.yaml", "app.yaml", "ci-deployer.yaml"): + with open(path, encoding="utf-8") as stream: + documents.extend(doc for doc in yaml.safe_load_all(stream) if doc) +images = [container["image"] for doc in documents if doc["kind"] == "Deployment" + for container in doc["spec"]["template"]["spec"]["containers"]] +assert images and all(":" in image and not image.endswith(":latest") for image in images) +print("Manifest valid; deployment images are pinned.") +PY