progmaweb
The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.
Clonegit clone https://git.progmasoft.com/progmaweb
Serve Progmasoft's Cargo registry at rustreg.progmasoft.com
- Commit
6f738b3b4707b7493c352593591a5636ab206348- Author
- Leitwolf11 ·
- Parents
2b582ca624e4
Changes
ops/README.md | 28 +++++++ ops/rustreg/nginx/rustreg.conf | 79 +++++++++++++++++++ ops/rustreg/publish-crate.py | 170 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 277 insertions(+) diff --git a/ops/README.md b/ops/README.md index b8eeac2..7a1ae73 100644 --- a/ops/README.md +++ b/ops/README.md @@ -334,6 +334,34 @@ ssh root@91.151.88.130 'chown -R root:root /srv/progmasoft-storage && restorecon The certificate of `progmasoft.com` must name `storage.progmasoft.com`. +## Cargo registry + +`rustreg.progmasoft.com` is Progmasoft's Cargo registry: a sparse index under `/index/` and the crates under +`/crates/<name>/<name>-<version>.crate`, files in `/srv/rustreg` served by `ops/rustreg/nginx/rustreg.conf` (installed as +`/etc/nginx/conf.d/rustreg.conf`). The index is served with `Cache-Control: no-cache`; a crate never changes, so it is +cached as immutable. Nothing on the server writes: there is no publish API, so no token can be stolen to publish. +A crate made by `cargo package` is published over SSH by `ops/rustreg/publish-crate.py`, which writes the index line +from the manifest Cargo normalized into the crate and refuses a version that is published already: + +```sh +semanage fcontext -a -t httpd_sys_content_t '/srv/rustreg(/.*)?' +install -d -m 0755 /srv/rustreg/index /srv/rustreg/crates +echo '{"dl":"https://rustreg.progmasoft.com/crates/{crate}/{crate}-{version}.crate"}' > /srv/rustreg/index/config.json +restorecon -R /srv/rustreg + +cargo package -p <crate> +python3 ops/rustreg/publish-crate.py target/package/<crate>-<version>.crate +``` + +A project that uses the registry names it in `.cargo/config.toml`: + +```toml +[registries.progmasoft] +index = "sparse+https://rustreg.progmasoft.com/index/" +``` + +The certificate of `progmasoft.com` must name `rustreg.progmasoft.com`. + ## Git mirrors `git.progmasoft.com` shows read-only mirrors of the public repositories under development, such as diff --git a/ops/rustreg/nginx/rustreg.conf b/ops/rustreg/nginx/rustreg.conf new file mode 100644 index 0000000..64754b0 --- /dev/null +++ b/ops/rustreg/nginx/rustreg.conf @@ -0,0 +1,79 @@ +# SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +# SPDX-License-Identifier: AGPL-3.0-or-later + +# https://rustreg.progmasoft.com: Progmasoft's Cargo registry. Cargo reads the +# sparse index under /index/ and downloads crates from /crates/; both are +# files in /srv/rustreg. Nothing here writes: crates are published over SSH by +# ops/rustreg/publish-crate.py. Installed as /etc/nginx/conf.d/rustreg.conf. + +server { + listen 80; + listen [::]:80; + server_name rustreg.progmasoft.com; + server_tokens off; + + location ^~ /.well-known/acme-challenge/ { + root /var/lib/letsencrypt; + default_type text/plain; + } + + location / { + return 308 https://rustreg.progmasoft.com$request_uri; + } +} + +server { + listen 443 ssl; + listen [::]:443 ssl; + http2 on; + server_name rustreg.progmasoft.com; + server_tokens off; + + ssl_certificate /etc/letsencrypt/live/progmasoft.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/progmasoft.com/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + + root /srv/rustreg; + autoindex off; + client_max_body_size 1k; + + add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Robots-Tag "noindex" always; + + # The index changes with every publication: Cargo asks again each time. + location ^~ /index/ { + limit_except GET { + deny all; + } + default_type text/plain; + charset utf-8; + types { + application/json json; + } + add_header Cache-Control "no-cache" always; + add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Robots-Tag "noindex" always; + try_files $uri =404; + } + + # A published crate never changes; a change is a new version. + location ~ "^/crates/[a-z0-9_-]+/[a-z0-9_-]+-[0-9A-Za-z.+-]+\.crate$" { + limit_except GET { + deny all; + } + types { } + default_type application/octet-stream; + expires 1y; + add_header Cache-Control "public, immutable" always; + add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Robots-Tag "noindex" always; + try_files $uri =404; + } + + location / { + return 404; + } +} diff --git a/ops/rustreg/publish-crate.py b/ops/rustreg/publish-crate.py new file mode 100644 index 0000000..20413b9 --- /dev/null +++ b/ops/rustreg/publish-crate.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +# SPDX-License-Identifier: AGPL-3.0-or-later + +"""Publishes a crate to Progmasoft's Cargo registry, rustreg.progmasoft.com. + +The registry has no publish API: its index and crates are files that nginx +serves. This script takes a .crate made by `cargo package`, reads the +manifest Cargo normalized into it, writes the line of the sparse index for +that version, and puts both on the server over SSH. A version that is in the +index already is refused: a published crate never changes. + + python3 publish-crate.py [--dry-run] [--host root@91.151.88.130] <file.crate> +""" + +import argparse +import hashlib +import json +import shlex +import subprocess +import sys +import tarfile +import tomllib + +REGISTRY_INDEX = "sparse+https://rustreg.progmasoft.com/index/" +CRATES_IO = "https://github.com/rust-lang/crates.io-index" +ROOT = "/srv/rustreg" + + +def index_path(name): + """The path of a crate's file in the index, as Cargo looks for it.""" + name = name.lower() + if len(name) == 1: + return f"1/{name}" + if len(name) == 2: + return f"2/{name}" + if len(name) == 3: + return f"3/{name[0]}/{name}" + return f"{name[0:2]}/{name[2:4]}/{name}" + + +def read_manifest(crate_path): + """The normalized Cargo.toml inside a .crate.""" + with tarfile.open(crate_path, "r:gz") as archive: + for member in archive.getmembers(): + parts = member.name.split("/") + if len(parts) == 2 and parts[1] == "Cargo.toml" and member.isfile(): + return tomllib.load(archive.extractfile(member)) + sys.exit(f"{crate_path}: no Cargo.toml") + + +def dependency(name, spec, kind, target): + """One dependency of the index line.""" + if isinstance(spec, str): + spec = {"version": spec} + if "path" in spec and "version" not in spec: + sys.exit(f"dependency {name} has a path but no version") + registry = spec.get("registry-index") + if registry is None: + registry = CRATES_IO + elif registry.rstrip("/") == REGISTRY_INDEX.rstrip("/"): + # A dependency of this registry names no registry. + registry = None + entry = { + "name": name, + "req": spec.get("version", "*"), + "features": spec.get("features", []), + "optional": spec.get("optional", False), + "default_features": spec.get("default-features", True), + "target": target, + "kind": kind, + "registry": registry, + } + if "package" in spec: + # A renamed dependency: `name` is the name the crate uses it by and + # `package` the crate it is. + entry["package"] = spec["package"] + return entry + + +def dependencies(manifest): + """Every dependency, with its kind and its target.""" + tables = [(manifest, None)] + for target, table in manifest.get("target", {}).items(): + tables.append((table, target)) + result = [] + for table, target in tables: + for key, kind in (("dependencies", "normal"), ("build-dependencies", "build"), ("dev-dependencies", "dev")): + for name, spec in table.get(key, {}).items(): + result.append(dependency(name, spec, kind, target)) + return sorted(result, key=lambda entry: (entry["name"], entry["kind"])) + + +def index_line(manifest, checksum): + """The line of the sparse index for this version.""" + package = manifest["package"] + features = manifest.get("features", {}) + plain = {key: value for key, value in features.items() if not any("dep:" in item or "?/" in item for item in value)} + extended = {key: value for key, value in features.items() if key not in plain} + line = { + "name": package["name"], + "vers": package["version"], + "deps": dependencies(manifest), + "cksum": checksum, + "features": plain, + "yanked": False, + "links": package.get("links"), + "rust_version": package.get("rust-version"), + "v": 2 if extended else 1, + } + if extended: + line["features2"] = extended + return json.dumps(line, separators=(",", ":"), sort_keys=True) + + +def run(host, script, data=None): + """Runs a shell script on the server, as one argument of ssh.""" + return subprocess.run(["ssh", host, script], input=data, check=True, capture_output=True) + + +def main(): + parser = argparse.ArgumentParser(description="Publish a .crate to rustreg.progmasoft.com.") + parser.add_argument("crate", help="the file cargo package wrote") + parser.add_argument("--host", default="root@91.151.88.130") + parser.add_argument("--dry-run", action="store_true", help="print the index line and change nothing") + arguments = parser.parse_args() + + with open(arguments.crate, "rb") as file: + data = file.read() + checksum = hashlib.sha256(data).hexdigest() + manifest = read_manifest(arguments.crate) + name, version = manifest["package"]["name"], manifest["package"]["version"] + publish = manifest["package"].get("publish") + if publish is not None and publish is not True and "progmasoft" not in (publish or []): + sys.exit(f"{name} is not published to the progmasoft registry") + line = index_line(manifest, checksum) + print(line) + if arguments.dry_run: + return + + entry = f"{ROOT}/index/{index_path(name)}" + crate = f"{ROOT}/crates/{name}/{name}-{version}.crate" + # The server checks that the version is new, writes the crate, then the + # index line, each through a temporary file and a rename. + script = f"""set -eu +entry={shlex.quote(entry)} +crate={shlex.quote(crate)} +line={shlex.quote(line)} +if [ -e "$crate" ] || {{ [ -e "$entry" ] && grep -q '"vers":{json.dumps(version)},' "$entry"; }}; then + echo "{name} {version} is published already" >&2 + exit 3 +fi +install -d -m 0755 "$(dirname "$entry")" "$(dirname "$crate")" +cat > "$crate.tmp" +echo {shlex.quote(checksum)} " $crate.tmp" | sha256sum -c --quiet - +mv "$crate.tmp" "$crate" +{{ [ -e "$entry" ] && cat "$entry"; printf '%s\\n' "$line"; }} > "$entry.tmp" +mv "$entry.tmp" "$entry" +chmod 0644 "$crate" "$entry" +restorecon -R {ROOT}/index {ROOT}/crates +""" + try: + run(arguments.host, script, data) + except subprocess.CalledProcessError as error: + sys.exit(error.stderr.decode() or f"publishing failed with {error.returncode}") + print(f"published {name} {version}") + + +if __name__ == "__main__": + main()