| 1 | #!/usr/bin/env python3 |
| 2 | """Prepare owned Git repositories and preserve Shale account bindings.""" |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import re |
| 7 | import sqlite3 |
| 8 | import sys |
| 9 | |
| 10 | data = json.load(sys.stdin) |
| 11 | root = Path(data["hostRoot"]) |
| 12 | path = root / "data/astheno.shale.db" |
| 13 | webhooks = {} |
| 14 | if path.exists(): |
| 15 | domain = os.environ["SNOWGLOBE_DOMAIN"] |
| 16 | old, new = "auth." + domain + "/realms/master", "snowglobe." + domain |
| 17 | db = sqlite3.connect(path, timeout=30) |
| 18 | db.execute("BEGIN IMMEDIATE") |
| 19 | if db.execute("SELECT 1 FROM users old JOIN users new ON old.snowflake=new.snowflake WHERE old.provider=? AND new.provider=?", (old,new)).fetchone(): |
| 20 | raise ValueError("duplicate Shale identity; resolve it before moving the issuer") |
| 21 | count = db.execute("UPDATE users SET provider=? WHERE provider=?", (new,old)).rowcount |
| 22 | db.commit() |
| 23 | webhooks = dict(db.execute( |
| 24 | "SELECT uuid, name FROM repositories WHERE name IN ('snowcord', 'snowcord-server')" |
| 25 | )) |
| 26 | assert db.execute("PRAGMA quick_check").fetchone() == ("ok",) |
| 27 | db.close() |
| 28 | print("Moved", count, "existing Shale identity bindings to Snowglobe") |
| 29 | |
| 30 | executable = root / "data/shale-webhook" |
| 31 | source = Path(__file__).with_name("webhook.sh").read_bytes() |
| 32 | if not executable.exists() or executable.read_bytes() != source: |
| 33 | pending = executable.with_suffix(".pending") |
| 34 | pending.write_bytes(source) |
| 35 | if os.geteuid() == 0: |
| 36 | os.chown(pending, data["uid"], data["uid"]) |
| 37 | pending.replace(executable) |
| 38 | executable.chmod(0o750) |
| 39 | if os.geteuid() == 0: |
| 40 | os.chown(executable, data["uid"], data["uid"]) |
| 41 | |
| 42 | target = "/data/shale-webhook" |
| 43 | for config in (root / "repositories_owned").glob("*/config"): |
| 44 | original = text = config.read_text() |
| 45 | if not re.search(r"(?im)^\s*receivepack\s*=", text): |
| 46 | # Shale authorizes pushes before CGI; its own repository initializer enables this. |
| 47 | text += "\n[http]\n\treceivepack = true\n" |
| 48 | text = re.sub(r'(?m)\n+\[shale "snowglobe"\]\n(?:[ \t].*\n?)*', "\n", text) |
| 49 | name = webhooks.get(config.parent.name) |
| 50 | if name: |
| 51 | text += f'\n[shale "snowglobe"]\n\trepository = {name}\n' |
| 52 | if text != original: |
| 53 | config.write_text(text) |
| 54 | |
| 55 | hook = config.parent / "hooks/post-receive" |
| 56 | if name: |
| 57 | hook.parent.mkdir(exist_ok=True) |
| 58 | if os.path.lexists(hook): |
| 59 | if not hook.is_symlink() or os.readlink(hook) != target: |
| 60 | raise ValueError(f"refusing to replace existing hook: {hook}") |
| 61 | if not os.path.lexists(hook): |
| 62 | hook.symlink_to(target) |
| 63 | if os.geteuid() == 0: |
| 64 | os.lchown(hook, data["uid"], data["uid"]) |
| 65 | elif hook.is_symlink() and os.readlink(hook) == target: |
| 66 | hook.unlink() |