From 6ab1db839bba73025399411985c3a86201fdbbdb Mon Sep 17 00:00:00 2001 From: David Young Date: Thu, 3 Dec 2026 15:01:11 -0700 Subject: [PATCH] Add the email need: each app gets its own sending-only mail account needs = ["email"] (coreapp new --email) makes an LDAP account coreapp- (not a person, no mailbox) and adds SMTP_* settings. Teardown removes it only if it's the app's; the installer warns about an entry it left alone. Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 35 ++++++++++++++++++++++++++++++++-- README.md | 3 ++- TEST.md | 25 ++++++++++++++++++++++++ bin/coreapp | 14 +++++++++----- core/coreapps | 17 ++++++++++++++++- core/needs/email/depends | 1 + core/needs/email/env.in | 8 ++++++++ core/needs/email/setup | 39 ++++++++++++++++++++++++++++++++++++++ core/needs/email/teardown | 11 +++++++++++ skills/coreapp/SKILL.md | 26 +++++++++++++++++++++++-- templates/app/README.md | 5 ++++- templates/app/coreapp.toml | 2 +- 12 files changed, 173 insertions(+), 13 deletions(-) create mode 100644 core/needs/email/depends create mode 100644 core/needs/email/env.in create mode 100644 core/needs/email/setup create mode 100644 core/needs/email/teardown diff --git a/PLAN.md b/PLAN.md index ab016b8..e38dcb7 100644 --- a/PLAN.md +++ b/PLAN.md @@ -44,6 +44,12 @@ What we learned provisioning (2026-12-02/03): - [ ] The Core shows as Active in the dashboard - [ ] Tell Federated (2026-12-03): + - Apps send mail as `fcore` (an `inetOrgPerson` in the `admins` group) + with the Core's admin password (`MAIL_PASSWORD=$ADMINPASS` in e.g. + `lib/bookstack.sh`), so every app holds the admin password. The + mail server also has no `SPOOF_PROTECTION`, so any account can send + as any address. coreapps gives each app its own sending-only + account instead (see Coreapps, step 4) - On this image, disabling systemd-resolved isn't enough: networkd's `systemd-networkd-resolve-hook.socket` starts it at boot, it takes port 53 and pdns can't start after a reboot. coreapp-dev masks it @@ -289,8 +295,33 @@ Steps: data may not survive that. Refuse, or warn? - [ ] Pin the image digest at install (from the registry at that moment) so a re-pushed tag can't change what's installed -4. [ ] Email and AI needs (findings below), then other services: PowerDNS - records beyond the wildcard, if an app needs other hostnames +4. Email (2026-12-03): `needs = ["email"]` (`coreapp new --email`) gives + the app its own mail account for sending: an LDAP entry + `uid=coreapp-,ou=people` with `objectClass: account`, + `simpleSecurityObject`, `PostfixBookMailAccount`, `mail: + coreapp-@`, `mailEnabled: FALSE` (no mailbox), `description: + coreapps: ` (whose it is), password from `.env` set with + `ldappasswd` (hashed by the server). Not a person, so Authelia + (`objectClass=person`), Nextcloud (`inetOrgPerson`) and Superpanel don't + see it; Federated's shared `fcore` account is an `inetOrgPerson` in + `admins`, hidden only by Superpanel's `HIDDEN_USERS`, and apps get the + admin password with it. Its own address so a person's mail login (by + mail or uid) never finds two entries. `SMTP_*` settings: mail., + 587, STARTTLS, user, password, `SMTP_FROM=@`. Setup leaves + an entry that isn't the app's alone and the installer warns. Tested on + dev.fedcom.net: login from the core network with a valid certificate, + sender and a local recipient accepted (nothing sent), wrong password + refused (535), password stored `{SSHA}`, Authelia sign-in refused (401), + not in Nextcloud or Superpanel users, teardown, a foreign entry left + alone (with a warning), email added to an existing app on update + - [ ] Send a real message, to an address outside the Core (through the + relay) + - [ ] Spoofing: the mail server has no `SPOOF_PROTECTION`, so any + authenticated account (Federated's apps' `fcore` too) can send as any + address. Tell Federated; with it on, an app could only send as its + account's address + - [ ] AI needs (findings below; needs Federated), then other services: + PowerDNS records beyond the wildcard, if an app needs other hostnames 5. The skill (moved ahead of 2-4, 2026-12-03): `skills/coreapp/SKILL.md`, linked into `~/.claude/skills/coreapp` by `update.sh`, so any Claude Code agent on this computer can use it. Rehearsed with a small app diff --git a/README.md b/README.md index 751d5cd..1de09af 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ image = "localhost/coreapp/my-app" # where the Core gets the image version = "0.1.0" port = 8080 # the port it serves HTTP on health = "/" # a path that answers when it's up -needs = ["postgres", "sso"] # its own database; sign-in with Core accounts +needs = ["postgres", "sso"] # its own database; sign-in with Core accounts; "email": sending mail ``` The app has no scripts or compose file: the Core side is made by @@ -79,6 +79,7 @@ each new app explains the files and the settings it gets. | `coreapp new --postgres ` | The same, with its own database on the Core's PostgreSQL (a Python example app that uses it) | | `coreapp new --sso ` | The same, with sign-in through the Core's Authelia (OpenID Connect; a Python example app) | | `coreapp new --postgres --sso ` | Both: a database and sign-in (the example counts each person's visits) | +| `coreapp new --email ` | (With any of the above.) Sending mail through the Core's mail server, with the app's own account | | `coreapp deploy [--recreate]` | Build its image, copy it to the Core over SSH, install it, or update it keeping its settings and data (`--recreate`: a fresh container even if nothing changed) | | `coreapp check` | It answers at `https://.` with a valid certificate | | `coreapp status` / `logs [-f]` / `shell [command]` | Its container, its log, a shell in it (or one command, without a terminal) | diff --git a/TEST.md b/TEST.md index 2b6ae4a..8819db6 100644 --- a/TEST.md +++ b/TEST.md @@ -270,6 +270,31 @@ has the `DB_*` variables, `.coreapp` says `needs=postgres`. `/federated/bin/reconcile-authelia-sso` doesn't bring it back; Authelia healthy, other clients (Nextcloud) still work. Deploy again: one block. +### With email + +1. `coreapp new --email mail-test`: `needs = ["email"]`, deploy. On the + Core, `.env` has `SMTP_HOST=mail.`, `SMTP_PORT=587`, + `SMTP_SECURITY=starttls`, `SMTP_USER=coreapp-mail-test`, a random + `SMTP_PASSWORD`, `SMTP_FROM=mail-test@`; `service` has + `DEPENDS="mail"`; LDAP has `uid=coreapp-mail-test,ou=people,` with + `objectClass` account, simpleSecurityObject, PostfixBookMailAccount (no + person), `mailEnabled: FALSE`, `description: coreapps: mail-test`, and + a `{SSHA}` password. +2. From a container on the `core` network with the app's `.env` + (`docker run --rm --network core --env-file /federated/apps/mail-test/.env ...`), + Python's `smtplib`: STARTTLS with certificate checking, login, `MAIL FROM` + `SMTP_FROM` and `RCPT TO postmaster@` both 250 (then `RSET`; nothing + sent). A wrong password: 535. +3. Authelia's `/api/firstfactor` as `coreapp-mail-test` with its real + password: 401. `occ user:list` in Nextcloud and Superpanel's + `user_store` (after `superpanel-sync-users`) don't have it. +4. `coreapp remove`: the LDAP entry is gone. +5. An LDAP entry `uid=coreapp-mail-test` that isn't the app's (another + `description`): deploy prints "Warning: mail-test can't send mail", the + entry is unchanged (its password too), and remove leaves it. +6. Adding `"email"` to an installed app's `needs` and deploying makes the + account and adds the `SMTP_*` settings. + ### With both 1. `coreapp new --postgres --sso team-visits`: `needs = ["postgres", diff --git a/bin/coreapp b/bin/coreapp index bbbc3a2..4407c56 100755 --- a/bin/coreapp +++ b/bin/coreapp @@ -3,9 +3,9 @@ # Purpose : Make, deploy and test apps for Federated Core, against the Core # in coreapp-dev's VM (coreapp-vm). # -# coreapp new [--postgres] [--sso] +# coreapp new [--postgres] [--sso] [--email] # make a new app in ./ (with its own database, -# sign-in through the Core's Authelia, or both) +# sign-in through the Core's Authelia, sending mail) # coreapp deploy [--recreate] # build, copy to the Core, install or update # (--recreate: a new container even if nothing changed) @@ -154,19 +154,20 @@ apply_overlay() { } cmd_new() { - local name="" postgres="" sso="" title dbname f needs="" what="" + local name="" postgres="" sso="" email="" title dbname f needs="" what="" local overlays=() while [[ $# -gt 0 ]]; do case "$1" in --postgres) postgres=1 ;; --sso) sso=1 ;; - -*) die "Unknown option $1. Usage: coreapp new [--postgres] [--sso] " ;; + --email) email=1 ;; + -*) die "Unknown option $1. Usage: coreapp new [--postgres] [--sso] [--email] " ;; *) name="$1" ;; esac shift done [[ -d "$TEMPLATE" ]] || die "The app template is missing: $TEMPLATE" - [[ -n "$name" ]] || die "Usage: coreapp new [--postgres] [--sso] (lowercase letters, digits and -)" + [[ -n "$name" ]] || die "Usage: coreapp new [--postgres] [--sso] [--email] (lowercase letters, digits and -)" [[ "$name" =~ ^[a-z]([a-z0-9-]{0,29}[a-z0-9])$ ]] \ || die "'$name': use 2-31 lowercase letters, digits and -, starting with a letter and not ending with -. It becomes https://$name.." [[ ! -e "$name" ]] || die "./$name already exists." @@ -189,6 +190,8 @@ cmd_new() { apply_overlay "$name" "$f" [[ -f "$f/.needs" ]] && needs="$needs, \"$(cat "$f/.needs")\"" done + # No example app for email: the settings are in README.md + [[ -n "$email" ]] && needs="$needs, \"email\"" sed -i "s/^needs = .*/needs = [${needs#, }]/" "$name/coreapp.toml" # The example apps with a database or sign-in have a /health path [[ ${#overlays[@]} -gt 0 ]] && sed -i 's|^health = .*|health = "/health"|' "$name/coreapp.toml" @@ -199,6 +202,7 @@ cmd_new() { [[ -n "$postgres" ]] && what=", with a database" [[ -n "$sso" ]] && what="$what, with sign-in through Authelia" + [[ -n "$email" ]] && what="$what, sending mail" echo "Made $title in ./$name$what." echo "Next: cd $name && coreapp deploy" } diff --git a/core/coreapps b/core/coreapps index 9145121..721b896 100755 --- a/core/coreapps +++ b/core/coreapps @@ -67,7 +67,7 @@ FIRST, LAST = 2, 254 RESERVED = {"www", "mail", "ns1", "ns2", "autodiscover", "autoconfig", "panel", "dashboard", "mcphub", "api", "admin"} # What an app can ask for, in the order they're set up -NEEDS = ["postgres", "sso"] +NEEDS = ["postgres", "sso", "email"] # What coreapp.toml may contain. Values end up in shell-sourced and YAML # files on the Core, so each is limited to characters that are safe there. @@ -768,6 +768,7 @@ def install_app(m, p, folder, origin, addon_ok, recreate): compose_up(m, log, recreate) wait_until_answering(m, p) + check_needs(m) if p["state"] == "update": remove_old_images(m) say(f"{'Installed' if p['state'] == 'new' else 'Updated'}: {p['url']}") @@ -797,6 +798,20 @@ def remove_images(image, keep=None): run(["docker", "image", "rm", f"{short_name(image)}:{tag}"]) +def check_needs(m): + """Say so if a need couldn't be set up (its setup only writes to the + Core's install log).""" + if "email" in m["needs"]: + out = run(["bash", "-c", ( + 'B=$(sed -n "s/^LDAP_SEARCH_BASE=ou=people,//p" /federated/apps/mail/.env); ' + 'docker exec ldap ldapsearch -LLL -x -H ldap://localhost -D "cn=admin,$B" ' + '-w "$(cat /federated/apps/ldap/.ldap.secret)" ' + f'-b "uid=coreapp-{m["name"]},ou=people,$B" -s base description')]) + if f"description: coreapps: {m['name']}" not in out.stdout: + say(f"Warning: {m['name']} can't send mail: LDAP has another entry with uid " + f"coreapp-{m['name']} (or its address), which was left alone.") + + def remove_old_images(m): """The app's images with other versions, now that this one runs.""" remove_images(m["image"], keep=m["version"]) diff --git a/core/needs/email/depends b/core/needs/email/depends new file mode 100644 index 0000000..fa7963c --- /dev/null +++ b/core/needs/email/depends @@ -0,0 +1 @@ +mail diff --git a/core/needs/email/env.in b/core/needs/email/env.in new file mode 100644 index 0000000..f05ccb7 --- /dev/null +++ b/core/needs/email/env.in @@ -0,0 +1,8 @@ +# Sending mail through the Core's mail server with its own account (made +# by needs/email/setup): STARTTLS on port 587, user and password below +SMTP_HOST=mail.@DOMAIN@ +SMTP_PORT=587 +SMTP_SECURITY=starttls +SMTP_USER=coreapp-{{NAME}} +SMTP_PASSWORD=@SMTP_PASSWORD:RANDOM:LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL@ +SMTP_FROM={{NAME}}@@DOMAIN@ diff --git a/core/needs/email/setup b/core/needs/email/setup new file mode 100644 index 0000000..868847a --- /dev/null +++ b/core/needs/email/setup @@ -0,0 +1,39 @@ +# Its own mail account in the Core's LDAP, for sending only, with the +# password from .env. Not a person (objectClass account, not +# inetOrgPerson), so it can't sign in to Authelia, Nextcloud or Superpanel; +# the mail server checks its password with LDAP. mailEnabled FALSE: no +# mailbox. Its address, coreapp-{{NAME}}@, is its own, so a +# person's mail login never finds two entries. Safe to run again. +LDAP_BASE="$(sed -n 's/^LDAP_SEARCH_BASE=ou=people,//p' /federated/apps/mail/.env)" +LDAP_ADMIN_PW="$(cat /federated/apps/ldap/.ldap.secret)" +MAIL_DN="uid=coreapp-{{NAME}},ou=people,$LDAP_BASE" +SMTP_PASSWORD="$(sed -n 's/^SMTP_PASSWORD=//p' /federated/apps/{{NAME}}/.env)" +# Entries with its uid or address: none, or only its own +MAIL_FOUND="$(docker exec ldap ldapsearch -LLL -o ldif-wrap=no -x -H ldap://localhost \ + -D "cn=admin,$LDAP_BASE" -w "$LDAP_ADMIN_PW" -b "$LDAP_BASE" \ + "(|(uid=coreapp-{{NAME}})(mail=coreapp-{{NAME}}@$DOMAIN))" dn description)" +if [ -z "$MAIL_FOUND" ]; then + docker exec -i ldap ldapadd -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" -w "$LDAP_ADMIN_PW" \ + > /dev/null <&2 +fi +unset LDAP_BASE LDAP_ADMIN_PW MAIL_DN SMTP_PASSWORD MAIL_FOUND diff --git a/core/needs/email/teardown b/core/needs/email/teardown new file mode 100644 index 0000000..297b890 --- /dev/null +++ b/core/needs/email/teardown @@ -0,0 +1,11 @@ +# Delete its mail account from the Core's LDAP (only if it's this app's) +LDAP_BASE="$(sed -n 's/^LDAP_SEARCH_BASE=ou=people,//p' /federated/apps/mail/.env)" +LDAP_ADMIN_PW="$(cat /federated/apps/ldap/.ldap.secret)" +MAIL_DN="uid=coreapp-{{NAME}},ou=people,$LDAP_BASE" +if docker exec ldap ldapsearch -LLL -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" \ + -w "$LDAP_ADMIN_PW" -b "$MAIL_DN" -s base description 2>/dev/null \ + | grep -qx "description: coreapps: {{NAME}}"; then + docker exec ldap ldapdelete -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" \ + -w "$LDAP_ADMIN_PW" "$MAIL_DN" +fi +unset LDAP_BASE LDAP_ADMIN_PW MAIL_DN diff --git a/skills/coreapp/SKILL.md b/skills/coreapp/SKILL.md index 661eaed..8601b17 100644 --- a/skills/coreapp/SKILL.md +++ b/skills/coreapp/SKILL.md @@ -67,7 +67,7 @@ image = "localhost/coreapp/my-app" # image name; the Core runs image:version version = "0.1.0" # the image tag; raise it for each release port = 8080 # the port the app serves HTTP on (any; 8080 is usual) health = "/health" # a path that answers once it's up -needs = ["postgres", "sso"] # what it needs from the Core, or [] +needs = ["postgres", "sso"] # what it needs from the Core, or [] ("email" too) ``` The installer is strict, because these values end up in files on the Core @@ -147,9 +147,29 @@ has one: most self-hosted apps have "generic OIDC" settings that map onto these variables. For a standard-library Python example of the whole flow, `coreapp new --sso example` makes one (`oidc.py`). +### `email`: sending mail + +The Core makes a mail account just for this app, for sending (no inbox), +and adds: + +| Variable | Value | +|---|---| +| `SMTP_HOST` | `mail.` (the Core's mail server; valid certificate) | +| `SMTP_PORT` | `587` | +| `SMTP_SECURITY` | `starttls` (connect, then STARTTLS; not implicit TLS) | +| `SMTP_USER` | `coreapp-` | +| `SMTP_PASSWORD` | random | +| `SMTP_FROM` | `@`, the address to send from | + +Most apps have SMTP settings; map these onto them. Mail to `SMTP_FROM` +isn't delivered anywhere (the account has no mailbox), so set a reply-to +if people should be able to answer. The account can't sign in to anything +else on the Core. `coreapp new --email` adds the need (there's no example +app for it). + ### Not available yet -Email (sending mail through the Core), the Core's AI features (offering +The Core's AI features (offering tools to its AI agent, or using its model), extra services such as Redis or a second container, extra hostnames, and public ports other than HTTPS. If the app can't work without one of these, say so plainly in your report @@ -193,6 +213,8 @@ and make the Dockerfile fit the Core: - **Keep state in `/data`**: point upload folders, SQLite files and generated keys there. If the app needs a directory structure under `/data`, create it at startup (it starts empty). +- **Send mail with `needs = ["email"]`**, if the app sends any (password + resets, notifications): map `SMTP_*` onto its mail settings. - **Read settings from the environment**, mapping the Core's names to the app's in an entrypoint script or config file if they differ (`SECRET_KEY="$APP_SECRET"`, `DATABASE_URL=...`). diff --git a/templates/app/README.md b/templates/app/README.md index 4f81f49..4d20bc8 100644 --- a/templates/app/README.md +++ b/templates/app/README.md @@ -42,7 +42,10 @@ keep its values and add new ones): `needs = ["postgres"]` adds `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER` and `DB_PASSWORD`; `needs = ["sso"]` adds `OIDC_ISSUER`, `OIDC_CLIENT_ID` and -`OIDC_CLIENT_SECRET`. +`OIDC_CLIENT_SECRET`; `needs = ["email"]` adds `SMTP_HOST`, `SMTP_PORT` +(587), `SMTP_SECURITY` (`starttls`), `SMTP_USER`, `SMTP_PASSWORD` and +`SMTP_FROM` (`{{NAME}}@`): the app's own account on the Core's mail +server, for sending. Its files: `/data` in the container is kept on the Core (`/federated/apps/{{NAME}}/data`) across updates and restarts. diff --git a/templates/app/coreapp.toml b/templates/app/coreapp.toml index a63c246..2d087aa 100644 --- a/templates/app/coreapp.toml +++ b/templates/app/coreapp.toml @@ -19,5 +19,5 @@ port = 8080 health = "/" # What it needs from the Core: "postgres" (its own database), "sso" -# (sign-in with Core accounts through Authelia) +# (sign-in with Core accounts through Authelia), "email" (sending mail) needs = []