diff --git a/PLAN.md b/PLAN.md index 4996775..ed2d7ac 100644 --- a/PLAN.md +++ b/PLAN.md @@ -497,9 +497,12 @@ Steps: pg_trgm and earthdistance, a refused postgis, and a rollback that kept them. Also fixed: `needs` with both postgres and mysql was meant to be refused, but the check never ran - - [ ] PostgreSQL 18 on the Core (Federated's), or a minimum version - in coreapp.toml that the installer checks, so an app refuses to - install on an older Core instead of failing later + - Done 2026-12-03: `[postgres]` / `[mysql]` `min_version` in + coreapp.toml: the installer compares it with the Core's server + (numerically) on install and update, and refuses an older one + with both versions. Tested on dev.fedcom.net (PostgreSQL 17.6, + MariaDB 12.1.2) + - [ ] PostgreSQL 18 on the Core (Federated's; David is asking) - [ ] David: the agent's changes, for Supermarket's own repository (and a login CSRF it noticed in the app's sign-in: its OIDC state isn't tied to a browser cookie) diff --git a/README.md b/README.md index 51573bb..6952ab5 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,9 @@ callback = "/auth/openid/core" # default "/auth/callback" pkce = false # default true token_auth = "client_secret_post" # default "client_secret_basic" -[postgres] # optional: extensions only a superuser can make -extensions = ["vector"] +[postgres] # optional ([mysql] has min_version too) +min_version = "16" # an older Core's PostgreSQL refuses the app +extensions = ["vector"] # ones only a superuser can make ``` The app has no scripts or compose file: the Core side is made by diff --git a/TEST.md b/TEST.md index f8d962a..2b7027d 100644 --- a/TEST.md +++ b/TEST.md @@ -233,6 +233,15 @@ The Core's own scripts trace secrets, so their output goes to the table and its index. `[postgres]` without `"postgres"` in needs, an unknown setting or a bad name is refused; so is `needs` with both postgres and mysql. +5d. `min_version`: on the Core's PostgreSQL 17.6, a new app with + `[postgres]` `min_version = "18"` is refused before anything is built + ("Vectest needs PostgreSQL 18 or newer ...; this Core has 17.6"); + `"17"` installs; raising it to `"18"` in an update is refused and the + app keeps running. A `--mysql` app with `[mysql]` `min_version = "13"` + is refused ("needs MariaDB 13 or newer ...; this Core has 12.1.2"). + Comparison is numeric (`"17.10"` is newer than 17.6); `min_version = + 17.4` (a TOML number with a dot) and `">=18"` are refused with a hint; + `[mysql]` has no `extensions`. 6. `coreapp status` (address and port), `coreapp logs`, `coreapp shell` (exit with `exit`), `coreapp shell 'id; echo $APP_URL'` (prints both, no terminal needed, also when piped), `coreapp list` (all apps installed diff --git a/core/coreapps b/core/coreapps index bb494bd..e9c717e 100755 --- a/core/coreapps +++ b/core/coreapps @@ -99,6 +99,7 @@ FIELDS = { "needs": None, "sso": None, "postgres": None, + "mysql": None, } REQUIRED = ["name", "title", "version", "image", "port"] @@ -109,9 +110,13 @@ SSO_DEFAULTS = {"callback": "/auth/callback", "pkce": True, SSO_CALLBACK = r"/[A-Za-z0-9._~/-]{0,200}" SSO_TOKEN_AUTH = ["client_secret_basic", "client_secret_post"] -# [postgres]: extensions the Core's superuser enables in the app's database -# (ones the app's own user can't, like vector) -POSTGRES_SETTINGS = ["extensions"] +# [postgres] and [mysql]: the oldest server version the app works with +# (the installer refuses an older Core), and for postgres the extensions +# the Core's superuser makes in the app's database (ones the app's own user +# can't, like vector) +DB_SETTINGS = {"postgres": ["min_version", "extensions"], "mysql": ["min_version"]} +DB_SERVER = {"postgres": "PostgreSQL", "mysql": "MariaDB"} +MIN_VERSION = r"[0-9]{1,4}(?:\.[0-9]{1,4}){0,2}" EXTENSION = r"[a-z0-9][a-z0-9_-]{0,62}" # Repositories apps are installed from: https only, no user or password in @@ -162,20 +167,31 @@ def read_sso(value): return sso -def read_postgres(value): - """coreapp.toml's [postgres] table, checked.""" +def read_database(need, value): + """coreapp.toml's [postgres] or [mysql] table, checked.""" + known = DB_SETTINGS[need] if not isinstance(value, dict): - raise Refused('coreapp.toml: postgres must be a table: [postgres] with extensions = ["vector"].') - unknown = sorted(set(value) - set(POSTGRES_SETTINGS)) + raise Refused(f"coreapp.toml: {need} must be a table: [{need}] with {', '.join(known)}.") + unknown = sorted(set(value) - set(known)) if unknown: - raise Refused(f"coreapp.toml: [postgres]: unknown setting(s) {', '.join(unknown)} " - f"(it knows {', '.join(POSTGRES_SETTINGS)}).") + raise Refused(f"coreapp.toml: [{need}]: unknown setting(s) {', '.join(unknown)} " + f"(it knows {', '.join(known)}).") + db = {"min_version": "", "extensions": []} + if "min_version" in value: + version = value["min_version"] + if isinstance(version, int) and not isinstance(version, bool): + version = str(version) + if not isinstance(version, str) or not re.fullmatch(MIN_VERSION, version): + raise Refused(f'coreapp.toml: [{need}] min_version must be a {DB_SERVER[need]} ' + 'version, like min_version = "18" or "17.4".') + db["min_version"] = version extensions = value.get("extensions", []) if not isinstance(extensions, list) or not all( isinstance(e, str) and re.fullmatch(EXTENSION, e) for e in extensions): raise Refused('coreapp.toml: [postgres] extensions must be a list of extension names, ' 'like extensions = ["vector"].') - return {"extensions": sorted(set(extensions))} + db["extensions"] = sorted(set(extensions)) + return db def read_manifest(folder): @@ -200,7 +216,8 @@ def read_manifest(folder): raise Refused(f"coreapp.toml needs {', '.join(missing)}.") m = {"description": "", "health": "/", "docs": "", "needs": [], "sso": dict(SSO_DEFAULTS), - "postgres": {"extensions": []}} + "postgres": {"min_version": "", "extensions": []}, + "mysql": {"min_version": "", "extensions": []}} for key, value in data.items(): if key == "port": if not isinstance(value, int) or isinstance(value, bool) or not 1 <= value <= 65535: @@ -218,8 +235,8 @@ def read_manifest(folder): "(both give the app DB_* settings).") elif key == "sso": value = read_sso(value) - elif key == "postgres": - value = read_postgres(value) + elif key in DB_SETTINGS: + value = read_database(key, value) else: if not isinstance(value, str) or not re.fullmatch(FIELDS[key], value): raise Refused(f"coreapp.toml: {key} = {value!r} isn't allowed. " @@ -227,8 +244,9 @@ def read_manifest(folder): m[key] = value if "sso" in data and "sso" not in m["needs"]: raise Refused('coreapp.toml: [sso] is for apps with "sso" in needs.') - if "postgres" in data and "postgres" not in m["needs"]: - raise Refused('coreapp.toml: [postgres] is for apps with "postgres" in needs.') + for need in DB_SETTINGS: + if need in data and need not in m["needs"]: + raise Refused(f'coreapp.toml: [{need}] is for apps with "{need}" in needs.') if m["name"] in RESERVED: raise Refused(f"'{m['name']}' is one of the Core's own hostnames. Pick another name.") m["dbname"] = "coreapp_" + m["name"].replace("-", "_") @@ -623,6 +641,7 @@ def plan(m, origin=None, downgrade=False, drop_database=False): else: p["new_needs"] = list(m["needs"]) p["dropped_needs"] = [] + check_db_version(m) p["new_extensions"] = extensions_to_enable(m, p) for key in ("source", "tag", "commit"): if origin: @@ -630,6 +649,34 @@ def plan(m, origin=None, downgrade=False, drop_database=False): return p +def db_version(need): + """The Core's PostgreSQL or MariaDB version, like "17.6", or "".""" + if need == "postgres": + out = run(["docker", "exec", "postgresql", "postgres", "--version"]).stdout + found = re.search(r"\) ([0-9][0-9.]*[0-9])", out) + else: + out = run(["docker", "exec", "pdnsmysql", "mariadb", "--version"]).stdout + found = re.search(r"([0-9][0-9.]*[0-9])-MariaDB", out) + return found.group(1) if found else "" + + +def check_db_version(m): + """Refused if the app's database server here is older than its + [postgres] or [mysql] min_version.""" + for need in DB_SETTINGS: + wanted = m[need]["min_version"] + if need not in m["needs"] or not wanted: + continue + have = db_version(need) + if not have: + raise Refused(f"Couldn't find this Core's {DB_SERVER[need]} version to check " + f"[{need}] min_version = \"{wanted}\". Is it running?") + key = lambda v: [int(x) for x in v.split(".")] + if key(have) < key(wanted): + raise Refused(f"{m['title']} needs {DB_SERVER[need]} {wanted} or newer " + f"([{need}] min_version in coreapp.toml); this Core has {have}.") + + def extensions_to_enable(m, p): """[postgres] extensions the app's database doesn't have yet, or Refused if this Core's PostgreSQL doesn't have one.""" @@ -1433,9 +1480,7 @@ def core_info(): """The Core's services, versions and machine, as apps see them.""" domain, _ = federated_settings() rows = [("Domain", domain)] - pg = run(["docker", "exec", "postgresql", "postgres", "--version"]).stdout - found = re.search(r"\) ([0-9][0-9.]*)", pg) - rows.append(("PostgreSQL", (found.group(1) if found else "?") + ' (needs = ["postgres"])')) + rows.append(("PostgreSQL", (db_version("postgres") or "?") + ' (needs = ["postgres"])')) ext = run(["docker", "exec", "postgresql", "psql", "-U", "postgres", "-Atc", "SELECT v.trusted, string_agg(e.name || ' ' || e.default_version, ', ' ORDER BY e.name) " "FROM pg_available_extensions e JOIN pg_available_extension_versions v " @@ -1445,9 +1490,7 @@ def core_info(): trusted, _, names = line.partition("|") rows.append((" extensions the app can enable" if trusted == "t" else " extensions only the Core can enable", names)) - maria = run(["docker", "exec", "pdnsmysql", "mariadb", "--version"]).stdout - found = re.search(r"([0-9][0-9.]*)-MariaDB", maria) - rows.append(("MariaDB", (found.group(1) if found else "?") + ' (needs = ["mysql"])')) + rows.append(("MariaDB", (db_version("mysql") or "?") + ' (needs = ["mysql"])')) rows.append(("Authelia", image_tag("authelia") + ' (needs = ["sso"], OpenID Connect)')) rows.append(("Mail server", "docker-mailserver " + image_tag("mail") + ' (needs = ["email"], sending with SMTP and STARTTLS)')) diff --git a/skills/coreapp/SKILL.md b/skills/coreapp/SKILL.md index b146c8f..106b1b1 100644 --- a/skills/coreapp/SKILL.md +++ b/skills/coreapp/SKILL.md @@ -113,6 +113,16 @@ them with the Core. Check the app's requirements against them before choosing a need (its docs, the images in its own compose file, its migrations): a feature the Core's version lacks fails only when the code using it runs, so deploy, `coreapp check` and a sign-in can all pass. +If the app needs a newer database server than some Cores have, put its +oldest working version in `coreapp.toml`, so an older Core refuses to +install it (with both versions in the message) instead of breaking later: + +```toml +[postgres] +min_version = "16" # or [mysql] min_version = "11.4" (MariaDB's numbering) +``` + +Use what the app's docs or code require, not just today's version. | What | Today | For the app | |---|---|---| @@ -199,6 +209,7 @@ 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). +`[mysql] min_version` works as for postgres; there are no extensions. ### `sso`: sign-in with Core accounts @@ -268,7 +279,8 @@ works. Ask the user to confirm delivery to a real address. A newer database server than the Core's (an app that needs PostgreSQL 18: if the app also supports SQLite, that in `/data` is a fine choice, and say -so in your report), the Core's AI features (offering +so in your report; otherwise set `min_version`, which refuses on this +Core, and report it), 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 diff --git a/templates/app/README.md b/templates/app/README.md index 92ba437..03ead44 100644 --- a/templates/app/README.md +++ b/templates/app/README.md @@ -47,7 +47,8 @@ keep its values and add new ones): `needs = ["postgres"]` adds `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER` and `DB_PASSWORD` (`needs = ["mysql"]` the same, for MariaDB; PostgreSQL extensions only a superuser can make, like vector, go in `coreapp.toml`'s -`[postgres] extensions`); `needs = ["sso"]` adds `OIDC_ISSUER`, `OIDC_CLIENT_ID` and +`[postgres] extensions`, and the oldest server version it works with in +`min_version`); `needs = ["sso"]` adds `OIDC_ISSUER`, `OIDC_CLIENT_ID` and `OIDC_CLIENT_SECRET` (the client's callback, PKCE and secret method are in `coreapp.toml`'s `[sso]`); `needs = ["email"]` adds `SMTP_HOST`, `SMTP_PORT` (587), `SMTP_SECURITY` (`starttls`), `SMTP_USER`, `SMTP_PASSWORD` and diff --git a/templates/app/coreapp.toml b/templates/app/coreapp.toml index eeca218..eb16afb 100644 --- a/templates/app/coreapp.toml +++ b/templates/app/coreapp.toml @@ -30,7 +30,10 @@ needs = [] # pkce = true # whether it sends PKCE (S256) # token_auth = "client_secret_basic" # or "client_secret_post" -# With "postgres": extensions the app's database user can't make itself, -# made by the Core (coreapp core lists what it has) +# With "postgres" (or "mysql"): the oldest server version the app works +# with, so an older Core refuses it; for postgres, extensions the app's +# database user can't make itself, made by the Core (coreapp core lists +# the versions and extensions it has) # [postgres] +# min_version = "16" # extensions = ["vector"]