From 8ef2112e3c2ff2fb0fd1996459f52fe7eed053da Mon Sep 17 00:00:00 2001 From: David Young Date: Thu, 3 Dec 2026 15:15:23 -0700 Subject: [PATCH] Add the mysql need: its own database on the Core's MariaDB needs = ["mysql"] (coreapp new --mysql) makes a user and database coreapp_ on pdnsmysql, with the same DB_* settings as postgres (so an app has one or the other). Root's password is read inside the container, so it stays out of installapp's set -x log. Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 13 ++++++++++++- README.md | 3 ++- TEST.md | 17 +++++++++++++++++ bin/coreapp | 15 +++++++++------ core/coreapps | 5 ++++- core/needs/mysql/depends | 1 + core/needs/mysql/env.in | 6 ++++++ core/needs/mysql/setup | 13 +++++++++++++ core/needs/mysql/teardown | 5 +++++ skills/coreapp/SKILL.md | 25 ++++++++++++++++++++++--- templates/app/README.md | 2 +- templates/app/coreapp.toml | 2 +- 12 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 core/needs/mysql/depends create mode 100644 core/needs/mysql/env.in create mode 100644 core/needs/mysql/setup create mode 100644 core/needs/mysql/teardown diff --git a/PLAN.md b/PLAN.md index e38dcb7..535fb72 100644 --- a/PLAN.md +++ b/PLAN.md @@ -320,6 +320,17 @@ Steps: 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 + - MySQL (2026-12-03): `needs = ["mysql"]` (`coreapp new --mysql`): a + user and database `coreapp_` (utf8mb4) on `pdnsmysql` (MariaDB + 12.1, shared with PowerDNS), as Federated's apps do (e.g. + `lib/bookstack.sh`); same `DB_*` names as postgres, so an app has one + or the other. Root's password is read inside the container + (`$MYSQL_ROOT_PASSWORD`), not by the script, so it isn't in the + `set -x` log (Federated's `functions.sh` logs it anyway as + `MYSQL_ROOTPASSWORD=`, for every install: see "Logs"). Tested on + dev.fedcom.net: both needs refused, install, a table made and read from + the core network, no access to `powerdns`, wrong password refused, + remove drops the user and database, reinstall - [ ] 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`, @@ -355,7 +366,7 @@ Their points, and where coreapp-dev stands: done - MySQL apps use the `pdnsmysql` container (MariaDB, 192.168.0.10, also PowerDNS's database), PostgreSQL apps `postgresql`: postgres is done; - [ ] a `mysql` need (its own user and database there) + done (`mysql` need, Coreapps step 4) - Authelia through `installapp-sso` / `configsso_`: needs a `lib/.sh`, so not used; the sso need does the same, and keeps the client copy Federated's apps keep diff --git a/README.md b/README.md index 1de09af..e312f94 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; "email": sending mail +needs = ["postgres", "sso"] # its own database ("mysql" for MariaDB); 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 --mysql ` | (Instead of `--postgres`, with no example app.) Its own database on the Core's MariaDB | | `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 | diff --git a/TEST.md b/TEST.md index 8819db6..8c788bb 100644 --- a/TEST.md +++ b/TEST.md @@ -270,6 +270,23 @@ 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 mysql + +1. `needs = ["postgres", "mysql"]`: refused ("postgres or mysql, not + both"); `coreapp new --postgres --mysql x` too. +2. `coreapp new --mysql my-test`: `needs = ["mysql"]`, deploy. `.env` has + `DB_HOST=pdnsmysql.`, `DB_PORT=3306`, `DB_NAME` and `DB_USER` + `coreapp_my_test`, a random `DB_PASSWORD`; `service` has + `DEPENDS="pdnsmysql"`. +3. `docker run --rm --network core --env-file /federated/apps/my-test/.env + mariadb:12.1.2 sh -c 'mariadb -h$DB_HOST -u$DB_USER -p$DB_PASSWORD $DB_NAME ...'`: + create, insert, select work; `show databases` lists only its own; + `@@character_set_database` is `utf8mb4`; `powerdns` is denied (1044); + a wrong password is denied (1045). +4. The install log (`/federated/logs/coreapps/my-test-install.log`) has + root's password only on Federated's `MYSQL_ROOTPASSWORD=` lines. +5. `coreapp remove`: no `coreapp%` user or database left in MariaDB. + ### With email 1. `coreapp new --email mail-test`: `needs = ["email"]`, deploy. On the diff --git a/bin/coreapp b/bin/coreapp index 4407c56..091d2bf 100755 --- a/bin/coreapp +++ b/bin/coreapp @@ -3,7 +3,7 @@ # Purpose : Make, deploy and test apps for Federated Core, against the Core # in coreapp-dev's VM (coreapp-vm). # -# coreapp new [--postgres] [--sso] [--email] +# coreapp new [--postgres | --mysql] [--sso] [--email] # make a new app in ./ (with its own database, # sign-in through the Core's Authelia, sending mail) # coreapp deploy [--recreate] @@ -154,20 +154,22 @@ apply_overlay() { } cmd_new() { - local name="" postgres="" sso="" email="" title dbname f needs="" what="" + local name="" postgres="" mysql="" sso="" email="" title dbname f needs="" what="" local overlays=() while [[ $# -gt 0 ]]; do case "$1" in --postgres) postgres=1 ;; + --mysql) mysql=1 ;; --sso) sso=1 ;; --email) email=1 ;; - -*) die "Unknown option $1. Usage: coreapp new [--postgres] [--sso] [--email] " ;; + -*) die "Unknown option $1. Usage: coreapp new [--postgres | --mysql] [--sso] [--email] " ;; *) name="$1" ;; esac shift done [[ -d "$TEMPLATE" ]] || die "The app template is missing: $TEMPLATE" - [[ -n "$name" ]] || die "Usage: coreapp new [--postgres] [--sso] [--email] (lowercase letters, digits and -)" + [[ -n "$name" ]] || die "Usage: coreapp new [--postgres | --mysql] [--sso] [--email] (lowercase letters, digits and -)" + [[ -z "$postgres" || -z "$mysql" ]] || die "--postgres or --mysql, not both." [[ "$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." @@ -190,7 +192,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 + # No example apps for mysql and email: the settings are in README.md + [[ -n "$mysql" ]] && needs="\"mysql\"$needs" [[ -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 @@ -200,7 +203,7 @@ cmd_new() { sed -i -e "s/{{NAME}}/$name/g" -e "s/{{TITLE}}/$title/g" -e "s/{{DBNAME}}/$dbname/g" "$f" done < <(find "$name" -type f ! -name '*.png' -print0) - [[ -n "$postgres" ]] && what=", with a database" + [[ -n "$postgres$mysql" ]] && 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." diff --git a/core/coreapps b/core/coreapps index 721b896..0057297 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", "email"] +NEEDS = ["postgres", "mysql", "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. @@ -150,6 +150,9 @@ def read_manifest(folder): raise Refused(f"coreapp.toml: needs: unknown {', '.join(bad)} " f"(it knows {', '.join(NEEDS)}).") value = [n for n in NEEDS if n in value] + if "postgres" in value and "mysql" in value: + raise Refused("coreapp.toml: needs: postgres or mysql, not both " + "(both give the app DB_* settings).") else: if not isinstance(value, str) or not re.fullmatch(FIELDS[key], value): raise Refused(f"coreapp.toml: {key} = {value!r} isn't allowed. " diff --git a/core/needs/mysql/depends b/core/needs/mysql/depends new file mode 100644 index 0000000..6f277bf --- /dev/null +++ b/core/needs/mysql/depends @@ -0,0 +1 @@ +pdnsmysql diff --git a/core/needs/mysql/env.in b/core/needs/mysql/env.in new file mode 100644 index 0000000..e15fbe2 --- /dev/null +++ b/core/needs/mysql/env.in @@ -0,0 +1,6 @@ +# Its database on the Core's MariaDB (pdnsmysql), made by needs/mysql/setup +DB_HOST=pdnsmysql.@DOMAIN@ +DB_PORT=3306 +DB_NAME={{DBNAME}} +DB_USER={{DBNAME}} +DB_PASSWORD=@DB_PASSWORD:RANDOM:LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL@ diff --git a/core/needs/mysql/setup b/core/needs/mysql/setup new file mode 100644 index 0000000..c8cd45a --- /dev/null +++ b/core/needs/mysql/setup @@ -0,0 +1,13 @@ +# Its own database and user on the Core's MariaDB (pdnsmysql, shared with +# PowerDNS and Federated's apps), with the password from .env. Safe to run +# again: an existing user gets the password, an existing database is kept. +# Root's password is read inside the container (its MYSQL_ROOT_PASSWORD), so +# it's not on a command line or in installapp's set -x log. +DB_PASSWORD="$(sed -n 's/^DB_PASSWORD=//p' /federated/apps/{{NAME}}/.env)" +docker exec -i pdnsmysql sh -c 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mariadb -uroot' <` (resolves only on the Core's network) | +| `DB_PORT` | `3306` | +| `DB_NAME`, `DB_USER` | `coreapp_` with `-` as `_` | +| `DB_PASSWORD` | random | + +The database is `utf8mb4`; the app has all privileges on it and nothing +else. The rest is as for `postgres` (migrations, retrying at boot; a URL +is `mysql://$DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME`). +`coreapp new --mysql` adds the need (there's no example app for it). + ### `sso`: sign-in with Core accounts The Core registers the app as an OpenID Connect client with its Authelia, @@ -231,8 +250,8 @@ and make the Dockerfile fit the Core: - **Answer on `health`** once ready (a dedicated health endpoint, or `/` if it returns 2xx-4xx without signing in). - **One container.** If the upstream project ships a compose file with - several services, keep only the app, use `needs = ["postgres"]` for its - database, and stop if it needs other services (see "Not available yet"). + several services, keep only the app, use `needs = ["postgres"]` (or + `["mysql"]` for MySQL/MariaDB) for its database, and stop if it needs other services (see "Not available yet"). Keep the app's other files as they are; add `.dockerignore` entries for `coreapp.toml`, `icon.png` and `.git` so they don't bust the build cache. diff --git a/templates/app/README.md b/templates/app/README.md index 4d20bc8..c474ed1 100644 --- a/templates/app/README.md +++ b/templates/app/README.md @@ -41,7 +41,7 @@ keep its values and add new ones): | `IMAGE_VERSION` | The version that's installed | `needs = ["postgres"]` adds `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER` and -`DB_PASSWORD`; `needs = ["sso"]` adds `OIDC_ISSUER`, `OIDC_CLIENT_ID` and +`DB_PASSWORD` (`needs = ["mysql"]` the same, for MariaDB); `needs = ["sso"]` adds `OIDC_ISSUER`, `OIDC_CLIENT_ID` and `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 diff --git a/templates/app/coreapp.toml b/templates/app/coreapp.toml index 2d087aa..7b1bc4e 100644 --- a/templates/app/coreapp.toml +++ b/templates/app/coreapp.toml @@ -18,6 +18,6 @@ version = "0.1.0" port = 8080 health = "/" -# What it needs from the Core: "postgres" (its own database), "sso" +# What it needs from the Core: "postgres" or "mysql" (its own database), "sso" # (sign-in with Core accounts through Authelia), "email" (sending mail) needs = []