Let an app ask for PostgreSQL extensions its user can't make
coreapp.toml's [postgres] extensions = ["vector"]: the installer checks the Core's PostgreSQL has each one (refusing before anything changes if not) and makes them in the app's database as the superuser, with CASCADE, on install and every update. Ones taken off the list are kept, since the data may use them. Also fixes the check refusing needs with both postgres and mysql, which never ran. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+53
-3
@@ -98,6 +98,7 @@ FIELDS = {
|
||||
"docs": r"(?:https://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[A-Za-z0-9._~/%+-]*)?)?",
|
||||
"needs": None,
|
||||
"sso": None,
|
||||
"postgres": None,
|
||||
}
|
||||
REQUIRED = ["name", "title", "version", "image", "port"]
|
||||
|
||||
@@ -108,6 +109,11 @@ 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"]
|
||||
EXTENSION = r"[a-z0-9][a-z0-9_-]{0,62}"
|
||||
|
||||
# Repositories apps are installed from: https only, no user or password in
|
||||
# the URL. Tags: v + a version; releases (picked by default) are numeric.
|
||||
REPO_URL = r"https://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[A-Za-z0-9._-]+){2,}?(?:\.git)?/?"
|
||||
@@ -156,6 +162,22 @@ def read_sso(value):
|
||||
return sso
|
||||
|
||||
|
||||
def read_postgres(value):
|
||||
"""coreapp.toml's [postgres] table, checked."""
|
||||
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))
|
||||
if unknown:
|
||||
raise Refused(f"coreapp.toml: [postgres]: unknown setting(s) {', '.join(unknown)} "
|
||||
f"(it knows {', '.join(POSTGRES_SETTINGS)}).")
|
||||
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))}
|
||||
|
||||
|
||||
def read_manifest(folder):
|
||||
"""coreapp.toml in folder, checked, with defaults filled in."""
|
||||
path = os.path.join(folder, "coreapp.toml")
|
||||
@@ -177,7 +199,8 @@ def read_manifest(folder):
|
||||
if missing:
|
||||
raise Refused(f"coreapp.toml needs {', '.join(missing)}.")
|
||||
|
||||
m = {"description": "", "health": "/", "docs": "", "needs": [], "sso": dict(SSO_DEFAULTS)}
|
||||
m = {"description": "", "health": "/", "docs": "", "needs": [], "sso": dict(SSO_DEFAULTS),
|
||||
"postgres": {"extensions": []}}
|
||||
for key, value in data.items():
|
||||
if key == "port":
|
||||
if not isinstance(value, int) or isinstance(value, bool) or not 1 <= value <= 65535:
|
||||
@@ -190,11 +213,13 @@ 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]
|
||||
elif key == "sso":
|
||||
value = read_sso(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).")
|
||||
elif key == "sso":
|
||||
value = read_sso(value)
|
||||
elif key == "postgres":
|
||||
value = read_postgres(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. "
|
||||
@@ -202,6 +227,8 @@ 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.')
|
||||
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("-", "_")
|
||||
@@ -596,12 +623,31 @@ def plan(m, origin=None, downgrade=False, drop_database=False):
|
||||
else:
|
||||
p["new_needs"] = list(m["needs"])
|
||||
p["dropped_needs"] = []
|
||||
p["new_extensions"] = extensions_to_enable(m, p)
|
||||
for key in ("source", "tag", "commit"):
|
||||
if origin:
|
||||
p[key] = origin[key]
|
||||
return p
|
||||
|
||||
|
||||
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."""
|
||||
wanted = m["postgres"]["extensions"]
|
||||
if not wanted:
|
||||
return []
|
||||
psql = ["docker", "exec", "postgresql", "psql", "-U", "postgres", "-Atc"]
|
||||
available = run(psql + ["SELECT name FROM pg_available_extensions"]).stdout.split()
|
||||
missing = [e for e in wanted if e not in available]
|
||||
if missing:
|
||||
raise Refused(f"coreapp.toml: [postgres] extensions: this Core's PostgreSQL doesn't have "
|
||||
f"{', '.join(missing)} (coreapp core lists the ones it has).")
|
||||
have = []
|
||||
if p["state"] == "update" and "postgres" not in p["new_needs"]:
|
||||
have = run(psql + ["SELECT extname FROM pg_extension", "-d", m["dbname"]]).stdout.split()
|
||||
return [e for e in wanted if e not in have]
|
||||
|
||||
|
||||
# --- Making the app's files ---------------------------------------------------
|
||||
|
||||
def read_text(*parts):
|
||||
@@ -671,6 +717,7 @@ def make_service_folder(m, p, source, origin):
|
||||
"SSO_PKCE": ("require_pkce: true\n pkce_challenge_method: 'S256'"
|
||||
if m["sso"]["pkce"] else "require_pkce: false"),
|
||||
"DEPENDS": " ".join(read_text(NEEDS_DIR, n, "depends").strip() for n in m["needs"]),
|
||||
"EXTENSIONS": " ".join(m["postgres"]["extensions"]),
|
||||
}
|
||||
compose_extra = "".join(read_text(NEEDS_DIR, n, "compose") for n in m["needs"])
|
||||
compose = read_text(TEMPLATES, "docker-compose.yml.in").replace(
|
||||
@@ -877,6 +924,9 @@ def install_app(m, p, folder, origin, addon_ok, recreate):
|
||||
if state != "ok":
|
||||
say(f"Note: {note}")
|
||||
|
||||
if p["new_extensions"]:
|
||||
say(f"Enabling PostgreSQL extension(s) {', '.join(p['new_extensions'])} in "
|
||||
f"{name}'s database (as the Core's superuser).")
|
||||
if p["state"] == "new":
|
||||
make_service_folder(m, p, folder, origin)
|
||||
say(f"Installing {name} at {p['address']} (the Core's installapp)...")
|
||||
|
||||
Reference in New Issue
Block a user