From f0468c43f229cb17d97a826bf9061a735958071e Mon Sep 17 00:00:00 2001 From: David Young Date: Thu, 3 Dec 2026 20:51:41 -0700 Subject: [PATCH] Let a Core keep a token for one owner's apps on a host coreapp login / saves a token used for that owner's repositories and images before the host's token, so apps from different accounts on one host can use different tokens. git sends it only under https:////; docker gets it for that pull only. Errors name the token that was tried. Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 9 ++- README.md | 9 ++- TEST.md | 15 +++++ bin/coreapp | 35 ++++++---- core/coreapps | 88 ++++++++++++++++--------- skills/coreapp/references/publishing.md | 6 +- 6 files changed, 114 insertions(+), 48 deletions(-) diff --git a/PLAN.md b/PLAN.md index 97cb1e4..ec6d83d 100644 --- a/PLAN.md +++ b/PLAN.md @@ -306,7 +306,14 @@ Steps: repository and package: installed at `https://pub-test.dev.fedcom.net/` (HTTP 200), `list` shows the source `@v0.1.0`, no docker login left on the Core - - [ ] Tokens per host+owner, if a Core needs several accounts on one host + - Done 2026-12-03: tokens per host+owner. `coreapp login + /` keeps a token for that owner's apps, used before the + host's (repository: its first path segment; image: the one after the + registry); git's header is scoped to `https:////`. + Tested on dev.fedcom.net with David's pub-test: a wrong owner token + is used first and fails with a hint naming it, another owner's + leaves the host's in use, logout, and a re-install pulled the image + with the host's token - [ ] Superpanel would manage these tokens - Done 2026-12-03: downgrades and digests. Installing an older release than the installed one is refused unless `--downgrade` (coreapps diff --git a/README.md b/README.md index 56a2673..6318912 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ each new app explains the files and the settings it gets. | `coreapp list` | The apps on the Core installed this way | | `coreapp install [--downgrade] [@tag]` | Install a published app from its repository on the Core, as its owner would: shows what it is and needs, asks, then installs (default: the newest release tag; an older release than the installed one only with `--downgrade`) | | `coreapp update ` | Update an app installed from a repository to its newest release | -| `coreapp login ` / `logout ` / `logins` | Give the Core a token for private apps from a host (asks for the user and token), forget it, list them | +| `coreapp login [/]` / `logout [/]` / `logins` | Give the Core a token for private apps from a host, or from one owner (user or organization) there (asks for the user and token), forget it, list them | | `coreapp publish [--platform ]` | Build the release image, push it to the registry in `coreapp.toml` (`image:version`, never replacing a published version) and tag the commit `v` | | `coreapp remove` | Uninstall it from the Core, with its database and sign-in client (asks first) | @@ -119,8 +119,11 @@ It reads only `coreapp.toml` and `icon.png` at the newest release tag `version`, and pulls the image from its registry. The repository and the image can be private: `coreapp login gitea.example.com` gives the Core a token for that host (for Gitea, one with read access to repositories and -packages), which the installer uses for the repository and the image. It -keeps tokens in `/etc/coreapps/credentials` on the Core (root only); docker +packages), which the installer uses for the repository and the image. +`coreapp login gitea.example.com/my-team` gives one for that owner's apps +only (used before the host's), for when apps from different accounts on +one host need different tokens; git sends it only under +`https://gitea.example.com/my-team/`. It keeps tokens in `/etc/coreapps/credentials` on the Core (root only); docker gets the token only for each pull, so the Core keeps no docker login. The Core runs the image it pulled at install, by its digest (`image:version@sha256:...`), so pushing the tag again can't change an diff --git a/TEST.md b/TEST.md index 39d1ce3..602d3f6 100644 --- a/TEST.md +++ b/TEST.md @@ -568,6 +568,21 @@ has a `coreapp.toml` with a published, public `image` (e.g. after token shown); on the Core `/etc/coreapps` is 700, `credentials` 600, and `/root/.docker/config.json` has no entry for the host. `coreapp logout `: installing fails again. +6a. Tokens per owner: `coreapp login /` with a wrong token is + rejected, the file unchanged. With a token saved for `/` + (on the Core, a wrong one written with `write_credentials`, to see which + is used), `coreapps plan https:////` uses it before + the host's: "Couldn't read ... (It used the token for /: + check it can read this repository.)"; with one for another owner, the + host's token is used and the plan works. `coreapp logins` shows + "/ (user ...; for 's apps)"; `logout + /` (any case) forgets it. Keys are lowercase; git's header + is scoped to `https:////` as the URL spells the owner + (`git config --get-urlmatch` gives it for `/Owner/app`, not for + `/owner/app`, `/Ownerx/app` or other owners). The image is pulled with + the owner's token (`//`), or else the host's; + re-installing pub-test with the host's token pulls it again (same + digest), with no docker login left. 7. `coreapp remove` in the app's folder (or `coreapps remove ` on the Core) removes it like any other. diff --git a/bin/coreapp b/bin/coreapp index 6af9d8a..c940dac 100755 --- a/bin/coreapp +++ b/bin/coreapp @@ -42,8 +42,10 @@ # installed one) # coreapp update update an app installed from a repository to its # newest release -# coreapp login give the Core a token for private apps from host -# (asks for the user and token); logout , +# coreapp login [/] +# give the Core a token for private apps from host +# (asks for the user and token; with /, for +# that owner's apps only); logout [/], # logins: forget one, list them # coreapp publish [--platform ] # build the release image and push it to the @@ -519,12 +521,20 @@ cmd_update() { } cmd_login() { - local host="${1:-}" user token - [[ $# -eq 1 && "$host" =~ ^[a-z0-9][a-z0-9.:-]*$ ]] \ - || die "Usage: coreapp login (e.g. coreapp login gitea.federated.computer)" - echo "The Core in '$VM' keeps this token to install private apps from $host:" - echo "their repositories and images. For Gitea, make a token for it with read" - echo "access to repositories and packages (Settings > Applications)." + local key="${1:-}" host user token + [[ $# -eq 1 && "$key" =~ ^[A-Za-z0-9][A-Za-z0-9.:-]*(/[A-Za-z0-9][A-Za-z0-9._-]*)?$ ]] \ + || die "Usage: coreapp login [/] (e.g. coreapp login gitea.federated.computer, or gitea.federated.computer/my-team for one owner's apps)" + host="${key%%/*}" + if [[ "$key" == */* ]]; then + echo "The Core in '$VM' keeps this token to install private apps from ${key#*/}" + echo "on $host (their repositories and images); other owners' apps there use" + echo "the token for $host, if it has one." + else + echo "The Core in '$VM' keeps this token to install private apps from $host:" + echo "their repositories and images." + fi + echo "For Gitea, make a token for it with read access to repositories and" + echo "packages (Settings > Applications)." read -r -p "User on $host: " user [[ "$user" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] || die "'$user' isn't a user name." read -r -s -p "Token: " token @@ -532,15 +542,16 @@ cmd_login() { init_core sync_installer # On stdin, so it's never in a command line - printf '%s\n' "$token" | core "$REMOTE_INSTALLER/coreapps login $host $user" + printf '%s\n' "$token" | core "$REMOTE_INSTALLER/coreapps login $key $user" } cmd_logout() { - local host="${1:-}" - [[ $# -eq 1 && "$host" =~ ^[a-z0-9][a-z0-9.:-]*$ ]] || die "Usage: coreapp logout " + local key="${1:-}" + [[ $# -eq 1 && "$key" =~ ^[A-Za-z0-9][A-Za-z0-9.:-]*(/[A-Za-z0-9][A-Za-z0-9._-]*)?$ ]] \ + || die "Usage: coreapp logout [/]" init_core sync_installer - core "$REMOTE_INSTALLER/coreapps logout $host" + core "$REMOTE_INSTALLER/coreapps logout $key" } cmd_logins() { diff --git a/core/coreapps b/core/coreapps index be40867..0aa4168 100755 --- a/core/coreapps +++ b/core/coreapps @@ -22,9 +22,12 @@ coreapps update [--addon-ok] update an app installed from a repository to its newest release coreapps remove uninstall an app installed by coreapps coreapps list the apps installed by coreapps -coreapps login keep a token (read from stdin) for +coreapps login [/] + keep a token (read from stdin) for installing private apps from host -coreapps logout forget host's token + (with /: that owner's apps only, + used before the host's token) +coreapps logout [/] forget that token coreapps logins the hosts this Core has tokens for coreapps mail what the Core's mail server did with the mail the app sent in the last day @@ -136,10 +139,14 @@ GIT_ENV = {"PATH": "/usr/bin:/bin", "GIT_TERMINAL_PROMPT": "0", "GIT_CONFIG_GLOBAL": "/dev/null", "HOME": "/nonexistent", "LC_ALL": "C"} GIT_TIMEOUT = 120 -# Tokens for private repositories and registries, by host (root only) +# Tokens for private repositories and registries (root only), by host, or +# by host/owner for one account or organization's apps on a host where +# others need another token CREDENTIALS = "/etc/coreapps/credentials" HOST = r"[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?(?::[0-9]{1,5})?" LOGIN_USER = r"[A-Za-z0-9][A-Za-z0-9._-]{0,63}" +OWNER = r"[a-z0-9][a-z0-9._-]{0,63}" +LOGIN_KEY = HOST + r"(?:/" + OWNER + r")?" TOKEN = r"[A-Za-z0-9._~+/=-]{8,512}" @@ -358,16 +365,17 @@ def read_credentials(): return {} except tomllib.TOMLDecodeError as e: raise Refused(f"{CREDENTIALS} isn't valid: {e}") - return {host: c for host, c in data.items() - if isinstance(c, dict) and re.fullmatch(LOGIN_USER, str(c.get("user", ""))) + return {key: c for key, c in data.items() + if re.fullmatch(LOGIN_KEY, key) and isinstance(c, dict) + and re.fullmatch(LOGIN_USER, str(c.get("user", ""))) and re.fullmatch(TOKEN, str(c.get("token", "")))} def write_credentials(creds): os.makedirs(os.path.dirname(CREDENTIALS), mode=0o700, exist_ok=True) os.chmod(os.path.dirname(CREDENTIALS), 0o700) - text = ("# Tokens coreapps uses to install private apps, by host. Manage with\n" - "# coreapps login / logout.\n") + text = ("# Tokens coreapps uses to install private apps, by host or host/owner\n" + "# (used first for that owner's apps). Manage with coreapps login / logout.\n") for host in sorted(creds): text += f'\n["{host}"]\nuser = "{creds[host]["user"]}"\ntoken = "{creds[host]["token"]}"\n' new = CREDENTIALS + ".new" @@ -377,12 +385,24 @@ def write_credentials(creds): os.rename(new, CREDENTIALS) -def basic_auth(host): - """'user:token' in base64 for host, or None.""" - c = read_credentials().get(host) - if not c: - return None - return base64.b64encode(f"{c['user']}:{c['token']}".encode()).decode() +def basic_auth(host, owner=""): + """(the login's key, 'user:token' in base64) for owner's apps on host: + host/owner's token, else host's; or (None, None).""" + creds = read_credentials() + for key in ([f"{host}/{owner.lower()}"] if owner else []) + [host]: + c = creds.get(key) + if c: + return key, base64.b64encode(f"{c['user']}:{c['token']}".encode()).decode() + return None, None + + +def login_key(arg): + """host or host/owner, checked and in lowercase.""" + key = arg.lower().rstrip("/") + if not re.fullmatch(LOGIN_KEY, key): + raise Refused(f"'{arg}' isn't a host name (like gitea.example.com) or a host and " + "owner (gitea.example.com/my-team).") + return key def url_host(url): @@ -420,10 +440,9 @@ def check_token(host, user, token): return False if status in (401, 403) else None -def login(host, user): - host = host.lower() - if not re.fullmatch(HOST, host): - raise Refused(f"'{host}' isn't a host name (like gitea.example.com).") +def login(arg, user): + key = login_key(arg) + host = key.split("/", 1)[0] if not re.fullmatch(LOGIN_USER, user): raise Refused(f"'{user}' isn't a user name.") token = sys.stdin.readline().strip() @@ -433,20 +452,22 @@ def login(host, user): if ok is False: raise Refused(f"{host} rejected the token for {user}. Nothing was saved.") creds = read_credentials() - creds[host] = {"user": user, "token": token} + creds[key] = {"user": user, "token": token} write_credentials(creds) say(f"Saved a token for {user}@{host}" + + (f", for {key.split('/', 1)[1]}'s apps there" if "/" in key else "") + (" (its registry accepts it)." if ok else " (couldn't check it with its registry).")) return 0 -def logout(host): +def logout(arg): + key = login_key(arg) creds = read_credentials() - if creds.pop(host.lower(), None) is None: - say(f"No token for {host}.") + if creds.pop(key, None) is None: + say(f"No token for {key}.") return 0 write_credentials(creds) - say(f"Forgot the token for {host}.") + say(f"Forgot the token for {key}.") return 0 @@ -454,8 +475,9 @@ def list_logins(): creds = read_credentials() if not creds: say("No tokens: this Core installs public apps only.") - for host in sorted(creds): - say(f"{host} (user {creds[host]['user']})") + for key in sorted(creds): + say(f"{key} (user {creds[key]['user']}" + + (f"; for {key.split('/', 1)[1]}'s apps)" if "/" in key else ")")) return 0 @@ -519,10 +541,16 @@ def fetch_repo(arg, staging): "https://host/owner/repo (no user or password in it).") url = normalize_url(url) host = url_host(url) - token = basic_auth(host) - auth = (f"https://{host}", token) if token else None - no_access = ("" if token else - f" If it's private, give this Core a token for {host}: coreapp login {host}.") + owner = url.split("://", 1)[1].split("/")[1] + key, token = basic_auth(host, owner) + # git sends it only under the login's address: the owner's (as the URL + # spells it; git compares paths exactly), or the host's + auth = ((f"https://{host}/{owner}" if "/" in key else f"https://{host}"), token) \ + if token else None + no_access = (f" If it's private, give this Core a token for {host}: coreapp login {host} " + f"(or coreapp login {host}/{owner.lower()} for {owner}'s apps only)." + if not token else + f" (It used the token for {key}: check it can read this repository.)") if tag and not re.fullmatch(TAG, tag): raise Refused(f"'{tag}' isn't a release tag (v and a version, like v1.2.0).") if not tag: @@ -834,8 +862,8 @@ def ensure_image(m, pull=False): raise Refused(f"The image {ref} isn't on this Core, and localhost/ images " "can't be pulled. coreapp deploy copies it here.") say(f"Pulling {ref}...") - registry = m["image"].split("/", 1)[0] - token = basic_auth(registry) + registry, owner = m["image"].split("/")[:2] + _, token = basic_auth(registry, owner) # The token goes in a docker config just for this pull, so the Core # keeps no docker login with tempfile.TemporaryDirectory(prefix="coreapps-docker-") as config: diff --git a/skills/coreapp/references/publishing.md b/skills/coreapp/references/publishing.md index 05d6dfb..f2d50b0 100644 --- a/skills/coreapp/references/publishing.md +++ b/skills/coreapp/references/publishing.md @@ -41,8 +41,10 @@ To check a release the way a Core's owner will get it: `version` has to match the tag. - Coreapps are often private: for a private repository and image, the Core needs a token for the host first, `coreapp login `, which asks - for a user name and a read-only token (repositories and packages). That's - the user's to enter, not yours. + for a user name and a read-only token (repositories and packages); or for + one owner there, `coreapp login /`, when that owner's + apps need another account's token. That's the user's to enter, not + yours. - On the same Core, `coreapp deploy` from the folder and `coreapp install` from the repository update the same app (same name, data and database). - A Core keeps running the image it installed, by digest: a release can't